From b8af8b6bd80d1fe7f69a7afdbfc310163edcb9d1 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 11:05:09 +0100 Subject: [PATCH 1/6] De-dupe to variant function and correctly throw when object is passed --- cpp/utils.cpp | 510 ++++++++++++++---------------- cpp/utils.h | 4 +- example/src/tests/queries.spec.ts | 12 + src/index.ts | 4 +- 4 files changed, 250 insertions(+), 280 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index 5d67f19..97b0df9 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -13,342 +13,300 @@ namespace opsqlite { namespace jsi = facebook::jsi; -jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { - if (std::holds_alternative(value)) { - return std::get(value); - } else if (std::holds_alternative(value)) { - return jsi::Value(std::get(value)); - } else if (std::holds_alternative(value)) { - return jsi::Value(static_cast(std::get(value))); - } else if (std::holds_alternative(value)) { - return jsi::Value(std::get(value)); - } else if (std::holds_alternative(value)) { - auto str = std::get(value); - return jsi::String::createFromUtf8(rt, str); - } else if (std::holds_alternative(value)) { - auto jsBuffer = std::get(value); - jsi::Function array_buffer_ctor = +inline jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { + if (std::holds_alternative(value)) { + return std::get(value); + } else if (std::holds_alternative(value)) { + return jsi::Value(std::get(value)); + } else if (std::holds_alternative(value)) { + return jsi::Value(static_cast(std::get(value))); + } else if (std::holds_alternative(value)) { + return jsi::Value(std::get(value)); + } else if (std::holds_alternative(value)) { + auto str = std::get(value); + return jsi::String::createFromUtf8(rt, str); + } else if (std::holds_alternative(value)) { + auto jsBuffer = std::get(value); + jsi::Function array_buffer_ctor = rt.global().getPropertyAsFunction(rt, "ArrayBuffer"); - jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size) - .getObject(rt); - jsi::ArrayBuffer buf = o.getArrayBuffer(rt); - memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size); - return o; - } - - return jsi::Value::null(); + jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size) + .getObject(rt); + jsi::ArrayBuffer buf = o.getArrayBuffer(rt); + memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size); + return o; + } + + return jsi::Value::null(); } -JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) { - if (value.isNull() || value.isUndefined()) { - return JSVariant(nullptr); - } else if (value.isBool()) { - return JSVariant(value.getBool()); - } else if (value.isNumber()) { - double doubleVal = value.asNumber(); - int intVal = (int)doubleVal; - long long longVal = (long)doubleVal; - if (intVal == doubleVal) { - return JSVariant(intVal); - } else if (longVal == doubleVal) { - return JSVariant(longVal); - } else { - return JSVariant(doubleVal); - } - } else if (value.isString()) { - std::string strVal = value.asString(rt).utf8(rt); - return JSVariant(strVal); - } else if (value.isObject()) { - auto obj = value.asObject(rt); - - if (!obj.isArrayBuffer(rt)) { - throw std::invalid_argument( - "Objects returned by OP-SQLite, are C++ HostObjects and thus cannot " - "store any object, only scalar " - "properties (int, long, double, string, bool) and ArrayBuffers."); - } - - auto buffer = obj.getArrayBuffer(rt); - uint8_t *data = new uint8_t[buffer.size(rt)]; - memcpy(data, buffer.data(rt), buffer.size(rt)); - - return JSVariant(ArrayBuffer{.data = std::shared_ptr{data}, - .size = buffer.size(rt)}); - +inline JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) { + if (value.isNull() || value.isUndefined()) { + return JSVariant(nullptr); + } else if (value.isBool()) { + return JSVariant(value.getBool()); + } else if (value.isNumber()) { + double doubleVal = value.asNumber(); + int intVal = (int)doubleVal; + long long longVal = (long)doubleVal; + if (intVal == doubleVal) { + return JSVariant(intVal); + } else if (longVal == doubleVal) { + return JSVariant(longVal); } else { - throw std::invalid_argument( - "Cannot convert JSI value to C++ Variant value"); + return JSVariant(doubleVal); } + } else if (value.isString()) { + std::string strVal = value.asString(rt).utf8(rt); + return JSVariant(strVal); + } else if (value.isObject()) { + auto obj = value.asObject(rt); + + if (!obj.isArrayBuffer(rt)) { + throw std::invalid_argument( + "Object is not an ArrayBuffer, cannot bind to SQLite"); + } + + auto buffer = obj.getArrayBuffer(rt); + uint8_t *data = new uint8_t[buffer.size(rt)]; + memcpy(data, buffer.data(rt), buffer.size(rt)); + + return JSVariant(ArrayBuffer{.data = std::shared_ptr{data}, + .size = buffer.size(rt)}); + + } else { + throw std::invalid_argument( + "Cannot convert JSI value to C++ Variant value"); + } } std::vector to_string_vec(jsi::Runtime &rt, jsi::Value const &xs) { - jsi::Array values = xs.asObject(rt).asArray(rt); - std::vector res; - for (int ii = 0; ii < values.length(rt); ii++) { - std::string value = values.getValueAtIndex(rt, ii).asString(rt).utf8(rt); - res.push_back(value); - } - return res; + jsi::Array values = xs.asObject(rt).asArray(rt); + std::vector res; + for (int ii = 0; ii < values.length(rt); ii++) { + std::string value = values.getValueAtIndex(rt, ii).asString(rt).utf8(rt); + res.push_back(value); + } + return res; } std::vector to_int_vec(jsi::Runtime &rt, jsi::Value const &xs) { - jsi::Array values = xs.asObject(rt).asArray(rt); - std::vector res; - for (int ii = 0; ii < values.length(rt); ii++) { - int value = static_cast(values.getValueAtIndex(rt, ii).asNumber()); - res.push_back(value); - } - return res; + jsi::Array values = xs.asObject(rt).asArray(rt); + std::vector res; + for (int ii = 0; ii < values.length(rt); ii++) { + int value = static_cast(values.getValueAtIndex(rt, ii).asNumber()); + res.push_back(value); + } + return res; } std::vector to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs) { - std::vector res; - - if (xs.isNull() || xs.isUndefined()) { - return res; - } - - jsi::Array values = xs.asObject(rt).asArray(rt); - - for (int ii = 0; ii < values.length(rt); ii++) { - jsi::Value value = values.getValueAtIndex(rt, ii); - - if (value.isNull() || value.isUndefined()) { - res.push_back(JSVariant(nullptr)); - } else if (value.isBool()) { - res.push_back(JSVariant(value.getBool())); - } else if (value.isNumber()) { - double doubleVal = value.asNumber(); - int intVal = (int)doubleVal; - long long longVal = (long)doubleVal; - if (intVal == doubleVal) { - res.push_back(JSVariant(intVal)); - } else if (longVal == doubleVal) { - res.push_back(JSVariant(longVal)); - } else { - res.push_back(JSVariant(doubleVal)); - } - } else if (value.isString()) { - std::string strVal = value.asString(rt).utf8(rt); - res.push_back(JSVariant(strVal)); - } else if (value.isObject()) { - auto obj = value.asObject(rt); - if (obj.isArrayBuffer(rt)) { - auto buffer = obj.getArrayBuffer(rt); - size_t size = buffer.size(rt); - uint8_t *data = new uint8_t[size]; - // You cannot share raw memory between native and JS - // always copy the data - // see https://github.com/facebook/hermes/pull/419 and - // https://github.com/facebook/hermes/issues/564. - memcpy(data, buffer.data(rt), size); - - res.push_back(JSVariant(ArrayBuffer{ - .data = std::shared_ptr{data}, .size = buffer.size(rt)})); - } else { - throw std::invalid_argument( - "Unknown JSI ArrayBuffer to variant value conversion, received " - "object instead of ArrayBuffer"); - } - } else { - throw std::invalid_argument("Unknown JSI to variant value conversion"); - } - } - + std::vector res; + + if (xs.isNull() || xs.isUndefined()) { return res; + } + + jsi::Array values = xs.asObject(rt).asArray(rt); + + for (int ii = 0; ii < values.length(rt); ii++) { + jsi::Value value = values.getValueAtIndex(rt, ii); + res.push_back(toVariant(rt, value)); + } + + return res; } jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status) { - if (status.type == SQLiteError) { - throw std::invalid_argument(status.message); - } - - jsi::Object res = jsi::Object(rt); - - res.setProperty(rt, "rowsAffected", status.affectedRows); - if (status.affectedRows > 0 && status.insertId != 0) { - res.setProperty(rt, "insertId", jsi::Value(status.insertId)); - } - - size_t row_count = status.rows.size(); - auto rows = jsi::Array(rt, row_count); - - if (row_count > 0) { - for (int i = 0; i < row_count; i++) { - auto row = jsi::Array(rt, status.column_names.size()); - std::vector native_row = status.rows[i]; - for (int j = 0; j < native_row.size(); j++) { - auto value = toJSI(rt, native_row[j]); - row.setValueAtIndex(rt, j, value); - } - rows.setValueAtIndex(rt, i, row); - } - } - res.setProperty(rt, "rawRows", rows); - - size_t column_count = status.column_names.size(); - auto column_array = jsi::Array(rt, column_count); - for (int i = 0; i < column_count; i++) { - auto column = status.column_names.at(i); - column_array.setValueAtIndex(rt, i, toJSI(rt, column)); + if (status.type == SQLiteError) { + throw std::invalid_argument(status.message); + } + + jsi::Object res = jsi::Object(rt); + + res.setProperty(rt, "rowsAffected", status.affectedRows); + if (status.affectedRows > 0 && status.insertId != 0) { + res.setProperty(rt, "insertId", jsi::Value(status.insertId)); + } + + size_t row_count = status.rows.size(); + auto rows = jsi::Array(rt, row_count); + + if (row_count > 0) { + for (int i = 0; i < row_count; i++) { + auto row = jsi::Array(rt, status.column_names.size()); + std::vector native_row = status.rows[i]; + for (int j = 0; j < native_row.size(); j++) { + auto value = toJSI(rt, native_row[j]); + row.setValueAtIndex(rt, j, value); + } + rows.setValueAtIndex(rt, i, row); } - res.setProperty(rt, "columnNames", std::move(column_array)); - return res; + } + res.setProperty(rt, "rawRows", rows); + + size_t column_count = status.column_names.size(); + auto column_array = jsi::Array(rt, column_count); + for (int i = 0; i < column_count; i++) { + auto column = status.column_names.at(i); + column_array.setValueAtIndex(rt, i, toJSI(rt, column)); + } + res.setProperty(rt, "columnNames", std::move(column_array)); + return res; } jsi::Value createResult(jsi::Runtime &rt, BridgeResult status, std::vector *results, std::shared_ptr> metadata) { - jsi::Object res = jsi::Object(rt); - - res.setProperty(rt, "rowsAffected", status.affectedRows); - if (status.affectedRows > 0 && status.insertId != 0) { - res.setProperty(rt, "insertId", jsi::Value(status.insertId)); - } - - size_t rowCount = results->size(); - - auto array = jsi::Array(rt, rowCount); - for (int i = 0; i < rowCount; i++) { - auto obj = results->at(i); - array.setValueAtIndex(rt, i, - jsi::Object::createFromHostObject( - rt, std::make_shared(obj))); - } - res.setProperty(rt, "rows", std::move(array)); - - size_t column_count = metadata->size(); - auto column_array = jsi::Array(rt, column_count); - for (int i = 0; i < column_count; i++) { - auto column = metadata->at(i); - column_array.setValueAtIndex( - rt, i, - jsi::Object::createFromHostObject( - rt, std::make_shared(column))); - } - res.setProperty(rt, "metadata", std::move(column_array)); - - return std::move(res); + jsi::Object res = jsi::Object(rt); + + res.setProperty(rt, "rowsAffected", status.affectedRows); + if (status.affectedRows > 0 && status.insertId != 0) { + res.setProperty(rt, "insertId", jsi::Value(status.insertId)); + } + + size_t rowCount = results->size(); + + auto array = jsi::Array(rt, rowCount); + for (int i = 0; i < rowCount; i++) { + auto obj = results->at(i); + array.setValueAtIndex(rt, i, + jsi::Object::createFromHostObject( + rt, std::make_shared(obj))); + } + res.setProperty(rt, "rows", std::move(array)); + + size_t column_count = metadata->size(); + auto column_array = jsi::Array(rt, column_count); + for (int i = 0; i < column_count; i++) { + auto column = metadata->at(i); + column_array.setValueAtIndex( + rt, i, + jsi::Object::createFromHostObject( + rt, std::make_shared(column))); + } + res.setProperty(rt, "metadata", std::move(column_array)); + + return std::move(res); } jsi::Value create_raw_result(jsi::Runtime &rt, BridgeResult status, const std::vector> *results) { - size_t row_count = results->size(); - jsi::Array res = jsi::Array(rt, row_count); - for (int i = 0; i < row_count; i++) { - auto row = results->at(i); - auto array = jsi::Array(rt, row.size()); - for (int j = 0; j < row.size(); j++) { - array.setValueAtIndex(rt, j, toJSI(rt, row[j])); - } - res.setValueAtIndex(rt, i, array); + size_t row_count = results->size(); + jsi::Array res = jsi::Array(rt, row_count); + for (int i = 0; i < row_count; i++) { + auto row = results->at(i); + auto array = jsi::Array(rt, row.size()); + for (int j = 0; j < row.size(); j++) { + array.setValueAtIndex(rt, j, toJSI(rt, row[j])); } - return res; + res.setValueAtIndex(rt, i, array); + } + return res; } void to_batch_arguments(jsi::Runtime &rt, jsi::Array const &batchParams, std::vector *commands) { - for (int i = 0; i < batchParams.length(rt); i++) { - const jsi::Array &command = + for (int i = 0; i < batchParams.length(rt); i++) { + const jsi::Array &command = batchParams.getValueAtIndex(rt, i).asObject(rt).asArray(rt); - if (command.length(rt) == 0) { - continue; - } - - const std::string query = + if (command.length(rt) == 0) { + continue; + } + + const std::string query = command.getValueAtIndex(rt, 0).asString(rt).utf8(rt); - const jsi::Value &commandParams = command.length(rt) > 1 - ? command.getValueAtIndex(rt, 1) - : jsi::Value::undefined(); - if (!commandParams.isUndefined() && - commandParams.asObject(rt).isArray(rt) && - commandParams.asObject(rt).asArray(rt).length(rt) > 0 && - commandParams.asObject(rt) + const jsi::Value &commandParams = command.length(rt) > 1 + ? command.getValueAtIndex(rt, 1) + : jsi::Value::undefined(); + if (!commandParams.isUndefined() && + commandParams.asObject(rt).isArray(rt) && + commandParams.asObject(rt).asArray(rt).length(rt) > 0 && + commandParams.asObject(rt) .asArray(rt) .getValueAtIndex(rt, 0) .isObject()) { - // This arguments is an array of arrays, like a batch update of a single - // sql command. - const jsi::Array &batchUpdateParams = - commandParams.asObject(rt).asArray(rt); - for (int x = 0; x < batchUpdateParams.length(rt); x++) { - const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); - auto params = - std::make_shared>(to_variant_vec(rt, p)); - commands->push_back({query, params}); - } - } else { - auto params = std::make_shared>( - to_variant_vec(rt, commandParams)); - commands->push_back({query, params}); - } + // This arguments is an array of arrays, like a batch update of a single + // sql command. + const jsi::Array &batchUpdateParams = + commandParams.asObject(rt).asArray(rt); + for (int x = 0; x < batchUpdateParams.length(rt); x++) { + const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); + auto params = + std::make_shared>(to_variant_vec(rt, p)); + commands->push_back({query, params}); + } + } else { + auto params = std::make_shared>( + to_variant_vec(rt, commandParams)); + commands->push_back({query, params}); } + } } #ifndef OP_SQLITE_USE_LIBSQL BatchResult importSQLFile(std::string dbName, std::string fileLocation) { - std::string line; - std::ifstream sqFile(fileLocation); - if (sqFile.is_open()) { - try { - int affectedRows = 0; - int commands = 0; - opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr); - while (std::getline(sqFile, line, '\n')) { - if (!line.empty()) { - BridgeResult result = opsqlite_execute(dbName, line, nullptr); - if (result.type == SQLiteError) { - opsqlite_execute(dbName, "ROLLBACK", nullptr); - sqFile.close(); - return {SQLiteError, result.message, 0, commands}; - } else { - affectedRows += result.affectedRows; - commands++; - } - } - } - sqFile.close(); - opsqlite_execute(dbName, "COMMIT", nullptr); - return {SQLiteOk, "", affectedRows, commands}; - } catch (...) { - sqFile.close(); + std::string line; + std::ifstream sqFile(fileLocation); + if (sqFile.is_open()) { + try { + int affectedRows = 0; + int commands = 0; + opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr); + while (std::getline(sqFile, line, '\n')) { + if (!line.empty()) { + BridgeResult result = opsqlite_execute(dbName, line, nullptr); + if (result.type == SQLiteError) { opsqlite_execute(dbName, "ROLLBACK", nullptr); - return {SQLiteError, - "[op-sqlite][loadSQLFile] Unexpected error, transaction was " - "rolledback", - 0, 0}; + sqFile.close(); + return {SQLiteError, result.message, 0, commands}; + } else { + affectedRows += result.affectedRows; + commands++; + } } - } else { - return {SQLiteError, "[op-sqlite][loadSQLFile] Could not open file", 0, 0}; + } + sqFile.close(); + opsqlite_execute(dbName, "COMMIT", nullptr); + return {SQLiteOk, "", affectedRows, commands}; + } catch (...) { + sqFile.close(); + opsqlite_execute(dbName, "ROLLBACK", nullptr); + return {SQLiteError, + "[op-sqlite][loadSQLFile] Unexpected error, transaction was " + "rolledback", + 0, 0}; } + } else { + return {SQLiteError, "[op-sqlite][loadSQLFile] Could not open file", 0, 0}; + } } #endif bool folder_exists(const std::string &foldername) { - struct stat buffer; - return (stat(foldername.c_str(), &buffer) == 0); + struct stat buffer; + return (stat(foldername.c_str(), &buffer) == 0); } bool file_exists(const std::string &path) { - struct stat buffer; - return (stat(path.c_str(), &buffer) == 0); + struct stat buffer; + return (stat(path.c_str(), &buffer) == 0); } int mkdir(std::string const &path) { - std::filesystem::create_directories(path); - return 0; + std::filesystem::create_directories(path); + return 0; } -std::vector parse_string_list(const std::string& str) { - std::vector result; - std::istringstream stream(str); - std::string token; - while (std::getline(stream, token, ',')) { - result.push_back(token); - } - return result; +std::vector parse_string_list(const std::string &str) { + std::vector result; + std::istringstream stream(str); + std::string token; + while (std::getline(stream, token, ',')) { + result.push_back(token); + } + return result; } } // namespace opsqlite diff --git a/cpp/utils.h b/cpp/utils.h index 575954b..0693399 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -15,9 +15,9 @@ namespace opsqlite { namespace jsi = facebook::jsi; -jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value); +inline jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value); -JSVariant toVariant(jsi::Runtime &rt, jsi::Value const &value); +inline JSVariant toVariant(jsi::Runtime &rt, jsi::Value const &value); std::vector to_string_vec(jsi::Runtime &rt, jsi::Value const &xs); diff --git a/example/src/tests/queries.spec.ts b/example/src/tests/queries.spec.ts index 845ed2b..6885927 100644 --- a/example/src/tests/queries.spec.ts +++ b/example/src/tests/queries.spec.ts @@ -67,6 +67,18 @@ export function queriesTests() { // }); // } + it('Trying to pass object as param should throw', async () => { + try { + // @ts-ignore + await db.execute('SELECT ?', [{foo: 'bar'}]); + } catch (e: any) { + console.log(e); + expect(e.message).to.include( + 'Exception in HostFunction: Object is not an ArrayBuffer, cannot bind to SQLite', + ); + } + }); + it('executeSync', () => { const res = db.executeSync('SELECT 1'); expect(res.rowsAffected).to.equal(0); diff --git a/src/index.ts b/src/index.ts index 42be8df..ca1ed8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -147,8 +147,8 @@ export type DB = { ) => void; detach: (mainDbName: string, alias: string) => void; transaction: (fn: (tx: Transaction) => Promise) => Promise; - executeSync: (query: string, params?: any[]) => QueryResult; - execute: (query: string, params?: any[]) => Promise; + executeSync: (query: string, params?: Scalar[]) => QueryResult; + execute: (query: string, params?: Scalar[]) => Promise; executeWithHostObjects: ( query: string, params?: any[] From 5b2b407a29bb092e800777e3a33d786bb9e77856 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 11:10:45 +0100 Subject: [PATCH 2/6] Use emplace back for utils --- cpp/utils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index 97b0df9..d7a41e1 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -84,7 +84,7 @@ std::vector to_string_vec(jsi::Runtime &rt, jsi::Value const &xs) { std::vector res; for (int ii = 0; ii < values.length(rt); ii++) { std::string value = values.getValueAtIndex(rt, ii).asString(rt).utf8(rt); - res.push_back(value); + res.emplace_back(value); } return res; } @@ -94,7 +94,7 @@ std::vector to_int_vec(jsi::Runtime &rt, jsi::Value const &xs) { std::vector res; for (int ii = 0; ii < values.length(rt); ii++) { int value = static_cast(values.getValueAtIndex(rt, ii).asNumber()); - res.push_back(value); + res.emplace_back(value); } return res; } @@ -110,7 +110,7 @@ std::vector to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs) { for (int ii = 0; ii < values.length(rt); ii++) { jsi::Value value = values.getValueAtIndex(rt, ii); - res.push_back(toVariant(rt, value)); + res.emplace_back(toVariant(rt, value)); } return res; @@ -235,12 +235,12 @@ void to_batch_arguments(jsi::Runtime &rt, jsi::Array const &batchParams, const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); auto params = std::make_shared>(to_variant_vec(rt, p)); - commands->push_back({query, params}); + commands->emplace_back(query, params); } } else { auto params = std::make_shared>( to_variant_vec(rt, commandParams)); - commands->push_back({query, params}); + commands->emplace_back(query, params); } } } @@ -304,7 +304,7 @@ std::vector parse_string_list(const std::string &str) { std::istringstream stream(str); std::string token; while (std::getline(stream, token, ',')) { - result.push_back(token); + result.emplace_back(token); } return result; } From 720c0d31c82cf3225de4d228ce81234c6a1549f8 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 11:12:29 +0100 Subject: [PATCH 3/6] Rename create_result --- cpp/DBHostObject.cpp | 4 ++-- cpp/PreparedStatementHostObject.cpp | 2 +- cpp/utils.cpp | 6 +++--- cpp/utils.h | 7 ++++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cpp/DBHostObject.cpp b/cpp/DBHostObject.cpp index 66b5b0b..5438923 100644 --- a/cpp/DBHostObject.cpp +++ b/cpp/DBHostObject.cpp @@ -48,7 +48,7 @@ void DBHostObject::flush_pending_reactive_queries( [this, results = std::make_shared>(results), callback = query->callback, metadata, status = std::move(status)] { - auto jsiResult = createResult(rt, status, results.get(), metadata); + auto jsiResult = create_result(rt, status, results.get(), metadata); callback->asObject(rt).asFunction(rt).call(rt, jsiResult); }); } @@ -486,7 +486,7 @@ void DBHostObject::create_jsi_functions() { metadata, status = std::move(status), resolve, reject] { if (status.type == SQLiteOk) { auto jsiResult = - createResult(rt, status, results.get(), metadata); + create_result(rt, status, results.get(), metadata); resolve->asObject(rt).asFunction(rt).call( rt, std::move(jsiResult)); } else { diff --git a/cpp/PreparedStatementHostObject.cpp b/cpp/PreparedStatementHostObject.cpp index 94ead13..38a27e4 100644 --- a/cpp/PreparedStatementHostObject.cpp +++ b/cpp/PreparedStatementHostObject.cpp @@ -69,7 +69,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, metadata, resolve, reject] { if (status.type == SQLiteOk) { auto jsiResult = - createResult(rt, status, results.get(), metadata); + create_result(rt, status, results.get(), metadata); resolve->asObject(rt).asFunction(rt).call( rt, std::move(jsiResult)); } else { diff --git a/cpp/utils.cpp b/cpp/utils.cpp index d7a41e1..d36b25d 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -155,9 +155,9 @@ jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status) { } jsi::Value -createResult(jsi::Runtime &rt, BridgeResult status, - std::vector *results, - std::shared_ptr> metadata) { +create_result(jsi::Runtime &rt, BridgeResult status, + std::vector *results, + std::shared_ptr> metadata) { jsi::Object res = jsi::Object(rt); res.setProperty(rt, "rowsAffected", status.affectedRows); diff --git a/cpp/utils.h b/cpp/utils.h index 0693399..a3494e5 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -25,9 +25,10 @@ std::vector to_variant_vec(jsi::Runtime &rt, jsi::Value const &xs); std::vector to_int_vec(jsi::Runtime &rt, jsi::Value const &xs); -jsi::Value createResult(jsi::Runtime &rt, BridgeResult status, - std::vector *results, - std::shared_ptr> metadata); +jsi::Value +create_result(jsi::Runtime &rt, BridgeResult status, + std::vector *results, + std::shared_ptr> metadata); jsi::Value create_js_rows(jsi::Runtime &rt, const BridgeResult &status); From b7f12c7e453f8a33f641289ef7a9386ccc18c47e Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 11:13:37 +0100 Subject: [PATCH 4/6] Convert more types to Scalar --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index ca1ed8f..53d4184 100644 --- a/src/index.ts +++ b/src/index.ts @@ -151,7 +151,7 @@ export type DB = { execute: (query: string, params?: Scalar[]) => Promise; executeWithHostObjects: ( query: string, - params?: any[] + params?: Scalar[] ) => Promise; executeBatch: (commands: SQLBatchTuple[]) => Promise; loadFile: (location: string) => Promise; @@ -169,7 +169,7 @@ export type DB = { rollbackHook: (callback?: (() => void) | null) => void; prepareStatement: (query: string) => PreparedStatementObj; loadExtension: (path: string, entryPoint?: string) => void; - executeRaw: (query: string, params?: any[]) => Promise; + executeRaw: (query: string, params?: Scalar[]) => Promise; getDbPath: (location?: string) => string; reactiveExecute: (params: { query: string; From 27030e7b6abd95b3cae643a2229d7ac9aad50ed9 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 11:48:37 +0100 Subject: [PATCH 5/6] Do not inline to variant functions to prevent CI breakage --- cpp/utils.cpp | 4 ++-- cpp/utils.h | 4 ++-- example/ios/Podfile.lock | 2 +- op-sqlite.podspec | 8 -------- 4 files changed, 5 insertions(+), 13 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index d36b25d..a2ba13e 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -13,7 +13,7 @@ namespace opsqlite { namespace jsi = facebook::jsi; -inline jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { +jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { if (std::holds_alternative(value)) { return std::get(value); } else if (std::holds_alternative(value)) { @@ -39,7 +39,7 @@ inline jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) { return jsi::Value::null(); } -inline JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) { +JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) { if (value.isNull() || value.isUndefined()) { return JSVariant(nullptr); } else if (value.isBool()) { diff --git a/cpp/utils.h b/cpp/utils.h index a3494e5..43a048a 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -15,9 +15,9 @@ namespace opsqlite { namespace jsi = facebook::jsi; -inline jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value); +jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value); -inline JSVariant toVariant(jsi::Runtime &rt, jsi::Value const &value); +JSVariant toVariant(jsi::Runtime &rt, jsi::Value const &value); std::vector to_string_vec(jsi::Runtime &rt, jsi::Value const &xs); diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 94a0cee..7f9537c 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1785,7 +1785,7 @@ SPEC CHECKSUMS: GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4 glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a hermes-engine: 46f1ffbf0297f4298862068dd4c274d4ac17a1fd - op-sqlite: 9917e5a5747fc813e42487c0cbdd2d35eaa687bb + op-sqlite: fc35cf9d2d0ace5c5b7189e2ccadcfbb80e6e6f5 RCT-Folly: bf5c0376ffe4dd2cf438dcf86db385df9fdce648 RCTDeprecation: fde92935b3caa6cb65cbff9fbb7d3a9867ffb259 RCTRequired: 75c6cee42d21c1530a6f204ba32ff57335d19007 diff --git a/op-sqlite.podspec b/op-sqlite.podspec index 8b41167..8dc38de 100644 --- a/op-sqlite.podspec +++ b/op-sqlite.podspec @@ -87,20 +87,12 @@ Pod::Spec.new do |s| else c_sources_dir = File.join("example", "c_sources") end - if tokenizers.any? generate_tokenizers_header_file(tokenizers, File.join(c_sources_dir, "tokenizers.h")) FileUtils.cp_r(c_sources_dir, __dir__) - # puts "Current directory: #{__dir__}" - # c_sources_dir_output = Dir.glob(File.join(c_sources_dir, "**/*.{h,cpp}")) - - # puts "c_sources_dir: #{c_sources_dir_output}" - # # Add all .h and .c files from the `c_sources` directory source_files += Dir.glob(File.join("c_sources", "**/*.{h,cpp}")) - # source_files += ["../../c_sources/tokenizers.h", "../../c_sources/tokenizers.cpp"] - # puts "Source files: #{source_files}" end # Assign the collected source files to `s.source_files` From d204539bb33f78cb7ba154fbc8c76405e587580f Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sun, 1 Dec 2024 12:06:24 +0100 Subject: [PATCH 6/6] Revert emplace back on utils --- cpp/utils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index a2ba13e..76c7550 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -235,12 +235,12 @@ void to_batch_arguments(jsi::Runtime &rt, jsi::Array const &batchParams, const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x); auto params = std::make_shared>(to_variant_vec(rt, p)); - commands->emplace_back(query, params); + commands->push_back({query, params}); } } else { auto params = std::make_shared>( to_variant_vec(rt, commandParams)); - commands->emplace_back(query, params); + commands->push_back({query, params}); } } }