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 |
/**
|
|
33 |
* This tr-sql CLI options are inspired by tr-guile options.
|
|
34 |
* These configurations are independent and might diverge, but when possible, they should share same option names and same logic for common parts.
|
|
35 |
*/
|
|
36 |
class CLIParser {
|
|
37 |
private:
|
|
38 |
|
|
39 |
// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
|
|
40 |
|
|
41 |
string_t readNext(std::vector<string_t> arguments, int& i) {
|
|
42 |
if (i < arguments.size()) return arguments[i++];
|
|
43 |
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);
|
|
44 |
}
|
|
45 |
|
|
46 |
void addRelation(Configuration& c, RelationConfiguration& currentRelation) {
|
|
47 |
if (currentRelation.relation.size()) {
|
|
48 |
c.relationConfigurations.push_back(currentRelation);
|
|
49 |
currentRelation = RelationConfiguration();
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
relpipe::writer::TypeId parseTypeId(const string_t& value) {
|
|
54 |
using t = relpipe::writer::TypeId;
|
|
55 |
if (value == L"string") return t::STRING;
|
|
56 |
else if (value == L"integer") return t::INTEGER;
|
|
57 |
else if (value == L"boolean") return t::BOOLEAN;
|
|
58 |
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse TypeId: " + value, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
59 |
}
|
|
60 |
|
|
61 |
public:
|
|
62 |
|
|
63 |
static const string_t OPTION_RELATION;
|
|
64 |
static const string_t OPTION_OUTPUT_ATTRIBUTE;
|
|
65 |
static const string_t OPTION_INPUT_ATTRIBUTES_APPEND;
|
|
66 |
static const string_t OPTION_INPUT_ATTRIBUTES_PREPEND;
|
|
67 |
static const string_t OPTION_DEBUG_VARIABLE_MAPPING;
|
|
68 |
static const string_t OPTION_BEFORE_RECORDS;
|
|
69 |
static const string_t OPTION_AFTER_RECORDS;
|
|
70 |
static const string_t OPTION_FOR_EACH;
|
|
71 |
static const string_t OPTION_DROP;
|
|
72 |
static const string_t OPTION_DEFINE;
|
|
73 |
|
|
74 |
Configuration parse(const std::vector<string_t>& arguments) {
|
|
75 |
Configuration c;
|
|
76 |
RelationConfiguration currentRelation;
|
|
77 |
|
|
78 |
for (int i = 0; i < arguments.size();) {
|
|
79 |
string_t option = readNext(arguments, i);
|
|
80 |
|
|
81 |
if (option == OPTION_BEFORE_RECORDS) currentRelation.sqlBeforeRecords = readNext(arguments, i);
|
|
82 |
else if (option == OPTION_AFTER_RECORDS) currentRelation.sqlAfterRecords = readNext(arguments, i);
|
|
83 |
else if (option == OPTION_FOR_EACH) currentRelation.sqlForEach = readNext(arguments, i);
|
|
84 |
else if (option == OPTION_DROP) currentRelation.drop = true;
|
|
85 |
else if (option == OPTION_INPUT_ATTRIBUTES_APPEND) currentRelation.inputAttributesAppend = true;
|
|
86 |
else if (option == OPTION_INPUT_ATTRIBUTES_PREPEND) currentRelation.inputAttributesPrepend = true;
|
|
87 |
else if (option == OPTION_DEBUG_VARIABLE_MAPPING) currentRelation.debugVariableMapping = true;
|
|
88 |
else if (option == OPTION_RELATION) {
|
|
89 |
addRelation(c, currentRelation); // previous relation
|
|
90 |
currentRelation.relation = readNext(arguments, i);
|
|
91 |
} else if (option == OPTION_OUTPUT_ATTRIBUTE) {
|
|
92 |
relpipe::writer::AttributeMetadata attribute;
|
|
93 |
attribute.attributeName = readNext(arguments, i);
|
|
94 |
attribute.typeId = parseTypeId(readNext(arguments, i));
|
|
95 |
currentRelation.writerMetadata.push_back(attribute);
|
|
96 |
} else if (option == OPTION_DEFINE) {
|
|
97 |
DefinitionRecipe definition;
|
|
98 |
definition.name = readNext(arguments, i);
|
|
99 |
definition.type = readNext(arguments, i);
|
|
100 |
definition.value = readNext(arguments, i);
|
|
101 |
if (currentRelation.relation.size()) currentRelation.definitions.push_back(definition);
|
|
102 |
else c.definitions.push_back(definition);
|
|
103 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
|
|
104 |
}
|
|
105 |
addRelation(c, currentRelation); // last relation
|
|
106 |
|
|
107 |
return c;
|
|
108 |
}
|
|
109 |
|
|
110 |
virtual ~CLIParser() {
|
|
111 |
}
|
|
112 |
};
|
|
113 |
|
|
114 |
const string_t CLIParser::OPTION_RELATION = L"--relation";
|
|
115 |
const string_t CLIParser::OPTION_OUTPUT_ATTRIBUTE = L"--output-attribute";
|
|
116 |
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_APPEND = L"--input-attributes-append";
|
|
117 |
const string_t CLIParser::OPTION_INPUT_ATTRIBUTES_PREPEND = L"--input-attributes-prepend";
|
|
118 |
const string_t CLIParser::OPTION_DEBUG_VARIABLE_MAPPING = L"--debug-variable-mapping";
|
|
119 |
const string_t CLIParser::OPTION_BEFORE_RECORDS = L"--before-records";
|
|
120 |
const string_t CLIParser::OPTION_AFTER_RECORDS = L"--after-records";
|
|
121 |
const string_t CLIParser::OPTION_FOR_EACH = L"--for-each";
|
|
122 |
const string_t CLIParser::OPTION_DROP = L"--drop";
|
|
123 |
const string_t CLIParser::OPTION_DEFINE = L"--define";
|
|
124 |
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|