diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index d4a13b2f..dc552224 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -393,24 +393,23 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query, sqlite3 *db = dbMap[name]; sqlite3_stmt *statement; - const char *errorMessage; + const char *errorMessage = nullptr; const char *remainingStatement = nullptr; - - bool isFailed = false; - int step_result, current_column, column_count, column_type; + bool has_failed = false; + int status, current_column, column_count, column_type; std::string column_name, column_declared_type; std::vector column_names; std::vector> rows; std::vector row; do { - const char *queryStr = + const char *query_str = remainingStatement == nullptr ? query.c_str() : remainingStatement; - int statementStatus = - sqlite3_prepare_v2(db, queryStr, -1, &statement, &remainingStatement); + status = + sqlite3_prepare_v2(db, query_str, -1, &statement, &remainingStatement); - if (statementStatus != SQLITE_OK) { + if (status != SQLITE_OK) { errorMessage = sqlite3_errmsg(db); return {.type = SQLiteError, .message = @@ -418,24 +417,31 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query, .affectedRows = 0}; } + // The statement did not fail to parse but there is nothing to do, just + // skip to the end + if (statement == nullptr) { + continue; + } + if (params != nullptr && !params->empty()) { opsqlite_bind_statement(statement, params); } column_count = sqlite3_column_count(statement); - bool is_consuming = true; + bool is_consuming_rows = true; + double double_value; + const char *string_value; + // Do a first pass to get the column names for (int i = 0; i < column_count; i++) { column_name = sqlite3_column_name(statement, i); column_names.push_back(column_name); } - double double_value; - const char *string_value; - while (is_consuming) { - step_result = sqlite3_step(statement); + while (is_consuming_rows) { + status = sqlite3_step(statement); - switch (step_result) { + switch (status) { case SQLITE_ROW: current_column = 0; row = std::vector(); @@ -488,20 +494,20 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query, break; case SQLITE_DONE: - is_consuming = false; + is_consuming_rows = false; break; default: - isFailed = true; - is_consuming = false; + has_failed = true; + is_consuming_rows = false; } } sqlite3_finalize(statement); } while (remainingStatement != nullptr && - strcmp(remainingStatement, "") != 0 && !isFailed); + strcmp(remainingStatement, "") != 0 && !has_failed); - if (isFailed) { + if (has_failed) { const char *message = sqlite3_errmsg(db); return {.type = SQLiteError, .message = diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 6aa50ff3..9e454d43 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -10,7 +10,7 @@ PODS: - hermes-engine (0.74.0): - hermes-engine/Pre-built (= 0.74.0) - hermes-engine/Pre-built (0.74.0) - - op-sqlite (8.0.0): + - op-sqlite (8.0.2): - React - React-callinvoker - React-Core @@ -1369,7 +1369,7 @@ SPEC CHECKSUMS: GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4 glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2 hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3 - op-sqlite: d34b2d2f64871a4a626b45af824ef8f6a10cd3d6 + op-sqlite: 63af78e9033946d98491201c2c21229891a03b64 RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47 RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5 diff --git a/example/src/tests/queries.spec.ts b/example/src/tests/queries.spec.ts index d2a5d8c1..6977a1fc 100644 --- a/example/src/tests/queries.spec.ts +++ b/example/src/tests/queries.spec.ts @@ -654,5 +654,12 @@ export function queriesTests() { {name: 'test', content: 'test content'}, ]); }); + + it('Various queries', async () => { + await db.execute('SELECT 1 '); + await db.execute('SELECT 1 '); + await db.execute('SELECT 1; ', []); + await db.execute('SELECT ?; ', [1]); + }); }); }