add --dump option: allow pass through of relation specified by a regular expression v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 25 Oct 2019 12:33:00 +0200
branchv_0
changeset 14 eacacf060755
parent 13 19580b27ade2
child 15 0ecde5272f8e
add --dump option: allow pass through of relation specified by a regular expression
src/CLIParser.h
src/Configuration.h
src/SqlHandler.h
--- a/src/CLIParser.h	Tue Oct 22 16:05:36 2019 +0200
+++ b/src/CLIParser.h	Fri Oct 25 12:33:00 2019 +0200
@@ -49,6 +49,7 @@
 
 	static const string_t OPTION_RELATION;
 	static const string_t OPTION_PARAMETER;
+	static const string_t OPTION_DUMP;
 	static const string_t OPTION_FILE;
 	static const string_t OPTION_KEEP_FILE;
 
@@ -67,6 +68,8 @@
 				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_FILE) {
 				c.file = readNext(arguments, i);
 			} else if (option == OPTION_KEEP_FILE) {
@@ -92,6 +95,7 @@
 
 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_FILE = L"--file";
 const string_t CLIParser::OPTION_KEEP_FILE = L"--keep-file";
 
--- a/src/Configuration.h	Tue Oct 22 16:05:36 2019 +0200
+++ b/src/Configuration.h	Fri Oct 25 12:33:00 2019 +0200
@@ -75,6 +75,8 @@
 	KeepFile keepFile = KeepFile::Automatic;
 
 	std::vector<Statement> statements;
+	
+	std::wstring dumpRelations;
 
 	virtual ~Configuration() {
 	}
--- a/src/SqlHandler.h	Tue Oct 22 16:05:36 2019 +0200
+++ b/src/SqlHandler.h	Fri Oct 25 12:33:00 2019 +0200
@@ -19,6 +19,7 @@
 #include <memory>
 #include <string>
 #include <sstream>
+#include <regex>
 #include <vector>
 #include <locale>
 #include <codecvt>
@@ -170,6 +171,29 @@
 		}
 	}
 
+	std::vector<string_t> getAllRelations() {
+		std::vector<string_t> relations;
+		std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement("SELECT name FROM sqlite_master WHERE type IN ('table', 'view')"));
+		while (prepared->next()) relations.push_back(convertor.from_bytes(prepared->getString(0)));
+		return relations;
+	}
+
+	void dumpRelations() {
+		std::wregex pattern(configuration.dumpRelations);
+		for (string_t relation : getAllRelations()) {
+			if (regex_match(relation, pattern)) {
+				std::wstringstream select;
+				select << L"SELECT * FROM ";
+				writeIdentifier(select, relation);
+
+				Statement statement;
+				statement.relation = relation;
+				statement.sql = select.str();
+				processStatement(statement);
+			}
+		}
+	}
+
 	relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) {
 		if (typeId == relpipe::reader::TypeId::BOOLEAN) return L"integer"; // TODO: map selected values back to booleans or allow optional storage as string 
 		else if (typeId == relpipe::reader::TypeId::INTEGER) return L"integer";
@@ -279,6 +303,9 @@
 		// run the transformation – process all statements:
 		for (const Statement& statement : configuration.statements) processStatement(statement);
 
+		// pass-through some relations:
+		if (configuration.dumpRelations.size()) dumpRelations();
+
 		// delete or keep the file:
 		if (configuration.file.size()) {
 			if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) {