react-native-nitro-sqlite 9.0.2 → 9.1.0

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.
@@ -34,7 +34,7 @@ public:
34
34
  explicit NitroSQLiteException(const std::string& message): NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, message) {}
35
35
  NitroSQLiteException(const NitroSQLiteExceptionType& type, const char* message) : NitroSQLiteException(type, std::string(message)) {}
36
36
  NitroSQLiteException(const NitroSQLiteExceptionType& type, const std::string& message)
37
- : _exceptionString("[react-native-nitro-sqlite] " + typeToString(type) + ": " + message) {}
37
+ : _exceptionString("[" + typeToString(type) + "] " + message) {}
38
38
 
39
39
  private:
40
40
  const std::string _exceptionString;
@@ -5,6 +5,7 @@ namespace margelo::nitro::rnnitrosqlite {
5
5
  std::optional<double> HybridNativeQueryResult::getInsertId() {
6
6
  return this->_insertId;
7
7
  }
8
+
8
9
  double HybridNativeQueryResult::getRowsAffected() {
9
10
  return this->_rowsAffected;
10
11
  }
@@ -27,8 +27,11 @@ private:
27
27
  public:
28
28
  // Properties
29
29
  std::optional<double> getInsertId() override;
30
+
30
31
  double getRowsAffected() override;
32
+
31
33
  SQLiteQueryResults getResults() override;
34
+
32
35
  std::optional<SQLiteQueryTableMetadata> getMetadata() override;
33
36
  };
34
37
 
@@ -1,6 +1,5 @@
1
1
  #include "HybridNitroSQLite.hpp"
2
2
  #include "HybridNativeQueryResult.hpp"
3
- #include "ThreadPool.hpp"
4
3
  #include "NitroSQLiteException.hpp"
5
4
  #include "logs.hpp"
6
5
  #include "macros.hpp"
@@ -14,8 +13,6 @@
14
13
 
15
14
  namespace margelo::nitro::rnnitrosqlite {
16
15
 
17
- auto pool = std::make_shared<margelo::rnnitrosqlite::ThreadPool>();
18
-
19
16
  const std::string getDocPath(const std::optional<std::string>& location) {
20
17
  std::string tempDocPath = std::string(HybridNitroSQLite::docPath);
21
18
  if (location) {
@@ -61,23 +58,12 @@ ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const s
61
58
  return std::make_shared<HybridNativeQueryResult>(result.insertId, result.rowsAffected, *result.results, *result.metadata);
62
59
  };
63
60
 
64
- std::future<ExecuteQueryResult> HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query,
65
- const std::optional<SQLiteQueryParams>& params) {
66
- auto promise = std::make_shared<std::promise<ExecuteQueryResult>>();
67
- auto future = promise->get_future();
68
-
69
- auto task = [this, promise, dbName, query, params]() {
70
- try {
71
- auto result = execute(dbName, query, params);
72
- promise->set_value(result);
73
- } catch (...) {
74
- promise->set_exception(std::current_exception());
75
- }
76
- };
77
-
78
- pool->queueWork(std::move(task));
79
-
80
- return future;
61
+ std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>> HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query,
62
+ const std::optional<SQLiteQueryParams>& params) {
63
+ return Promise<std::shared_ptr<HybridNativeQueryResultSpec>>::async([=, this]() -> std::shared_ptr<HybridNativeQueryResultSpec> {
64
+ auto result = execute(dbName, query, params);
65
+ return result;
66
+ });
81
67
  };
82
68
 
83
69
  BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
@@ -87,23 +73,12 @@ BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, cons
87
73
  return BatchQueryResult(result.rowsAffected);
88
74
  };
89
75
 
90
- std::future<BatchQueryResult> HybridNitroSQLite::executeBatchAsync(const std::string& dbName,
91
- const std::vector<BatchQueryCommand>& batchParams) {
92
- auto promise = std::make_shared<std::promise<BatchQueryResult>>();
93
- auto future = promise->get_future();
94
-
95
- auto task = [this, promise, dbName, batchParams]() {
96
- try {
97
- auto result = executeBatch(dbName, batchParams);
98
- promise->set_value(result);
99
- } catch (...) {
100
- promise->set_exception(std::current_exception());
101
- }
102
- };
103
-
104
- pool->queueWork(std::move(task));
105
-
106
- return future;
76
+ std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync(const std::string& dbName,
77
+ const std::vector<BatchQueryCommand>& batchParams) {
78
+ return Promise<BatchQueryResult>::async([=, this]() -> BatchQueryResult {
79
+ auto result = executeBatch(dbName, batchParams);
80
+ return result;
81
+ });
107
82
  };
108
83
 
109
84
  FileLoadResult HybridNitroSQLite::loadFile(const std::string& dbName, const std::string& location) {
@@ -111,22 +86,11 @@ FileLoadResult HybridNitroSQLite::loadFile(const std::string& dbName, const std:
111
86
  return FileLoadResult(result.commands, result.rowsAffected);
112
87
  };
113
88
 
114
- std::future<FileLoadResult> HybridNitroSQLite::loadFileAsync(const std::string& dbName, const std::string& location) {
115
- auto promise = std::make_shared<std::promise<FileLoadResult>>();
116
- auto future = promise->get_future();
117
-
118
- auto task = [this, promise, dbName, location]() {
119
- try {
120
- auto result = loadFile(dbName, location);
121
- promise->set_value(result);
122
- } catch (...) {
123
- promise->set_exception(std::current_exception());
124
- }
125
- };
126
-
127
- pool->queueWork(std::move(task));
128
-
129
- return future;
89
+ std::shared_ptr<Promise<FileLoadResult>> HybridNitroSQLite::loadFileAsync(const std::string& dbName, const std::string& location) {
90
+ return Promise<FileLoadResult>::async([=, this]() -> FileLoadResult {
91
+ auto result = loadFile(dbName, location);
92
+ return result;
93
+ });
130
94
  };
131
95
 
132
96
  } // namespace margelo::nitro::rnnitrosqlite
@@ -18,17 +18,25 @@ public:
18
18
  public:
19
19
  // Methods
20
20
  void open(const std::string& dbName, const std::optional<std::string>& location) override;
21
+
21
22
  void close(const std::string& dbName) override;
23
+
22
24
  void drop(const std::string& dbName, const std::optional<std::string>& location) override;
25
+
23
26
  void attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias,
24
27
  const std::optional<std::string>& location) override;
28
+
25
29
  void detach(const std::string& mainDbName, const std::string& alias) override;
30
+
26
31
  std::shared_ptr<HybridNativeQueryResultSpec> execute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) override;
27
- std::future<std::shared_ptr<HybridNativeQueryResultSpec>> executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) override;
32
+
33
+ std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>> executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) override;
34
+
28
35
  BatchQueryResult executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) override;
29
- std::future<BatchQueryResult> executeBatchAsync(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) override;
36
+ std::shared_ptr<Promise<BatchQueryResult>> executeBatchAsync(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) override;
37
+
30
38
  FileLoadResult loadFile(const std::string& dbName, const std::string& location) override;
31
- std::future<FileLoadResult> loadFileAsync(const std::string& dbName, const std::string& location) override;
39
+ std::shared_ptr<Promise<FileLoadResult>> loadFileAsync(const std::string& dbName, const std::string& location) override;
32
40
  };
33
41
 
34
42
  inline std::string HybridNitroSQLite::docPath = "";
@@ -19,7 +19,6 @@
19
19
  83BC6E792C8F78A200B954D2 /* sqlfileloader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83BC6E682C8F78A200B954D2 /* sqlfileloader.cpp */; };
20
20
  83BC6E7A2C8F78A200B954D2 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 83BC6E6A2C8F78A200B954D2 /* sqlite3.c */; };
21
21
  83BC6E7B2C8F78A200B954D2 /* sqliteBridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83BC6E6C2C8F78A200B954D2 /* sqliteBridge.cpp */; };
22
- 83BC6E7C2C8F78A200B954D2 /* ThreadPool.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 83BC6E6E2C8F78A200B954D2 /* ThreadPool.cpp */; };
23
22
  83BC6E7F2C8F7BDD00B954D2 /* Empty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83BC6E7E2C8F7BDD00B954D2 /* Empty.swift */; };
24
23
  /* End PBXBuildFile section */
25
24
 
@@ -75,8 +74,6 @@
75
74
  83BC6E6B2C8F78A200B954D2 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sqlite3.h; sourceTree = "<group>"; };
76
75
  83BC6E6C2C8F78A200B954D2 /* sqliteBridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sqliteBridge.cpp; sourceTree = "<group>"; };
77
76
  83BC6E6D2C8F78A200B954D2 /* sqliteBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sqliteBridge.h; sourceTree = "<group>"; };
78
- 83BC6E6E2C8F78A200B954D2 /* ThreadPool.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadPool.cpp; sourceTree = "<group>"; };
79
- 83BC6E6F2C8F78A200B954D2 /* ThreadPool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadPool.h; sourceTree = "<group>"; };
80
77
  83BC6E7E2C8F7BDD00B954D2 /* Empty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Empty.swift; sourceTree = "<group>"; };
81
78
  /* End PBXFileReference section */
82
79
 
@@ -206,8 +203,6 @@
206
203
  83BC6E6B2C8F78A200B954D2 /* sqlite3.h */,
207
204
  83BC6E6C2C8F78A200B954D2 /* sqliteBridge.cpp */,
208
205
  83BC6E6D2C8F78A200B954D2 /* sqliteBridge.h */,
209
- 83BC6E6E2C8F78A200B954D2 /* ThreadPool.cpp */,
210
- 83BC6E6F2C8F78A200B954D2 /* ThreadPool.h */,
211
206
  );
212
207
  name = cpp;
213
208
  path = ../cpp;
@@ -272,7 +267,6 @@
272
267
  files = (
273
268
  83BC6E772C8F78A200B954D2 /* OnLoad.cpp in Sources */,
274
269
  83BC6E792C8F78A200B954D2 /* sqlfileloader.cpp in Sources */,
275
- 83BC6E7C2C8F78A200B954D2 /* ThreadPool.cpp in Sources */,
276
270
  83BC6E712C8F78A200B954D2 /* RNNitroSQLite-Swift-Cxx-Bridge.cpp in Sources */,
277
271
  83BC6E762C8F78A200B954D2 /* JSIHelper.cpp in Sources */,
278
272
  83BC6E7F2C8F7BDD00B954D2 /* Empty.swift in Sources */,
@@ -20,7 +20,7 @@
20
20
  #include <NitroModules/ArrayBufferHolder.hpp>
21
21
  #include <NitroModules/AnyMapHolder.hpp>
22
22
  #include <NitroModules/HybridContext.hpp>
23
- #include <NitroModules/PromiseHolder.hpp>
23
+ #include <NitroModules/RuntimeError.hpp>
24
24
 
25
25
  // Forward declarations of Swift defined types
26
26
 
@@ -31,7 +31,7 @@ namespace margelo::nitro::rnnitrosqlite { struct FileLoadResult; }
31
31
  #include <vector>
32
32
  #include <variant>
33
33
  #include <NitroModules/ArrayBuffer.hpp>
34
- #include <future>
34
+ #include <NitroModules/Promise.hpp>
35
35
  #include "BatchQueryResult.hpp"
36
36
  #include "BatchQueryCommand.hpp"
37
37
  #include "FileLoadResult.hpp"
@@ -73,11 +73,11 @@ namespace margelo::nitro::rnnitrosqlite {
73
73
  virtual void attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias, const std::optional<std::string>& location) = 0;
74
74
  virtual void detach(const std::string& mainDbName, const std::string& alias) = 0;
75
75
  virtual std::shared_ptr<margelo::nitro::rnnitrosqlite::HybridNativeQueryResultSpec> execute(const std::string& dbName, const std::string& query, const std::optional<std::vector<std::variant<std::string, double, int64_t, bool, std::shared_ptr<ArrayBuffer>>>>& params) = 0;
76
- virtual std::future<std::shared_ptr<margelo::nitro::rnnitrosqlite::HybridNativeQueryResultSpec>> executeAsync(const std::string& dbName, const std::string& query, const std::optional<std::vector<std::variant<std::string, double, int64_t, bool, std::shared_ptr<ArrayBuffer>>>>& params) = 0;
76
+ virtual std::shared_ptr<Promise<std::shared_ptr<margelo::nitro::rnnitrosqlite::HybridNativeQueryResultSpec>>> executeAsync(const std::string& dbName, const std::string& query, const std::optional<std::vector<std::variant<std::string, double, int64_t, bool, std::shared_ptr<ArrayBuffer>>>>& params) = 0;
77
77
  virtual BatchQueryResult executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) = 0;
78
- virtual std::future<BatchQueryResult> executeBatchAsync(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) = 0;
78
+ virtual std::shared_ptr<Promise<BatchQueryResult>> executeBatchAsync(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) = 0;
79
79
  virtual FileLoadResult loadFile(const std::string& dbName, const std::string& location) = 0;
80
- virtual std::future<FileLoadResult> loadFileAsync(const std::string& dbName, const std::string& location) = 0;
80
+ virtual std::shared_ptr<Promise<FileLoadResult>> loadFileAsync(const std::string& dbName, const std::string& location) = 0;
81
81
 
82
82
  protected:
83
83
  // Hybrid Setup
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-sqlite",
3
- "version": "9.0.2",
3
+ "version": "9.1.0",
4
4
  "description": "Fast SQLite library for React Native built using Nitro Modules",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/commonjs/index",
@@ -79,7 +79,7 @@
79
79
  "peerDependencies": {
80
80
  "react": ">=17.0.0",
81
81
  "react-native": ">=0.74.0",
82
- "react-native-nitro-modules": "0.16.2"
82
+ "react-native-nitro-modules": ">=0.17.0"
83
83
  },
84
84
  "resolutions": {
85
85
  "@types/react": "^18.2.44"
@@ -1,91 +0,0 @@
1
- //
2
- // ThreadPool.cpp
3
- // react-native-nitro-sqlite
4
- //
5
- // Created by Oscar on 13.03.22.
6
- //
7
-
8
- #include "ThreadPool.hpp"
9
-
10
- namespace margelo::rnnitrosqlite {
11
-
12
- ThreadPool::ThreadPool() : done(false) {
13
- // This returns the number of threads supported by the system. If the
14
- // function can't figure out this information, it returns 0. 0 is not good,
15
- // so we create at least 1
16
- auto numberOfThreads = std::thread::hardware_concurrency();
17
- if (numberOfThreads == 0) {
18
- numberOfThreads = 1;
19
- }
20
-
21
- for (unsigned i = 0; i < numberOfThreads; ++i) {
22
- // The threads will execute the private member `doWork`. Note that we need
23
- // to pass a reference to the function (namespaced with the class name) as
24
- // the first argument, and the current object as second argument
25
- threads.push_back(std::thread(&ThreadPool::doWork, this));
26
- }
27
- }
28
-
29
- // The destructor joins all the threads so the program can exit gracefully.
30
- // This will be executed if there is any exception (e.g. creating the threads)
31
- ThreadPool::~ThreadPool() {
32
- // So threads know it's time to shut down
33
- done = true;
34
-
35
- // Wake up all the threads, so they can finish and be joined
36
- workQueueConditionVariable.notify_all();
37
- for (auto& thread : threads) {
38
- if (thread.joinable()) {
39
- thread.join();
40
- }
41
- }
42
- }
43
-
44
- // This function will be called by the server every time there is a request
45
- // that needs to be processed by the thread pool
46
- void ThreadPool::queueWork(std::function<void(void)> task) {
47
- // Grab the mutex
48
- std::lock_guard<std::mutex> g(workQueueMutex);
49
-
50
- // Push the request to the queue
51
- workQueue.push(task);
52
-
53
- // Notify one thread that there are requests to process
54
- workQueueConditionVariable.notify_one();
55
- }
56
-
57
- // Function used by the threads to grab work from the queue
58
- void ThreadPool::doWork() {
59
- // Loop while the queue is not destructing
60
- while (!done) {
61
- std::function<void(void)> task;
62
-
63
- // Create a scope, so we don't lock the queue for longer than necessary
64
- {
65
- std::unique_lock<std::mutex> g(workQueueMutex);
66
- workQueueConditionVariable.wait(g, [&] {
67
- // Only wake up if there are elements in the queue or the program is
68
- // shutting down
69
- return !workQueue.empty() || done;
70
- });
71
-
72
- // If we are shutting down exit witout trying to process more work
73
- if (done) {
74
- break;
75
- }
76
-
77
- task = workQueue.front();
78
- workQueue.pop();
79
- }
80
- ++busy;
81
- task();
82
- --busy;
83
- }
84
- }
85
-
86
- void ThreadPool::waitFinished() {
87
- std::unique_lock<std::mutex> g(workQueueMutex);
88
- workQueueConditionVariable.wait(g, [&] { return workQueue.empty() && (busy == 0); });
89
- }
90
-
91
- } // namespace margelo::rnnitrosqlite
@@ -1,50 +0,0 @@
1
- //
2
- // ThreadPool.hpp
3
- // react-native-nitro-sqlite
4
- //
5
- // Created by Oscar on 13.03.22.
6
- //
7
-
8
- #pragma once
9
-
10
- #include <condition_variable>
11
- #include <exception>
12
- #include <mutex>
13
- #include <queue>
14
- #include <stdio.h>
15
- #include <thread>
16
- #include <vector>
17
-
18
- namespace margelo::rnnitrosqlite {
19
-
20
- class ThreadPool {
21
- public:
22
- ThreadPool();
23
- ~ThreadPool();
24
- void queueWork(std::function<void(void)> task);
25
- void waitFinished();
26
-
27
- private:
28
- unsigned int busy;
29
- // This condition variable is used for the threads to wait until there is work
30
- // to do
31
- std::condition_variable_any workQueueConditionVariable;
32
-
33
- // We store the threads in a vector, so we can later stop them gracefully
34
- std::vector<std::thread> threads;
35
-
36
- // Mutex to protect workQueue
37
- std::mutex workQueueMutex;
38
-
39
- // Queue of requests waiting to be processed
40
- std::queue<std::function<void(void)>> workQueue;
41
-
42
- // This will be set to true when the thread pool is shutting down. This tells
43
- // the threads to stop looping and finish
44
- bool done;
45
-
46
- // Function used by the threads to grab work from the queue
47
- void doWork();
48
- };
49
-
50
- } // namespace margelo::rnnitrosqlite