author | František Kučera <franta-hg@frantovo.cz> |
Thu, 04 Jun 2020 13:24:17 +0200 | |
branch | v_0 |
changeset 48 | c83119110c7b |
parent 47 | 428c278af4be |
child 54 | bc6e11cccdf4 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
10
7da7173d84b0
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
7 |
* the Free Software Foundation, version 3. |
0 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <vector> |
|
20 |
||
21 |
#include <relpipe/writer/typedefs.h> |
|
22 |
#include <relpipe/cli/CLI.h> |
|
23 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
24 |
||
25 |
#include "Configuration.h" |
|
26 |
||
27 |
namespace relpipe { |
|
28 |
namespace tr { |
|
29 |
namespace sql { |
|
30 |
||
31 |
class CLIParser { |
|
32 |
private: |
|
33 |
||
34 |
// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper |
|
35 |
||
36 |
string_t readNext(std::vector<string_t> arguments, int& i) { |
|
37 |
if (i < arguments.size()) return arguments[i++]; |
|
38 |
else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
39 |
} |
|
40 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
void addQuery(Configuration& c, Statement& currentQuery) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
if (currentQuery.sql.size()) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
c.statements.push_back(currentQuery); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
currentQuery = Statement(); |
0 | 45 |
} |
46 |
} |
|
47 |
||
48 |
public: |
|
49 |
||
50 |
static const string_t OPTION_RELATION; |
|
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
51 |
static const string_t OPTION_TYPE_CAST; |
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
static const string_t OPTION_PARAMETER; |
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
53 |
static const string_t OPTION_COPY; |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
54 |
static const string_t OPTION_COPY_RENAMED; |
37
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
55 |
static const string_t OPTION_DATA_SOURCE_NAME; |
47
428c278af4be
rename option --data-source-url to --data-source-string
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
56 |
static const string_t OPTION_DATA_SOURCE_STRING; |
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
57 |
static const string_t OPTION_LIST_DATA_SOURCES; |
0 | 58 |
|
59 |
Configuration parse(const std::vector<string_t>& arguments) { |
|
60 |
Configuration c; |
|
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
Statement currentQuery; |
0 | 62 |
|
63 |
for (int i = 0; i < arguments.size();) { |
|
64 |
string_t option = readNext(arguments, i); |
|
65 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
if (option == OPTION_RELATION) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
addQuery(c, currentQuery); // previous relation |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
currentQuery.relation = readNext(arguments, i); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
currentQuery.sql = readNext(arguments, i); |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
70 |
} else if (option == OPTION_TYPE_CAST) { |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
71 |
TypeCast typeCast; |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
72 |
typeCast.name = readNext(arguments, i); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
73 |
typeCast.type = readNext(arguments, i); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
74 |
currentQuery.typeCasts.push_back(typeCast); |
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
} else if (option == OPTION_PARAMETER) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
Parameter parameter; |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
parameter.value = readNext(arguments, i); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
currentQuery.parameters.push_back(parameter); |
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
79 |
} else if (option == OPTION_COPY) { |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
80 |
c.copyRelations.push_back({readNext(arguments, i), L"", false}); |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
81 |
} else if (option == OPTION_COPY_RENAMED) { |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
82 |
c.copyRelations.push_back({readNext(arguments, i), readNext(arguments, i), true}); |
37
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
83 |
} else if (option == OPTION_DATA_SOURCE_NAME) { |
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
84 |
c.dataSourceName = readNext(arguments, i); |
47
428c278af4be
rename option --data-source-url to --data-source-string
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
85 |
} else if (option == OPTION_DATA_SOURCE_STRING) { |
428c278af4be
rename option --data-source-url to --data-source-string
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
86 |
c.dataSourceString = readNext(arguments, i); |
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
87 |
} else if (option == OPTION_LIST_DATA_SOURCES) { |
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
88 |
c.listDataSources = true; |
0 | 89 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
90 |
} |
|
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
addQuery(c, currentQuery); // last relation |
0 | 92 |
|
47
428c278af4be
rename option --data-source-url to --data-source-string
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
93 |
if (c.dataSourceName.size() && c.dataSourceString.size()) throw relpipe::cli::RelpipeCLIException(L"Specify data source name or data source string, not both.", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
37
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
94 |
|
0 | 95 |
return c; |
96 |
} |
|
97 |
||
98 |
virtual ~CLIParser() { |
|
99 |
} |
|
100 |
}; |
|
101 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
102 |
|
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
103 |
// relpipe-tr-sql --relation "tabulka" "SELECT * FROM fstab WHERE id = ?" --parameter "123" |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
104 |
// TODO: --type string/integer/boolean (default is string) |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
105 |
|
0 | 106 |
const string_t CLIParser::OPTION_RELATION = L"--relation"; |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
107 |
const string_t CLIParser::OPTION_TYPE_CAST = L"--type-cast"; |
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
108 |
const string_t CLIParser::OPTION_PARAMETER = L"--parameter"; |
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
109 |
const string_t CLIParser::OPTION_COPY = L"--copy"; |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
110 |
const string_t CLIParser::OPTION_COPY_RENAMED = L"--copy-renamed"; |
34
24c05e69d68f
add ODBC library and --list-data-sources mode (DSN)
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
111 |
const string_t CLIParser::OPTION_LIST_DATA_SOURCES = L"--list-data-sources"; |
37
3de41719d7eb
add options --data-source-name and --data-source-url for custom datasource; drop options --file and --file-keep
František Kučera <franta-hg@frantovo.cz>
parents:
34
diff
changeset
|
112 |
const string_t CLIParser::OPTION_DATA_SOURCE_NAME = L"--data-source-name"; |
47
428c278af4be
rename option --data-source-url to --data-source-string
František Kučera <franta-hg@frantovo.cz>
parents:
37
diff
changeset
|
113 |
const string_t CLIParser::OPTION_DATA_SOURCE_STRING = L"--data-source-string"; |
0 | 114 |
|
115 |
} |
|
116 |
} |
|
117 |
} |