Skip to content

Commit

Permalink
Move sqlite3 *db into HostObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 14, 2024
1 parent 899a507 commit 333e771
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 270 deletions.
55 changes: 22 additions & 33 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void DBHostObject::flush_pending_reactive_queries(
std::shared_ptr<std::vector<SmartHostObject>> metadata =
std::make_shared<std::vector<SmartHostObject>>();

auto status = opsqlite_execute_prepared_statement(db_name, query->stmt,
auto status = opsqlite_execute_prepared_statement(db, query->stmt,
&results, metadata);

if (status.type == SQLiteError) {
Expand Down Expand Up @@ -128,6 +128,12 @@ void DBHostObject::auto_register_update_hook() {
}
#endif

// _____ _ _
// / ____| | | | |
// | | ___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __
// | | / _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__|
// | |___| (_) | | | \__ \ |_| | | |_| | (__| || (_) | |
// \_____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_|
#ifdef OP_SQLITE_USE_LIBSQL
DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &url,
std::string &auth_token,
Expand Down Expand Up @@ -180,14 +186,8 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
#elif OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_open(db_name, path, crsqlite_path);
#else
BridgeResult result =
opsqlite_open(db_name, path, crsqlite_path, sqlite_vec_path);
db = opsqlite_open(db_name, path, crsqlite_path, sqlite_vec_path);
#endif

if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}

create_jsi_functions();
};

Expand Down Expand Up @@ -221,7 +221,7 @@ void DBHostObject::create_jsi_functions() {
opsqlite_libsql_attach(dbName, tempDocPath, databaseToAttach, alias);
#else
BridgeResult result =
opsqlite_attach(dbName, tempDocPath, databaseToAttach, alias);
opsqlite_attach(db, dbName, tempDocPath, databaseToAttach, alias);
#endif
if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
Expand All @@ -246,7 +246,7 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_detach(dbName, alias);
#else
BridgeResult result = opsqlite_detach(dbName, alias);
BridgeResult result = opsqlite_detach(db, dbName, alias);
#endif

if (result.type == SQLiteError) {
Expand All @@ -260,13 +260,9 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_close(db_name);
#else
BridgeResult result = opsqlite_close(db_name);
opsqlite_close(db);
#endif

if (result.type == SQLiteError) {
throw jsi::JSError(rt, result.message.c_str());
}

return {};
});

Expand Down Expand Up @@ -295,13 +291,9 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_remove(db_name, path);
#else
BridgeResult result = opsqlite_remove(db_name, path);
opsqlite_remove(db, db_name, path);
#endif

if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}

return {};
});

Expand All @@ -327,7 +319,7 @@ void DBHostObject::create_jsi_functions() {
auto status =
opsqlite_libsql_execute_raw(db_name, query, &params, &results);
#else
auto status = opsqlite_execute_raw(db_name, query, &params, &results);
auto status = opsqlite_execute_raw(db, query, &params, &results);
#endif

if (invalidated) {
Expand Down Expand Up @@ -378,7 +370,7 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute(db_name, query, &params);
#else
auto status = opsqlite_execute(db_name, query, &params);
auto status = opsqlite_execute(db, query, &params);
#endif

if (status.type != SQLiteOk) {
Expand Down Expand Up @@ -407,7 +399,7 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute(db_name, query, &params);
#else
auto status = opsqlite_execute(db_name, query, &params);
auto status = opsqlite_execute(db, query, &params);
#endif

if (invalidated) {
Expand Down Expand Up @@ -472,7 +464,7 @@ void DBHostObject::create_jsi_functions() {
auto status = opsqlite_libsql_execute_with_host_objects(
db_name, query, &params, &results, metadata);
#else
auto status = opsqlite_execute_host_objects(db_name, query, &params,
auto status = opsqlite_execute_host_objects(db, query, &params,
&results, metadata);
#endif

Expand Down Expand Up @@ -552,7 +544,7 @@ void DBHostObject::create_jsi_functions() {
auto batchResult =
opsqlite_libsql_execute_batch(db_name, commands.get());
#else
auto batchResult = opsqlite_execute_batch(db_name, commands.get());
auto batchResult = opsqlite_execute_batch(db, commands.get());
#endif

if (invalidated) {
Expand Down Expand Up @@ -617,7 +609,7 @@ void DBHostObject::create_jsi_functions() {

auto task = [&rt, this, sqlFileName, resolve, reject]() {
try {
const auto result = importSQLFile(db_name, sqlFileName);
const auto result = import_sql_file(db, sqlFileName);

invoker->invokeAsync([&rt, result = std::move(result), resolve,
reject] {
Expand Down Expand Up @@ -716,10 +708,7 @@ void DBHostObject::create_jsi_functions() {
entry_point = args[1].asString(rt).utf8(rt);
}

auto result = opsqlite_load_extension(db_name, path, entry_point);
if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}
opsqlite_load_extension(db, path, entry_point);
return {};
});

Expand All @@ -742,7 +731,7 @@ void DBHostObject::create_jsi_functions() {
query.getProperty(rt, "fireOn").asObject(rt).asArray(rt);
auto variant_args = to_variant_vec(rt, js_args);

sqlite3_stmt *stmt = opsqlite_prepare_statement(db_name, query_str);
sqlite3_stmt *stmt = opsqlite_prepare_statement(db, query_str);
opsqlite_bind_statement(stmt, &variant_args);

auto callback =
Expand Down Expand Up @@ -803,10 +792,10 @@ void DBHostObject::create_jsi_functions() {
#ifdef OP_SQLITE_USE_LIBSQL
libsql_stmt_t statement = opsqlite_libsql_prepare_statement(db_name, query);
#else
sqlite3_stmt *statement = opsqlite_prepare_statement(db_name, query);
sqlite3_stmt *statement = opsqlite_prepare_statement(db, query);
#endif
auto preparedStatementHostObject =
std::make_shared<PreparedStatementHostObject>(db_name, statement,
std::make_shared<PreparedStatementHostObject>(db, db_name, statement,
invoker, thread_pool);

return jsi::Object::createFromHostObject(rt, preparedStatementHostObject);
Expand Down
11 changes: 6 additions & 5 deletions cpp/DBHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
#include "types.h"
#include <ReactCommon/CallInvoker.h>
#include <jsi/jsi.h>
#include <set>
#include <unordered_map>
#include <vector>
#include <set>

namespace opsqlite {

namespace jsi = facebook::jsi;
namespace react = facebook::react;

struct PendingReactiveInvocation {
std::string db_name;
std::string table;
std::string rowid;
std::string db_name;
std::string table;
std::string rowid;
};

struct TableRowDiscriminator {
Expand Down Expand Up @@ -61,7 +61,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
~DBHostObject();

private:
std::set<std::shared_ptr<ReactiveQuery>> pending_reactive_queries;
std::set<std::shared_ptr<ReactiveQuery>> pending_reactive_queries;
void auto_register_update_hook();
void create_jsi_functions();
void flush_pending_reactive_queries(std::shared_ptr<jsi::Value> resolve);
Expand All @@ -80,6 +80,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
std::vector<PendingReactiveInvocation> pending_reactive_invocations;
bool is_update_hook_registered = false;
bool invalidated = false;
sqlite3 *db;
};

} // namespace opsqlite
2 changes: 1 addition & 1 deletion cpp/PreparedStatementHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
auto status = opsqlite_libsql_execute_prepared_statement(
_name, _stmt, &results, metadata);
#else
auto status = opsqlite_execute_prepared_statement(_name, _stmt,
auto status = opsqlite_execute_prepared_statement(_db, _stmt,
&results, metadata);
#endif
invoker->invokeAsync(
Expand Down
4 changes: 3 additions & 1 deletion cpp/PreparedStatementHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class PreparedStatementHostObject : public jsi::HostObject {
_thread_pool(thread_pool){};
#else
PreparedStatementHostObject(
sqlite3 *db,
std::string name, sqlite3_stmt *stmt,
std::shared_ptr<react::CallInvoker> js_call_invoker,
std::shared_ptr<ThreadPool> thread_pool)
: _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
: _db(db), _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker),
_thread_pool(thread_pool){};
#endif
virtual ~PreparedStatementHostObject();
Expand All @@ -42,6 +43,7 @@ class PreparedStatementHostObject : public jsi::HostObject {
std::shared_ptr<react::CallInvoker> _js_call_invoker;
std::shared_ptr<ThreadPool> _thread_pool;
std::string _name;
sqlite3 *_db;

Check failure on line 46 in cpp/PreparedStatementHostObject.h

View workflow job for this annotation

GitHub Actions / test-ios-libsql

unknown type name 'sqlite3'
#ifdef OP_SQLITE_USE_LIBSQL
libsql_stmt_t _stmt;
#else
Expand Down
Loading

0 comments on commit 333e771

Please sign in to comment.