From d3e6dfae2136abefaa9a9c5a370f3888921a89ff Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Mon, 30 Dec 2024 23:28:34 +0100 Subject: [PATCH 1/4] Strip file:// from passed locations --- src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.ts b/src/index.ts index d8c65f0..41cbc12 100644 --- a/src/index.ts +++ b/src/index.ts @@ -494,6 +494,10 @@ export const open = (options: { location?: string; encryptionKey?: string; }): DB => { + if (options.location?.startsWith('file://')) { + options.location = options.location.substring(7); + } + const db = OPSQLite.open(options); const enhancedDb = enhanceDB(db, options); From 215b0967c4f88fe3604f34e3daaf11e39ca22c58 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Tue, 31 Dec 2024 08:35:30 +0100 Subject: [PATCH 2/4] clean up mkdir function --- cpp/bridge.cpp | 12 +++++--- cpp/libsql/bridge.cpp | 70 +++++++++++++++++++++---------------------- cpp/utils.cpp | 8 ----- cpp/utils.h | 5 ---- src/index.ts | 3 ++ 5 files changed, 46 insertions(+), 52 deletions(-) diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index 8ffa6b9..2eaf98c 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -8,6 +8,7 @@ #include "SmartHostObject.h" #include "logs.h" #include "utils.h" +#include #include #include #include @@ -66,11 +67,14 @@ std::string opsqlite_get_db_path(std::string const &db_name, if (location == ":memory:") { return location; } + char resolved_location[PATH_MAX]; + realpath(location.c_str(), resolved_location); + std::string resolved_location_string = std::string(resolved_location); - mkdir(location); - char resolved_path[PATH_MAX]; - realpath((location + "/" + db_name).c_str(), resolved_path); - return std::string(resolved_path); + // Will return false if the directory already exists, no need to check + std::filesystem::create_directories(resolved_location); + + return resolved_location_string + "/" + db_name; } #ifdef OP_SQLITE_USE_SQLCIPHER diff --git a/cpp/libsql/bridge.cpp b/cpp/libsql/bridge.cpp index e895f10..0da80bb 100644 --- a/cpp/libsql/bridge.cpp +++ b/cpp/libsql/bridge.cpp @@ -25,15 +25,22 @@ std::string opsqlite_get_db_path(std::string const &db_name, return location; } - mkdir(location); - return location + "/" + db_name; + char resolved_location[PATH_MAX]; + realpath(location.c_str(), resolved_location); + std::string resolved_location_string = std::string(resolved_location); + + if (std::filesystem::create_directories(resolved_location)) { + throw std::runtime_error("Could not create directory: " + + resolved_location_string); + } + + return resolved_location_string + "/" + db_name; } DB opsqlite_libsql_open_sync(std::string const &name, - std::string const &base_path, - std::string const &url, - std::string const &auth_token, - int sync_interval) { + std::string const &base_path, + std::string const &url, + std::string const &auth_token, int sync_interval) { std::string path = opsqlite_get_db_path(name, base_path); int status; @@ -50,21 +57,20 @@ DB opsqlite_libsql_open_sync(std::string const &name, .with_webpki = '1'}; status = libsql_open_sync_with_config(config, &db, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } status = libsql_connect(db, &c, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } - return {.db= db, .c= c }; + return {.db = db, .c = c}; } -DB opsqlite_libsql_open(std::string const &name, - std::string const &last_path, - std::string const &crsqlitePath) { +DB opsqlite_libsql_open(std::string const &name, std::string const &last_path, + std::string const &crsqlitePath) { std::string path = opsqlite_get_db_path(name, last_path); int status; @@ -75,13 +81,13 @@ DB opsqlite_libsql_open(std::string const &name, status = libsql_open_file(path.c_str(), &db, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } status = libsql_connect(db, &c, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } #ifdef OP_SQLITE_USE_CRSQLITE @@ -102,7 +108,7 @@ DB opsqlite_libsql_open(std::string const &name, } DB opsqlite_libsql_open_remote(std::string const &url, - std::string const &auth_token) { + std::string const &auth_token) { int status; libsql_database_t db; libsql_connection_t c; @@ -112,13 +118,13 @@ DB opsqlite_libsql_open_remote(std::string const &url, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } status = libsql_connect(db, &c, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } return {.db = db, .c = c}; @@ -135,19 +141,16 @@ void opsqlite_libsql_close(DB &db) { } } -void opsqlite_libsql_attach(DB const &db, - std::string const &docPath, - std::string const &databaseToAttach, - std::string const &alias) { +void opsqlite_libsql_attach(DB const &db, std::string const &docPath, + std::string const &databaseToAttach, + std::string const &alias) { std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath); std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias; opsqlite_libsql_execute(db, statement, nullptr); - } -void opsqlite_libsql_detach(DB const &db, - std::string const &alias) { +void opsqlite_libsql_detach(DB const &db, std::string const &alias) { std::string statement = "DETACH DATABASE " + alias; opsqlite_libsql_execute(db, statement, nullptr); } @@ -160,17 +163,17 @@ void opsqlite_libsql_sync(DB const &db) { if (status != 0) { throw std::runtime_error(err); } - } void opsqlite_libsql_remove(DB &db, std::string const &name, - std::string const &path) { + std::string const &path) { opsqlite_libsql_close(db); std::string full_path = opsqlite_get_db_path(name, path); if (!file_exists(full_path)) { - throw std::runtime_error("[op-sqlite]: Database file not found" + full_path); + throw std::runtime_error("[op-sqlite]: Database file not found" + + full_path); } remove(full_path.c_str()); @@ -215,9 +218,7 @@ void opsqlite_libsql_bind_statement(libsql_stmt_t statement, } BridgeResult opsqlite_libsql_execute_prepared_statement( - DB const &db, - libsql_stmt_t stmt, - std::vector *results, + DB const &db, libsql_stmt_t stmt, std::vector *results, const std::shared_ptr> &metadatas) { libsql_rows_t rows; @@ -348,8 +349,7 @@ libsql_stmt_t opsqlite_libsql_prepare_statement(DB const &db, return stmt; } -BridgeResult opsqlite_libsql_execute(DB const &db, - std::string const &query, +BridgeResult opsqlite_libsql_execute(DB const &db, std::string const &query, const std::vector *params) { std::vector column_names; @@ -364,7 +364,7 @@ BridgeResult opsqlite_libsql_execute(DB const &db, status = libsql_prepare(db.c, query.c_str(), &stmt, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } if (params != nullptr && !params->empty()) { @@ -374,7 +374,7 @@ BridgeResult opsqlite_libsql_execute(DB const &db, status = libsql_query_stmt(stmt, &rows, &err); if (status != 0) { - throw std::runtime_error(err); + throw std::runtime_error(err); } // Get the column names on the first pass @@ -719,7 +719,7 @@ opsqlite_libsql_execute_batch(DB const &db, // don't need/want to handle this results in a batch execution auto result = opsqlite_libsql_execute(db, command.sql, command.params.get()); - affectedRows += result.affectedRows; + affectedRows += result.affectedRows; } opsqlite_libsql_execute(db, "COMMIT", nullptr); return BatchResult{ diff --git a/cpp/utils.cpp b/cpp/utils.cpp index a97866e..6f5eafa 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -4,10 +4,7 @@ #include "bridge.h" #endif #include -#include -#include #include -#include namespace opsqlite { @@ -315,11 +312,6 @@ bool file_exists(const std::string &path) { return (stat(path.c_str(), &buffer) == 0); } -int mkdir(std::string const &path) { - std::filesystem::create_directories(path); - return 0; -} - void log_to_console(jsi::Runtime &runtime, const std::string &message) { auto console = runtime.global().getPropertyAsObject(runtime, "console"); auto log = console.getPropertyAsFunction(runtime, "log"); diff --git a/cpp/utils.h b/cpp/utils.h index 5389821..306a84b 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -3,10 +3,7 @@ #include "DumbHostObject.h" #include "SmartHostObject.h" #include "types.h" -#include #include -#include -#include #include #include #include @@ -41,8 +38,6 @@ void to_batch_arguments(jsi::Runtime &rt, jsi::Array const &batch_params, BatchResult import_sql_file(sqlite3 *db, std::string path); -int mkdir(const std::string &path); - bool folder_exists(const std::string &name); bool file_exists(const std::string &path); diff --git a/src/index.ts b/src/index.ts index 41cbc12..8931bb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -495,6 +495,9 @@ export const open = (options: { encryptionKey?: string; }): DB => { if (options.location?.startsWith('file://')) { + console.warn( + "[op-sqlite] You are passing a path with 'file://' prefix, it's automatically removed" + ); options.location = options.location.substring(7); } From f789a53bbd50b05fb5e0a52a2144e8fee3ae1bb1 Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Tue, 31 Dec 2024 08:37:17 +0100 Subject: [PATCH 3/4] Adjust code for libsql --- cpp/libsql/bridge.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cpp/libsql/bridge.cpp b/cpp/libsql/bridge.cpp index 0da80bb..cdc2b22 100644 --- a/cpp/libsql/bridge.cpp +++ b/cpp/libsql/bridge.cpp @@ -29,10 +29,8 @@ std::string opsqlite_get_db_path(std::string const &db_name, realpath(location.c_str(), resolved_location); std::string resolved_location_string = std::string(resolved_location); - if (std::filesystem::create_directories(resolved_location)) { - throw std::runtime_error("Could not create directory: " + - resolved_location_string); - } + // Will return false if the directory already exists, no need to check + std::filesystem::create_directories(resolved_location); return resolved_location_string + "/" + db_name; } From 6ca596331edc37ecfadc35968ca5e110369c888a Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Tue, 31 Dec 2024 08:48:20 +0100 Subject: [PATCH 4/4] include filesystem in libsql bridge --- cpp/libsql/bridge.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/libsql/bridge.cpp b/cpp/libsql/bridge.cpp index cdc2b22..cbbb233 100644 --- a/cpp/libsql/bridge.cpp +++ b/cpp/libsql/bridge.cpp @@ -3,6 +3,7 @@ #include "SmartHostObject.h" #include "logs.h" #include "utils.h" +#include #include #include #include