Skip to content

Commit

Permalink
Get rid of close_all method
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 14, 2024
1 parent 31044c6 commit d9275fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 72 deletions.
6 changes: 0 additions & 6 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ void clearState() {
}
invalidated = true;

#ifdef OP_SQLITE_USE_LIBSQL
opsqlite_libsql_close_all();
#else
opsqlite_close_all();
#endif

// We then join all the threads before the context gets invalidated
thread_pool->restartPool();
}
Expand Down
89 changes: 35 additions & 54 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@
#endif

namespace opsqlite {

inline void opsqlite_bind_statement(sqlite3_stmt *statement,
const std::vector<JSVariant> *values) {
sqlite3_clear_bindings(statement);

size_t size = values->size();

for (int ii = 0; ii < size; ii++) {
int stmt_index = ii + 1;
JSVariant value = values->at(ii);

if (std::holds_alternative<bool>(value)) {
sqlite3_bind_int(statement, stmt_index,
static_cast<int>(std::get<bool>(value)));
} else if (std::holds_alternative<int>(value)) {
sqlite3_bind_int(statement, stmt_index, std::get<int>(value));
} else if (std::holds_alternative<long long>(value)) {
sqlite3_bind_double(statement, stmt_index,
static_cast<double>(std::get<long long>(value)));
} else if (std::holds_alternative<double>(value)) {
sqlite3_bind_double(statement, stmt_index, std::get<double>(value));
} else if (std::holds_alternative<std::string>(value)) {
std::string str = std::get<std::string>(value);
sqlite3_bind_text(statement, stmt_index, str.c_str(),
static_cast<int>(str.length()), SQLITE_TRANSIENT);
} else if (std::holds_alternative<ArrayBuffer>(value)) {
ArrayBuffer buffer = std::get<ArrayBuffer>(value);
sqlite3_bind_blob(statement, stmt_index, buffer.data.get(),
static_cast<int>(buffer.size), SQLITE_TRANSIENT);
} else {
sqlite3_bind_null(statement, stmt_index);
}
}
}

/// Returns the completely formed db path, but it also creates any sub-folders
/// along the way
std::string opsqlite_get_db_path(std::string const &db_name,
Expand Down Expand Up @@ -149,41 +184,6 @@ void opsqlite_remove(sqlite3 *db, std::string const &name,
remove(db_path.c_str());
}

inline void opsqlite_bind_statement(sqlite3_stmt *statement,
const std::vector<JSVariant> *values) {
// reset any existing bound values
sqlite3_clear_bindings(statement);

size_t size = values->size();

for (int ii = 0; ii < size; ii++) {
int sqIndex = ii + 1;
JSVariant value = values->at(ii);

if (std::holds_alternative<bool>(value)) {
sqlite3_bind_int(statement, sqIndex,
static_cast<int>(std::get<bool>(value)));
} else if (std::holds_alternative<int>(value)) {
sqlite3_bind_int(statement, sqIndex, std::get<int>(value));
} else if (std::holds_alternative<long long>(value)) {
sqlite3_bind_double(statement, sqIndex,
static_cast<double>(std::get<long long>(value)));
} else if (std::holds_alternative<double>(value)) {
sqlite3_bind_double(statement, sqIndex, std::get<double>(value));
} else if (std::holds_alternative<std::string>(value)) {
std::string str = std::get<std::string>(value);
sqlite3_bind_text(statement, sqIndex, str.c_str(),
static_cast<int>(str.length()), SQLITE_TRANSIENT);
} else if (std::holds_alternative<ArrayBuffer>(value)) {
ArrayBuffer buffer = std::get<ArrayBuffer>(value);
sqlite3_bind_blob(statement, sqIndex, buffer.data.get(),
static_cast<int>(buffer.size), SQLITE_TRANSIENT);
} else {
sqlite3_bind_null(statement, sqIndex);
}
}
}

BridgeResult opsqlite_execute_prepared_statement(
sqlite3 *db, sqlite3_stmt *statement, std::vector<DumbHostObject> *results,
std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {
Expand Down Expand Up @@ -464,7 +464,6 @@ BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
.column_names = std::move(column_names)};
}

/// Base execution function, returns HostObjects to the JS environment
BridgeResult opsqlite_execute_host_objects(
sqlite3 *db, std::string const &query, const std::vector<JSVariant> *params,
std::vector<DumbHostObject> *results,
Expand Down Expand Up @@ -763,24 +762,6 @@ opsqlite_execute_raw(sqlite3 *db, std::string const &query,
.insertId = static_cast<double>(latestInsertRowId)};
}

void opsqlite_close_all() {
// TODO figure out if there is a way to close all the connections
// IS THIS EVEN NEEDED? Each host object now has a destructor, so we can clear
// the connection there

// for (auto const &x : dbMap) {
// // Interrupt will make all pending operations to fail with
// // SQLITE_INTERRUPT The ongoing work from threads will then fail ASAP
// sqlite3_interrupt(x.second);
// // Each DB connection can then be safely interrupted
// sqlite3_close_v2(x.second);
// }
// dbMap.clear();
// updateCallbackMap.clear();
// rollbackCallbackMap.clear();
// commitCallbackMap.clear();
}

std::string operation_to_string(int operation_type) {
switch (operation_type) {
case SQLITE_INSERT:
Expand Down
2 changes: 0 additions & 2 deletions cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ BridgeResult opsqlite_execute_raw(sqlite3 *db, std::string const &query,
const std::vector<JSVariant> *params,
std::vector<std::vector<JSVariant>> *results);

void opsqlite_close_all();

BridgeResult opsqlite_register_update_hook(std::string const &dbName,
const UpdateCallback &callback);
BridgeResult opsqlite_deregister_update_hook(std::string const &dbName);
Expand Down
18 changes: 8 additions & 10 deletions cpp/libsql/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ BridgeResult opsqlite_libsql_detach(std::string const &mainDBName,

BridgeResult opsqlite_libsql_sync(std::string const &name);

BridgeResult opsqlite_libsql_execute(
std::string const &name, std::string const &query,
const std::vector<JSVariant> *params);
BridgeResult opsqlite_libsql_execute(std::string const &name,
std::string const &query,
const std::vector<JSVariant> *params);

BridgeResult opsqlite_libsql_execute_with_host_objects(
std::string const &name, std::string const &query,
const std::vector<JSVariant> *params, std::vector<DumbHostObject> *results,
const std::shared_ptr<std::vector<SmartHostObject>>& metadatas);
BridgeResult opsqlite_libsql_execute_with_host_objects(
std::string const &name, std::string const &query,
const std::vector<JSVariant> *params, std::vector<DumbHostObject> *results,
const std::shared_ptr<std::vector<SmartHostObject>> &metadatas);

BridgeResult
opsqlite_libsql_execute_raw(std::string const &dbName, std::string const &query,
Expand All @@ -73,8 +73,6 @@ BatchResult
opsqlite_libsql_execute_batch(std::string const &name,
std::vector<BatchArguments> *commands);

void opsqlite_libsql_close_all();

libsql_stmt_t opsqlite_libsql_prepare_statement(std::string const &name,
std::string const &query);

Expand All @@ -84,6 +82,6 @@ void opsqlite_libsql_bind_statement(libsql_stmt_t stmt,
BridgeResult opsqlite_libsql_execute_prepared_statement(
std::string const &name, libsql_stmt_t stmt,
std::vector<DumbHostObject> *results,
const std::shared_ptr<std::vector<SmartHostObject>>& metadatas);
const std::shared_ptr<std::vector<SmartHostObject>> &metadatas);

} // namespace opsqlite

0 comments on commit d9275fc

Please sign in to comment.