-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathOPSqlite.cpp
More file actions
238 lines (201 loc) · 7.22 KB
/
Copy pathOPSqlite.cpp
File metadata and controls
238 lines (201 loc) · 7.22 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
#include "OPSqlite.hpp"
#include "DBHostObject.hpp"
#include "DumbHostObject.hpp"
#include "OPThreadPool.hpp"
#ifdef OP_SQLITE_USE_LIBSQL
#include "libsql/bridge.hpp"
#else
#include "bridge.hpp"
#endif
#include "logs.h"
#include "macros.hpp"
#include "utils.hpp"
#include <functional>
#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 _sqlite_vec_path;
std::shared_ptr<react::CallInvoker> invoker;
std::shared_ptr<std::atomic<bool>> generation_alive;
// React native will try to clean the module on JS context invalidation
// (CodePush/Hot Reload) The clearState function is called. Each currently
// open DBHostObject cleans itself up independently -- ~DBHostObject()
// already calls invalidate() (interrupt + drain + close) whenever the JS
// runtime destroys it, so there's no registry to walk here. All this needs
// to do is mark THIS generation dead so in-flight async work drops its
// result instead of resolving into whichever runtime replaces it.
void invalidate(const std::shared_ptr<std::atomic<bool>> &generation_alive) {
if (generation_alive != nullptr) {
generation_alive->store(false);
}
}
std::shared_ptr<std::atomic<bool>>
install(jsi::Runtime &rt, const std::shared_ptr<react::CallInvoker> &_invoker,
const char *base_path, const char *sqlite_vec_path) {
_base_path = std::string(base_path);
_sqlite_vec_path = std::string(sqlite_vec_path);
opsqlite::invoker = _invoker;
// Also returned to the caller: DBs opened by this generation shadow-copy
// the global at construction time (see DBHostObject.hpp), while
// invalidate() needs the shared_ptr handed back directly so it flips THIS
// generation's flag even if a newer, overlapping generation's install()
// has already reassigned the global.
auto local_generation_alive = std::make_shared<std::atomic<bool>>(true);
opsqlite::generation_alive = local_generation_alive;
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;
bool readOnly = false;
bool failOnCreate = false;
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 (options.hasProperty(rt, "readOnly")) {
readOnly = options.getProperty(rt, "readOnly").asBool();
}
if (options.hasProperty(rt, "failOnCreate")) {
failOnCreate = options.getProperty(rt, "failOnCreate").asBool();
}
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, readOnly, failOnCreate, encryption_key);
return jsi::Object::createFromHostObject(rt, db);
});
auto is_sqlcipher = HFN(=) {
#ifdef OP_SQLITE_USE_SQLCIPHER
return true;
#else
return false;
#endif
});
auto is_ios_embedded = HFN(=) {
#ifdef OP_SQLITE_USE_PHONE_VERSION
return true;
#else
return false;
#endif
});
auto is_libsql = HFN(=) {
#ifdef OP_SQLITE_USE_LIBSQL
return true;
#else
return false;
#endif
});
auto is_turso = HFN(=) {
#ifdef OP_SQLITE_USE_TURSO
return true;
#else
return false;
#endif
});
#if defined(OP_SQLITE_USE_LIBSQL) || defined(OP_SQLITE_USE_TURSO)
auto open_remote = HFN(=) {
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);
#ifdef OP_SQLITE_USE_LIBSQL
std::shared_ptr<DBHostObject> db =
std::make_shared<DBHostObject>(rt, url, auth_token);
#else
std::string path = std::string(_base_path);
std::shared_ptr<DBHostObject> db =
std::make_shared<DBHostObject>(rt, url, auth_token, path);
#endif
return jsi::Object::createFromHostObject(rt, db);
});
auto open_sync = HFN(=) {
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;
}
}
#ifdef OP_SQLITE_USE_LIBSQL
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, name, path, url, auth_token, sync_interval, offline, encryption_key,
remote_encryption_key);
#else
(void)sync_interval;
(void)offline;
std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, name, path, url, auth_token, remote_encryption_key);
#endif
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, "isTurso", std::move(is_turso));
module.setProperty(rt, "isIOSEmbedded", std::move(is_ios_embedded));
#if defined(OP_SQLITE_USE_LIBSQL) || defined(OP_SQLITE_USE_TURSO)
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));
return local_generation_alive;
}
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, false);
#endif
}
} // namespace opsqlite