-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathOPSqlite.cpp
More file actions
201 lines (169 loc) · 5.79 KB
/
OPSqlite.cpp
File metadata and controls
201 lines (169 loc) · 5.79 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
#include "OPSqlite.hpp"
#include "DBHostObject.h"
#include "DumbHostObject.h"
#include "OPThreadPool.h"
#ifdef OP_SQLITE_USE_LIBSQL
#include "libsql/bridge.hpp"
#else
#include "bridge.h"
#endif
#include "logs.h"
#include "macros.hpp"
#include "utils.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
namespace opsqlite {
namespace jsi = facebook::jsi;
namespace react = facebook::react;
std::string _base_path;
std::string _crsqlite_path;
std::string _sqlite_vec_path;
std::vector<std::shared_ptr<DBHostObject>> dbs;
bool invalidated = false;
std::shared_ptr<react::CallInvoker> invoker;
// React native will try to clean the module on JS context invalidation
// (CodePush/Hot Reload) The clearState function is called
void invalidate() {
// Global flag used by the threads to stop work
invalidated = true;
for (const auto &db : dbs) {
db->invalidate();
}
// Clear our existing vector of shared pointers so they can be garbage
// collected
dbs.clear();
}
void install(jsi::Runtime &rt,
const std::shared_ptr<react::CallInvoker> &invoker,
const char *base_path, const char *crsqlite_path,
const char *sqlite_vec_path) {
_base_path = std::string(base_path);
_crsqlite_path = std::string(crsqlite_path);
_sqlite_vec_path = std::string(sqlite_vec_path);
opsqlite::invoker = invoker;
opsqlite::invalidated = false;
auto open = HFN0 {
jsi::Object options = args[0].asObject(rt);
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
std::string path = std::string(_base_path);
std::string location;
std::string encryption_key;
if (options.hasProperty(rt, "location")) {
location = options.getProperty(rt, "location").asString(rt).utf8(rt);
}
if (options.hasProperty(rt, "encryptionKey")) {
encryption_key =
options.getProperty(rt, "encryptionKey").asString(rt).utf8(rt);
}
if (!location.empty()) {
if (location == ":memory:") {
path = ":memory:";
} else if (location.rfind('/', 0) == 0) {
path = location;
} else {
path = path + "/" + location;
}
}
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, path, name, path, _crsqlite_path, _sqlite_vec_path, encryption_key);
dbs.emplace_back(db);
return jsi::Object::createFromHostObject(rt, db);
});
auto is_sqlcipher = HOST_STATIC_FN("isSQLCipher") {
#ifdef OP_SQLITE_USE_SQLCIPHER
return true;
#else
return false;
#endif
});
auto is_ios_embedded = HOST_STATIC_FN("isIOSEmbedded") {
#ifdef OP_SQLITE_USE_PHONE_VERSION
return true;
#else
return false;
#endif
});
auto is_libsql = HOST_STATIC_FN("isLibsql") {
#ifdef OP_SQLITE_USE_LIBSQL
return true;
#else
return false;
#endif
});
#ifdef OP_SQLITE_USE_LIBSQL
auto open_remote = HOST_STATIC_FN("openRemote") {
jsi::Object options = args[0].asObject(rt);
std::string url = options.getProperty(rt, "url").asString(rt).utf8(rt);
std::string auth_token =
options.getProperty(rt, "authToken").asString(rt).utf8(rt);
std::shared_ptr<DBHostObject> db =
std::make_shared<DBHostObject>(rt, url, auth_token);
return jsi::Object::createFromHostObject(rt, db);
});
auto open_sync = HOST_STATIC_FN("openSync") {
jsi::Object options = args[0].asObject(rt);
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
std::string path = std::string(_base_path);
std::string url = options.getProperty(rt, "url").asString(rt).utf8(rt);
std::string auth_token =
options.getProperty(rt, "authToken").asString(rt).utf8(rt);
int sync_interval = 0;
if (options.hasProperty(rt, "libsqlSyncInterval")) {
sync_interval = static_cast<int>(
options.getProperty(rt, "libsqlSyncInterval").asNumber());
}
bool offline = false;
if (options.hasProperty(rt, "libsqlOffline")) {
offline = options.getProperty(rt, "libsqlOffline").asBool();
}
std::string encryption_key;
if (options.hasProperty(rt, "encryptionKey")) {
encryption_key =
options.getProperty(rt, "encryptionKey").asString(rt).utf8(rt);
}
std::string remote_encryption_key;
if (options.hasProperty(rt, "remoteEncryptionKey")) {
remote_encryption_key =
options.getProperty(rt, "remoteEncryptionKey").asString(rt).utf8(rt);
}
std::string location;
if (options.hasProperty(rt, "location")) {
location = options.getProperty(rt, "location").asString(rt).utf8(rt);
}
if (!location.empty()) {
if (location == ":memory:") {
path = ":memory:";
} else if (location.rfind("/", 0) == 0) {
path = location;
} else {
path = path + "/" + location;
}
}
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, name, path, url, auth_token, sync_interval, offline, encryption_key,
remote_encryption_key);
return jsi::Object::createFromHostObject(rt, db);
});
#endif
jsi::Object module = jsi::Object(rt);
module.setProperty(rt, "open", std::move(open));
module.setProperty(rt, "isSQLCipher", std::move(is_sqlcipher));
module.setProperty(rt, "isLibsql", std::move(is_libsql));
module.setProperty(rt, "isIOSEmbedded", std::move(is_ios_embedded));
#ifdef OP_SQLITE_USE_LIBSQL
module.setProperty(rt, "openRemote", std::move(open_remote));
module.setProperty(rt, "openSync", std::move(open_sync));
#endif
rt.global().setProperty(rt, "__OPSQLiteProxy", std::move(module));
}
void expoUpdatesWorkaround(const char *base_path) {
#ifdef OP_SQLITE_USE_LIBSQL
std::string path = std::string(base_path);
// Open a DB before anything else so that expo-updates does not mess up the
// configuration
opsqlite_libsql_open("__dummy", path, "");
#endif
}
} // namespace opsqlite