|
14 | 14 | #include <vector> |
15 | 15 | #include <iostream> |
16 | 16 | #include <thread> |
| 17 | +#include <iostream> |
| 18 | +#include <fstream> |
17 | 19 |
|
18 | 20 | using namespace std; |
19 | 21 | using namespace facebook; |
@@ -177,6 +179,7 @@ void installSequel(jsi::Runtime &rt, const char *docPath) |
177 | 179 | return move(result.value); |
178 | 180 | }); |
179 | 181 |
|
| 182 | + // Execute a batch of SQL queries in a transaction |
180 | 183 | // Parameters can be: [[sql: string, arguments: any[] | arguments: any[][] ]] |
181 | 184 | auto execSQLBatch = jsi::Function::createFromHostFunction( |
182 | 185 | rt, |
@@ -258,6 +261,60 @@ void installSequel(jsi::Runtime &rt, const char *docPath) |
258 | 261 | res.setProperty(rt, "rowsAffected", jsi::Value(rowsAffected)); |
259 | 262 | return move(res); |
260 | 263 | }); |
| 264 | + |
| 265 | + // Load SQL File from disk |
| 266 | + auto loadSQLFile = jsi::Function::createFromHostFunction( |
| 267 | + rt, |
| 268 | + jsi::PropNameID::forAscii(rt, "sequel_loadSQLFile"), |
| 269 | + 2, |
| 270 | + [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value |
| 271 | + { |
| 272 | + const string dbName = args[0].asString(rt).utf8(rt); |
| 273 | + const string sqlFileName = args[1].asString(rt).utf8(rt); |
| 274 | + |
| 275 | + string line; |
| 276 | + ifstream sqFile (sqlFileName); |
| 277 | + if (sqFile.is_open()) |
| 278 | + { |
| 279 | + try { |
| 280 | + int affectedRows = 0; |
| 281 | + int commands = 0; |
| 282 | + sequel_execute_literal_update(dbName, "BEGIN TRANSACTION"); |
| 283 | + while ( std::getline (sqFile, line, '\n') ) |
| 284 | + { |
| 285 | + if (!line.empty()) { |
| 286 | + SequelLiteralUpdateResult result = sequel_execute_literal_update(dbName, line); |
| 287 | + if( result.type == SequelResultError ) { |
| 288 | + sequel_execute_literal_update(dbName, "ROLLBACK"); |
| 289 | + auto res = jsi::Object(rt); |
| 290 | + res.setProperty(rt, "status", jsi::Value(1)); |
| 291 | + res.setProperty(rt, "message", jsi::String::createFromUtf8(rt, result.message.c_str())); |
| 292 | + sqFile.close(); |
| 293 | + return move(res); |
| 294 | + } else { |
| 295 | + affectedRows += result.affectedRows; |
| 296 | + commands++; |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + sqFile.close(); |
| 301 | + sequel_execute_literal_update(dbName, "COMMIT"); |
| 302 | + auto res = jsi::Object(rt); |
| 303 | + res.setProperty(rt, "status", jsi::Value(0)); |
| 304 | + res.setProperty(rt, "rowsAffected", jsi::Value(affectedRows)); |
| 305 | + res.setProperty(rt, "commands", jsi::Value(commands)); |
| 306 | + return move(res); |
| 307 | + } catch (...) { |
| 308 | + sqFile.close(); |
| 309 | + sequel_execute_literal_update(dbName, "ROLLBACK"); |
| 310 | + jsi::detail::throwJSError(rt, "Unexpected error, transaction was rolledback"); |
| 311 | + return {}; |
| 312 | + } |
| 313 | + } else { |
| 314 | + jsi::detail::throwJSError(rt, "Unable to open file"); |
| 315 | + return {}; |
| 316 | + } |
| 317 | + }); |
261 | 318 |
|
262 | 319 | // Async Execute SQL |
263 | 320 | // auto asyncExecSQL = jsi::Function::createFromHostFunction( |
@@ -292,18 +349,18 @@ void installSequel(jsi::Runtime &rt, const char *docPath) |
292 | 349 | // return promise; |
293 | 350 | // }); |
294 | 351 |
|
295 | | - // Create final object that will be injected into the global object |
| 352 | + // Global object |
296 | 353 | jsi::Object module = jsi::Object(rt); |
297 | 354 |
|
298 | | - // Open/Close |
| 355 | + |
| 356 | + // Callable properties |
299 | 357 | module.setProperty(rt, "open", move(open)); |
300 | 358 | module.setProperty(rt, "close", move(close)); |
301 | 359 | // module.setProperty(rt, "attach", move(attach)); |
302 | 360 | module.setProperty(rt, "delete", move(remove)); |
303 | | - |
304 | 361 | module.setProperty(rt, "executeSql", move(execSQL)); |
305 | 362 | module.setProperty(rt, "executeSqlBatch", move(execSQLBatch)); |
306 | | - |
| 363 | + module.setProperty(rt, "loadSqlFile", move(loadSQLFile)); |
307 | 364 | // module.setProperty(rt, "backgroundExecuteSql", move(asyncExecSQL)); |
308 | 365 |
|
309 | 366 | rt.global().setProperty(rt, "sqlite", move(module)); |
|
0 commit comments