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 android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "OPSqlite.hpp"
#include "OPLogs.h"
#include "OPSqlite.hpp"
#include <ReactCommon/CallInvokerHolder.h>
#include <atomic>
#include <fbjni/fbjni.h>
Expand Down
18 changes: 10 additions & 8 deletions cpp/OPBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "OPBridge.hpp"
#include "OPDatabase.hpp"
#include "OPDumbHostObject.hpp"
#include "OPSmartHostObject.hpp"
#include "OPLogs.h"
#include "OPSmartHostObject.hpp"
#include "OPUtils.hpp"
#include <filesystem>
#include <iostream>
Expand Down Expand Up @@ -216,9 +216,7 @@ void create_dirs_if_needed(const std::string &path) {
}
}

void opsqlite_close(sqlite3 *db) {
sqlite3_close_v2(db);
}
void opsqlite_close(sqlite3 *db) { sqlite3_close_v2(db); }

void opsqlite_attach(sqlite3 *db, std::string const &doc_path,
std::string const &secondary_db_name,
Expand Down Expand Up @@ -437,7 +435,8 @@ BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
}

if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params, /* should_clear_bindings */ false);
opsqlite_bind_statement(statement, params,
/* should_clear_bindings */ false);
}

// sqlite3_column_count is the correct signal: it's non-zero for any
Expand Down Expand Up @@ -574,7 +573,8 @@ BridgeResult opsqlite_execute_host_objects(
}

if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params, /* should_clear_bindings */ false);
opsqlite_bind_statement(statement, params,
/* should_clear_bindings */ false);
}

int i, count, column_type;
Expand Down Expand Up @@ -727,7 +727,8 @@ opsqlite_execute_raw(sqlite3 *db, std::string const &query,
}

if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params, /* should_clear_bindings */ false);
opsqlite_bind_statement(statement, params,
/* should_clear_bindings */ false);
}

int i, column_type;
Expand Down Expand Up @@ -939,7 +940,8 @@ opsqlite_execute_batch(sqlite3 *db,
return run_in_transaction(
[db](const char *sql) { opsqlite_execute(db, sql, nullptr); },
[db]() -> std::optional<bool> { return opsqlite_in_transaction(db); },
"BEGIN TRANSACTION", [&]() {
"BEGIN TRANSACTION",
[&]() {
int affectedRows = 0;
for (int i = 0; i < commandCount; i++) {
const auto &command = commands->at(i);
Expand Down
35 changes: 18 additions & 17 deletions cpp/OPDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void OPDatabase::on_rollback() {
}

void OPDatabase::on_update(const std::string &table,
const std::string &operation, long long row_id) {
const std::string &operation, long long row_id) {
if (alive != nullptr && !alive->load()) {
return;
}
Expand Down Expand Up @@ -203,7 +203,7 @@ void OPDatabase::release_hooks() {
#ifdef OP_SQLITE_USE_LIBSQL
// Remote connection constructor
OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &url, std::string &auth_token)
std::string &url, std::string &auth_token)
: db_name(url) {
worker_thread = std::make_shared<WorkerThread>();
db = opsqlite_libsql_open_remote(url, auth_token);
Expand All @@ -213,11 +213,11 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,

// Sync connection constructor
OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &db_name, std::string &path,
std::string &url, std::string &auth_token,
int sync_interval, bool offline,
std::string &encryption_key,
std::string &remote_encryption_key)
std::string &db_name, std::string &path,
std::string &url, std::string &auth_token,
int sync_interval, bool offline,
std::string &encryption_key,
std::string &remote_encryption_key)
: base_path(path), db_name(db_name), delete_db_name(db_name) {

worker_thread = std::make_shared<WorkerThread>();
Expand All @@ -232,8 +232,8 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
#elif defined(OP_SQLITE_USE_TURSO)
// Remote connection constructor
OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &url, std::string &auth_token,
std::string &base_path)
std::string &url, std::string &auth_token,
std::string &base_path)
: base_path(base_path), db_name(url),
delete_db_name(turso_remote_db_name(url)) {
worker_thread = std::make_shared<WorkerThread>();
Expand All @@ -244,9 +244,9 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,

// Sync connection constructor
OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &db_name, std::string &path,
std::string &url, std::string &auth_token,
std::string &remote_encryption_key)
std::string &db_name, std::string &path,
std::string &url, std::string &auth_token,
std::string &remote_encryption_key)
: base_path(path), db_name(db_name), delete_db_name(db_name) {

worker_thread = std::make_shared<WorkerThread>();
Expand All @@ -260,9 +260,9 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
#endif

OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &base_path, std::string &db_name,
std::string &path, bool readOnly,
bool failOnCreate, std::string &encryption_key)
std::string &base_path, std::string &db_name,
std::string &path, bool readOnly, bool failOnCreate,
std::string &encryption_key)
: base_path(base_path), db_name(db_name), delete_db_name(db_name) {
worker_thread = std::make_shared<WorkerThread>();

Expand All @@ -280,7 +280,7 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
};

void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
jsi::Object &js_object) {
jsi::Object &js_object) {
js_object.setProperty(rt, "attach", HFN(this) {
throw_if_closed("attach");

Expand Down Expand Up @@ -757,7 +757,8 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
} catch (const SQLiteError &e) {
throw_js_error(rt, e);
}
opsqlite_bind_statement(stmt, &variant_args, /* should_clear_bindings */ false);
opsqlite_bind_statement(stmt, &variant_args,
/* should_clear_bindings */ false);

auto callback =
std::make_shared<jsi::Value>(query.getProperty(rt, "callback"));
Expand Down
29 changes: 14 additions & 15 deletions cpp/OPDatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,33 @@ struct ReactiveQuery {
std::shared_ptr<jsi::Value> callback;
};

class JSI_EXPORT OPDatabase
: public jsi::NativeState,
public std::enable_shared_from_this<OPDatabase> {
class JSI_EXPORT OPDatabase : public jsi::NativeState,
public std::enable_shared_from_this<OPDatabase> {
public:
// Normal constructor shared between all backends
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object,
std::string &base_path, std::string &db_name,
std::string &path, bool readOnly, bool failOnCreate,
std::string &encryption_key);
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &base_path,
std::string &db_name, std::string &path, bool readOnly,
bool failOnCreate, std::string &encryption_key);

#ifdef OP_SQLITE_USE_LIBSQL
// Constructor for remoteOpen, purely for remote databases
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &url,
std::string &auth_token);
std::string &auth_token);

// Constructor for a local database with remote sync
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &db_name,
std::string &path, std::string &url, std::string &auth_token,
int sync_interval, bool offline, std::string &encryption_key,
std::string &remote_encryption_key);
std::string &path, std::string &url, std::string &auth_token,
int sync_interval, bool offline, std::string &encryption_key,
std::string &remote_encryption_key);
#elif defined(OP_SQLITE_USE_TURSO)
// Constructor for remoteOpen, purely for remote databases
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &url,
std::string &auth_token, std::string &base_path);
std::string &auth_token, std::string &base_path);

// Constructor for a local database with remote sync
OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &db_name,
std::string &path, std::string &url, std::string &auth_token,
std::string &remote_encryption_key);
std::string &path, std::string &url, std::string &auth_token,
std::string &remote_encryption_key);
#endif

void on_update(const std::string &table, const std::string &operation,
Expand All @@ -88,7 +86,8 @@ class JSI_EXPORT OPDatabase
void release_hooks();
void throw_if_closed(const char *function_name) const;
void create_jsi_functions(jsi::Runtime &rt, jsi::Object &js_object);
void flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);
void
flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);

std::string base_path;
// Bound at construction, on the JS thread, to the generation that created
Expand Down
70 changes: 35 additions & 35 deletions cpp/OPDumbHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,64 @@ namespace jsi = facebook::jsi;

DumbHostObject::DumbHostObject(
std::shared_ptr<std::vector<SmartHostObject>> metadata) {
this->metadata = metadata;
this->metadata = metadata;
};

std::vector<jsi::PropNameID>
DumbHostObject::getPropertyNames(jsi::Runtime &rt) {
std::vector<jsi::PropNameID> keys;
std::vector<jsi::PropNameID> keys;

for (auto field : *metadata) {
// TODO improve this by generating the propName once on metadata
// creation
keys.push_back(jsi::PropNameID::forAscii(
rt, std::get<std::string>(field.fields[0].second)));
}
for (auto field : *metadata) {
// TODO improve this by generating the propName once on metadata
// creation
keys.push_back(jsi::PropNameID::forAscii(
rt, std::get<std::string>(field.fields[0].second)));
}

return keys;
return keys;
}

jsi::Value DumbHostObject::get(jsi::Runtime &rt,
const jsi::PropNameID &propNameID) {

auto name = propNameID.utf8(rt);
auto fields = metadata.get();
for (int i = 0; i < fields->size(); i++) {
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
if (fieldName == name) {
return to_jsi(rt, values.at(i));
}
auto name = propNameID.utf8(rt);
auto fields = metadata.get();
for (int i = 0; i < fields->size(); i++) {
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
if (fieldName == name) {
return to_jsi(rt, values.at(i));
}
}

for (auto pairField : ownValues) {
if (name == pairField.first) {
return to_jsi(rt, pairField.second);
}
for (auto pairField : ownValues) {
if (name == pairField.first) {
return to_jsi(rt, pairField.second);
}
}

return {};
return {};
}

void DumbHostObject::set(jsi::Runtime &rt, const jsi::PropNameID &name,
const jsi::Value &value) {
auto key = name.utf8(rt);
auto fields = metadata.get();
for (int i = 0; i < fields->size(); i++) {
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
if (fieldName == key) {
values[i] = to_variant(rt, value);
return;
}
auto key = name.utf8(rt);
auto fields = metadata.get();
for (int i = 0; i < fields->size(); i++) {
auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
if (fieldName == key) {
values[i] = to_variant(rt, value);
return;
}
}

for (auto pairField : ownValues) {
if (key == pairField.first) {
pairField.second = to_variant(rt, value);
return;
}
for (auto pairField : ownValues) {
if (key == pairField.first) {
pairField.second = to_variant(rt, value);
return;
}
}

ownValues.push_back(std::make_pair(key, to_variant(rt, value)));
ownValues.push_back(std::make_pair(key, to_variant(rt, value)));
}

} // namespace opsqlite
34 changes: 17 additions & 17 deletions cpp/OPLogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@
#include <stdio.h>
#define LOG_TAG "op-sqlite"
#define LOGV(...) \
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
#define LOGD(...) \
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
#define LOGI(...) \
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
printf(" "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
#define LOGW(...) \
printf(" * Warning: "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
printf(" * Warning: "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
#define LOGE(...) \
printf(" *** Error: "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
printf(" *** Error: "); \
printf(__VA_ARGS__); \
printf("\t - <%s> \n", LOG_TAG);
#define LOGSIMPLE(...) \
printf(" "); \
printf(__VA_ARGS__);
printf(" "); \
printf(__VA_ARGS__);
#endif // ANDROID
Loading
Loading