|
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 |
|
7 * the Free Software Foundation, version 3. |
|
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 |
|
18 #include "Connection.h" |
|
19 |
|
20 namespace relpipe { |
|
21 namespace tr { |
|
22 namespace sql { |
|
23 |
|
24 void Connection::begin() { |
|
25 sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); |
|
26 } |
|
27 |
|
28 Connection::Connection(const char* filename) { |
|
29 int result = sqlite3_open(filename, &db); |
|
30 if (result != SQLITE_OK) { |
|
31 sqlite3_close(db); |
|
32 throw SqlException(L"Unable to open SQLite database."); |
|
33 } |
|
34 begin(); |
|
35 } |
|
36 |
|
37 Connection::~Connection() { |
|
38 sqlite3_close(db); |
|
39 } |
|
40 |
|
41 PreparedStatement* Connection::prepareStatement(const char* sql) { |
|
42 const char* remaining; |
|
43 sqlite3_stmt *stmt; |
|
44 int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
|
45 if (result == SQLITE_OK) return new PreparedStatement(stmt); |
|
46 else throw SqlException(L"Unable to prepare SQLite statement."); |
|
47 } |
|
48 |
|
49 bool Connection::getAutoCommit() { |
|
50 return sqlite3_get_autocommit(db); |
|
51 } |
|
52 |
|
53 void Connection::setAutoCommit(bool autoCommit) { |
|
54 bool autoCommitOld = getAutoCommit(); |
|
55 if (autoCommit && !autoCommitOld) commit(); |
|
56 else if (!autoCommit && autoCommitOld) begin(); |
|
57 } |
|
58 |
|
59 void Connection::commit() { |
|
60 sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); |
|
61 } |
|
62 |
|
63 void Connection::rollback() { |
|
64 sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr); |
|
65 } |
|
66 |
|
67 } |
|
68 } |
|
69 } |