@op-engineering/op-sqlite 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,6 +22,8 @@ You can find the [benchmarking code in the example app](https://github.com/OP-En
22
22
 
23
23
  Memory consumption is also 1/4 compared to `react-native-quick-sqlite`. This query used to take 1.2 GB of peak memory usage, and now runs in 250mbs.
24
24
 
25
+ You can also turn on Memory Mapping to make your queries even faster by skipping the kernel during I/O, this comes with some disadvantages though. If you want even more speed and you can re-use your queries you can use prepared statements.
26
+
25
27
  # Encryption
26
28
 
27
29
  If you need to encrypt your entire database, there is [`op-sqlcipher`](https://github.com/OP-Engineering/op-sqlcipher), which is a fork of this library that uses [SQLCipher](https://github.com/sqlcipher/sqlcipher). It completely encrypts the database with minimal overhead.
@@ -102,6 +104,21 @@ const largeDb = open({
102
104
  });
103
105
  ```
104
106
 
107
+ # Speed
108
+
109
+ op-sqlite is already the fastest solution it can be, but it doesn't mean you cannot tweak SQLite to be faster (at the cost of some disadvantages). One possible tweak is turning on [Memory Mapping](https://www.sqlite.org/mmap.html). It allows to read/write to/from the disk without going through the kernel. However, if your queries throw an error your application might crash.
110
+
111
+ To turn on Memory Mapping, execute the following pragma statement after opening a db:
112
+
113
+ ```ts
114
+ const db = open({
115
+ name: 'mydb.sqlite',
116
+ });
117
+
118
+ // 0 turns of memory mapping, any other number enables it with the cache size
119
+ db.execute('PRAGMA mmap_size=268435456');
120
+ ```
121
+
105
122
  # API
106
123
 
107
124
  ```typescript
@@ -134,7 +151,7 @@ db = {
134
151
  }
135
152
  ```
136
153
 
137
- ### Simple queries
154
+ ## Simple queries
138
155
 
139
156
  The basic query is **synchronous**, it will block rendering on large operations, further below you will find async versions.
140
157
 
@@ -162,7 +179,7 @@ try {
162
179
  }
163
180
  ```
164
181
 
165
- ### Multiple statements in a single string
182
+ ## Multiple statements in a single string
166
183
 
167
184
  You can execute multiple statements in a single operation. The API however is not really thought for this use case and the results (and their metadata) will be mangled, so you can discard it.
168
185
 
@@ -186,7 +203,7 @@ let t2name = db.execute(
186
203
  console.log(t2name.rows?._array[0].name); // outputs "T2"
187
204
  ```
188
205
 
189
- ### Transactions
206
+ ## Transactions
190
207
 
191
208
  Throwing an error inside the callback will ROLLBACK the transaction.
192
209
 
@@ -215,7 +232,7 @@ await db.transaction('myDatabase', (tx) => {
215
232
  });
216
233
  ```
217
234
 
218
- ### Batch operation
235
+ ## Batch operation
219
236
 
220
237
  Batch execution allows the transactional execution of a set of commands
221
238
 
@@ -232,7 +249,7 @@ const res = db.executeSqlBatch('myDatabase', commands);
232
249
  console.log(`Batch affected ${result.rowsAffected} rows`);
233
250
  ```
234
251
 
235
- ### Dynamic Column Metadata
252
+ ## Dynamic Column Metadata
236
253
 
237
254
  In some scenarios, dynamic applications may need to get some metadata information about the returned result set.
238
255
 
@@ -253,7 +270,7 @@ metadata.forEach((column) => {
253
270
  });
254
271
  ```
255
272
 
256
- ### Async operations
273
+ ## Async operations
257
274
 
258
275
  You might have too much SQL to process and it will cause your application to freeze. There are async versions for some of the operations. This will offload the SQLite processing to a different thread.
259
276
 
@@ -267,7 +284,7 @@ db.executeAsync(
267
284
  );
268
285
  ```
269
286
 
270
- ### Blobs
287
+ ## Blobs
271
288
 
272
289
  Blobs are supported via `ArrayBuffer`, you need to be careful about the semantics though. You cannot instantiate an instance of `ArrayBuffer` directly, nor pass a typed array directly. Here is an example:
273
290
 
@@ -295,7 +312,22 @@ const result = db.execute('SELECT content FROM BlobTable');
295
312
  const finalUint8 = new Uint8Array(result.rows!._array[0].content);
296
313
  ```
297
314
 
298
- ### Attach or Detach other databases
315
+ ## Prepared statements
316
+
317
+ A lot of the work when executing queries is not iterating through the result set itself but, sometimes, planning the execution. If you have a query which is expensive but you can re-use (even if you have to change the arguments) you can use a `prepared statement`:
318
+
319
+ ```ts
320
+ const statement = db.prepareStatement('SELECT * FROM User WHERE name = ?;');
321
+ statement.bind(['Oscar']);
322
+ let results1 = statement.execute();
323
+
324
+ statement.bind(['Carlos']);
325
+ let results2 = statement.execute();
326
+ ```
327
+
328
+ You only pay the price of parsing the query once, and each subsequent execution should be faster.
329
+
330
+ # Attach or Detach other databases
299
331
 
300
332
  SQLite supports attaching or detaching other database files into your main database connection through an alias.
301
333
  You can do any operation you like on this attached database like JOIN results across tables in different schemas, or update data or objects.
@@ -322,7 +354,7 @@ if (!detachResult.status) {
322
354
  }
323
355
  ```
324
356
 
325
- ### Loading SQL Dump Files
357
+ # Loading SQL Dump Files
326
358
 
327
359
  If you have a SQL dump file, you can load it directly, with low memory consumption:
328
360
 
@@ -334,7 +366,7 @@ const { rowsAffected, commands } = db
334
366
  });
335
367
  ```
336
368
 
337
- ## Hooks
369
+ # Hooks
338
370
 
339
371
  You can subscribe to changes in your database by using an update hook:
340
372
 
@@ -396,7 +428,7 @@ db.commitHook(null);
396
428
  db.rollbackHook(null);
397
429
  ```
398
430
 
399
- ## Use built-in SQLite
431
+ # Use built-in SQLite
400
432
 
401
433
  On iOS you can use the embedded SQLite, when running `pod-install` add an environment flag:
402
434
 
@@ -406,11 +438,11 @@ OP_SQLITE_USE_PHONE_VERSION=1 npx pod-install
406
438
 
407
439
  On Android, it is not possible to link the OS SQLite. It is also a bad idea due to vendor changes, old android bugs, etc. Unfortunately, this means this library will add some megabytes to your app size.
408
440
 
409
- ## Enable compile-time options
441
+ # Enable compile-time options
410
442
 
411
443
  By specifying pre-processor flags, you can enable optional features like FTS5, Geopoly, etc.
412
444
 
413
- ### iOS
445
+ ## iOS
414
446
 
415
447
  Add a `post_install` block to your `<PROJECT_ROOT>/ios/Podfile` like so:
416
448
 
@@ -429,7 +461,7 @@ end
429
461
  Replace the `<SQLITE_FLAGS>` part with the flags you want to add.
430
462
  For example, you could add `SQLITE_ENABLE_FTS5=1` to `GCC_PREPROCESSOR_DEFINITIONS` to enable FTS5 in the iOS project.
431
463
 
432
- ### Android
464
+ ## Android
433
465
 
434
466
  You can specify flags via `<PROJECT_ROOT>/android/gradle.properties` like so:
435
467
 
@@ -437,14 +469,18 @@ You can specify flags via `<PROJECT_ROOT>/android/gradle.properties` like so:
437
469
  OPSQLiteFlags="-DSQLITE_ENABLE_FTS5=1"
438
470
  ```
439
471
 
440
- ## Additional configuration
472
+ # Additional configuration
441
473
 
442
- ### App groups (iOS only)
474
+ ## App groups (iOS only)
443
475
 
444
476
  On iOS, the SQLite database can be placed in an app group, in order to make it accessible from other apps in that app group. E.g. for sharing capabilities.
445
477
 
446
478
  To use an app group, add the app group ID as the value for the `OPSQLite_AppGroup` key in your project's `Info.plist` file. You'll also need to configure the app group in your project settings. (Xcode -> Project Settings -> Signing & Capabilities -> Add Capability -> App Groups)
447
479
 
448
- ## License
480
+ # Contribute
481
+
482
+ You need to have clang-format installed (`brew install clang-format`)
483
+
484
+ # License
449
485
 
450
486
  MIT License.
@@ -29,8 +29,10 @@ add_library(
29
29
  ../cpp/ThreadPool.cpp
30
30
  ../cpp/sqlbatchexecutor.h
31
31
  ../cpp/sqlbatchexecutor.cpp
32
- ../cpp/DynamicHostObject.cpp
33
- ../cpp/DynamicHostObject.h
32
+ ../cpp/SmartHostObject.cpp
33
+ ../cpp/SmartHostObject.h
34
+ ../cpp/PreparedStatementHostObject.h
35
+ ../cpp/PreparedStatementHostObject.cpp
34
36
  ../cpp/DumbHostObject.cpp
35
37
  ../cpp/DumbHostObject.h
36
38
  ../cpp/macros.h
@@ -1,37 +1,42 @@
1
1
  #include "DumbHostObject.h"
2
+ #include "SmartHostObject.h"
2
3
  #include "utils.h"
3
4
  #include <iostream>
4
5
 
5
6
  namespace opsqlite {
6
7
 
7
- namespace jsi = facebook::jsi;
8
-
9
- DumbHostObject::DumbHostObject(std::shared_ptr<std::vector<DynamicHostObject>> metadata) {
10
- this->metadata = metadata;
11
- };
12
-
13
- std::vector<jsi::PropNameID> DumbHostObject::getPropertyNames(jsi::Runtime &rt) {
14
- std::vector<jsi::PropNameID> keys;
15
-
16
- for (auto field : *metadata) {
17
- // TODO improve this by generating the propName once on metadata creation
18
- keys.push_back(jsi::PropNameID::forAscii(rt, std::get<std::string>(field.fields[0].second)));
19
- }
20
-
21
- return keys;
22
- }
8
+ namespace jsi = facebook::jsi;
9
+
10
+ DumbHostObject::DumbHostObject(
11
+ std::shared_ptr<std::vector<SmartHostObject>> metadata) {
12
+ this->metadata = metadata;
13
+ };
14
+
15
+ std::vector<jsi::PropNameID>
16
+ DumbHostObject::getPropertyNames(jsi::Runtime &rt) {
17
+ std::vector<jsi::PropNameID> keys;
18
+
19
+ for (auto field : *metadata) {
20
+ // TODO improve this by generating the propName once on metadata creation
21
+ keys.push_back(jsi::PropNameID::forAscii(
22
+ rt, std::get<std::string>(field.fields[0].second)));
23
+ }
23
24
 
24
- jsi::Value DumbHostObject::get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) {
25
- auto name = propNameID.utf8(rt);
26
- auto fields = metadata.get();
27
- for(int i = 0; i < fields->size(); i++) {
28
- auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
29
- if(fieldName == name) {
30
- return toJSI(rt, values.at(i));
31
- }
32
- }
33
-
34
- return {};
25
+ return keys;
26
+ }
27
+
28
+ jsi::Value DumbHostObject::get(jsi::Runtime &rt,
29
+ const jsi::PropNameID &propNameID) {
30
+ auto name = propNameID.utf8(rt);
31
+ auto fields = metadata.get();
32
+ for (int i = 0; i < fields->size(); i++) {
33
+ auto fieldName = std::get<std::string>(fields->at(i).fields[0].second);
34
+ if (fieldName == name) {
35
+ return toJSI(rt, values.at(i));
35
36
  }
37
+ }
36
38
 
39
+ return {};
37
40
  }
41
+
42
+ } // namespace opsqlite
@@ -3,31 +3,31 @@
3
3
 
4
4
  #include <stdio.h>
5
5
 
6
- #include <jsi/jsi.h>
6
+ #include "SmartHostObject.h"
7
+ #include "types.h"
7
8
  #include <any>
9
+ #include <jsi/jsi.h>
8
10
  #include <vector>
9
- #include "types.h"
10
- #include "DynamicHostObject.h"
11
11
 
12
12
  namespace opsqlite {
13
13
 
14
- namespace jsi = facebook::jsi;
14
+ namespace jsi = facebook::jsi;
15
+
16
+ class JSI_EXPORT DumbHostObject : public jsi::HostObject {
17
+ public:
18
+ DumbHostObject(){};
19
+
20
+ DumbHostObject(std::shared_ptr<std::vector<SmartHostObject>> metadata);
15
21
 
16
- class JSI_EXPORT DumbHostObject: public jsi::HostObject {
17
- public:
18
- DumbHostObject() {};
19
-
20
- DumbHostObject(std::shared_ptr<std::vector<DynamicHostObject>> metadata);
22
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
21
23
 
22
- std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
24
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
23
25
 
24
- jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
26
+ std::vector<JSVariant> values;
25
27
 
26
- std::vector<JSVariant> values;
27
-
28
- std::shared_ptr<std::vector<DynamicHostObject>> metadata;
29
- };
28
+ std::shared_ptr<std::vector<SmartHostObject>> metadata;
29
+ };
30
30
 
31
- }
31
+ } // namespace opsqlite
32
32
 
33
33
  #endif /* DumbHostObject_h */
@@ -0,0 +1,88 @@
1
+ //
2
+ // PreparedStatementHostObject.cpp
3
+ // op-sqlite
4
+ //
5
+ // Created by Oscar Franco on 5/12/23.
6
+ //
7
+
8
+ #include "PreparedStatementHostObject.h"
9
+ #include "bridge.h"
10
+ #include "macros.h"
11
+ #include "utils.h"
12
+
13
+ namespace opsqlite {
14
+
15
+ namespace jsi = facebook::jsi;
16
+
17
+ PreparedStatementHostObject::PreparedStatementHostObject(
18
+ std::string dbName, sqlite3_stmt *statementPtr)
19
+ : _dbName(dbName), _statement(statementPtr) {}
20
+
21
+ std::vector<jsi::PropNameID>
22
+ PreparedStatementHostObject::getPropertyNames(jsi::Runtime &rt) {
23
+ std::vector<jsi::PropNameID> keys;
24
+
25
+ // for (auto field : fields) {
26
+ // keys.push_back(jsi::PropNameID::forAscii(rt, field.first));
27
+ // }
28
+
29
+ return keys;
30
+ }
31
+
32
+ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
33
+ const jsi::PropNameID &propNameID) {
34
+ auto name = propNameID.utf8(rt);
35
+
36
+ if (name == "bind") {
37
+ return HOSTFN("bind", 1) {
38
+ if (_statement == NULL) {
39
+ throw std::runtime_error("statement has been freed");
40
+ }
41
+
42
+ std::vector<JSVariant> params;
43
+
44
+ const jsi::Value &originalParams = args[0];
45
+ params = toVariantVec(rt, originalParams);
46
+
47
+ std::vector<DumbHostObject> results;
48
+ std::shared_ptr<std::vector<SmartHostObject>> metadata =
49
+ std::make_shared<std::vector<SmartHostObject>>();
50
+
51
+ sqlite_bind_statement(_statement, &params);
52
+
53
+ return {};
54
+ });
55
+ }
56
+
57
+ if (name == "execute") {
58
+ return HOSTFN("execute", 1) {
59
+ if (_statement == NULL) {
60
+ throw std::runtime_error("statement has been freed");
61
+ }
62
+ std::vector<DumbHostObject> results;
63
+ std::shared_ptr<std::vector<SmartHostObject>> metadata =
64
+ std::make_shared<std::vector<SmartHostObject>>();
65
+
66
+ auto status = sqlite_execute_prepared_statement(_dbName, _statement,
67
+ &results, metadata);
68
+
69
+ if (status.type == SQLiteError) {
70
+ throw std::runtime_error(status.message);
71
+ }
72
+
73
+ auto jsiResult = createResult(rt, status, &results, metadata);
74
+ return jsiResult;
75
+ });
76
+ }
77
+
78
+ return {};
79
+ }
80
+
81
+ PreparedStatementHostObject::~PreparedStatementHostObject() {
82
+ if (_statement != NULL) {
83
+ sqlite3_finalize(_statement);
84
+ _statement = NULL;
85
+ }
86
+ }
87
+
88
+ } // namespace opsqlite
@@ -0,0 +1,36 @@
1
+ //
2
+ // PreparedStatementHostObject.hpp
3
+ // op-sqlite
4
+ //
5
+ // Created by Oscar Franco on 5/12/23.
6
+ //
7
+
8
+ #ifndef PreparedStatementHostObject_h
9
+ #define PreparedStatementHostObject_h
10
+
11
+ #include <jsi/jsi.h>
12
+ #include <memory>
13
+ #include <sqlite3.h>
14
+ #include <string>
15
+
16
+ namespace opsqlite {
17
+ namespace jsi = facebook::jsi;
18
+
19
+ class PreparedStatementHostObject : public jsi::HostObject {
20
+ public:
21
+ PreparedStatementHostObject(std::string dbName, sqlite3_stmt *statement);
22
+ virtual ~PreparedStatementHostObject();
23
+
24
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
25
+
26
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
27
+
28
+ private:
29
+ std::string _dbName;
30
+ // This shouldn't be de-allocated until sqlite3_finalize is called on it
31
+ sqlite3_stmt *_statement;
32
+ };
33
+
34
+ } // namespace opsqlite
35
+
36
+ #endif /* PreparedStatementHostObject_hpp */
@@ -0,0 +1,33 @@
1
+ #include "SmartHostObject.h"
2
+ #include "utils.h"
3
+
4
+ namespace opsqlite {
5
+
6
+ namespace jsi = facebook::jsi;
7
+
8
+ std::vector<jsi::PropNameID>
9
+ SmartHostObject::getPropertyNames(jsi::Runtime &rt) {
10
+ std::vector<jsi::PropNameID> keys;
11
+
12
+ for (auto field : fields) {
13
+ keys.push_back(jsi::PropNameID::forAscii(rt, field.first));
14
+ }
15
+
16
+ return keys;
17
+ }
18
+
19
+ jsi::Value SmartHostObject::get(jsi::Runtime &rt,
20
+ const jsi::PropNameID &propNameID) {
21
+ auto name = propNameID.utf8(rt);
22
+
23
+ for (auto field : fields) {
24
+ auto fieldName = field.first;
25
+ if (fieldName == name) {
26
+ return toJSI(rt, field.second);
27
+ }
28
+ }
29
+
30
+ return {};
31
+ }
32
+
33
+ } // namespace opsqlite
@@ -0,0 +1,26 @@
1
+ #ifndef SmartHostObject_h
2
+ #define SmartHostObject_h
3
+
4
+ #include "types.h"
5
+ #include <any>
6
+ #include <jsi/jsi.h>
7
+ #include <vector>
8
+
9
+ namespace opsqlite {
10
+
11
+ namespace jsi = facebook::jsi;
12
+
13
+ class JSI_EXPORT SmartHostObject : public jsi::HostObject {
14
+ public:
15
+ SmartHostObject(){};
16
+
17
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
18
+
19
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
20
+
21
+ std::vector<std::pair<std::string, JSVariant>> fields;
22
+ };
23
+
24
+ } // namespace opsqlite
25
+
26
+ #endif /* SmartHostObject_h */