|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 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, version 3 of the License. |
|
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 <codecvt> |
|
20 #include <memory> |
|
21 #include <iostream> |
|
22 |
|
23 #include "Configuration.h" |
|
24 |
|
25 namespace relpipe { |
|
26 namespace in { |
|
27 namespace cli { |
|
28 |
|
29 class CLICommand { |
|
30 private: |
|
31 |
|
32 std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default |
|
33 |
|
34 void processRelation(RelationConfiguration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
|
35 // Write header / metadata: |
|
36 std::vector<relpipe::writer::AttributeMetadata> attributesMetadata; |
|
37 for (AttributeRecipe ar : configuration.attributes) attributesMetadata.push_back({ar.name, ar.type}); |
|
38 writer->startRelation(configuration.relation, attributesMetadata, true); |
|
39 |
|
40 // Write records from CLI: |
|
41 for (auto value : configuration.values) writer->writeAttribute(value); |
|
42 |
|
43 // Write records from STDIN: |
|
44 if (configuration.valueStream) { |
|
45 std::stringstream rawValue; |
|
46 while (true) { |
|
47 char ch = 0; |
|
48 configuration.valueStream->get(ch); |
|
49 if (ch == 0 && configuration.valueStream->good()) { |
|
50 writer->writeAttribute(convertor.from_bytes(rawValue.str())); |
|
51 rawValue.str(""); |
|
52 rawValue.clear(); |
|
53 } else if (configuration.valueStream->good()) { |
|
54 rawValue << ch; |
|
55 } else if (configuration.valueStream->eof()) { |
|
56 return; |
|
57 } else { |
|
58 throw relpipe::cli::RelpipeCLIException(L"Error while reading values from the input stream.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 } |
|
64 |
|
65 public: |
|
66 |
|
67 void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
|
68 for (RelationConfiguration& relationConfiguration : configuration.relationConfigurations) processRelation(relationConfiguration, writer); |
|
69 } |
|
70 }; |
|
71 |
|
72 } |
|
73 } |
|
74 } |