@op-engineering/op-sqlite 6.0.2-beta1 → 6.0.2-beta3

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/android/CMakeLists.txt +26 -15
  2. package/android/build.gradle +10 -1
  3. package/android/cpp-adapter.cpp +4 -0
  4. package/android/jniLibs/arm64-v8a/libsql_experimental.a +0 -0
  5. package/android/jniLibs/armeabi-v7a/libsql_experimental.a +0 -0
  6. package/android/jniLibs/x86/libsql_experimental.a +0 -0
  7. package/android/jniLibs/x86_64/libsql_experimental.a +0 -0
  8. package/cpp/DBHostObject.cpp +208 -81
  9. package/cpp/DBHostObject.h +10 -5
  10. package/cpp/PreparedStatementHostObject.cpp +28 -14
  11. package/cpp/PreparedStatementHostObject.h +18 -14
  12. package/cpp/bindings.cpp +14 -12
  13. package/cpp/bindings.h +2 -0
  14. package/cpp/bridge.cpp +45 -0
  15. package/cpp/bridge.h +3 -0
  16. package/cpp/libsql/bridge.cpp +629 -0
  17. package/cpp/libsql/bridge.h +88 -0
  18. package/cpp/libsql/libsql.h +133 -0
  19. package/cpp/sqlite3.h +0 -1
  20. package/cpp/types.h +5 -0
  21. package/cpp/utils.cpp +45 -3
  22. package/cpp/utils.h +2 -3
  23. package/ios/libsql.xcframework/Info.plist +48 -0
  24. package/ios/libsql.xcframework/ios-arm64/Headers/libsql.h +133 -0
  25. package/ios/libsql.xcframework/ios-arm64/libsql_experimental.a +0 -0
  26. package/ios/libsql.xcframework/ios-arm64_x86_64-simulator/Headers/libsql.h +133 -0
  27. package/ios/libsql.xcframework/ios-arm64_x86_64-simulator/libsql_experimental.a +0 -0
  28. package/lib/commonjs/index.js +5 -2
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/module/index.js +3 -1
  31. package/lib/module/index.js.map +1 -1
  32. package/lib/typescript/src/index.d.ts +6 -2
  33. package/lib/typescript/src/index.d.ts.map +1 -1
  34. package/op-sqlite.podspec +20 -3
  35. package/package.json +1 -1
  36. package/src/index.ts +10 -3
  37. package/cpp/sqlbatchexecutor.cpp +0 -93
  38. package/cpp/sqlbatchexecutor.h +0 -30
@@ -2,6 +2,7 @@
2
2
  #define DBHostObject_h
3
3
 
4
4
  #include "ThreadPool.h"
5
+ #include "sqlite3.h"
5
6
  #include "types.h"
6
7
  #include <ReactCommon/CallInvoker.h>
7
8
  #include <any>
@@ -14,11 +15,14 @@ namespace opsqlite {
14
15
  namespace jsi = facebook::jsi;
15
16
  namespace react = facebook::react;
16
17
 
18
+ struct TableRowDiscriminator {
19
+ std::string table;
20
+ std::vector<int> ids;
21
+ };
22
+
17
23
  struct ReactiveQuery {
18
- std::string query;
19
- std::vector<JSVariant> args;
20
- std::vector<std::string> tables;
21
- std::vector<int> rowIds;
24
+ sqlite3_stmt *stmt;
25
+ std::vector<TableRowDiscriminator> discriminators;
22
26
  std::shared_ptr<jsi::Value> callback;
23
27
  };
24
28
 
@@ -31,8 +35,9 @@ public:
31
35
  std::string &encryption_key);
32
36
 
33
37
  std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
34
-
35
38
  jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
39
+ void set(jsi::Runtime &rt, const jsi::PropNameID &name,
40
+ const jsi::Value &value);
36
41
 
37
42
  void auto_register_update_hook();
38
43
 
@@ -1,5 +1,9 @@
1
1
  #include "PreparedStatementHostObject.h"
2
+ #if OP_SQLITE_USE_LIBSQL
3
+ #include "libsql/bridge.h"
4
+ #else
2
5
  #include "bridge.h"
6
+ #endif
3
7
  #include "macros.h"
4
8
  #include "utils.h"
5
9
 
@@ -7,10 +11,6 @@ namespace opsqlite {
7
11
 
8
12
  namespace jsi = facebook::jsi;
9
13
 
10
- PreparedStatementHostObject::PreparedStatementHostObject(
11
- std::string dbName, sqlite3_stmt *statementPtr)
12
- : _dbName(dbName), _statement(statementPtr) {}
13
-
14
14
  std::vector<jsi::PropNameID>
15
15
  PreparedStatementHostObject::getPropertyNames(jsi::Runtime &rt) {
16
16
  std::vector<jsi::PropNameID> keys;
@@ -24,14 +24,17 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
24
24
 
25
25
  if (name == "bind") {
26
26
  return HOSTFN("bind", 1) {
27
- if (_statement == nullptr) {
27
+ if (_stmt == nullptr) {
28
28
  throw std::runtime_error("statement has been freed");
29
29
  }
30
30
 
31
31
  const jsi::Value &js_params = args[0];
32
32
  std::vector<JSVariant> params = to_variant_vec(rt, js_params);
33
-
34
- opsqlite_bind_statement(_statement, &params);
33
+ #ifdef OP_SQLITE_USE_LIBSQL
34
+ opsqlite_libsql_bind_statement(_stmt, &params);
35
+ #else
36
+ opsqlite_bind_statement(_stmt, &params);
37
+ #endif
35
38
 
36
39
  return {};
37
40
  });
@@ -39,16 +42,20 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
39
42
 
40
43
  if (name == "execute") {
41
44
  return HOSTFN("execute", 1) {
42
- if (_statement == nullptr) {
45
+ if (_stmt == nullptr) {
43
46
  throw std::runtime_error("statement has been freed");
44
47
  }
45
48
 
46
49
  std::vector<DumbHostObject> results;
47
50
  std::shared_ptr<std::vector<SmartHostObject>> metadata =
48
51
  std::make_shared<std::vector<SmartHostObject>>();
49
-
50
- auto status = opsqlite_execute_prepared_statement(_dbName, _statement,
51
- &results, metadata);
52
+ #ifdef OP_SQLITE_USE_LIBSQL
53
+ auto status = opsqlite_libsql_execute_prepared_statement(
54
+ _name, _stmt, &results, metadata);
55
+ #else
56
+ auto status =
57
+ opsqlite_execute_prepared_statement(_name, _stmt, &results, metadata);
58
+ #endif
52
59
 
53
60
  if (status.type == SQLiteError) {
54
61
  throw std::runtime_error(status.message);
@@ -63,10 +70,17 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
63
70
  }
64
71
 
65
72
  PreparedStatementHostObject::~PreparedStatementHostObject() {
66
- if (_statement != nullptr) {
67
- sqlite3_finalize(_statement);
68
- _statement = nullptr;
73
+ #ifdef OP_SQLITE_USE_LIBSQL
74
+ if (_stmt != nullptr) {
75
+ libsql_free_stmt(_stmt);
76
+ _stmt = nullptr;
77
+ }
78
+ #else
79
+ if (_stmt != nullptr) {
80
+ sqlite3_finalize(_stmt);
81
+ _stmt = nullptr;
69
82
  }
83
+ #endif
70
84
  }
71
85
 
72
86
  } // namespace opsqlite
@@ -1,16 +1,12 @@
1
- //
2
- // PreparedStatementHostObject.hpp
3
- // op-sqlite
4
- //
5
- // Created by Oscar Franco on 5/12/23.
6
- //
7
-
8
- #ifndef PreparedStatementHostObject_h
9
- #define PreparedStatementHostObject_h
1
+ #pragma once
10
2
 
11
3
  #include <jsi/jsi.h>
12
4
  #include <memory>
5
+ #ifdef OP_SQLITE_USE_LIBSQL
6
+ #include "libsql.h"
7
+ #else
13
8
  #include <sqlite3.h>
9
+ #endif
14
10
  #include <string>
15
11
 
16
12
  namespace opsqlite {
@@ -18,7 +14,13 @@ namespace jsi = facebook::jsi;
18
14
 
19
15
  class PreparedStatementHostObject : public jsi::HostObject {
20
16
  public:
21
- PreparedStatementHostObject(std::string dbName, sqlite3_stmt *statement);
17
+ #ifdef OP_SQLITE_USE_LIBSQL
18
+ PreparedStatementHostObject(std::string name, libsql_stmt_t stmt)
19
+ : _name(name), _stmt(stmt){};
20
+ #else
21
+ PreparedStatementHostObject(std::string name, sqlite3_stmt *stmt)
22
+ : _name(name), _stmt(stmt){};
23
+ #endif
22
24
  virtual ~PreparedStatementHostObject();
23
25
 
24
26
  std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
@@ -26,11 +28,13 @@ public:
26
28
  jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
27
29
 
28
30
  private:
29
- std::string _dbName;
31
+ std::string _name;
32
+ #ifdef OP_SQLITE_USE_LIBSQL
33
+ libsql_stmt_t _stmt;
34
+ #else
30
35
  // This shouldn't be de-allocated until sqlite3_finalize is called on it
31
- sqlite3_stmt *_statement;
36
+ sqlite3_stmt *_stmt;
37
+ #endif
32
38
  };
33
39
 
34
40
  } // namespace opsqlite
35
-
36
- #endif /* PreparedStatementHostObject_hpp */
package/cpp/bindings.cpp CHANGED
@@ -1,12 +1,14 @@
1
1
  #include "bindings.h"
2
2
  #include "DBHostObject.h"
3
3
  #include "DumbHostObject.h"
4
- #include "PreparedStatementHostObject.h"
5
4
  #include "ThreadPool.h"
5
+ #ifdef OP_SQLITE_USE_LIBSQL
6
+ #include "libsql/bridge.h"
7
+ #else
6
8
  #include "bridge.h"
9
+ #endif
7
10
  #include "logs.h"
8
11
  #include "macros.h"
9
- #include "sqlbatchexecutor.h"
10
12
  #include "utils.h"
11
13
  #include <iostream>
12
14
  #include <string>
@@ -21,12 +23,6 @@ std::string basePath;
21
23
  std::string crsqlitePath;
22
24
  std::shared_ptr<react::CallInvoker> invoker;
23
25
  std::shared_ptr<ThreadPool> thread_pool = std::make_shared<ThreadPool>();
24
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>> updateHooks =
25
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>>();
26
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>> commitHooks =
27
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>>();
28
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>> rollbackHooks =
29
- // std::unordered_map<std::string, std::shared_ptr<jsi::Value>>();
30
26
 
31
27
  // React native will try to clean the module on JS context invalidation
32
28
  // (CodePush/Hot Reload) The clearState function is called and we use this flag
@@ -36,12 +32,9 @@ bool invalidated = false;
36
32
  void clearState() {
37
33
  invalidated = true;
38
34
  // Will terminate all operations and database connections
39
- opsqlite_close_all();
35
+ // opsqlite_close_all();
40
36
  // We then join all the threads before the context gets invalidated
41
37
  thread_pool->restartPool();
42
- // updateHooks.clear();
43
- // commitHooks.clear();
44
- // rollbackHooks.clear();
45
38
  }
46
39
 
47
40
  void install(jsi::Runtime &rt,
@@ -104,9 +97,18 @@ void install(jsi::Runtime &rt,
104
97
  #endif
105
98
  });
106
99
 
100
+ auto is_libsql = HOSTFN("isLibsql", 0) {
101
+ #ifdef OP_SQLITE_USE_LIBSQL
102
+ return true;
103
+ #else
104
+ return false;
105
+ #endif
106
+ });
107
+
107
108
  jsi::Object module = jsi::Object(rt);
108
109
  module.setProperty(rt, "open", std::move(open));
109
110
  module.setProperty(rt, "isSQLCipher", std::move(is_sqlcipher));
111
+ module.setProperty(rt, "isLibsql", std::move(is_libsql));
110
112
 
111
113
  rt.global().setProperty(rt, "__OPSQLiteProxy", std::move(module));
112
114
  }
package/cpp/bindings.h CHANGED
@@ -1,3 +1,5 @@
1
+ #pragma once
2
+
1
3
  #include <ReactCommon/CallInvoker.h>
2
4
  #include <jsi/jsi.h>
3
5
  #include <jsi/jsilib.h>
package/cpp/bridge.cpp CHANGED
@@ -867,4 +867,49 @@ BridgeResult opsqlite_load_extension(std::string const &db_name,
867
867
  #endif
868
868
  }
869
869
 
870
+ BatchResult opsqlite_execute_batch(std::string dbName,
871
+ std::vector<BatchArguments> *commands) {
872
+ size_t commandCount = commands->size();
873
+ if (commandCount <= 0) {
874
+ return BatchResult{
875
+ .type = SQLiteError,
876
+ .message = "No SQL commands provided",
877
+ };
878
+ }
879
+
880
+ try {
881
+ int affectedRows = 0;
882
+ opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr,
883
+ nullptr);
884
+ for (int i = 0; i < commandCount; i++) {
885
+ auto command = commands->at(i);
886
+ // We do not provide a datastructure to receive query data because we
887
+ // don't need/want to handle this results in a batch execution
888
+ auto result = opsqlite_execute(dbName, command.sql, command.params.get(),
889
+ nullptr, nullptr);
890
+ if (result.type == SQLiteError) {
891
+ opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
892
+ return BatchResult{
893
+ .type = SQLiteError,
894
+ .message = result.message,
895
+ };
896
+ } else {
897
+ affectedRows += result.affectedRows;
898
+ }
899
+ }
900
+ opsqlite_execute(dbName, "COMMIT", nullptr, nullptr, nullptr);
901
+ return BatchResult{
902
+ .type = SQLiteOk,
903
+ .affectedRows = affectedRows,
904
+ .commands = static_cast<int>(commandCount),
905
+ };
906
+ } catch (std::exception &exc) {
907
+ opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
908
+ return BatchResult{
909
+ .type = SQLiteError,
910
+ .message = exc.what(),
911
+ };
912
+ }
913
+ }
914
+
870
915
  } // namespace opsqlite
package/cpp/bridge.h CHANGED
@@ -50,6 +50,9 @@ opsqlite_execute(std::string const &dbName, std::string const &query,
50
50
  std::vector<DumbHostObject> *results,
51
51
  std::shared_ptr<std::vector<SmartHostObject>> metadatas);
52
52
 
53
+ BatchResult opsqlite_execute_batch(std::string dbName,
54
+ std::vector<BatchArguments> *commands);
55
+
53
56
  BridgeResult opsqlite_execute_raw(std::string const &dbName,
54
57
  std::string const &query,
55
58
  const std::vector<JSVariant> *params,