Skip to content

Commit

Permalink
Add test for attach/dettach
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 15, 2024
1 parent 33438ff commit 28be06a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 38 deletions.
31 changes: 14 additions & 17 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,31 @@ void DBHostObject::create_jsi_functions() {
"[op-sqlite][attach] Incorrect number of arguments");
}
if (!args[0].isString() || !args[1].isString() || !args[2].isString()) {
throw jsi::JSError(
rt, "dbName, databaseToAttach and alias must be a strings");
throw jsi::JSError(rt,
"dbName, databaseToAttach and alias must be strings");
return {};
}

std::string tempDocPath = std::string(base_path);
if (count > 3 && !args[3].isUndefined() && !args[3].isNull()) {
std::string secondary_db_path = std::string(base_path);
if (count > 3) {
if (!args[3].isString()) {
throw std::runtime_error(
"[op-sqlite][attach] database location must be a string");
}

tempDocPath = tempDocPath + "/" + args[3].asString(rt).utf8(rt);
secondary_db_path += "/" + args[3].asString(rt).utf8(rt);
}

std::string dbName = args[0].asString(rt).utf8(rt);
std::string databaseToAttach = args[1].asString(rt).utf8(rt);
std::string main_db_name = args[0].asString(rt).utf8(rt);
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(dbName, tempDocPath, databaseToAttach, alias);
BridgeResult result = opsqlite_libsql_attach(
main_db_name, secondary_db_path, secondary_db_name, alias);
#else
BridgeResult result =
opsqlite_attach(db, dbName, tempDocPath, databaseToAttach, alias);
opsqlite_attach(db, main_db_name, secondary_db_path, secondary_db_name,
alias);
#endif
if (result.type == SQLiteError) {
throw std::runtime_error(result.message);
}

return {};
});
Expand Down Expand Up @@ -310,7 +307,7 @@ void DBHostObject::create_jsi_functions() {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);

auto task = [&rt, this, query, params = std::move(params), resolve,
auto task = [this, &rt, query, params = std::move(params), resolve,
reject]() {
try {
std::vector<std::vector<JSVariant>> results;
Expand Down Expand Up @@ -392,7 +389,7 @@ void DBHostObject::create_jsi_functions() {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);

auto task = [&rt, this, query = std::move(query),
auto task = [this, &rt, query = std::move(query),
params = std::move(params), resolve, reject]() {
try {

Expand Down Expand Up @@ -535,7 +532,7 @@ void DBHostObject::create_jsi_functions() {
auto resolve = std::make_shared<jsi::Value>(rt, args[0]);
auto reject = std::make_shared<jsi::Value>(rt, args[1]);

auto task = [&rt, this,
auto task = [this, &rt,
commands =
std::make_shared<std::vector<BatchArguments>>(commands),
resolve, reject]() {
Expand Down
10 changes: 5 additions & 5 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void install(jsi::Runtime &rt,
_crsqlite_path = std::string(crsqlite_path);
_sqlite_vec_path = std::string(sqlite_vec_path);

auto open = HOSTFN("open") {
auto open = HOST_STATIC_FN("open") {
jsi::Object options = args[0].asObject(rt);
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
std::string path = std::string(_base_path);
Expand Down Expand Up @@ -89,15 +89,15 @@ void install(jsi::Runtime &rt,
return jsi::Object::createFromHostObject(rt, db);
});

auto is_sqlcipher = HOSTFN("isSQLCipher") {
auto is_sqlcipher = HOST_STATIC_FN("isSQLCipher") {
#ifdef OP_SQLITE_USE_SQLCIPHER
return true;
#else
return false;
#endif
});

auto is_libsql = HOSTFN("isLibsql") {
auto is_libsql = HOST_STATIC_FN("isLibsql") {
#ifdef OP_SQLITE_USE_LIBSQL
return true;
#else
Expand All @@ -106,7 +106,7 @@ void install(jsi::Runtime &rt,
});

#ifdef OP_SQLITE_USE_LIBSQL
auto open_remote = HOSTFN("openRemote") {
auto open_remote = HOST_STATIC_FN("openRemote") {
jsi::Object options = args[0].asObject(rt);
std::string url = options.getProperty(rt, "url").asString(rt).utf8(rt);
std::string auth_token =
Expand All @@ -117,7 +117,7 @@ void install(jsi::Runtime &rt,
return jsi::Object::createFromHostObject(rt, db);
});

auto open_sync = HOSTFN("openSync") {
auto open_sync = HOST_STATIC_FN("openSync") {
jsi::Object options = args[0].asObject(rt);
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
std::string path = std::string(_base_path);
Expand Down
20 changes: 7 additions & 13 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,19 @@ void opsqlite_close(sqlite3 *db) {
sqlite3_close_v2(db);
}

BridgeResult opsqlite_attach(sqlite3 *db, std::string const &mainDBName,
std::string const &docPath,
std::string const &databaseToAttach,
void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
std::string const &doc_path,
std::string const &secondary_db_name,
std::string const &alias) {
std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath);
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;
std::string secondary_db_path = opsqlite_get_db_path(secondary_db_name, doc_path);
std::string statement = "ATTACH DATABASE '" + secondary_db_path + "' AS " + alias;

BridgeResult result = opsqlite_execute(db, statement, nullptr);

if (result.type == SQLiteError) {
return {
.type = SQLiteError,
.message = mainDBName + " was unable to attach another database: " +
std::string(result.message),
};
throw std::runtime_error(main_db_name + " was unable to attach another database: " +
std::string(result.message));
}
return {
.type = SQLiteOk,
};
}

BridgeResult opsqlite_detach(sqlite3 *db, std::string const &mainDBName,
Expand Down
6 changes: 3 additions & 3 deletions cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ void opsqlite_close(sqlite3 *db);
void opsqlite_remove(sqlite3 *db, std::string const &name,
std::string const &doc_path);

BridgeResult opsqlite_attach(sqlite3 *db, std::string const &mainDBName,
std::string const &docPath,
std::string const &databaseToAttach,
void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
std::string const &doc_path,
std::string const &secondary_db_name,
std::string const &alias);

BridgeResult opsqlite_detach(sqlite3 *db, std::string const &mainDBName,
Expand Down
7 changes: 7 additions & 0 deletions cpp/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ jsi::Function::createFromHostFunction( \
rt, \
jsi::PropNameID::forAscii(rt, name), \
0, \
[=, this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value

#define HOST_STATIC_FN(name) \
jsi::Function::createFromHostFunction( \
rt, \
jsi::PropNameID::forAscii(rt, name), \
0, \
[=](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value

#endif /* macros_h */
25 changes: 25 additions & 0 deletions example/src/tests/dbsetup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,29 @@ export function dbSetupTests() {
}
});
});

it('Can attach/dettach database', () => {
let db = open({
name: 'attachTest.sqlite',
encryptionKey: 'test',
});
let db2 = open({
name: 'attachTest2.sqlite',
encryptionKey: 'test',
});
db2.close();
db.attach('attachTest.sqlite', 'attachTest2.sqlite', 'attach2');
db.executeSync(
'CREATE TABLE IF NOT EXISTS attach2.test (id INTEGER PRIMARY KEY);',
);
let res = db.executeSync('INSERT INTO attach2.test (id) VALUES (1);');
expect(res).to.exist;
db.detach('attachTest2.sqlite', 'attach2');
db2 = open({
name: 'attachTest2.sqlite',
encryptionKey: 'test',
});
db2.close();
db2.delete();
});
}

0 comments on commit 28be06a

Please sign in to comment.