author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 21:54:27 +0200 | |
branch | v_0 |
changeset 10 | 4bcf3fb7cc48 |
parent 6 | de4c706edf41 |
child 14 | a7596589a5b0 |
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 |
||
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
32 |
#include "RelpipeCSVWriterException.h" |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
|
0 | 34 |
namespace relpipe { |
35 |
namespace out { |
|
36 |
namespace csv { |
|
37 |
||
38 |
using namespace relpipe; |
|
39 |
using namespace relpipe::reader; |
|
40 |
using namespace relpipe::reader::handlers; |
|
41 |
||
6
de4c706edf41
fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
42 |
class CSVHandler : public RelationalReaderStringHandler { |
0 | 43 |
private: |
44 |
std::ostream& output; |
|
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
const char QUOTE = '"'; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
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
|
47 |
std::vector<AttributeMetadata> firstAttributes; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
integer_t valueCount = 0; |
0 | 49 |
public: |
50 |
||
51 |
CSVHandler(std::ostream& output) : output(output) { |
|
52 |
} |
|
53 |
||
54 |
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
|
55 |
if (firstAttributes.empty()) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
firstAttributes = attributes; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
for (auto attr : attributes) attribute(attr.getAttributeName()); |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
} else { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
// 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
|
60 |
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
|
61 |
} |
0 | 62 |
} |
63 |
||
64 |
void attribute(const string_t& value) override { |
|
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
valueCount++; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
|
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
if (value.size() > 0) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
output << QUOTE; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
for (auto ch : convertor.to_bytes(value)) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
if (ch == QUOTE) output << QUOTE << QUOTE; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
else output << ch; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
} |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
output << QUOTE; |
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 |
|
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
if (valueCount % firstAttributes.size()) { |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
output << ","; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
} else { |
3
b5a5bf32f7ff
generate CRLF line ends according to RFC 4180
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
output << "\r\n"; |
1
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
valueCount = 0; |
82f86dc48339
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
} |
0 | 82 |
} |
83 |
||
84 |
void endOfPipe() { |
|
85 |
output.flush(); |
|
86 |
} |
|
87 |
||
88 |
}; |
|
89 |
||
90 |
} |
|
91 |
} |
|
92 |
} |