@op-engineering/op-sqlite 2.0.1 → 2.0.3

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/cpp/bridge.h CHANGED
@@ -1,11 +1,12 @@
1
1
  #ifndef bridge_h
2
2
  #define bridge_h
3
3
 
4
- #include "utils.h"
5
- #include <vector>
6
- #include "DynamicHostObject.h"
7
4
  #include "DumbHostObject.h"
5
+ #include "SmartHostObject.h"
8
6
  #include "types.h"
7
+ #include "utils.h"
8
+ #include <sqlite3.h>
9
+ #include <vector>
9
10
 
10
11
  namespace opsqlite {
11
12
 
@@ -15,31 +16,52 @@ BridgeResult sqliteOpenDb(std::string const dbName, std::string const dbPath);
15
16
 
16
17
  BridgeResult sqliteCloseDb(std::string const dbName);
17
18
 
18
- BridgeResult sqliteRemoveDb(std::string const dbName, std::string const docPath);
19
+ BridgeResult sqliteRemoveDb(std::string const dbName,
20
+ std::string const docPath);
19
21
 
20
- BridgeResult sqliteAttachDb(std::string const mainDBName, std::string const docPath, std::string const databaseToAttach, std::string const alias);
22
+ BridgeResult sqliteAttachDb(std::string const mainDBName,
23
+ std::string const docPath,
24
+ std::string const databaseToAttach,
25
+ std::string const alias);
21
26
 
22
- BridgeResult sqliteDetachDb(std::string const mainDBName, std::string const alias);
27
+ BridgeResult sqliteDetachDb(std::string const mainDBName,
28
+ std::string const alias);
23
29
 
24
- BridgeResult sqliteExecute(std::string const dbName,
25
- std::string const &query,
26
- const std::vector<JSVariant> *params,
27
- std::vector<DumbHostObject> *results,
28
- std::shared_ptr<std::vector<DynamicHostObject>> metadatas);
30
+ BridgeResult
31
+ sqliteExecute(std::string const dbName, std::string const &query,
32
+ const std::vector<JSVariant> *params,
33
+ std::vector<DumbHostObject> *results,
34
+ std::shared_ptr<std::vector<SmartHostObject>> metadatas);
29
35
 
30
- BridgeResult sqliteExecuteLiteral(std::string const dbName, std::string const &query);
36
+ BridgeResult sqliteExecuteLiteral(std::string const dbName,
37
+ std::string const &query);
31
38
 
32
39
  void sqliteCloseAll();
33
40
 
34
- BridgeResult registerUpdateHook(std::string const dbName,
35
- std::function<void (std::string dbName, std::string tableName, std::string operation, int rowId)> const callback);
41
+ BridgeResult registerUpdateHook(
42
+ std::string const dbName,
43
+ std::function<void(std::string dbName, std::string tableName,
44
+ std::string operation, int rowId)> const callback);
36
45
  BridgeResult unregisterUpdateHook(std::string const dbName);
37
- BridgeResult registerCommitHook(std::string const dbName,
38
- std::function<void (std::string dbName)> const callback);
46
+ BridgeResult
47
+ registerCommitHook(std::string const dbName,
48
+ std::function<void(std::string dbName)> const callback);
39
49
  BridgeResult unregisterCommitHook(std::string const dbName);
40
- BridgeResult registerRollbackHook(std::string const dbName,
41
- std::function<void (std::string dbName)> const callback);
50
+ BridgeResult
51
+ registerRollbackHook(std::string const dbName,
52
+ std::function<void(std::string dbName)> const callback);
42
53
  BridgeResult unregisterRollbackHook(std::string const dbName);
43
- }
54
+
55
+ sqlite3_stmt *sqlite_prepare_statement(std::string const dbName,
56
+ std::string const &query);
57
+
58
+ void sqlite_bind_statement(sqlite3_stmt *statement,
59
+ const std::vector<JSVariant> *params);
60
+
61
+ BridgeResult sqlite_execute_prepared_statement(
62
+ std::string const dbName, sqlite3_stmt *statement,
63
+ std::vector<DumbHostObject> *results,
64
+ std::shared_ptr<std::vector<SmartHostObject>> metadatas);
65
+ } // namespace opsqlite
44
66
 
45
67
  #endif /* bridge_h */
package/cpp/logs.h CHANGED
@@ -12,27 +12,27 @@
12
12
  // LOGS NO ANDROID
13
13
  #include <stdio.h>
14
14
  #define LOG_TAG "op-sqlite"
15
- #define LOGV(...) \
16
- printf(" "); \
17
- printf(__VA_ARGS__); \
15
+ #define LOGV(...) \
16
+ printf(" "); \
17
+ printf(__VA_ARGS__); \
18
18
  printf("\t - <%s> \n", LOG_TAG);
19
- #define LOGD(...) \
20
- printf(" "); \
21
- printf(__VA_ARGS__); \
19
+ #define LOGD(...) \
20
+ printf(" "); \
21
+ printf(__VA_ARGS__); \
22
22
  printf("\t - <%s> \n", LOG_TAG);
23
- #define LOGI(...) \
24
- printf(" "); \
25
- printf(__VA_ARGS__); \
23
+ #define LOGI(...) \
24
+ printf(" "); \
25
+ printf(__VA_ARGS__); \
26
26
  printf("\t - <%s> \n", LOG_TAG);
27
- #define LOGW(...) \
28
- printf(" * Warning: "); \
29
- printf(__VA_ARGS__); \
27
+ #define LOGW(...) \
28
+ printf(" * Warning: "); \
29
+ printf(__VA_ARGS__); \
30
30
  printf("\t - <%s> \n", LOG_TAG);
31
- #define LOGE(...) \
32
- printf(" *** Error: "); \
33
- printf(__VA_ARGS__); \
31
+ #define LOGE(...) \
32
+ printf(" *** Error: "); \
33
+ printf(__VA_ARGS__); \
34
34
  printf("\t - <%s> \n", LOG_TAG);
35
- #define LOGSIMPLE(...) \
36
- printf(" "); \
35
+ #define LOGSIMPLE(...) \
36
+ printf(" "); \
37
37
  printf(__VA_ARGS__);
38
38
  #endif // ANDROID
package/cpp/macros.h CHANGED
@@ -1,7 +1,7 @@
1
1
  #ifndef macros_h
2
2
  #define macros_h
3
3
 
4
- #define HOSTFN(name, basecount) \
4
+ #define HOSTFN(name, basecount) \
5
5
  jsi::Function::createFromHostFunction( \
6
6
  rt, \
7
7
  jsi::PropNameID::forAscii(rt, name), \
@@ -6,89 +6,87 @@ namespace opsqlite {
6
6
  * Batch execution implementation
7
7
  */
8
8
 
9
- void toBatchArguments(jsi::Runtime &rt, jsi::Array const &batchParams, std::vector<BatchArguments> *commands)
10
- {
11
- for (int i = 0; i < batchParams.length(rt); i++)
12
- {
13
- const jsi::Array &command = batchParams.getValueAtIndex(rt, i).asObject(rt).asArray(rt);
14
- if (command.length(rt) == 0)
15
- {
16
- continue;
17
- }
18
-
19
- const std::string query = command.getValueAtIndex(rt, 0).asString(rt).utf8(rt);
20
- const jsi::Value &commandParams = command.length(rt) > 1 ? command.getValueAtIndex(rt, 1) : jsi::Value::undefined();
21
- if (!commandParams.isUndefined() && commandParams.asObject(rt).isArray(rt) && commandParams.asObject(rt).asArray(rt).length(rt) > 0 && commandParams.asObject(rt).asArray(rt).getValueAtIndex(rt, 0).isObject())
22
- {
23
- // This arguments is an array of arrays, like a batch update of a single sql command.
24
- const jsi::Array &batchUpdateParams = commandParams.asObject(rt).asArray(rt);
25
- for (int x = 0; x < batchUpdateParams.length(rt); x++)
26
- {
27
- const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x);
28
- auto params = std::make_shared<std::vector<JSVariant>>(toVariantVec(rt, p));
29
- commands->push_back({
30
- query,
31
- params
32
- });
33
- }
34
- }
35
- else
36
- {
37
- auto params = std::make_shared<std::vector<JSVariant>>(toVariantVec(rt, commandParams));
38
- commands->push_back({
39
- query,
40
- params
41
- });
42
- }
9
+ void toBatchArguments(jsi::Runtime &rt, jsi::Array const &batchParams,
10
+ std::vector<BatchArguments> *commands) {
11
+ for (int i = 0; i < batchParams.length(rt); i++) {
12
+ const jsi::Array &command =
13
+ batchParams.getValueAtIndex(rt, i).asObject(rt).asArray(rt);
14
+ if (command.length(rt) == 0) {
15
+ continue;
43
16
  }
44
- }
45
17
 
46
- BatchResult sqliteExecuteBatch(std::string dbName, std::vector<BatchArguments> *commands)
47
- {
48
- size_t commandCount = commands->size();
49
- if(commandCount <= 0)
50
- {
51
- return BatchResult {
52
- .type = SQLiteError,
53
- .message = "No SQL commands provided",
54
- };
18
+ const std::string query =
19
+ command.getValueAtIndex(rt, 0).asString(rt).utf8(rt);
20
+ const jsi::Value &commandParams = command.length(rt) > 1
21
+ ? command.getValueAtIndex(rt, 1)
22
+ : jsi::Value::undefined();
23
+ if (!commandParams.isUndefined() &&
24
+ commandParams.asObject(rt).isArray(rt) &&
25
+ commandParams.asObject(rt).asArray(rt).length(rt) > 0 &&
26
+ commandParams.asObject(rt)
27
+ .asArray(rt)
28
+ .getValueAtIndex(rt, 0)
29
+ .isObject()) {
30
+ // This arguments is an array of arrays, like a batch update of a single
31
+ // sql command.
32
+ const jsi::Array &batchUpdateParams =
33
+ commandParams.asObject(rt).asArray(rt);
34
+ for (int x = 0; x < batchUpdateParams.length(rt); x++) {
35
+ const jsi::Value &p = batchUpdateParams.getValueAtIndex(rt, x);
36
+ auto params =
37
+ std::make_shared<std::vector<JSVariant>>(toVariantVec(rt, p));
38
+ commands->push_back({query, params});
39
+ }
40
+ } else {
41
+ auto params = std::make_shared<std::vector<JSVariant>>(
42
+ toVariantVec(rt, commandParams));
43
+ commands->push_back({query, params});
55
44
  }
56
-
57
- try
58
- {
59
- int affectedRows = 0;
60
- sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
61
- for(int i = 0; i < commandCount; i++) {
62
- auto command = commands->at(i);
63
- // We do not provide a datastructure to receive query data because we don't need/want to handle this results in a batch execution
64
- auto result = sqliteExecute(dbName, command.sql, command.params.get(), nullptr, nullptr);
65
- if(result.type == SQLiteError)
66
- {
67
- sqliteExecuteLiteral(dbName, "ROLLBACK");
68
- return BatchResult {
69
- .type = SQLiteError,
70
- .message = result.message,
71
- };
72
- } else
73
- {
74
- affectedRows += result.affectedRows;
75
- }
76
- }
77
- sqliteExecuteLiteral(dbName, "COMMIT");
78
- return BatchResult {
79
- .type = SQLiteOk,
80
- .affectedRows = affectedRows,
81
- .commands = static_cast<int>(commandCount),
82
- };
83
- } catch(std::exception &exc)
84
- {
45
+ }
46
+ }
47
+
48
+ BatchResult sqliteExecuteBatch(std::string dbName,
49
+ std::vector<BatchArguments> *commands) {
50
+ size_t commandCount = commands->size();
51
+ if (commandCount <= 0) {
52
+ return BatchResult{
53
+ .type = SQLiteError,
54
+ .message = "No SQL commands provided",
55
+ };
56
+ }
57
+
58
+ try {
59
+ int affectedRows = 0;
60
+ sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
61
+ for (int i = 0; i < commandCount; i++) {
62
+ auto command = commands->at(i);
63
+ // We do not provide a datastructure to receive query data because we
64
+ // don't need/want to handle this results in a batch execution
65
+ auto result = sqliteExecute(dbName, command.sql, command.params.get(),
66
+ nullptr, nullptr);
67
+ if (result.type == SQLiteError) {
85
68
  sqliteExecuteLiteral(dbName, "ROLLBACK");
86
- return BatchResult {
69
+ return BatchResult{
87
70
  .type = SQLiteError,
88
- .message = exc.what(),
71
+ .message = result.message,
89
72
  };
73
+ } else {
74
+ affectedRows += result.affectedRows;
75
+ }
90
76
  }
77
+ sqliteExecuteLiteral(dbName, "COMMIT");
78
+ return BatchResult{
79
+ .type = SQLiteOk,
80
+ .affectedRows = affectedRows,
81
+ .commands = static_cast<int>(commandCount),
82
+ };
83
+ } catch (std::exception &exc) {
84
+ sqliteExecuteLiteral(dbName, "ROLLBACK");
85
+ return BatchResult{
86
+ .type = SQLiteError,
87
+ .message = exc.what(),
88
+ };
89
+ }
91
90
  }
92
91
 
93
-
94
- }
92
+ } // namespace opsqlite
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * SQL Batch execution implementation using default sqliteBridge implementation
3
- */
4
- #include "utils.h"
3
+ */
5
4
  #include "bridge.h"
6
5
  #include "types.h"
6
+ #include "utils.h"
7
7
 
8
8
  namespace opsqlite {
9
9
 
@@ -17,13 +17,14 @@ struct BatchArguments {
17
17
  /**
18
18
  * Local Helper method to translate JSI objects BatchArguments data structure
19
19
  * MUST be called in the JavaScript Thread
20
- */
21
- void toBatchArguments(jsi::Runtime &rt, jsi::Array const &batchParams, std::vector<BatchArguments> *commands);
20
+ */
21
+ void toBatchArguments(jsi::Runtime &rt, jsi::Array const &batchParams,
22
+ std::vector<BatchArguments> *commands);
22
23
 
23
24
  /**
24
25
  * Execute a batch of commands in a exclusive transaction
25
- */
26
- BatchResult sqliteExecuteBatch(std::string dbName, std::vector<BatchArguments> *commands);
27
-
28
- }
26
+ */
27
+ BatchResult sqliteExecuteBatch(std::string dbName,
28
+ std::vector<BatchArguments> *commands);
29
29
 
30
+ } // namespace opsqlite