author | František Kučera <franta-hg@frantovo.cz> |
Sat, 14 Dec 2019 14:00:36 +0100 | |
branch | v_0 |
changeset 24 | 884ece10575d |
parent 23 | d3bfbce022aa |
child 25 | ec793cb3e686 |
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> |
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
22 |
#include <regex> |
0 | 23 |
#include <vector> |
24 |
#include <locale> |
|
25 |
#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
|
26 |
#include <unistd.h> |
8 | 27 |
#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
|
28 |
#include <sys/stat.h> |
0 | 29 |
|
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
#include <sqlite3.h> |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
31 |
|
0 | 32 |
#include <relpipe/reader/typedefs.h> |
33 |
#include <relpipe/reader/TypeId.h> |
|
8 | 34 |
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h> |
0 | 35 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
36 |
||
37 |
#include <relpipe/writer/Factory.h> |
|
38 |
||
39 |
#include "Configuration.h" |
|
40 |
#include "SqlException.h" |
|
41 |
||
42 |
namespace relpipe { |
|
43 |
namespace tr { |
|
44 |
namespace sql { |
|
45 |
||
46 |
using namespace std; |
|
47 |
using namespace relpipe; |
|
48 |
using namespace relpipe::reader; |
|
49 |
using namespace relpipe::reader::handlers; |
|
50 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
class PreparedStatement { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
sqlite3_stmt* stmt; |
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 |
public: |
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 |
PreparedStatement(sqlite3_stmt* stmt) : stmt(stmt) { |
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 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
virtual ~PreparedStatement() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
sqlite3_finalize(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
|
8 | 64 |
void setBoolean(int parameterIndex, relpipe::reader::boolean_t value) { |
65 |
int result = sqlite3_bind_int(stmt, parameterIndex, value); |
|
66 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
67 |
} |
|
68 |
||
69 |
void setInteger(int parameterIndex, relpipe::reader::integer_t value) { |
|
70 |
int result = sqlite3_bind_int64(stmt, parameterIndex, value); |
|
71 |
if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter."); |
|
72 |
} |
|
73 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
void setNull(int parameterIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
bool next() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
int result = sqlite3_step(stmt); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
if (result == SQLITE_ROW) return true; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
|
7 | 91 |
void reset() { |
92 |
int result = sqlite3_reset(stmt); |
|
93 |
if (result != SQLITE_OK) throw SqlException(L"Unable to reset SQLite prepared statement."); |
|
94 |
} |
|
95 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
int getColumnCount() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
return sqlite3_column_count(stmt); |
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 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
100 |
std::string getColumName(int columnIndex) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
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
|
102 |
if (name) return name; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
103 |
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
|
104 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
105 |
|
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
106 |
relpipe::writer::TypeId getColumType(int columnIndex, relpipe::writer::TypeId defaultType = relpipe::writer::TypeId::STRING) { |
23
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
107 |
const char* type = sqlite3_column_decltype(stmt, columnIndex); |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
108 |
|
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
109 |
// TODO: sqlite3_column_decltype returns value only for columns of existing tables, not for dynamic expressions – SQLite uses dynamic types |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
110 |
// maybe we could write a function/module that returns result set metadata for given query (before executing it) |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
111 |
// or use at least explicit casts in SQL and modify sqlite3_column_decltype() function or add some new one to return such casted type |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
112 |
// |
23
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
113 |
// fprintf(stderr, "%d → %s\n", columnIndex, type); |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
114 |
// SELECT typeof(1+1); == "integer" |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
115 |
// https://www.sqlite.org/c3ref/column_decltype.html – sqlite3_column_decltype |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
116 |
// https://www.sqlite.org/c3ref/column_blob.html – sqlite3_column_type |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
117 |
// https://www.sqlite.org/datatype3.html – Datatypes In SQLite Version 3 |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
118 |
// https://dba.stackexchange.com/questions/203220/sqlite-what-is-the-use-of-specifying-data-types |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
119 |
// https://www.mail-archive.com/sqlite-users@mailinglists.sqlite.org/msg118093.html |
23
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
120 |
|
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
121 |
if (type == nullptr) return relpipe::writer::TypeId::STRING; |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
122 |
else if (strcmp(type, "integer") == 0) return relpipe::writer::TypeId::INTEGER; |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
123 |
else if (strcmp(type, "text") == 0) return relpipe::writer::TypeId::STRING; |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
124 |
else return defaultType; |
23
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
125 |
// TODO: support also other data types |
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
126 |
} |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
127 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
128 |
std::string getString(int columnIndex) { |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
132 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
133 |
}; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
134 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
135 |
class Connection { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
136 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
137 |
sqlite3* db; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
138 |
public: |
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 |
Connection(const char* filename) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
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
|
145 |
} |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
146 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
147 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
148 |
virtual ~Connection() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
149 |
sqlite3_close(db); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
150 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
151 |
|
7 | 152 |
PreparedStatement* prepareStatement(const char* sql) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
153 |
const char* remaining; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
154 |
sqlite3_stmt *stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
155 |
int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
7 | 156 |
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
|
157 |
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
|
158 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
159 |
|
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
160 |
void transactionBegin() { |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
161 |
sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
162 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
163 |
|
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
164 |
void transactionCommit() { |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
165 |
sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
166 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
167 |
|
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
168 |
void transactionRollback() { |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
169 |
sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr); |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
170 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
171 |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
172 |
}; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
173 |
|
8 | 174 |
class SqlHandler : public RelationalReaderValueHandler { |
0 | 175 |
private: |
176 |
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
|
177 |
boolean_t fileAlreadyExisted = false; |
0 | 178 |
writer::RelationalWriter* relationalWriter; |
179 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
|
7 | 180 |
vector<AttributeMetadata> currentReaderMetadata; |
181 |
integer_t currentAttributeIndex = 0; |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
182 |
std::unique_ptr<Connection> connection; |
7 | 183 |
std::unique_ptr<PreparedStatement> currentInsert; |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
184 |
|
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
185 |
bool readNextSqlStatement(std::wistream* input, std::wstringstream* sql) { |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
186 |
sql->str(L""); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
187 |
sql->clear(); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
188 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
189 |
for (wchar_t ch; *input >> ch;) { |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
190 |
*sql << ch; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
191 |
if (ch == L';' && sqlite3_complete(convertor.to_bytes(sql->str()).c_str())) return true; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
192 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
193 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
194 |
string_t remainingSql = sql->str(); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
195 |
for (wchar_t ch : remainingSql) if (ch != L' ' && ch != L'\n' && ch != L'\r' && ch != L'\t') throw SqlException(L"Unexpected EOF, missing „;“ after: „" + remainingSql + L"“"); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
196 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
197 |
return false; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
198 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
199 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
200 |
void processSqlInput(std::wistream* input) { |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
201 |
if (input == nullptr) return; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
202 |
*input >> std::ws >> std::noskipws; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
203 |
for (std::wstringstream sql; readNextSqlStatement(input, &sql);) { |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
204 |
std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
205 |
while (prepared->next()); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
206 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
207 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
208 |
|
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
209 |
relpipe::writer::TypeId findType(string_t columnName, int columnIndex, const Statement& statement, std::shared_ptr<PreparedStatement> preparedStatement) { |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
210 |
for (TypeCast typeCast : statement.typeCasts) if (typeCast.name == columnName) return relationalWriter->toTypeId(typeCast.type); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
211 |
return preparedStatement->getColumType(columnIndex); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
212 |
} |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
213 |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
214 |
void processStatement(const Statement& statement) { |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
215 |
std::shared_ptr<PreparedStatement> prepared(connection->prepareStatement(convertor.to_bytes(statement.sql).c_str())); |
7 | 216 |
int columnCount = prepared->getColumnCount(); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
217 |
int parameterCount = statement.parameters.size(); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
218 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
219 |
for (int i = 0; i < parameterCount; i++) { |
7 | 220 |
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
|
221 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
222 |
|
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
223 |
std::vector<relpipe::writer::AttributeMetadata> metadata; |
24
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
224 |
for (int i = 0; i < columnCount; i++) { |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
225 |
string_t columnName = convertor.from_bytes(prepared->getColumName(i).c_str()); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
226 |
metadata.push_back({columnName, findType(columnName, i, statement, prepared)}); |
884ece10575d
add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
227 |
} |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
228 |
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
|
229 |
|
7 | 230 |
while (prepared->next()) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
231 |
for (int i = 0; i < columnCount; i++) { |
7 | 232 |
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
|
233 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
234 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
235 |
} |
0 | 236 |
|
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
237 |
std::vector<string_t> getAllRelations() { |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
238 |
std::vector<string_t> relations; |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
239 |
std::unique_ptr<PreparedStatement> prepared(connection->prepareStatement("SELECT name FROM sqlite_master WHERE type IN ('table', 'view')")); |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
240 |
while (prepared->next()) relations.push_back(convertor.from_bytes(prepared->getString(0))); |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
241 |
return relations; |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
242 |
} |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
243 |
|
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
244 |
void copyRelations(const CopyRelations& copy) { |
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
245 |
std::wregex pattern(copy.pattern); |
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
246 |
for (string_t relation : getAllRelations()) { |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
247 |
if (regex_match(relation, pattern)) { |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
248 |
std::wstringstream select; |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
249 |
select << L"SELECT * FROM "; |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
250 |
writeIdentifier(select, relation); |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
251 |
|
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
252 |
Statement statement; |
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
253 |
statement.relation = copy.replace ? regex_replace(relation, pattern, copy.replacement) : relation; |
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
254 |
statement.sql = select.str(); |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
255 |
processStatement(statement); |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
256 |
} |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
257 |
} |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
258 |
} |
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
259 |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
260 |
relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) { |
8 | 261 |
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
|
262 |
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
|
263 |
else return L"text"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
264 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
265 |
|
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
266 |
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
|
267 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
268 |
for (auto & ch : identifier) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
269 |
if (ch == L'"') output << L"\"\""; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
270 |
else output << ch; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
271 |
} |
11
ccf19c7e7adf
finish writeIdentifier() method
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
272 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
273 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
274 |
|
0 | 275 |
public: |
276 |
||
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
|
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 |
// 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
|
283 |
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
|
284 |
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
|
285 |
} 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
|
286 |
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
|
287 |
} |
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 |
|
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
|
289 |
connection.reset(new Connection(file.c_str())); |
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
290 |
connection->transactionBegin(); |
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
291 |
} |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
292 |
|
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
293 |
virtual ~SqlHandler() { |
0 | 294 |
} |
295 |
||
296 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
7 | 297 |
currentReaderMetadata = attributes; |
298 |
||
299 |
// CREATE TABLE: |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
300 |
std::wstringstream sql; |
7 | 301 |
// 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
|
302 |
sql << L"CREATE TABLE "; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
303 |
writeIdentifier(sql, name); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
304 |
sql << L" (\n"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
305 |
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
|
306 |
sql << L"\t"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
307 |
writeIdentifier(sql, attributes[i].getAttributeName()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
308 |
sql << L" " << toSQLType(attributes[i].getTypeId()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
309 |
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
|
310 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
311 |
sql << L"\n)"; |
0 | 312 |
|
7 | 313 |
std::unique_ptr<PreparedStatement> createTable(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
314 |
createTable->next(); |
|
315 |
||
316 |
// prepare INSERT: |
|
317 |
sql = wstringstream(); |
|
318 |
sql << L"INSERT INTO "; |
|
319 |
writeIdentifier(sql, name); |
|
320 |
sql << L" VALUES ("; |
|
321 |
for (int i = 0; i < attributes.size(); i++) { |
|
322 |
sql << L"?"; |
|
323 |
if (i < attributes.size() - 1) sql << L","; |
|
324 |
} |
|
325 |
sql << L")"; |
|
326 |
currentInsert.reset(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
|
0 | 327 |
} |
328 |
||
8 | 329 |
void attribute(const void* value, const std::type_info& typeInfo) override { |
330 |
relpipe::reader::TypeId type = currentReaderMetadata[currentAttributeIndex].getTypeId(); |
|
7 | 331 |
currentAttributeIndex++; |
8 | 332 |
|
333 |
switch (type) { |
|
334 |
case relpipe::reader::TypeId::BOOLEAN: |
|
335 |
{ |
|
336 |
assert(typeInfo == typeid (boolean_t)); |
|
337 |
auto* typedValue = static_cast<const boolean_t*> (value); |
|
338 |
currentInsert->setBoolean(currentAttributeIndex, *typedValue); |
|
339 |
break; |
|
340 |
} |
|
341 |
case relpipe::reader::TypeId::INTEGER: |
|
342 |
{ |
|
343 |
assert(typeInfo == typeid (integer_t)); |
|
344 |
auto* typedValue = static_cast<const integer_t*> (value); |
|
345 |
currentInsert->setInteger(currentAttributeIndex, *typedValue); |
|
346 |
break; |
|
347 |
} |
|
348 |
case relpipe::reader::TypeId::STRING: |
|
349 |
{ |
|
350 |
assert(typeInfo == typeid (string_t)); |
|
351 |
auto* typedValue = static_cast<const string_t*> (value); |
|
352 |
currentInsert->setString(currentAttributeIndex, convertor.to_bytes(*typedValue).c_str()); |
|
353 |
break; |
|
354 |
} |
|
355 |
default: |
|
356 |
throw SqlException(L"Unsupported type in attribute()"); |
|
357 |
} |
|
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
|
358 |
|
8 | 359 |
if (currentAttributeIndex % currentReaderMetadata.size() == 0) { |
7 | 360 |
currentInsert->next(); |
361 |
currentInsert->reset(); |
|
362 |
currentAttributeIndex = 0; |
|
363 |
} |
|
0 | 364 |
} |
365 |
||
366 |
void endOfPipe() { |
|
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
367 |
// process optional SQL input |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
368 |
processSqlInput(configuration.sqlBeforeRelational); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
369 |
|
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
|
370 |
// 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
|
371 |
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
|
372 |
|
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
373 |
// process optional SQL input |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
374 |
processSqlInput(configuration.sqlAfterRelational); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
375 |
|
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
376 |
// pass-through some relations: |
16
3c51a2c32c86
rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
377 |
for (const CopyRelations& copy : configuration.copyRelations) copyRelations(copy); |
23
d3bfbce022aa
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
378 |
|
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
379 |
connection->transactionCommit(); |
14
eacacf060755
add --dump option: allow pass through of relation specified by a regular expression
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
380 |
|
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
|
381 |
// 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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
} |
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
|
388 |
} // else: we had no file, everything was in memory |
0 | 389 |
} |
390 |
||
391 |
}; |
|
392 |
||
393 |
} |
|
394 |
} |
|
395 |
} |