author | František Kučera <franta-hg@frantovo.cz> |
Thu, 10 Jan 2019 00:10:19 +0100 | |
branch | v_0 |
changeset 3 | d7907be4cc40 |
parent 2 | e83895da3e8f |
child 10 | 1ae185cac1f3 |
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 |
|
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 |
#include <cstdlib> |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <vector> |
0 | 20 |
#include <memory> |
21 |
#include <regex> |
|
22 |
#include <algorithm> |
|
23 |
#include <unistd.h> |
|
24 |
||
25 |
#include <relpipe/writer/RelationalWriter.h> |
|
26 |
#include <relpipe/writer/RelpipeWriterException.h> |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
#include <relpipe/writer/AttributeMetadata.h> |
0 | 28 |
#include <relpipe/writer/Factory.h> |
29 |
#include <relpipe/writer/TypeId.h> |
|
30 |
||
31 |
#include <relpipe/cli/CLI.h> |
|
32 |
||
33 |
using namespace std; |
|
34 |
using namespace relpipe::cli; |
|
35 |
using namespace relpipe::writer; |
|
36 |
||
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
bool readValue(istream& input, stringstream& currentValue, bool& lastInRecord) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
lastInRecord = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
char ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
while (input.get(ch)) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
if (ch == '"') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
if (ch == '\r') input.get(ch); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
if (ch == '\n') lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
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
|
51 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
} else if (ch == ',') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
} else if (ch == '\n') { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
} 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
|
63 |
input.get(ch); |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
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 { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
throw RelpipeWriterException(L"Crazy carriage stuck during journey"); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
for (currentValue << ch; input.get(ch);) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
switch (ch) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
case ',': return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
case '\r': break; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
case '\n': |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
lastInRecord = true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
return true; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
default: currentValue << ch; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
return false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
} |
0 | 84 |
|
2
e83895da3e8f
optional custom relation name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
void processDataStream(ostream &output, istream& input, const vector<string_t>& args) { |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
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
|
87 |
std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
vector<AttributeMetadata> metadata; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
bool headerDone = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
bool lastInRecord = false; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
stringstream currentValue; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
|
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
|
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
while (readValue(input, currentValue, lastInRecord) && input.good()) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
if (headerDone) { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
writer->writeAttribute(convertor.from_bytes(currentValue.str())); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
} else { |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
AttributeMetadata am; |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
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
|
100 |
am.typeId = TypeId::STRING; |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
101 |
metadata.push_back(am); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
102 |
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
|
103 |
|
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
/* |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
* Usage (simple syntax): |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
* relpipe-in-csv → default relation name, attribute names on the first line, all types are string |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
* relpipe-in-csv my_relation → custom relation name |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
* relpipe-in-csv my_relation a b c → custom relation name, custom attribute names (a,b,c), first line contains data |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
* relpipe-in-csv my_relation a integer b string c boolean → custom relation name, custom attribute names (a,b,c), custom types (integer,string,boolean), first line contains data |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
*/ |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
|
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
vector<string_t> firstLine; |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
if (args.size() == (1 + metadata.size())) { |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
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
|
115 |
firstLine.push_back(metadata[i].attributeName); |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
metadata[i].attributeName = args[1 + i]; |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
} |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
} else if (args.size() == (1 + 2 * metadata.size())) { |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
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
|
120 |
firstLine.push_back(metadata[i].attributeName); |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
metadata[i].attributeName = args[1 + i * 2]; |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
metadata[i].typeId = writer->toTypeId(args[1 + i * 2 + 1]); |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
} |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
124 |
} |
d7907be4cc40
allow also custom relation name, attribute names and types (optional)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
125 |
|
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
126 |
headerDone = true; |
2
e83895da3e8f
optional custom relation name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
127 |
writer->startRelation(args.size() > 0 ? args[0] : L"csv", 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
|
128 |
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
|
129 |
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
|
130 |
} |
1
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
131 |
} |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
132 |
} |
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 |
currentValue.str(""); |
5eb4d149c6e2
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
135 |
currentValue.clear(); |
0 | 136 |
} |
137 |
} |
|
138 |
||
139 |
int main(int argc, char** argv) { |
|
140 |
setlocale(LC_ALL, ""); |
|
141 |
CLI::untieStdIO(); |
|
142 |
CLI cli(argc, argv); |
|
143 |
||
144 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
|
145 |
||
146 |
try { |
|
2
e83895da3e8f
optional custom relation name
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
147 |
processDataStream(cout, cin, cli.arguments()); |
0 | 148 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
149 |
} catch (RelpipeWriterException e) { |
|
150 |
fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str()); |
|
151 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
|
152 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
|
153 |
} |
|
154 |
||
155 |
return resultCode; |
|
156 |
} |