Skip to content

Commit 1dfef44

Browse files
authored
Merge pull request #256 from OP-Engineering/oscar/rework-attach
Rework attach function
2 parents d0bc662 + 8d1f51d commit 1dfef44

File tree

9 files changed

+55
-61
lines changed

9 files changed

+55
-61
lines changed

cpp/DBHostObject.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,54 +188,43 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
188188

189189
void DBHostObject::create_jsi_functions() {
190190
function_map["attach"] = HOSTFN("attach") {
191-
if (count < 3) {
192-
throw std::runtime_error(
193-
"[op-sqlite][attach] Incorrect number of arguments");
194-
}
195-
if (!args[0].isString() || !args[1].isString() || !args[2].isString()) {
196-
throw std::runtime_error("[op-sqlite] name, database to attach and "
197-
"alias must be strings");
198-
}
199-
200191
std::string secondary_db_path = std::string(base_path);
201-
if (count > 3) {
202-
if (!args[3].isString()) {
203-
throw std::runtime_error(
204-
"[op-sqlite][attach] database location must be a string");
205-
}
206192

207-
secondary_db_path += "/" + args[3].asString(rt).utf8(rt);
193+
auto obj_params = args[0].asObject(rt);
194+
195+
std::string secondary_db_name =
196+
obj_params.getProperty(rt, "secondaryDbFileName")
197+
.asString(rt)
198+
.utf8(rt);
199+
std::string alias =
200+
obj_params.getProperty(rt, "alias").asString(rt).utf8(rt);
201+
202+
if (obj_params.hasProperty(rt, "location")) {
203+
std::string location =
204+
obj_params.getProperty(rt, "location").asString(rt).utf8(rt);
205+
secondary_db_path = secondary_db_path + location;
208206
}
209207

210-
std::string main_db_name = args[0].asString(rt).utf8(rt);
211-
std::string secondary_db_name = args[1].asString(rt).utf8(rt);
212-
std::string alias = args[2].asString(rt).utf8(rt);
213208
#ifdef OP_SQLITE_USE_LIBSQL
214209
opsqlite_libsql_attach(db, secondary_db_path, secondary_db_name, alias);
215210
#else
216-
opsqlite_attach(db, main_db_name, secondary_db_path, secondary_db_name,
217-
alias);
211+
opsqlite_attach(db, secondary_db_path, secondary_db_name, alias);
218212
#endif
219213

220214
return {};
221215
});
222216

223217
function_map["detach"] = HOSTFN("detach") {
224-
if (count < 2) {
225-
throw std::runtime_error(
226-
"[op-sqlite][detach] Incorrect number of arguments");
227-
}
228-
if (!args[0].isString() || !args[1].isString()) {
229-
throw std::runtime_error(
230-
"[op-sqlite] database name and alias must be a strings");
218+
219+
if (!args[0].isString()) {
220+
throw std::runtime_error("[op-sqlite] alias must be a strings");
231221
}
232222

233-
std::string dbName = args[0].asString(rt).utf8(rt);
234-
std::string alias = args[1].asString(rt).utf8(rt);
223+
std::string alias = args[0].asString(rt).utf8(rt);
235224
#ifdef OP_SQLITE_USE_LIBSQL
236225
opsqlite_libsql_detach(db, alias);
237226
#else
238-
opsqlite_detach(db, dbName, alias);
227+
opsqlite_detach(db, alias);
239228
#endif
240229

241230
return {};

cpp/bridge.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ void opsqlite_close(sqlite3 *db) {
150150
sqlite3_close_v2(db);
151151
}
152152

153-
void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
154-
std::string const &doc_path,
153+
void opsqlite_attach(sqlite3 *db, std::string const &doc_path,
155154
std::string const &secondary_db_name,
156155
std::string const &alias) {
157156
auto secondary_db_path = opsqlite_get_db_path(secondary_db_name, doc_path);
@@ -160,8 +159,7 @@ void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
160159
opsqlite_execute(db, statement, nullptr);
161160
}
162161

163-
void opsqlite_detach(sqlite3 *db, std::string const &main_db_name,
164-
std::string const &alias) {
162+
void opsqlite_detach(sqlite3 *db, std::string const &alias) {
165163
std::string statement = "DETACH DATABASE " + alias;
166164
opsqlite_execute(db, statement, nullptr);
167165
}

cpp/bridge.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ void opsqlite_close(sqlite3 *db);
3737
void opsqlite_remove(sqlite3 *db, std::string const &name,
3838
std::string const &doc_path);
3939

40-
void opsqlite_attach(sqlite3 *db, std::string const &main_db_name,
41-
std::string const &doc_path,
40+
void opsqlite_attach(sqlite3 *db, std::string const &doc_path,
4241
std::string const &secondary_db_name,
4342
std::string const &alias);
4443

45-
void opsqlite_detach(sqlite3 *db, std::string const &main_db_name,
46-
std::string const &alias);
44+
void opsqlite_detach(sqlite3 *db, std::string const &alias);
4745

4846
BridgeResult opsqlite_execute(sqlite3 *db, std::string const &query,
4947
const std::vector<JSVariant> *params);

docs/docs/api.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,19 @@ SQLite has a limit for attached databases: A default of 10, and a global max of
188188
SQLite docs for [Attach](https://www.sqlite.org/lang_attach.html) - [Detach](https://www.sqlite.org/lang_detach.html)
189189

190190
```tsx
191-
db.attach('mainDatabase', 'statistics', 'stats', '../databases');
191+
// Follows similar API to the `open` call
192+
db.attach({
193+
secondaryDbFileName: 'statistics.sqlite', // Filename of the database (JUST THE FILENAME)
194+
alias: 'stats', // Alias to be applied to the db
195+
location: '../databases', // Path to be prepended to secondaryFileName, in this case full db path: ../databases/statistics.sqlite
196+
});
192197

193198
const res = await db.execute(
194199
'SELECT * FROM some_table_from_mainschema a INNER JOIN stats.some_table b on a.id_column = b.id_column'
195200
);
196201

197202
// You can detach databases at any moment
198-
db.detach('mainDatabase', 'stats');
199-
if (!detachResult.status) {
200-
// Database de-attached
201-
}
203+
db.detach('stats');
202204
```
203205

204206
## Loading SQL Dump Files

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ SPEC CHECKSUMS:
17851785
GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4
17861786
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
17871787
hermes-engine: 06a9c6900587420b90accc394199527c64259db4
1788-
op-sqlite: 4f4f7845e0208b1a6dfa6ac3db7dcab039c109b7
1788+
op-sqlite: e71ef23bf3ab929cc6f2263ad2683317b21a80a6
17891789
RCT-Folly: bf5c0376ffe4dd2cf438dcf86db385df9fdce648
17901790
RCTDeprecation: fb7d408617e25d7f537940000d766d60149c5fea
17911791
RCTRequired: 9aaf0ffcc1f41f0c671af863970ef25c422a9920

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"node": ">=18"
6767
},
6868
"op-sqlite": {
69-
"libsql": true,
69+
"libsql": false,
7070
"sqlcipher": false,
7171
"iosSqlite": false,
7272
"fts5": true,

example/src/tests/dbsetup.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,21 @@ export function dbSetupTests() {
238238
encryptionKey: 'test',
239239
});
240240
db2.close();
241-
db.attach('attachTest.sqlite', 'attachTest2.sqlite', 'attach2');
241+
242+
db.attach({
243+
secondaryDbFileName: 'attachTest2.sqlite',
244+
alias: 'attach2',
245+
});
246+
242247
db.executeSync('DROP TABLE IF EXISTS attach2.test;');
243248
db.executeSync(
244249
'CREATE TABLE IF NOT EXISTS attach2.test (id INTEGER PRIMARY KEY);',
245250
);
246251
let res = db.executeSync('INSERT INTO attach2.test (id) VALUES (1);');
247252
expect(res).to.exist;
248-
db.detach('attachTest2.sqlite', 'attach2');
253+
254+
db.detach('attach2');
255+
249256
db.delete();
250257

251258
db2 = open({

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tools]
2+
ruby = "3.3.0"

src/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,12 @@ export type PreparedStatement = {
103103
type InternalDB = {
104104
close: () => void;
105105
delete: (location?: string) => void;
106-
attach: (
107-
mainDbName: string,
108-
dbNameToAttach: string,
109-
alias: string,
110-
location?: string
111-
) => void;
112-
detach: (mainDbName: string, alias: string) => void;
106+
attach: (params: {
107+
secondaryDbFileName: string;
108+
alias: string;
109+
location?: string;
110+
}) => void;
111+
detach: (alias: string) => void;
113112
transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
114113
executeSync: (query: string, params?: Scalar[]) => QueryResult;
115114
execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
@@ -151,13 +150,12 @@ type InternalDB = {
151150
export type DB = {
152151
close: () => void;
153152
delete: (location?: string) => void;
154-
attach: (
155-
mainDbName: string,
156-
dbNameToAttach: string,
157-
alias: string,
158-
location?: string
159-
) => void;
160-
detach: (mainDbName: string, alias: string) => void;
153+
attach: (params: {
154+
secondaryDbFileName: string;
155+
alias: string;
156+
location?: string;
157+
}) => void;
158+
detach: (alias: string) => void;
161159
/**
162160
* Wraps all the executions into a transaction. If an error is thrown it will rollback all of the changes
163161
*

0 commit comments

Comments
 (0)