@op-engineering/op-sqlite 7.4.0 → 8.0.0-beta0

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.
Files changed (38) hide show
  1. package/README.md +11 -3
  2. package/android/build.gradle +8 -0
  3. package/cpp/DBHostObject.cpp +101 -106
  4. package/cpp/PreparedStatementHostObject.cpp +39 -14
  5. package/cpp/PreparedStatementHostObject.h +17 -4
  6. package/cpp/bindings.cpp +5 -5
  7. package/cpp/bridge.cpp +206 -85
  8. package/cpp/bridge.h +9 -7
  9. package/cpp/libsql/bridge.cpp +212 -92
  10. package/cpp/libsql/bridge.h +7 -3
  11. package/cpp/macros.h +2 -2
  12. package/cpp/types.h +11 -8
  13. package/cpp/utils.cpp +43 -7
  14. package/cpp/utils.h +2 -0
  15. package/lib/commonjs/index.js +53 -30
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/package.json +1 -0
  18. package/lib/module/NativeOPSQLite.js +2 -0
  19. package/lib/module/NativeOPSQLite.js.map +1 -1
  20. package/lib/module/index.js +55 -29
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/package.json +1 -0
  23. package/lib/typescript/commonjs/package.json +1 -0
  24. package/lib/typescript/commonjs/src/NativeOPSQLite.d.ts.map +1 -0
  25. package/lib/typescript/{src → commonjs/src}/index.d.ts +11 -11
  26. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  27. package/lib/typescript/module/package.json +1 -0
  28. package/lib/typescript/module/src/NativeOPSQLite.d.ts +15 -0
  29. package/lib/typescript/module/src/NativeOPSQLite.d.ts.map +1 -0
  30. package/lib/typescript/module/src/index.d.ts +163 -0
  31. package/lib/typescript/module/src/index.d.ts.map +1 -0
  32. package/package.json +44 -14
  33. package/src/index.ts +80 -46
  34. package/android/.project +0 -17
  35. package/android/.settings/org.eclipse.buildship.core.prefs +0 -13
  36. package/lib/typescript/src/NativeOPSQLite.d.ts.map +0 -1
  37. package/lib/typescript/src/index.d.ts.map +0 -1
  38. /package/lib/typescript/{src → commonjs/src}/NativeOPSQLite.d.ts +0 -0
package/README.md CHANGED
@@ -1,13 +1,21 @@
1
1
  ![benchmark](benchmark.png)
2
2
 
3
- **Current SQLite version: 3.45.1**
4
-
5
3
  Created by [@ospfranco](https://twitter.com/ospfranco). **Please consider sponsoring!**.
6
4
 
7
- # Docs
5
+ OP-SQLite has grown large to cover a lot of plugins, sqlite versions and APIs. Please read the full documentation before opening an issue.
8
6
 
9
7
  [See the docs](https://ospfranco.notion.site/OP-SQLite-Documentation-a279a52102464d0cb13c3fa230d2f2dc?pvs=4)
10
8
 
9
+ Some of the big and external (back-ends, out-of-tree features, plugins) supported features:
10
+
11
+ - Vanilla sqlite ofc
12
+ - Libsql is supported as a sqlite backend
13
+ - SQLCipher is supported as a sqlite backend
14
+ - FTS5 plugin
15
+ - cr-sqlite plugin
16
+ - sqlite-vec plugin
17
+ - Reactive queries (currently with some issues, please donate)
18
+
11
19
  # License
12
20
 
13
21
  MIT License.
@@ -225,4 +225,12 @@ tasks.whenTaskAdded { task ->
225
225
  task.dependsOn(it)
226
226
  }
227
227
  }
228
+ }
229
+
230
+ if (isNewArchitectureEnabled()) {
231
+ react {
232
+ jsRootDir = file("../src/")
233
+ libraryName = "opsqlite"
234
+ codegenJavaPackageName = "com.op.sqlite.example"
235
+ }
228
236
  }
@@ -39,7 +39,7 @@ void DBHostObject::auto_register_update_hook() {
39
39
  if (operation != "DELETE") {
40
40
  std::string query = "SELECT * FROM " + table_name +
41
41
  " where rowid = " + std::to_string(rowId) + ";";
42
- opsqlite_execute(name, query, &params, &results, metadata);
42
+ opsqlite_execute_host_objects(name, query, &params, &results, metadata);
43
43
  }
44
44
 
45
45
  jsCallInvoker->invokeAsync(
@@ -57,7 +57,7 @@ void DBHostObject::auto_register_update_hook() {
57
57
  res.setProperty(
58
58
  rt, "row",
59
59
  jsi::Object::createFromHostObject(
60
- rt, std::make_shared<DumbHostObject>(results->at(0))));
60
+ rt, std::make_shared<HostObject>(results->at(0))));
61
61
  }
62
62
 
63
63
  callback->asObject(rt).asFunction(rt).call(rt, res);
@@ -177,8 +177,8 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
177
177
  thread_pool(thread_pool), db_name(db_name), rt(rt) {
178
178
 
179
179
  #ifdef OP_SQLITE_USE_SQLCIPHER
180
- BridgeResult result =
181
- opsqlite_open(db_name, path, crsqlite_path, sqlite_vec_path, encryption_key);
180
+ BridgeResult result = opsqlite_open(db_name, path, crsqlite_path,
181
+ sqlite_vec_path, encryption_key);
182
182
  #elif OP_SQLITE_USE_LIBSQL
183
183
  BridgeResult result = opsqlite_libsql_open(db_name, path, crsqlite_path);
184
184
  #else
@@ -194,7 +194,7 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
194
194
  };
195
195
 
196
196
  void DBHostObject::create_jsi_functions() {
197
- auto attach = HOSTFN("attach", 4) {
197
+ auto attach = HOSTFN("attach") {
198
198
  if (count < 3) {
199
199
  throw jsi::JSError(rt,
200
200
  "[op-sqlite][attach] Incorrect number of arguments");
@@ -232,7 +232,7 @@ void DBHostObject::create_jsi_functions() {
232
232
  return {};
233
233
  });
234
234
 
235
- auto detach = HOSTFN("detach", 2) {
235
+ auto detach = HOSTFN("detach") {
236
236
  if (count < 2) {
237
237
  throw std::runtime_error(
238
238
  "[op-sqlite][detach] Incorrect number of arguments");
@@ -258,7 +258,7 @@ void DBHostObject::create_jsi_functions() {
258
258
  return {};
259
259
  });
260
260
 
261
- auto close = HOSTFN("close", 0) {
261
+ auto close = HOSTFN("close") {
262
262
  #ifdef OP_SQLITE_USE_LIBSQL
263
263
  BridgeResult result = opsqlite_libsql_close(db_name);
264
264
  #else
@@ -272,8 +272,7 @@ void DBHostObject::create_jsi_functions() {
272
272
  return {};
273
273
  });
274
274
 
275
- auto remove = HOSTFN("delete", 1) {
276
-
275
+ auto remove = HOSTFN("delete") {
277
276
  std::string path = std::string(base_path);
278
277
 
279
278
  if (count == 1 && !args[0].isUndefined() && !args[0].isNull()) {
@@ -308,7 +307,7 @@ void DBHostObject::create_jsi_functions() {
308
307
  return {};
309
308
  });
310
309
 
311
- auto execute = HOSTFN("execute", 2) {
310
+ auto execute_raw = HOSTFN("executeRaw") {
312
311
  const std::string query = args[0].asString(rt).utf8(rt);
313
312
  std::vector<JSVariant> params;
314
313
 
@@ -317,59 +316,86 @@ void DBHostObject::create_jsi_functions() {
317
316
  params = to_variant_vec(rt, originalParams);
318
317
  }
319
318
 
320
- std::vector<DumbHostObject> results;
321
- std::shared_ptr<std::vector<SmartHostObject>> metadata =
322
- std::make_shared<std::vector<SmartHostObject>>();
319
+ auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
320
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor") {
321
+ auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
322
+ auto reject = std::make_shared<jsi::Value>(rt, args[1]);
323
+
324
+ auto task = [&rt, this, query, params = std::move(params), resolve,
325
+ reject, invoker = this->jsCallInvoker]() {
326
+ try {
327
+ std::vector<std::vector<JSVariant>> results;
323
328
 
324
329
  #ifdef OP_SQLITE_USE_LIBSQL
325
- auto status =
326
- opsqlite_libsql_execute(db_name, query, &params, &results, metadata);
330
+ auto status =
331
+ opsqlite_libsql_execute_raw(db_name, query, &params, &results);
327
332
  #else
328
- auto status = opsqlite_execute(db_name, query, &params, &results, metadata);
333
+ auto status = opsqlite_execute_raw(db_name, query, &params, &results);
329
334
  #endif
330
335
 
331
- if (status.type == SQLiteError) {
332
- throw std::runtime_error(status.message);
333
- }
336
+ invoker->invokeAsync([&rt, results = std::move(results),
337
+ status = std::move(status), resolve, reject] {
338
+ if (status.type == SQLiteOk) {
339
+ auto jsiResult = create_raw_result(rt, status, &results);
340
+ resolve->asObject(rt).asFunction(rt).call(rt,
341
+ std::move(jsiResult));
342
+ } else {
343
+ auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
344
+ auto error = errorCtr.callAsConstructor(
345
+ rt, jsi::String::createFromUtf8(rt, status.message));
346
+ reject->asObject(rt).asFunction(rt).call(rt, error);
347
+ }
348
+ });
349
+
350
+ } catch (std::exception &exc) {
351
+ invoker->invokeAsync([&rt, exc = std::move(exc), reject] {
352
+ auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
353
+ auto error = errorCtr.callAsConstructor(
354
+ rt, jsi::String::createFromAscii(rt, exc.what()));
355
+ reject->asObject(rt).asFunction(rt).call(rt, error);
356
+ });
357
+ }
358
+ };
359
+
360
+ thread_pool->queueWork(task);
334
361
 
335
- auto jsiResult = createResult(rt, status, &results, metadata);
336
- return jsiResult;
362
+ return {};
363
+ }));
364
+
365
+ return promise;
337
366
  });
338
367
 
339
- auto execute_raw_async = HOSTFN("executeRawAsync", 2) {
368
+ auto execute = HOSTFN("execute") {
340
369
  const std::string query = args[0].asString(rt).utf8(rt);
341
370
  std::vector<JSVariant> params;
342
371
 
343
372
  if (count == 2) {
344
- const jsi::Value &originalParams = args[1];
345
- params = to_variant_vec(rt, originalParams);
373
+ params = to_variant_vec(rt, args[1]);
346
374
  }
347
375
 
348
376
  auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
349
- auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
377
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor") {
350
378
  auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
351
379
  auto reject = std::make_shared<jsi::Value>(rt, args[1]);
352
380
 
353
381
  auto task = [&rt, this, query, params = std::move(params), resolve,
354
382
  reject, invoker = this->jsCallInvoker]() {
355
383
  try {
356
- std::vector<std::vector<JSVariant>> results;
357
384
 
358
385
  #ifdef OP_SQLITE_USE_LIBSQL
359
- auto status =
360
- opsqlite_libsql_execute_raw(db_name, query, &params, &results);
386
+ auto status = opsqlite_libsql_execute(db_name, query, &params);
361
387
  #else
362
- auto status = opsqlite_execute_raw(db_name, query, &params, &results);
388
+ auto status = opsqlite_execute(db_name, query, &params);
363
389
  #endif
364
- //
390
+
365
391
  // if (invalidated) {
366
392
  // return;
367
393
  // }
368
394
 
369
- invoker->invokeAsync([&rt, results = std::move(results),
370
- status = std::move(status), resolve, reject] {
395
+ invoker->invokeAsync([&rt, status = std::move(status), resolve,
396
+ reject] {
371
397
  if (status.type == SQLiteOk) {
372
- auto jsiResult = create_raw_result(rt, status, &results);
398
+ auto jsiResult = create_js_rows(rt, status);
373
399
  resolve->asObject(rt).asFunction(rt).call(rt,
374
400
  std::move(jsiResult));
375
401
  } else {
@@ -381,8 +407,11 @@ void DBHostObject::create_jsi_functions() {
381
407
  });
382
408
 
383
409
  } catch (std::exception &exc) {
410
+ std::cout << "Exception executing function" << exc.what()
411
+ << std::endl;
384
412
  invoker->invokeAsync([&rt, exc = std::move(exc), reject] {
385
413
  auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
414
+ auto what = exc.what();
386
415
  auto error = errorCtr.callAsConstructor(
387
416
  rt, jsi::String::createFromAscii(rt, exc.what()));
388
417
  reject->asObject(rt).asFunction(rt).call(rt, error);
@@ -393,12 +422,12 @@ void DBHostObject::create_jsi_functions() {
393
422
  thread_pool->queueWork(task);
394
423
 
395
424
  return {};
396
- }));
425
+ }));
397
426
 
398
- return promise;
427
+ return promise;
399
428
  });
400
429
 
401
- auto execute_async = HOSTFN("executeAsync", 2) {
430
+ auto execute_with_host_objects = HOSTFN("executeWithHostObjects") {
402
431
  const std::string query = args[0].asString(rt).utf8(rt);
403
432
  std::vector<JSVariant> params;
404
433
 
@@ -408,7 +437,7 @@ void DBHostObject::create_jsi_functions() {
408
437
  }
409
438
 
410
439
  auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
411
- auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
440
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor") {
412
441
  auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
413
442
  auto reject = std::make_shared<jsi::Value>(rt, args[1]);
414
443
 
@@ -419,11 +448,11 @@ void DBHostObject::create_jsi_functions() {
419
448
  std::shared_ptr<std::vector<SmartHostObject>> metadata =
420
449
  std::make_shared<std::vector<SmartHostObject>>();
421
450
  #ifdef OP_SQLITE_USE_LIBSQL
422
- auto status = opsqlite_libsql_execute(db_name, query, &params,
423
- &results, metadata);
451
+ auto status = opsqlite_libsql_execute_with_host_objects(
452
+ db_name, query, &params, &results, metadata);
424
453
  #else
425
- auto status =
426
- opsqlite_execute(db_name, query, &params, &results, metadata);
454
+ auto status = opsqlite_execute_host_objects(db_name, query, &params,
455
+ &results, metadata);
427
456
  #endif
428
457
 
429
458
  // if (invalidated) {
@@ -466,36 +495,7 @@ void DBHostObject::create_jsi_functions() {
466
495
  return promise;
467
496
  });
468
497
 
469
- auto execute_batch = HOSTFN("executeBatch", 1) {
470
- if (sizeof(args) < 1) {
471
- throw std::runtime_error(
472
- "[op-sqlite][executeBatch] - Incorrect parameter count");
473
- }
474
-
475
- const jsi::Value &params = args[0];
476
- if (params.isNull() || params.isUndefined()) {
477
- throw std::runtime_error("[op-sqlite][executeBatch] - An array of SQL "
478
- "commands or parameters is needed");
479
- }
480
- const jsi::Array &batchParams = params.asObject(rt).asArray(rt);
481
- std::vector<BatchArguments> commands;
482
- to_batch_arguments(rt, batchParams, &commands);
483
-
484
- #ifdef OP_SQLITE_USE_LIBSQL
485
- auto batchResult = opsqlite_libsql_execute_batch(db_name, &commands);
486
- #else
487
- auto batchResult = opsqlite_execute_batch(db_name, &commands);
488
- #endif
489
- if (batchResult.type == SQLiteOk) {
490
- auto res = jsi::Object(rt);
491
- res.setProperty(rt, "rowsAffected", jsi::Value(batchResult.affectedRows));
492
- return std::move(res);
493
- } else {
494
- throw std::runtime_error(batchResult.message);
495
- }
496
- });
497
-
498
- auto execute_batch_async = HOSTFN("executeBatchAsync", 1) {
498
+ auto execute_batch = HOSTFN("executeBatch") {
499
499
  if (sizeof(args) < 1) {
500
500
  throw std::runtime_error(
501
501
  "[op-sqlite][executeAsyncBatch] Incorrect parameter count");
@@ -517,7 +517,7 @@ void DBHostObject::create_jsi_functions() {
517
517
  to_batch_arguments(rt, batchParams, &commands);
518
518
 
519
519
  auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
520
- auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
520
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor") {
521
521
  auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
522
522
  auto reject = std::make_shared<jsi::Value>(rt, args[1]);
523
523
 
@@ -560,17 +560,15 @@ void DBHostObject::create_jsi_functions() {
560
560
  });
561
561
 
562
562
  #ifdef OP_SQLITE_USE_LIBSQL
563
- auto sync = HOSTFN("sync", 0) {
563
+ auto sync = HOSTFN("sync") {
564
564
  BridgeResult result = opsqlite_libsql_sync(db_name);
565
565
  if (result.type == SQLiteError) {
566
566
  throw std::runtime_error(result.message);
567
567
  }
568
568
  return {};
569
569
  });
570
- #endif
571
-
572
- #ifndef OP_SQLITE_USE_LIBSQL
573
- auto load_file = HOSTFN("loadFile", 1) {
570
+ #else
571
+ auto load_file = HOSTFN("loadFile") {
574
572
  if (sizeof(args) < 1) {
575
573
  throw std::runtime_error(
576
574
  "[op-sqlite][loadFile] Incorrect parameter count");
@@ -580,17 +578,17 @@ void DBHostObject::create_jsi_functions() {
580
578
  const std::string sqlFileName = args[0].asString(rt).utf8(rt);
581
579
 
582
580
  auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
583
- auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2)
581
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor")
584
582
  {
585
583
  auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
586
584
  auto reject = std::make_shared<jsi::Value>(rt, args[1]);
587
585
 
588
586
  auto task = [&rt, this, sqlFileName, resolve, reject]() {
589
587
  try {
590
- const auto importResult = importSQLFile(db_name, sqlFileName);
588
+ const auto result = importSQLFile(db_name, sqlFileName);
591
589
 
592
- jsCallInvoker->invokeAsync([&rt, result = std::move(importResult),
593
- resolve, reject] {
590
+ jsCallInvoker->invokeAsync([&rt, result = std::move(result), resolve,
591
+ reject] {
594
592
  if (result.type == SQLiteOk) {
595
593
  auto res = jsi::Object(rt);
596
594
  res.setProperty(rt, "rowsAffected",
@@ -616,7 +614,7 @@ void DBHostObject::create_jsi_functions() {
616
614
  return promise;
617
615
  });
618
616
 
619
- auto update_hook = HOSTFN("updateHook", 1) {
617
+ auto update_hook = HOSTFN("updateHook") {
620
618
  auto callback = std::make_shared<jsi::Value>(rt, args[0]);
621
619
 
622
620
  if (callback->isUndefined() || callback->isNull()) {
@@ -628,7 +626,7 @@ void DBHostObject::create_jsi_functions() {
628
626
  return {};
629
627
  });
630
628
 
631
- auto commit_hook = HOSTFN("commitHook", 1) {
629
+ auto commit_hook = HOSTFN("commitHook") {
632
630
  if (sizeof(args) < 1) {
633
631
  throw std::runtime_error("[op-sqlite][commitHook] callback needed");
634
632
  return {};
@@ -651,7 +649,7 @@ void DBHostObject::create_jsi_functions() {
651
649
  return {};
652
650
  });
653
651
 
654
- auto rollback_hook = HOSTFN("rollbackHook", 1) {
652
+ auto rollback_hook = HOSTFN("rollbackHook") {
655
653
  if (sizeof(args) < 1) {
656
654
  throw std::runtime_error("[op-sqlite][rollbackHook] callback needed");
657
655
  return {};
@@ -674,7 +672,7 @@ void DBHostObject::create_jsi_functions() {
674
672
  return {};
675
673
  });
676
674
 
677
- auto load_extension = HOSTFN("loadExtension", 1) {
675
+ auto load_extension = HOSTFN("loadExtension") {
678
676
  auto path = args[0].asString(rt).utf8(rt);
679
677
  std::string entry_point = "";
680
678
  if (count > 1 && args[1].isString()) {
@@ -688,7 +686,7 @@ void DBHostObject::create_jsi_functions() {
688
686
  return {};
689
687
  });
690
688
 
691
- auto reactive_execute = HOSTFN("reactiveExecute", 0) {
689
+ auto reactive_execute = HOSTFN("reactiveExecute") {
692
690
  auto query = args[0].asObject(rt);
693
691
  // if (!query.hasProperty(rt, "query") || !query.hasProperty(rt, "args")
694
692
  // ||
@@ -748,7 +746,7 @@ void DBHostObject::create_jsi_functions() {
748
746
 
749
747
  auto_register_update_hook();
750
748
 
751
- auto unsubscribe = HOSTFN("unsubscribe", 0) {
749
+ auto unsubscribe = HOSTFN("unsubscribe") {
752
750
  auto it = std::find(reactive_queries.begin(), reactive_queries.end(),
753
751
  reactiveQuery);
754
752
  if (it != reactive_queries.end()) {
@@ -763,7 +761,7 @@ void DBHostObject::create_jsi_functions() {
763
761
 
764
762
  #endif
765
763
 
766
- auto prepare_statement = HOSTFN("prepareStatement", 1) {
764
+ auto prepare_statement = HOSTFN("prepareStatement") {
767
765
  auto query = args[0].asString(rt).utf8(rt);
768
766
  #ifdef OP_SQLITE_USE_LIBSQL
769
767
  libsql_stmt_t statement = opsqlite_libsql_prepare_statement(db_name, query);
@@ -771,12 +769,13 @@ void DBHostObject::create_jsi_functions() {
771
769
  sqlite3_stmt *statement = opsqlite_prepare_statement(db_name, query);
772
770
  #endif
773
771
  auto preparedStatementHostObject =
774
- std::make_shared<PreparedStatementHostObject>(db_name, statement);
772
+ std::make_shared<PreparedStatementHostObject>(
773
+ db_name, statement, jsCallInvoker, thread_pool);
775
774
 
776
775
  return jsi::Object::createFromHostObject(rt, preparedStatementHostObject);
777
776
  });
778
777
 
779
- auto get_db_path = HOSTFN("getDbPath", 1) {
778
+ auto get_db_path = HOSTFN("getDbPath") {
780
779
  std::string path = std::string(base_path);
781
780
  if (count == 1 && !args[0].isUndefined() && !args[0].isNull()) {
782
781
  if (!args[0].isString()) {
@@ -802,12 +801,11 @@ void DBHostObject::create_jsi_functions() {
802
801
  function_map["attach"] = std::move(attach);
803
802
  function_map["detach"] = std::move(detach);
804
803
  function_map["close"] = std::move(close);
805
- function_map["executeRawAsync"] = std::move(execute_raw_async);
806
804
  function_map["execute"] = std::move(execute);
807
- function_map["executeAsync"] = std::move(execute_async);
805
+ function_map["executeRaw"] = std::move(execute_raw);
806
+ function_map["executeWithHostObjects"] = std::move(execute_with_host_objects);
808
807
  function_map["delete"] = std::move(remove);
809
808
  function_map["executeBatch"] = std::move(execute_batch);
810
- function_map["executeBatchAsync"] = std::move(execute_batch_async);
811
809
  function_map["prepareStatement"] = std::move(prepare_statement);
812
810
  function_map["getDbPath"] = std::move(get_db_path);
813
811
  #ifdef OP_SQLITE_USE_LIBSQL
@@ -841,14 +839,14 @@ jsi::Value DBHostObject::get(jsi::Runtime &rt,
841
839
  if (name == "close") {
842
840
  return jsi::Value(rt, function_map["close"]);
843
841
  }
844
- if (name == "executeRawAsync") {
845
- return jsi::Value(rt, function_map["executeRawAsync"]);
842
+ if (name == "executeRaw") {
843
+ return jsi::Value(rt, function_map["executeRaw"]);
846
844
  }
847
845
  if (name == "execute") {
848
846
  return jsi::Value(rt, function_map["execute"]);
849
847
  }
850
- if (name == "executeAsync") {
851
- return jsi::Value(rt, function_map["executeAsync"]);
848
+ if (name == "executeWithHostObjects") {
849
+ return jsi::Value(rt, function_map["executeWithHostObjects"]);
852
850
  }
853
851
  if (name == "delete") {
854
852
  return jsi::Value(rt, function_map["delete"]);
@@ -856,9 +854,6 @@ jsi::Value DBHostObject::get(jsi::Runtime &rt,
856
854
  if (name == "executeBatch") {
857
855
  return jsi::Value(rt, function_map["executeBatch"]);
858
856
  }
859
- if (name == "executeBatchAsync") {
860
- return jsi::Value(rt, function_map["executeBatchAsync"]);
861
- }
862
857
  if (name == "prepareStatement") {
863
858
  return jsi::Value(rt, function_map["prepareStatement"]);
864
859
  }
@@ -870,32 +865,32 @@ jsi::Value DBHostObject::get(jsi::Runtime &rt,
870
865
  }
871
866
  #ifdef OP_SQLITE_USE_LIBSQL
872
867
  if (name == "loadFile") {
873
- return HOSTFN("loadFile", 0) {
868
+ return HOSTFN("loadFile") {
874
869
  throw std::runtime_error("[op-sqlite] Load file not implemented");
875
870
  });
876
871
  }
877
872
  if (name == "updateHook") {
878
- return HOSTFN("updateHook", 0) {
873
+ return HOSTFN("updateHook") {
879
874
  throw std::runtime_error("[op-sqlite] Hooks not supported in libsql");
880
875
  });
881
876
  }
882
877
  if (name == "commitHook") {
883
- return HOSTFN("commitHook", 0) {
878
+ return HOSTFN("commitHook") {
884
879
  throw std::runtime_error("[op-sqlite] Hooks not supported in libsql");
885
880
  });
886
881
  }
887
882
  if (name == "rollbackHook") {
888
- return HOSTFN("rollbackHook", 0) {
883
+ return HOSTFN("rollbackHook") {
889
884
  throw std::runtime_error("[op-sqlite] Hooks not supported in libsql");
890
885
  });
891
886
  }
892
887
  if (name == "loadExtension") {
893
- return HOSTFN("loadExtension", 0) {
888
+ return HOSTFN("loadExtension") {
894
889
  throw std::runtime_error("[op-sqlite] Hooks not supported in libsql");
895
890
  });
896
891
  }
897
892
  if (name == "reactiveExecute") {
898
- return HOSTFN("reactiveExecute", 0) {
893
+ return HOSTFN("reactiveExecute") {
899
894
  throw std::runtime_error("[op-sqlite] Hooks not supported in libsql");
900
895
  });
901
896
  }
@@ -23,7 +23,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
23
23
  auto name = propNameID.utf8(rt);
24
24
 
25
25
  if (name == "bind") {
26
- return HOSTFN("bind", 1) {
26
+ return HOSTFN("bind") {
27
27
  if (_stmt == nullptr) {
28
28
  throw std::runtime_error("statement has been freed");
29
29
  }
@@ -41,28 +41,53 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
41
41
  }
42
42
 
43
43
  if (name == "execute") {
44
- return HOSTFN("execute", 1) {
44
+ return HOSTFN("execute") {
45
45
  if (_stmt == nullptr) {
46
46
  throw std::runtime_error("statement has been freed");
47
47
  }
48
48
 
49
- std::vector<DumbHostObject> results;
50
- std::shared_ptr<std::vector<SmartHostObject>> metadata =
51
- std::make_shared<std::vector<SmartHostObject>>();
49
+ auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
50
+ auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor") {
51
+ auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
52
+ auto reject = std::make_shared<jsi::Value>(rt, args[1]);
53
+
54
+ auto task = [&rt, this, resolve, reject,
55
+ invoker = this->_js_call_invoker]() {
56
+ std::vector<DumbHostObject> results;
57
+ std::shared_ptr<std::vector<SmartHostObject>> metadata =
58
+ std::make_shared<std::vector<SmartHostObject>>();
52
59
  #ifdef OP_SQLITE_USE_LIBSQL
53
- auto status = opsqlite_libsql_execute_prepared_statement(
54
- _name, _stmt, &results, metadata);
60
+ auto status = opsqlite_libsql_execute_prepared_statement(
61
+ _name, _stmt, &results, metadata);
55
62
  #else
56
- auto status =
57
- opsqlite_execute_prepared_statement(_name, _stmt, &results, metadata);
63
+ auto status = opsqlite_execute_prepared_statement(_name, _stmt,
64
+ &results, metadata);
58
65
  #endif
66
+ invoker->invokeAsync(
67
+ [&rt, status = std::move(status),
68
+ results = std::make_shared<std::vector<DumbHostObject>>(results),
69
+ metadata, resolve, reject] {
70
+ if (status.type == SQLiteOk) {
71
+ auto jsiResult =
72
+ createResult(rt, status, results.get(), metadata);
73
+ resolve->asObject(rt).asFunction(rt).call(
74
+ rt, std::move(jsiResult));
75
+ } else {
76
+ auto errorCtr =
77
+ rt.global().getPropertyAsFunction(rt, "Error");
78
+ auto error = errorCtr.callAsConstructor(
79
+ rt, jsi::String::createFromUtf8(rt, status.message));
80
+ reject->asObject(rt).asFunction(rt).call(rt, error);
81
+ }
82
+ });
83
+ };
59
84
 
60
- if (status.type == SQLiteError) {
61
- throw std::runtime_error(status.message);
62
- }
85
+ _thread_pool->queueWork(task);
86
+
87
+ return {};
88
+ }));
63
89
 
64
- auto jsiResult = createResult(rt, status, &results, metadata);
65
- return jsiResult;
90
+ return promise;
66
91
  });
67
92
  }
68
93
 
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include <ReactCommon/CallInvoker.h>
3
4
  #include <jsi/jsi.h>
4
5
  #include <memory>
5
6
  #ifdef OP_SQLITE_USE_LIBSQL
@@ -7,19 +8,29 @@
7
8
  #else
8
9
  #include <sqlite3.h>
9
10
  #endif
11
+ #include "ThreadPool.h"
10
12
  #include <string>
11
13
 
12
14
  namespace opsqlite {
13
15
  namespace jsi = facebook::jsi;
16
+ namespace react = facebook::react;
14
17
 
15
18
  class PreparedStatementHostObject : public jsi::HostObject {
16
19
  public:
17
20
  #ifdef OP_SQLITE_USE_LIBSQL
18
- PreparedStatementHostObject(std::string name, libsql_stmt_t stmt)
19
- : _name(name), _stmt(stmt){};
21
+ PreparedStatementHostObject(
22
+ std::string name, libsql_stmt_t stmt,
23
+ std::shared_ptr<react::CallInvoker> js_call_invoker,
24
+ std::shared_ptr<ThreadPool> thread_pool)
25
+ : _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
26
+ _thread_pool(thread_pool){};
20
27
  #else
21
- PreparedStatementHostObject(std::string name, sqlite3_stmt *stmt)
22
- : _name(name), _stmt(stmt){};
28
+ PreparedStatementHostObject(
29
+ std::string name, sqlite3_stmt *stmt,
30
+ std::shared_ptr<react::CallInvoker> js_call_invoker,
31
+ std::shared_ptr<ThreadPool> thread_pool)
32
+ : _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
33
+ _thread_pool(thread_pool){};
23
34
  #endif
24
35
  virtual ~PreparedStatementHostObject();
25
36
 
@@ -28,6 +39,8 @@ public:
28
39
  jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
29
40
 
30
41
  private:
42
+ std::shared_ptr<react::CallInvoker> _js_call_invoker;
43
+ std::shared_ptr<ThreadPool> _thread_pool;
31
44
  std::string _name;
32
45
  #ifdef OP_SQLITE_USE_LIBSQL
33
46
  libsql_stmt_t _stmt;