add --output-attribute so output relation can have different attributes than the input relation v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 05 Feb 2019 00:06:44 +0100
branchv_0
changeset 12 7977c1bdba1f
parent 11 9603e64324bc
child 13 c9fece435aa2
add --output-attribute so output relation can have different attributes than the input relation
src/CLIParser.h
src/Configuration.h
src/GuileHandler.h
--- a/src/CLIParser.h	Sun Feb 03 21:53:10 2019 +0100
+++ b/src/CLIParser.h	Tue Feb 05 00:06:44 2019 +0100
@@ -32,7 +32,7 @@
 class CLIParser {
 private:
 
-	// TODO: move common methods/classes to relpipe-lib-cli
+	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
 
 	string_t readNext(std::vector<string_t> arguments, int& i) {
 		if (i < arguments.size()) return arguments[i++];
@@ -52,9 +52,18 @@
 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value of option: " + optionName + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 	}
 
+	relpipe::writer::TypeId parseTypeId(const string_t& value) {
+		using t = relpipe::writer::TypeId;
+		if (value == L"string") return t::STRING;
+		else if (value == L"integer") return t::INTEGER;
+		else if (value == L"boolean") return t::BOOLEAN;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
 public:
 
 	static const string_t OPTION_RELATION;
+	static const string_t OPTION_OUTPUT_ATTRIBUTE;
 	static const string_t OPTION_BEFORE_RECORDS;
 	static const string_t OPTION_AFTER_RECORDS;
 	static const string_t OPTION_FOR_EACH;
@@ -78,6 +87,11 @@
 				else if (option == OPTION_RELATION) {
 					addRelation(c, currentRelation); // previous relation
 					currentRelation.relation = readNext(arguments, i);
+				} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
+					relpipe::writer::AttributeMetadata attribute;
+					attribute.attributeName = readNext(arguments, i);
+					attribute.typeId = parseTypeId(readNext(arguments, i));
+					currentRelation.writerMetadata.push_back(attribute);
 				} else if (option == OPTION_DEFINE) {
 					DefinitionRecipe definition;
 					definition.name = readNext(arguments, i);
@@ -99,6 +113,7 @@
 };
 
 const string_t CLIParser::OPTION_RELATION = L"--relation";
+const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
 const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
 const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
 const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
--- a/src/Configuration.h	Sun Feb 03 21:53:10 2019 +0100
+++ b/src/Configuration.h	Tue Feb 05 00:06:44 2019 +0100
@@ -70,7 +70,6 @@
 
 	// TODO: --define – global definitions (for all relations)?
 	// TODO: --relation … --drop – will execute --for-each, but no output will be generated for this relation
-	// TODO: --relation … --output-attribute 'name' 'type' – output relation will have different attributes than the input relation
 	// TODO: --create t2 3 a integer b boolean '…guile…' – allow creating new relations? Or allow calling startRelation() and attribute() from Guile?
 
 	virtual ~Configuration() {
--- a/src/GuileHandler.h	Sun Feb 03 21:53:10 2019 +0100
+++ b/src/GuileHandler.h	Tue Feb 05 00:06:44 2019 +0100
@@ -190,13 +190,6 @@
 			for (DefinitionRecipe definition : currentRelationConfiguration->definitions) undefineGuileVariable(definition.name);
 		}
 		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
-		currentReaderMetadata = attributes;
-		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
-		// TODO: allow structural changes during transformation
-		currentWriterMetadata.clear();
-		for (AttributeMetadata readerMetadata : attributes) {
-			currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
-		}
 
 		currentRelationConfiguration = nullptr;
 		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
@@ -207,6 +200,15 @@
 			}
 		}
 
+		currentReaderMetadata = attributes;
+		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
+		currentWriterMetadata.clear();
+		if (currentRelationConfiguration && currentRelationConfiguration->writerMetadata.size()) {
+			currentWriterMetadata = currentRelationConfiguration->writerMetadata;
+		} else {
+			for (AttributeMetadata readerMetadata : attributes) currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
+		}
+
 		relationalWriter->startRelation(name, currentWriterMetadata, true);
 
 		if (currentRelationConfiguration) {