diff --git a/bun.lock b/bun.lock index 700fb75e..87261e75 100644 --- a/bun.lock +++ b/bun.lock @@ -56,7 +56,7 @@ "expo-status-bar": "^1.12.1", "react": "19.1.1", "react-native": "0.82.1", - "react-native-nitro-modules": "0.33.5", + "react-native-nitro-modules": "*", "react-native-nitro-sqlite": "*", "react-native-safe-area-context": "^5.5.2", "react-native-screens": "^4.18.0", diff --git a/example/package.json b/example/package.json index e5fc5e6d..f8bee20b 100644 --- a/example/package.json +++ b/example/package.json @@ -22,7 +22,7 @@ "expo-status-bar": "^1.12.1", "react": "19.1.1", "react-native": "0.82.1", - "react-native-nitro-modules": "0.33.5", + "react-native-nitro-modules": "*", "react-native-nitro-sqlite": "*", "react-native-safe-area-context": "^5.5.2", "react-native-screens": "^4.18.0", diff --git a/example/src/screens/UnitTestScreen.tsx b/example/src/screens/UnitTestScreen.tsx index bf9eeb15..bf52d4f9 100644 --- a/example/src/screens/UnitTestScreen.tsx +++ b/example/src/screens/UnitTestScreen.tsx @@ -1,12 +1,11 @@ import React, { useEffect, useState } from 'react' -import { ScrollView, Text } from 'react-native' +import { FlatList, StyleSheet, Text } from 'react-native' import type { MochaTestResult } from '../tests/MochaSetup' import { runTests } from '../tests/MochaSetup' import { registerUnitTests, /* registerTypeORMUnitTests, */ } from '../tests/unit' -import { ScreenStyles } from '../styles' export function UnitTestScreen() { const [results, setResults] = useState([]) @@ -20,26 +19,33 @@ export function UnitTestScreen() { }, []) return ( - - {results.map((r, i) => { - if (r.type === 'grouping') return {r.description} + { + if (item.type === 'grouping') return {item.description} - if (r.type === 'incorrect') { + if (item.type === 'incorrect') { return ( - - 🔴 {r.description}: {r.errorMsg} + + 🔴 {item.description}: {item.errorMsg} ) } - return 🟢 {r.description} - })} - + return 🟢 {item.description} + }} + /> ) } + +const styles = StyleSheet.create({ + unitTestsScreenContainer: { + flex: 1, + }, + contentContainer: { + padding: 20, + paddingBottom: 50, + }, +}) diff --git a/example/src/tests/unit/specs/operations/execute.spec.ts b/example/src/tests/unit/specs/operations/execute.spec.ts index 4c50196f..78fd5b02 100644 --- a/example/src/tests/unit/specs/operations/execute.spec.ts +++ b/example/src/tests/unit/specs/operations/execute.spec.ts @@ -1,8 +1,4 @@ import { chance, expect, isNitroSQLiteError } from '../../common' -import { - enableSimpleNullHandling, - NITRO_SQLITE_NULL, -} from 'react-native-nitro-sqlite' import { describe, it } from '../../../MochaRNAdapter' import { testDb } from '../../../db' @@ -29,8 +25,8 @@ export default function registerExecuteUnitTests() { it('Insert with null', () => { const id = chance.integer() const name = chance.name() - const age = NITRO_SQLITE_NULL - const networth = NITRO_SQLITE_NULL + const age = null + const networth = null const res = testDb.execute( 'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)', [id, name, age, networth], @@ -53,35 +49,6 @@ export default function registerExecuteUnitTests() { ]) }) - it('Insert with null (simple null handling)', () => { - enableSimpleNullHandling(true) - - const id = chance.integer() - const name = chance.name() - const age = undefined - const networth = null - const res = testDb.execute( - 'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)', - [id, name, age, networth], - ) - - expect(res.rowsAffected).to.equal(1) - expect(res.insertId).to.equal(1) - expect(res.rows?._array).to.eql([]) - expect(res.rows?.length).to.equal(0) - expect(res.rows?.item).to.be.a('function') - - const selectRes = testDb.execute('SELECT * FROM User') - expect(selectRes.rows?._array).to.eql([ - { - id, - name, - age: null, - networth: null, - }, - ]) - }) - it('Failed insert', () => { const id = chance.integer() const name = chance.name() @@ -115,7 +82,7 @@ export default function registerExecuteUnitTests() { [id, name, age, networth], ) } catch (e: unknown) { - expect(e).to.not.equal(undefined) + expect(e).to.not.equal(null) } }) }) diff --git a/package/cpp/operations.cpp b/package/cpp/operations.cpp index ceae408e..bc339ca3 100644 --- a/package/cpp/operations.cpp +++ b/package/cpp/operations.cpp @@ -105,7 +105,7 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) { for (int valueIndex = 0; valueIndex < values.size(); valueIndex++) { int sqliteIndex = valueIndex + 1; SQLiteValue value = values.at(valueIndex); - if (std::holds_alternative(value)) { + if (std::holds_alternative(value)) { sqlite3_bind_null(statement, sqliteIndex); } else if (std::holds_alternative(value)) { sqlite3_bind_int(statement, sqliteIndex, std::get(value)); @@ -191,7 +191,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str case SQLITE_NULL: // Intentionally left blank to switch to default case default: - row[column_name] = SQLiteNullValue(true); + row[column_name] = NullType::null; break; } i++; @@ -205,7 +205,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str column_name = sqlite3_column_name(statement, i); const char* tp = sqlite3_column_decltype(statement, i); column_declared_type = mapSQLiteTypeToColumnType(tp); - auto columnMeta = SQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i); + auto columnMeta = NitroSQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i); if (!metadata) { metadata = std::make_optional(); diff --git a/package/cpp/specs/HybridNativeQueryResult.cpp b/package/cpp/specs/HybridNativeQueryResult.cpp deleted file mode 100644 index 22544687..00000000 --- a/package/cpp/specs/HybridNativeQueryResult.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "HybridNativeQueryResult.hpp" - -namespace margelo::nitro::rnnitrosqlite { - -std::optional HybridNativeQueryResult::getInsertId() { - return _result.insertId; -} - -double HybridNativeQueryResult::getRowsAffected() { - return _result.rowsAffected; -} - -SQLiteQueryResults HybridNativeQueryResult::getResults() { - return _result.results; -}; - -std::optional HybridNativeQueryResult::getMetadata() { - return _result.metadata; -} - -} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/cpp/specs/HybridNitroSQLite.cpp b/package/cpp/specs/HybridNitroSQLite.cpp index 07e0c2e6..b54b3f0b 100644 --- a/package/cpp/specs/HybridNitroSQLite.cpp +++ b/package/cpp/specs/HybridNitroSQLite.cpp @@ -1,5 +1,5 @@ #include "HybridNitroSQLite.hpp" -#include "HybridNativeQueryResult.hpp" +#include "HybridNitroSQLiteQueryResult.hpp" #include "NitroSQLiteException.hpp" #include "importSqlFile.hpp" #include "logs.hpp" @@ -50,23 +50,24 @@ void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string& sqliteDetachDb(mainDbName, alias); }; -using ExecuteQueryResult = std::shared_ptr; +using ExecuteQueryResult = std::shared_ptr; ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query, const std::optional& params) { SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params); - return std::make_shared(std::move(result)); + return std::make_shared(std::move(result)); }; -std::shared_ptr>> +std::shared_ptr>> HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query, const std::optional& params) { - return Promise>::async([=, this]() -> std::shared_ptr { - auto result = execute(dbName, query, params); - return result; - }); + return Promise>::async( + [=, this]() -> std::shared_ptr { + auto result = execute(dbName, query, params); + return result; + }); }; -BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector& batchParams) { +BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector& batchParams) { const auto commands = batchParamsToCommands(batchParams); auto result = sqliteExecuteBatch(dbName, commands); @@ -74,7 +75,7 @@ BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, cons }; std::shared_ptr> HybridNitroSQLite::executeBatchAsync(const std::string& dbName, - const std::vector& batchParams) { + const std::vector& batchParams) { return Promise::async([=, this]() -> BatchQueryResult { auto result = executeBatch(dbName, batchParams); return result; diff --git a/package/cpp/specs/HybridNitroSQLite.hpp b/package/cpp/specs/HybridNitroSQLite.hpp index 3c3fa28b..a5331964 100644 --- a/package/cpp/specs/HybridNitroSQLite.hpp +++ b/package/cpp/specs/HybridNitroSQLite.hpp @@ -1,6 +1,6 @@ #pragma once -#include "HybridNativeQueryResultSpec.hpp" +#include "HybridNitroSQLiteQueryResultSpec.hpp" #include "HybridNitroSQLiteSpec.hpp" #include "types.hpp" @@ -28,15 +28,15 @@ class HybridNitroSQLite : public HybridNitroSQLiteSpec { void detach(const std::string& mainDbName, const std::string& alias) override; - std::shared_ptr execute(const std::string& dbName, const std::string& query, - const std::optional& params) override; + std::shared_ptr execute(const std::string& dbName, const std::string& query, + const std::optional& params) override; - std::shared_ptr>> + std::shared_ptr>> executeAsync(const std::string& dbName, const std::string& query, const std::optional& params) override; - BatchQueryResult executeBatch(const std::string& dbName, const std::vector& commands) override; + BatchQueryResult executeBatch(const std::string& dbName, const std::vector& commands) override; std::shared_ptr> executeBatchAsync(const std::string& dbName, - const std::vector& commands) override; + const std::vector& commands) override; FileLoadResult loadFile(const std::string& dbName, const std::string& location) override; std::shared_ptr> loadFileAsync(const std::string& dbName, const std::string& location) override; diff --git a/package/cpp/specs/HybridNitroSQLiteQueryResult.cpp b/package/cpp/specs/HybridNitroSQLiteQueryResult.cpp new file mode 100644 index 00000000..5ccf734a --- /dev/null +++ b/package/cpp/specs/HybridNitroSQLiteQueryResult.cpp @@ -0,0 +1,49 @@ +#include "HybridNitroSQLiteQueryResult.hpp" +#include +#include +#include +#include +#include +#include + +namespace margelo::nitro::rnnitrosqlite { + +std::optional HybridNitroSQLiteQueryResult::getInsertId() { + return _result.insertId; +} + +double HybridNitroSQLiteQueryResult::getRowsAffected() { + return _result.rowsAffected; +} + +SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() { + return _result.results; +}; + +std::optional HybridNitroSQLiteQueryResult::getRows() { + if (_result.results.empty()) { + return std::nullopt; + } + + auto rows = _result.results; + + // Create the item function that returns a Promise + auto itemFunction = [rows](double idx) -> std::shared_ptr>> { + return Promise>::async([rows, idx]() -> std::optional { + const auto index = static_cast(idx); + if (index >= rows.size()) { + return std::nullopt; + } + return rows[index]; + }); + }; + + const auto length = static_cast(rows.size()); + return NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction); +} + +std::optional HybridNitroSQLiteQueryResult::getMetadata() { + return _result.metadata; +} + +} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/cpp/specs/HybridNativeQueryResult.hpp b/package/cpp/specs/HybridNitroSQLiteQueryResult.hpp similarity index 55% rename from package/cpp/specs/HybridNativeQueryResult.hpp rename to package/cpp/specs/HybridNitroSQLiteQueryResult.hpp index ae002e55..bad545d4 100644 --- a/package/cpp/specs/HybridNativeQueryResult.hpp +++ b/package/cpp/specs/HybridNitroSQLiteQueryResult.hpp @@ -1,6 +1,6 @@ #pragma once -#include "HybridNativeQueryResultSpec.hpp" +#include "HybridNitroSQLiteQueryResultSpec.hpp" #include "types.hpp" #include @@ -8,10 +8,10 @@ using namespace margelo::rnnitrosqlite; namespace margelo::nitro::rnnitrosqlite { -class HybridNativeQueryResult : public HybridNativeQueryResultSpec { +class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec { public: - HybridNativeQueryResult() : HybridObject(TAG) {} - HybridNativeQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {} + HybridNitroSQLiteQueryResult() : HybridObject(TAG) {} + HybridNitroSQLiteQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {} private: SQLiteExecuteQueryResult _result; @@ -21,6 +21,7 @@ class HybridNativeQueryResult : public HybridNativeQueryResultSpec { std::optional getInsertId() override; double getRowsAffected() override; SQLiteQueryResults getResults() override; + std::optional getRows() override; std::optional getMetadata() override; }; diff --git a/package/cpp/sqliteExecuteBatch.cpp b/package/cpp/sqliteExecuteBatch.cpp index a11ba708..7e06770f 100644 --- a/package/cpp/sqliteExecuteBatch.cpp +++ b/package/cpp/sqliteExecuteBatch.cpp @@ -8,7 +8,7 @@ namespace margelo::rnnitrosqlite { -std::vector batchParamsToCommands(const std::vector& batchParams) { +std::vector batchParamsToCommands(const std::vector& batchParams) { auto commands = std::vector(); for (auto& command : batchParams) { diff --git a/package/cpp/sqliteExecuteBatch.hpp b/package/cpp/sqliteExecuteBatch.hpp index 8fd9f7f2..4b07b89d 100644 --- a/package/cpp/sqliteExecuteBatch.hpp +++ b/package/cpp/sqliteExecuteBatch.hpp @@ -3,7 +3,7 @@ */ #pragma once -#include "NativeBatchQueryCommand.hpp" +#include "BatchQueryCommand.hpp" #include "types.hpp" using namespace facebook; @@ -20,7 +20,7 @@ struct BatchQuery { * Local Helper method to translate JSI objects BatchQuery datastructure * MUST be called in the JavaScript Thread */ -std::vector batchParamsToCommands(const std::vector& batchParams); +std::vector batchParamsToCommands(const std::vector& batchParams); /** * Execute a batch of commands in a exclusive transaction diff --git a/package/cpp/types.hpp b/package/cpp/types.hpp index 836115d3..48d1e5f6 100644 --- a/package/cpp/types.hpp +++ b/package/cpp/types.hpp @@ -1,8 +1,7 @@ #pragma once #include "ColumnType.hpp" -#include "SQLiteNullValue.hpp" -#include "SQLiteQueryColumnMetadata.hpp" +#include "NitroSQLiteQueryColumnMetadata.hpp" #include #include @@ -11,11 +10,11 @@ using namespace margelo::nitro::rnnitrosqlite; namespace margelo::rnnitrosqlite { -using SQLiteValue = std::variant, std::string, double, SQLiteNullValue>; +using SQLiteValue = std::variant, std::string, double>; using SQLiteQueryParams = std::vector; using SQLiteQueryResultRow = std::unordered_map; using SQLiteQueryResults = std::vector; -using SQLiteQueryTableMetadata = std::unordered_map; +using SQLiteQueryTableMetadata = std::unordered_map; struct SQLiteOperationResult { int rowsAffected; diff --git a/package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake b/package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake index 7db98d1c..15e44d42 100644 --- a/package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake +++ b/package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake @@ -33,9 +33,9 @@ target_sources( # Autolinking Setup ../nitrogen/generated/android/RNNitroSQLiteOnLoad.cpp # Shared Nitrogen C++ sources - ../nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp ../nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.cpp ../nitrogen/generated/shared/c++/HybridNitroSQLiteOnLoadSpec.cpp + ../nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.cpp # Android-specific Nitrogen C++ sources ../nitrogen/generated/android/c++/JHybridNitroSQLiteOnLoadSpec.cpp ) diff --git a/package/nitrogen/generated/shared/c++/NativeBatchQueryCommand.hpp b/package/nitrogen/generated/shared/c++/BatchQueryCommand.hpp similarity index 50% rename from package/nitrogen/generated/shared/c++/NativeBatchQueryCommand.hpp rename to package/nitrogen/generated/shared/c++/BatchQueryCommand.hpp index 7e60b555..3a5c5bbd 100644 --- a/package/nitrogen/generated/shared/c++/NativeBatchQueryCommand.hpp +++ b/package/nitrogen/generated/shared/c++/BatchQueryCommand.hpp @@ -1,5 +1,5 @@ /// -/// NativeBatchQueryCommand.hpp +/// BatchQueryCommand.hpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -28,12 +28,11 @@ #error NitroModules cannot be found! Are you sure you installed NitroModules properly? #endif -// Forward declaration of `SQLiteNullValue` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { struct SQLiteNullValue; } + #include +#include #include -#include "SQLiteNullValue.hpp" #include #include #include @@ -41,39 +40,39 @@ namespace margelo::nitro::rnnitrosqlite { struct SQLiteNullValue; } namespace margelo::nitro::rnnitrosqlite { /** - * A struct which can be represented as a JavaScript object (NativeBatchQueryCommand). + * A struct which can be represented as a JavaScript object (BatchQueryCommand). */ - struct NativeBatchQueryCommand final { + struct BatchQueryCommand final { public: std::string query SWIFT_PRIVATE; - std::optional, std::string, double, SQLiteNullValue>>>, std::vector, std::string, double, SQLiteNullValue>>>> params SWIFT_PRIVATE; + std::optional, std::string, double>>>, std::vector, std::string, double>>>> params SWIFT_PRIVATE; public: - NativeBatchQueryCommand() = default; - explicit NativeBatchQueryCommand(std::string query, std::optional, std::string, double, SQLiteNullValue>>>, std::vector, std::string, double, SQLiteNullValue>>>> params): query(query), params(params) {} + BatchQueryCommand() = default; + explicit BatchQueryCommand(std::string query, std::optional, std::string, double>>>, std::vector, std::string, double>>>> params): query(query), params(params) {} public: - friend bool operator==(const NativeBatchQueryCommand& lhs, const NativeBatchQueryCommand& rhs) = default; + friend bool operator==(const BatchQueryCommand& lhs, const BatchQueryCommand& rhs) = default; }; } // namespace margelo::nitro::rnnitrosqlite namespace margelo::nitro { - // C++ NativeBatchQueryCommand <> JS NativeBatchQueryCommand (object) + // C++ BatchQueryCommand <> JS BatchQueryCommand (object) template <> - struct JSIConverter final { - static inline margelo::nitro::rnnitrosqlite::NativeBatchQueryCommand fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { + struct JSIConverter final { + static inline margelo::nitro::rnnitrosqlite::BatchQueryCommand fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { jsi::Object obj = arg.asObject(runtime); - return margelo::nitro::rnnitrosqlite::NativeBatchQueryCommand( + return margelo::nitro::rnnitrosqlite::BatchQueryCommand( JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "query"))), - JSIConverter, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>, std::vector, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "params"))) + JSIConverter, std::string, double>>>, std::vector, std::string, double>>>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "params"))) ); } - static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::NativeBatchQueryCommand& arg) { + static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::BatchQueryCommand& arg) { jsi::Object obj(runtime); obj.setProperty(runtime, PropNameIDCache::get(runtime, "query"), JSIConverter::toJSI(runtime, arg.query)); - obj.setProperty(runtime, PropNameIDCache::get(runtime, "params"), JSIConverter, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>, std::vector, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>>>::toJSI(runtime, arg.params)); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "params"), JSIConverter, std::string, double>>>, std::vector, std::string, double>>>>>::toJSI(runtime, arg.params)); return obj; } static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) { @@ -85,7 +84,7 @@ namespace margelo::nitro { return false; } if (!JSIConverter::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "query")))) return false; - if (!JSIConverter, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>, std::vector, std::string, double, margelo::nitro::rnnitrosqlite::SQLiteNullValue>>>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "params")))) return false; + if (!JSIConverter, std::string, double>>>, std::vector, std::string, double>>>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "params")))) return false; return true; } }; diff --git a/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp b/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp deleted file mode 100644 index d1640959..00000000 --- a/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/// -/// HybridNativeQueryResultSpec.cpp -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/nitro -/// Copyright © Marc Rousavy @ Margelo -/// - -#include "HybridNativeQueryResultSpec.hpp" - -namespace margelo::nitro::rnnitrosqlite { - - void HybridNativeQueryResultSpec::loadHybridMethods() { - // load base methods/properties - HybridObject::loadHybridMethods(); - // load custom methods/properties - registerHybrids(this, [](Prototype& prototype) { - prototype.registerHybridGetter("rowsAffected", &HybridNativeQueryResultSpec::getRowsAffected); - prototype.registerHybridGetter("insertId", &HybridNativeQueryResultSpec::getInsertId); - prototype.registerHybridGetter("results", &HybridNativeQueryResultSpec::getResults); - prototype.registerHybridGetter("metadata", &HybridNativeQueryResultSpec::getMetadata); - }); - } - -} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.hpp b/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.hpp deleted file mode 100644 index 211ebc3d..00000000 --- a/package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/// -/// HybridNativeQueryResultSpec.hpp -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/nitro -/// Copyright © Marc Rousavy @ Margelo -/// - -#pragma once - -#if __has_include() -#include -#else -#error NitroModules cannot be found! Are you sure you installed NitroModules properly? -#endif - -// Forward declaration of `SQLiteNullValue` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { struct SQLiteNullValue; } -// Forward declaration of `SQLiteQueryColumnMetadata` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { struct SQLiteQueryColumnMetadata; } - -#include -#include -#include -#include "SQLiteNullValue.hpp" -#include -#include -#include -#include "SQLiteQueryColumnMetadata.hpp" - -namespace margelo::nitro::rnnitrosqlite { - - using namespace margelo::nitro; - - /** - * An abstract base class for `NativeQueryResult` - * Inherit this class to create instances of `HybridNativeQueryResultSpec` in C++. - * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual. - * @example - * ```cpp - * class HybridNativeQueryResult: public HybridNativeQueryResultSpec { - * public: - * HybridNativeQueryResult(...): HybridObject(TAG) { ... } - * // ... - * }; - * ``` - */ - class HybridNativeQueryResultSpec: public virtual HybridObject { - public: - // Constructor - explicit HybridNativeQueryResultSpec(): HybridObject(TAG) { } - - // Destructor - ~HybridNativeQueryResultSpec() override = default; - - public: - // Properties - virtual double getRowsAffected() = 0; - virtual std::optional getInsertId() = 0; - virtual std::vector, std::string, double, SQLiteNullValue>>> getResults() = 0; - virtual std::optional> getMetadata() = 0; - - public: - // Methods - - - protected: - // Hybrid Setup - void loadHybridMethods() override; - - protected: - // Tag for logging - static constexpr auto TAG = "NativeQueryResult"; - }; - -} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.cpp b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.cpp new file mode 100644 index 00000000..77d1a175 --- /dev/null +++ b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.cpp @@ -0,0 +1,25 @@ +/// +/// HybridNitroSQLiteQueryResultSpec.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include "HybridNitroSQLiteQueryResultSpec.hpp" + +namespace margelo::nitro::rnnitrosqlite { + + void HybridNitroSQLiteQueryResultSpec::loadHybridMethods() { + // load base methods/properties + HybridObject::loadHybridMethods(); + // load custom methods/properties + registerHybrids(this, [](Prototype& prototype) { + prototype.registerHybridGetter("rowsAffected", &HybridNitroSQLiteQueryResultSpec::getRowsAffected); + prototype.registerHybridGetter("insertId", &HybridNitroSQLiteQueryResultSpec::getInsertId); + prototype.registerHybridGetter("results", &HybridNitroSQLiteQueryResultSpec::getResults); + prototype.registerHybridGetter("rows", &HybridNitroSQLiteQueryResultSpec::getRows); + prototype.registerHybridGetter("metadata", &HybridNitroSQLiteQueryResultSpec::getMetadata); + }); + } + +} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.hpp b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.hpp new file mode 100644 index 00000000..b03eec82 --- /dev/null +++ b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteQueryResultSpec.hpp @@ -0,0 +1,77 @@ +/// +/// HybridNitroSQLiteQueryResultSpec.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif + +// Forward declaration of `NitroSQLiteQueryResultRows` to properly resolve imports. +namespace margelo::nitro::rnnitrosqlite { struct NitroSQLiteQueryResultRows; } +// Forward declaration of `NitroSQLiteQueryColumnMetadata` to properly resolve imports. +namespace margelo::nitro::rnnitrosqlite { struct NitroSQLiteQueryColumnMetadata; } + +#include +#include +#include +#include +#include +#include +#include +#include "NitroSQLiteQueryResultRows.hpp" +#include "NitroSQLiteQueryColumnMetadata.hpp" + +namespace margelo::nitro::rnnitrosqlite { + + using namespace margelo::nitro; + + /** + * An abstract base class for `NitroSQLiteQueryResult` + * Inherit this class to create instances of `HybridNitroSQLiteQueryResultSpec` in C++. + * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual. + * @example + * ```cpp + * class HybridNitroSQLiteQueryResult: public HybridNitroSQLiteQueryResultSpec { + * public: + * HybridNitroSQLiteQueryResult(...): HybridObject(TAG) { ... } + * // ... + * }; + * ``` + */ + class HybridNitroSQLiteQueryResultSpec: public virtual HybridObject { + public: + // Constructor + explicit HybridNitroSQLiteQueryResultSpec(): HybridObject(TAG) { } + + // Destructor + ~HybridNitroSQLiteQueryResultSpec() override = default; + + public: + // Properties + virtual double getRowsAffected() = 0; + virtual std::optional getInsertId() = 0; + virtual std::vector, std::string, double>>> getResults() = 0; + virtual std::optional getRows() = 0; + virtual std::optional> getMetadata() = 0; + + public: + // Methods + + + protected: + // Hybrid Setup + void loadHybridMethods() override; + + protected: + // Tag for logging + static constexpr auto TAG = "NitroSQLiteQueryResult"; + }; + +} // namespace margelo::nitro::rnnitrosqlite diff --git a/package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp index b897b82f..a773422e 100644 --- a/package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp +++ b/package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp @@ -13,28 +13,26 @@ #error NitroModules cannot be found! Are you sure you installed NitroModules properly? #endif -// Forward declaration of `HybridNativeQueryResultSpec` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { class HybridNativeQueryResultSpec; } -// Forward declaration of `SQLiteNullValue` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { struct SQLiteNullValue; } +// Forward declaration of `HybridNitroSQLiteQueryResultSpec` to properly resolve imports. +namespace margelo::nitro::rnnitrosqlite { class HybridNitroSQLiteQueryResultSpec; } // Forward declaration of `BatchQueryResult` to properly resolve imports. namespace margelo::nitro::rnnitrosqlite { struct BatchQueryResult; } -// Forward declaration of `NativeBatchQueryCommand` to properly resolve imports. -namespace margelo::nitro::rnnitrosqlite { struct NativeBatchQueryCommand; } +// Forward declaration of `BatchQueryCommand` to properly resolve imports. +namespace margelo::nitro::rnnitrosqlite { struct BatchQueryCommand; } // Forward declaration of `FileLoadResult` to properly resolve imports. namespace margelo::nitro::rnnitrosqlite { struct FileLoadResult; } #include #include #include -#include "HybridNativeQueryResultSpec.hpp" +#include "HybridNitroSQLiteQueryResultSpec.hpp" +#include #include -#include "SQLiteNullValue.hpp" #include #include #include #include "BatchQueryResult.hpp" -#include "NativeBatchQueryCommand.hpp" +#include "BatchQueryCommand.hpp" #include "FileLoadResult.hpp" namespace margelo::nitro::rnnitrosqlite { @@ -73,10 +71,10 @@ namespace margelo::nitro::rnnitrosqlite { virtual void drop(const std::string& dbName, const std::optional& location) = 0; virtual void attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias, const std::optional& location) = 0; virtual void detach(const std::string& mainDbName, const std::string& alias) = 0; - virtual std::shared_ptr execute(const std::string& dbName, const std::string& query, const std::optional, std::string, double, SQLiteNullValue>>>& params) = 0; - virtual std::shared_ptr>> executeAsync(const std::string& dbName, const std::string& query, const std::optional, std::string, double, SQLiteNullValue>>>& params) = 0; - virtual BatchQueryResult executeBatch(const std::string& dbName, const std::vector& commands) = 0; - virtual std::shared_ptr> executeBatchAsync(const std::string& dbName, const std::vector& commands) = 0; + virtual std::shared_ptr execute(const std::string& dbName, const std::string& query, const std::optional, std::string, double>>>& params) = 0; + virtual std::shared_ptr>> executeAsync(const std::string& dbName, const std::string& query, const std::optional, std::string, double>>>& params) = 0; + virtual BatchQueryResult executeBatch(const std::string& dbName, const std::vector& commands) = 0; + virtual std::shared_ptr> executeBatchAsync(const std::string& dbName, const std::vector& commands) = 0; virtual FileLoadResult loadFile(const std::string& dbName, const std::string& location) = 0; virtual std::shared_ptr> loadFileAsync(const std::string& dbName, const std::string& location) = 0; diff --git a/package/nitrogen/generated/shared/c++/SQLiteQueryColumnMetadata.hpp b/package/nitrogen/generated/shared/c++/NitroSQLiteQueryColumnMetadata.hpp similarity index 76% rename from package/nitrogen/generated/shared/c++/SQLiteQueryColumnMetadata.hpp rename to package/nitrogen/generated/shared/c++/NitroSQLiteQueryColumnMetadata.hpp index 57c6d684..20337f74 100644 --- a/package/nitrogen/generated/shared/c++/SQLiteQueryColumnMetadata.hpp +++ b/package/nitrogen/generated/shared/c++/NitroSQLiteQueryColumnMetadata.hpp @@ -1,5 +1,5 @@ /// -/// SQLiteQueryColumnMetadata.hpp +/// NitroSQLiteQueryColumnMetadata.hpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -37,38 +37,38 @@ namespace margelo::nitro::rnnitrosqlite { enum class ColumnType; } namespace margelo::nitro::rnnitrosqlite { /** - * A struct which can be represented as a JavaScript object (SQLiteQueryColumnMetadata). + * A struct which can be represented as a JavaScript object (NitroSQLiteQueryColumnMetadata). */ - struct SQLiteQueryColumnMetadata final { + struct NitroSQLiteQueryColumnMetadata final { public: std::string name SWIFT_PRIVATE; ColumnType type SWIFT_PRIVATE; double index SWIFT_PRIVATE; public: - SQLiteQueryColumnMetadata() = default; - explicit SQLiteQueryColumnMetadata(std::string name, ColumnType type, double index): name(name), type(type), index(index) {} + NitroSQLiteQueryColumnMetadata() = default; + explicit NitroSQLiteQueryColumnMetadata(std::string name, ColumnType type, double index): name(name), type(type), index(index) {} public: - friend bool operator==(const SQLiteQueryColumnMetadata& lhs, const SQLiteQueryColumnMetadata& rhs) = default; + friend bool operator==(const NitroSQLiteQueryColumnMetadata& lhs, const NitroSQLiteQueryColumnMetadata& rhs) = default; }; } // namespace margelo::nitro::rnnitrosqlite namespace margelo::nitro { - // C++ SQLiteQueryColumnMetadata <> JS SQLiteQueryColumnMetadata (object) + // C++ NitroSQLiteQueryColumnMetadata <> JS NitroSQLiteQueryColumnMetadata (object) template <> - struct JSIConverter final { - static inline margelo::nitro::rnnitrosqlite::SQLiteQueryColumnMetadata fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { + struct JSIConverter final { + static inline margelo::nitro::rnnitrosqlite::NitroSQLiteQueryColumnMetadata fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { jsi::Object obj = arg.asObject(runtime); - return margelo::nitro::rnnitrosqlite::SQLiteQueryColumnMetadata( + return margelo::nitro::rnnitrosqlite::NitroSQLiteQueryColumnMetadata( JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "name"))), JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "type"))), JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "index"))) ); } - static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::SQLiteQueryColumnMetadata& arg) { + static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::NitroSQLiteQueryColumnMetadata& arg) { jsi::Object obj(runtime); obj.setProperty(runtime, PropNameIDCache::get(runtime, "name"), JSIConverter::toJSI(runtime, arg.name)); obj.setProperty(runtime, PropNameIDCache::get(runtime, "type"), JSIConverter::toJSI(runtime, arg.type)); diff --git a/package/nitrogen/generated/shared/c++/NitroSQLiteQueryResultRows.hpp b/package/nitrogen/generated/shared/c++/NitroSQLiteQueryResultRows.hpp new file mode 100644 index 00000000..3e0c995b --- /dev/null +++ b/package/nitrogen/generated/shared/c++/NitroSQLiteQueryResultRows.hpp @@ -0,0 +1,99 @@ +/// +/// NitroSQLiteQueryResultRows.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif + + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace margelo::nitro::rnnitrosqlite { + + /** + * A struct which can be represented as a JavaScript object (NitroSQLiteQueryResultRows). + */ + struct NitroSQLiteQueryResultRows final { + public: + std::vector, std::string, double>>> _array SWIFT_PRIVATE; + double length SWIFT_PRIVATE; + std::function, std::string, double>>>>>(double /* idx */)> item SWIFT_PRIVATE; + + public: + NitroSQLiteQueryResultRows() = default; + explicit NitroSQLiteQueryResultRows(std::vector, std::string, double>>> _array, double length, std::function, std::string, double>>>>>(double /* idx */)> item): _array(_array), length(length), item(item) {} + + public: + // NitroSQLiteQueryResultRows is not equatable because these properties are not equatable: item + }; + +} // namespace margelo::nitro::rnnitrosqlite + +namespace margelo::nitro { + + // C++ NitroSQLiteQueryResultRows <> JS NitroSQLiteQueryResultRows (object) + template <> + struct JSIConverter final { + static inline margelo::nitro::rnnitrosqlite::NitroSQLiteQueryResultRows fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { + jsi::Object obj = arg.asObject(runtime); + return margelo::nitro::rnnitrosqlite::NitroSQLiteQueryResultRows( + JSIConverter, std::string, double>>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "_array"))), + JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "length"))), + JSIConverter, std::string, double>>>>>(double)>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "item"))) + ); + } + static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::NitroSQLiteQueryResultRows& arg) { + jsi::Object obj(runtime); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "_array"), JSIConverter, std::string, double>>>>::toJSI(runtime, arg._array)); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "length"), JSIConverter::toJSI(runtime, arg.length)); + obj.setProperty(runtime, PropNameIDCache::get(runtime, "item"), JSIConverter, std::string, double>>>>>(double)>>::toJSI(runtime, arg.item)); + return obj; + } + static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) { + if (!value.isObject()) { + return false; + } + jsi::Object obj = value.getObject(runtime); + if (!nitro::isPlainObject(runtime, obj)) { + return false; + } + if (!JSIConverter, std::string, double>>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "_array")))) return false; + if (!JSIConverter::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "length")))) return false; + if (!JSIConverter, std::string, double>>>>>(double)>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "item")))) return false; + return true; + } + }; + +} // namespace margelo::nitro diff --git a/package/nitrogen/generated/shared/c++/SQLiteNullValue.hpp b/package/nitrogen/generated/shared/c++/SQLiteNullValue.hpp deleted file mode 100644 index 553c187c..00000000 --- a/package/nitrogen/generated/shared/c++/SQLiteNullValue.hpp +++ /dev/null @@ -1,83 +0,0 @@ -/// -/// SQLiteNullValue.hpp -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/nitro -/// Copyright © Marc Rousavy @ Margelo -/// - -#pragma once - -#if __has_include() -#include -#else -#error NitroModules cannot be found! Are you sure you installed NitroModules properly? -#endif -#if __has_include() -#include -#else -#error NitroModules cannot be found! Are you sure you installed NitroModules properly? -#endif -#if __has_include() -#include -#else -#error NitroModules cannot be found! Are you sure you installed NitroModules properly? -#endif -#if __has_include() -#include -#else -#error NitroModules cannot be found! Are you sure you installed NitroModules properly? -#endif - - - - - -namespace margelo::nitro::rnnitrosqlite { - - /** - * A struct which can be represented as a JavaScript object (SQLiteNullValue). - */ - struct SQLiteNullValue final { - public: - bool isNitroSQLiteNull SWIFT_PRIVATE; - - public: - SQLiteNullValue() = default; - explicit SQLiteNullValue(bool isNitroSQLiteNull): isNitroSQLiteNull(isNitroSQLiteNull) {} - - public: - friend bool operator==(const SQLiteNullValue& lhs, const SQLiteNullValue& rhs) = default; - }; - -} // namespace margelo::nitro::rnnitrosqlite - -namespace margelo::nitro { - - // C++ SQLiteNullValue <> JS SQLiteNullValue (object) - template <> - struct JSIConverter final { - static inline margelo::nitro::rnnitrosqlite::SQLiteNullValue fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { - jsi::Object obj = arg.asObject(runtime); - return margelo::nitro::rnnitrosqlite::SQLiteNullValue( - JSIConverter::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isNitroSQLiteNull"))) - ); - } - static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::rnnitrosqlite::SQLiteNullValue& arg) { - jsi::Object obj(runtime); - obj.setProperty(runtime, PropNameIDCache::get(runtime, "isNitroSQLiteNull"), JSIConverter::toJSI(runtime, arg.isNitroSQLiteNull)); - return obj; - } - static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) { - if (!value.isObject()) { - return false; - } - jsi::Object obj = value.getObject(runtime); - if (!nitro::isPlainObject(runtime, obj)) { - return false; - } - if (!JSIConverter::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "isNitroSQLiteNull")))) return false; - return true; - } - }; - -} // namespace margelo::nitro diff --git a/package/src/index.ts b/package/src/index.ts index 9f350d5a..0cd0a0cc 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -21,13 +21,10 @@ export const NitroSQLite = { executeBatchAsync, } +/** NOOP on NitroSQLite versions >= 9.3.0 */ +export function enableSimpleNullHandling() {} + export { open } from './operations/session' -export { - isNitroSQLiteNull, - NITRO_SQLITE_NULL, - isSimpleNullHandlingEnabled, - enableSimpleNullHandling, -} from './nullHandling' export { default as NitroSQLiteError } from './NitroSQLiteError' export type * from './types' export { typeORMDriver } from './typeORM' diff --git a/package/src/nullHandling.ts b/package/src/nullHandling.ts deleted file mode 100644 index 5d778c95..00000000 --- a/package/src/nullHandling.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { SQLiteNullValue, SQLiteValue } from './types' -import { SQLiteQueryParamItem } from './types' - -let ENABLE_SIMPLE_NULL_HANDLING = false - -export function enableSimpleNullHandling( - shouldEnableSimpleNullHandling = true, -) { - ENABLE_SIMPLE_NULL_HANDLING = shouldEnableSimpleNullHandling -} - -export function isSimpleNullHandlingEnabled() { - return ENABLE_SIMPLE_NULL_HANDLING -} - -export const NITRO_SQLITE_NULL: SQLiteNullValue = { isNitroSQLiteNull: true } -export function isNitroSQLiteNull(value: unknown): value is SQLiteNullValue { - if ( - value !== null && - typeof value === 'object' && - 'isNitroSQLiteNull' in value - ) { - return true - } - return false -} - -export function replaceWithNativeNullValue( - value: SQLiteQueryParamItem, -): SQLiteValue { - if (value === undefined || value === null) { - return NITRO_SQLITE_NULL - } - return value -} diff --git a/package/src/operations/execute.ts b/package/src/operations/execute.ts index ce2d629c..c0ef34b1 100644 --- a/package/src/operations/execute.ts +++ b/package/src/operations/execute.ts @@ -1,13 +1,6 @@ -import { isNitroSQLiteNull, isSimpleNullHandlingEnabled } from '../nullHandling' import { HybridNitroSQLite } from '../nitro' -import { replaceWithNativeNullValue } from '../nullHandling' -import type { NativeQueryResult } from '../specs/NativeQueryResult.nitro' -import type { - QueryResult, - NativeSQLiteQueryParams, - SQLiteQueryParams, - QueryResultRow, -} from '../types' +import type { NitroSQLiteQueryResult } from '../specs/NitroSQLiteQueryResult.nitro' +import type { QueryResult, QueryResultRow, SQLiteQueryParams } from '../types' import NitroSQLiteError from '../NitroSQLiteError' export function execute( @@ -15,18 +8,9 @@ export function execute( query: string, params?: SQLiteQueryParams, ): QueryResult { - const transformedParams = isSimpleNullHandlingEnabled() - ? toNativeQueryParams(params) - : (params as NativeSQLiteQueryParams) - try { - const nativeResult = HybridNitroSQLite.execute( - dbName, - query, - transformedParams, - ) - - return buildJsQueryResult(nativeResult) + const result = HybridNitroSQLite.execute(dbName, query, params) + return buildJsQueryResult(result) } catch (error) { throw NitroSQLiteError.fromError(error) } @@ -37,55 +21,27 @@ export async function executeAsync( query: string, params?: SQLiteQueryParams, ): Promise> { - const transformedParams = isSimpleNullHandlingEnabled() - ? toNativeQueryParams(params) - : (params as NativeSQLiteQueryParams) - try { - const nativeResult = await HybridNitroSQLite.executeAsync( - dbName, - query, - transformedParams, - ) - return buildJsQueryResult(nativeResult) + const result = await HybridNitroSQLite.executeAsync(dbName, query, params) + return buildJsQueryResult(result) } catch (error) { throw NitroSQLiteError.fromError(error) } } -function toNativeQueryParams( - params: SQLiteQueryParams | undefined, -): NativeSQLiteQueryParams | undefined { - return params?.map((param) => replaceWithNativeNullValue(param)) -} - -function buildJsQueryResult({ - insertId, - rowsAffected, - results, -}: NativeQueryResult): QueryResult { - let data: Row[] = results as Row[] - - if (isSimpleNullHandlingEnabled()) { - data = results.map((row) => - Object.fromEntries( - Object.entries(row).map(([key, value]) => { - if (isNitroSQLiteNull(value)) { - return [key, null] - } - return [key, value] - }), - ), - ) as Row[] - } +function buildJsQueryResult( + result: NitroSQLiteQueryResult, +): QueryResult { + const data = result.results as Row[] return { - insertId, - rowsAffected, + ...result, + insertId: result.insertId, + rowsAffected: result.rowsAffected, rows: { _array: data, length: data.length, item: (idx: number) => data[idx], }, - } + } as QueryResult } diff --git a/package/src/operations/executeBatch.ts b/package/src/operations/executeBatch.ts index dde6501c..983e40e7 100644 --- a/package/src/operations/executeBatch.ts +++ b/package/src/operations/executeBatch.ts @@ -1,20 +1,11 @@ -import { - isSimpleNullHandlingEnabled, - replaceWithNativeNullValue, -} from '../nullHandling' import { HybridNitroSQLite } from '../nitro' import { queueOperationAsync, startOperationSync, throwIfDatabaseIsNotOpen, } from '../DatabaseQueue' -import type { - NativeSQLiteQueryParams, - BatchQueryResult, - BatchQueryCommand, - NativeBatchQueryCommand, -} from '../types' import NitroSQLiteError from '../NitroSQLiteError' +import type { BatchQueryCommand, BatchQueryResult } from '../types' export function executeBatch( dbName: string, @@ -22,13 +13,9 @@ export function executeBatch( ): BatchQueryResult { throwIfDatabaseIsNotOpen(dbName) - const transformedCommands = isSimpleNullHandlingEnabled() - ? toNativeBatchQueryCommands(commands) - : (commands as NativeBatchQueryCommand[]) - try { return startOperationSync(dbName, () => - HybridNitroSQLite.executeBatch(dbName, transformedCommands), + HybridNitroSQLite.executeBatch(dbName, commands), ) } catch (error) { throw NitroSQLiteError.fromError(error) @@ -41,36 +28,11 @@ export async function executeBatchAsync( ): Promise { throwIfDatabaseIsNotOpen(dbName) - const transformedCommands = isSimpleNullHandlingEnabled() - ? toNativeBatchQueryCommands(commands) - : (commands as NativeBatchQueryCommand[]) - return queueOperationAsync(dbName, async () => { try { - return await HybridNitroSQLite.executeBatchAsync( - dbName, - transformedCommands, - ) + return await HybridNitroSQLite.executeBatchAsync(dbName, commands) } catch (error) { throw NitroSQLiteError.fromError(error) } }) } - -function toNativeBatchQueryCommands( - commands: BatchQueryCommand[], -): NativeBatchQueryCommand[] { - return commands.map((command) => { - const transformedParams = command.params?.map((param) => { - if (Array.isArray(param)) { - return param.map((p) => replaceWithNativeNullValue(p)) - } - return replaceWithNativeNullValue(param) - }) as NativeSQLiteQueryParams | NativeSQLiteQueryParams[] - - return { - query: command.query, - params: transformedParams, - } - }) -} diff --git a/package/src/operations/session.ts b/package/src/operations/session.ts index da5c3536..391407f2 100644 --- a/package/src/operations/session.ts +++ b/package/src/operations/session.ts @@ -4,10 +4,10 @@ import type { BatchQueryCommand, NitroSQLiteConnection, NitroSQLiteConnectionOptions, - QueryResult, Transaction, SQLiteQueryParams, QueryResultRow, + QueryResult, } from '../types' import { execute, executeAsync } from './execute' import { executeBatch, executeBatchAsync } from './executeBatch' diff --git a/package/src/operations/transaction.ts b/package/src/operations/transaction.ts index ec6a6d0b..ae969b3c 100644 --- a/package/src/operations/transaction.ts +++ b/package/src/operations/transaction.ts @@ -1,8 +1,8 @@ import { queueOperationAsync, throwIfDatabaseIsNotOpen } from '../DatabaseQueue' import type { - QueryResult, Transaction, SQLiteQueryParams, + QueryResult, QueryResultRow, } from '../types' import { execute, executeAsync } from './execute' diff --git a/package/src/specs/NitroSQLite.nitro.ts b/package/src/specs/NitroSQLite.nitro.ts index fdeb72dd..65c75733 100644 --- a/package/src/specs/NitroSQLite.nitro.ts +++ b/package/src/specs/NitroSQLite.nitro.ts @@ -1,11 +1,11 @@ import type { HybridObject } from 'react-native-nitro-modules' import type { + BatchQueryCommand, BatchQueryResult, FileLoadResult, - NativeSQLiteQueryParams, - NativeBatchQueryCommand, + SQLiteQueryParams, } from '../types' -import type { NativeQueryResult } from './NativeQueryResult.nitro' +import type { NitroSQLiteQueryResult } from './NitroSQLiteQueryResult.nitro' export interface NitroSQLite extends HybridObject<{ ios: 'c++'; android: 'c++' }> { @@ -22,20 +22,17 @@ export interface NitroSQLite execute( dbName: string, query: string, - params?: NativeSQLiteQueryParams, - ): NativeQueryResult + params?: SQLiteQueryParams, + ): NitroSQLiteQueryResult executeAsync( dbName: string, query: string, - params?: NativeSQLiteQueryParams, - ): Promise - executeBatch( - dbName: string, - commands: NativeBatchQueryCommand[], - ): BatchQueryResult + params?: SQLiteQueryParams, + ): Promise + executeBatch(dbName: string, commands: BatchQueryCommand[]): BatchQueryResult executeBatchAsync( dbName: string, - commands: NativeBatchQueryCommand[], + commands: BatchQueryCommand[], ): Promise loadFile(dbName: string, location: string): FileLoadResult loadFileAsync(dbName: string, location: string): Promise diff --git a/package/src/specs/NativeQueryResult.nitro.ts b/package/src/specs/NitroSQLiteQueryResult.nitro.ts similarity index 50% rename from package/src/specs/NativeQueryResult.nitro.ts rename to package/src/specs/NitroSQLiteQueryResult.nitro.ts index 49e1d2b4..860dff83 100644 --- a/package/src/specs/NativeQueryResult.nitro.ts +++ b/package/src/specs/NitroSQLiteQueryResult.nitro.ts @@ -9,36 +9,51 @@ import type { ColumnType, SQLiteValue } from '../types' * rows: if status is undefined or 0 this object will contain the query results * } * - * @interface NativeQueryResult + * @interface QueryResult */ -export interface NativeQueryResult +export interface NitroSQLiteQueryResult extends HybridObject<{ ios: 'c++'; android: 'c++' }> { readonly rowsAffected: number readonly insertId?: number /** Query results */ - readonly results: NativeSQLiteQueryResults + readonly results: Record[] + + /** Query results in a row format for TypeORM compatibility */ + readonly rows?: NitroSQLiteQueryResultRows + /** Table metadata */ - readonly metadata?: Record + readonly metadata?: Record } -/** - * Table metadata - * Describes some information about the table and it's columns fetched by the query - * The index is the name of the column - */ -// TODO: Investigate why this doesn't work with nitrogen -// export type SQLiteQueryResultRow = Record -// export type SQLiteQueryResults = SQLiteQueryResultRow[] -export type NativeSQLiteQueryResults = Record[] - -// TODO: Investigate why this doesn't work with nitrogen -// export type SQLiteQueryTableMetadata = Record -export interface SQLiteQueryColumnMetadata { +// TODO: Investigate why this cannot be represented in Nitro +// export type NitroQueryResultRow = { +// [key: string]: SQLiteValue +// } + +// type NitroQueryResultRow = Record + +export type NitroSQLiteQueryResultRows< + Row extends Record = Record, +> = { + /** Raw array with all dataset */ + _array: Row[] + /** The lengh of the dataset */ + length: number + /** A convenience function to acess the index based the row object + * @param idx the row index + * @returns the row structure identified by column names + */ + item: (idx: number) => Row | undefined +} + +export type NitroSQLiteQueryColumnMetadata = { /** The name used for this column for this result set */ name: string + /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. "UNKNOWN" for dynamic values, like function returned ones. */ type: ColumnType + /** The index for this column for this result set */ index: number } diff --git a/package/src/typeORM.ts b/package/src/typeORM.ts index 20b30b8c..83e94bf1 100644 --- a/package/src/typeORM.ts +++ b/package/src/typeORM.ts @@ -6,10 +6,10 @@ // |_| |_| |_| |______\____/|_| \_\_| |_| /_/ \_\_| |_____| import type { - QueryResult, Transaction, SQLiteQueryParams, QueryResultRow, + QueryResult, } from './types' import * as Operations from './operations/session' diff --git a/package/src/types.ts b/package/src/types.ts index dd11c308..09e652eb 100644 --- a/package/src/types.ts +++ b/package/src/types.ts @@ -1,3 +1,9 @@ +import type { + NitroSQLiteQueryColumnMetadata, + NitroSQLiteQueryResult, + NitroSQLiteQueryResultRows, +} from './specs/NitroSQLiteQueryResult.nitro' + export interface NitroSQLiteConnectionOptions { name: string location?: string @@ -28,48 +34,30 @@ export enum ColumnType { NULL_VALUE, } -// Passing null/undefined in array types is not possible, so we us a special struct as a workaround. -export type SQLiteNullValue = { - isNitroSQLiteNull: true -} -export type SQLiteValue = - | boolean - | number - | string - | ArrayBuffer - | SQLiteNullValue +export type SQLiteValue = boolean | number | string | ArrayBuffer | null -/** Used internally to transform the query params into a native format without nullish values */ -export type NativeSQLiteQueryParams = SQLiteValue[] +export type SQLiteQueryParams = SQLiteValue[] -/** - * Represents a value that can be stored in a SQLite database - */ -export type SQLiteQueryParamItem = SQLiteValue | null | undefined -export type SQLiteQueryParams = SQLiteQueryParamItem[] - -export type QueryResultRowItem = SQLiteValue | undefined -export type QueryResultRow = Record -export interface QueryResult { - readonly insertId?: number - readonly rowsAffected: number - - readonly rows?: { - /** Raw array with all dataset */ - _array: Row[] - /** The lengh of the dataset */ - length: number - /** A convenience function to acess the index based the row object - * @param idx the row index - * @returns the row structure identified by column names - */ - item: (idx: number) => Row | undefined +export type QueryResultRow = Record + +export type QueryResult = + NitroSQLiteQueryResult & { + readonly rowsAffected: number + readonly insertId?: number + + /** Query results */ + readonly results: Row[] + + /** Query results in a row format for TypeORM compatibility */ + readonly rows?: NitroSQLiteQueryResultRows + + /** Table metadata */ + readonly metadata?: Record } -} export type ExecuteQuery = ( query: string, - params?: SQLiteQueryParams, + params?: SQLiteValue[], ) => QueryResult export type ExecuteAsyncQuery = ( @@ -78,8 +66,8 @@ export type ExecuteAsyncQuery = ( ) => Promise> export interface Transaction { - commit(): QueryResult - rollback(): QueryResult + commit(): NitroSQLiteQueryResult + rollback(): NitroSQLiteQueryResult execute: ExecuteQuery executeAsync: ExecuteAsyncQuery } @@ -95,14 +83,6 @@ export interface BatchQueryCommand { params?: SQLiteQueryParams | SQLiteQueryParams[] } -/** - * Used internally to transform the batch query commands into a native format without nullish values - */ -export interface NativeBatchQueryCommand { - query: string - params?: NativeSQLiteQueryParams | NativeSQLiteQueryParams[] -} - /** * status: 0 or undefined for correct execution, 1 for error * message: if status === 1, here you will find error description