Skip to content

Commit

Permalink
Merge pull request #219 from OP-Engineering/oscar/strip-file-protocol
Browse files Browse the repository at this point in the history
Clean up db location code
  • Loading branch information
ospfranco authored Dec 31, 2024
2 parents cdd3a65 + 6ca5963 commit 6f426ef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
12 changes: 8 additions & 4 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "SmartHostObject.h"
#include "logs.h"
#include "utils.h"
#include <filesystem>
#include <iostream>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -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
Expand Down
69 changes: 34 additions & 35 deletions cpp/libsql/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "SmartHostObject.h"
#include "logs.h"
#include "utils.h"
#include <filesystem>
#include <iostream>
#include <unordered_map>
#include <variant>
Expand All @@ -25,15 +26,20 @@ 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);

// Will return false if the directory already exists, no need to check
std::filesystem::create_directories(resolved_location);

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;
Expand All @@ -50,21 +56,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;
Expand All @@ -75,13 +80,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
Expand All @@ -102,7 +107,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;
Expand All @@ -112,13 +117,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};
Expand All @@ -135,19 +140,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);
}
Expand All @@ -160,17 +162,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());
Expand Down Expand Up @@ -215,9 +217,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<DumbHostObject> *results,
DB const &db, libsql_stmt_t stmt, std::vector<DumbHostObject> *results,
const std::shared_ptr<std::vector<SmartHostObject>> &metadatas) {

libsql_rows_t rows;
Expand Down Expand Up @@ -348,8 +348,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<JSVariant> *params) {

std::vector<std::string> column_names;
Expand All @@ -364,7 +363,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()) {
Expand All @@ -374,7 +373,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
Expand Down Expand Up @@ -719,7 +718,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{
Expand Down
8 changes: 0 additions & 8 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#include "bridge.h"
#endif
#include <fstream>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>

namespace opsqlite {

Expand Down Expand Up @@ -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");
Expand Down
5 changes: 0 additions & 5 deletions cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
#include "DumbHostObject.h"
#include "SmartHostObject.h"
#include "types.h"
#include <any>
#include <jsi/jsi.h>
#include <jsi/jsilib.h>
#include <map>
#include <sqlite3.h>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,13 @@ export const open = (options: {
location?: string;
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);
}

const db = OPSQLite.open(options);
const enhancedDb = enhanceDB(db, options);

Expand Down

0 comments on commit 6f426ef

Please sign in to comment.