-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHybridNitroSQLite.cpp
More file actions
144 lines (116 loc) · 5.22 KB
/
HybridNitroSQLite.cpp
File metadata and controls
144 lines (116 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "HybridNitroSQLite.hpp"
#include "HybridNitroSQLiteQueryResult.hpp"
#include "NitroSQLiteException.hpp"
#include "importSqlFile.hpp"
#include "logs.hpp"
#include "macros.hpp"
#include "operations.hpp"
#include "sqliteExecuteBatch.hpp"
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <variant>
#include <vector>
namespace margelo::nitro::rnnitrosqlite {
// Copy any JS-backed ArrayBuffers on the JS thread so they can be safely
// accessed from the background thread used by Promise::async.
static std::optional<SQLiteQueryParams> copyArrayBufferParamsForBackground(const std::optional<SQLiteQueryParams>& params) {
if (!params) {
return std::nullopt;
}
SQLiteQueryParams copiedParams;
copiedParams.reserve(params->size());
for (const auto& value : *params) {
if (std::holds_alternative<std::shared_ptr<ArrayBuffer>>(value)) {
const auto& buffer = std::get<std::shared_ptr<ArrayBuffer>>(value);
const auto copiedBuffer = ArrayBuffer::copy(buffer);
copiedParams.push_back(copiedBuffer);
} else {
copiedParams.push_back(value);
}
}
return copiedParams;
}
// Overload for batch execution: copy ArrayBuffer params inside each BatchQuery.
static std::vector<BatchQuery> copyArrayBufferParamsForBackground(const std::vector<BatchQuery>& commands) {
std::vector<BatchQuery> copiedCommands;
copiedCommands.reserve(commands.size());
for (const auto& command : commands) {
BatchQuery copiedCommand = command;
if (command.params) {
copiedCommand.params = copyArrayBufferParamsForBackground(command.params);
}
copiedCommands.push_back(std::move(copiedCommand));
}
return copiedCommands;
}
const std::string getDocPath(const std::optional<std::string>& location) {
std::string tempDocPath = std::string(HybridNitroSQLite::docPath);
if (location) {
tempDocPath = tempDocPath + "/" + *location;
}
return tempDocPath;
}
void HybridNitroSQLite::open(const std::string& dbName, const std::optional<std::string>& location) {
const auto docPath = getDocPath(location);
sqliteOpenDb(dbName, docPath);
}
void HybridNitroSQLite::close(const std::string& dbName) {
sqliteCloseDb(dbName);
};
void HybridNitroSQLite::drop(const std::string& dbName, const std::optional<std::string>& location) {
const auto docPath = getDocPath(location);
sqliteRemoveDb(dbName, docPath);
};
void HybridNitroSQLite::attach(const std::string& mainDbName, const std::string& dbNameToAttach, const std::string& alias,
const std::optional<std::string>& location) {
std::string tempDocPath = std::string(docPath);
if (location) {
tempDocPath = tempDocPath + "/" + *location;
}
sqliteAttachDb(mainDbName, tempDocPath, dbNameToAttach, alias);
};
void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string& alias) {
sqliteDetachDb(mainDbName, alias);
};
std::shared_ptr<HybridNitroSQLiteQueryResultSpec> HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
const std::optional<SQLiteQueryParams>& params) {
return sqliteExecute(dbName, query, params);
};
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) {
const auto copiedParams = copyArrayBufferParamsForBackground(params);
return Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>::async(
[=, this]() -> std::shared_ptr<HybridNitroSQLiteQueryResultSpec> {
auto result = sqliteExecute(dbName, query, copiedParams);
return result;
});
};
BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
const auto commands = batchParamsToCommands(batchParams);
auto result = sqliteExecuteBatch(dbName, commands);
return BatchQueryResult(result.rowsAffected);
};
std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync(const std::string& dbName,
const std::vector<BatchQueryCommand>& batchParams) {
// Convert BatchQueryCommand objects on the JS thread and copy any JS-backed
// ArrayBuffers into native buffers before going off-thread.
const auto commands = batchParamsToCommands(batchParams);
const auto copiedCommands = copyArrayBufferParamsForBackground(commands);
return Promise<BatchQueryResult>::async([=, this]() -> BatchQueryResult {
auto result = sqliteExecuteBatch(dbName, copiedCommands);
return BatchQueryResult(result.rowsAffected);
});
};
FileLoadResult HybridNitroSQLite::loadFile(const std::string& dbName, const std::string& location) {
const auto result = importSqlFile(dbName, location);
return FileLoadResult(result.commands, result.rowsAffected);
};
std::shared_ptr<Promise<FileLoadResult>> HybridNitroSQLite::loadFileAsync(const std::string& dbName, const std::string& location) {
return Promise<FileLoadResult>::async([=, this]() -> FileLoadResult {
auto result = loadFile(dbName, location);
return result;
});
};
} // namespace margelo::nitro::rnnitrosqlite