--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh Wed Sep 23 17:17:39 2020 +0200
@@ -0,0 +1,39 @@
+# Relational pipes
+# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+_relpipe_out_csv_completion() {
+ local w0 w1 w2 w3
+
+ COMPREPLY=()
+ w0=${COMP_WORDS[COMP_CWORD]}
+ w1=${COMP_WORDS[COMP_CWORD-1]}
+ w2=${COMP_WORDS[COMP_CWORD-2]}
+ w3=${COMP_WORDS[COMP_CWORD-3]}
+
+ WRITE_HEADER=(
+ "true"
+ "false"
+ )
+
+ if [[ "$w1" == "--write-header" ]]; then COMPREPLY=($(compgen -W "${WRITE_HEADER[*]}" -- "$w0"))
+ else
+ OPTIONS=(
+ "--write-header"
+ )
+ COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+ fi
+}
+
+complete -F _relpipe_out_csv_completion relpipe-out-csv
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h Wed Sep 23 17:17:39 2020 +0200
@@ -0,0 +1,75 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/reader/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace out {
+namespace csv {
+
+class CLIParser {
+private:
+
+ relpipe::reader::string_t readNext(const std::vector<relpipe::reader::string_t>& arguments, int& i) {
+ if (i < arguments.size()) return arguments[i++];
+ else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+ }
+
+ /**
+ * TODO: use a common method
+ */
+ bool parseBoolean(const relpipe::reader::string_t& value) {
+ if (value == L"true") return true;
+ else if (value == L"false") return false;
+ else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+ }
+
+public:
+
+ static const relpipe::reader::string_t OPTION_WRITE_HEADER;
+
+ Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
+ Configuration c;
+
+ for (int i = 0; i < arguments.size();) {
+ relpipe::reader::string_t option = readNext(arguments, i);
+
+ if (option == OPTION_WRITE_HEADER) {
+ c.writeHeader = parseBoolean(readNext(arguments, i));
+ } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+ }
+
+ return c;
+ }
+
+ virtual ~CLIParser() {
+ }
+};
+
+const relpipe::reader::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
+
+}
+}
+}
--- a/src/CSVHandler.h Sat Jun 06 01:50:44 2020 +0200
+++ b/src/CSVHandler.h Wed Sep 23 17:17:39 2020 +0200
@@ -29,6 +29,7 @@
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
+#include "Configuration.h"
#include "RelpipeCSVWriterException.h"
namespace relpipe {
@@ -42,19 +43,20 @@
class CSVHandler : public RelationalReaderStringHandler {
private:
std::ostream& output;
+ Configuration& configuration;
const char QUOTE = '"';
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML?
std::vector<AttributeMetadata> firstAttributes;
integer_t valueCount = 0;
public:
- CSVHandler(std::ostream& output) : output(output) {
+ CSVHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
}
void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
if (firstAttributes.empty()) {
firstAttributes = attributes;
- for (auto attr : attributes) attribute(attr.getAttributeName());
+ if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
} else {
// TODO: UNION ALL if data types and attribute count matches
throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format.");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h Wed Sep 23 17:17:39 2020 +0200
@@ -0,0 +1,39 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/reader/typedefs.h>
+
+
+namespace relpipe {
+namespace out {
+namespace csv {
+
+class Configuration {
+public:
+ relpipe::reader::boolean_t writeHeader = true;
+
+ virtual ~Configuration() {
+ }
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/relpipe-out-csv.cpp Sat Jun 06 01:50:44 2020 +0200
+++ b/src/relpipe-out-csv.cpp Wed Sep 23 17:17:39 2020 +0200
@@ -25,6 +25,8 @@
#include <relpipe/reader/RelationalReader.h>
#include <relpipe/reader/RelpipeReaderException.h>
+#include "Configuration.h"
+#include "CLIParser.h"
#include "CSVHandler.h"
#include "RelpipeCSVWriterException.h"
@@ -39,8 +41,10 @@
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
try {
+ CLIParser cliParser;
+ Configuration configuration = cliParser.parse(cli.arguments());
std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
- CSVHandler handler(std::cout);
+ CSVHandler handler(std::cout, configuration);
reader->addHandler(&handler);
reader->process();