-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbindings.cpp
559 lines (469 loc) · 19.4 KB
/
bindings.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "bindings.h"
#include "ConnectionPool.h"
#include "JSIHelper.h"
#include "logs.h"
#include "macros.h"
#include "sqlbatchexecutor.h"
#include "sqlite3.h"
#include "sqliteBridge.h"
#include "sqliteExecute.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace facebook;
namespace osp {
string docPathStr;
std::shared_ptr<react::CallInvoker> invoker;
jsi::Runtime *runtime;
extern "C" {
int sqlite3_powersync_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);
}
/**
* This function loads the PowerSync extension into SQLite
*/
int init_powersync_sqlite_plugin() {
int result =
sqlite3_auto_extension((void (*)(void)) & sqlite3_powersync_init);
return result;
}
void osp::clearState() { sqliteCloseAll(); }
/**
* Callback handler for SQLite table updates
*/
void updateTableHandler(void *voidDBName, int opType, char const *dbName,
char const *tableName, sqlite3_int64 rowId) {
/**
* No DB operations should occur when this callback is fired from SQLite.
* This function triggers an async invocation to call watch callbacks,
* avoiding holding SQLite up.
*/
invoker->invokeAsync([voidDBName, opType, dbName, tableName, rowId] {
try {
// Sqlite 3 just returns main as the db name if no other DBs are attached
auto global = runtime->global();
jsi::Function handlerFunction =
global.getPropertyAsFunction(*runtime, "triggerUpdateHook");
std::string actualDBName = std::string((char *)voidDBName);
auto jsiDbName = jsi::String::createFromAscii(*runtime, actualDBName);
auto jsiTableName = jsi::String::createFromAscii(*runtime, tableName);
auto jsiOpType = jsi::Value(opType);
auto jsiRowId =
jsi::String::createFromAscii(*runtime, std::to_string(rowId));
handlerFunction.call(*runtime, move(jsiDbName), move(jsiTableName),
move(jsiOpType), move(jsiRowId));
} catch (jsi::JSINativeException e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "Unknown error" << std::endl;
}
});
}
/**
* Callback handler for SQLite transaction updates
*/
void transactionFinalizerHandler(const TransactionCallbackPayload *payload) {
/**
* No DB operations should occur when this callback is fired from SQLite.
* This function triggers an async invocation to call watch callbacks,
* avoiding holding SQLite up.
*/
invoker->invokeAsync([payload] {
try {
auto global = runtime->global();
jsi::Function handlerFunction = global.getPropertyAsFunction(
*runtime, "triggerTransactionFinalizerHook");
auto jsiDbName = jsi::String::createFromAscii(*runtime, *payload->dbName);
auto jsiEventType = jsi::Value((int)payload->event);
handlerFunction.call(*runtime, move(jsiDbName), move(jsiEventType));
} catch (jsi::JSINativeException e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "Unknown error" << std::endl;
}
});
}
/**
* Callback handler for Concurrent context is available
*/
void contextLockAvailableHandler(std::string dbName,
ConnectionLockId contextId) {
invoker->invokeAsync([dbName, contextId] {
try {
auto global = runtime->global();
jsi::Function handlerFunction =
global.getPropertyAsFunction(*runtime, "onLockContextIsAvailable");
auto jsiDBName = jsi::String::createFromAscii(*runtime, dbName);
auto jsiLockID = jsi::String::createFromAscii(*runtime, contextId);
handlerFunction.call(*runtime, move(jsiDBName), move(jsiLockID));
} catch (jsi::JSINativeException e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "[contextLockAvailableHandler]: Unknown error" << std::endl;
}
});
}
void osp::install(jsi::Runtime &rt,
std::shared_ptr<react::CallInvoker> jsCallInvoker,
const char *docPath) {
docPathStr = std::string(docPath);
invoker = jsCallInvoker;
runtime = &rt;
// Any DBs opened after this call will have PowerSync SQLite extension loaded
init_powersync_sqlite_plugin();
auto open = HOSTFN("open", 2) {
if (count == 0) {
throw jsi::JSError(
rt, "[react-native-quick-sqlite][open] database name is required");
}
if (!args[0].isString()) {
throw jsi::JSError(
rt,
"[react-native-quick-sqlite][open] database name must be a string");
}
string dbName = args[0].asString(rt).utf8(rt);
string tempDocPath = string(docPathStr);
unsigned int numReadConnections = 0;
if (count > 1 && !args[1].isUndefined() && !args[1].isNull()) {
if (!args[1].isObject()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][open] database "
"options must be an object");
}
auto options = args[1].asObject(rt);
auto numReadConnectionsProperty =
options.getProperty(rt, "numReadConnections");
if (!numReadConnectionsProperty.isUndefined()) {
numReadConnections = numReadConnectionsProperty.asNumber();
}
auto locationPropertyProperty = options.getProperty(rt, "location");
if (!locationPropertyProperty.isUndefined() &&
!locationPropertyProperty.isNull()) {
tempDocPath =
tempDocPath + "/" + locationPropertyProperty.asString(rt).utf8(rt);
}
}
auto result = sqliteOpenDb(
dbName, tempDocPath, &contextLockAvailableHandler, &updateTableHandler,
&transactionFinalizerHandler, numReadConnections);
if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.errorMessage.c_str());
}
return {};
});
auto attach = HOSTFN("attach", 4) {
if (count < 3) {
throw jsi::JSError(
rt,
"[react-native-quick-sqlite][attach] Incorrect number of arguments");
}
if (!args[0].isString() || !args[1].isString() || !args[2].isString()) {
throw jsi::JSError(
rt, "dbName, databaseToAttach and alias must be a strings");
return {};
}
string tempDocPath = string(docPathStr);
if (count > 3 && !args[3].isUndefined() && !args[3].isNull()) {
if (!args[3].isString()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][attach] database "
"location must be a string");
}
tempDocPath = tempDocPath + "/" + args[3].asString(rt).utf8(rt);
}
string dbName = args[0].asString(rt).utf8(rt);
string databaseToAttach = args[1].asString(rt).utf8(rt);
string alias = args[2].asString(rt).utf8(rt);
SQLiteOPResult result =
sqliteAttachDb(dbName, tempDocPath, databaseToAttach, alias);
if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.errorMessage.c_str());
}
return {};
});
auto detach = HOSTFN("detach", 2) {
if (count < 2) {
throw jsi::JSError(
rt,
"[react-native-quick-sqlite][detach] Incorrect number of arguments");
}
if (!args[0].isString() || !args[1].isString()) {
throw jsi::JSError(
rt, "dbName, databaseToAttach and alias must be a strings");
return {};
}
string dbName = args[0].asString(rt).utf8(rt);
string alias = args[1].asString(rt).utf8(rt);
SQLiteOPResult result = sqliteDetachDb(dbName, alias);
if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.errorMessage.c_str());
}
return {};
});
auto close = HOSTFN("close", 1) {
if (count == 0) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][closeConcurrent] "
"database name is required");
}
if (!args[0].isString()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][closeConcurrent] "
"database name must be a string");
}
string dbName = args[0].asString(rt).utf8(rt);
SQLiteOPResult result = sqliteCloseDb(dbName);
if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.errorMessage.c_str());
}
return {};
});
auto remove = HOSTFN("delete", 2) {
if (count == 0) {
throw jsi::JSError(
rt, "[react-native-quick-sqlite][open] database name is required");
}
if (!args[0].isString()) {
throw jsi::JSError(
rt,
"[react-native-quick-sqlite][open] database name must be a string");
}
string dbName = args[0].asString(rt).utf8(rt);
string tempDocPath = string(docPathStr);
if (count > 1 && !args[1].isUndefined() && !args[1].isNull()) {
if (!args[1].isString()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][open] database "
"location must be a string");
}
tempDocPath = tempDocPath + "/" + args[1].asString(rt).utf8(rt);
}
SQLiteOPResult result = sqliteRemoveDb(dbName, tempDocPath);
if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.errorMessage.c_str());
}
return {};
});
auto refreshSchema = HOSTFN("refreshSchema", 1) {
if (count == 0) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][refreshSchema] database name is required");
}
if (!args[0].isString()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][refreshSchema] database name must be a string");
}
std::string dbName = args[0].asString(rt).utf8(rt);
auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
auto jsPromise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);
try {
auto future = sqliteRefreshSchema(dbName);
// Waiting for the future to complete in a separate thread
std::thread([future = std::move(future), &rt, resolve, reject]() mutable {
try {
future.get();
invoker->invokeAsync([&rt, resolve] {
resolve->asObject(rt).asFunction(rt).call(rt);
});
} catch (const std::exception& exc) {
invoker->invokeAsync([&rt, reject, exc] {
auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
auto error = errorCtr.callAsConstructor(rt, jsi::String::createFromUtf8(rt, exc.what()));
reject->asObject(rt).asFunction(rt).call(rt, error);
});
}
}).detach();
} catch (const std::exception& exc) {
invoker->invokeAsync([&rt, &exc] { jsi::JSError(rt, exc.what()); });
}
return {};
}));
return jsPromise;
});
auto executeInContext = HOSTFN("executeInContext", 3) {
if (count < 4) {
throw jsi::JSError(rt,
"[react-native-quick-sqlite][executeInContextAsync] "
"Incorrect arguments for executeInContextAsync");
}
const string dbName = args[0].asString(rt).utf8(rt);
const string contextLockId = args[1].asString(rt).utf8(rt);
const string query = args[2].asString(rt).utf8(rt);
const jsi::Value &originalParams = args[3];
// Converting query parameters inside the javascript caller thread
vector<QuickValue> params;
jsiQueryArgumentsToSequelParam(rt, originalParams, ¶ms);
auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);
auto task = [&rt, dbName, contextLockId, query,
params = make_shared<vector<QuickValue>>(params), resolve,
reject](sqlite3 *db) {
try {
vector<map<string, QuickValue>> results;
vector<QuickColumnMetadata> metadata;
auto status =
sqliteExecuteWithDB(db, query, params.get(), &results, &metadata);
invoker->invokeAsync(
[&rt,
results = make_shared<vector<map<string, QuickValue>>>(results),
metadata = make_shared<vector<QuickColumnMetadata>>(metadata),
status_copy = move(status), resolve, reject] {
if (status_copy.type == SQLiteOk) {
auto jsiResult = createSequelQueryExecutionResult(
rt, status_copy, results.get(), metadata.get());
resolve->asObject(rt).asFunction(rt).call(rt,
move(jsiResult));
} else {
auto errorCtr =
rt.global().getPropertyAsFunction(rt, "Error");
auto error = errorCtr.callAsConstructor(
rt, jsi::String::createFromUtf8(
rt, status_copy.errorMessage));
reject->asObject(rt).asFunction(rt).call(rt, error);
}
});
} catch (std::exception &exc) {
invoker->invokeAsync([&rt, &exc] { jsi::JSError(rt, exc.what()); });
}
};
sqliteQueueInContext(dbName, contextLockId, task);
return {};
}));
return promise;
});
auto executeBatch = HOSTFN("executeBatch", 2) {
if (sizeof(args) < 3) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][executeAsyncBatch] "
"Incorrect parameter count");
return {};
}
const jsi::Value ¶ms = args[1];
if (params.isNull() || params.isUndefined()) {
throw jsi::JSError(rt,
"[react-native-quick-sqlite][executeAsyncBatch] - An "
"array of SQL commands or parameters is needed");
return {};
}
const string dbName = args[0].asString(rt).utf8(rt);
const jsi::Array &batchParams = params.asObject(rt).asArray(rt);
const string contextLockId = args[2].asString(rt).utf8(rt);
vector<QuickQueryArguments> commands;
jsiBatchParametersToQuickArguments(rt, batchParams, &commands);
auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);
auto task = [&rt, dbName,
commands =
make_shared<vector<QuickQueryArguments>>(commands),
resolve, reject, contextLockId](sqlite3 *db) {
try {
// Inside the new worker thread, we can now call sqlite operations
auto batchResult = sqliteExecuteBatch(db, commands.get());
invoker->invokeAsync(
[&rt, batchResult = move(batchResult), resolve, reject] {
if (batchResult.type == SQLiteOk) {
auto res = jsi::Object(rt);
res.setProperty(rt, "rowsAffected",
jsi::Value(batchResult.affectedRows));
resolve->asObject(rt).asFunction(rt).call(rt, move(res));
} else {
throw jsi::JSError(rt, batchResult.message);
}
});
} catch (std::exception &exc) {
invoker->invokeAsync(
[&rt, reject, &exc] { throw jsi::JSError(rt, exc.what()); });
}
};
sqliteQueueInContext(dbName, contextLockId, task);
return {};
}));
return promise;
});
// Load SQL File from disk in another thread
auto loadFileAsync = HOSTFN("loadFile", 2) {
if (sizeof(args) < 3) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][loadFileAsync] "
"Incorrect parameter count");
return {};
}
const string dbName = args[0].asString(rt).utf8(rt);
const string sqlFileName = args[1].asString(rt).utf8(rt);
const string contextLockId = args[2].asString(rt).utf8(rt);
auto promiseCtr = rt.global().getPropertyAsFunction(rt, "Promise");
auto promise = promiseCtr.callAsConstructor(rt, HOSTFN("executor", 2) {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);
auto task = [&rt, dbName, sqlFileName, resolve, reject](sqlite3 *db) {
try {
const auto importResult = sqliteImportFile(db, sqlFileName);
invoker->invokeAsync(
[&rt, result = move(importResult), resolve, reject] {
if (result.type == SQLiteOk) {
auto res = jsi::Object(rt);
res.setProperty(rt, "rowsAffected",
jsi::Value(result.affectedRows));
res.setProperty(rt, "commands", jsi::Value(result.commands));
resolve->asObject(rt).asFunction(rt).call(rt, move(res));
} else {
throw jsi::JSError(rt, result.message);
}
});
} catch (std::exception &exc) {
// LOGW("Catched exception: %s", exc.what());
invoker->invokeAsync(
[&rt, err = exc.what(), reject] { throw jsi::JSError(rt, err); });
}
};
sqliteQueueInContext(dbName, contextLockId, task);
return {};
}));
return promise;
});
auto requestLock = HOSTFN("requestLock", 3) {
if (count < 3) {
throw jsi::JSError(rt,
"[react-native-quick-sqlite][requestLock] "
"database name, lock ID and lock type are required");
}
if (!args[0].isString() || !args[1].isString() || !args[2].isNumber()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][requestLock] "
"invalid argument types received");
}
string dbName = args[0].asString(rt).utf8(rt);
string lockId = args[1].asString(rt).utf8(rt);
ConcurrentLockType lockType = (ConcurrentLockType)args[2].asNumber();
auto lockResult = sqliteRequestLock(dbName, lockId, lockType);
vector<map<string, QuickValue>> resultsHolder;
auto jsiResult =
createSequelQueryExecutionResult(rt, lockResult, &resultsHolder, NULL);
return jsiResult;
});
auto releaseLock = HOSTFN("releaseLock", 3) {
if (count < 2) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][requestLock] "
"database name and lock ID are required");
}
if (!args[0].isString() || !args[1].isString()) {
throw jsi::JSError(rt, "[react-native-quick-sqlite][requestLock] "
"invalid argument types received");
}
string dbName = args[0].asString(rt).utf8(rt);
string lockId = args[1].asString(rt).utf8(rt);
sqliteReleaseLock(dbName, lockId);
return {};
});
jsi::Object module = jsi::Object(rt);
module.setProperty(rt, "open", move(open));
module.setProperty(rt, "requestLock", move(requestLock));
module.setProperty(rt, "releaseLock", move(releaseLock));
module.setProperty(rt, "executeInContext", move(executeInContext));
module.setProperty(rt, "close", move(close));
module.setProperty(rt, "refreshSchema", move(refreshSchema));
module.setProperty(rt, "attach", move(attach));
module.setProperty(rt, "detach", move(detach));
module.setProperty(rt, "delete", move(remove));
module.setProperty(rt, "executeBatch", move(executeBatch));
module.setProperty(rt, "loadFileAsync", move(loadFileAsync));
rt.global().setProperty(rt, "__QuickSQLiteProxy", move(module));
}
} // namespace osp