author | František Kučera <franta-hg@frantovo.cz> |
Wed, 25 Dec 2019 00:57:26 +0100 | |
branch | v_0 |
changeset 26 | 49919a60c747 |
parent 25 | ec793cb3e686 |
child 27 | 9441a517fa1f |
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" |
|
26
49919a60c747
move PreparedStatement class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
41 |
#include "PreparedStatement.h" |
0 | 42 |
|
43 |
namespace relpipe { |
|
44 |
namespace tr { |
|
45 |
namespace sql { |
|
46 |
||
47 |
using namespace std; |
|
48 |
using namespace relpipe; |
|
49 |
using namespace relpipe::reader; |
|
50 |
using namespace relpipe::reader::handlers; |
|
51 |
||
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
class Connection { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
private: |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
sqlite3* db; |
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
55 |
|
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
56 |
void begin() { |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
57 |
sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
58 |
} |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
59 |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
public: |
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 |
Connection(const char* filename) { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
} |
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
68 |
begin(); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
69 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
70 |
|
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
virtual ~Connection() { |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
sqlite3_close(db); |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
73 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
|
7 | 75 |
PreparedStatement* prepareStatement(const char* sql) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
const char* remaining; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
sqlite3_stmt *stmt; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
7 | 79 |
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
|
80 |
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
|
81 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
|
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
83 |
bool getAutoCommit() { |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
84 |
return sqlite3_get_autocommit(db); |
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
85 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
86 |
|
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
87 |
void setAutoCommit(bool autoCommit) { |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
88 |
bool autoCommitOld = getAutoCommit(); |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
89 |
if (autoCommit && !autoCommitOld) commit(); |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
90 |
else if (!autoCommit && autoCommitOld) begin(); |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
91 |
} |
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
92 |
|
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
93 |
void commit() { |
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
94 |
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
|
95 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
96 |
|
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
97 |
void rollback() { |
20
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
98 |
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
|
99 |
} |
b295f8985f13
run whole transformation in a single transaction
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
100 |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
}; |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
102 |
|
8 | 103 |
class SqlHandler : public RelationalReaderValueHandler { |
0 | 104 |
private: |
105 |
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
|
106 |
boolean_t fileAlreadyExisted = false; |
0 | 107 |
writer::RelationalWriter* relationalWriter; |
108 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
|
7 | 109 |
vector<AttributeMetadata> currentReaderMetadata; |
110 |
integer_t currentAttributeIndex = 0; |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
111 |
std::unique_ptr<Connection> connection; |
7 | 112 |
std::unique_ptr<PreparedStatement> currentInsert; |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
113 |
|
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
sql->clear(); |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
117 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
118 |
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
|
119 |
*sql << ch; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
120 |
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
|
121 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
122 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
126 |
return false; |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
127 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
128 |
|
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
129 |
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
|
130 |
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
|
131 |
*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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
136 |
} |
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
137 |
|
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
} |
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
|
142 |
|
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
143 |
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
|
144 |
std::shared_ptr<PreparedStatement> prepared(connection->prepareStatement(convertor.to_bytes(statement.sql).c_str())); |
7 | 145 |
int columnCount = prepared->getColumnCount(); |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
146 |
int parameterCount = statement.parameters.size(); |
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 |
for (int i = 0; i < parameterCount; i++) { |
7 | 149 |
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
|
150 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
151 |
|
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
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
|
156 |
} |
4
925b15fb5c63
generate relational outputs
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
157 |
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
|
158 |
|
7 | 159 |
while (prepared->next()) { |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
160 |
for (int i = 0; i < columnCount; i++) { |
7 | 161 |
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
|
162 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
163 |
} |
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
164 |
} |
0 | 165 |
|
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
} |
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
|
172 |
|
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
|
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
} |
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
|
186 |
} |
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
|
187 |
} |
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
|
188 |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
189 |
relpipe::writer::string_t toSQLType(relpipe::reader::TypeId typeId) { |
8 | 190 |
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
|
191 |
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
|
192 |
else return L"text"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
193 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
194 |
|
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
195 |
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
|
196 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
197 |
for (auto & ch : identifier) { |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
198 |
if (ch == L'"') output << L"\"\""; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
199 |
else output << ch; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
200 |
} |
11
ccf19c7e7adf
finish writeIdentifier() method
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
201 |
output << L'"'; |
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
202 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
203 |
|
0 | 204 |
public: |
205 |
||
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
|
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
|
211 |
// 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
|
212 |
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
|
213 |
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
|
214 |
} 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
|
215 |
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
|
216 |
} |
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
|
217 |
|
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
|
218 |
connection.reset(new Connection(file.c_str())); |
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
219 |
connection->setAutoCommit(false); |
1
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
220 |
} |
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
221 |
|
eb7134dfdcc5
link to the sqlite3 library
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
222 |
virtual ~SqlHandler() { |
0 | 223 |
} |
224 |
||
225 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
7 | 226 |
currentReaderMetadata = attributes; |
227 |
||
228 |
// CREATE TABLE: |
|
6
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
229 |
std::wstringstream sql; |
7 | 230 |
// 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
|
231 |
sql << L"CREATE TABLE "; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
232 |
writeIdentifier(sql, name); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
233 |
sql << L" (\n"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
234 |
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
|
235 |
sql << L"\t"; |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
236 |
writeIdentifier(sql, attributes[i].getAttributeName()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
237 |
sql << L" " << toSQLType(attributes[i].getTypeId()); |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
238 |
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
|
239 |
} |
32b4293307f4
create table for each relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
240 |
sql << L"\n)"; |
0 | 241 |
|
7 | 242 |
std::unique_ptr<PreparedStatement> createTable(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
243 |
createTable->next(); |
|
244 |
||
245 |
// prepare INSERT: |
|
246 |
sql = wstringstream(); |
|
247 |
sql << L"INSERT INTO "; |
|
248 |
writeIdentifier(sql, name); |
|
249 |
sql << L" VALUES ("; |
|
250 |
for (int i = 0; i < attributes.size(); i++) { |
|
251 |
sql << L"?"; |
|
252 |
if (i < attributes.size() - 1) sql << L","; |
|
253 |
} |
|
254 |
sql << L")"; |
|
255 |
currentInsert.reset(connection->prepareStatement(convertor.to_bytes(sql.str()).c_str())); |
|
0 | 256 |
} |
257 |
||
8 | 258 |
void attribute(const void* value, const std::type_info& typeInfo) override { |
259 |
relpipe::reader::TypeId type = currentReaderMetadata[currentAttributeIndex].getTypeId(); |
|
7 | 260 |
currentAttributeIndex++; |
8 | 261 |
|
262 |
switch (type) { |
|
263 |
case relpipe::reader::TypeId::BOOLEAN: |
|
264 |
{ |
|
265 |
assert(typeInfo == typeid (boolean_t)); |
|
266 |
auto* typedValue = static_cast<const boolean_t*> (value); |
|
267 |
currentInsert->setBoolean(currentAttributeIndex, *typedValue); |
|
268 |
break; |
|
269 |
} |
|
270 |
case relpipe::reader::TypeId::INTEGER: |
|
271 |
{ |
|
272 |
assert(typeInfo == typeid (integer_t)); |
|
273 |
auto* typedValue = static_cast<const integer_t*> (value); |
|
274 |
currentInsert->setInteger(currentAttributeIndex, *typedValue); |
|
275 |
break; |
|
276 |
} |
|
277 |
case relpipe::reader::TypeId::STRING: |
|
278 |
{ |
|
279 |
assert(typeInfo == typeid (string_t)); |
|
280 |
auto* typedValue = static_cast<const string_t*> (value); |
|
281 |
currentInsert->setString(currentAttributeIndex, convertor.to_bytes(*typedValue).c_str()); |
|
282 |
break; |
|
283 |
} |
|
284 |
default: |
|
285 |
throw SqlException(L"Unsupported type in attribute()"); |
|
286 |
} |
|
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
|
287 |
|
8 | 288 |
if (currentAttributeIndex % currentReaderMetadata.size() == 0) { |
7 | 289 |
currentInsert->next(); |
290 |
currentInsert->reset(); |
|
291 |
currentAttributeIndex = 0; |
|
292 |
} |
|
0 | 293 |
} |
294 |
||
295 |
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
|
296 |
// 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
|
297 |
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
|
298 |
|
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
|
299 |
// 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
|
300 |
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
|
301 |
|
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
302 |
// 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
|
303 |
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
|
304 |
|
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
|
305 |
// 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
|
306 |
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
|
307 |
|
25
ec793cb3e686
rename DB methods to match standard API
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
308 |
connection->commit(); |
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
|
309 |
|
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
|
310 |
// 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
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
} |
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
|
317 |
} // else: we had no file, everything was in memory |
0 | 318 |
} |
319 |
||
320 |
}; |
|
321 |
||
322 |
} |
|
323 |
} |
|
324 |
} |