Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Oct 31, 2024
1 parent ea11b7c commit 3ccd01c
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 105 deletions.
6 changes: 2 additions & 4 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace jsi = facebook::jsi;
std::string _base_path;
std::string _crsqlite_path;
std::string _sqlite_vec_path;
std::shared_ptr<react::CallInvoker> _invoker;
std::shared_ptr<ThreadPool> thread_pool = std::make_shared<ThreadPool>();
std::vector<std::shared_ptr<DBHostObject>> dbs;

Expand All @@ -47,14 +46,13 @@ void clearState() {
thread_pool->restartPool();
}

void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker,
void install(jsi::Runtime &rt, const std::shared_ptr<react::CallInvoker>& invoker,
const char *base_path, const char *crsqlite_path,
const char *sqlite_vec_path) {
invalidated = false;
_base_path = std::string(base_path);
_crsqlite_path = std::string(crsqlite_path);
_sqlite_vec_path = std::string(sqlite_vec_path);
_invoker = invoker;

auto open = HOSTFN("open") {
jsi::Object options = args[0].asObject(rt);
Expand Down Expand Up @@ -82,7 +80,7 @@ void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker,
if (!location.empty()) {
if (location == ":memory:") {
path = ":memory:";
} else if (location.rfind("/", 0) == 0) {
} else if (location.rfind('/', 0) == 0) {
path = location;
} else {
path = path + "/" + location;
Expand Down
2 changes: 1 addition & 1 deletion cpp/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace opsqlite {
namespace jsi = facebook::jsi;
namespace react = facebook::react;

void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker,
void install(jsi::Runtime &rt, const std::shared_ptr<react::CallInvoker>& invoker,
const char *base_path, const char *crsqlite_path,
const char *sqlite_vec_path);
void clearState();
Expand Down
15 changes: 4 additions & 11 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ BridgeResult opsqlite_open(std::string const &name,
#else
BridgeResult opsqlite_open(std::string const &name,
std::string const &last_path,
std::string const &crsqlite_path,
[[maybe_unused]] std::string const &crsqlite_path,
std::string const &sqlite_vec_path) {

if (dbMap.count(name) != 0) {
Expand Down Expand Up @@ -253,10 +253,6 @@ BridgeResult opsqlite_execute_prepared_statement(

switch (result) {
case SQLITE_ROW: {
if (results == nullptr) {
break;
}

i = 0;
DumbHostObject row = DumbHostObject(metadatas);

Expand Down Expand Up @@ -314,9 +310,7 @@ BridgeResult opsqlite_execute_prepared_statement(
i++;
}

if (results != nullptr) {
results->emplace_back(row);
}
results->emplace_back(row);

break;
}
Expand Down Expand Up @@ -641,9 +635,8 @@ BridgeResult opsqlite_execute_host_objects(
}
i++;
}
if (results != nullptr) {
results->push_back(row);
}

results->emplace_back(row);
break;
}

Expand Down
8 changes: 4 additions & 4 deletions cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BridgeResult opsqlite_open(std::string const &dbName, std::string const &dbPath,
std::string const &encryptionKey);
#else
BridgeResult opsqlite_open(std::string const &name, std::string const &path,
std::string const &crsqlite_path,
[[maybe_unused]] std::string const &crsqlite_path,
std::string const &sqlite_vec_path);
#endif

Expand Down Expand Up @@ -65,13 +65,13 @@ BridgeResult opsqlite_execute_raw(std::string const &dbName,
void opsqlite_close_all();

BridgeResult opsqlite_register_update_hook(std::string const &dbName,
UpdateCallback const callback);
UpdateCallback callback);
BridgeResult opsqlite_deregister_update_hook(std::string const &dbName);
BridgeResult opsqlite_register_commit_hook(std::string const &dbName,
CommitCallback const callback);
CommitCallback callback);
BridgeResult opsqlite_deregister_commit_hook(std::string const &dbName);
BridgeResult opsqlite_register_rollback_hook(std::string const &dbName,
RollbackCallback const callback);
RollbackCallback callback);
BridgeResult opsqlite_deregister_rollback_hook(std::string const &dbName);

sqlite3_stmt *opsqlite_prepare_statement(std::string const &dbName,
Expand Down
25 changes: 8 additions & 17 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ jsi::Value
createResult(jsi::Runtime &rt, BridgeResult status,
std::vector<DumbHostObject> *results,
std::shared_ptr<std::vector<SmartHostObject>> metadata) {
if (status.type == SQLiteError) {
throw std::invalid_argument(status.message);
}

jsi::Object res = jsi::Object(rt);

res.setProperty(rt, "rowsAffected", status.affectedRows);
Expand All @@ -212,20 +208,15 @@ createResult(jsi::Runtime &rt, BridgeResult status,
}

size_t rowCount = results->size();
jsi::Object rows = jsi::Object(rt);
rows.setProperty(rt, "length", jsi::Value((int)rowCount));

if (rowCount > 0) {
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<DumbHostObject>(obj)));
}
rows.setProperty(rt, "_array", std::move(array));
res.setProperty(rt, "rows", std::move(rows));

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<DumbHostObject>(obj)));
}
res.setProperty(rt, "rows", std::move(array));

size_t column_count = metadata->size();
auto column_array = jsi::Array(rt, column_count);
Expand Down
1 change: 0 additions & 1 deletion cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <jsi/jsi.h>
#include <jsi/jsilib.h>
#include <map>
#include <stdio.h>
#include <vector>

namespace opsqlite {
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"mocha": "^10.7.3",
"nativewind": "^2.0.11",
"react": "18.3.1",
"react-native": "0.76.0",
"react-native": "0.76.1",
"react-native-http-bridge-refurbished": "1.2.9",
"react-native-restart": "^0.0.27",
"react-native-share": "^11.0.3",
Expand Down
8 changes: 4 additions & 4 deletions example/src/tests/dbsetup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ export function dbSetupTests() {
});

if (Platform.OS === 'android') {
it('Create db in external directory Android', () => {
it('Create db in external directory Android', async () => {
let androidDb = open({
name: 'AndroidSDCardDB.sqlite',
location: ANDROID_EXTERNAL_FILES_PATH,
encryptionKey: 'test',
});

androidDb.execute('DROP TABLE IF EXISTS User;');
androidDb.execute(
await androidDb.execute('DROP TABLE IF EXISTS User;');
await androidDb.execute(
'CREATE TABLE User ( id INT PRIMARY KEY, name TEXT NOT NULL, age INT, networth REAL) STRICT;',
);

androidDb.close();
await androidDb.close();
});
}

Expand Down
1 change: 1 addition & 0 deletions example/src/tests/preparedStatements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function preparedStatementsTests() {
db = null;
}
});

it('Creates a prepared statement and executes a prepared statement', async () => {
const statement = db.prepareStatement('SELECT * FROM User;');
let results = await statement.execute();
Expand Down
3 changes: 1 addition & 2 deletions example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,8 @@ export function queriesTests() {

const res = await db.executeWithHostObjects('SELECT * FROM User');

expect(res.rowsAffected).to.equal(1);
expect(res.insertId).to.equal(1);
expect(res.rows!).to.eql([
expect(res.rows).to.eql([
{
id,
name,
Expand Down
8 changes: 4 additions & 4 deletions example/src/tests/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function reactiveTests() {
table: 'User',
},
],
callback: () => {
callback: data => {

Check failure on line 54 in example/src/tests/reactive.spec.ts

View workflow job for this annotation

GitHub Actions / lint

'data' is declared but its value is never read.
fullSelectRan = true;
},
});
Expand All @@ -65,7 +65,7 @@ export function reactiveTests() {
},
],
callback: data => {
emittedUser = data.rows._array[0];
emittedUser = data.rows[0];
},
});

Expand Down Expand Up @@ -170,7 +170,7 @@ export function reactiveTests() {
},
],
callback: data => {
emittedUser = data.rows._array[0];
emittedUser = data.rows[0];
},
});

Expand Down Expand Up @@ -218,7 +218,7 @@ export function reactiveTests() {
},
],
callback: data => {
emittedUser = data.rows._array[0];
emittedUser = data.rows[0];
},
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"clang-format": "^1.8.0",
"lefthook": "^1.5.5",
"react": "18.3.1",
"react-native": "0.76.0",
"react-native": "0.76.1",
"react-native-builder-bob": "^0.23.2",
"turbo": "^1.12.4",
"typescript": "5.0.4"
Expand Down
12 changes: 1 addition & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ function enhanceDB(db: DB, options: any): DB {

const result = await db.executeWithHostObjects(query, sanitizedParams);

// Fix this on the native side
// @ts-ignore
result.rows = result.rows?._array ?? [];

return result;
},
execute: async (
Expand Down Expand Up @@ -320,13 +316,7 @@ function enhanceDB(db: DB, options: any): DB {

stmt.bind(sanitizedParams);
},
execute: async () => {
const res = await stmt.execute();
// TODO fix on the native side
// @ts-ignore
res.rows = res.rows?._array;
return res;
},
execute: stmt.execute,
};
},
transaction: async (
Expand Down
Loading

0 comments on commit 3ccd01c

Please sign in to comment.