-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLiteDriver.cpp
More file actions
292 lines (243 loc) · 7.22 KB
/
Copy pathSQLiteDriver.cpp
File metadata and controls
292 lines (243 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
*
* @file SQLiteDriver.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira.
* https://github.com/vixcpp/vix
* MIT license.
*
* Vix.cpp
*/
#include <vix/db/core/Errors.hpp>
#if VIX_DB_HAS_SQLITE
#include <vix/db/drivers/sqlite/SQLiteDriver.hpp>
#include <cstring>
namespace vix::db
{
static void throw_sqlite(sqlite3 *db, const char *prefix)
{
const char *msg = db ? sqlite3_errmsg(db) : "sqlite error";
throw DBError(std::string(prefix) + ": " + msg);
}
// -------------------- Result --------------------
class SQLiteResultRow final : public ResultRow
{
sqlite3_stmt *stmt_ = nullptr;
public:
explicit SQLiteResultRow(sqlite3_stmt *stmt) : stmt_(stmt) {}
bool isNull(std::size_t i) const override
{
return sqlite3_column_type(stmt_, static_cast<int>(i)) == SQLITE_NULL;
}
std::string getString(std::size_t i) const override
{
const unsigned char *txt = sqlite3_column_text(stmt_, static_cast<int>(i));
if (!txt)
return {};
return std::string(reinterpret_cast<const char *>(txt));
}
std::int64_t getInt64(std::size_t i) const override
{
return static_cast<std::int64_t>(sqlite3_column_int64(stmt_, static_cast<int>(i)));
}
double getDouble(std::size_t i) const override
{
return sqlite3_column_double(stmt_, static_cast<int>(i));
}
};
class SQLiteResultSet final : public ResultSet
{
sqlite3_stmt *stmt_ = nullptr;
mutable SQLiteResultRow row_;
bool has_row_ = false;
public:
explicit SQLiteResultSet(sqlite3_stmt *stmt)
: stmt_(stmt), row_(stmt) {}
~SQLiteResultSet() override
{
if (stmt_)
sqlite3_finalize(stmt_);
}
bool next() override
{
if (!stmt_)
return false;
const int rc = sqlite3_step(stmt_);
if (rc == SQLITE_ROW)
{
has_row_ = true;
return true;
}
if (rc == SQLITE_DONE)
{
has_row_ = false;
return false;
}
throw_sqlite(sqlite3_db_handle(stmt_), "SQLite step failed");
return false;
}
std::size_t cols() const override
{
return stmt_ ? static_cast<std::size_t>(sqlite3_column_count(stmt_)) : 0;
}
const ResultRow &row() const override
{
if (!has_row_)
throw DBError("SQLiteResultSet::row() called before next()");
return row_;
}
};
// -------------------- Statement --------------------
class SQLiteStatement final : public Statement
{
sqlite3 *db_ = nullptr;
sqlite3_stmt *stmt_ = nullptr;
static int idx1(std::size_t i)
{
// parameters are 1-based in sqlite bind APIs
return static_cast<int>(i);
}
public:
SQLiteStatement(sqlite3 *db, sqlite3_stmt *stmt)
: db_(db), stmt_(stmt) {}
~SQLiteStatement() override
{
if (stmt_)
sqlite3_finalize(stmt_);
}
void bind(std::size_t idx, const DbValue &v) override
{
if (!stmt_)
throw DBError("SQLiteStatement::bind on null stmt");
const int i = idx1(idx);
const int rc = std::visit(
[&](auto &&val) -> int
{
using T = std::decay_t<decltype(val)>;
if constexpr (std::is_same_v<T, std::nullptr_t>)
{
return sqlite3_bind_null(stmt_, i);
}
else if constexpr (std::is_same_v<T, bool>)
{
return sqlite3_bind_int(stmt_, i, val ? 1 : 0);
}
else if constexpr (std::is_same_v<T, std::int64_t>)
{
return sqlite3_bind_int64(stmt_, i, static_cast<sqlite3_int64>(val));
}
else if constexpr (std::is_same_v<T, double>)
{
return sqlite3_bind_double(stmt_, i, val);
}
else if constexpr (std::is_same_v<T, std::string>)
{
// SQLITE_TRANSIENT => sqlite copies the bytes
return sqlite3_bind_text(stmt_, i, val.c_str(), static_cast<int>(val.size()), SQLITE_TRANSIENT);
}
else if constexpr (std::is_same_v<T, Blob>)
{
const void *p = val.bytes.empty() ? nullptr : val.bytes.data();
const int n = static_cast<int>(val.bytes.size());
return sqlite3_bind_blob(stmt_, i, p, n, SQLITE_TRANSIENT);
}
else
{
return SQLITE_MISUSE;
}
},
v);
if (rc != SQLITE_OK)
throw_sqlite(db_, "SQLite bind failed");
}
std::unique_ptr<ResultSet> query() override
{
if (!stmt_)
throw DBError("SQLiteStatement::query on null stmt");
// query returns ResultSet that owns stmt_
auto *st = stmt_;
stmt_ = nullptr;
return std::make_unique<SQLiteResultSet>(st);
}
std::uint64_t exec() override
{
if (!stmt_)
throw DBError("SQLiteStatement::exec on null stmt");
const int rc = sqlite3_step(stmt_);
if (rc != SQLITE_DONE && rc != SQLITE_ROW)
throw_sqlite(db_, "SQLite exec failed");
const auto changes = static_cast<std::uint64_t>(sqlite3_changes(db_));
// reset for reuse
sqlite3_reset(stmt_);
sqlite3_clear_bindings(stmt_);
return changes;
}
};
// -------------------- Connection --------------------
SQLiteConnection::~SQLiteConnection()
{
if (db_)
sqlite3_close(db_);
}
std::unique_ptr<Statement> SQLiteConnection::prepare(std::string_view sql)
{
if (!db_)
throw DBError("SQLiteConnection::prepare on null db");
sqlite3_stmt *stmt = nullptr;
const int rc = sqlite3_prepare_v2(
db_,
sql.data(),
static_cast<int>(sql.size()),
&stmt,
nullptr);
if (rc != SQLITE_OK || !stmt)
throw_sqlite(db_, "SQLite prepare failed");
return std::make_unique<SQLiteStatement>(db_, stmt);
}
void SQLiteConnection::begin()
{
prepare("BEGIN")->exec();
}
void SQLiteConnection::commit()
{
prepare("COMMIT")->exec();
}
void SQLiteConnection::rollback()
{
prepare("ROLLBACK")->exec();
}
std::uint64_t SQLiteConnection::lastInsertId()
{
if (!db_)
throw DBError("SQLiteConnection::lastInsertId on null db");
return static_cast<std::uint64_t>(sqlite3_last_insert_rowid(db_));
}
sqlite3 *open_sqlite(const std::string &path)
{
sqlite3 *db = nullptr;
const int rc = sqlite3_open(path.c_str(), &db);
if (rc != SQLITE_OK || !db)
{
// sqlite3_open may allocate db even on error; close it
if (db)
sqlite3_close(db);
throw DBError("SQLite open failed for: " + path);
}
// Basic sane defaults (safe + practical)
sqlite3_exec(db, "PRAGMA foreign_keys = ON;", nullptr, nullptr, nullptr);
sqlite3_exec(db, "PRAGMA journal_mode = WAL;", nullptr, nullptr, nullptr);
sqlite3_exec(db, "PRAGMA synchronous = NORMAL;", nullptr, nullptr, nullptr);
return db;
}
ConnectionFactory make_sqlite_factory(std::string path)
{
return [path = std::move(path)]() -> ConnectionPtr
{
sqlite3 *db = open_sqlite(path);
auto c = std::make_shared<SQLiteConnection>(db);
return std::static_pointer_cast<Connection>(c);
};
}
} // namespace vix::db
#endif // VIX_DB_HAS_SQLITE