From d9bee65a7cda278d88efbb7813b2093174099f0a Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Mon, 11 Mar 2024 21:36:08 +0100 Subject: [PATCH] Get rid of execute literal method --- cpp/bridge.cpp | 71 ++++++---------------------------------- cpp/bridge.h | 3 -- cpp/sqlbatchexecutor.cpp | 9 ++--- cpp/utils.cpp | 12 ++++--- 4 files changed, 22 insertions(+), 73 deletions(-) diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index fa2ce643..309acfb4 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -87,7 +87,8 @@ BridgeResult opsqlite_attach(std::string const &mainDBName, std::string dbPath = get_db_path(databaseToAttach, docPath); std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias; - BridgeResult result = opsqlite_execute_literal(mainDBName, statement); + BridgeResult result = + opsqlite_execute(mainDBName, statement, nullptr, nullptr, nullptr); if (result.type == SQLiteError) { return { @@ -108,7 +109,8 @@ BridgeResult opsqlite_detach(std::string const &mainDBName, * sqliteExecuteLiteral will do that. * */ std::string statement = "DETACH DATABASE " + alias; - BridgeResult result = opsqlite_execute_literal(mainDBName, statement); + BridgeResult result = + opsqlite_execute(mainDBName, statement, nullptr, nullptr, nullptr); if (result.type == SQLiteError) { return BridgeResult{ .type = SQLiteError, @@ -526,7 +528,7 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query, bool isConsuming = true; bool isFailed = false; - int result = SQLITE_OK; + int step = SQLITE_OK; do { const char *queryStr = @@ -562,13 +564,14 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query, std::string column_name, column_declared_type; while (isConsuming) { - result = sqlite3_step(statement); + step = sqlite3_step(statement); - switch (result) { + switch (step) { case SQLITE_ROW: { - if (results == NULL) { + if (results == nullptr) { break; } + std::vector row; i = 0; @@ -649,7 +652,7 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query, return {.type = SQLiteError, .message = - "[op-sqlite] SQLite error code: " + std::to_string(result) + + "[op-sqlite] SQLite error code: " + std::to_string(step) + ", description: " + std::string(errorMessage) + ".\nSee SQLite error codes reference: " "https://www.sqlite.org/rescode.html"}; @@ -663,60 +666,6 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query, .insertId = static_cast(latestInsertRowId)}; } -/// Executes without returning any results, Useful for performance critical -/// operations -BridgeResult opsqlite_execute_literal(std::string const &dbName, - std::string const &query) { - check_db_open(dbName); - - sqlite3 *db = dbMap[dbName]; - sqlite3_stmt *statement; - - int statementStatus = - sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL); - - if (statementStatus != SQLITE_OK) { - const char *message = sqlite3_errmsg(db); - return {SQLiteError, - "[op-sqlite] SQL statement error: " + std::string(message), 0}; - } - - bool isConsuming = true; - bool isFailed = false; - - int result; - std::string column_name; - - while (isConsuming) { - result = sqlite3_step(statement); - - switch (result) { - case SQLITE_ROW: - isConsuming = true; - break; - - case SQLITE_DONE: - isConsuming = false; - break; - - default: - isFailed = true; - isConsuming = false; - } - } - - sqlite3_finalize(statement); - - if (isFailed) { - const char *message = sqlite3_errmsg(db); - return {SQLiteError, - "[op-sqlite] SQL execution error: " + std::string(message), 0}; - } - - int changedRowCount = sqlite3_changes(db); - return {SQLiteOk, "", changedRowCount}; -} - void opsqlite_close_all() { for (auto const &x : dbMap) { // Interrupt will make all pending operations to fail with SQLITE_INTERRUPT diff --git a/cpp/bridge.h b/cpp/bridge.h index f81c1729..a311bdb6 100644 --- a/cpp/bridge.h +++ b/cpp/bridge.h @@ -46,9 +46,6 @@ BridgeResult opsqlite_execute_raw(std::string const &dbName, const std::vector *params, std::vector> *results); -BridgeResult opsqlite_execute_literal(std::string const &dbName, - std::string const &query); - void opsqlite_close_all(); BridgeResult opsqlite_register_update_hook(std::string const &dbName, diff --git a/cpp/sqlbatchexecutor.cpp b/cpp/sqlbatchexecutor.cpp index 4041d866..787fa01b 100644 --- a/cpp/sqlbatchexecutor.cpp +++ b/cpp/sqlbatchexecutor.cpp @@ -57,7 +57,8 @@ BatchResult sqliteExecuteBatch(std::string dbName, try { int affectedRows = 0; - opsqlite_execute_literal(dbName, "BEGIN EXCLUSIVE TRANSACTION"); + opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, + nullptr); for (int i = 0; i < commandCount; i++) { auto command = commands->at(i); // We do not provide a datastructure to receive query data because we @@ -65,7 +66,7 @@ BatchResult sqliteExecuteBatch(std::string dbName, auto result = opsqlite_execute(dbName, command.sql, command.params.get(), nullptr, nullptr); if (result.type == SQLiteError) { - opsqlite_execute_literal(dbName, "ROLLBACK"); + opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr); return BatchResult{ .type = SQLiteError, .message = result.message, @@ -74,14 +75,14 @@ BatchResult sqliteExecuteBatch(std::string dbName, affectedRows += result.affectedRows; } } - opsqlite_execute_literal(dbName, "COMMIT"); + opsqlite_execute(dbName, "COMMIT", nullptr, nullptr, nullptr); return BatchResult{ .type = SQLiteOk, .affectedRows = affectedRows, .commands = static_cast(commandCount), }; } catch (std::exception &exc) { - opsqlite_execute_literal(dbName, "ROLLBACK"); + opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr); return BatchResult{ .type = SQLiteError, .message = exc.what(), diff --git a/cpp/utils.cpp b/cpp/utils.cpp index e19411ab..c75f22bf 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -205,12 +205,14 @@ BatchResult importSQLFile(std::string dbName, std::string fileLocation) { try { int affectedRows = 0; int commands = 0; - opsqlite_execute_literal(dbName, "BEGIN EXCLUSIVE TRANSACTION"); + opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, + nullptr); while (std::getline(sqFile, line, '\n')) { if (!line.empty()) { - BridgeResult result = opsqlite_execute_literal(dbName, line); + BridgeResult result = + opsqlite_execute(dbName, line, nullptr, nullptr, nullptr); if (result.type == SQLiteError) { - opsqlite_execute_literal(dbName, "ROLLBACK"); + opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr); sqFile.close(); return {SQLiteError, result.message, 0, commands}; } else { @@ -220,11 +222,11 @@ BatchResult importSQLFile(std::string dbName, std::string fileLocation) { } } sqFile.close(); - opsqlite_execute_literal(dbName, "COMMIT"); + opsqlite_execute(dbName, "COMMIT", nullptr, nullptr, nullptr); return {SQLiteOk, "", affectedRows, commands}; } catch (...) { sqFile.close(); - opsqlite_execute_literal(dbName, "ROLLBACK"); + opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr); return {SQLiteError, "[op-sqlite][loadSQLFile] Unexpected error, transaction was " "rolledback",