author | František Kučera <franta-hg@frantovo.cz> |
Thu, 24 Sep 2020 16:03:05 +0200 | |
branch | v_0 |
changeset 45 | 5e5815fdcd2d |
parent 44 | dd7094457e44 |
permissions | -rw-r--r-- |
24
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
1 |
/** |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
2 |
* Relational pipes |
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
24
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
4 |
* |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
37
27f0ffa712d9
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
24
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
8 |
* |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
12 |
* GNU General Public License for more details. |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
13 |
* |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
c31fdd965028
license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
16 |
*/ |
11
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
19 |
#include <codecvt> |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
20 |
#include <memory> |
11
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <iostream> |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
23 |
#include "Configuration.h" |
11
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
namespace relpipe { |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
namespace in { |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
namespace cli { |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
29 |
class CLICommand { |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
30 |
private: |
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
31 |
|
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
32 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
33 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
34 |
void processRelation(RelationConfiguration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
35 |
// Write header / metadata: |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
36 |
std::vector<relpipe::writer::AttributeMetadata> attributesMetadata; |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
37 |
for (AttributeRecipe ar : configuration.attributes) attributesMetadata.push_back({ar.name, ar.type}); |
44
dd7094457e44
add option: --write-header
František Kučera <franta-hg@frantovo.cz>
parents:
43
diff
changeset
|
38 |
writer->startRelation(configuration.relation, attributesMetadata, configuration.writeHeader); |
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
39 |
|
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
40 |
// Write records from CLI: |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
41 |
for (auto value : configuration.values) writer->writeAttribute(value); |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
42 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
43 |
// Write records from STDIN: |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
44 |
if (configuration.valueStream) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
45 |
std::stringstream rawValue; |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
46 |
while (true) { |
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
47 |
char ch = 0; |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
48 |
configuration.valueStream->get(ch); |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
49 |
if (ch == 0 && configuration.valueStream->good()) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
50 |
writer->writeAttribute(convertor.from_bytes(rawValue.str())); |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
51 |
rawValue.str(""); |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
52 |
rawValue.clear(); |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
53 |
} else if (configuration.valueStream->good()) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
54 |
rawValue << ch; |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
55 |
} else if (configuration.valueStream->eof()) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
56 |
return; |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
57 |
} else { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
58 |
throw relpipe::cli::RelpipeCLIException(L"Error while reading values from the input stream.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
59 |
} |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
60 |
} |
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
61 |
} |
16
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
62 |
|
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
63 |
} |
70af16946466
StdInCommand: generate relational data from STDIN (and also CLI arguments)
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
64 |
|
11
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
public: |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
43
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
67 |
void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) { |
3c8ea5dcf793
change CLI interface: options: --relation --attribute --record --records --records-from-stdin
František Kučera <franta-hg@frantovo.cz>
parents:
39
diff
changeset
|
68 |
for (RelationConfiguration& relationConfiguration : configuration.relationConfigurations) processRelation(relationConfiguration, writer); |
11
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
} |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
}; |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
} |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
} |
3798b6bc9aea
ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
} |