author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 16:05:36 +0200 | |
branch | v_0 |
changeset 13 | 19580b27ade2 |
parent 12 | 0b38339b871b |
child 14 | eacacf060755 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 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
7da7173d84b0
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
7 |
* the Free Software Foundation, version 3. |
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> |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
21 |
#include <sstream> |
0 | 22 |
#include <vector> |
23 |
#include <locale> |
|
24 |
#include <codecvt> |
|
5
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
25 |
#include <unistd.h> |
8 | 26 |
#include <cassert> |
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
27 |
#include <sys/stat.h> |
0 | 28 |
|
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <sqlite3.h> |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
#include <relpipe/reader/typedefs.h> |
32 |
#include <relpipe/reader/TypeId.h> |
|
8 | 33 |
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h> |
0 | 34 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
35 |
||
36 |
#include <relpipe/writer/Factory.h> |
|
37 |
||
38 |
#include "Configuration.h" |
|
39 |
#include "SqlException.h" |
|
40 |
||
41 |
namespace relpipe { |
|
42 |
namespace tr { |
|
43 |
namespace sql { |
|
44 |
||
45 |
using namespace std; |
|
46 |
using namespace relpipe; |
|
47 |
using namespace relpipe::reader; |
|
48 |
using namespace relpipe::reader::handlers; |
|
49 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
class PreparedStatement { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
sqlite3_stmt* stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
public: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
55 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
56 |
PreparedStatement(sqlite3_stmt* stmt) : stmt(stmt) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
57 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
58 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
virtual ~PreparedStatement() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
sqlite3_finalize(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
|
8 | 63 |
void setBoolean(int parameterIndex, relpipe::reader::boolean_t value) { |
64 |
int result = sqlite3_bind_int(stmt, parameterIndex, value); |
|
65 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
66 |
} |
|
67 |
||
68 |
void setInteger(int parameterIndex, relpipe::reader::integer_t value) { |
|
69 |
int result = sqlite3_bind_int64(stmt, parameterIndex, value); |
|
70 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
71 |
} |
|
72 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
73 |
void setString(int parameterIndex, std::string value) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
int result = sqlite3_bind_text(stmt, parameterIndex, value.c_str(), -1, SQLITE_TRANSIENT); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
75 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
void setNull(int parameterIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
int result = sqlite3_bind_null(stmt, parameterIndex); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
80 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
81 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
bool next() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
int result = sqlite3_step(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
if (result == SQLITE_ROW) return true; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
else if (result == SQLITE_DONE) return false; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
else throw SqlException(L"Error while iterating over SQLite result."); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
|
7 | 90 |
void reset() { |
91 |
int result = sqlite3_reset(stmt); |
|
92 |
if (result != SQLITE_OK) throw SqlException(L"Unable to reset SQLite prepared statement."); |
|
93 |
} |
|
94 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
int getColumnCount() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
return sqlite3_column_count(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
std::string getColumName(int columnIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
100 |
const char* name = sqlite3_column_name(stmt, columnIndex); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
if (name) return name; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
102 |
else throw SqlException(L"Unable to get SQLite column name."); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
103 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
104 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
105 |
// TODO: sqlite3_column_type |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
106 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
107 |
std::string getString(int columnIndex) { |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
108 |
const char* value = (const char*) sqlite3_column_text(stmt, columnIndex); |
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
109 |
return value ? value : ""; // TODO: support NULL values (when supported in relpipe format) |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
110 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
111 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
112 |
}; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
113 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
114 |
class Connection { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
115 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
116 |
sqlite3* db; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
117 |
public: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
118 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
119 |
Connection(const char* filename) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
120 |
int result = sqlite3_open(filename, &db); |
5
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
121 |
if (result != SQLITE_OK) { |
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
122 |
sqlite3_close(db); |
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
123 |
throw SqlException(L"Unable to open SQLite database."); |
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
124 |
} |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
125 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
126 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
127 |
virtual ~Connection() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
128 |
sqlite3_close(db); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
129 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
130 |
|
7 | 131 |
PreparedStatement* prepareStatement(const char* sql) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
132 |
const char* remaining; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
133 |
sqlite3_stmt *stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
134 |
int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
7 | 135 |
if (result == SQLITE_OK) return new PreparedStatement(stmt); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
136 |
else throw SqlException(L"Unable to prepare SQLite statement."); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
137 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
138 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
139 |
}; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
140 |
|
8 | 141 |
class SqlHandler : public RelationalReaderValueHandler { |
0 | 142 |
private: |
143 |
Configuration configuration; |
|
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
144 |
boolean_t fileAlreadyExisted = false; |
0 | 145 |
writer::RelationalWriter* relationalWriter; |
146 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
|
7 | 147 |
vector<AttributeMetadata> currentReaderMetadata; |
148 |
integer_t currentAttributeIndex = 0; |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
149 |
std::unique_ptr<Connection> connection; |
7 | 150 |
std::unique_ptr<PreparedStatement> currentInsert; |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
151 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
152 |
void processStatement(const Statement& statement) { |
7 | 153 |
std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement(convertor.to_bytes(statement.sql).c_str())); |
154 |
int columnCount = prepared->getColumnCount(); |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
155 |
int parameterCount = statement.parameters.size(); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
156 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
157 |
for (int i = 0; i < parameterCount; i++) { |
7 | 158 |
prepared->setString(i + 1, convertor.to_bytes(statement.parameters[i].value)); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
159 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
160 |
|
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
161 |
std::vector<relpipe::writer::AttributeMetadata> metadata; |
5
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
162 |
// TODO: support also other data types |
7 | 163 |
for (int i = 0; i < columnCount; i++) metadata.push_back({convertor.from_bytes(prepared->getColumName(i).c_str()), relpipe::writer::TypeId::STRING}); |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
164 |
relationalWriter->startRelation(statement.relation, metadata, true); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
165 |
|
7 | 166 |
while (prepared->next()) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
167 |
for (int i = 0; i < columnCount; i++) { |
7 | 168 |
relationalWriter->writeAttribute(convertor.from_bytes(prepared->getString(i))); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
169 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
170 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
171 |
} |
0 | 172 |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
173 |
relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) { |
8 | 174 |
if (typeId == relpipe::reader::TypeId::BOOLEAN) return L"integer"; // TODO: map selected values back to booleans or allow optional storage as string |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
175 |
else if (typeId == relpipe::reader::TypeId::INTEGER) return L"integer"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
176 |
else return L"text"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
177 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
178 |
|
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
179 |
void writeIdentifier(std::wstringstream& output, relpipe::writer::string_t identifier) { |
11
ccf19c7e7adf
finish writeIdentifier() method
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
180 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
181 |
for (auto & ch : identifier) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
182 |
if (ch == L'"') output << L"\"\""; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
183 |
else output << ch; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
184 |
} |
11
ccf19c7e7adf
finish writeIdentifier() method
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
185 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
186 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
187 |
|
0 | 188 |
public: |
189 |
||
190 |
SqlHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
191 |
std::string file; |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
192 |
if (configuration.file.size()) { |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
193 |
file = convertor.to_bytes(configuration.file); |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
194 |
|
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
195 |
// in C++17 we can use: std::filesystem::exists() |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
196 |
struct stat fileStat; |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
197 |
fileAlreadyExisted = (stat(file.c_str(), &fileStat) == 0); |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
198 |
} else { |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
199 |
file = ":memory:"; |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
200 |
} |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
201 |
|
5
cbc7817a3346
add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
202 |
connection.reset(new Connection(file.c_str())); |
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
203 |
} |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
204 |
|
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
205 |
virtual ~SqlHandler() { |
0 | 206 |
} |
207 |
||
208 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
7 | 209 |
currentReaderMetadata = attributes; |
210 |
||
211 |
// CREATE TABLE: |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
212 |
std::wstringstream sql; |
7 | 213 |
// TODO: if already exist just append new columns |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
214 |
sql << L"CREATE TABLE "; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
215 |
writeIdentifier(sql, name); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
216 |
sql << L" (\n"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
217 |
for (int i = 0; i < attributes.size(); i++) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
218 |
sql << L"\t"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
219 |
writeIdentifier(sql, attributes[i].getAttributeName()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
220 |
sql << L" " << toSQLType(attributes[i].getTypeId()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
221 |
if (i < attributes.size() - 1) sql << L",\n"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
222 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
223 |
sql << L"\n)"; |
0 | 224 |
|
7 | 225 |
std::unique_ptr<PreparedStatement> createTable(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
226 |
createTable->next(); |
|
227 |
||
228 |
// prepare INSERT: |
|
229 |
sql = wstringstream(); |
|
230 |
sql << L"INSERT INTO "; |
|
231 |
writeIdentifier(sql, name); |
|
232 |
sql << L" VALUES ("; |
|
233 |
for (int i = 0; i < attributes.size(); i++) { |
|
234 |
sql << L"?"; |
|
235 |
if (i < attributes.size() - 1) sql << L","; |
|
236 |
} |
|
237 |
sql << L")"; |
|
238 |
currentInsert.reset(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
|
0 | 239 |
} |
240 |
||
8 | 241 |
void attribute(const void* value, const std::type_info& typeInfo) override { |
242 |
relpipe::reader::TypeId type = currentReaderMetadata[currentAttributeIndex].getTypeId(); |
|
7 | 243 |
currentAttributeIndex++; |
8 | 244 |
|
245 |
switch (type) { |
|
246 |
case relpipe::reader::TypeId::BOOLEAN: |
|
247 |
{ |
|
248 |
assert(typeInfo == typeid (boolean_t)); |
|
249 |
auto* typedValue = static_cast<const boolean_t*> (value); |
|
250 |
currentInsert->setBoolean(currentAttributeIndex, *typedValue); |
|
251 |
break; |
|
252 |
} |
|
253 |
case relpipe::reader::TypeId::INTEGER: |
|
254 |
{ |
|
255 |
assert(typeInfo == typeid (integer_t)); |
|
256 |
auto* typedValue = static_cast<const integer_t*> (value); |
|
257 |
currentInsert->setInteger(currentAttributeIndex, *typedValue); |
|
258 |
break; |
|
259 |
} |
|
260 |
case relpipe::reader::TypeId::STRING: |
|
261 |
{ |
|
262 |
assert(typeInfo == typeid (string_t)); |
|
263 |
auto* typedValue = static_cast<const string_t*> (value); |
|
264 |
currentInsert->setString(currentAttributeIndex, convertor.to_bytes(*typedValue).c_str()); |
|
265 |
break; |
|
266 |
} |
|
267 |
default: |
|
268 |
throw SqlException(L"Unsupported type in attribute()"); |
|
269 |
} |
|
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
270 |
|
8 | 271 |
if (currentAttributeIndex % currentReaderMetadata.size() == 0) { |
7 | 272 |
currentInsert->next(); |
273 |
currentInsert->reset(); |
|
274 |
currentAttributeIndex = 0; |
|
275 |
} |
|
0 | 276 |
} |
277 |
||
278 |
void endOfPipe() { |
|
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
279 |
// run the transformation – process all statements: |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
280 |
for (const Statement& statement : configuration.statements) processStatement(statement); |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
281 |
|
12
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
282 |
// delete or keep the file: |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
283 |
if (configuration.file.size()) { |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
284 |
if (configuration.keepFile == KeepFile::Never || (configuration.keepFile == KeepFile::Automatic && !fileAlreadyExisted)) { |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
285 |
std::wcerr << L"will unlink file" << std::endl; |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
286 |
int result = unlink(convertor.to_bytes(configuration.file).c_str()); |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
287 |
if (result) throw SqlException(L"Unable to delete SQLite file."); |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
288 |
} |
0b38339b871b
improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
289 |
} // else: we had no file, everything was in memory |
0 | 290 |
} |
291 |
||
292 |
}; |
|
293 |
||
294 |
} |
|
295 |
} |
|
296 |
} |