author | František Kučera <franta-hg@frantovo.cz> |
Sun, 18 Apr 2021 10:53:28 +0200 | |
branch | v_0 |
changeset 19 | 0d858e0eedf8 |
parent 16 | 15ee963675af |
child 20 | 90ae67de2f68 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 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
1ae185cac1f3
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
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 |
#include <cstdlib> |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
18 |
#include <vector> |
0 | 19 |
#include <memory> |
20 |
#include <regex> |
|
21 |
#include <algorithm> |
|
22 |
#include <unistd.h> |
|
23 |
||
24 |
#include <relpipe/writer/RelationalWriter.h> |
|
25 |
#include <relpipe/writer/RelpipeWriterException.h> |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
#include <relpipe/writer/AttributeMetadata.h> |
0 | 27 |
#include <relpipe/writer/Factory.h> |
28 |
#include <relpipe/writer/TypeId.h> |
|
29 |
||
30 |
#include <relpipe/cli/CLI.h> |
|
31 |
||
14
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
32 |
#include "CSVCommand.h" |
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
33 |
|
0 | 34 |
using namespace std; |
35 |
using namespace relpipe::cli; |
|
36 |
using namespace relpipe::writer; |
|
37 |
||
14
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
38 |
namespace relpipe { |
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
39 |
namespace in { |
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
40 |
namespace csv { |
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
41 |
|
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
42 |
bool CSVCommand::readValue(std::istream& input, std::stringstream& currentValue, bool& lastInRecord) { |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
lastInRecord = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
char ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
while (input.get(ch)) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
if (ch == '\r') input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
if (ch == '\n') lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
else if (ch != ',') throw RelpipeWriterException(L"Unexpected character (should be „\\n“ or „,“)"); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
} else if (ch == ',') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
} else if (ch == '\n') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
} else if (ch == '\r') { |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
input.get(ch); |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
if (ch == '\n') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
throw RelpipeWriterException(L"Crazy carriage stuck during journey"); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
for (currentValue << ch; input.get(ch);) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
switch (ch) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
case ',': return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
case '\r': break; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
case '\n': |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
default: currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
return false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
} |
0 | 89 |
|
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
90 |
void CSVCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) { |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
wstring_convert < codecvt_utf8<wchar_t>> convertor; // UTF-8 is required for CSV |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
vector<AttributeMetadata> metadata; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
bool headerDone = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
bool lastInRecord = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
stringstream currentValue; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
|
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
|
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
while (readValue(input, currentValue, lastInRecord) && input.good()) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
if (headerDone) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
writer->writeAttribute(convertor.from_bytes(currentValue.str())); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
101 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
102 |
AttributeMetadata am; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
103 |
am.attributeName = convertor.from_bytes(currentValue.str()); |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
am.typeId = TypeId::STRING; |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
105 |
metadata.push_back(am); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
106 |
if (lastInRecord) { |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
|
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
108 |
// TODO: allow types on CLI and names from CSV? |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
109 |
// TODO: allow types on the second line of the CSV? |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
110 |
// TODO: allow regex pattern+replacement for extracting name and type from the first line of the CSV? |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
111 |
// TODO: allow attribute filtering, subset, like relpipe-tr-cur? |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
112 |
// TODO: allow skipping lines, like tail -n +2 ? |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
113 |
|
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
114 |
vector<string_t> firstLine; |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
115 |
|
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
116 |
if (metadata.size() == configuration.attributes.size()) { |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
for (int i = 0; i < metadata.size(); i++) { |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
firstLine.push_back(metadata[i].attributeName); |
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
119 |
metadata[i].attributeName = configuration.attributes[i].name; |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
120 |
metadata[i].typeId = configuration.attributes[i].type; |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
} |
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
122 |
} else if (configuration.attributes.size() == 0) { |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
123 |
// first line contains attribute names and type is always string |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
124 |
} else { |
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
125 |
throw RelpipeWriterException(L"Declared attribute count (" + std::to_wstring(configuration.attributes.size()) + L") does not match with number of columns of the first line (" + std::to_wstring(metadata.size()) + L")"); |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
126 |
} |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
128 |
headerDone = true; |
16
15ee963675af
change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
129 |
writer->startRelation(configuration.relation, metadata, true); |
3
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
if (firstLine.size()) { |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
for (string_t value : firstLine) writer->writeAttribute(value); |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
132 |
} |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
133 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
134 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
135 |
|
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
136 |
currentValue.str(""); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
137 |
currentValue.clear(); |
0 | 138 |
} |
139 |
} |
|
140 |
||
14
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
141 |
} |
0 | 142 |
} |
14
012d491e219a
separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
143 |
} |