Skip to content

Commit 2e11e0a

Browse files
author
Oscar Franco
committed
Remove original execute_sequel method (depended on JSI) and rename base structs
1 parent a069019 commit 2e11e0a

8 files changed

Lines changed: 133 additions & 384 deletions

File tree

cpp/JSIHelper.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ void jsiQueryArgumentsToSequelParam(jsi::Runtime &rt, jsi::Value const &params,
8585
}
8686
else if (value.isBool())
8787
{
88-
int intVal = int(value.getBool());
8988
target->push_back(createBooleanQuickValue(value.getBool()));
9089
}
9190
else if (value.isNumber())
@@ -127,10 +126,10 @@ void jsiQueryArgumentsToSequelParam(jsi::Runtime &rt, jsi::Value const &params,
127126
}
128127
}
129128

130-
jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SequelOperationStatus status, vector<map<string, QuickValue>> *results)
129+
jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SQLiteOPResult status, vector<map<string, QuickValue>> *results)
131130
{
132131
jsi::Object res = jsi::Object(rt);
133-
if (status.type == SequelResultOk)
132+
if (status.type == SQLiteOk)
134133
{
135134
// res.setProperty(rt, "rows", move(rows));
136135
res.setProperty(rt, "rowsAffected", jsi::Value(status.rowsAffected));
@@ -194,4 +193,4 @@ jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SequelOperationSta
194193
}
195194

196195
return move(res);
197-
}
196+
}

cpp/JSIHelper.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,11 @@ struct QuickColumnValue
5959
*/
6060
enum ResultType
6161
{
62-
SequelResultOk,
63-
SequelResultError
62+
SQLiteOk,
63+
SQLiteError
6464
};
6565

66-
struct SequelResult
67-
{
68-
ResultType type;
69-
string message;
70-
jsi::Value value;
71-
};
72-
73-
struct SequelOperationStatus
66+
struct SQLiteOPResult
7467
{
7568
ResultType type;
7669
string errorMessage;
@@ -106,6 +99,6 @@ QuickValue createIntegerQuickValue(double value);
10699
QuickValue createInt64QuickValue(long long value);
107100
QuickValue createDoubleQuickValue(double value);
108101
QuickValue createArrayBufferQuickValue(uint8_t *arrayBufferValue, size_t arrayBufferSize);
109-
jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SequelOperationStatus status, vector<map<string, QuickValue>> *results);
102+
jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SQLiteOPResult status, vector<map<string, QuickValue>> *results);
110103

111104
#endif /* JSIHelper_h */

cpp/installer.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
7575
tempDocPath = tempDocPath + "/" + args[1].asString(rt).utf8(rt);
7676
}
7777

78-
SequelResult result = sequel_open(dbName, tempDocPath);
78+
SQLiteOPResult result = sqliteOpenDb(dbName, tempDocPath);
7979

80-
if (result.type == SequelResultError)
80+
if (result.type == SQLiteError)
8181
{
82-
return createError(rt, result.message.c_str());
82+
return createError(rt, result.errorMessage.c_str());
8383
}
8484

8585
return createOk(rt);
@@ -99,7 +99,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
9999
// string dbName = args[0].asString(rt).utf8(rt);
100100
// SequelResult result = sequel_attach(dbName);
101101
//
102-
// if (result.type == SequelResultError)
102+
// if (result.type == SQLiteError)
103103
// {
104104
// jsi::detail::throwJSError(rt, result.message.c_str());
105105
// return {};
@@ -127,14 +127,14 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
127127

128128
string dbName = args[0].asString(rt).utf8(rt);
129129

130-
SequelResult result = sequel_close(dbName);
130+
SQLiteOPResult result = sqliteCloseDb(dbName);
131131

132-
if (result.type == SequelResultError)
132+
if (result.type == SQLiteError)
133133
{
134-
return createError(rt, result.message.c_str());
134+
return createError(rt, result.errorMessage.c_str());
135135
}
136136

137-
return move(result.value);
137+
return createOk(rt);
138138
});
139139

140140
// Delete db
@@ -156,11 +156,11 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
156156

157157
string dbName = args[0].asString(rt).utf8(rt);
158158

159-
SequelResult result = sequel_remove(dbName, docPathStr);
159+
SQLiteOPResult result = sqliteRemoveDb(dbName, docPathStr);
160160

161-
if (result.type == SequelResultError)
161+
if (result.type == SQLiteError)
162162
{
163-
return createError(rt, result.message.c_str());
163+
return createError(rt, result.errorMessage.c_str());
164164
}
165165

166166
return jsi::Value::undefined();
@@ -182,7 +182,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
182182

183183
// Filling the results
184184
vector<map<string, QuickValue>> results;
185-
auto status = sequel_execute3(dbName, query, &params, &results);
185+
auto status = sqliteExecute(dbName, query, &params, &results);
186186

187187
// Converting results into a JSI Response
188188
auto jsiResult = createSequelQueryExecutionResult(rt, status, &results);
@@ -213,13 +213,14 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
213213
jsiBatchParametersToQuickArguments(rt, batchParams, &commands);
214214

215215
auto batchResult = executeBatch(dbName, &commands);
216-
if(batchResult.type == SequelResultOk)
216+
if (batchResult.type == SQLiteOk)
217217
{
218218
auto res = jsi::Object(rt);
219219
res.setProperty(rt, "status", jsi::Value(0));
220220
res.setProperty(rt, "rowsAffected", jsi::Value(batchResult.affectedRows));
221221
return move(res);
222-
} else
222+
}
223+
else
223224
{
224225
return createError(rt, batchResult.message);
225226
}
@@ -239,7 +240,8 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
239240

240241
const jsi::Value &params = args[1];
241242
const jsi::Value &callbackHolder = args[2];
242-
if(!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt)) {
243+
if (!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt))
244+
{
243245
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][asyncExecuteSqlBatch] The callback argument must be a function");
244246
return {};
245247
}
@@ -264,9 +266,9 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
264266
{
265267
// Inside the new worker thread, we can now call sqlite operations
266268
auto batchResult = executeBatch(dbName, commands.get());
267-
invoker->invokeAsync([&rt, batchResult = move(batchResult), callback]
268-
{
269-
if(batchResult.type == SequelResultOk)
269+
invoker->invokeAsync([&rt, batchResult = move(batchResult), callback]
270+
{
271+
if(batchResult.type == SQLiteOk)
270272
{
271273
auto res = jsi::Object(rt);
272274
res.setProperty(rt, "status", jsi::Value(0));
@@ -275,8 +277,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
275277
} else
276278
{
277279
callback->asObject(rt).asFunction(rt).call(rt, createError(rt, batchResult.message));
278-
}
279-
});
280+
} });
280281
}
281282
catch (std::exception &exc)
282283
{
@@ -299,7 +300,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
299300
const string sqlFileName = args[1].asString(rt).utf8(rt);
300301

301302
const auto importResult = importSQLFile(dbName, sqlFileName);
302-
if (importResult.type == SequelResultOk)
303+
if (importResult.type == SQLiteOk)
303304
{
304305
auto res = jsi::Object(rt);
305306
res.setProperty(rt, "status", jsi::Value(0));
@@ -327,7 +328,8 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
327328
}
328329

329330
const jsi::Value &callbackHolder = args[2];
330-
if(!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt)) {
331+
if (!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt))
332+
{
331333
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][asyncLoadSqlFile] The callback argument must be a function");
332334
return {};
333335
}
@@ -347,7 +349,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
347349
// Executing the callback invoke inside the JavaScript thread in order to safe build JSI objects that depends on jsi::Runtime and must be synchronized.
348350
invoker->invokeAsync([&rt, result = move(importResult), callback]
349351
{
350-
if(result.type == SequelResultOk)
352+
if(result.type == SQLiteOk)
351353
{
352354
auto res = jsi::Object(rt);
353355
res.setProperty(rt, "status", jsi::Value(0));
@@ -383,11 +385,12 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
383385
}
384386

385387
const jsi::Value &callbackHolder = args[3];
386-
if(!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt)) {
388+
if (!callbackHolder.isObject() || !callbackHolder.asObject(rt).isFunction(rt))
389+
{
387390
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][asyncExecuteSql] The callback argument must be a function");
388391
return {};
389392
}
390-
393+
391394
const string dbName = args[0].asString(rt).utf8(rt);
392395
const string query = args[1].asString(rt).utf8(rt);
393396
const jsi::Value &originalParams = args[2];
@@ -404,7 +407,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> jsCallInvoker
404407
{
405408
// Inside the new worker thread, we can now call sqlite operations
406409
vector<map<string, QuickValue>> results;
407-
auto status = sequel_execute3(dbName, query, params.get(), &results);
410+
auto status = sqliteExecute(dbName, query, params.get(), &results);
408411
invoker->invokeAsync([&rt, results = make_shared<vector<map<string, QuickValue>>>(results), status_copy = move(status), callback]
409412
{
410413
// Now, back into the JavaScript thread, we can translate the results

cpp/sqlbatchexecutor.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,41 @@ SequelBatchOperationResult executeBatch(std::string dbName, vector<QuickQueryArg
4848
if(commandCount <= 0)
4949
{
5050
return SequelBatchOperationResult {
51-
.type = SequelResultError,
51+
.type = SQLiteError,
5252
.message = "No SQL commands provided",
5353
};
5454
}
5555

5656
try
5757
{
5858
int affectedRows = 0;
59-
sequel_execute_literal_update(dbName, "BEGIN EXCLUSIVE TRANSACTION");
59+
sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
6060
for(int i = 0; i<commandCount; i++) {
6161
auto command = commands->at(i);
6262
// We do not provide a datastructure to receive query data because we don't need/want to handle this results in a batch execution
63-
auto result = sequel_execute3(dbName, command.sql, command.params.get(), NULL);
64-
if(result.type == SequelResultError)
63+
auto result = sqliteExecute(dbName, command.sql, command.params.get(), NULL);
64+
if(result.type == SQLiteError)
6565
{
6666
return SequelBatchOperationResult {
67-
.type = SequelResultError,
67+
.type = SQLiteError,
6868
.message = result.errorMessage,
6969
};
7070
} else
7171
{
7272
affectedRows += result.rowsAffected;
7373
}
7474
}
75-
sequel_execute_literal_update(dbName, "COMMIT");
75+
sqliteExecuteLiteral(dbName, "COMMIT");
7676
return SequelBatchOperationResult {
77-
.type = SequelResultOk,
77+
.type = SQLiteOk,
7878
.affectedRows = affectedRows,
7979
.commands = (int) commandCount,
8080
};
8181
} catch(std::exception &exc)
8282
{
83-
sequel_execute_literal_update(dbName, "ROLLBACK");
83+
sqliteExecuteLiteral(dbName, "ROLLBACK");
8484
return SequelBatchOperationResult {
85-
.type = SequelResultError,
85+
.type = SQLiteError,
8686
.message = exc.what(),
8787
};
8888
}

cpp/sqlfileloader.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ SequelBatchOperationResult importSQLFile(string dbName, string fileLocation)
1717
{
1818
int affectedRows = 0;
1919
int commands = 0;
20-
sequel_execute_literal_update(dbName, "BEGIN EXCLUSIVE TRANSACTION");
20+
sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
2121
while (std::getline(sqFile, line, '\n'))
2222
{
2323
if (!line.empty())
2424
{
25-
SequelLiteralUpdateResult result = sequel_execute_literal_update(dbName, line);
26-
if (result.type == SequelResultError)
25+
SequelLiteralUpdateResult result = sqliteExecuteLiteral(dbName, line);
26+
if (result.type == SQLiteError)
2727
{
28-
sequel_execute_literal_update(dbName, "ROLLBACK");
28+
sqliteExecuteLiteral(dbName, "ROLLBACK");
2929
sqFile.close();
30-
return {SequelResultError, result.message, 0, commands};
30+
return {SQLiteError, result.message, 0, commands};
3131
}
3232
else
3333
{
@@ -37,18 +37,18 @@ SequelBatchOperationResult importSQLFile(string dbName, string fileLocation)
3737
}
3838
}
3939
sqFile.close();
40-
sequel_execute_literal_update(dbName, "COMMIT");
41-
return {SequelResultOk, "", affectedRows, commands};
40+
sqliteExecuteLiteral(dbName, "COMMIT");
41+
return {SQLiteOk, "", affectedRows, commands};
4242
}
4343
catch (...)
4444
{
4545
sqFile.close();
46-
sequel_execute_literal_update(dbName, "ROLLBACK");
47-
return {SequelResultError, "[react-native-quick-sqlite][loadSQLFile] Unexpected error, transaction was rolledback", 0, 0};
46+
sqliteExecuteLiteral(dbName, "ROLLBACK");
47+
return {SQLiteError, "[react-native-quick-sqlite][loadSQLFile] Unexpected error, transaction was rolledback", 0, 0};
4848
}
4949
}
5050
else
5151
{
52-
return {SequelResultError, "[react-native-quick-sqlite][loadSQLFile] Could not open file", 0, 0};
52+
return {SQLiteError, "[react-native-quick-sqlite][loadSQLFile] Could not open file", 0, 0};
5353
}
5454
}

0 commit comments

Comments
 (0)