Skip to content

Commit 9e044f8

Browse files
authored
Merge pull request #71 from OP-Engineering/export
Add get db path function
2 parents d2cd29d + 4e2ec31 commit 9e044f8

File tree

7 files changed

+56
-65
lines changed

7 files changed

+56
-65
lines changed

cpp/bindings.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,30 @@ void install(jsi::Runtime &rt,
635635
return {};
636636
});
637637

638+
auto get_db_path = HOSTFN("getDbPath", 2) {
639+
std::string db_name = args[0].asString(rt).utf8(rt);
640+
std::string path = std::string(basePath);
641+
if (count > 1 && !args[1].isUndefined() && !args[1].isNull()) {
642+
if (!args[1].isString()) {
643+
throw std::runtime_error(
644+
"[op-sqlite][open] database location must be a string");
645+
}
646+
647+
std::string lastPath = args[1].asString(rt).utf8(rt);
648+
649+
if (lastPath == ":memory:") {
650+
path = ":memory:";
651+
} else if (lastPath.rfind("/", 0) == 0) {
652+
path = lastPath;
653+
} else {
654+
path = path + "/" + lastPath;
655+
}
656+
}
657+
658+
auto result = opsqlite_get_db_path(db_name, path);
659+
return jsi::String::createFromUtf8(rt, result);
660+
});
661+
638662
jsi::Object module = jsi::Object(rt);
639663

640664
module.setProperty(rt, "open", std::move(open));
@@ -653,6 +677,7 @@ void install(jsi::Runtime &rt,
653677
module.setProperty(rt, "prepareStatement", std::move(prepare_statement));
654678
module.setProperty(rt, "loadExtension", std::move(load_extension));
655679
module.setProperty(rt, "executeRawAsync", std::move(execute_raw_async));
680+
module.setProperty(rt, "getDbPath", std::move(get_db_path));
656681

657682
rt.global().setProperty(rt, "__OPSQLiteProxy", std::move(module));
658683
}

cpp/bridge.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ inline void check_db_open(std::string const &db_name) {
3030

3131
/// Returns the completely formed db path, but it also creates any sub-folders
3232
/// along the way
33-
std::string get_db_path(std::string const &db_name,
34-
std::string const &location) {
33+
std::string opsqlite_get_db_path(std::string const &db_name,
34+
std::string const &location) {
3535

3636
if (location == ":memory:") {
3737
return location;
@@ -49,7 +49,7 @@ BridgeResult opsqlite_open(std::string const &dbName,
4949
BridgeResult opsqlite_open(std::string const &dbName,
5050
std::string const &last_path) {
5151
#endif
52-
std::string dbPath = get_db_path(dbName, last_path);
52+
std::string dbPath = opsqlite_get_db_path(dbName, last_path);
5353

5454
int sqlOpenFlags =
5555
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
@@ -63,12 +63,12 @@ BridgeResult opsqlite_open(std::string const &dbName,
6363
}
6464

6565
dbMap[dbName] = db;
66+
6667
#ifdef OP_SQLITE_USE_SQLCIPHER
67-
auto encryptionResult =
68-
opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr,
69-
nullptr, nullptr);
70-
LOGD("Encrypting database");
68+
opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr,
69+
nullptr, nullptr);
7170
#endif
71+
7272
return BridgeResult{.type = SQLiteOk, .affectedRows = 0};
7373
}
7474

@@ -91,7 +91,7 @@ BridgeResult opsqlite_attach(std::string const &mainDBName,
9191
std::string const &docPath,
9292
std::string const &databaseToAttach,
9393
std::string const &alias) {
94-
std::string dbPath = get_db_path(databaseToAttach, docPath);
94+
std::string dbPath = opsqlite_get_db_path(databaseToAttach, docPath);
9595
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;
9696

9797
BridgeResult result =
@@ -135,7 +135,7 @@ BridgeResult opsqlite_remove(std::string const &dbName,
135135
}
136136
}
137137

138-
std::string dbPath = get_db_path(dbName, docPath);
138+
std::string dbPath = opsqlite_get_db_path(dbName, docPath);
139139

140140
if (!file_exists(dbPath)) {
141141
return {.type = SQLiteError,

cpp/bridge.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ typedef std::function<void(std::string dbName, std::string tableName,
1919
typedef std::function<void(std::string dbName)> CommitCallback;
2020
typedef std::function<void(std::string dbName)> RollbackCallback;
2121

22+
std::string opsqlite_get_db_path(std::string const &db_name,
23+
std::string const &location);
24+
2225
#ifdef OP_SQLITE_USE_SQLCIPHER
2326
BridgeResult opsqlite_open(std::string const &dbName, std::string const &dbPath,
2427
std::string const &encryptionKey);

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PODS:
1515
- hermes-engine/Pre-built (= 0.73.1)
1616
- hermes-engine/Pre-built (0.73.1)
1717
- libevent (2.1.12)
18-
- op-sqlite (2.0.22):
18+
- op-sqlite (3.0.1):
1919
- React
2020
- React-callinvoker
2121
- React-Core
@@ -1240,7 +1240,7 @@ SPEC CHECKSUMS:
12401240
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
12411241
hermes-engine: 34df9d5034e90bd9bf1505e1ca198760373935af
12421242
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
1243-
op-sqlite: 6e2f955abb2f307107edfb2f128c99089b03ef8d
1243+
op-sqlite: 24443c22e059972abf4109b4e781d3f03336a761
12441244
RCT-Folly: 7169b2b1c44399c76a47b5deaaba715eeeb476c0
12451245
RCTRequired: 6dda55e483f75d2b43781d8ad5bd7df276a50981
12461246
RCTTypeSafety: df0f2632f4e89938b9b9f6152b5e6c66fc6e969e
@@ -1289,4 +1289,4 @@ SPEC CHECKSUMS:
12891289

12901290
PODFILE CHECKSUM: 0ab74fecad6ac2e35f8eab32fe5772c19d2015b2
12911291

1292-
COCOAPODS: 1.14.0
1292+
COCOAPODS: 1.14.3

example/src/App.tsx

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useEffect, useState} from 'react';
22
import {
33
ActivityIndicator,
44
Button,
5+
Clipboard,
56
SafeAreaView,
67
ScrollView,
78
Text,
@@ -39,8 +40,6 @@ export default function App() {
3940
[],
4041
);
4142
const [singleRecordTime, setSingleRecordTime] = useState<number>(0);
42-
const [sqliteMMSetTime, setSqliteMMSetTime] = useState(0);
43-
const [sqliteGetTime, setSqliteMMGetTime] = useState(0);
4443
const [rawExecutionTimes, setRawExecutionTimes] = useState<number[]>([]);
4544
useEffect(() => {
4645
setResults([]);
@@ -84,70 +83,31 @@ export default function App() {
8483
}
8584
};
8685

87-
const queryAndReload = async () => {
88-
queryLargeDB();
89-
setTimeout(() => {
90-
RNRestart.restart();
91-
}, 200);
86+
const copyDbPathToClipboad = async () => {
87+
const db = await open({name: 'dbPath.sqlite'});
88+
db.execute('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)');
89+
const path = db.getDbPath();
90+
await db.close();
91+
console.warn(path);
92+
Clipboard.setString(path);
9293
};
9394

9495
const allTestsPassed = results.reduce((acc: boolean, r: any) => {
9596
return acc && r.type !== 'incorrect';
9697
}, true);
9798

98-
const testAgainstMMKV = () => {
99-
const db = open({
100-
name: 'mmkvTestDb',
101-
});
102-
103-
db.execute('PRAGMA mmap_size=268435456');
104-
// db.execute('PRAGMA journal_mode = OFF;');
105-
db.execute('DROP TABLE IF EXISTS mmkvTest;');
106-
db.execute('CREATE TABLE mmkvTest (text TEXT);');
107-
108-
let insertStatment = db.prepareStatement(
109-
'INSERT INTO "mmkvTest" (text) VALUES (?)',
110-
);
111-
insertStatment.bind(['quack']);
112-
113-
let start = performance.now();
114-
insertStatment.execute();
115-
let end = performance.now();
116-
setSqliteMMSetTime(end - start);
117-
118-
let readStatement = db.prepareStatement('SELECT text from mmkvTest;');
119-
start = performance.now();
120-
readStatement.execute();
121-
end = performance.now();
122-
setSqliteMMGetTime(end - start);
123-
124-
db.close();
125-
};
126-
12799
return (
128100
<SafeAreaView className="flex-1 bg-neutral-900">
129101
<StyledScrollView>
130102
<View className="flex-row p-2 bg-neutral-800 items-center">
131103
<Text className={'font-bold flex-1 text-white'}>Tools</Text>
132104
</View>
133-
<Button title="Reload app middle of query" onPress={queryAndReload} />
105+
<Button
106+
title="Copy DB Path to clipboard"
107+
onPress={copyDbPathToClipboad}
108+
/>
134109
<Button title="Create 300k Record DB" onPress={createLargeDb} />
135110
<Button title="Query 300k Records" onPress={queryLargeDb} />
136-
{/* <Button title="Query single record" onPress={querySingleRecord} />
137-
<Button title="Against MMKV" onPress={testAgainstMMKV} /> */}
138-
<View className="gap-2 items-center mt-4">
139-
{!!sqliteMMSetTime && (
140-
<Text className="text-white">
141-
MM SQLite Write: {sqliteMMSetTime.toFixed(3)} ms
142-
</Text>
143-
)}
144-
145-
{!!sqliteGetTime && (
146-
<Text className="text-white">
147-
MM SQLite Get: {sqliteGetTime.toFixed(3)} ms
148-
</Text>
149-
)}
150-
</View>
151111
{isLoading && <ActivityIndicator color={'white'} size="large" />}
152112

153113
{!!singleRecordTime && (

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@op-engineering/op-sqlite",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Next generation SQLite for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ interface ISQLite {
203203
query: string,
204204
params?: any[]
205205
) => Promise<any[]>;
206+
getDbPath: (dbName: string, location?: string) => string;
206207
}
207208

208209
const locks: Record<
@@ -428,6 +429,7 @@ export type OPSQLiteConnection = {
428429
prepareStatement: (query: string) => PreparedStatementObj;
429430
loadExtension: (path: string, entryPoint?: string) => void;
430431
executeRawAsync: (query: string, params?: any[]) => Promise<any[]>;
432+
getDbPath: () => string;
431433
};
432434

433435
export const open = (options: {
@@ -465,5 +467,6 @@ export const open = (options: {
465467
OPSQLite.loadExtension(options.name, path, entryPoint),
466468
executeRawAsync: (query, params) =>
467469
OPSQLite.executeRawAsync(options.name, query, params),
470+
getDbPath: () => OPSQLite.getDbPath(options.name, options.location),
468471
};
469472
};

0 commit comments

Comments
 (0)