Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate libsql and sqlcipher #207

Merged
merged 5 commits into from
Dec 20, 2024
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
57 changes: 25 additions & 32 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace react = facebook::react;

#ifdef OP_SQLITE_USE_LIBSQL
void DBHostObject::flush_pending_reactive_queries(
std::shared_ptr<jsi::Value> resolve) {
const std::shared_ptr<jsi::Value> &resolve) {
invoker->invokeAsync(
[this, resolve]() { resolve->asObject(rt).asFunction(rt).call(rt, {}); });
}
Expand Down Expand Up @@ -135,33 +135,25 @@ void DBHostObject::auto_register_update_hook() {
#ifdef OP_SQLITE_USE_LIBSQL
DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &url,
std::string &auth_token,
std::shared_ptr<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> thread_pool)
std::shared_ptr<react::CallInvoker> invoker
)
: db_name(url), invoker(std::move(invoker)),
thread_pool(std::move(thread_pool)), rt(rt) {
BridgeResult result = opsqlite_libsql_open_remote(url, auth_token);

if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}
rt(rt) {
db = opsqlite_libsql_open_remote(url, auth_token);

create_jsi_functions();
}

DBHostObject::DBHostObject(jsi::Runtime &rt,
std::shared_ptr<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> thread_pool,
std::string &db_name, std::string &path,
std::string &url, std::string &auth_token,
int sync_interval)
: db_name(db_name), invoker(std::move(invoker)),
thread_pool(std::move(thread_pool)), rt(rt) {
BridgeResult result =
rt(rt) {
db =
opsqlite_libsql_open_sync(db_name, path, url, auth_token, sync_interval);

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

create_jsi_functions();
}
Expand All @@ -179,10 +171,10 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
_thread_pool = std::make_shared<ThreadPool>();

#ifdef OP_SQLITE_USE_SQLCIPHER
BridgeResult result = opsqlite_open(db_name, path, crsqlite_path,
db = opsqlite_open(db_name, path, crsqlite_path,
sqlite_vec_path, encryption_key);
#elif OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_open(db_name, path, crsqlite_path);
db = opsqlite_libsql_open(db_name, path, crsqlite_path);
#else
db = opsqlite_open(db_name, path, crsqlite_path, sqlite_vec_path);
#endif
Expand Down Expand Up @@ -214,8 +206,8 @@ void DBHostObject::create_jsi_functions() {
std::string secondary_db_name = args[1].asString(rt).utf8(rt);
std::string alias = args[2].asString(rt).utf8(rt);
#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_attach(
main_db_name, secondary_db_path, secondary_db_name, alias);
opsqlite_libsql_attach(
db, secondary_db_path, secondary_db_name, alias);
#else
opsqlite_attach(db, main_db_name, secondary_db_path, secondary_db_name,
alias);
Expand All @@ -237,7 +229,7 @@ void DBHostObject::create_jsi_functions() {
std::string dbName = args[0].asString(rt).utf8(rt);
std::string alias = args[1].asString(rt).utf8(rt);
#ifdef OP_SQLITE_USE_LIBSQL
opsqlite_libsql_detach(dbName, alias);
opsqlite_libsql_detach(db, alias);
#else
opsqlite_detach(db, dbName, alias);
#endif
Expand All @@ -247,7 +239,7 @@ void DBHostObject::create_jsi_functions() {

function_map["close"] = HOSTFN("close") {
#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_close(db_name);
opsqlite_libsql_close(db);
#else
opsqlite_close(db);
#endif
Expand Down Expand Up @@ -278,7 +270,7 @@ void DBHostObject::create_jsi_functions() {
}

#ifdef OP_SQLITE_USE_LIBSQL
BridgeResult result = opsqlite_libsql_remove(db_name, path);
opsqlite_libsql_remove(db, db_name, path);
#else
opsqlite_remove(db, db_name, path);
#endif
Expand All @@ -303,7 +295,7 @@ void DBHostObject::create_jsi_functions() {

#ifdef OP_SQLITE_USE_LIBSQL
auto status =
opsqlite_libsql_execute_raw(db_name, query, &params, &results);
opsqlite_libsql_execute_raw(db, query, &params, &results);
#else
auto status = opsqlite_execute_raw(db, query, &params, &results);
#endif
Expand Down Expand Up @@ -352,7 +344,7 @@ void DBHostObject::create_jsi_functions() {
params = to_variant_vec(rt, args[1]);
}
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute(db_name, query, &params);
auto status = opsqlite_libsql_execute(db, query, &params);
#else
auto status = opsqlite_execute(db, query, &params);
#endif
Expand All @@ -374,7 +366,7 @@ void DBHostObject::create_jsi_functions() {
reject = std::make_shared<jsi::Value>(rt, args[1])]() {
try {
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute(db_name, query, &params);
auto status = opsqlite_libsql_execute(db, query, &params);
#else
auto status = opsqlite_execute(db, query, &params);
#endif
Expand Down Expand Up @@ -439,7 +431,7 @@ void DBHostObject::create_jsi_functions() {
std::make_shared<std::vector<SmartHostObject>>();
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute_with_host_objects(
db_name, query, &params, &results, metadata);
db, query, &params, &results, metadata);
#else
auto status = opsqlite_execute_host_objects(db, query, &params,
&results, metadata);
Expand Down Expand Up @@ -518,7 +510,7 @@ void DBHostObject::create_jsi_functions() {
try {
#ifdef OP_SQLITE_USE_LIBSQL
auto batchResult =
opsqlite_libsql_execute_batch(db_name, commands.get());
opsqlite_libsql_execute_batch(db, commands.get());
#else
auto batchResult = opsqlite_execute_batch(db, commands.get());
#endif
Expand Down Expand Up @@ -562,10 +554,7 @@ void DBHostObject::create_jsi_functions() {

#ifdef OP_SQLITE_USE_LIBSQL
function_map["sync"] = HOSTFN("sync") {
BridgeResult result = opsqlite_libsql_sync(db_name);
if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}
opsqlite_libsql_sync(db);
return {};
});
#else
Expand Down Expand Up @@ -736,7 +725,7 @@ void DBHostObject::create_jsi_functions() {
function_map["prepareStatement"] = HOSTFN("prepareStatement") {
auto query = args[0].asString(rt).utf8(rt);
#ifdef OP_SQLITE_USE_LIBSQL
libsql_stmt_t statement = opsqlite_libsql_prepare_statement(db_name, query);
libsql_stmt_t statement = opsqlite_libsql_prepare_statement(db, query);
#else
sqlite3_stmt *statement = opsqlite_prepare_statement(db, query);
#endif
Expand Down Expand Up @@ -818,10 +807,14 @@ void DBHostObject::set(jsi::Runtime &_rt, const jsi::PropNameID &name,
void DBHostObject::invalidate() {
invalidated = true;
_thread_pool->restartPool();
#ifdef OP_SQLITE_USE_LIBSQL
opsqlite_libsql_close(db);
#else
if (db != nullptr) {
opsqlite_close(db);
db = nullptr;
}
#endif
}

DBHostObject::~DBHostObject() { invalidate(); }
Expand Down
12 changes: 8 additions & 4 deletions cpp/DBHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#include <ReactCommon/CallInvoker.h>
#include <jsi/jsi.h>
#include <set>
#ifdef OP_SQLITE_USE_LIBSQL
#include "libsql/bridge.h"
#else
#include <sqlite3.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, weird that this was working when compiling for libsql, but good catch! 👍

#endif
#include <unordered_map>
#include <vector>

Expand All @@ -26,7 +30,9 @@ struct TableRowDiscriminator {
};

struct ReactiveQuery {
#ifndef OP_SQLITE_USE_LIBSQL
sqlite3_stmt *stmt;
#endif
std::vector<TableRowDiscriminator> discriminators;
std::shared_ptr<jsi::Value> callback;
};
Expand All @@ -43,8 +49,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
#ifdef OP_SQLITE_USE_LIBSQL
// Constructor for remoteOpen, purely for remote databases
DBHostObject(jsi::Runtime &rt, std::string &url, std::string &auth_token,
std::shared_ptr<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> thread_pool);
std::shared_ptr<react::CallInvoker> invoker);

// Constructor for a local database with remote sync
DBHostObject(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker,
Expand Down Expand Up @@ -84,8 +89,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
bool is_update_hook_registered = false;
bool invalidated = false;
#ifdef OP_SQLITE_USE_LIBSQL
libsql_database_t db;
libsql_connection_t c;
DB db;
#else
sqlite3 *db;
#endif
Expand Down
2 changes: 1 addition & 1 deletion cpp/PreparedStatementHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
try {
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute_prepared_statement(
_name, _stmt, &results, metadata);
_db, _stmt, &results, metadata);
#else
auto status = opsqlite_execute_prepared_statement(
_db, _stmt, &results, metadata);
Expand Down
8 changes: 5 additions & 3 deletions cpp/PreparedStatementHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <jsi/jsi.h>
#include <memory>
#ifdef OP_SQLITE_USE_LIBSQL
#include "libsql/bridge.h"
#include "libsql.h"
#else
#include <sqlite3.h>
Expand All @@ -20,10 +21,10 @@ class PreparedStatementHostObject : public jsi::HostObject {
public:
#ifdef OP_SQLITE_USE_LIBSQL
PreparedStatementHostObject(
std::string name, libsql_stmt_t stmt,
DB const &db, std::string name, libsql_stmt_t 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(std::move(name)), _stmt(stmt), _js_call_invoker(js_call_invoker),
_thread_pool(thread_pool) {};
#else
PreparedStatementHostObject(
Expand All @@ -40,11 +41,12 @@ class PreparedStatementHostObject : public jsi::HostObject {
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override;

private:
sqlite3 *_db;
std::string _name;
#ifdef OP_SQLITE_USE_LIBSQL
DB _db;
libsql_stmt_t _stmt;
#else
sqlite3 *_db;
// This shouldn't be de-allocated until sqlite3_finalize is called on it
sqlite3_stmt *_stmt;
#endif
Expand Down
2 changes: 1 addition & 1 deletion cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void install(jsi::Runtime &rt,
options.getProperty(rt, "authToken").asString(rt).utf8(rt);

std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, url, auth_token, invoker, thread_pool);
rt, url, auth_token, invoker);
return jsi::Object::createFromHostObject(rt, db);
});

Expand Down
2 changes: 1 addition & 1 deletion cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ std::string opsqlite_get_db_path(std::string const &db_name,
}

#ifdef OP_SQLITE_USE_SQLCIPHER
BridgeResult opsqlite_open(std::string const &name, std::string const &path,
sqlite3 *opsqlite_open(std::string const &name, std::string const &path,
std::string const &crsqlite_path,
std::string const &sqlite_vec_path,
std::string const &encryption_key) {
Expand Down
2 changes: 1 addition & 1 deletion cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::string opsqlite_get_db_path(std::string const &db_name,
std::string const &location);

#ifdef OP_SQLITE_USE_SQLCIPHER
BridgeResult opsqlite_open(std::string const &dbName, std::string const &path,
sqlite3 *opsqlite_open(std::string const &dbName, std::string const &path,
std::string const &crsqlite_path,
std::string const &sqlite_vec_path,
std::string const &encryption_key);
Expand Down
Loading
Loading