From a1f5ad60035d39fc17ff772bda9052e99b5d06d5 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Mon, 30 Nov 2026 13:30:37 +0100 Subject: [PATCH 1/2] refactor: share SQLite value binding and buffer snapshots Co-authored-by: Codex --- .../cpp/SQLiteOperations.cpp | 14 +++++---- .../cpp/hybridObjects/HybridNitroSQLite.cpp | 24 +-------------- .../hybridObjects/HybridPreparedStatement.cpp | 25 +--------------- .../SQLiteBackgroundBindings.hpp | 30 +++++++++++++++++++ 4 files changed, 40 insertions(+), 53 deletions(-) create mode 100644 packages/nitro-sqlite-core/cpp/hybridObjects/SQLiteBackgroundBindings.hpp diff --git a/packages/nitro-sqlite-core/cpp/SQLiteOperations.cpp b/packages/nitro-sqlite-core/cpp/SQLiteOperations.cpp index 7ca3e5c2..5e11107f 100644 --- a/packages/nitro-sqlite-core/cpp/SQLiteOperations.cpp +++ b/packages/nitro-sqlite-core/cpp/SQLiteOperations.cpp @@ -83,10 +83,9 @@ void sqliteRemoveDb(DatabaseConnections& connections, const std::string& dbName, connections.drop(dbName, std::filesystem::path(docPath) / dbName, connectionId, otherPath); } -void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) { - for (size_t valueIndex = 0; valueIndex < values.size(); valueIndex++) { - int sqliteIndex = valueIndex + 1; - const auto& optionalValue = values.at(valueIndex); +namespace { + + void bindValue(sqlite3_stmt* statement, int sqliteIndex, const SQLiteValue& optionalValue) { int bindStatus = SQLITE_OK; if (!optionalValue || std::holds_alternative(*optionalValue)) { @@ -117,9 +116,12 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) { std::to_string(bindStatus) + "): " + sqlite3_errstr(bindStatus)); } } -} -namespace { + void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) { + for (size_t valueIndex = 0; valueIndex < values.size(); valueIndex++) { + bindValue(statement, valueIndex + 1, values[valueIndex]); + } + } struct SQLiteStatementFinalizer { void operator()(sqlite3_stmt* statement) const noexcept { diff --git a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridNitroSQLite.cpp b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridNitroSQLite.cpp index 7b7a048e..277c77c9 100644 --- a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridNitroSQLite.cpp +++ b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridNitroSQLite.cpp @@ -7,6 +7,7 @@ #include "../SQLiteMacros.hpp" #include "../SQLiteOperations.hpp" #include "HybridPreparedStatement.hpp" +#include "SQLiteBackgroundBindings.hpp" #include "SQLiteConnectionOperation.hpp" #include "SQLiteRowBatchSender.hpp" #include @@ -43,29 +44,6 @@ HybridNitroSQLite::HybridNitroSQLite(std::string databaseDirectory, const std::o } } -// Copy any JS-backed ArrayBuffers on the JS thread so they can be safely -// accessed from the connection's background worker. -static std::optional copyArrayBufferParamsForBackground(const std::optional& params) { - if (!params) { - return std::nullopt; - } - - SQLiteQueryParams copiedParams; - copiedParams.reserve(params->size()); - - for (const auto& value : *params) { - if (value && std::holds_alternative(*value)) { - const auto& buffer = std::get(*value); - const auto copiedBuffer = ArrayBuffer::copy(buffer); - copiedParams.push_back(copiedBuffer); - } else { - copiedParams.push_back(value); - } - } - - return copiedParams; -} - // Replace JS-backed ArrayBuffers inside each grouped batch command with native copies, in place. static void copyArrayBuffersForBackground(std::vector& commands) { for (auto& command : commands) { diff --git a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp index 8f55a510..a0d3bcf2 100644 --- a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp +++ b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp @@ -1,32 +1,9 @@ #include "HybridPreparedStatement.hpp" #include "SQLiteConnectionOperation.hpp" -#include +#include "SQLiteBackgroundBindings.hpp" namespace margelo::nitro::nitrosqlite { -namespace { - - std::optional copyArrayBufferParamsForBackground(const std::optional& params) { - if (!params) { - return std::nullopt; - } - - SQLiteQueryParams copiedParams; - copiedParams.reserve(params->size()); - - for (const auto& value : *params) { - if (value && std::holds_alternative(*value)) { - copiedParams.push_back(ArrayBuffer::copy(std::get(*value))); - } else { - copiedParams.push_back(value); - } - } - - return copiedParams; - } - -} // namespace - HybridPreparedStatement::HybridPreparedStatement(std::shared_ptr statement) : _statement(std::move(statement)) {} NativeQueryResult HybridPreparedStatement::execute(const std::optional& params) { diff --git a/packages/nitro-sqlite-core/cpp/hybridObjects/SQLiteBackgroundBindings.hpp b/packages/nitro-sqlite-core/cpp/hybridObjects/SQLiteBackgroundBindings.hpp new file mode 100644 index 00000000..4911fab7 --- /dev/null +++ b/packages/nitro-sqlite-core/cpp/hybridObjects/SQLiteBackgroundBindings.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "../SQLiteTypes.hpp" + +namespace margelo::nitro::nitrosqlite { + +// Copy any JS-backed ArrayBuffers on the JS thread so they can be safely +// accessed from the connection's background worker. +inline std::optional copyArrayBufferParamsForBackground(const std::optional& params) { + if (!params) { + return std::nullopt; + } + + SQLiteQueryParams copiedParams; + copiedParams.reserve(params->size()); + + for (const auto& value : *params) { + if (value && std::holds_alternative(*value)) { + const auto& buffer = std::get(*value); + const auto copiedBuffer = ArrayBuffer::copy(buffer); + copiedParams.push_back(copiedBuffer); + } else { + copiedParams.push_back(value); + } + } + + return copiedParams; +} + +} // namespace margelo::nitro::nitrosqlite From 9ea9c5f5085935fa0d85af0f57901ea56592d90b Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 2 Dec 2026 22:51:03 +0100 Subject: [PATCH 2/2] style: sort shared binding includes Co-authored-by: Codex --- .../cpp/hybridObjects/HybridPreparedStatement.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp index a0d3bcf2..c824a14b 100644 --- a/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp +++ b/packages/nitro-sqlite-core/cpp/hybridObjects/HybridPreparedStatement.cpp @@ -1,6 +1,6 @@ #include "HybridPreparedStatement.hpp" -#include "SQLiteConnectionOperation.hpp" #include "SQLiteBackgroundBindings.hpp" +#include "SQLiteConnectionOperation.hpp" namespace margelo::nitro::nitrosqlite {