diff --git a/cpp/bindings.cpp b/cpp/bindings.cpp index e48d8db5..a884f32c 100644 --- a/cpp/bindings.cpp +++ b/cpp/bindings.cpp @@ -635,6 +635,30 @@ void install(jsi::Runtime &rt, return {}; }); + auto get_db_path = HOSTFN("getDbPath", 2) { + std::string db_name = args[0].asString(rt).utf8(rt); + std::string path = std::string(basePath); + if (count > 1 && !args[1].isUndefined() && !args[1].isNull()) { + if (!args[1].isString()) { + throw std::runtime_error( + "[op-sqlite][open] database location must be a string"); + } + + std::string lastPath = args[1].asString(rt).utf8(rt); + + if (lastPath == ":memory:") { + path = ":memory:"; + } else if (lastPath.rfind("/", 0) == 0) { + path = lastPath; + } else { + path = path + "/" + lastPath; + } + } + + auto result = opsqlite_get_db_path(db_name, path); + return jsi::String::createFromUtf8(rt, result); + }); + jsi::Object module = jsi::Object(rt); module.setProperty(rt, "open", std::move(open)); @@ -653,6 +677,7 @@ void install(jsi::Runtime &rt, module.setProperty(rt, "prepareStatement", std::move(prepare_statement)); module.setProperty(rt, "loadExtension", std::move(load_extension)); module.setProperty(rt, "executeRawAsync", std::move(execute_raw_async)); + module.setProperty(rt, "getDbPath", std::move(get_db_path)); rt.global().setProperty(rt, "__OPSQLiteProxy", std::move(module)); } diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index aa4b5dcf..7ae7e72e 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -30,8 +30,8 @@ inline void check_db_open(std::string const &db_name) { /// Returns the completely formed db path, but it also creates any sub-folders /// along the way -std::string get_db_path(std::string const &db_name, - std::string const &location) { +std::string opsqlite_get_db_path(std::string const &db_name, + std::string const &location) { if (location == ":memory:") { return location; @@ -49,7 +49,7 @@ BridgeResult opsqlite_open(std::string const &dbName, BridgeResult opsqlite_open(std::string const &dbName, std::string const &last_path) { #endif - std::string dbPath = get_db_path(dbName, last_path); + std::string dbPath = opsqlite_get_db_path(dbName, last_path); int sqlOpenFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX; @@ -63,12 +63,12 @@ BridgeResult opsqlite_open(std::string const &dbName, } dbMap[dbName] = db; + #ifdef OP_SQLITE_USE_SQLCIPHER - auto encryptionResult = - opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr, - nullptr, nullptr); - LOGD("Encrypting database"); + opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr, + nullptr, nullptr); #endif + return BridgeResult{.type = SQLiteOk, .affectedRows = 0}; } @@ -91,7 +91,7 @@ BridgeResult opsqlite_attach(std::string const &mainDBName, std::string const &docPath, std::string const &databaseToAttach, std::string const &alias) { - std::string dbPath = get_db_path(databaseToAttach, docPath); + std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath); std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias; BridgeResult result = @@ -135,7 +135,7 @@ BridgeResult opsqlite_remove(std::string const &dbName, } } - std::string dbPath = get_db_path(dbName, docPath); + std::string dbPath = opsqlite_get_db_path(dbName, docPath); if (!file_exists(dbPath)) { return {.type = SQLiteError, diff --git a/cpp/bridge.h b/cpp/bridge.h index 016e78d1..14e8b916 100644 --- a/cpp/bridge.h +++ b/cpp/bridge.h @@ -19,6 +19,9 @@ typedef std::function CommitCallback; typedef std::function RollbackCallback; +std::string opsqlite_get_db_path(std::string const &db_name, + std::string const &location); + #ifdef OP_SQLITE_USE_SQLCIPHER BridgeResult opsqlite_open(std::string const &dbName, std::string const &dbPath, std::string const &encryptionKey); diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 28a23328..1b00a691 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -15,7 +15,7 @@ PODS: - hermes-engine/Pre-built (= 0.73.1) - hermes-engine/Pre-built (0.73.1) - libevent (2.1.12) - - op-sqlite (2.0.22): + - op-sqlite (3.0.1): - React - React-callinvoker - React-Core @@ -1240,7 +1240,7 @@ SPEC CHECKSUMS: glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2 hermes-engine: 34df9d5034e90bd9bf1505e1ca198760373935af libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913 - op-sqlite: 6e2f955abb2f307107edfb2f128c99089b03ef8d + op-sqlite: 24443c22e059972abf4109b4e781d3f03336a761 RCT-Folly: 7169b2b1c44399c76a47b5deaaba715eeeb476c0 RCTRequired: 6dda55e483f75d2b43781d8ad5bd7df276a50981 RCTTypeSafety: df0f2632f4e89938b9b9f6152b5e6c66fc6e969e @@ -1289,4 +1289,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 0ab74fecad6ac2e35f8eab32fe5772c19d2015b2 -COCOAPODS: 1.14.0 +COCOAPODS: 1.14.3 diff --git a/example/src/App.tsx b/example/src/App.tsx index 77fe57eb..9dd880e6 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -2,6 +2,7 @@ import React, {useEffect, useState} from 'react'; import { ActivityIndicator, Button, + Clipboard, SafeAreaView, ScrollView, Text, @@ -39,8 +40,6 @@ export default function App() { [], ); const [singleRecordTime, setSingleRecordTime] = useState(0); - const [sqliteMMSetTime, setSqliteMMSetTime] = useState(0); - const [sqliteGetTime, setSqliteMMGetTime] = useState(0); const [rawExecutionTimes, setRawExecutionTimes] = useState([]); useEffect(() => { setResults([]); @@ -84,70 +83,31 @@ export default function App() { } }; - const queryAndReload = async () => { - queryLargeDB(); - setTimeout(() => { - RNRestart.restart(); - }, 200); + const copyDbPathToClipboad = async () => { + const db = await open({name: 'dbPath.sqlite'}); + db.execute('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)'); + const path = db.getDbPath(); + await db.close(); + console.warn(path); + Clipboard.setString(path); }; const allTestsPassed = results.reduce((acc: boolean, r: any) => { return acc && r.type !== 'incorrect'; }, true); - const testAgainstMMKV = () => { - const db = open({ - name: 'mmkvTestDb', - }); - - db.execute('PRAGMA mmap_size=268435456'); - // db.execute('PRAGMA journal_mode = OFF;'); - db.execute('DROP TABLE IF EXISTS mmkvTest;'); - db.execute('CREATE TABLE mmkvTest (text TEXT);'); - - let insertStatment = db.prepareStatement( - 'INSERT INTO "mmkvTest" (text) VALUES (?)', - ); - insertStatment.bind(['quack']); - - let start = performance.now(); - insertStatment.execute(); - let end = performance.now(); - setSqliteMMSetTime(end - start); - - let readStatement = db.prepareStatement('SELECT text from mmkvTest;'); - start = performance.now(); - readStatement.execute(); - end = performance.now(); - setSqliteMMGetTime(end - start); - - db.close(); - }; - return ( Tools -