src/CLICommand.h
branchv_0
changeset 43 3c8ea5dcf793
parent 39 c3672791b2e8
child 44 dd7094457e44
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLICommand.h	Tue Sep 22 21:00:30 2020 +0200
@@ -0,0 +1,74 @@
+/**
+ * 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 <codecvt>
+#include <memory>
+#include <iostream>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace cli {
+
+class CLICommand {
+private:
+
+	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
+
+	void processRelation(RelationConfiguration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
+		// Write header / metadata:
+		std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
+		for (AttributeRecipe ar : configuration.attributes) attributesMetadata.push_back({ar.name, ar.type});
+		writer->startRelation(configuration.relation, attributesMetadata, true);
+
+		// Write records from CLI:
+		for (auto value : configuration.values) writer->writeAttribute(value);
+
+		// Write records from STDIN:
+		if (configuration.valueStream) {
+			std::stringstream rawValue;
+			while (true) {
+				char ch = 0;
+				configuration.valueStream->get(ch);
+				if (ch == 0 && configuration.valueStream->good()) {
+					writer->writeAttribute(convertor.from_bytes(rawValue.str()));
+					rawValue.str("");
+					rawValue.clear();
+				} else if (configuration.valueStream->good()) {
+					rawValue << ch;
+				} else if (configuration.valueStream->eof()) {
+					return;
+				} else {
+					throw relpipe::cli::RelpipeCLIException(L"Error while reading values from the input stream.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception
+				}
+			}
+		}
+
+	}
+
+public:
+
+	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
+		for (RelationConfiguration& relationConfiguration : configuration.relationConfigurations) processRelation(relationConfiguration, writer);
+	}
+};
+
+}
+}
+}