Skip to content

Commit

Permalink
Merge pull request #180 from OP-Engineering/oscar/refactor
Browse files Browse the repository at this point in the history
Clean old code and get rid of ._array
  • Loading branch information
ospfranco authored Oct 31, 2024
2 parents ea11b7c + 75bccad commit fdf28a9
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 400 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
32 changes: 13 additions & 19 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 Expand Up @@ -801,7 +794,7 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
i++;
}

results->emplace_back(row);
results->emplace_back(row);

break;
}
Expand Down Expand Up @@ -867,16 +860,17 @@ std::string operation_to_string(int operation_type) {
}
}

void update_callback(void *dbName, int operation_type, char const *database,
char const *table, sqlite3_int64 rowid) {
void update_callback(void *dbName, int operation_type,
[[maybe_unused]] char const *database, char const *table,
sqlite3_int64 row_id) {
std::string &strDbName = *(static_cast<std::string *>(dbName));
auto callback = updateCallbackMap[strDbName];
callback(strDbName, std::string(table), operation_to_string(operation_type),
static_cast<int>(rowid));
static_cast<int>(row_id));
}

BridgeResult opsqlite_register_update_hook(std::string const &dbName,
UpdateCallback const callback) {
UpdateCallback const &callback) {
check_db_open(dbName);

sqlite3 *db = dbMap[dbName];
Expand Down Expand Up @@ -915,7 +909,7 @@ int commit_callback(void *dbName) {
}

BridgeResult opsqlite_register_commit_hook(std::string const &dbName,
CommitCallback const callback) {
CommitCallback const &callback) {
check_db_open(dbName);

sqlite3 *db = dbMap[dbName];
Expand Down Expand Up @@ -951,7 +945,7 @@ void rollback_callback(void *dbName) {
}

BridgeResult opsqlite_register_rollback_hook(std::string const &dbName,
RollbackCallback const callback) {
RollbackCallback const &callback) {
check_db_open(dbName);

sqlite3 *db = dbMap[dbName];
Expand Down Expand Up @@ -1026,7 +1020,7 @@ BatchResult opsqlite_execute_batch(std::string &name,
int affectedRows = 0;
opsqlite_execute(name, "BEGIN EXCLUSIVE TRANSACTION", nullptr);
for (int i = 0; i < commandCount; i++) {
auto command = commands->at(i);
const auto &command = commands->at(i);
// We do not provide a datastructure to receive query data because we
// don't need/want to handle this results in a batch execution
auto result = opsqlite_execute(name, command.sql, command.params.get());
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);
const UpdateCallback& callback);
BridgeResult opsqlite_deregister_update_hook(std::string const &dbName);
BridgeResult opsqlite_register_commit_hook(std::string const &dbName,
CommitCallback const callback);
const CommitCallback& callback);
BridgeResult opsqlite_deregister_commit_hook(std::string const &dbName);
BridgeResult opsqlite_register_rollback_hook(std::string const &dbName,
RollbackCallback const callback);
const 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
Loading

0 comments on commit fdf28a9

Please sign in to comment.