author | František Kučera <franta-hg@frantovo.cz> |
Wed, 07 Dec 2022 01:38:50 +0100 | |
branch | v_0 |
changeset 1 | 4c0366e1b4df |
parent 0 | 7727b4d5560d |
child 2 | 8a30971d285f |
permissions | -rw-r--r-- |
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <memory> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <iostream> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <locale> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <codecvt> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include <relpipe/reader/typedefs.h> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/reader/TypeId.h> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
#include "Configuration.h" |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
#include "RelpipeSQLWriterException.h" |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
namespace relpipe { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
namespace out { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
namespace sql { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
using namespace relpipe; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
using namespace relpipe::reader; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
using namespace relpipe::reader::handlers; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
class SQLHandler : public RelationalReaderStringHandler { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
private: |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
std::ostream& output; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
Configuration& configuration; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // generate SQL always in UTF-8 |
1 | 48 |
std::vector<AttributeMetadata> currentAttributes; |
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
integer_t valueCount = 0; |
1 | 50 |
integer_t recordCount = 0; |
51 |
string_t currentTable; |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
/** |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
* @param a |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
* @param b |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
* @return true if relations have same number and types of attributes (names may differ) |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
*/ |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
bool matches(const std::vector<AttributeMetadata>& a, const std::vector<AttributeMetadata>& b) { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
if (a.size() != b.size()) return false; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
for (int i = 0, limit = a.size(); i < limit; i++) if (a[i].getTypeId() != b[i].getTypeId()) return false; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
return true; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
} |
1 | 63 |
|
64 |
static void writeIdentifier(std::ostream& output, std::string identifier) { |
|
65 |
output << '"'; |
|
66 |
for (auto & ch : identifier) { |
|
67 |
if (ch == '"') output << "\"\""; |
|
68 |
else output << ch; |
|
69 |
} |
|
70 |
output << '"'; |
|
71 |
} |
|
72 |
||
73 |
static void writeValue(std::ostream& output, std::string value) { |
|
74 |
output << '\''; |
|
75 |
for (auto & ch : value) { |
|
76 |
if (ch == '\'') output << "''"; |
|
77 |
else output << ch; |
|
78 |
} |
|
79 |
output << '\''; |
|
80 |
} |
|
81 |
||
82 |
void writeRecordCount() { |
|
83 |
// currently disabled due to relpipe-in-sql parser issues with last comment without any following expression |
|
84 |
// output << "-- Record count: " << recordCount << std::endl; |
|
85 |
} |
|
86 |
||
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
public: |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
SQLHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
// TODO: ALTER TABLE / add columns on duplicate relation name |
1 | 94 |
// TODO: optionally omit CREATE/ALTER table (just INSERT) |
95 |
// TODO: optional transformation to upper/lower case |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
// TODO: custom data type mapping |
1 | 97 |
// TODO: custom primary key or other column properties |
98 |
// TODO: custom table properties |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
// TODO: custom SQL script before/after stream/relation/record |
1 | 100 |
// TODO: comments and/or custom comments + record count of each table as a comment |
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
// TODO: optional transactions: BEGIN/COMMIT/ROLLBACK for stream/relation/record |
1 | 102 |
// TODO: optional wrapping at certain width (like 80 characters)? |
103 |
// TODO: optional syntax highlighting? |
|
104 |
// TODO: share code/behavior with relpipe-tr-sql (but it uses parametrized statements) |
|
105 |
||
106 |
if (currentTable.size()) { |
|
107 |
writeRecordCount(); |
|
108 |
output << std::endl; |
|
109 |
} |
|
110 |
||
111 |
currentTable = name; |
|
112 |
currentAttributes = attributes; |
|
113 |
recordCount = 0; |
|
114 |
valueCount = 0; |
|
115 |
||
116 |
output << "CREATE TABLE "; |
|
117 |
writeIdentifier(output, convertor.to_bytes(currentTable)); |
|
118 |
output << " (" << std::endl; |
|
119 |
for (size_t i = 0, limit = attributes.size(); i < limit; i++) { |
|
120 |
auto attribute = attributes[i]; |
|
121 |
output << "\t"; |
|
122 |
writeIdentifier(output, convertor.to_bytes(attribute.getAttributeName())); |
|
123 |
// TODO: support all data types + implement RelationalReaderValueHandler |
|
124 |
output << " TEXT"; |
|
125 |
if (i < (limit - 1)) output << ","; |
|
126 |
output << std::endl; |
|
127 |
||
128 |
} |
|
129 |
output << ");" << std::endl << std::endl; |
|
130 |
||
131 |
if (currentAttributes.empty()) { |
|
132 |
//if (configuration.writeHeader) for (auto attr : attributes) attribute(configuration.writeTypes ? attr.getAttributeName() + L"::" + attr.getTypeName() : attr.getAttributeName()); |
|
133 |
} else if (matches(currentAttributes, attributes)) { |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
// do UNION ALL – just append the records |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
} else { |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
// throw RelpipeSQLWriterException(L"To the SQL format we can convert only one relation or multiple relations that have same number of attributes of same types (relation and attribute names may differ – result is named after the first one)."); |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
139 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
140 |
void attribute(const string_t& value) override { |
1 | 141 |
|
142 |
if (valueCount % currentAttributes.size() == 0) { |
|
143 |
// TODO: optional use of function/procedure instead of INSERT |
|
144 |
// TODO: optional INSERT of multiple records |
|
145 |
// TODO: custom line-ends + indentation |
|
146 |
// TODO: optionally write also the column names |
|
147 |
recordCount++; |
|
148 |
output << "INSERT INTO "; |
|
149 |
writeIdentifier(output, convertor.to_bytes(currentTable)); |
|
150 |
output << " VALUES ("; |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
151 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
|
1 | 153 |
valueCount++; |
154 |
||
155 |
if (value.size() > 0) { |
|
156 |
// TODO: support all data types + implement RelationalReaderValueHandler |
|
157 |
writeValue(output, convertor.to_bytes(value)); |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
} else { |
1 | 159 |
// TODO: support actual nulls when supported in the relpipe data format + just optional conversion from empty strings to NULLs |
160 |
output << "NULL"; |
|
161 |
} |
|
162 |
||
163 |
if (valueCount % currentAttributes.size()) { |
|
164 |
output << ", "; |
|
165 |
} else { |
|
166 |
output << ");" << std::endl; |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
167 |
valueCount = 0; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
168 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
169 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
170 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
171 |
void endOfPipe() { |
1 | 172 |
if (currentTable.size()) { |
173 |
writeRecordCount(); |
|
174 |
} |
|
0
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
175 |
output.flush(); |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
176 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
177 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
178 |
}; |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
179 |
|
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
180 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
181 |
} |
7727b4d5560d
new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
182 |
} |