author | František Kučera <franta-hg@frantovo.cz> |
Sun, 18 Apr 2021 10:42:54 +0200 | |
branch | v_0 |
changeset 17 | ea36eed9683f |
parent 14 | a7596589a5b0 |
child 21 | af4cb72127c1 |
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
4bcf3fb7cc48
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
6
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 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
26 |
||
27 |
#include <relpipe/reader/typedefs.h> |
|
28 |
#include <relpipe/reader/TypeId.h> |
|
29 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
30 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
31 |
||
14
a7596589a5b0
change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
32 |
#include "Configuration.h" |
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
#include "RelpipeCSVWriterException.h" |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
|
0 | 35 |
namespace relpipe { |
36 |
namespace out { |
|
37 |
namespace csv { |
|
38 |
||
39 |
using namespace relpipe; |
|
40 |
using namespace relpipe::reader; |
|
41 |
using namespace relpipe::reader::handlers; |
|
42 |
||
6
de4c706edf41
fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
43 |
class CSVHandler : public RelationalReaderStringHandler { |
0 | 44 |
private: |
45 |
std::ostream& output; |
|
14
a7596589a5b0
change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
46 |
Configuration& configuration; |
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
const char QUOTE = '"'; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML? |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
std::vector<AttributeMetadata> firstAttributes; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
integer_t valueCount = 0; |
0 | 51 |
public: |
52 |
||
14
a7596589a5b0
change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
53 |
CSVHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) { |
0 | 54 |
} |
55 |
||
56 |
void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override { |
|
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
if (firstAttributes.empty()) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
firstAttributes = attributes; |
17
ea36eed9683f
optionally write data types into the CSV header: --write-types
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
59 |
if (configuration.writeHeader) for (auto attr : attributes) attribute(configuration.writeTypes ? attr.getAttributeName() + L"::" + attr.getTypeName() : attr.getAttributeName()); |
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
} else { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
// TODO: UNION ALL if data types and attribute count matches |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format."); |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
} |
0 | 64 |
} |
65 |
||
66 |
void attribute(const string_t& value) override { |
|
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
valueCount++; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
|
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
if (value.size() > 0) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
output << QUOTE; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
for (auto ch : convertor.to_bytes(value)) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
if (ch == QUOTE) output << QUOTE << QUOTE; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
else output << ch; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
output << QUOTE; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
} |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
|
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
if (valueCount % firstAttributes.size()) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
output << ","; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
} else { |
3
b5a5bf32f7ff
generate CRLF line ends according to RFC 4180
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
81 |
output << "\r\n"; |
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
valueCount = 0; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
} |
0 | 84 |
} |
85 |
||
86 |
void endOfPipe() { |
|
87 |
output.flush(); |
|
88 |
} |
|
89 |
||
90 |
}; |
|
91 |
||
92 |
} |
|
93 |
} |
|
94 |
} |