@op-engineering/op-sqlite 11.2.5 → 11.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/CMakeLists.txt +5 -16
- package/android/build.gradle +1 -1
- package/android/cpp-adapter.cpp +1 -1
- package/android/src/main/java/com/op/sqlite/OPSQLiteBridge.kt +1 -1
- package/android/src/main/java/com/op/sqlite/OPSQLiteModule.kt +1 -1
- package/cpp/DBHostObject.cpp +286 -374
- package/cpp/DBHostObject.h +29 -16
- package/cpp/DumbHostObject.cpp +5 -5
- package/cpp/DumbHostObject.h +8 -11
- package/cpp/PreparedStatementHostObject.cpp +54 -20
- package/cpp/PreparedStatementHostObject.h +15 -11
- package/cpp/SmartHostObject.cpp +5 -4
- package/cpp/SmartHostObject.h +4 -7
- package/cpp/ThreadPool.cpp +9 -8
- package/cpp/ThreadPool.h +4 -7
- package/cpp/bindings.cpp +14 -25
- package/cpp/bindings.h +3 -2
- package/cpp/bridge.cpp +146 -365
- package/cpp/bridge.h +30 -42
- package/cpp/libsql/bridge.cpp +65 -164
- package/cpp/libsql/bridge.h +25 -22
- package/cpp/logs.h +2 -0
- package/cpp/macros.h +8 -4
- package/cpp/sqlite3.c +5622 -2808
- package/cpp/sqlite3.h +180 -22
- package/cpp/types.h +1 -8
- package/cpp/utils.cpp +78 -67
- package/cpp/utils.h +9 -13
- package/ios/OPSQLite.mm +1 -1
- package/lib/commonjs/index.js +4 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +4 -5
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +16 -13
package/cpp/bridge.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define bridge_h
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include "DumbHostObject.h"
|
|
5
4
|
#include "SmartHostObject.h"
|
|
@@ -23,72 +22,61 @@ std::string opsqlite_get_db_path(std::string const &db_name,
|
|
|
23
22
|
std::string const &location);
|
|
24
23
|
|
|
25
24
|
#ifdef OP_SQLITE_USE_SQLCIPHER
|
|
26
|
-
|
|
25
|
+
sqlite3 *opsqlite_open(std::string const &dbName, std::string const &path,
|
|
27
26
|
std::string const &crsqlite_path,
|
|
28
27
|
std::string const &sqlite_vec_path,
|
|
29
|
-
std::string const &
|
|
28
|
+
std::string const &encryption_key);
|
|
30
29
|
#else
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
sqlite3 *opsqlite_open(std::string const &name, std::string const &path,
|
|
31
|
+
[[maybe_unused]] std::string const &crsqlite_path,
|
|
32
|
+
std::string const &sqlite_vec_path);
|
|
34
33
|
#endif
|
|
35
34
|
|
|
36
|
-
|
|
35
|
+
void opsqlite_close(sqlite3 *db);
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
void opsqlite_remove(sqlite3 *db, std::string const &name,
|
|
38
|
+
std::string const &doc_path);
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
|
|
41
|
+
std::string const &doc_path,
|
|
42
|
+
std::string const &secondary_db_name,
|
|
43
|
+
std::string const &alias);
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
void opsqlite_detach(sqlite3 *db, std::string const &main_db_name,
|
|
46
|
+
std::string const &alias);
|
|
48
47
|
|
|
49
|
-
BridgeResult opsqlite_execute(
|
|
48
|
+
BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
|
|
50
49
|
const std::vector<JSVariant> *params);
|
|
51
50
|
|
|
52
51
|
BridgeResult opsqlite_execute_host_objects(
|
|
53
|
-
std::string const &
|
|
54
|
-
|
|
52
|
+
sqlite3 *db, std::string const &query, const std::vector<JSVariant> *params,
|
|
53
|
+
std::vector<DumbHostObject> *results,
|
|
55
54
|
std::shared_ptr<std::vector<SmartHostObject>> &metadatas);
|
|
56
55
|
|
|
57
|
-
BatchResult opsqlite_execute_batch(
|
|
56
|
+
BatchResult opsqlite_execute_batch(sqlite3 *db,
|
|
58
57
|
std::vector<BatchArguments> *commands);
|
|
59
58
|
|
|
60
|
-
BridgeResult opsqlite_execute_raw(std::string const &
|
|
61
|
-
std::string const &query,
|
|
59
|
+
BridgeResult opsqlite_execute_raw(sqlite3 *db, std::string const &query,
|
|
62
60
|
const std::vector<JSVariant> *params,
|
|
63
61
|
std::vector<std::vector<JSVariant>> *results);
|
|
64
62
|
|
|
65
|
-
void
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const CommitCallback &callback);
|
|
72
|
-
BridgeResult opsqlite_deregister_commit_hook(std::string const &dbName);
|
|
73
|
-
BridgeResult opsqlite_register_rollback_hook(std::string const &dbName,
|
|
74
|
-
const RollbackCallback &callback);
|
|
75
|
-
BridgeResult opsqlite_deregister_rollback_hook(std::string const &dbName);
|
|
63
|
+
void opsqlite_register_update_hook(sqlite3 *db, void *db_host_object_ptr);
|
|
64
|
+
void opsqlite_deregister_update_hook(sqlite3 *db);
|
|
65
|
+
void opsqlite_register_commit_hook(sqlite3 *db, void *db_host_object_ptr);
|
|
66
|
+
void opsqlite_deregister_commit_hook(sqlite3 *db);
|
|
67
|
+
void opsqlite_register_rollback_hook(sqlite3 *db, void *db_host_object_ptr);
|
|
68
|
+
void opsqlite_deregister_rollback_hook(sqlite3 *db);
|
|
76
69
|
|
|
77
|
-
sqlite3_stmt *opsqlite_prepare_statement(std::string const &
|
|
78
|
-
std::string const &query);
|
|
70
|
+
sqlite3_stmt *opsqlite_prepare_statement(sqlite3 *db, std::string const &query);
|
|
79
71
|
|
|
80
72
|
void opsqlite_bind_statement(sqlite3_stmt *statement,
|
|
81
73
|
const std::vector<JSVariant> *params);
|
|
82
74
|
|
|
83
75
|
BridgeResult opsqlite_execute_prepared_statement(
|
|
84
|
-
|
|
85
|
-
std::vector<DumbHostObject> *results,
|
|
76
|
+
sqlite3 *db, sqlite3_stmt *statement, std::vector<DumbHostObject> *results,
|
|
86
77
|
std::shared_ptr<std::vector<SmartHostObject>> &metadatas);
|
|
87
78
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
std::string &entry_point);
|
|
79
|
+
void opsqlite_load_extension(sqlite3 *db, std::string &path,
|
|
80
|
+
std::string &entry_point);
|
|
91
81
|
|
|
92
82
|
} // namespace opsqlite
|
|
93
|
-
|
|
94
|
-
#endif /* bridge_h */
|
package/cpp/libsql/bridge.cpp
CHANGED
|
@@ -9,19 +9,6 @@
|
|
|
9
9
|
|
|
10
10
|
namespace opsqlite {
|
|
11
11
|
|
|
12
|
-
struct DB {
|
|
13
|
-
libsql_database_t db;
|
|
14
|
-
libsql_connection_t c;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
std::unordered_map<std::string, DB> db_map =
|
|
18
|
-
std::unordered_map<std::string, DB>();
|
|
19
|
-
|
|
20
|
-
inline void check_db_open(std::string const &db_name) {
|
|
21
|
-
if (db_map.count(db_name) == 0) {
|
|
22
|
-
throw std::runtime_error("[OP-SQLite] DB is not open");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
12
|
// _____ _____
|
|
26
13
|
// /\ | __ \_ _|
|
|
27
14
|
// / \ | |__) || |
|
|
@@ -42,7 +29,7 @@ std::string opsqlite_get_db_path(std::string const &db_name,
|
|
|
42
29
|
return location + "/" + db_name;
|
|
43
30
|
}
|
|
44
31
|
|
|
45
|
-
|
|
32
|
+
DB opsqlite_libsql_open_sync(std::string const &name,
|
|
46
33
|
std::string const &base_path,
|
|
47
34
|
std::string const &url,
|
|
48
35
|
std::string const &auth_token,
|
|
@@ -63,21 +50,19 @@ BridgeResult opsqlite_libsql_open_sync(std::string const &name,
|
|
|
63
50
|
.with_webpki = '1'};
|
|
64
51
|
status = libsql_open_sync_with_config(config, &db, &err);
|
|
65
52
|
if (status != 0) {
|
|
66
|
-
|
|
53
|
+
throw std::runtime_error(err);
|
|
67
54
|
}
|
|
68
55
|
|
|
69
56
|
status = libsql_connect(db, &c, &err);
|
|
70
57
|
|
|
71
58
|
if (status != 0) {
|
|
72
|
-
|
|
59
|
+
throw std::runtime_error(err);
|
|
73
60
|
}
|
|
74
61
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return {.type = SQLiteOk, .affectedRows = 0};
|
|
62
|
+
return {.db= db, .c= c };
|
|
78
63
|
}
|
|
79
64
|
|
|
80
|
-
|
|
65
|
+
DB opsqlite_libsql_open(std::string const &name,
|
|
81
66
|
std::string const &last_path,
|
|
82
67
|
std::string const &crsqlitePath) {
|
|
83
68
|
std::string path = opsqlite_get_db_path(name, last_path);
|
|
@@ -90,13 +75,13 @@ BridgeResult opsqlite_libsql_open(std::string const &name,
|
|
|
90
75
|
status = libsql_open_file(path.c_str(), &db, &err);
|
|
91
76
|
|
|
92
77
|
if (status != 0) {
|
|
93
|
-
|
|
78
|
+
throw std::runtime_error(err);
|
|
94
79
|
}
|
|
95
80
|
|
|
96
81
|
status = libsql_connect(db, &c, &err);
|
|
97
82
|
|
|
98
83
|
if (status != 0) {
|
|
99
|
-
|
|
84
|
+
throw std::runtime_error(err);
|
|
100
85
|
}
|
|
101
86
|
|
|
102
87
|
#ifdef OP_SQLITE_USE_CRSQLITE
|
|
@@ -113,12 +98,10 @@ BridgeResult opsqlite_libsql_open(std::string const &name,
|
|
|
113
98
|
}
|
|
114
99
|
#endif
|
|
115
100
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return {.type = SQLiteOk, .affectedRows = 0};
|
|
101
|
+
return {.db = db, .c = c};
|
|
119
102
|
}
|
|
120
103
|
|
|
121
|
-
|
|
104
|
+
DB opsqlite_libsql_open_remote(std::string const &url,
|
|
122
105
|
std::string const &auth_token) {
|
|
123
106
|
int status;
|
|
124
107
|
libsql_database_t db;
|
|
@@ -129,118 +112,68 @@ BridgeResult opsqlite_libsql_open_remote(std::string const &url,
|
|
|
129
112
|
&err);
|
|
130
113
|
|
|
131
114
|
if (status != 0) {
|
|
132
|
-
|
|
115
|
+
throw std::runtime_error(err);
|
|
133
116
|
}
|
|
134
117
|
|
|
135
118
|
status = libsql_connect(db, &c, &err);
|
|
136
119
|
|
|
137
120
|
if (status != 0) {
|
|
138
|
-
|
|
121
|
+
throw std::runtime_error(err);
|
|
139
122
|
}
|
|
140
123
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return {.type = SQLiteOk, .affectedRows = 0};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
BridgeResult opsqlite_libsql_close(std::string const &name) {
|
|
147
|
-
|
|
148
|
-
check_db_open(name);
|
|
149
|
-
|
|
150
|
-
DB db = db_map[name];
|
|
151
|
-
|
|
152
|
-
libsql_disconnect(db.c);
|
|
153
|
-
libsql_close(db.db);
|
|
154
|
-
|
|
155
|
-
db_map.erase(name);
|
|
156
|
-
|
|
157
|
-
return BridgeResult{
|
|
158
|
-
.type = SQLiteOk,
|
|
159
|
-
};
|
|
124
|
+
return {.db = db, .c = c};
|
|
160
125
|
}
|
|
161
126
|
|
|
162
|
-
void
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
127
|
+
void opsqlite_libsql_close(DB &db) {
|
|
128
|
+
if (db.c != nullptr) {
|
|
129
|
+
libsql_disconnect(db.c);
|
|
130
|
+
db.c = nullptr;
|
|
131
|
+
}
|
|
132
|
+
if (db.db != nullptr) {
|
|
133
|
+
libsql_close(db.db);
|
|
134
|
+
db.db = nullptr;
|
|
168
135
|
}
|
|
169
136
|
}
|
|
170
137
|
|
|
171
|
-
|
|
138
|
+
void opsqlite_libsql_attach(DB const &db,
|
|
172
139
|
std::string const &docPath,
|
|
173
140
|
std::string const &databaseToAttach,
|
|
174
141
|
std::string const &alias) {
|
|
175
142
|
std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath);
|
|
176
143
|
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;
|
|
177
144
|
|
|
178
|
-
|
|
145
|
+
opsqlite_libsql_execute(db, statement, nullptr);
|
|
179
146
|
|
|
180
|
-
if (result.type == SQLiteError) {
|
|
181
|
-
return {
|
|
182
|
-
.type = SQLiteError,
|
|
183
|
-
.message = mainDBName + " was unable to attach another database: " +
|
|
184
|
-
std::string(result.message),
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
.type = SQLiteOk,
|
|
189
|
-
};
|
|
190
147
|
}
|
|
191
148
|
|
|
192
|
-
|
|
149
|
+
void opsqlite_libsql_detach(DB const &db,
|
|
193
150
|
std::string const &alias) {
|
|
194
151
|
std::string statement = "DETACH DATABASE " + alias;
|
|
195
|
-
|
|
196
|
-
if (result.type == SQLiteError) {
|
|
197
|
-
return BridgeResult{
|
|
198
|
-
.type = SQLiteError,
|
|
199
|
-
.message = mainDBName + "was unable to detach database: " +
|
|
200
|
-
std::string(result.message),
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
return BridgeResult{
|
|
204
|
-
.type = SQLiteOk,
|
|
205
|
-
};
|
|
152
|
+
opsqlite_libsql_execute(db, statement, nullptr);
|
|
206
153
|
}
|
|
207
154
|
|
|
208
|
-
|
|
209
|
-
check_db_open(name);
|
|
210
|
-
|
|
211
|
-
auto db = db_map[name].db;
|
|
155
|
+
void opsqlite_libsql_sync(DB const &db) {
|
|
212
156
|
const char *err = nullptr;
|
|
213
157
|
|
|
214
|
-
int status = libsql_sync(db, &err);
|
|
158
|
+
int status = libsql_sync(db.db, &err);
|
|
215
159
|
|
|
216
160
|
if (status != 0) {
|
|
217
|
-
|
|
161
|
+
throw std::runtime_error(err);
|
|
218
162
|
}
|
|
219
163
|
|
|
220
|
-
return {.type = SQLiteOk};
|
|
221
164
|
}
|
|
222
165
|
|
|
223
|
-
|
|
166
|
+
void opsqlite_libsql_remove(DB &db, std::string const &name,
|
|
224
167
|
std::string const &path) {
|
|
225
|
-
|
|
226
|
-
BridgeResult closeResult = opsqlite_libsql_close(name);
|
|
227
|
-
if (closeResult.type == SQLiteError) {
|
|
228
|
-
return closeResult;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
168
|
+
opsqlite_libsql_close(db);
|
|
231
169
|
|
|
232
170
|
std::string full_path = opsqlite_get_db_path(name, path);
|
|
233
171
|
|
|
234
172
|
if (!file_exists(full_path)) {
|
|
235
|
-
|
|
236
|
-
.message = "[op-sqlite]: Database file not found" + full_path};
|
|
173
|
+
throw std::runtime_error("[op-sqlite]: Database file not found" + full_path);
|
|
237
174
|
}
|
|
238
175
|
|
|
239
176
|
remove(full_path.c_str());
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
.type = SQLiteOk,
|
|
243
|
-
};
|
|
244
177
|
}
|
|
245
178
|
|
|
246
179
|
void opsqlite_libsql_bind_statement(libsql_stmt_t statement,
|
|
@@ -255,7 +188,7 @@ void opsqlite_libsql_bind_statement(libsql_stmt_t statement,
|
|
|
255
188
|
|
|
256
189
|
if (std::holds_alternative<bool>(value)) {
|
|
257
190
|
status = libsql_bind_int(statement, index,
|
|
258
|
-
|
|
191
|
+
static_cast<int>(std::get<bool>(value)), &err);
|
|
259
192
|
} else if (std::holds_alternative<int>(value)) {
|
|
260
193
|
status = libsql_bind_int(statement, index, std::get<int>(value), &err);
|
|
261
194
|
} else if (std::holds_alternative<long long>(value)) {
|
|
@@ -282,13 +215,11 @@ void opsqlite_libsql_bind_statement(libsql_stmt_t statement,
|
|
|
282
215
|
}
|
|
283
216
|
|
|
284
217
|
BridgeResult opsqlite_libsql_execute_prepared_statement(
|
|
285
|
-
|
|
218
|
+
DB const &db,
|
|
219
|
+
libsql_stmt_t stmt,
|
|
286
220
|
std::vector<DumbHostObject> *results,
|
|
287
221
|
const std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {
|
|
288
222
|
|
|
289
|
-
check_db_open(name);
|
|
290
|
-
|
|
291
|
-
libsql_connection_t c = db_map[name].c;
|
|
292
223
|
libsql_rows_t rows;
|
|
293
224
|
libsql_row_t row;
|
|
294
225
|
|
|
@@ -298,7 +229,7 @@ BridgeResult opsqlite_libsql_execute_prepared_statement(
|
|
|
298
229
|
status = libsql_query_stmt(stmt, &rows, &err);
|
|
299
230
|
|
|
300
231
|
if (status != 0) {
|
|
301
|
-
|
|
232
|
+
throw std::runtime_error(err);
|
|
302
233
|
}
|
|
303
234
|
|
|
304
235
|
bool metadata_set = false;
|
|
@@ -393,22 +324,17 @@ BridgeResult opsqlite_libsql_execute_prepared_statement(
|
|
|
393
324
|
|
|
394
325
|
libsql_free_rows(rows);
|
|
395
326
|
|
|
396
|
-
unsigned long long changes = libsql_changes(c);
|
|
397
|
-
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
327
|
+
unsigned long long changes = libsql_changes(db.c);
|
|
328
|
+
long long insert_row_id = libsql_last_insert_rowid(db.c);
|
|
398
329
|
|
|
399
330
|
libsql_reset_stmt(stmt, &err);
|
|
400
331
|
|
|
401
|
-
return {.
|
|
402
|
-
.affectedRows = static_cast<int>(changes),
|
|
332
|
+
return {.affectedRows = static_cast<int>(changes),
|
|
403
333
|
.insertId = static_cast<double>(insert_row_id)};
|
|
404
334
|
}
|
|
405
335
|
|
|
406
|
-
libsql_stmt_t opsqlite_libsql_prepare_statement(
|
|
336
|
+
libsql_stmt_t opsqlite_libsql_prepare_statement(DB const &db,
|
|
407
337
|
std::string const &query) {
|
|
408
|
-
check_db_open(name);
|
|
409
|
-
|
|
410
|
-
DB db = db_map[name];
|
|
411
|
-
|
|
412
338
|
libsql_stmt_t stmt;
|
|
413
339
|
|
|
414
340
|
const char *err;
|
|
@@ -422,26 +348,23 @@ libsql_stmt_t opsqlite_libsql_prepare_statement(std::string const &name,
|
|
|
422
348
|
return stmt;
|
|
423
349
|
}
|
|
424
350
|
|
|
425
|
-
BridgeResult opsqlite_libsql_execute(
|
|
351
|
+
BridgeResult opsqlite_libsql_execute(DB const &db,
|
|
426
352
|
std::string const &query,
|
|
427
353
|
const std::vector<JSVariant> *params) {
|
|
428
354
|
|
|
429
|
-
check_db_open(name);
|
|
430
|
-
|
|
431
355
|
std::vector<std::string> column_names;
|
|
432
356
|
std::vector<std::vector<JSVariant>> out_rows;
|
|
433
357
|
std::vector<JSVariant> out_row;
|
|
434
|
-
libsql_connection_t c = db_map[name].c;
|
|
435
358
|
libsql_rows_t rows;
|
|
436
359
|
libsql_row_t row;
|
|
437
360
|
libsql_stmt_t stmt;
|
|
438
361
|
int status;
|
|
439
362
|
const char *err = nullptr;
|
|
440
363
|
|
|
441
|
-
status = libsql_prepare(c, query.c_str(), &stmt, &err);
|
|
364
|
+
status = libsql_prepare(db.c, query.c_str(), &stmt, &err);
|
|
442
365
|
|
|
443
366
|
if (status != 0) {
|
|
444
|
-
|
|
367
|
+
throw std::runtime_error(err);
|
|
445
368
|
}
|
|
446
369
|
|
|
447
370
|
if (params != nullptr && !params->empty()) {
|
|
@@ -451,7 +374,7 @@ BridgeResult opsqlite_libsql_execute(std::string const &name,
|
|
|
451
374
|
status = libsql_query_stmt(stmt, &rows, &err);
|
|
452
375
|
|
|
453
376
|
if (status != 0) {
|
|
454
|
-
|
|
377
|
+
throw std::runtime_error(err);
|
|
455
378
|
}
|
|
456
379
|
|
|
457
380
|
// Get the column names on the first pass
|
|
@@ -533,34 +456,30 @@ BridgeResult opsqlite_libsql_execute(std::string const &name,
|
|
|
533
456
|
libsql_free_rows(rows);
|
|
534
457
|
libsql_free_stmt(stmt);
|
|
535
458
|
|
|
536
|
-
unsigned long long changes = libsql_changes(c);
|
|
537
|
-
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
459
|
+
unsigned long long changes = libsql_changes(db.c);
|
|
460
|
+
long long insert_row_id = libsql_last_insert_rowid(db.c);
|
|
538
461
|
|
|
539
|
-
return {.
|
|
540
|
-
.affectedRows = static_cast<int>(changes),
|
|
462
|
+
return {.affectedRows = static_cast<int>(changes),
|
|
541
463
|
.insertId = static_cast<double>(insert_row_id),
|
|
542
464
|
.rows = std::move(out_rows),
|
|
543
465
|
.column_names = std::move(column_names)};
|
|
544
466
|
}
|
|
545
467
|
|
|
546
468
|
BridgeResult opsqlite_libsql_execute_with_host_objects(
|
|
547
|
-
|
|
469
|
+
DB const &db, std::string const &query,
|
|
548
470
|
const std::vector<JSVariant> *params, std::vector<DumbHostObject> *results,
|
|
549
471
|
const std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {
|
|
550
472
|
|
|
551
|
-
check_db_open(name);
|
|
552
|
-
|
|
553
|
-
libsql_connection_t c = db_map[name].c;
|
|
554
473
|
libsql_rows_t rows;
|
|
555
474
|
libsql_row_t row;
|
|
556
475
|
libsql_stmt_t stmt;
|
|
557
476
|
int status;
|
|
558
477
|
const char *err = nullptr;
|
|
559
478
|
|
|
560
|
-
status = libsql_prepare(c, query.c_str(), &stmt, &err);
|
|
479
|
+
status = libsql_prepare(db.c, query.c_str(), &stmt, &err);
|
|
561
480
|
|
|
562
481
|
if (status != 0) {
|
|
563
|
-
|
|
482
|
+
throw std::runtime_error(err);
|
|
564
483
|
}
|
|
565
484
|
|
|
566
485
|
if (params != nullptr && !params->empty()) {
|
|
@@ -570,7 +489,7 @@ BridgeResult opsqlite_libsql_execute_with_host_objects(
|
|
|
570
489
|
status = libsql_query_stmt(stmt, &rows, &err);
|
|
571
490
|
|
|
572
491
|
if (status != 0) {
|
|
573
|
-
|
|
492
|
+
throw std::runtime_error(err);
|
|
574
493
|
}
|
|
575
494
|
|
|
576
495
|
bool metadata_set = false;
|
|
@@ -666,34 +585,30 @@ BridgeResult opsqlite_libsql_execute_with_host_objects(
|
|
|
666
585
|
libsql_free_rows(rows);
|
|
667
586
|
libsql_free_stmt(stmt);
|
|
668
587
|
|
|
669
|
-
unsigned long long changes = libsql_changes(c);
|
|
670
|
-
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
588
|
+
unsigned long long changes = libsql_changes(db.c);
|
|
589
|
+
long long insert_row_id = libsql_last_insert_rowid(db.c);
|
|
671
590
|
|
|
672
|
-
return {.
|
|
673
|
-
.affectedRows = static_cast<int>(changes),
|
|
591
|
+
return {.affectedRows = static_cast<int>(changes),
|
|
674
592
|
.insertId = static_cast<double>(insert_row_id)};
|
|
675
593
|
}
|
|
676
594
|
|
|
677
595
|
/// Executes returning data in raw arrays, a small performance optimization
|
|
678
596
|
/// for certain use cases
|
|
679
597
|
BridgeResult
|
|
680
|
-
opsqlite_libsql_execute_raw(
|
|
598
|
+
opsqlite_libsql_execute_raw(DB const &db, std::string const &query,
|
|
681
599
|
const std::vector<JSVariant> *params,
|
|
682
600
|
std::vector<std::vector<JSVariant>> *results) {
|
|
683
601
|
|
|
684
|
-
check_db_open(name);
|
|
685
|
-
|
|
686
|
-
libsql_connection_t c = db_map[name].c;
|
|
687
602
|
libsql_rows_t rows;
|
|
688
603
|
libsql_row_t row;
|
|
689
604
|
libsql_stmt_t stmt;
|
|
690
605
|
int status;
|
|
691
606
|
const char *err = nullptr;
|
|
692
607
|
|
|
693
|
-
status = libsql_prepare(c, query.c_str(), &stmt, &err);
|
|
608
|
+
status = libsql_prepare(db.c, query.c_str(), &stmt, &err);
|
|
694
609
|
|
|
695
610
|
if (status != 0) {
|
|
696
|
-
|
|
611
|
+
throw std::runtime_error(err);
|
|
697
612
|
}
|
|
698
613
|
|
|
699
614
|
if (params != nullptr && !params->empty()) {
|
|
@@ -703,7 +618,7 @@ opsqlite_libsql_execute_raw(std::string const &name, std::string const &query,
|
|
|
703
618
|
status = libsql_query_stmt(stmt, &rows, &err);
|
|
704
619
|
|
|
705
620
|
if (status != 0) {
|
|
706
|
-
|
|
621
|
+
throw std::runtime_error(err);
|
|
707
622
|
}
|
|
708
623
|
|
|
709
624
|
int num_cols = libsql_column_count(rows);
|
|
@@ -780,54 +695,40 @@ opsqlite_libsql_execute_raw(std::string const &name, std::string const &query,
|
|
|
780
695
|
libsql_free_rows(rows);
|
|
781
696
|
libsql_free_stmt(stmt);
|
|
782
697
|
|
|
783
|
-
unsigned long long changes = libsql_changes(c);
|
|
784
|
-
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
698
|
+
unsigned long long changes = libsql_changes(db.c);
|
|
699
|
+
long long insert_row_id = libsql_last_insert_rowid(db.c);
|
|
785
700
|
|
|
786
|
-
return {.
|
|
787
|
-
.affectedRows = static_cast<int>(changes),
|
|
701
|
+
return {.affectedRows = static_cast<int>(changes),
|
|
788
702
|
.insertId = static_cast<double>(insert_row_id)};
|
|
789
703
|
}
|
|
790
704
|
|
|
791
705
|
BatchResult
|
|
792
|
-
opsqlite_libsql_execute_batch(
|
|
706
|
+
opsqlite_libsql_execute_batch(DB const &db,
|
|
793
707
|
std::vector<BatchArguments> *commands) {
|
|
794
708
|
size_t commandCount = commands->size();
|
|
795
709
|
if (commandCount <= 0) {
|
|
796
|
-
|
|
797
|
-
.type = SQLiteError,
|
|
798
|
-
.message = "No SQL commands provided",
|
|
799
|
-
};
|
|
710
|
+
throw std::runtime_error("No SQL commands provided");
|
|
800
711
|
}
|
|
801
712
|
|
|
802
713
|
try {
|
|
803
714
|
int affectedRows = 0;
|
|
804
|
-
opsqlite_libsql_execute(
|
|
715
|
+
opsqlite_libsql_execute(db, "BEGIN EXCLUSIVE TRANSACTION", nullptr);
|
|
805
716
|
for (int i = 0; i < commandCount; i++) {
|
|
806
717
|
auto command = commands->at(i);
|
|
807
718
|
// We do not provide a datastructure to receive query data because we
|
|
808
719
|
// don't need/want to handle this results in a batch execution
|
|
809
720
|
auto result =
|
|
810
|
-
opsqlite_libsql_execute(
|
|
811
|
-
if (result.type == SQLiteError) {
|
|
812
|
-
opsqlite_libsql_execute(name, "ROLLBACK", nullptr);
|
|
813
|
-
return BatchResult{
|
|
814
|
-
.type = SQLiteError,
|
|
815
|
-
.message = result.message,
|
|
816
|
-
};
|
|
817
|
-
} else {
|
|
721
|
+
opsqlite_libsql_execute(db, command.sql, command.params.get());
|
|
818
722
|
affectedRows += result.affectedRows;
|
|
819
|
-
}
|
|
820
723
|
}
|
|
821
|
-
opsqlite_libsql_execute(
|
|
724
|
+
opsqlite_libsql_execute(db, "COMMIT", nullptr);
|
|
822
725
|
return BatchResult{
|
|
823
|
-
.type = SQLiteOk,
|
|
824
726
|
.affectedRows = affectedRows,
|
|
825
727
|
.commands = static_cast<int>(commandCount),
|
|
826
728
|
};
|
|
827
729
|
} catch (std::exception &exc) {
|
|
828
|
-
opsqlite_libsql_execute(
|
|
730
|
+
opsqlite_libsql_execute(db, "ROLLBACK", nullptr);
|
|
829
731
|
return BatchResult{
|
|
830
|
-
.type = SQLiteError,
|
|
831
732
|
.message = exc.what(),
|
|
832
733
|
};
|
|
833
734
|
}
|