author | František Kučera <franta-hg@frantovo.cz> |
Mon, 29 Jul 2019 19:49:12 +0200 | |
branch | v_0 |
changeset 2 | 362f2689cb87 |
parent 0 | c205f5d06418 |
child 5 | cbc7817a3346 |
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 |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
#pragma once |
|
19 |
||
20 |
#include <vector> |
|
21 |
||
22 |
#include <relpipe/writer/typedefs.h> |
|
23 |
#include <relpipe/cli/CLI.h> |
|
24 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
25 |
||
26 |
#include "Configuration.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace tr { |
|
30 |
namespace sql { |
|
31 |
||
32 |
class CLIParser { |
|
33 |
private: |
|
34 |
||
35 |
// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper |
|
36 |
||
37 |
string_t readNext(std::vector<string_t> arguments, int& i) { |
|
38 |
if (i < arguments.size()) return arguments[i++]; |
|
39 |
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); |
|
40 |
} |
|
41 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
void addQuery(Configuration& c, Statement& currentQuery) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
if (currentQuery.sql.size()) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
c.statements.push_back(currentQuery); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
currentQuery = Statement(); |
0 | 46 |
} |
47 |
} |
|
48 |
||
49 |
public: |
|
50 |
||
51 |
static const string_t OPTION_RELATION; |
|
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; |
0 | 53 |
|
54 |
Configuration parse(const std::vector<string_t>& arguments) { |
|
55 |
Configuration c; |
|
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
Statement currentQuery; |
0 | 57 |
|
58 |
for (int i = 0; i < arguments.size();) { |
|
59 |
string_t option = readNext(arguments, i); |
|
60 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
if (option == OPTION_RELATION) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
addQuery(c, currentQuery); // previous relation |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
currentQuery.relation = readNext(arguments, i); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
currentQuery.sql = readNext(arguments, i); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
} else if (option == OPTION_PARAMETER) { |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
Parameter parameter; |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
parameter.value = readNext(arguments, i); |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
currentQuery.parameters.push_back(parameter); |
0 | 69 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
70 |
} |
|
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
addQuery(c, currentQuery); // last relation |
0 | 72 |
|
73 |
return c; |
|
74 |
} |
|
75 |
||
76 |
virtual ~CLIParser() { |
|
77 |
} |
|
78 |
}; |
|
79 |
||
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
|
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
// 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
|
82 |
// TODO: --file db.sqlite --keep-file |
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
// 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
|
84 |
|
0 | 85 |
const string_t CLIParser::OPTION_RELATION = L"--relation"; |
2
362f2689cb87
configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
const string_t CLIParser::OPTION_PARAMETER = L"--parameter"; |
0 | 87 |
|
88 |
} |
|
89 |
} |
|
90 |
} |