@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/DBHostObject.h
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
#include <ReactCommon/CallInvoker.h>
|
|
6
6
|
#include <jsi/jsi.h>
|
|
7
7
|
#include <set>
|
|
8
|
+
#ifdef OP_SQLITE_USE_LIBSQL
|
|
9
|
+
#include "libsql/bridge.h"
|
|
10
|
+
#else
|
|
8
11
|
#include <sqlite3.h>
|
|
12
|
+
#endif
|
|
9
13
|
#include <unordered_map>
|
|
10
14
|
#include <vector>
|
|
11
15
|
|
|
@@ -26,51 +30,55 @@ struct TableRowDiscriminator {
|
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
struct ReactiveQuery {
|
|
33
|
+
#ifndef OP_SQLITE_USE_LIBSQL
|
|
29
34
|
sqlite3_stmt *stmt;
|
|
35
|
+
#endif
|
|
30
36
|
std::vector<TableRowDiscriminator> discriminators;
|
|
31
37
|
std::shared_ptr<jsi::Value> callback;
|
|
32
38
|
};
|
|
33
39
|
|
|
34
40
|
class JSI_EXPORT DBHostObject : public jsi::HostObject {
|
|
35
41
|
public:
|
|
36
|
-
//
|
|
42
|
+
// Normal constructor shared between all backends
|
|
37
43
|
DBHostObject(jsi::Runtime &rt, std::string &base_path,
|
|
38
44
|
std::shared_ptr<react::CallInvoker> invoker,
|
|
39
|
-
std::
|
|
40
|
-
std::string &
|
|
41
|
-
std::string &
|
|
45
|
+
std::string &db_name, std::string &path,
|
|
46
|
+
std::string &crsqlite_path, std::string &sqlite_vec_path,
|
|
47
|
+
std::string &encryption_key);
|
|
42
48
|
|
|
43
49
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
44
50
|
// Constructor for remoteOpen, purely for remote databases
|
|
45
51
|
DBHostObject(jsi::Runtime &rt, std::string &url, std::string &auth_token,
|
|
46
|
-
std::shared_ptr<react::CallInvoker> invoker
|
|
47
|
-
std::shared_ptr<ThreadPool> thread_pool);
|
|
52
|
+
std::shared_ptr<react::CallInvoker> invoker);
|
|
48
53
|
|
|
49
54
|
// Constructor for a local database with remote sync
|
|
50
55
|
DBHostObject(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker,
|
|
51
|
-
std::
|
|
52
|
-
std::string &
|
|
53
|
-
int sync_interval);
|
|
56
|
+
std::string &db_name, std::string &path, std::string &url,
|
|
57
|
+
std::string &auth_token, int sync_interval);
|
|
54
58
|
#endif
|
|
55
59
|
|
|
56
|
-
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
|
|
57
|
-
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
|
|
60
|
+
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
|
61
|
+
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;
|
|
58
62
|
void set(jsi::Runtime &rt, const jsi::PropNameID &name,
|
|
59
|
-
const jsi::Value &value);
|
|
63
|
+
const jsi::Value &value) override;
|
|
64
|
+
void on_update(const std::string &table, const std::string &operation,
|
|
65
|
+
long long row_id);
|
|
66
|
+
void on_commit();
|
|
67
|
+
void on_rollback();
|
|
60
68
|
void invalidate();
|
|
61
|
-
~DBHostObject();
|
|
69
|
+
~DBHostObject() override;
|
|
62
70
|
|
|
63
71
|
private:
|
|
64
72
|
std::set<std::shared_ptr<ReactiveQuery>> pending_reactive_queries;
|
|
65
73
|
void auto_register_update_hook();
|
|
66
74
|
void create_jsi_functions();
|
|
67
|
-
void
|
|
75
|
+
void
|
|
76
|
+
flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);
|
|
68
77
|
|
|
69
78
|
std::unordered_map<std::string, jsi::Value> function_map;
|
|
70
79
|
std::string base_path;
|
|
71
|
-
std::shared_ptr<jsi::Value> update_hook;
|
|
72
80
|
std::shared_ptr<react::CallInvoker> invoker;
|
|
73
|
-
std::shared_ptr<ThreadPool>
|
|
81
|
+
std::shared_ptr<ThreadPool> _thread_pool;
|
|
74
82
|
std::string db_name;
|
|
75
83
|
std::shared_ptr<jsi::Value> update_hook_callback;
|
|
76
84
|
std::shared_ptr<jsi::Value> commit_hook_callback;
|
|
@@ -80,6 +88,11 @@ private:
|
|
|
80
88
|
std::vector<PendingReactiveInvocation> pending_reactive_invocations;
|
|
81
89
|
bool is_update_hook_registered = false;
|
|
82
90
|
bool invalidated = false;
|
|
91
|
+
#ifdef OP_SQLITE_USE_LIBSQL
|
|
92
|
+
DB db;
|
|
93
|
+
#else
|
|
94
|
+
sqlite3 *db;
|
|
95
|
+
#endif
|
|
83
96
|
};
|
|
84
97
|
|
|
85
98
|
} // namespace opsqlite
|
package/cpp/DumbHostObject.cpp
CHANGED
|
@@ -33,13 +33,13 @@ jsi::Value DumbHostObject::get(jsi::Runtime &rt,
|
|
|
33
33
|
for (int i = 0; i < fields->size(); i++) {
|
|
34
34
|
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
|
|
35
35
|
if (fieldName == name) {
|
|
36
|
-
return
|
|
36
|
+
return to_jsi(rt, values.at(i));
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
for (auto pairField : ownValues) {
|
|
41
41
|
if (name == pairField.first) {
|
|
42
|
-
return
|
|
42
|
+
return to_jsi(rt, pairField.second);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -53,19 +53,19 @@ void DumbHostObject::set(jsi::Runtime &rt, const jsi::PropNameID &name,
|
|
|
53
53
|
for (int i = 0; i < fields->size(); i++) {
|
|
54
54
|
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
|
|
55
55
|
if (fieldName == key) {
|
|
56
|
-
values[i] =
|
|
56
|
+
values[i] = to_variant(rt, value);
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
for (auto pairField : ownValues) {
|
|
62
62
|
if (key == pairField.first) {
|
|
63
|
-
pairField.second =
|
|
63
|
+
pairField.second = to_variant(rt, value);
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
ownValues.push_back(std::make_pair(key,
|
|
68
|
+
ownValues.push_back(std::make_pair(key, to_variant(rt, value)));
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
} // namespace opsqlite
|
package/cpp/DumbHostObject.h
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define DumbHostObject_h
|
|
3
|
-
|
|
4
|
-
#include <stdio.h>
|
|
1
|
+
#pragma once
|
|
5
2
|
|
|
6
3
|
#include "SmartHostObject.h"
|
|
7
4
|
#include "types.h"
|
|
8
5
|
#include <any>
|
|
9
6
|
#include <jsi/jsi.h>
|
|
7
|
+
#include <stdio.h>
|
|
10
8
|
#include <vector>
|
|
11
9
|
|
|
12
10
|
namespace opsqlite {
|
|
@@ -15,16 +13,17 @@ namespace jsi = facebook::jsi;
|
|
|
15
13
|
|
|
16
14
|
class JSI_EXPORT DumbHostObject : public jsi::HostObject {
|
|
17
15
|
public:
|
|
18
|
-
DumbHostObject()
|
|
16
|
+
DumbHostObject() = default;
|
|
19
17
|
|
|
20
|
-
DumbHostObject(
|
|
18
|
+
explicit DumbHostObject(
|
|
19
|
+
std::shared_ptr<std::vector<SmartHostObject>> metadata);
|
|
21
20
|
|
|
22
|
-
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
|
|
21
|
+
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
|
23
22
|
|
|
24
|
-
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
|
|
23
|
+
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;
|
|
25
24
|
|
|
26
25
|
void set(jsi::Runtime &rt, const jsi::PropNameID &name,
|
|
27
|
-
const jsi::Value &value);
|
|
26
|
+
const jsi::Value &value) override;
|
|
28
27
|
|
|
29
28
|
std::vector<JSVariant> values;
|
|
30
29
|
|
|
@@ -34,5 +33,3 @@ public:
|
|
|
34
33
|
};
|
|
35
34
|
|
|
36
35
|
} // namespace opsqlite
|
|
37
|
-
|
|
38
|
-
#endif /* DumbHostObject_h */
|
|
@@ -30,13 +30,45 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
|
|
|
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
|
+
auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
|
|
35
|
+
auto promise = promiseCtr.callAsConstructor(
|
|
36
|
+
rt, HOSTFN("executor") {
|
|
37
|
+
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
|
|
38
|
+
auto reject = std::make_shared<jsi::Value>(rt, args[1]);
|
|
39
|
+
auto task = [&rt, this, resolve, reject,
|
|
40
|
+
invoker = this->_js_call_invoker, params]() {
|
|
41
|
+
try {
|
|
33
42
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
34
|
-
|
|
43
|
+
opsqlite_libsql_bind_statement(_stmt, ¶ms);
|
|
35
44
|
#else
|
|
36
|
-
|
|
45
|
+
opsqlite_bind_statement(_stmt, ¶ms);
|
|
37
46
|
#endif
|
|
47
|
+
invoker->invokeAsync([&rt, resolve] {
|
|
48
|
+
resolve->asObject(rt).asFunction(rt).call(rt, {});
|
|
49
|
+
});
|
|
50
|
+
} catch (const std::runtime_error &e) {
|
|
51
|
+
invoker->invokeAsync([&rt, e, reject] {
|
|
52
|
+
auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
|
|
53
|
+
auto error = errorCtr.callAsConstructor(
|
|
54
|
+
rt, jsi::String::createFromUtf8(rt, e.what()));
|
|
55
|
+
reject->asObject(rt).asFunction(rt).call(rt, error);
|
|
56
|
+
});
|
|
57
|
+
} catch (const std::exception &e) {
|
|
58
|
+
invoker->invokeAsync([&rt, e, reject] {
|
|
59
|
+
auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
|
|
60
|
+
auto error = errorCtr.callAsConstructor(
|
|
61
|
+
rt, jsi::String::createFromUtf8(rt, e.what()));
|
|
62
|
+
reject->asObject(rt).asFunction(rt).call(rt, error);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
_thread_pool->queueWork(task);
|
|
38
68
|
|
|
39
|
-
|
|
69
|
+
return {};
|
|
70
|
+
}));
|
|
71
|
+
return promise;
|
|
40
72
|
});
|
|
41
73
|
}
|
|
42
74
|
|
|
@@ -56,30 +88,32 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
|
|
|
56
88
|
std::vector<DumbHostObject> results;
|
|
57
89
|
std::shared_ptr<std::vector<SmartHostObject>> metadata =
|
|
58
90
|
std::make_shared<std::vector<SmartHostObject>>();
|
|
91
|
+
try {
|
|
59
92
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
60
|
-
|
|
61
|
-
|
|
93
|
+
auto status = opsqlite_libsql_execute_prepared_statement(
|
|
94
|
+
_db, _stmt, &results, metadata);
|
|
62
95
|
#else
|
|
63
|
-
|
|
64
|
-
|
|
96
|
+
auto status = opsqlite_execute_prepared_statement(
|
|
97
|
+
_db, _stmt, &results, metadata);
|
|
65
98
|
#endif
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
99
|
+
invoker->invokeAsync(
|
|
100
|
+
[&rt, status = std::move(status),
|
|
101
|
+
results =
|
|
102
|
+
std::make_shared<std::vector<DumbHostObject>>(results),
|
|
103
|
+
metadata, resolve] {
|
|
71
104
|
auto jsiResult =
|
|
72
105
|
create_result(rt, status, results.get(), metadata);
|
|
73
106
|
resolve->asObject(rt).asFunction(rt).call(
|
|
74
107
|
rt, std::move(jsiResult));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
108
|
+
});
|
|
109
|
+
} catch (std::exception &exc) {
|
|
110
|
+
invoker->invokeAsync([&rt, &exc, reject] {
|
|
111
|
+
auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
|
|
112
|
+
auto error = errorCtr.callAsConstructor(
|
|
113
|
+
rt, jsi::String::createFromUtf8(rt, exc.what()));
|
|
114
|
+
reject->asObject(rt).asFunction(rt).call(rt, error);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
83
117
|
};
|
|
84
118
|
|
|
85
119
|
_thread_pool->queueWork(task);
|
|
@@ -4,12 +4,14 @@
|
|
|
4
4
|
#include <jsi/jsi.h>
|
|
5
5
|
#include <memory>
|
|
6
6
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
7
|
+
#include "libsql/bridge.h"
|
|
7
8
|
#include "libsql.h"
|
|
8
9
|
#else
|
|
9
10
|
#include <sqlite3.h>
|
|
10
11
|
#endif
|
|
11
12
|
#include "ThreadPool.h"
|
|
12
13
|
#include <string>
|
|
14
|
+
#include <utility>
|
|
13
15
|
|
|
14
16
|
namespace opsqlite {
|
|
15
17
|
namespace jsi = facebook::jsi;
|
|
@@ -19,35 +21,37 @@ class PreparedStatementHostObject : public jsi::HostObject {
|
|
|
19
21
|
public:
|
|
20
22
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
21
23
|
PreparedStatementHostObject(
|
|
22
|
-
std::string name, libsql_stmt_t stmt,
|
|
24
|
+
DB const &db, std::string name, libsql_stmt_t stmt,
|
|
23
25
|
std::shared_ptr<react::CallInvoker> js_call_invoker,
|
|
24
26
|
std::shared_ptr<ThreadPool> thread_pool)
|
|
25
|
-
: _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
|
|
26
|
-
_thread_pool(thread_pool){};
|
|
27
|
+
: _db(db), _name(std::move(name)), _stmt(stmt), _js_call_invoker(js_call_invoker),
|
|
28
|
+
_thread_pool(thread_pool) {};
|
|
27
29
|
#else
|
|
28
30
|
PreparedStatementHostObject(
|
|
29
|
-
std::string name, sqlite3_stmt *stmt,
|
|
31
|
+
sqlite3 *db, std::string name, sqlite3_stmt *stmt,
|
|
30
32
|
std::shared_ptr<react::CallInvoker> js_call_invoker,
|
|
31
33
|
std::shared_ptr<ThreadPool> thread_pool)
|
|
32
|
-
: _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
|
|
33
|
-
_thread_pool(thread_pool){};
|
|
34
|
+
: _db(db), _name(std::move(name)), _stmt(stmt), _js_call_invoker(std::move(js_call_invoker)),
|
|
35
|
+
_thread_pool(std::move(thread_pool)) {};
|
|
34
36
|
#endif
|
|
35
|
-
|
|
37
|
+
~PreparedStatementHostObject() override;
|
|
36
38
|
|
|
37
|
-
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
|
|
39
|
+
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
|
38
40
|
|
|
39
|
-
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
|
|
41
|
+
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;
|
|
40
42
|
|
|
41
43
|
private:
|
|
42
|
-
std::shared_ptr<react::CallInvoker> _js_call_invoker;
|
|
43
|
-
std::shared_ptr<ThreadPool> _thread_pool;
|
|
44
44
|
std::string _name;
|
|
45
45
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
46
|
+
DB _db;
|
|
46
47
|
libsql_stmt_t _stmt;
|
|
47
48
|
#else
|
|
49
|
+
sqlite3 *_db;
|
|
48
50
|
// This shouldn't be de-allocated until sqlite3_finalize is called on it
|
|
49
51
|
sqlite3_stmt *_stmt;
|
|
50
52
|
#endif
|
|
53
|
+
std::shared_ptr<react::CallInvoker> _js_call_invoker;
|
|
54
|
+
std::shared_ptr<ThreadPool> _thread_pool;
|
|
51
55
|
};
|
|
52
56
|
|
|
53
57
|
} // namespace opsqlite
|
package/cpp/SmartHostObject.cpp
CHANGED
|
@@ -9,8 +9,9 @@ std::vector<jsi::PropNameID>
|
|
|
9
9
|
SmartHostObject::getPropertyNames(jsi::Runtime &rt) {
|
|
10
10
|
std::vector<jsi::PropNameID> keys;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
keys.reserve(fields.size());
|
|
13
|
+
for (const auto& field : fields) {
|
|
14
|
+
keys.emplace_back(jsi::PropNameID::forAscii(rt, field.first));
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
return keys;
|
|
@@ -20,10 +21,10 @@ jsi::Value SmartHostObject::get(jsi::Runtime &rt,
|
|
|
20
21
|
const jsi::PropNameID &propNameID) {
|
|
21
22
|
auto name = propNameID.utf8(rt);
|
|
22
23
|
|
|
23
|
-
for (auto field : fields) {
|
|
24
|
+
for (const auto& field : fields) {
|
|
24
25
|
auto fieldName = field.first;
|
|
25
26
|
if (fieldName == name) {
|
|
26
|
-
return
|
|
27
|
+
return to_jsi(rt, field.second);
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
|
package/cpp/SmartHostObject.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define SmartHostObject_h
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include "types.h"
|
|
5
4
|
#include <any>
|
|
@@ -12,15 +11,13 @@ namespace jsi = facebook::jsi;
|
|
|
12
11
|
|
|
13
12
|
class JSI_EXPORT SmartHostObject : public jsi::HostObject {
|
|
14
13
|
public:
|
|
15
|
-
SmartHostObject()
|
|
14
|
+
SmartHostObject() = default;
|
|
16
15
|
|
|
17
|
-
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
|
|
16
|
+
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
|
|
18
17
|
|
|
19
|
-
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
|
|
18
|
+
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;
|
|
20
19
|
|
|
21
20
|
std::vector<std::pair<std::string, JSVariant>> fields;
|
|
22
21
|
};
|
|
23
22
|
|
|
24
23
|
} // namespace opsqlite
|
|
25
|
-
|
|
26
|
-
#endif /* SmartHostObject_h */
|
package/cpp/ThreadPool.cpp
CHANGED
|
@@ -6,16 +6,17 @@ ThreadPool::ThreadPool() : done(false) {
|
|
|
6
6
|
// This returns the number of threads supported by the system. If the
|
|
7
7
|
// function can't figure out this information, it returns 0. 0 is not good,
|
|
8
8
|
// so we create at least 1
|
|
9
|
-
auto numberOfThreads = std::thread::hardware_concurrency();
|
|
10
|
-
if (numberOfThreads == 0) {
|
|
11
|
-
numberOfThreads = 1;
|
|
12
|
-
}
|
|
9
|
+
// auto numberOfThreads = std::thread::hardware_concurrency();
|
|
10
|
+
// if (numberOfThreads == 0) {
|
|
11
|
+
// numberOfThreads = 1;
|
|
12
|
+
// }
|
|
13
13
|
|
|
14
|
+
auto numberOfThreads = 1;
|
|
14
15
|
for (unsigned i = 0; i < numberOfThreads; ++i) {
|
|
15
16
|
// The threads will execute the private member `doWork`. Note that we need
|
|
16
17
|
// to pass a reference to the function (namespaced with the class name) as
|
|
17
18
|
// the first argument, and the current object as second argument
|
|
18
|
-
threads.
|
|
19
|
+
threads.emplace_back(&ThreadPool::doWork, this);
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -39,7 +40,7 @@ ThreadPool::~ThreadPool() {
|
|
|
39
40
|
|
|
40
41
|
// This function will be called by the server every time there is a request
|
|
41
42
|
// that needs to be processed by the thread pool
|
|
42
|
-
void ThreadPool::queueWork(std::function<void(void)
|
|
43
|
+
void ThreadPool::queueWork(const std::function<void(void)>& task) {
|
|
43
44
|
// Grab the mutex
|
|
44
45
|
std::lock_guard<std::mutex> g(workQueueMutex);
|
|
45
46
|
|
|
@@ -65,7 +66,7 @@ void ThreadPool::doWork() {
|
|
|
65
66
|
return !workQueue.empty() || done;
|
|
66
67
|
});
|
|
67
68
|
|
|
68
|
-
// If we are shutting down exit
|
|
69
|
+
// If we are shutting down exit without trying to process more work
|
|
69
70
|
if (done) {
|
|
70
71
|
break;
|
|
71
72
|
}
|
|
@@ -109,7 +110,7 @@ void ThreadPool::restartPool() {
|
|
|
109
110
|
// The threads will execute the private member `doWork`. Note that we need
|
|
110
111
|
// to pass a reference to the function (namespaced with the class name) as
|
|
111
112
|
// the first argument, and the current object as second argument
|
|
112
|
-
threads.
|
|
113
|
+
threads.emplace_back(&ThreadPool::doWork, this);
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
done = false;
|
package/cpp/ThreadPool.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define ThreadPool_h
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <condition_variable>
|
|
5
4
|
#include <exception>
|
|
@@ -15,12 +14,12 @@ class ThreadPool {
|
|
|
15
14
|
public:
|
|
16
15
|
ThreadPool();
|
|
17
16
|
~ThreadPool();
|
|
18
|
-
void queueWork(std::function<void(void)
|
|
17
|
+
void queueWork(const std::function<void(void)>& task);
|
|
19
18
|
void waitFinished();
|
|
20
19
|
void restartPool();
|
|
21
20
|
|
|
22
21
|
private:
|
|
23
|
-
unsigned int busy;
|
|
22
|
+
unsigned int busy{};
|
|
24
23
|
// This condition variable is used for the threads to wait until there is work
|
|
25
24
|
// to do
|
|
26
25
|
std::condition_variable_any workQueueConditionVariable;
|
|
@@ -42,6 +41,4 @@ private:
|
|
|
42
41
|
void doWork();
|
|
43
42
|
};
|
|
44
43
|
|
|
45
|
-
} // namespace opsqlite
|
|
46
|
-
|
|
47
|
-
#endif /* ThreadPool_h */
|
|
44
|
+
} // namespace opsqlite
|
package/cpp/bindings.cpp
CHANGED
|
@@ -22,40 +22,29 @@ namespace jsi = facebook::jsi;
|
|
|
22
22
|
std::string _base_path;
|
|
23
23
|
std::string _crsqlite_path;
|
|
24
24
|
std::string _sqlite_vec_path;
|
|
25
|
-
std::shared_ptr<ThreadPool> thread_pool = std::make_shared<ThreadPool>();
|
|
26
25
|
std::vector<std::shared_ptr<DBHostObject>> dbs;
|
|
27
26
|
|
|
28
27
|
// React native will try to clean the module on JS context invalidation
|
|
29
|
-
// (CodePush/Hot Reload) The clearState function is called
|
|
30
|
-
|
|
31
|
-
bool invalidated = false;
|
|
32
|
-
|
|
33
|
-
void clearState() {
|
|
28
|
+
// (CodePush/Hot Reload) The clearState function is called
|
|
29
|
+
void invalidate() {
|
|
34
30
|
for (const auto &db : dbs) {
|
|
35
31
|
db->invalidate();
|
|
36
32
|
}
|
|
37
|
-
invalidated = true;
|
|
38
|
-
|
|
39
|
-
#ifdef OP_SQLITE_USE_LIBSQL
|
|
40
|
-
opsqlite_libsql_close_all();
|
|
41
|
-
#else
|
|
42
|
-
opsqlite_close_all();
|
|
43
|
-
#endif
|
|
44
33
|
|
|
45
|
-
//
|
|
46
|
-
|
|
34
|
+
// Clear our existing vector of shared pointers so they can be garbage
|
|
35
|
+
// collected
|
|
36
|
+
dbs.clear();
|
|
47
37
|
}
|
|
48
38
|
|
|
49
39
|
void install(jsi::Runtime &rt,
|
|
50
40
|
const std::shared_ptr<react::CallInvoker> &invoker,
|
|
51
41
|
const char *base_path, const char *crsqlite_path,
|
|
52
42
|
const char *sqlite_vec_path) {
|
|
53
|
-
invalidated = false;
|
|
54
43
|
_base_path = std::string(base_path);
|
|
55
44
|
_crsqlite_path = std::string(crsqlite_path);
|
|
56
45
|
_sqlite_vec_path = std::string(sqlite_vec_path);
|
|
57
46
|
|
|
58
|
-
auto open =
|
|
47
|
+
auto open = HOST_STATIC_FN("open") {
|
|
59
48
|
jsi::Object options = args[0].asObject(rt);
|
|
60
49
|
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
|
|
61
50
|
std::string path = std::string(_base_path);
|
|
@@ -89,13 +78,13 @@ void install(jsi::Runtime &rt,
|
|
|
89
78
|
}
|
|
90
79
|
|
|
91
80
|
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
|
|
92
|
-
rt, path, invoker,
|
|
93
|
-
|
|
81
|
+
rt, path, invoker, name, path, _crsqlite_path, _sqlite_vec_path,
|
|
82
|
+
encryptionKey);
|
|
94
83
|
dbs.emplace_back(db);
|
|
95
84
|
return jsi::Object::createFromHostObject(rt, db);
|
|
96
85
|
});
|
|
97
86
|
|
|
98
|
-
auto is_sqlcipher =
|
|
87
|
+
auto is_sqlcipher = HOST_STATIC_FN("isSQLCipher") {
|
|
99
88
|
#ifdef OP_SQLITE_USE_SQLCIPHER
|
|
100
89
|
return true;
|
|
101
90
|
#else
|
|
@@ -103,7 +92,7 @@ void install(jsi::Runtime &rt,
|
|
|
103
92
|
#endif
|
|
104
93
|
});
|
|
105
94
|
|
|
106
|
-
auto is_libsql =
|
|
95
|
+
auto is_libsql = HOST_STATIC_FN("isLibsql") {
|
|
107
96
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
108
97
|
return true;
|
|
109
98
|
#else
|
|
@@ -112,18 +101,18 @@ void install(jsi::Runtime &rt,
|
|
|
112
101
|
});
|
|
113
102
|
|
|
114
103
|
#ifdef OP_SQLITE_USE_LIBSQL
|
|
115
|
-
auto open_remote =
|
|
104
|
+
auto open_remote = HOST_STATIC_FN("openRemote") {
|
|
116
105
|
jsi::Object options = args[0].asObject(rt);
|
|
117
106
|
std::string url = options.getProperty(rt, "url").asString(rt).utf8(rt);
|
|
118
107
|
std::string auth_token =
|
|
119
108
|
options.getProperty(rt, "authToken").asString(rt).utf8(rt);
|
|
120
109
|
|
|
121
110
|
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
|
|
122
|
-
rt, url, auth_token, invoker
|
|
111
|
+
rt, url, auth_token, invoker);
|
|
123
112
|
return jsi::Object::createFromHostObject(rt, db);
|
|
124
113
|
});
|
|
125
114
|
|
|
126
|
-
auto open_sync =
|
|
115
|
+
auto open_sync = HOST_STATIC_FN("openSync") {
|
|
127
116
|
jsi::Object options = args[0].asObject(rt);
|
|
128
117
|
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
|
|
129
118
|
std::string path = std::string(_base_path);
|
|
@@ -152,7 +141,7 @@ void install(jsi::Runtime &rt,
|
|
|
152
141
|
}
|
|
153
142
|
|
|
154
143
|
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
|
|
155
|
-
rt, invoker,
|
|
144
|
+
rt, invoker, name, path, url, auth_token, sync_interval);
|
|
156
145
|
return jsi::Object::createFromHostObject(rt, db);
|
|
157
146
|
});
|
|
158
147
|
#endif
|
package/cpp/bindings.h
CHANGED
|
@@ -9,9 +9,10 @@ namespace opsqlite {
|
|
|
9
9
|
namespace jsi = facebook::jsi;
|
|
10
10
|
namespace react = facebook::react;
|
|
11
11
|
|
|
12
|
-
void install(jsi::Runtime &rt,
|
|
12
|
+
void install(jsi::Runtime &rt,
|
|
13
|
+
const std::shared_ptr<react::CallInvoker> &invoker,
|
|
13
14
|
const char *base_path, const char *crsqlite_path,
|
|
14
15
|
const char *sqlite_vec_path);
|
|
15
|
-
void
|
|
16
|
+
void invalidate();
|
|
16
17
|
|
|
17
18
|
} // namespace opsqlite
|