diff --git a/cpp/bindings.cpp b/cpp/bindings.cpp index 168998e..1c3756c 100644 --- a/cpp/bindings.cpp +++ b/cpp/bindings.cpp @@ -36,12 +36,6 @@ void clearState() { } invalidated = true; -#ifdef OP_SQLITE_USE_LIBSQL - opsqlite_libsql_close_all(); -#else - opsqlite_close_all(); -#endif - // We then join all the threads before the context gets invalidated thread_pool->restartPool(); } diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index ff6ccee..f3d190a 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -19,6 +19,41 @@ #endif namespace opsqlite { + +inline void opsqlite_bind_statement(sqlite3_stmt *statement, + const std::vector *values) { + sqlite3_clear_bindings(statement); + + size_t size = values->size(); + + for (int ii = 0; ii < size; ii++) { + int stmt_index = ii + 1; + JSVariant value = values->at(ii); + + if (std::holds_alternative(value)) { + sqlite3_bind_int(statement, stmt_index, + static_cast(std::get(value))); + } else if (std::holds_alternative(value)) { + sqlite3_bind_int(statement, stmt_index, std::get(value)); + } else if (std::holds_alternative(value)) { + sqlite3_bind_double(statement, stmt_index, + static_cast(std::get(value))); + } else if (std::holds_alternative(value)) { + sqlite3_bind_double(statement, stmt_index, std::get(value)); + } else if (std::holds_alternative(value)) { + std::string str = std::get(value); + sqlite3_bind_text(statement, stmt_index, str.c_str(), + static_cast(str.length()), SQLITE_TRANSIENT); + } else if (std::holds_alternative(value)) { + ArrayBuffer buffer = std::get(value); + sqlite3_bind_blob(statement, stmt_index, buffer.data.get(), + static_cast(buffer.size), SQLITE_TRANSIENT); + } else { + sqlite3_bind_null(statement, stmt_index); + } + } +} + /// Returns the completely formed db path, but it also creates any sub-folders /// along the way std::string opsqlite_get_db_path(std::string const &db_name, @@ -149,41 +184,6 @@ void opsqlite_remove(sqlite3 *db, std::string const &name, remove(db_path.c_str()); } -inline void opsqlite_bind_statement(sqlite3_stmt *statement, - const std::vector *values) { - // reset any existing bound values - sqlite3_clear_bindings(statement); - - size_t size = values->size(); - - for (int ii = 0; ii < size; ii++) { - int sqIndex = ii + 1; - JSVariant value = values->at(ii); - - if (std::holds_alternative(value)) { - sqlite3_bind_int(statement, sqIndex, - static_cast(std::get(value))); - } else if (std::holds_alternative(value)) { - sqlite3_bind_int(statement, sqIndex, std::get(value)); - } else if (std::holds_alternative(value)) { - sqlite3_bind_double(statement, sqIndex, - static_cast(std::get(value))); - } else if (std::holds_alternative(value)) { - sqlite3_bind_double(statement, sqIndex, std::get(value)); - } else if (std::holds_alternative(value)) { - std::string str = std::get(value); - sqlite3_bind_text(statement, sqIndex, str.c_str(), - static_cast(str.length()), SQLITE_TRANSIENT); - } else if (std::holds_alternative(value)) { - ArrayBuffer buffer = std::get(value); - sqlite3_bind_blob(statement, sqIndex, buffer.data.get(), - static_cast(buffer.size), SQLITE_TRANSIENT); - } else { - sqlite3_bind_null(statement, sqIndex); - } - } -} - BridgeResult opsqlite_execute_prepared_statement( sqlite3 *db, sqlite3_stmt *statement, std::vector *results, std::shared_ptr> &metadatas) { @@ -464,7 +464,6 @@ BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query, .column_names = std::move(column_names)}; } -/// Base execution function, returns HostObjects to the JS environment BridgeResult opsqlite_execute_host_objects( sqlite3 *db, std::string const &query, const std::vector *params, std::vector *results, @@ -763,24 +762,6 @@ opsqlite_execute_raw(sqlite3 *db, std::string const &query, .insertId = static_cast(latestInsertRowId)}; } -void opsqlite_close_all() { - // TODO figure out if there is a way to close all the connections - // IS THIS EVEN NEEDED? Each host object now has a destructor, so we can clear - // the connection there - - // for (auto const &x : dbMap) { - // // Interrupt will make all pending operations to fail with - // // SQLITE_INTERRUPT The ongoing work from threads will then fail ASAP - // sqlite3_interrupt(x.second); - // // Each DB connection can then be safely interrupted - // sqlite3_close_v2(x.second); - // } - // dbMap.clear(); - // updateCallbackMap.clear(); - // rollbackCallbackMap.clear(); - // commitCallbackMap.clear(); -} - std::string operation_to_string(int operation_type) { switch (operation_type) { case SQLITE_INSERT: diff --git a/cpp/bridge.h b/cpp/bridge.h index 2b7f2a2..3fdad84 100644 --- a/cpp/bridge.h +++ b/cpp/bridge.h @@ -61,8 +61,6 @@ BridgeResult opsqlite_execute_raw(sqlite3 *db, std::string const &query, const std::vector *params, std::vector> *results); -void opsqlite_close_all(); - BridgeResult opsqlite_register_update_hook(std::string const &dbName, const UpdateCallback &callback); BridgeResult opsqlite_deregister_update_hook(std::string const &dbName); diff --git a/cpp/libsql/bridge.h b/cpp/libsql/bridge.h index 56ee89e..5847ee5 100644 --- a/cpp/libsql/bridge.h +++ b/cpp/libsql/bridge.h @@ -55,14 +55,14 @@ BridgeResult opsqlite_libsql_detach(std::string const &mainDBName, BridgeResult opsqlite_libsql_sync(std::string const &name); -BridgeResult opsqlite_libsql_execute( - std::string const &name, std::string const &query, - const std::vector *params); +BridgeResult opsqlite_libsql_execute(std::string const &name, + std::string const &query, + const std::vector *params); - BridgeResult opsqlite_libsql_execute_with_host_objects( - std::string const &name, std::string const &query, - const std::vector *params, std::vector *results, - const std::shared_ptr>& metadatas); +BridgeResult opsqlite_libsql_execute_with_host_objects( + std::string const &name, std::string const &query, + const std::vector *params, std::vector *results, + const std::shared_ptr> &metadatas); BridgeResult opsqlite_libsql_execute_raw(std::string const &dbName, std::string const &query, @@ -73,8 +73,6 @@ BatchResult opsqlite_libsql_execute_batch(std::string const &name, std::vector *commands); -void opsqlite_libsql_close_all(); - libsql_stmt_t opsqlite_libsql_prepare_statement(std::string const &name, std::string const &query); @@ -84,6 +82,6 @@ void opsqlite_libsql_bind_statement(libsql_stmt_t stmt, BridgeResult opsqlite_libsql_execute_prepared_statement( std::string const &name, libsql_stmt_t stmt, std::vector *results, - const std::shared_ptr>& metadatas); + const std::shared_ptr> &metadatas); } // namespace opsqlite