-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbindings.cpp
More file actions
454 lines (377 loc) · 13.9 KB
/
bindings.cpp
File metadata and controls
454 lines (377 loc) · 13.9 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
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
#include "bindings.h"
#include "sqliteBridge.h"
#include "logs.h"
#include "JSIHelper.h"
#include "ThreadPool.h"
#include "sqlfileloader.h"
#include "sqlbatchexecutor.h"
#include <vector>
#include <string>
#include "macros.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace facebook;
namespace osp {
string docPathStr;
std::shared_ptr<react::CallInvoker> invoker;
void clearState() {
sqliteCloseAll();
}
void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker, const char *docPath, const char *cachePath)
{
// android 12 fix
setenv("TMPDIR", cachePath, 1);
docPathStr = std::string(docPath);
auto pool = std::make_shared<ThreadPool>();
invoker = jsCallInvoker;
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);
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 = sqliteOpenDb(dbName, tempDocPath);
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][close] database name is required");
}
if (!args[0].isString())
{
throw jsi::JSError(rt, "[react-native-quick-sqlite][close] 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 execute = HOSTFN("execute", 3)
{
const string dbName = args[0].asString(rt).utf8(rt);
const string query = args[1].asString(rt).utf8(rt);
vector<QuickValue> params;
if(count == 3) {
const jsi::Value &originalParams = args[2];
jsiQueryArgumentsToSequelParam(rt, originalParams, ¶ms);
}
vector<map<string, QuickValue>> results;
vector<QuickColumnMetadata> metadata;
// Converting results into a JSI Response
try {
auto status = sqliteExecute(dbName, query, ¶ms, &results, &metadata);
if(status.type == SQLiteError) {
// throw std::runtime_error(status.errorMessage);
throw jsi::JSError(rt, status.errorMessage);
// return {};
}
auto jsiResult = createSequelQueryExecutionResult(rt, status, &results, &metadata);
return jsiResult;
} catch(std::exception &e) {
throw jsi::JSError(rt, e.what());
}
});
auto executeAsync = HOSTFN("executeAsync", 3)
{
if (count < 3)
{
throw jsi::JSError(rt, "[react-native-quick-sqlite][executeAsync] Incorrect arguments for executeAsync");
}
const string dbName = args[0].asString(rt).utf8(rt);
const string query = args[1].asString(rt).utf8(rt);
const jsi::Value &originalParams = args[2];
// 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, query, params = make_shared<vector<QuickValue>>(params), resolve, reject]()
{
try
{
vector<map<string, QuickValue>> results;
vector<QuickColumnMetadata> metadata;
auto status = sqliteExecute(dbName, 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());
});
}
};
pool->queueWork(task);
return {};
}));
return promise;
});
// Execute a batch of SQL queries in a transaction
// Parameters can be: [[sql: string, arguments: any[] | arguments: any[][] ]]
auto executeBatch = HOSTFN("executeBatch", 2)
{
if (sizeof(args) < 2)
{
throw jsi::JSError(rt, "[react-native-quick-sqlite][executeBatch] - Incorrect parameter count");
}
const jsi::Value ¶ms = args[1];
if (params.isNull() || params.isUndefined())
{
throw jsi::JSError(rt, "[react-native-quick-sqlite][executeBatch] - An array of SQL commands or parameters is needed");
}
const string dbName = args[0].asString(rt).utf8(rt);
const jsi::Array &batchParams = params.asObject(rt).asArray(rt);
vector<QuickQueryArguments> commands;
jsiBatchParametersToQuickArguments(rt, batchParams, &commands);
auto batchResult = sqliteExecuteBatch(dbName, &commands);
if (batchResult.type == SQLiteOk)
{
auto res = jsi::Object(rt);
res.setProperty(rt, "rowsAffected", jsi::Value(batchResult.affectedRows));
return move(res);
}
else
{
throw jsi::JSError(rt, batchResult.message);
}
});
auto executeBatchAsync = HOSTFN("executeBatchAsync", 2)
{
if (sizeof(args) < 2)
{
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);
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]()
{
try
{
// Inside the new worker thread, we can now call sqlite operations
auto batchResult = sqliteExecuteBatch(dbName, 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());
});
}
};
pool->queueWork(task);
return {};
}));
return promise;
});
auto loadFile = HOSTFN("loadFile", 2)
{
const string dbName = args[0].asString(rt).utf8(rt);
const string sqlFileName = args[1].asString(rt).utf8(rt);
const auto importResult = importSQLFile(dbName, sqlFileName);
if (importResult.type == SQLiteOk)
{
auto res = jsi::Object(rt);
res.setProperty(rt, "rowsAffected", jsi::Value(importResult.affectedRows));
res.setProperty(rt, "commands", jsi::Value(importResult.commands));
return move(res);
}
else
{
throw jsi::JSError(rt, "[react-native-quick-sqlite][loadFile] Could not open file");
}
});
// Load SQL File from disk in another thread
auto loadFileAsync = HOSTFN("loadFileAsync", 2)
{
if (sizeof(args) < 2)
{
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);
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]()
{
try
{
const auto importResult = importSQLFile(dbName, 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);
});
}
};
pool->queueWork(task);
return {};
}));
return promise;
});
jsi::Object module = jsi::Object(rt);
module.setProperty(rt, "open", move(open));
module.setProperty(rt, "close", move(close));
module.setProperty(rt, "attach", move(attach));
module.setProperty(rt, "detach", move(detach));
module.setProperty(rt, "delete", move(remove));
module.setProperty(rt, "execute", move(execute));
module.setProperty(rt, "executeAsync", move(executeAsync));
module.setProperty(rt, "executeBatch", move(executeBatch));
module.setProperty(rt, "executeBatchAsync", move(executeBatchAsync));
module.setProperty(rt, "loadFile", move(loadFile));
module.setProperty(rt, "loadFileAsync", move(loadFileAsync));
rt.global().setProperty(rt, "__QuickSQLiteProxy", move(module));
}
}