Skip to content

Commit

Permalink
Simplify detach
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 15, 2024
1 parent 28be06a commit c950144
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
8 changes: 2 additions & 6 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,11 @@ 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
BridgeResult result = opsqlite_libsql_detach(dbName, alias);
opsqlite_libsql_detach(dbName, alias);
#else
BridgeResult result = opsqlite_detach(db, dbName, alias);
opsqlite_detach(db, dbName, alias);
#endif

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

return {};
});

Expand Down
12 changes: 3 additions & 9 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,14 @@ void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
}
}

BridgeResult opsqlite_detach(sqlite3 *db, std::string const &mainDBName,
void opsqlite_detach(sqlite3 *db, std::string const &main_db_name,
std::string const &alias) {
std::string statement = "DETACH DATABASE " + alias;
BridgeResult result = opsqlite_execute(db, statement, nullptr);
if (result.type == SQLiteError) {
return BridgeResult{
.type = SQLiteError,
.message = mainDBName + "was unable to detach database: " +
std::string(result.message),
};
throw std::runtime_error(main_db_name + "was unable to detach database: " +
std::string(result.message));
}
return BridgeResult{
.type = SQLiteOk,
};
}

void opsqlite_remove(sqlite3 *db, std::string const &name,
Expand Down
2 changes: 1 addition & 1 deletion cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
std::string const &secondary_db_name,
std::string const &alias);

BridgeResult opsqlite_detach(sqlite3 *db, std::string const &mainDBName,
void opsqlite_detach(sqlite3 *db, std::string const &main_db_name,
std::string const &alias);

BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
Expand Down

0 comments on commit c950144

Please sign in to comment.