# HG changeset patch # User František Kučera # Date 1571999580 -7200 # Node ID eacacf06075568c430e9507e7e72c08b8c40bb36 # Parent 19580b27ade22c4b7b71f357ee4f1e0aa61998a1 add --dump option: allow pass through of relation specified by a regular expression diff -r 19580b27ade2 -r eacacf060755 src/CLIParser.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"; diff -r 19580b27ade2 -r eacacf060755 src/Configuration.h --- 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 statements; + + std::wstring dumpRelations; virtual ~Configuration() { } diff -r 19580b27ade2 -r eacacf060755 src/SqlHandler.h --- 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 #include #include +#include #include #include #include @@ -170,6 +171,29 @@ } } + std::vector getAllRelations() { + std::vector relations; + std::unique_ptr 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)) {