forked from OP-Engineering/op-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.cpp
More file actions
870 lines (702 loc) · 29 KB
/
Copy pathbridge.cpp
File metadata and controls
870 lines (702 loc) · 29 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// This file contains pure sqlite operations without JSI interaction
// Allows a clear defined boundary between the JSI and the SQLite operations
// so that threading operations are safe and contained within DBHostObject
#include "bridge.h"
#include "DBHostObject.h"
#include "DumbHostObject.h"
#include "SmartHostObject.h"
#include "logs.h"
#include "utils.h"
#include <filesystem>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <unordered_map>
#include <variant>
#ifdef TOKENIZERS_HEADER_PATH
#include TOKENIZERS_HEADER_PATH
#else
#define TOKENIZER_LIST
#endif
namespace opsqlite {
inline void opsqlite_bind_statement(sqlite3_stmt *statement,
const std::vector<JSVariant> *values) {
sqlite3_clear_bindings(statement);
size_t size = values->size();
for (int ii = 0; ii < size; ii++) {
int stmt_index = ii + 1;
JSVariant value = values->at(ii);
std::visit(
[&](auto &&v) {
using T = std::decay_t<decltype(v)>;
if constexpr (std::is_same_v<T, bool>) {
sqlite3_bind_int(statement, stmt_index,
static_cast<int>(v));
} else if constexpr (std::is_same_v<T, int>) {
sqlite3_bind_int(statement, stmt_index, v);
} else if constexpr (std::is_same_v<T, long long>) {
sqlite3_bind_double(statement, stmt_index,
static_cast<double>(v));
} else if constexpr (std::is_same_v<T, double>) {
sqlite3_bind_double(statement, stmt_index, v);
} else if constexpr (std::is_same_v<T, std::string>) {
sqlite3_bind_text(statement, stmt_index, v.c_str(),
static_cast<int>(v.length()),
SQLITE_TRANSIENT);
} else if constexpr (std::is_same_v<T, ArrayBuffer>) {
sqlite3_bind_blob(statement, stmt_index, v.data.get(),
static_cast<int>(v.size),
SQLITE_TRANSIENT);
} else {
sqlite3_bind_null(statement, stmt_index);
}
},
value);
}
}
/// Returns the completely formed db path, but it also creates any sub-folders
/// along the way
std::string opsqlite_get_db_path(std::string const &db_name,
std::string const &location) {
if (location == ":memory:") {
return location;
}
// Will return false if the directory already exists, no need to check
std::filesystem::create_directories(location);
if (!location.empty() && location.back() != '/') {
return location + "/" + db_name;
}
return location + db_name;
}
#ifdef OP_SQLITE_USE_SQLCIPHER
sqlite3 *opsqlite_open(std::string const &name, std::string const &path,
std::string const &crsqlite_path,
std::string const &sqlite_vec_path,
std::string const &encryption_key) {
#else
sqlite3 *opsqlite_open(std::string const &name, std::string const &path,
[[maybe_unused]] std::string const &crsqlite_path,
[[maybe_unused]] std::string const &sqlite_vec_path) {
#endif
std::string final_path = opsqlite_get_db_path(name, path);
char *errMsg;
sqlite3 *db;
int flags =
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
int status = sqlite3_open_v2(final_path.c_str(), &db, flags, nullptr);
if (status != SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(db));
}
#ifdef OP_SQLITE_USE_SQLCIPHER
if (!encryption_key.empty()) {
opsqlite_execute(db, "PRAGMA key = '" + encryption_key + "'", nullptr);
}
#endif
#ifndef OP_SQLITE_USE_PHONE_VERSION
sqlite3_enable_load_extension(db, 1);
#endif
#ifdef OP_SQLITE_USE_CRSQLITE
const char *crsqliteEntryPoint = "sqlite3_crsqlite_init";
sqlite3_load_extension(db, crsqlite_path.c_str(), crsqliteEntryPoint,
&errMsg);
if (errMsg != nullptr) {
throw std::runtime_error(errMsg);
}
#endif
#ifdef OP_SQLITE_USE_SQLITE_VEC
const char *vec_entry_point = "sqlite3_vec_init";
sqlite3_load_extension(db, sqlite_vec_path.c_str(), vec_entry_point,
&errMsg);
if (errMsg != nullptr) {
throw std::runtime_error(errMsg);
}
#endif
TOKENIZER_LIST
return db;
}
void opsqlite_close(sqlite3 *db) {
#ifdef OP_SQLITE_USE_CRSQLITE
opsqlite_execute(db, "select crsql_finalize();", nullptr);
#endif
sqlite3_close_v2(db);
}
void opsqlite_attach(sqlite3 *db, std::string const &doc_path,
std::string const &secondary_db_name,
std::string const &alias) {
auto secondary_db_path = opsqlite_get_db_path(secondary_db_name, doc_path);
auto statement = "ATTACH DATABASE '" + secondary_db_path + "' AS " + alias;
opsqlite_execute(db, statement, nullptr);
}
void opsqlite_detach(sqlite3 *db, std::string const &alias) {
std::string statement = "DETACH DATABASE " + alias;
opsqlite_execute(db, statement, nullptr);
}
void opsqlite_remove(sqlite3 *db, std::string const &name,
std::string const &doc_path) {
opsqlite_close(db);
std::string db_path = opsqlite_get_db_path(name, doc_path);
if (!file_exists(db_path)) {
throw std::runtime_error("op-sqlite: db file not found:" + db_path);
}
remove(db_path.c_str());
}
BridgeResult opsqlite_execute_prepared_statement(
sqlite3 *db, sqlite3_stmt *statement, std::vector<DumbHostObject> *results,
std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {
const char *errorMessage;
bool isConsuming = true;
bool isFailed = false;
int result = SQLITE_OK;
int i, count, column_type;
std::string column_name, column_declared_type;
while (isConsuming) {
result = sqlite3_step(statement);
switch (result) {
case SQLITE_ROW: {
i = 0;
DumbHostObject row = DumbHostObject(metadatas);
count = sqlite3_column_count(statement);
while (i < count) {
column_type = sqlite3_column_type(statement, i);
switch (column_type) {
case SQLITE_INTEGER: {
/**
* Warning this will loose precision because JS can
* only represent Integers up to 53 bits
*/
double column_value = sqlite3_column_double(statement, i);
row.values.emplace_back(column_value);
break;
}
case SQLITE_FLOAT: {
double column_value = sqlite3_column_double(statement, i);
row.values.emplace_back(column_value);
break;
}
case SQLITE_TEXT: {
const char *column_value = reinterpret_cast<const char *>(
sqlite3_column_text(statement, i));
int byteLen = sqlite3_column_bytes(statement, i);
// Specify length too; in case string contains NULL in the
// middle
row.values.emplace_back(std::string(column_value, byteLen));
break;
}
case SQLITE_BLOB: {
int blob_size = sqlite3_column_bytes(statement, i);
const void *blob = sqlite3_column_blob(statement, i);
auto *data = new uint8_t[blob_size];
// You cannot share raw memory between native and JS
// always copy the data
memcpy(data, blob, blob_size);
row.values.emplace_back(
ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
.size = static_cast<size_t>(blob_size)});
break;
}
case SQLITE_NULL:
// Intentionally left blank
default:
row.values.emplace_back(nullptr);
break;
}
i++;
}
results->emplace_back(row);
break;
}
case SQLITE_DONE:
if (metadatas != nullptr) {
i = 0;
count = sqlite3_column_count(statement);
while (i < count) {
column_name = sqlite3_column_name(statement, i);
const char *type = sqlite3_column_decltype(statement, i);
auto metadata = SmartHostObject();
metadata.fields.emplace_back("name", column_name);
metadata.fields.emplace_back("index", i);
metadata.fields.emplace_back(
"type", type == nullptr ? "UNKNOWN" : type);
metadatas->emplace_back(metadata);
i++;
}
}
isConsuming = false;
break;
default:
errorMessage = sqlite3_errmsg(db);
isFailed = true;
isConsuming = false;
}
}
sqlite3_reset(statement);
if (isFailed) {
throw std::runtime_error(
"[op-sqlite] SQLite code: " + std::to_string(result) +
" execution error: " + std::string(errorMessage));
}
int changedRowCount = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return {.affectedRows = changedRowCount,
.insertId = static_cast<double>(latestInsertRowId)};
}
sqlite3_stmt *opsqlite_prepare_statement(sqlite3 *db,
std::string const &query) {
sqlite3_stmt *statement;
const char *queryStr = query.c_str();
int statementStatus =
sqlite3_prepare_v2(db, queryStr, -1, &statement, nullptr);
if (statementStatus == SQLITE_ERROR) {
const char *message = sqlite3_errmsg(db);
throw std::runtime_error("[op-sqlite] SQL prepare statement error: " +
std::string(message));
}
return statement;
}
BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
const std::vector<JSVariant> *params) {
sqlite3_stmt *statement;
const char *errorMessage = nullptr;
const char *remainingStatement = nullptr;
bool has_failed = false;
int status, current_column, column_count, column_type;
std::string column_name, column_declared_type;
std::vector<std::string> column_names;
std::vector<std::vector<JSVariant>> rows;
rows.reserve(20);
std::vector<JSVariant> row;
do {
const char *query_str =
remainingStatement == nullptr ? query.c_str() : remainingStatement;
status = sqlite3_prepare_v2(db, query_str, -1, &statement,
&remainingStatement);
if (status != SQLITE_OK) {
errorMessage = sqlite3_errmsg(db);
throw std::runtime_error("[op-sqlite] sqlite query error: " +
std::string(errorMessage));
}
// The statement did not fail to parse but there is nothing to do, just
// skip to the end
if (statement == nullptr) {
continue;
}
if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params);
}
column_count = sqlite3_column_count(statement);
column_names.reserve(column_count);
bool is_consuming_rows = true;
double double_value;
const char *string_value;
// Do a first pass to get the column names
for (int i = 0; i < column_count; i++) {
column_name = sqlite3_column_name(statement, i);
column_names.emplace_back(column_name);
}
while (is_consuming_rows) {
status = sqlite3_step(statement);
switch (status) {
case SQLITE_ROW:
current_column = 0;
row = std::vector<JSVariant>();
row.reserve(column_count);
while (current_column < column_count) {
column_type =
sqlite3_column_type(statement, current_column);
switch (column_type) {
case SQLITE_INTEGER:
// intentional fallthrough
case SQLITE_FLOAT: {
double_value =
sqlite3_column_double(statement, current_column);
row.emplace_back(double_value);
break;
}
case SQLITE_TEXT: {
string_value = reinterpret_cast<const char *>(
sqlite3_column_text(statement, current_column));
int len =
sqlite3_column_bytes(statement, current_column);
// Specify length too; in case string contains NULL in
// the middle
row.emplace_back(std::string(string_value, len));
break;
}
case SQLITE_BLOB: {
int blob_size =
sqlite3_column_bytes(statement, current_column);
const void *blob =
sqlite3_column_blob(statement, current_column);
auto *data = new uint8_t[blob_size];
memcpy(data, blob, blob_size);
row.emplace_back(ArrayBuffer{
.data = std::shared_ptr<uint8_t>{data},
.size = static_cast<size_t>(blob_size)});
break;
}
case SQLITE_NULL:
// Intentionally left blank to switch to default case
default:
row.emplace_back(nullptr);
break;
}
current_column++;
}
rows.emplace_back(std::move(row));
break;
case SQLITE_DONE:
is_consuming_rows = false;
break;
default:
has_failed = true;
is_consuming_rows = false;
}
}
sqlite3_finalize(statement);
} while (remainingStatement != nullptr &&
strcmp(remainingStatement, "") != 0 && !has_failed);
if (has_failed) {
const char *message = sqlite3_errmsg(db);
throw std::runtime_error("[op-sqlite] statement execution error: " +
std::string(message));
}
int changedRowCount = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return {.affectedRows = changedRowCount,
.insertId = static_cast<double>(latestInsertRowId),
.rows = std::move(rows),
.column_names = std::move(column_names)};
}
BridgeResult opsqlite_execute_host_objects(
sqlite3 *db, std::string const &query, const std::vector<JSVariant> *params,
std::vector<DumbHostObject> *results,
std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {
sqlite3_stmt *statement;
const char *errorMessage;
const char *remainingStatement = nullptr;
bool isConsuming = true;
bool isFailed = false;
int result = SQLITE_OK;
do {
const char *queryStr =
remainingStatement == nullptr ? query.c_str() : remainingStatement;
int statementStatus = sqlite3_prepare_v2(db, queryStr, -1, &statement,
&remainingStatement);
if (statementStatus != SQLITE_OK) {
const char *message = sqlite3_errmsg(db);
throw std::runtime_error(
"[op-sqlite] SQL statement error on opsqlite_execute:\n" +
std::to_string(statementStatus) + " description:\n" +
std::string(message));
}
// The statement did not fail to parse but there is nothing to do, just
// skip to the end
if (statement == nullptr) {
continue;
}
if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params);
}
int i, count, column_type;
std::string column_name, column_declared_type;
while (isConsuming) {
result = sqlite3_step(statement);
switch (result) {
case SQLITE_ROW: {
if (results == nullptr) {
break;
}
i = 0;
DumbHostObject row = DumbHostObject(metadatas);
count = sqlite3_column_count(statement);
while (i < count) {
column_type = sqlite3_column_type(statement, i);
switch (column_type) {
case SQLITE_INTEGER: {
/**
* Warning this will loose precision because JS can
* only represent Integers up to 53 bits
*/
double column_value =
sqlite3_column_double(statement, i);
row.values.emplace_back(column_value);
break;
}
case SQLITE_FLOAT: {
double column_value =
sqlite3_column_double(statement, i);
row.values.emplace_back(column_value);
break;
}
case SQLITE_TEXT: {
const char *column_value =
reinterpret_cast<const char *>(
sqlite3_column_text(statement, i));
int byteLen = sqlite3_column_bytes(statement, i);
// Specify length too; in case string contains NULL in
// the middle
row.values.emplace_back(
std::string(column_value, byteLen));
break;
}
case SQLITE_BLOB: {
int blob_size = sqlite3_column_bytes(statement, i);
const void *blob = sqlite3_column_blob(statement, i);
auto *data = new uint8_t[blob_size];
// You cannot share raw memory between native and JS
// always copy the data
memcpy(data, blob, blob_size);
row.values.emplace_back(ArrayBuffer{
.data = std::shared_ptr<uint8_t>{data},
.size = static_cast<size_t>(blob_size)});
break;
}
case SQLITE_NULL:
// Intentionally left blank
default:
row.values.emplace_back(nullptr);
break;
}
i++;
}
results->emplace_back(row);
break;
}
case SQLITE_DONE:
if (metadatas != nullptr) {
i = 0;
count = sqlite3_column_count(statement);
while (i < count) {
column_name = sqlite3_column_name(statement, i);
const char *type =
sqlite3_column_decltype(statement, i);
auto metadata = SmartHostObject();
metadata.fields.emplace_back("name", column_name);
metadata.fields.emplace_back("index", i);
metadata.fields.emplace_back(
"type", type == nullptr ? "UNKNOWN" : type);
metadatas->push_back(metadata);
i++;
}
}
isConsuming = false;
break;
default:
errorMessage = sqlite3_errmsg(db);
isFailed = true;
isConsuming = false;
}
}
sqlite3_finalize(statement);
} while (remainingStatement != nullptr &&
strcmp(remainingStatement, "") != 0 && !isFailed);
if (isFailed) {
throw std::runtime_error(
"[op-sqlite] SQLite error code: " + std::to_string(result) +
", description: " + std::string(errorMessage));
}
int changedRowCount = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return {.affectedRows = changedRowCount,
.insertId = static_cast<double>(latestInsertRowId)};
}
/// Executes returning data in raw arrays, a small performance optimization
/// for certain use cases
BridgeResult
opsqlite_execute_raw(sqlite3 *db, std::string const &query,
const std::vector<JSVariant> *params,
std::vector<std::vector<JSVariant>> *results) {
sqlite3_stmt *statement;
const char *errorMessage;
const char *remainingStatement = nullptr;
bool isConsuming = true;
bool isFailed = false;
int step = SQLITE_OK;
do {
const char *queryStr =
remainingStatement == nullptr ? query.c_str() : remainingStatement;
int statementStatus = sqlite3_prepare_v2(db, queryStr, -1, &statement,
&remainingStatement);
if (statementStatus != SQLITE_OK) {
const char *message = sqlite3_errmsg(db);
throw std::runtime_error("[op-sqlite] SQL statement error:" +
std::to_string(statementStatus) +
" description:" + std::string(message));
}
// The statement did not fail to parse but there is nothing to do, just
// skip to the end
if (statement == nullptr) {
continue;
}
if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params);
}
int i, column_type;
std::string column_name, column_declared_type;
int column_count = sqlite3_column_count(statement);
while (isConsuming) {
step = sqlite3_step(statement);
switch (step) {
case SQLITE_ROW: {
if (results == nullptr) {
break;
}
std::vector<JSVariant> row;
row.reserve(column_count);
i = 0;
while (i < column_count) {
column_type = sqlite3_column_type(statement, i);
switch (column_type) {
case SQLITE_INTEGER:
case SQLITE_FLOAT: {
double column_value =
sqlite3_column_double(statement, i);
row.emplace_back(column_value);
break;
}
case SQLITE_TEXT: {
const char *column_value =
reinterpret_cast<const char *>(
sqlite3_column_text(statement, i));
int byteLen = sqlite3_column_bytes(statement, i);
// Specify length too; in case string contains NULL in
// the middle
row.emplace_back(std::string(column_value, byteLen));
break;
}
case SQLITE_BLOB: {
int blob_size = sqlite3_column_bytes(statement, i);
const void *blob = sqlite3_column_blob(statement, i);
auto *data = new uint8_t[blob_size];
memcpy(data, blob, blob_size);
row.emplace_back(ArrayBuffer{
.data = std::shared_ptr<uint8_t>{data},
.size = static_cast<size_t>(blob_size)});
break;
}
case SQLITE_NULL:
// intentional fallthrough
default:
row.emplace_back(nullptr);
break;
}
i++;
}
results->emplace_back(row);
break;
}
case SQLITE_DONE:
isConsuming = false;
break;
default:
errorMessage = sqlite3_errmsg(db);
isFailed = true;
isConsuming = false;
}
}
sqlite3_finalize(statement);
} while (remainingStatement != nullptr &&
strcmp(remainingStatement, "") != 0 && !isFailed);
if (isFailed) {
throw std::runtime_error(
"[op-sqlite] SQLite error code: " + std::to_string(step) +
", description: " + std::string(errorMessage));
}
int changedRowCount = sqlite3_changes(db);
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
return {.affectedRows = changedRowCount,
.insertId = static_cast<double>(latestInsertRowId)};
}
std::string operation_to_string(int operation_type) {
switch (operation_type) {
case SQLITE_INSERT:
return "INSERT";
case SQLITE_DELETE:
return "DELETE";
case SQLITE_UPDATE:
return "UPDATE";
default:
throw std::runtime_error("Unknown SQLite operation on hook");
}
}
void update_callback(void *db_host_object_ptr, int operation_type,
[[maybe_unused]] char const *database, char const *table,
sqlite3_int64 row_id) {
auto db_host_object = reinterpret_cast<DBHostObject *>(db_host_object_ptr);
db_host_object->on_update(std::string(table),
operation_to_string(operation_type), row_id);
}
void opsqlite_register_update_hook(sqlite3 *db, void *db_host_object) {
sqlite3_update_hook(db, &update_callback, (void *)db_host_object);
}
void opsqlite_deregister_update_hook(sqlite3 *db) {
sqlite3_update_hook(db, nullptr, nullptr);
}
int commit_callback(void *db_host_object_ptr) {
auto db_host_object = reinterpret_cast<DBHostObject *>(db_host_object_ptr);
db_host_object->on_commit();
return 0;
}
void opsqlite_register_commit_hook(sqlite3 *db, void *db_host_object_ptr) {
sqlite3_commit_hook(db, &commit_callback, db_host_object_ptr);
}
void opsqlite_deregister_commit_hook(sqlite3 *db) {
sqlite3_commit_hook(db, nullptr, nullptr);
}
void rollback_callback(void *db_host_object_ptr) {
auto db_host_object = reinterpret_cast<DBHostObject *>(db_host_object_ptr);
db_host_object->on_rollback();
}
void opsqlite_register_rollback_hook(sqlite3 *db, void *db_host_object_ptr) {
sqlite3_rollback_hook(db, &rollback_callback, db_host_object_ptr);
}
void opsqlite_deregister_rollback_hook(sqlite3 *db) {
sqlite3_rollback_hook(db, nullptr, nullptr);
}
void opsqlite_load_extension(sqlite3 *db, std::string &path,
std::string &entry_point) {
#ifdef OP_SQLITE_USE_PHONE_VERSION
throw std::runtime_error("[op-sqlite] Embedded version of SQLite does not "
"support loading extensions");
#else
int status = 0;
status = sqlite3_enable_load_extension(db, 1);
if (status != SQLITE_OK) {
throw std::runtime_error("Could not enable extension loading");
}
const char *entry_point_cstr = nullptr;
if (!entry_point.empty()) {
entry_point_cstr = entry_point.c_str();
}
char *error_message;
status = sqlite3_load_extension(db, path.c_str(), entry_point_cstr,
&error_message);
if (status != SQLITE_OK) {
throw std::runtime_error(error_message);
}
#endif
}
BatchResult
opsqlite_execute_batch(sqlite3 *db,
const std::vector<BatchArguments> *commands) {
size_t commandCount = commands->size();
if (commandCount <= 0) {
throw std::runtime_error("No SQL commands provided");
}
int affectedRows = 0;
// opsqlite_execute(db, "BEGIN EXCLUSIVE TRANSACTION", nullptr);
for (int i = 0; i < commandCount; i++) {
const auto &command = commands->at(i);
// We do not provide a datastructure to receive query data because we
// don't need/want to handle this results in a batch execution
// There is also no need to commit/catch this transaction, this is done
// in the JS code
auto result = opsqlite_execute(db, command.sql, &command.params);
affectedRows += result.affectedRows;
}
return BatchResult{
.affectedRows = affectedRows,
.commands = static_cast<int>(commandCount),
};
}
} // namespace opsqlite