react-native-nitro-sqlite 8.2.1-nitro.1
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/RNNitroSQLite.podspec +59 -0
- package/android/.gradle/8.9/checksums/checksums.lock +0 -0
- package/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
- package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/8.9/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/CMakeLists.txt +38 -0
- package/android/build.gradle +119 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/cpp/cpp-adapter.cpp +21 -0
- package/android/src/main/java/com/margelo/rnnitrosqlite/RNNitroSQLiteOnLoadModule.java +57 -0
- package/android/src/main/java/com/margelo/rnnitrosqlite/RNNitroSQLitePackage.java +48 -0
- package/cpp/NitroSQLiteException.hpp +63 -0
- package/cpp/ThreadPool.cpp +91 -0
- package/cpp/ThreadPool.hpp +50 -0
- package/cpp/importSqlFile.cpp +48 -0
- package/cpp/importSqlFile.hpp +15 -0
- package/cpp/logs.hpp +38 -0
- package/cpp/macros.hpp +13 -0
- package/cpp/operations.cpp +296 -0
- package/cpp/operations.hpp +24 -0
- package/cpp/specs/HybridNativeQueryResult.cpp +20 -0
- package/cpp/specs/HybridNativeQueryResult.hpp +35 -0
- package/cpp/specs/HybridNitroSQLite.cpp +132 -0
- package/cpp/specs/HybridNitroSQLite.hpp +36 -0
- package/cpp/sqlite/sqlite3.c +237594 -0
- package/cpp/sqlite/sqlite3.h +12556 -0
- package/cpp/sqliteExecuteBatch.cpp +70 -0
- package/cpp/sqliteExecuteBatch.hpp +30 -0
- package/cpp/types.hpp +53 -0
- package/cpp/utils.hpp +61 -0
- package/ios/NitroSQLite.xcodeproj/project.pbxproj +447 -0
- package/ios/OnLoad.mm +41 -0
- package/ios/RNNitroSQLite-Swift.h +1 -0
- package/lib/commonjs/index.js +58 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/nitro.js +10 -0
- package/lib/commonjs/nitro.js.map +1 -0
- package/lib/commonjs/operations/execute.js +35 -0
- package/lib/commonjs/operations/execute.js.map +1 -0
- package/lib/commonjs/operations/session.js +39 -0
- package/lib/commonjs/operations/session.js.map +1 -0
- package/lib/commonjs/operations/transaction.js +95 -0
- package/lib/commonjs/operations/transaction.js.map +1 -0
- package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.ios.js +11 -0
- package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.ios.js.map +1 -0
- package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.js +9 -0
- package/lib/commonjs/specs/NativeNitroSQLiteOnLoad.js.map +1 -0
- package/lib/commonjs/specs/NativeQueryResult.nitro.js +6 -0
- package/lib/commonjs/specs/NativeQueryResult.nitro.js.map +1 -0
- package/lib/commonjs/specs/NitroSQLite.nitro.js +6 -0
- package/lib/commonjs/specs/NitroSQLite.nitro.js.map +1 -0
- package/lib/commonjs/typeORM.js +62 -0
- package/lib/commonjs/typeORM.js.map +1 -0
- package/lib/commonjs/types.js +35 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/index.js +23 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/nitro.js +4 -0
- package/lib/module/nitro.js.map +1 -0
- package/lib/module/operations/execute.js +28 -0
- package/lib/module/operations/execute.js.map +1 -0
- package/lib/module/operations/session.js +31 -0
- package/lib/module/operations/session.js.map +1 -0
- package/lib/module/operations/transaction.js +88 -0
- package/lib/module/operations/transaction.js.map +1 -0
- package/lib/module/specs/NativeNitroSQLiteOnLoad.ios.js +5 -0
- package/lib/module/specs/NativeNitroSQLiteOnLoad.ios.js.map +1 -0
- package/lib/module/specs/NativeNitroSQLiteOnLoad.js +3 -0
- package/lib/module/specs/NativeNitroSQLiteOnLoad.js.map +1 -0
- package/lib/module/specs/NativeQueryResult.nitro.js +2 -0
- package/lib/module/specs/NativeQueryResult.nitro.js.map +1 -0
- package/lib/module/specs/NitroSQLite.nitro.js +2 -0
- package/lib/module/specs/NitroSQLite.nitro.js.map +1 -0
- package/lib/module/typeORM.js +54 -0
- package/lib/module/typeORM.js.map +1 -0
- package/lib/module/types.js +33 -0
- package/lib/module/types.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/typescript/index.d.ts +30 -0
- package/lib/typescript/nitro.d.ts +7 -0
- package/lib/typescript/operations/execute.d.ts +3 -0
- package/lib/typescript/operations/session.d.ts +4 -0
- package/lib/typescript/operations/transaction.d.ts +5 -0
- package/lib/typescript/specs/NativeNitroSQLiteOnLoad.d.ts +6 -0
- package/lib/typescript/specs/NativeNitroSQLiteOnLoad.ios.d.ts +3 -0
- package/lib/typescript/specs/NativeQueryResult.nitro.d.ts +37 -0
- package/lib/typescript/specs/NitroSQLite.nitro.d.ts +19 -0
- package/lib/typescript/typeORM.d.ts +19 -0
- package/lib/typescript/types.d.ts +79 -0
- package/nitrogen/generated/android/RNNitroSQLite+autolinking.cmake +48 -0
- package/nitrogen/generated/android/RNNitroSQLite+autolinking.gradle +25 -0
- package/nitrogen/generated/android/RNNitroSQLiteOnLoad.cpp +40 -0
- package/nitrogen/generated/android/RNNitroSQLiteOnLoad.hpp +25 -0
- package/nitrogen/generated/android/RNNitroSQLiteOnLoad.kt +1 -0
- package/nitrogen/generated/ios/RNNitroSQLite+autolinking.rb +56 -0
- package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Bridge.cpp +8 -0
- package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Bridge.hpp +303 -0
- package/nitrogen/generated/ios/RNNitroSQLite-Swift-Cxx-Umbrella.hpp +67 -0
- package/nitrogen/generated/ios/RNNitroSQLiteAutolinking.mm +35 -0
- package/nitrogen/generated/ios/RNNitroSQLiteAutolinking.swift +10 -0
- package/nitrogen/generated/shared/c++/BatchQueryCommand.hpp +77 -0
- package/nitrogen/generated/shared/c++/BatchQueryResult.hpp +68 -0
- package/nitrogen/generated/shared/c++/ColumnType.hpp +68 -0
- package/nitrogen/generated/shared/c++/FileLoadResult.hpp +72 -0
- package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridNativeQueryResultSpec.hpp +74 -0
- package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.cpp +31 -0
- package/nitrogen/generated/shared/c++/HybridNitroSQLiteSpec.hpp +91 -0
- package/nitrogen/generated/shared/c++/SQLiteQueryColumnMetadata.hpp +78 -0
- package/package.json +98 -0
- package/src/index.ts +26 -0
- package/src/nitro.ts +11 -0
- package/src/operations/execute.ts +45 -0
- package/src/operations/session.ts +58 -0
- package/src/operations/transaction.ts +137 -0
- package/src/specs/NativeNitroSQLiteOnLoad.ios.ts +7 -0
- package/src/specs/NativeNitroSQLiteOnLoad.ts +7 -0
- package/src/specs/NativeQueryResult.nitro.ts +44 -0
- package/src/specs/NitroSQLite.nitro.ts +39 -0
- package/src/typeORM.ts +101 -0
- package/src/types.ts +105 -0
package/cpp/macros.hpp
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#ifndef macros_h
|
|
2
|
+
#define macros_h
|
|
3
|
+
|
|
4
|
+
#define HOSTFN(name, basecount) \
|
|
5
|
+
jsi::Function::createFromHostFunction( \
|
|
6
|
+
rt, \
|
|
7
|
+
jsi::PropNameID::forAscii(rt, name), \
|
|
8
|
+
basecount, \
|
|
9
|
+
[=](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
|
|
10
|
+
|
|
11
|
+
#define JSIFN(capture) capture(jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count)->jsi::Value
|
|
12
|
+
|
|
13
|
+
#endif /* macros_h */
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
#include "operations.hpp"
|
|
2
|
+
#include "logs.hpp"
|
|
3
|
+
#include "utils.hpp"
|
|
4
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <ctime>
|
|
7
|
+
#include <iostream>
|
|
8
|
+
#include <map>
|
|
9
|
+
#include <sqlite3.h>
|
|
10
|
+
#include <sstream>
|
|
11
|
+
#include <unistd.h>
|
|
12
|
+
#include "NitroSQLiteException.hpp"
|
|
13
|
+
|
|
14
|
+
using namespace facebook;
|
|
15
|
+
using namespace margelo::nitro;
|
|
16
|
+
using namespace margelo::nitro::rnnitrosqlite;
|
|
17
|
+
|
|
18
|
+
namespace margelo::rnnitrosqlite {
|
|
19
|
+
|
|
20
|
+
std::map<std::string, sqlite3*> dbMap = std::map<std::string, sqlite3*>();
|
|
21
|
+
|
|
22
|
+
void sqliteOpenDb(const std::string& dbName, const std::string& docPath) {
|
|
23
|
+
std::string dbPath = get_db_path(dbName, docPath);
|
|
24
|
+
|
|
25
|
+
int sqlOpenFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
|
|
26
|
+
|
|
27
|
+
sqlite3* db;
|
|
28
|
+
int exit = 0;
|
|
29
|
+
exit = sqlite3_open_v2(dbPath.c_str(), &db, sqlOpenFlags, nullptr);
|
|
30
|
+
|
|
31
|
+
if (exit != SQLITE_OK) {
|
|
32
|
+
throw NitroSQLiteException(NitroSQLiteExceptionType::DatabaseCannotBeOpened, sqlite3_errmsg(db));
|
|
33
|
+
} else {
|
|
34
|
+
dbMap[dbName] = db;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void sqliteCloseDb(const std::string& dbName) {
|
|
39
|
+
|
|
40
|
+
if (dbMap.count(dbName) == 0) {
|
|
41
|
+
throw NitroSQLiteException::DatabaseNotOpen(dbName);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
sqlite3* db = dbMap[dbName];
|
|
45
|
+
|
|
46
|
+
sqlite3_close_v2(db);
|
|
47
|
+
|
|
48
|
+
dbMap.erase(dbName);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void sqliteCloseAll() {
|
|
52
|
+
for (auto const& x : dbMap) {
|
|
53
|
+
// In certain cases, this will return SQLITE_OK, mark the database connection as an unusable "zombie",
|
|
54
|
+
// and deallocate the connection later.
|
|
55
|
+
sqlite3_close_v2(x.second);
|
|
56
|
+
}
|
|
57
|
+
dbMap.clear();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void sqliteAttachDb(const std::string& mainDBName, const std::string& docPath, const std::string& databaseToAttach,
|
|
61
|
+
const std::string& alias) {
|
|
62
|
+
/**
|
|
63
|
+
* There is no need to check if mainDBName is opened because sqliteExecuteLiteral will do that.
|
|
64
|
+
* */
|
|
65
|
+
std::string dbPath = get_db_path(databaseToAttach, docPath);
|
|
66
|
+
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
sqliteExecuteLiteral(mainDBName, statement);
|
|
70
|
+
} catch (NitroSQLiteException& e) {
|
|
71
|
+
throw NitroSQLiteException(NitroSQLiteExceptionType::UnableToAttachToDatabase, mainDBName + " was unable to attach another database: " + std::string(e.what()));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
void sqliteDetachDb(const std::string& mainDBName, const std::string& alias) {
|
|
76
|
+
/**
|
|
77
|
+
* There is no need to check if mainDBName is opened because sqliteExecuteLiteral will do that.
|
|
78
|
+
* */
|
|
79
|
+
std::string statement = "DETACH DATABASE " + alias;
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
sqliteExecuteLiteral(mainDBName, statement);
|
|
83
|
+
} catch (NitroSQLiteException& e) {
|
|
84
|
+
throw NitroSQLiteException(NitroSQLiteExceptionType::UnableToAttachToDatabase, mainDBName + " was unable to detach database: " + std::string(e.what()));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void sqliteRemoveDb(const std::string& dbName, const std::string& docPath) {
|
|
89
|
+
if (dbMap.count(dbName) == 1) {
|
|
90
|
+
sqliteCloseDb(dbName);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
std::string dbFilePath = get_db_path(dbName, docPath);
|
|
94
|
+
if (!file_exists(dbFilePath)) {
|
|
95
|
+
throw NitroSQLiteException::DatabaseFileNotFound(dbFilePath);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
remove(dbFilePath.c_str());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
|
|
102
|
+
for (int valueIndex = 0; valueIndex < values.size(); valueIndex++) {
|
|
103
|
+
int sqliteIndex = valueIndex+1;
|
|
104
|
+
SQLiteValue value = values.at(valueIndex);
|
|
105
|
+
|
|
106
|
+
// if (std::holds_alternative<std::monostate>(value))
|
|
107
|
+
// {
|
|
108
|
+
// sqlite3_bind_null(statement, sqliteIndex);
|
|
109
|
+
// }
|
|
110
|
+
if (std::holds_alternative<bool>(value)) {
|
|
111
|
+
sqlite3_bind_int(statement, sqliteIndex, std::get<bool>(value));
|
|
112
|
+
} else if (std::holds_alternative<double>(value)) {
|
|
113
|
+
sqlite3_bind_double(statement, sqliteIndex, std::get<double>(value));
|
|
114
|
+
} else if (std::holds_alternative<int64_t>(value)) {
|
|
115
|
+
sqlite3_bind_int64(statement, sqliteIndex, std::get<int64_t>(value));
|
|
116
|
+
} else if (std::holds_alternative<std::string>(value)) {
|
|
117
|
+
const auto stringValue = std::get<std::string>(value);
|
|
118
|
+
sqlite3_bind_text(statement, sqliteIndex, stringValue.c_str(), stringValue.length(), SQLITE_TRANSIENT);
|
|
119
|
+
} else if (std::holds_alternative<std::shared_ptr<ArrayBuffer>>(value)) {
|
|
120
|
+
const auto arrayBufferValue = std::get<std::shared_ptr<ArrayBuffer>>(value);
|
|
121
|
+
sqlite3_bind_blob(statement, sqliteIndex, arrayBufferValue->data(), arrayBufferValue->size(), SQLITE_STATIC);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) {
|
|
127
|
+
if (dbMap.count(dbName) == 0) {
|
|
128
|
+
throw NitroSQLiteException::DatabaseNotOpen(dbName);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
auto db = dbMap[dbName];
|
|
132
|
+
|
|
133
|
+
sqlite3_stmt* statement;
|
|
134
|
+
int statementStatus = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);
|
|
135
|
+
if (statementStatus == SQLITE_OK) // statement is correct, bind the passed parameters
|
|
136
|
+
{
|
|
137
|
+
if (params) {
|
|
138
|
+
bindStatement(statement, *params);
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
auto isConsuming = true;
|
|
145
|
+
auto isFailed = false;
|
|
146
|
+
|
|
147
|
+
int result, i, count, column_type;
|
|
148
|
+
std::string column_name;
|
|
149
|
+
ColumnType column_declared_type;
|
|
150
|
+
SQLiteQueryResultRow row;
|
|
151
|
+
|
|
152
|
+
auto results = std::make_unique<SQLiteQueryResults>();
|
|
153
|
+
auto metadata = new SQLiteQueryTableMetadata();
|
|
154
|
+
|
|
155
|
+
while (isConsuming) {
|
|
156
|
+
result = sqlite3_step(statement);
|
|
157
|
+
|
|
158
|
+
switch (result) {
|
|
159
|
+
case SQLITE_ROW:
|
|
160
|
+
i = 0;
|
|
161
|
+
row = std::unordered_map<std::string, SQLiteValue>();
|
|
162
|
+
count = sqlite3_column_count(statement);
|
|
163
|
+
|
|
164
|
+
while (i < count) {
|
|
165
|
+
column_type = sqlite3_column_type(statement, i);
|
|
166
|
+
column_name = sqlite3_column_name(statement, i);
|
|
167
|
+
switch (column_type) {
|
|
168
|
+
|
|
169
|
+
case SQLITE_INTEGER: {
|
|
170
|
+
auto column_value = sqlite3_column_int64(statement, i);
|
|
171
|
+
row[column_name] = column_value;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case SQLITE_FLOAT: {
|
|
175
|
+
auto column_value = sqlite3_column_double(statement, i);
|
|
176
|
+
row[column_name] = column_value;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
case SQLITE_TEXT: {
|
|
180
|
+
auto column_value = reinterpret_cast<const char*>(sqlite3_column_text(statement, i));
|
|
181
|
+
sqlite3_column_bytes(statement, i);
|
|
182
|
+
row[column_name] = column_value;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case SQLITE_BLOB: {
|
|
186
|
+
int blob_size = sqlite3_column_bytes(statement, i);
|
|
187
|
+
const void* blob = sqlite3_column_blob(statement, i);
|
|
188
|
+
uint8_t* data = new uint8_t[blob_size];
|
|
189
|
+
memcpy(data, blob, blob_size);
|
|
190
|
+
row[column_name] = ArrayBuffer::makeBuffer(data, blob_size, [&data]() -> void { delete[] data; });
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case SQLITE_NULL:
|
|
194
|
+
// Intentionally left blank to switch to default case
|
|
195
|
+
default:
|
|
196
|
+
// row[column_name] = std::monostate();
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
i++;
|
|
200
|
+
}
|
|
201
|
+
results->push_back(std::move(row));
|
|
202
|
+
break;
|
|
203
|
+
case SQLITE_DONE:
|
|
204
|
+
i = 0;
|
|
205
|
+
count = sqlite3_column_count(statement);
|
|
206
|
+
while (i < count) {
|
|
207
|
+
column_name = sqlite3_column_name(statement, i);
|
|
208
|
+
const char* tp = sqlite3_column_decltype(statement, i);
|
|
209
|
+
column_declared_type = mapSQLiteTypeToColumnType(tp);
|
|
210
|
+
auto columnMeta = SQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i);
|
|
211
|
+
metadata->insert({column_name, columnMeta});
|
|
212
|
+
i++;
|
|
213
|
+
}
|
|
214
|
+
isConsuming = false;
|
|
215
|
+
break;
|
|
216
|
+
default:
|
|
217
|
+
isFailed = true;
|
|
218
|
+
isConsuming = false;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
sqlite3_finalize(statement);
|
|
223
|
+
|
|
224
|
+
if (isFailed) {
|
|
225
|
+
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
int rowsAffected = sqlite3_changes(db);
|
|
229
|
+
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
|
|
230
|
+
auto meta = std::make_unique<std::optional<SQLiteQueryTableMetadata>>(
|
|
231
|
+
metadata && metadata->size() > 0
|
|
232
|
+
? std::make_optional(std::move(*metadata))
|
|
233
|
+
: std::nullopt
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
.rowsAffected = rowsAffected,
|
|
238
|
+
.insertId = static_cast<double>(latestInsertRowId),
|
|
239
|
+
.results = std::move(results),
|
|
240
|
+
.metadata = std::move(meta)
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query) {
|
|
245
|
+
// Check if db connection is opened
|
|
246
|
+
if (dbMap.count(dbName) == 0) {
|
|
247
|
+
throw NitroSQLiteException::DatabaseNotOpen(dbName);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
sqlite3* db = dbMap[dbName];
|
|
251
|
+
|
|
252
|
+
// SQLite statements need to be compiled before executed
|
|
253
|
+
sqlite3_stmt* statement;
|
|
254
|
+
|
|
255
|
+
// Compile and move result into statement memory spot
|
|
256
|
+
int statementStatus = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);
|
|
257
|
+
|
|
258
|
+
if (statementStatus != SQLITE_OK) // statemnet is correct, bind the passed parameters
|
|
259
|
+
{
|
|
260
|
+
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
bool isConsuming = true;
|
|
264
|
+
bool isFailed = false;
|
|
265
|
+
|
|
266
|
+
int result, i, count, column_type;
|
|
267
|
+
std::string column_name;
|
|
268
|
+
|
|
269
|
+
while (isConsuming) {
|
|
270
|
+
result = sqlite3_step(statement);
|
|
271
|
+
|
|
272
|
+
switch (result) {
|
|
273
|
+
case SQLITE_ROW:
|
|
274
|
+
isConsuming = true;
|
|
275
|
+
break;
|
|
276
|
+
|
|
277
|
+
case SQLITE_DONE:
|
|
278
|
+
isConsuming = false;
|
|
279
|
+
break;
|
|
280
|
+
|
|
281
|
+
default:
|
|
282
|
+
isFailed = true;
|
|
283
|
+
isConsuming = false;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
sqlite3_finalize(statement);
|
|
288
|
+
|
|
289
|
+
if (isFailed) {
|
|
290
|
+
throw NitroSQLiteException::SqlExecution(sqlite3_errmsg(db));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return {.rowsAffected = sqlite3_changes(db)};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
} // namespace margelo::rnnitrosqlite
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
|
|
5
|
+
namespace margelo::rnnitrosqlite {
|
|
6
|
+
|
|
7
|
+
void sqliteOpenDb(const std::string& dbName, const std::string& docPath);
|
|
8
|
+
|
|
9
|
+
void sqliteCloseDb(const std::string& dbName);
|
|
10
|
+
|
|
11
|
+
void sqliteRemoveDb(const std::string& dbName, const std::string& docPath);
|
|
12
|
+
|
|
13
|
+
void sqliteAttachDb(const std::string& mainDBName, const std::string& docPath, const std::string& databaseToAttach,
|
|
14
|
+
const std::string& alias);
|
|
15
|
+
|
|
16
|
+
void sqliteDetachDb(const std::string& mainDBName, const std::string& alias);
|
|
17
|
+
|
|
18
|
+
SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params);
|
|
19
|
+
|
|
20
|
+
SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query);
|
|
21
|
+
|
|
22
|
+
void sqliteCloseAll();
|
|
23
|
+
|
|
24
|
+
} // namespace margelo::rnnitrosqlite
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#include "HybridNativeQueryResult.hpp"
|
|
2
|
+
|
|
3
|
+
namespace margelo::nitro::rnnitrosqlite {
|
|
4
|
+
|
|
5
|
+
std::optional<double> HybridNativeQueryResult::getInsertId() {
|
|
6
|
+
return this->_insertId;
|
|
7
|
+
}
|
|
8
|
+
double HybridNativeQueryResult::getRowsAffected() {
|
|
9
|
+
return this->_rowsAffected;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
SQLiteQueryResults HybridNativeQueryResult::getResults() {
|
|
13
|
+
return this->_results;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
std::optional<SQLiteQueryTableMetadata> HybridNativeQueryResult::getMetadata() {
|
|
17
|
+
return this->_metadata;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
} // namespace margelo::nitro::rnnitrosqlite
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "HybridNativeQueryResultSpec.hpp"
|
|
4
|
+
#include "types.hpp"
|
|
5
|
+
#include <map>
|
|
6
|
+
|
|
7
|
+
using namespace margelo::rnnitrosqlite;
|
|
8
|
+
|
|
9
|
+
namespace margelo::nitro::rnnitrosqlite {
|
|
10
|
+
|
|
11
|
+
class HybridNativeQueryResult : public HybridNativeQueryResultSpec {
|
|
12
|
+
public:
|
|
13
|
+
HybridNativeQueryResult() : HybridObject(TAG) {}
|
|
14
|
+
HybridNativeQueryResult(std::optional<double> insertId, int rowsAffected, SQLiteQueryResults& results, std::optional<SQLiteQueryTableMetadata>& metadata)
|
|
15
|
+
: HybridObject(TAG),
|
|
16
|
+
_insertId(insertId),
|
|
17
|
+
_rowsAffected(rowsAffected),
|
|
18
|
+
_results(std::move(results)),
|
|
19
|
+
_metadata(std::move(metadata)) {}
|
|
20
|
+
|
|
21
|
+
private:
|
|
22
|
+
std::optional<double> _insertId;
|
|
23
|
+
int _rowsAffected;
|
|
24
|
+
SQLiteQueryResults _results;
|
|
25
|
+
std::optional<SQLiteQueryTableMetadata> _metadata;
|
|
26
|
+
|
|
27
|
+
public:
|
|
28
|
+
// Properties
|
|
29
|
+
std::optional<double> getInsertId() override;
|
|
30
|
+
double getRowsAffected() override;
|
|
31
|
+
SQLiteQueryResults getResults() override;
|
|
32
|
+
std::optional<SQLiteQueryTableMetadata> getMetadata() override;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
} // namespace margelo::nitro::rnnitrosqlite
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#include "HybridNitroSQLite.hpp"
|
|
2
|
+
#include "HybridNativeQueryResult.hpp"
|
|
3
|
+
#include "ThreadPool.hpp"
|
|
4
|
+
#include "NitroSQLiteException.hpp"
|
|
5
|
+
#include "logs.hpp"
|
|
6
|
+
#include "macros.hpp"
|
|
7
|
+
#include "sqliteExecuteBatch.hpp"
|
|
8
|
+
#include "importSqlFile.hpp"
|
|
9
|
+
#include "operations.hpp"
|
|
10
|
+
#include <iostream>
|
|
11
|
+
#include <map>
|
|
12
|
+
#include <string>
|
|
13
|
+
#include <vector>
|
|
14
|
+
|
|
15
|
+
namespace margelo::nitro::rnnitrosqlite {
|
|
16
|
+
|
|
17
|
+
auto pool = std::make_shared<margelo::rnnitrosqlite::ThreadPool>();
|
|
18
|
+
|
|
19
|
+
const std::string getDocPath(const std::optional<std::string>& location) {
|
|
20
|
+
std::string tempDocPath = std::string(HybridNitroSQLite::docPath);
|
|
21
|
+
if (location) {
|
|
22
|
+
tempDocPath = tempDocPath + "/" + *location;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return tempDocPath;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void HybridNitroSQLite::open(const std::string& dbName, const std::optional<std::string>& location) {
|
|
29
|
+
const auto docPath = getDocPath(location);
|
|
30
|
+
sqliteOpenDb(dbName, docPath);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void HybridNitroSQLite::close(const std::string& dbName) {
|
|
34
|
+
sqliteCloseDb(dbName);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
void HybridNitroSQLite::drop(const std::string& dbName, const std::optional<std::string>& location) {
|
|
38
|
+
const auto docPath = getDocPath(location);
|
|
39
|
+
sqliteRemoveDb(dbName, docPath);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
void HybridNitroSQLite::attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias,
|
|
43
|
+
const std::optional<std::string>& location) {
|
|
44
|
+
std::string tempDocPath = std::string(docPath);
|
|
45
|
+
if (location) {
|
|
46
|
+
tempDocPath = tempDocPath + "/" + *location;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
sqliteAttachDb(mainDbName, tempDocPath, dbNameToAttach, alias);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string& alias) {
|
|
53
|
+
sqliteDetachDb(mainDbName, alias);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
using ExecuteQueryResult = std::shared_ptr<HybridNativeQueryResultSpec>;
|
|
57
|
+
|
|
58
|
+
ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
|
|
59
|
+
const std::optional<SQLiteQueryParams>& params) {
|
|
60
|
+
auto result = sqliteExecute(dbName, query, params);
|
|
61
|
+
return std::make_shared<HybridNativeQueryResult>(result.insertId, result.rowsAffected, *result.results, *result.metadata);
|
|
62
|
+
};
|
|
63
|
+
|
|
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;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
|
|
84
|
+
const auto commands = batchParamsToCommands(batchParams);
|
|
85
|
+
|
|
86
|
+
auto result = sqliteExecuteBatch(dbName, commands);
|
|
87
|
+
return BatchQueryResult(result.rowsAffected);
|
|
88
|
+
};
|
|
89
|
+
|
|
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;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
FileLoadResult HybridNitroSQLite::loadFile(const std::string& dbName, const std::string& location) {
|
|
110
|
+
const auto result = importSqlFile(dbName, location);
|
|
111
|
+
return FileLoadResult(result.commands, result.rowsAffected);
|
|
112
|
+
};
|
|
113
|
+
|
|
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;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
} // namespace margelo::nitro::rnnitrosqlite
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "HybridNitroSQLiteSpec.hpp"
|
|
4
|
+
#include "HybridNativeQueryResultSpec.hpp"
|
|
5
|
+
#include "types.hpp"
|
|
6
|
+
|
|
7
|
+
using namespace margelo::rnnitrosqlite;
|
|
8
|
+
|
|
9
|
+
namespace margelo::nitro::rnnitrosqlite {
|
|
10
|
+
|
|
11
|
+
class HybridNitroSQLite : public HybridNitroSQLiteSpec {
|
|
12
|
+
public:
|
|
13
|
+
HybridNitroSQLite() : HybridObject(TAG) {}
|
|
14
|
+
|
|
15
|
+
public:
|
|
16
|
+
static std::string docPath;
|
|
17
|
+
|
|
18
|
+
public:
|
|
19
|
+
// Methods
|
|
20
|
+
void open(const std::string& dbName, const std::optional<std::string>& location) override;
|
|
21
|
+
void close(const std::string& dbName) override;
|
|
22
|
+
void drop(const std::string& dbName, const std::optional<std::string>& location) override;
|
|
23
|
+
void attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias,
|
|
24
|
+
const std::optional<std::string>& location) override;
|
|
25
|
+
void detach(const std::string& mainDbName, const std::string& alias) override;
|
|
26
|
+
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;
|
|
28
|
+
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;
|
|
30
|
+
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;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
inline std::string HybridNitroSQLite::docPath = "";
|
|
35
|
+
|
|
36
|
+
} // namespace margelo::nitro::rnnitrosqlite
|