rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 26 Oct 2019 19:28:39 +0200
branchv_0
changeset 16 3c51a2c32c86
parent 15 0ecde5272f8e
child 17 b3c07fb178be
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
src/CLIParser.h
src/Configuration.h
src/SqlHandler.h
--- a/src/CLIParser.h	Sat Oct 26 17:45:37 2019 +0200
+++ b/src/CLIParser.h	Sat Oct 26 19:28:39 2019 +0200
@@ -49,7 +49,8 @@
 
 	static const string_t OPTION_RELATION;
 	static const string_t OPTION_PARAMETER;
-	static const string_t OPTION_DUMP;
+	static const string_t OPTION_COPY;
+	static const string_t OPTION_COPY_RENAMED;
 	static const string_t OPTION_FILE;
 	static const string_t OPTION_KEEP_FILE;
 
@@ -68,8 +69,10 @@
 				Parameter parameter;
 				parameter.value = readNext(arguments, i);
 				currentQuery.parameters.push_back(parameter);
-			} else if (option == OPTION_DUMP) {
-				c.dumpRelations = readNext(arguments, i);
+			} else if (option == OPTION_COPY) {
+				c.copyRelations.push_back({readNext(arguments, i), L"", false});
+			} else if (option == OPTION_COPY_RENAMED) {
+				c.copyRelations.push_back({readNext(arguments, i), readNext(arguments, i), true});
 			} else if (option == OPTION_FILE) {
 				c.file = readNext(arguments, i);
 			} else if (option == OPTION_KEEP_FILE) {
@@ -95,7 +98,8 @@
 
 const string_t CLIParser::OPTION_RELATION = L"--relation";
 const string_t CLIParser::OPTION_PARAMETER = L"--parameter";
-const string_t CLIParser::OPTION_DUMP = L"--dump";
+const string_t CLIParser::OPTION_COPY = L"--copy";
+const string_t CLIParser::OPTION_COPY_RENAMED = L"--copy-renamed";
 const string_t CLIParser::OPTION_FILE = L"--file";
 const string_t CLIParser::OPTION_KEEP_FILE = L"--keep-file";
 
--- a/src/Configuration.h	Sat Oct 26 17:45:37 2019 +0200
+++ b/src/Configuration.h	Sat Oct 26 19:28:39 2019 +0200
@@ -58,6 +58,25 @@
 	Automatic
 };
 
+/**
+ * Allows copying relations from the input stream to the output stream.
+ * Such relations are specified by a regular expression.
+ * We can also specify the regex replacement string and rename such relations. 
+ */
+class CopyRelations {
+public:
+
+	CopyRelations(const relpipe::writer::string_t pattern, const relpipe::writer::string_t replacement, const relpipe::writer::boolean_t replace) : pattern(pattern), replacement(replacement), replace(replace) {
+	}
+
+	virtual ~CopyRelations() {
+	}
+
+	const relpipe::writer::string_t pattern;
+	const relpipe::writer::string_t replacement;
+	const relpipe::writer::boolean_t replace;
+};
+
 class Configuration {
 public:
 
@@ -80,13 +99,13 @@
 	 * SQL script to be executed before the relational input.
 	 */
 	std::wistream* sqlBeforeRelational = nullptr;
-	
+
 	/**
 	 * SQL script to be executed after the relational input.
 	 */
 	std::wistream* sqlAfterRelational = nullptr;
 
-	std::wstring dumpRelations;
+	std::vector<CopyRelations> copyRelations;
 
 	virtual ~Configuration() {
 	}
--- a/src/SqlHandler.h	Sat Oct 26 17:45:37 2019 +0200
+++ b/src/SqlHandler.h	Sat Oct 26 19:28:39 2019 +0200
@@ -202,8 +202,8 @@
 		return relations;
 	}
 
-	void dumpRelations() {
-		std::wregex pattern(configuration.dumpRelations);
+	void copyRelations(const CopyRelations& copy) {
+		std::wregex pattern(copy.pattern);
 		for (string_t relation : getAllRelations()) {
 			if (regex_match(relation, pattern)) {
 				std::wstringstream select;
@@ -211,7 +211,7 @@
 				writeIdentifier(select, relation);
 
 				Statement statement;
-				statement.relation = relation;
+				statement.relation = copy.replace ? regex_replace(relation, pattern, copy.replacement) : relation;
 				statement.sql = select.str();
 				processStatement(statement);
 			}
@@ -334,7 +334,7 @@
 		processSqlInput(configuration.sqlAfterRelational);
 
 		// pass-through some relations:
-		if (configuration.dumpRelations.size()) dumpRelations();
+		for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy);
 
 		// delete or keep the file:
 		if (configuration.file.size()) {