Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 23 additions & 17 deletions example/src/screens/UnitTestScreen.tsx
Original file line number Diff line number Diff line change
@@ -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<MochaTestResult[]>([])
Expand All @@ -20,26 +19,33 @@ export function UnitTestScreen() {
}, [])

return (
<ScrollView
contentContainerStyle={[
ScreenStyles.container,
// eslint-disable-next-line react-native/no-inline-styles
{ alignItems: 'flex-start' },
]}
>
{results.map((r, i) => {
if (r.type === 'grouping') return <Text key={i}>{r.description}</Text>
<FlatList
style={styles.unitTestsScreenContainer}
contentContainerStyle={styles.contentContainer}
data={results}
renderItem={({ item }) => {
if (item.type === 'grouping') return <Text>{item.description}</Text>

if (r.type === 'incorrect') {
if (item.type === 'incorrect') {
return (
<Text key={i}>
🔴 {r.description}: {r.errorMsg}
<Text>
🔴 {item.description}: {item.errorMsg}
</Text>
)
}

return <Text key={i}>🟢 {r.description}</Text>
})}
</ScrollView>
return <Text>🟢 {item.description}</Text>
}}
/>
)
}

const styles = StyleSheet.create({
unitTestsScreenContainer: {
flex: 1,
},
contentContainer: {
padding: 20,
paddingBottom: 50,
},
})
39 changes: 3 additions & 36 deletions example/src/tests/unit/specs/operations/execute.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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],
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
})
})
Expand Down
6 changes: 3 additions & 3 deletions package/cpp/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SQLiteNullValue>(value)) {
if (std::holds_alternative<NullType>(value)) {
sqlite3_bind_null(statement, sqliteIndex);
} else if (std::holds_alternative<bool>(value)) {
sqlite3_bind_int(statement, sqliteIndex, std::get<bool>(value));
Expand Down Expand Up @@ -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++;
Expand All @@ -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<SQLiteQueryTableMetadata>();
Expand Down
21 changes: 0 additions & 21 deletions package/cpp/specs/HybridNativeQueryResult.cpp

This file was deleted.

21 changes: 11 additions & 10 deletions package/cpp/specs/HybridNitroSQLite.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "HybridNitroSQLite.hpp"
#include "HybridNativeQueryResult.hpp"
#include "HybridNitroSQLiteQueryResult.hpp"
#include "NitroSQLiteException.hpp"
#include "importSqlFile.hpp"
#include "logs.hpp"
Expand Down Expand Up @@ -50,31 +50,32 @@ void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string&
sqliteDetachDb(mainDbName, alias);
};

using ExecuteQueryResult = std::shared_ptr<HybridNativeQueryResultSpec>;
using ExecuteQueryResult = std::shared_ptr<HybridNitroSQLiteQueryResultSpec>;

ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params);
return std::make_shared<HybridNativeQueryResult>(std::move(result));
return std::make_shared<HybridNitroSQLiteQueryResult>(std::move(result));
};

std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>>
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) {
return Promise<std::shared_ptr<HybridNativeQueryResultSpec>>::async([=, this]() -> std::shared_ptr<HybridNativeQueryResultSpec> {
auto result = execute(dbName, query, params);
return result;
});
return Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>::async(
[=, this]() -> std::shared_ptr<HybridNitroSQLiteQueryResultSpec> {
auto result = execute(dbName, query, params);
return result;
});
};

BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<NativeBatchQueryCommand>& batchParams) {
BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
const auto commands = batchParamsToCommands(batchParams);

auto result = sqliteExecuteBatch(dbName, commands);
return BatchQueryResult(result.rowsAffected);
};

std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync(const std::string& dbName,
const std::vector<NativeBatchQueryCommand>& batchParams) {
const std::vector<BatchQueryCommand>& batchParams) {
return Promise<BatchQueryResult>::async([=, this]() -> BatchQueryResult {
auto result = executeBatch(dbName, batchParams);
return result;
Expand Down
12 changes: 6 additions & 6 deletions package/cpp/specs/HybridNitroSQLite.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "HybridNativeQueryResultSpec.hpp"
#include "HybridNitroSQLiteQueryResultSpec.hpp"
#include "HybridNitroSQLiteSpec.hpp"
#include "types.hpp"

Expand Down Expand Up @@ -28,15 +28,15 @@ class HybridNitroSQLite : public HybridNitroSQLiteSpec {

void detach(const std::string& mainDbName, const std::string& alias) override;

std::shared_ptr<HybridNativeQueryResultSpec> execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) override;
std::shared_ptr<HybridNitroSQLiteQueryResultSpec> execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) override;

std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>>
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) override;

BatchQueryResult executeBatch(const std::string& dbName, const std::vector<NativeBatchQueryCommand>& commands) override;
BatchQueryResult executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) override;
std::shared_ptr<Promise<BatchQueryResult>> executeBatchAsync(const std::string& dbName,
const std::vector<NativeBatchQueryCommand>& commands) override;
const std::vector<BatchQueryCommand>& commands) override;

FileLoadResult loadFile(const std::string& dbName, const std::string& location) override;
std::shared_ptr<Promise<FileLoadResult>> loadFileAsync(const std::string& dbName, const std::string& location) override;
Expand Down
49 changes: 49 additions & 0 deletions package/cpp/specs/HybridNitroSQLiteQueryResult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "HybridNitroSQLiteQueryResult.hpp"
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/Null.hpp>
#include <NitroModules/Promise.hpp>
#include <unordered_map>
#include <variant>
#include <vector>

namespace margelo::nitro::rnnitrosqlite {

std::optional<double> HybridNitroSQLiteQueryResult::getInsertId() {
return _result.insertId;
}

double HybridNitroSQLiteQueryResult::getRowsAffected() {
return _result.rowsAffected;
}

SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
return _result.results;
};

std::optional<NitroSQLiteQueryResultRows> 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<Promise<std::optional<SQLiteQueryResultRow>>> {
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
const auto index = static_cast<size_t>(idx);
if (index >= rows.size()) {
return std::nullopt;
}
return rows[index];
});
};

const auto length = static_cast<double>(rows.size());
return NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction);
}

std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
return _result.metadata;
}

} // namespace margelo::nitro::rnnitrosqlite
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once

#include "HybridNativeQueryResultSpec.hpp"
#include "HybridNitroSQLiteQueryResultSpec.hpp"
#include "types.hpp"
#include <map>

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;
Expand All @@ -21,6 +21,7 @@ class HybridNativeQueryResult : public HybridNativeQueryResultSpec {
std::optional<double> getInsertId() override;
double getRowsAffected() override;
SQLiteQueryResults getResults() override;
std::optional<NitroSQLiteQueryResultRows> getRows() override;
std::optional<SQLiteQueryTableMetadata> getMetadata() override;
};

Expand Down
2 changes: 1 addition & 1 deletion package/cpp/sqliteExecuteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace margelo::rnnitrosqlite {

std::vector<BatchQuery> batchParamsToCommands(const std::vector<NativeBatchQueryCommand>& batchParams) {
std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryCommand>& batchParams) {
auto commands = std::vector<BatchQuery>();

for (auto& command : batchParams) {
Expand Down
4 changes: 2 additions & 2 deletions package/cpp/sqliteExecuteBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#pragma once

#include "NativeBatchQueryCommand.hpp"
#include "BatchQueryCommand.hpp"
#include "types.hpp"

using namespace facebook;
Expand All @@ -20,7 +20,7 @@ struct BatchQuery {
* Local Helper method to translate JSI objects BatchQuery datastructure
* MUST be called in the JavaScript Thread
*/
std::vector<BatchQuery> batchParamsToCommands(const std::vector<NativeBatchQueryCommand>& batchParams);
std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryCommand>& batchParams);

/**
* Execute a batch of commands in a exclusive transaction
Expand Down
Loading
Loading