@op-engineering/op-sqlite 6.0.2-beta1 → 6.0.2-beta4
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 +26 -15
- package/android/build.gradle +10 -1
- package/android/cpp-adapter.cpp +4 -0
- package/android/jniLibs/arm64-v8a/libsql_experimental.a +0 -0
- package/android/jniLibs/armeabi-v7a/libsql_experimental.a +0 -0
- package/android/jniLibs/x86/libsql_experimental.a +0 -0
- package/android/jniLibs/x86_64/libsql_experimental.a +0 -0
- package/cpp/DBHostObject.cpp +234 -87
- package/cpp/DBHostObject.h +19 -10
- package/cpp/PreparedStatementHostObject.cpp +28 -14
- package/cpp/PreparedStatementHostObject.h +18 -14
- package/cpp/bindings.cpp +49 -32
- package/cpp/bindings.h +4 -3
- package/cpp/bridge.cpp +45 -0
- package/cpp/bridge.h +3 -0
- package/cpp/libsql/bridge.cpp +660 -0
- package/cpp/libsql/bridge.h +76 -0
- package/cpp/libsql/libsql.h +133 -0
- package/cpp/sqlite3.h +0 -1
- package/cpp/types.h +5 -0
- package/cpp/utils.cpp +45 -3
- package/cpp/utils.h +2 -3
- package/ios/libsql.xcframework/Info.plist +48 -0
- package/ios/libsql.xcframework/ios-arm64/Headers/libsql.h +133 -0
- package/ios/libsql.xcframework/ios-arm64/libsql_experimental.a +0 -0
- package/ios/libsql.xcframework/ios-arm64_x86_64-simulator/Headers/libsql.h +133 -0
- package/ios/libsql.xcframework/ios-arm64_x86_64-simulator/libsql_experimental.a +0 -0
- package/lib/commonjs/index.js +167 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +164 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts +14 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/op-sqlite.podspec +20 -3
- package/package.json +1 -1
- package/src/index.ts +209 -3
- package/cpp/sqlbatchexecutor.cpp +0 -93
- package/cpp/sqlbatchexecutor.h +0 -30
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
#include "bridge.h"
|
|
2
|
+
#include "DumbHostObject.h"
|
|
3
|
+
#include "SmartHostObject.h"
|
|
4
|
+
#include "logs.h"
|
|
5
|
+
#include "utils.h"
|
|
6
|
+
#include <iostream>
|
|
7
|
+
#include <unordered_map>
|
|
8
|
+
#include <variant>
|
|
9
|
+
|
|
10
|
+
namespace opsqlite {
|
|
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
|
+
// _____ _____
|
|
26
|
+
// /\ | __ \_ _|
|
|
27
|
+
// / \ | |__) || |
|
|
28
|
+
// / /\ \ | ___/ | |
|
|
29
|
+
// / ____ \| | _| |_
|
|
30
|
+
// /_/ \_\_| |_____|
|
|
31
|
+
|
|
32
|
+
/// Returns the completely formed db path, but it also creates any sub-folders
|
|
33
|
+
/// along the way
|
|
34
|
+
std::string opsqlite_get_db_path(std::string const &db_name,
|
|
35
|
+
std::string const &location) {
|
|
36
|
+
|
|
37
|
+
if (location == ":memory:") {
|
|
38
|
+
return location;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
mkdir(location);
|
|
42
|
+
return location + "/" + db_name;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#ifdef OP_SQLITE_USE_SQLCIPHER
|
|
46
|
+
BridgeResult opsqlite_open(std::string const &dbName,
|
|
47
|
+
std::string const &last_path,
|
|
48
|
+
std::string const &crsqlitePath,
|
|
49
|
+
std::string const &encryptionKey) {
|
|
50
|
+
#else
|
|
51
|
+
BridgeResult opsqlite_libsql_open(std::string const &name,
|
|
52
|
+
std::string const &last_path) {
|
|
53
|
+
#endif
|
|
54
|
+
std::string path = opsqlite_get_db_path(name, last_path);
|
|
55
|
+
|
|
56
|
+
int status = 0;
|
|
57
|
+
libsql_database_t db;
|
|
58
|
+
libsql_connection_t c;
|
|
59
|
+
const char *err = NULL;
|
|
60
|
+
|
|
61
|
+
status = libsql_open_file(path.c_str(), &db, &err);
|
|
62
|
+
|
|
63
|
+
if (status != 0) {
|
|
64
|
+
return {.type = SQLiteError, .message = err};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
status = libsql_connect(db, &c, &err);
|
|
68
|
+
|
|
69
|
+
if (status != 0) {
|
|
70
|
+
return {.type = SQLiteError, .message = err};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
db_map[name] = {.db = db, .c = c};
|
|
74
|
+
|
|
75
|
+
#ifdef OP_SQLITE_USE_SQLCIPHER
|
|
76
|
+
opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr,
|
|
77
|
+
nullptr, nullptr);
|
|
78
|
+
#endif
|
|
79
|
+
|
|
80
|
+
return {.type = SQLiteOk, .affectedRows = 0};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
BridgeResult opsqlite_libsql_open_remote(std::string const &url,
|
|
84
|
+
std::string const &auth_token) {
|
|
85
|
+
int status = 0;
|
|
86
|
+
libsql_database_t db;
|
|
87
|
+
libsql_connection_t c;
|
|
88
|
+
const char *err = NULL;
|
|
89
|
+
|
|
90
|
+
status = libsql_open_remote(url.c_str(), auth_token.c_str(), &db, &err);
|
|
91
|
+
|
|
92
|
+
if (status != 0) {
|
|
93
|
+
return {.type = SQLiteError, .message = err};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
status = libsql_connect(db, &c, &err);
|
|
97
|
+
|
|
98
|
+
if (status != 0) {
|
|
99
|
+
return {.type = SQLiteError, .message = err};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
db_map[url] = {.db = db, .c = c};
|
|
103
|
+
|
|
104
|
+
return {.type = SQLiteOk, .affectedRows = 0};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
BridgeResult opsqlite_libsql_close(std::string const &name) {
|
|
108
|
+
|
|
109
|
+
check_db_open(name);
|
|
110
|
+
|
|
111
|
+
DB db = db_map[name];
|
|
112
|
+
|
|
113
|
+
libsql_disconnect(db.c);
|
|
114
|
+
libsql_close(db.db);
|
|
115
|
+
|
|
116
|
+
db_map.erase(name);
|
|
117
|
+
|
|
118
|
+
return BridgeResult{
|
|
119
|
+
.type = SQLiteOk,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void opsqlite_libsql_close_all() {
|
|
124
|
+
// for (auto const &db : db_map) {
|
|
125
|
+
// libsql_close();
|
|
126
|
+
// }
|
|
127
|
+
for (auto const &db : db_map) {
|
|
128
|
+
opsqlite_libsql_close(db.first);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
BridgeResult opsqlite_libsql_attach(std::string const &mainDBName,
|
|
133
|
+
std::string const &docPath,
|
|
134
|
+
std::string const &databaseToAttach,
|
|
135
|
+
std::string const &alias) {
|
|
136
|
+
std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath);
|
|
137
|
+
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;
|
|
138
|
+
|
|
139
|
+
BridgeResult result =
|
|
140
|
+
opsqlite_libsql_execute(mainDBName, statement, nullptr, nullptr, nullptr);
|
|
141
|
+
|
|
142
|
+
if (result.type == SQLiteError) {
|
|
143
|
+
return {
|
|
144
|
+
.type = SQLiteError,
|
|
145
|
+
.message = mainDBName + " was unable to attach another database: " +
|
|
146
|
+
std::string(result.message),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
.type = SQLiteOk,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
BridgeResult opsqlite_libsql_detach(std::string const &mainDBName,
|
|
155
|
+
std::string const &alias) {
|
|
156
|
+
std::string statement = "DETACH DATABASE " + alias;
|
|
157
|
+
BridgeResult result =
|
|
158
|
+
opsqlite_libsql_execute(mainDBName, statement, nullptr, nullptr, nullptr);
|
|
159
|
+
if (result.type == SQLiteError) {
|
|
160
|
+
return BridgeResult{
|
|
161
|
+
.type = SQLiteError,
|
|
162
|
+
.message = mainDBName + "was unable to detach database: " +
|
|
163
|
+
std::string(result.message),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return BridgeResult{
|
|
167
|
+
.type = SQLiteOk,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
BridgeResult opsqlite_libsql_remove(std::string const &name,
|
|
172
|
+
std::string const &path) {
|
|
173
|
+
if (db_map.count(name) == 1) {
|
|
174
|
+
BridgeResult closeResult = opsqlite_libsql_close(name);
|
|
175
|
+
if (closeResult.type == SQLiteError) {
|
|
176
|
+
return closeResult;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
std::string full_path = opsqlite_get_db_path(name, path);
|
|
181
|
+
|
|
182
|
+
if (!file_exists(full_path)) {
|
|
183
|
+
return {.type = SQLiteError,
|
|
184
|
+
.message = "[op-sqlite]: Database file not found" + full_path};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
remove(full_path.c_str());
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
.type = SQLiteOk,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
void opsqlite_libsql_bind_statement(libsql_stmt_t statement,
|
|
195
|
+
const std::vector<JSVariant> *values) {
|
|
196
|
+
const char *err;
|
|
197
|
+
size_t size = values->size();
|
|
198
|
+
|
|
199
|
+
for (int ii = 0; ii < size; ii++) {
|
|
200
|
+
int index = ii + 1;
|
|
201
|
+
JSVariant value = values->at(ii);
|
|
202
|
+
|
|
203
|
+
if (std::holds_alternative<bool>(value)) {
|
|
204
|
+
libsql_bind_int(statement, index, std::get<int>(value), &err);
|
|
205
|
+
} else if (std::holds_alternative<int>(value)) {
|
|
206
|
+
libsql_bind_int(statement, index, std::get<int>(value), &err);
|
|
207
|
+
} else if (std::holds_alternative<long long>(value)) {
|
|
208
|
+
libsql_bind_int(statement, index, std::get<long long>(value), &err);
|
|
209
|
+
} else if (std::holds_alternative<double>(value)) {
|
|
210
|
+
libsql_bind_float(statement, index, std::get<double>(value), &err);
|
|
211
|
+
} else if (std::holds_alternative<std::string>(value)) {
|
|
212
|
+
std::string str = std::get<std::string>(value);
|
|
213
|
+
libsql_bind_string(statement, index, str.c_str(), &err);
|
|
214
|
+
} else if (std::holds_alternative<ArrayBuffer>(value)) {
|
|
215
|
+
ArrayBuffer buffer = std::get<ArrayBuffer>(value);
|
|
216
|
+
libsql_bind_blob(statement, index, buffer.data.get(), buffer.size, &err);
|
|
217
|
+
} else {
|
|
218
|
+
libsql_bind_null(statement, index, &err);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
BridgeResult opsqlite_libsql_execute_prepared_statement(
|
|
224
|
+
std::string const &name, libsql_stmt_t stmt,
|
|
225
|
+
std::vector<DumbHostObject> *results,
|
|
226
|
+
std::shared_ptr<std::vector<SmartHostObject>> metadatas) {
|
|
227
|
+
|
|
228
|
+
check_db_open(name);
|
|
229
|
+
|
|
230
|
+
libsql_connection_t c = db_map[name].c;
|
|
231
|
+
libsql_rows_t rows;
|
|
232
|
+
libsql_row_t row;
|
|
233
|
+
|
|
234
|
+
int status = 0;
|
|
235
|
+
const char *err = NULL;
|
|
236
|
+
|
|
237
|
+
if (status != 0) {
|
|
238
|
+
return {.type = SQLiteError, .message = err};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
status = libsql_query_stmt(stmt, &rows, &err);
|
|
242
|
+
|
|
243
|
+
if (status != 0) {
|
|
244
|
+
return {.type = SQLiteError, .message = err};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
bool metadata_set = false;
|
|
248
|
+
|
|
249
|
+
int num_cols = libsql_column_count(rows);
|
|
250
|
+
while ((status = libsql_next_row(rows, &row, &err)) == 0) {
|
|
251
|
+
|
|
252
|
+
if (!err && !row) {
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
DumbHostObject row_host_object = DumbHostObject(metadatas);
|
|
257
|
+
|
|
258
|
+
for (int col = 0; col < num_cols; col++) {
|
|
259
|
+
int type;
|
|
260
|
+
|
|
261
|
+
libsql_column_type(rows, row, col, &type, &err);
|
|
262
|
+
|
|
263
|
+
switch (type) {
|
|
264
|
+
case LIBSQL_INT:
|
|
265
|
+
long long int_value;
|
|
266
|
+
status = libsql_get_int(row, col, &int_value, &err);
|
|
267
|
+
row_host_object.values.push_back(JSVariant(int_value));
|
|
268
|
+
break;
|
|
269
|
+
|
|
270
|
+
case LIBSQL_FLOAT:
|
|
271
|
+
double float_value;
|
|
272
|
+
status = libsql_get_float(row, col, &float_value, &err);
|
|
273
|
+
row_host_object.values.push_back(JSVariant(float_value));
|
|
274
|
+
break;
|
|
275
|
+
|
|
276
|
+
case LIBSQL_TEXT:
|
|
277
|
+
const char *text_value;
|
|
278
|
+
status = libsql_get_string(row, col, &text_value, &err);
|
|
279
|
+
row_host_object.values.push_back(JSVariant(text_value));
|
|
280
|
+
break;
|
|
281
|
+
|
|
282
|
+
case LIBSQL_BLOB: {
|
|
283
|
+
blob value_blob;
|
|
284
|
+
libsql_get_blob(row, col, &value_blob, &err);
|
|
285
|
+
uint8_t *data = new uint8_t[value_blob.len];
|
|
286
|
+
// You cannot share raw memory between native and JS
|
|
287
|
+
// always copy the data
|
|
288
|
+
memcpy(data, value_blob.ptr, value_blob.len);
|
|
289
|
+
libsql_free_blob(value_blob);
|
|
290
|
+
row_host_object.values.push_back(JSVariant(
|
|
291
|
+
ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
|
|
292
|
+
.size = static_cast<size_t>(value_blob.len)}));
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
case LIBSQL_NULL:
|
|
297
|
+
// intentional fall-through
|
|
298
|
+
default:
|
|
299
|
+
row_host_object.values.push_back(JSVariant(nullptr));
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (status != 0) {
|
|
304
|
+
fprintf(stderr, "%s\n", err);
|
|
305
|
+
throw std::runtime_error("libsql error");
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// On the first interation through the columns, set the metadata
|
|
309
|
+
if (!metadata_set && metadatas != nullptr) {
|
|
310
|
+
const char *col_name;
|
|
311
|
+
status = libsql_column_name(rows, col, &col_name, &err);
|
|
312
|
+
|
|
313
|
+
auto metadata = SmartHostObject();
|
|
314
|
+
metadata.fields.push_back(std::make_pair("name", col_name));
|
|
315
|
+
metadata.fields.push_back(std::make_pair("index", col));
|
|
316
|
+
metadata.fields.push_back(std::make_pair("type", "UNKNOWN"));
|
|
317
|
+
// metadata.fields.push_back(
|
|
318
|
+
// std::make_pair("type", type == -1 ? "UNKNOWN" :
|
|
319
|
+
// type));
|
|
320
|
+
|
|
321
|
+
metadatas->push_back(metadata);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (results != nullptr) {
|
|
326
|
+
results->push_back(row_host_object);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
metadata_set = true;
|
|
330
|
+
err = NULL;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (status != 0) {
|
|
334
|
+
fprintf(stderr, "%s\n", err);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
libsql_free_rows(rows);
|
|
338
|
+
|
|
339
|
+
int changes = libsql_changes(c);
|
|
340
|
+
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
341
|
+
|
|
342
|
+
libsql_reset_stmt(stmt, &err);
|
|
343
|
+
|
|
344
|
+
return {.type = SQLiteOk,
|
|
345
|
+
.affectedRows = changes,
|
|
346
|
+
.insertId = static_cast<double>(insert_row_id)};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
libsql_stmt_t opsqlite_libsql_prepare_statement(std::string const &name,
|
|
350
|
+
std::string const &query) {
|
|
351
|
+
check_db_open(name);
|
|
352
|
+
|
|
353
|
+
DB db = db_map[name];
|
|
354
|
+
|
|
355
|
+
libsql_stmt_t stmt;
|
|
356
|
+
|
|
357
|
+
const char *err;
|
|
358
|
+
|
|
359
|
+
int status = libsql_prepare(db.c, query.c_str(), &stmt, &err);
|
|
360
|
+
|
|
361
|
+
if (status != 0) {
|
|
362
|
+
throw std::runtime_error(err);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return stmt;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/// Base execution function, returns HostObjects to the JS environment
|
|
369
|
+
BridgeResult opsqlite_libsql_execute(
|
|
370
|
+
std::string const &name, std::string const &query,
|
|
371
|
+
const std::vector<JSVariant> *params, std::vector<DumbHostObject> *results,
|
|
372
|
+
std::shared_ptr<std::vector<SmartHostObject>> metadatas) {
|
|
373
|
+
|
|
374
|
+
check_db_open(name);
|
|
375
|
+
|
|
376
|
+
libsql_connection_t c = db_map[name].c;
|
|
377
|
+
libsql_rows_t rows;
|
|
378
|
+
libsql_row_t row;
|
|
379
|
+
libsql_stmt_t stmt;
|
|
380
|
+
int status = 0;
|
|
381
|
+
const char *err = NULL;
|
|
382
|
+
|
|
383
|
+
status = libsql_prepare(c, query.c_str(), &stmt, &err);
|
|
384
|
+
|
|
385
|
+
if (status != 0) {
|
|
386
|
+
return {.type = SQLiteError, .message = err};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (params != nullptr && params->size() > 0) {
|
|
390
|
+
opsqlite_libsql_bind_statement(stmt, params);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
status = libsql_query_stmt(stmt, &rows, &err);
|
|
394
|
+
|
|
395
|
+
if (status != 0) {
|
|
396
|
+
return {.type = SQLiteError, .message = err};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
bool metadata_set = false;
|
|
400
|
+
|
|
401
|
+
int num_cols = libsql_column_count(rows);
|
|
402
|
+
while ((status = libsql_next_row(rows, &row, &err)) == 0) {
|
|
403
|
+
|
|
404
|
+
if (!err && !row) {
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
DumbHostObject row_host_object = DumbHostObject(metadatas);
|
|
409
|
+
|
|
410
|
+
for (int col = 0; col < num_cols; col++) {
|
|
411
|
+
int type;
|
|
412
|
+
|
|
413
|
+
libsql_column_type(rows, row, col, &type, &err);
|
|
414
|
+
|
|
415
|
+
switch (type) {
|
|
416
|
+
case LIBSQL_INT:
|
|
417
|
+
long long int_value;
|
|
418
|
+
status = libsql_get_int(row, col, &int_value, &err);
|
|
419
|
+
row_host_object.values.push_back(JSVariant(int_value));
|
|
420
|
+
break;
|
|
421
|
+
|
|
422
|
+
case LIBSQL_FLOAT:
|
|
423
|
+
double float_value;
|
|
424
|
+
status = libsql_get_float(row, col, &float_value, &err);
|
|
425
|
+
row_host_object.values.push_back(JSVariant(float_value));
|
|
426
|
+
break;
|
|
427
|
+
|
|
428
|
+
case LIBSQL_TEXT:
|
|
429
|
+
const char *text_value;
|
|
430
|
+
status = libsql_get_string(row, col, &text_value, &err);
|
|
431
|
+
row_host_object.values.push_back(JSVariant(text_value));
|
|
432
|
+
break;
|
|
433
|
+
|
|
434
|
+
case LIBSQL_BLOB: {
|
|
435
|
+
blob value_blob;
|
|
436
|
+
libsql_get_blob(row, col, &value_blob, &err);
|
|
437
|
+
uint8_t *data = new uint8_t[value_blob.len];
|
|
438
|
+
// You cannot share raw memory between native and JS
|
|
439
|
+
// always copy the data
|
|
440
|
+
memcpy(data, value_blob.ptr, value_blob.len);
|
|
441
|
+
libsql_free_blob(value_blob);
|
|
442
|
+
row_host_object.values.push_back(JSVariant(
|
|
443
|
+
ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
|
|
444
|
+
.size = static_cast<size_t>(value_blob.len)}));
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
case LIBSQL_NULL:
|
|
449
|
+
// intentional fall-through
|
|
450
|
+
default:
|
|
451
|
+
row_host_object.values.push_back(JSVariant(nullptr));
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (status != 0) {
|
|
456
|
+
fprintf(stderr, "%s\n", err);
|
|
457
|
+
throw std::runtime_error("libsql error");
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// On the first interation through the columns, set the metadata
|
|
461
|
+
if (!metadata_set && metadatas != nullptr) {
|
|
462
|
+
const char *col_name;
|
|
463
|
+
status = libsql_column_name(rows, col, &col_name, &err);
|
|
464
|
+
|
|
465
|
+
auto metadata = SmartHostObject();
|
|
466
|
+
metadata.fields.push_back(std::make_pair("name", col_name));
|
|
467
|
+
metadata.fields.push_back(std::make_pair("index", col));
|
|
468
|
+
metadata.fields.push_back(std::make_pair("type", "UNKNOWN"));
|
|
469
|
+
// metadata.fields.push_back(
|
|
470
|
+
// std::make_pair("type", type == -1 ? "UNKNOWN" :
|
|
471
|
+
// type));
|
|
472
|
+
|
|
473
|
+
metadatas->push_back(metadata);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (results != nullptr) {
|
|
478
|
+
results->push_back(row_host_object);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
metadata_set = true;
|
|
482
|
+
err = NULL;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (status != 0) {
|
|
486
|
+
fprintf(stderr, "%s\n", err);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
libsql_free_rows(rows);
|
|
490
|
+
libsql_free_stmt(stmt);
|
|
491
|
+
|
|
492
|
+
int changes = libsql_changes(c);
|
|
493
|
+
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
494
|
+
|
|
495
|
+
return {.type = SQLiteOk,
|
|
496
|
+
.affectedRows = changes,
|
|
497
|
+
.insertId = static_cast<double>(insert_row_id)};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/// Executes returning data in raw arrays, a small performance optimization
|
|
501
|
+
/// for certain use cases
|
|
502
|
+
BridgeResult
|
|
503
|
+
opsqlite_libsql_execute_raw(std::string const &name, std::string const &query,
|
|
504
|
+
const std::vector<JSVariant> *params,
|
|
505
|
+
std::vector<std::vector<JSVariant>> *results) {
|
|
506
|
+
|
|
507
|
+
check_db_open(name);
|
|
508
|
+
|
|
509
|
+
libsql_connection_t c = db_map[name].c;
|
|
510
|
+
libsql_rows_t rows;
|
|
511
|
+
libsql_row_t row;
|
|
512
|
+
libsql_stmt_t stmt;
|
|
513
|
+
int status = 0;
|
|
514
|
+
const char *err = NULL;
|
|
515
|
+
|
|
516
|
+
status = libsql_prepare(c, query.c_str(), &stmt, &err);
|
|
517
|
+
|
|
518
|
+
if (status != 0) {
|
|
519
|
+
return {.type = SQLiteError, .message = err};
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (params != nullptr && params->size() > 0) {
|
|
523
|
+
opsqlite_libsql_bind_statement(stmt, params);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
status = libsql_query_stmt(stmt, &rows, &err);
|
|
527
|
+
|
|
528
|
+
if (status != 0) {
|
|
529
|
+
return {.type = SQLiteError, .message = err};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
int num_cols = libsql_column_count(rows);
|
|
533
|
+
while ((status = libsql_next_row(rows, &row, &err)) == 0) {
|
|
534
|
+
|
|
535
|
+
if (!err && !row) {
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
std::vector<JSVariant> row_vector;
|
|
540
|
+
|
|
541
|
+
for (int col = 0; col < num_cols; col++) {
|
|
542
|
+
int type;
|
|
543
|
+
|
|
544
|
+
libsql_column_type(rows, row, col, &type, &err);
|
|
545
|
+
|
|
546
|
+
switch (type) {
|
|
547
|
+
case LIBSQL_INT:
|
|
548
|
+
long long int_value;
|
|
549
|
+
status = libsql_get_int(row, col, &int_value, &err);
|
|
550
|
+
row_vector.push_back(JSVariant(int_value));
|
|
551
|
+
break;
|
|
552
|
+
|
|
553
|
+
case LIBSQL_FLOAT:
|
|
554
|
+
double float_value;
|
|
555
|
+
status = libsql_get_float(row, col, &float_value, &err);
|
|
556
|
+
row_vector.push_back(JSVariant(float_value));
|
|
557
|
+
break;
|
|
558
|
+
|
|
559
|
+
case LIBSQL_TEXT:
|
|
560
|
+
const char *text_value;
|
|
561
|
+
status = libsql_get_string(row, col, &text_value, &err);
|
|
562
|
+
row_vector.push_back(JSVariant(text_value));
|
|
563
|
+
break;
|
|
564
|
+
|
|
565
|
+
case LIBSQL_BLOB: {
|
|
566
|
+
blob value_blob;
|
|
567
|
+
libsql_get_blob(row, col, &value_blob, &err);
|
|
568
|
+
uint8_t *data = new uint8_t[value_blob.len];
|
|
569
|
+
// You cannot share raw memory between native and JS
|
|
570
|
+
// always copy the data
|
|
571
|
+
memcpy(data, value_blob.ptr, value_blob.len);
|
|
572
|
+
libsql_free_blob(value_blob);
|
|
573
|
+
row_vector.push_back(JSVariant(
|
|
574
|
+
ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
|
|
575
|
+
.size = static_cast<size_t>(value_blob.len)}));
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
case LIBSQL_NULL:
|
|
580
|
+
// intentional fall-through
|
|
581
|
+
default:
|
|
582
|
+
row_vector.push_back(JSVariant(nullptr));
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (status != 0) {
|
|
587
|
+
fprintf(stderr, "%s\n", err);
|
|
588
|
+
throw std::runtime_error("libsql error");
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (results != nullptr) {
|
|
593
|
+
results->push_back(row_vector);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
err = NULL;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (status != 0) {
|
|
600
|
+
fprintf(stderr, "%s\n", err);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
libsql_free_rows(rows);
|
|
604
|
+
libsql_free_stmt(stmt);
|
|
605
|
+
|
|
606
|
+
int changes = libsql_changes(c);
|
|
607
|
+
long long insert_row_id = libsql_last_insert_rowid(c);
|
|
608
|
+
|
|
609
|
+
return {.type = SQLiteOk,
|
|
610
|
+
.affectedRows = changes,
|
|
611
|
+
.insertId = static_cast<double>(insert_row_id)};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
BatchResult
|
|
615
|
+
opsqlite_libsql_execute_batch(std::string const &name,
|
|
616
|
+
std::vector<BatchArguments> *commands) {
|
|
617
|
+
size_t commandCount = commands->size();
|
|
618
|
+
if (commandCount <= 0) {
|
|
619
|
+
return BatchResult{
|
|
620
|
+
.type = SQLiteError,
|
|
621
|
+
.message = "No SQL commands provided",
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
try {
|
|
626
|
+
int affectedRows = 0;
|
|
627
|
+
opsqlite_libsql_execute(name, "BEGIN EXCLUSIVE TRANSACTION", nullptr,
|
|
628
|
+
nullptr, nullptr);
|
|
629
|
+
for (int i = 0; i < commandCount; i++) {
|
|
630
|
+
auto command = commands->at(i);
|
|
631
|
+
// We do not provide a datastructure to receive query data because we
|
|
632
|
+
// don't need/want to handle this results in a batch execution
|
|
633
|
+
auto result = opsqlite_libsql_execute(
|
|
634
|
+
name, command.sql, command.params.get(), nullptr, nullptr);
|
|
635
|
+
if (result.type == SQLiteError) {
|
|
636
|
+
opsqlite_libsql_execute(name, "ROLLBACK", nullptr, nullptr, nullptr);
|
|
637
|
+
return BatchResult{
|
|
638
|
+
.type = SQLiteError,
|
|
639
|
+
.message = result.message,
|
|
640
|
+
};
|
|
641
|
+
} else {
|
|
642
|
+
affectedRows += result.affectedRows;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
opsqlite_libsql_execute(name, "COMMIT", nullptr, nullptr, nullptr);
|
|
646
|
+
return BatchResult{
|
|
647
|
+
.type = SQLiteOk,
|
|
648
|
+
.affectedRows = affectedRows,
|
|
649
|
+
.commands = static_cast<int>(commandCount),
|
|
650
|
+
};
|
|
651
|
+
} catch (std::exception &exc) {
|
|
652
|
+
opsqlite_libsql_execute(name, "ROLLBACK", nullptr, nullptr, nullptr);
|
|
653
|
+
return BatchResult{
|
|
654
|
+
.type = SQLiteError,
|
|
655
|
+
.message = exc.what(),
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
} // namespace opsqlite
|