author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 16:02:57 +0200 | |
branch | v_0 |
changeset 10 | 7da7173d84b0 |
parent 8 | 3e076cc76c89 |
child 11 | ccf19c7e7adf |
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> |
0 | 27 |
|
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
#include <sqlite3.h> |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
|
0 | 30 |
#include <relpipe/reader/typedefs.h> |
31 |
#include <relpipe/reader/TypeId.h> |
|
8 | 32 |
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h> |
0 | 33 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
34 |
||
35 |
#include <relpipe/writer/Factory.h> |
|
36 |
||
37 |
#include "Configuration.h" |
|
38 |
#include "SqlException.h" |
|
39 |
||
40 |
namespace relpipe { |
|
41 |
namespace tr { |
|
42 |
namespace sql { |
|
43 |
||
44 |
using namespace std; |
|
45 |
using namespace relpipe; |
|
46 |
using namespace relpipe::reader; |
|
47 |
using namespace relpipe::reader::handlers; |
|
48 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
class PreparedStatement { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
sqlite3_stmt* stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
public: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
55 |
PreparedStatement(sqlite3_stmt* stmt) : stmt(stmt) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
56 |
} |
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 |
virtual ~PreparedStatement() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
sqlite3_finalize(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
|
8 | 62 |
void setBoolean(int parameterIndex, relpipe::reader::boolean_t value) { |
63 |
int result = sqlite3_bind_int(stmt, parameterIndex, value); |
|
64 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
65 |
} |
|
66 |
||
67 |
void setInteger(int parameterIndex, relpipe::reader::integer_t value) { |
|
68 |
int result = sqlite3_bind_int64(stmt, parameterIndex, value); |
|
69 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
70 |
} |
|
71 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
} |
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 |
void setNull(int parameterIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
} |
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 |
bool next() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
int result = sqlite3_step(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
if (result == SQLITE_ROW) return true; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
|
7 | 89 |
void reset() { |
90 |
int result = sqlite3_reset(stmt); |
|
91 |
if (result != SQLITE_OK) throw SqlException(L"Unable to reset SQLite prepared statement."); |
|
92 |
} |
|
93 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
94 |
int getColumnCount() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
return sqlite3_column_count(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
} |
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 |
std::string getColumName(int columnIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
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
|
100 |
if (name) return name; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
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
|
102 |
} |
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 |
// TODO: sqlite3_column_type |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
105 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
106 |
std::string getString(int columnIndex) { |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
} |
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 |
class Connection { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
114 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
115 |
sqlite3* db; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
116 |
public: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
117 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
118 |
Connection(const char* filename) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
} |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
124 |
} |
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 |
virtual ~Connection() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
127 |
sqlite3_close(db); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
128 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
129 |
|
7 | 130 |
PreparedStatement* prepareStatement(const char* sql) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
131 |
const char* remaining; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
132 |
sqlite3_stmt *stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
133 |
int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
7 | 134 |
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
|
135 |
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
|
136 |
} |
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 |
|
8 | 140 |
class SqlHandler : public RelationalReaderValueHandler { |
0 | 141 |
private: |
142 |
Configuration configuration; |
|
143 |
writer::RelationalWriter* relationalWriter; |
|
144 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
|
7 | 145 |
vector<AttributeMetadata> currentReaderMetadata; |
146 |
integer_t currentAttributeIndex = 0; |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
147 |
std::unique_ptr<Connection> connection; |
7 | 148 |
std::unique_ptr<PreparedStatement> currentInsert; |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
149 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
150 |
void processStatement(const Statement& statement) { |
7 | 151 |
std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement(convertor.to_bytes(statement.sql).c_str())); |
152 |
int columnCount = prepared->getColumnCount(); |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
153 |
int parameterCount = statement.parameters.size(); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
154 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
155 |
for (int i = 0; i < parameterCount; i++) { |
7 | 156 |
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
|
157 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
158 |
|
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
159 |
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
|
160 |
// TODO: support also other data types |
7 | 161 |
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
|
162 |
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
|
163 |
|
7 | 164 |
while (prepared->next()) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
165 |
for (int i = 0; i < columnCount; i++) { |
7 | 166 |
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
|
167 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
168 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
169 |
} |
0 | 170 |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
171 |
relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) { |
8 | 172 |
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
|
173 |
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
|
174 |
else return L"text"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
175 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
176 |
|
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
177 |
void writeIdentifier(std::wstringstream& output, relpipe::writer::string_t identifier) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
178 |
for (auto & ch : identifier) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
179 |
if (ch == L'"') output << L"\"\""; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
180 |
else output << ch; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
181 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
182 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
183 |
|
0 | 184 |
public: |
185 |
||
186 |
SqlHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
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
|
187 |
std::string file = configuration.file.size() ? convertor.to_bytes(configuration.file) : ":memory:"; |
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
|
188 |
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
|
189 |
} |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
190 |
|
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
191 |
virtual ~SqlHandler() { |
0 | 192 |
} |
193 |
||
194 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
7 | 195 |
currentReaderMetadata = attributes; |
196 |
||
197 |
// CREATE TABLE: |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
198 |
std::wstringstream sql; |
7 | 199 |
// 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
|
200 |
sql << L"CREATE TABLE "; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
201 |
writeIdentifier(sql, name); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
202 |
sql << L" (\n"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
203 |
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
|
204 |
sql << L"\t"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
205 |
writeIdentifier(sql, attributes[i].getAttributeName()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
206 |
sql << L" " << toSQLType(attributes[i].getTypeId()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
207 |
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
|
208 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
209 |
sql << L"\n)"; |
0 | 210 |
|
7 | 211 |
std::unique_ptr<PreparedStatement> createTable(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
212 |
createTable->next(); |
|
213 |
||
214 |
// prepare INSERT: |
|
215 |
sql = wstringstream(); |
|
216 |
sql << L"INSERT INTO "; |
|
217 |
writeIdentifier(sql, name); |
|
218 |
sql << L" VALUES ("; |
|
219 |
for (int i = 0; i < attributes.size(); i++) { |
|
220 |
sql << L"?"; |
|
221 |
if (i < attributes.size() - 1) sql << L","; |
|
222 |
} |
|
223 |
sql << L")"; |
|
224 |
currentInsert.reset(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
|
0 | 225 |
} |
226 |
||
8 | 227 |
void attribute(const void* value, const std::type_info& typeInfo) override { |
228 |
relpipe::reader::TypeId type = currentReaderMetadata[currentAttributeIndex].getTypeId(); |
|
7 | 229 |
currentAttributeIndex++; |
8 | 230 |
|
231 |
switch (type) { |
|
232 |
case relpipe::reader::TypeId::BOOLEAN: |
|
233 |
{ |
|
234 |
assert(typeInfo == typeid (boolean_t)); |
|
235 |
auto* typedValue = static_cast<const boolean_t*> (value); |
|
236 |
currentInsert->setBoolean(currentAttributeIndex, *typedValue); |
|
237 |
break; |
|
238 |
} |
|
239 |
case relpipe::reader::TypeId::INTEGER: |
|
240 |
{ |
|
241 |
assert(typeInfo == typeid (integer_t)); |
|
242 |
auto* typedValue = static_cast<const integer_t*> (value); |
|
243 |
currentInsert->setInteger(currentAttributeIndex, *typedValue); |
|
244 |
break; |
|
245 |
} |
|
246 |
case relpipe::reader::TypeId::STRING: |
|
247 |
{ |
|
248 |
assert(typeInfo == typeid (string_t)); |
|
249 |
auto* typedValue = static_cast<const string_t*> (value); |
|
250 |
currentInsert->setString(currentAttributeIndex, convertor.to_bytes(*typedValue).c_str()); |
|
251 |
break; |
|
252 |
} |
|
253 |
default: |
|
254 |
throw SqlException(L"Unsupported type in attribute()"); |
|
255 |
} |
|
256 |
||
257 |
if (currentAttributeIndex % currentReaderMetadata.size() == 0) { |
|
7 | 258 |
currentInsert->next(); |
259 |
currentInsert->reset(); |
|
260 |
currentAttributeIndex = 0; |
|
261 |
} |
|
0 | 262 |
} |
263 |
||
264 |
void endOfPipe() { |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
265 |
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
|
266 |
|
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
|
267 |
if (configuration.file.size() && !configuration.keepFile) { |
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
|
268 |
int result = unlink(convertor.to_bytes(configuration.file).c_str()); |
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
|
269 |
if (result) throw SqlException(L"Unable to delete SQLite file."); |
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
|
270 |
} |
0 | 271 |
} |
272 |
||
273 |
}; |
|
274 |
||
275 |
} |
|
276 |
} |
|
277 |
} |