author | František Kučera <franta-hg@frantovo.cz> |
Fri, 31 Jan 2020 23:22:56 +0100 | |
branch | v_0 |
changeset 30 | 629565ff82d3 |
parent 29 | b0ef1e1dc9c8 |
child 32 | 77180ee275df |
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 |
||
29
b0ef1e1dc9c8
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
18 |
#include <sqlite3.h> |
b0ef1e1dc9c8
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
19 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
20 |
#include "Connection.h" |
0 | 21 |
|
22 |
namespace relpipe { |
|
23 |
namespace tr { |
|
24 |
namespace sql { |
|
25 |
||
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
26 |
void Connection::begin() { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
27 |
sqlite3_exec((sqlite3*) db, "BEGIN", nullptr, nullptr, nullptr); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
28 |
} |
3
0b932e05aa9f
sqlite wrapper objects + execute query
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
29 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
30 |
Connection::Connection(const char* filename) { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
31 |
int result = sqlite3_open(filename, (sqlite3**) & db); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
32 |
if (result != SQLITE_OK) { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
33 |
sqlite3_close((sqlite3*) db); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
34 |
throw SqlException(L"Unable to open SQLite database."); |
15
0ecde5272f8e
process SQL input in the relpipe-in-sql mode
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
35 |
} |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
36 |
begin(); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
37 |
} |
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
|
38 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
39 |
Connection::~Connection() { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
40 |
sqlite3_close((sqlite3*) db); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
41 |
} |
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
|
42 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
43 |
PreparedStatement* Connection::prepareStatement(const char* sql) { |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
44 |
const char* remaining; |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
45 |
sqlite3_stmt *stmt; |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
46 |
int result = sqlite3_prepare((sqlite3*) db, sql, -1, &stmt, &remaining); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
47 |
if (result == SQLITE_OK) return new PreparedStatement(stmt); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
48 |
else throw SqlException(L"Unable to prepare SQLite statement."); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
49 |
} |
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
|
50 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
51 |
bool Connection::getAutoCommit() { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
52 |
return sqlite3_get_autocommit((sqlite3*) db); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
53 |
} |
7 | 54 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
55 |
void Connection::setAutoCommit(bool autoCommit) { |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
56 |
bool autoCommitOld = getAutoCommit(); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
57 |
if (autoCommit && !autoCommitOld) commit(); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
58 |
else if (!autoCommit && autoCommitOld) begin(); |
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
59 |
} |
8 | 60 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
61 |
void Connection::commit() { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
62 |
sqlite3_exec((sqlite3*) db, "COMMIT", nullptr, nullptr, nullptr); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
63 |
} |
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
|
64 |
|
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
65 |
void Connection::rollback() { |
30
629565ff82d3
keep sqlite3.h dependency in Connection.cpp and PreparedStatement.cpp only II
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
66 |
sqlite3_exec((sqlite3*) db, "ROLLBACK", nullptr, nullptr, nullptr); |
27
9441a517fa1f
move Connection class to separate .h and .cpp files
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
67 |
} |
0 | 68 |
|
69 |
} |
|
70 |
} |
|
71 |
} |