|
8 | 8 | #include "sqliteExecuteBatch.hpp" |
9 | 9 | #include <iostream> |
10 | 10 | #include <map> |
| 11 | +#include <optional> |
11 | 12 | #include <string> |
| 13 | +#include <variant> |
12 | 14 | #include <vector> |
13 | 15 |
|
14 | 16 | namespace margelo::nitro::rnnitrosqlite { |
@@ -36,6 +38,24 @@ static std::optional<SQLiteQueryParams> copyArrayBufferParamsForBackground(const |
36 | 38 | return copiedParams; |
37 | 39 | } |
38 | 40 |
|
| 41 | +// Overload for batch execution: copy ArrayBuffer params inside each BatchQuery. |
| 42 | +static std::vector<BatchQuery> copyArrayBufferParamsForBackground(const std::vector<BatchQuery>& commands) { |
| 43 | + std::vector<BatchQuery> copiedCommands; |
| 44 | + copiedCommands.reserve(commands.size()); |
| 45 | + |
| 46 | + for (const auto& command : commands) { |
| 47 | + BatchQuery copiedCommand = command; |
| 48 | + |
| 49 | + if (command.params) { |
| 50 | + copiedCommand.params = copyArrayBufferParamsForBackground(command.params); |
| 51 | + } |
| 52 | + |
| 53 | + copiedCommands.push_back(std::move(copiedCommand)); |
| 54 | + } |
| 55 | + |
| 56 | + return copiedCommands; |
| 57 | +} |
| 58 | + |
39 | 59 | const std::string getDocPath(const std::optional<std::string>& location) { |
40 | 60 | std::string tempDocPath = std::string(HybridNitroSQLite::docPath); |
41 | 61 | if (location) { |
@@ -98,9 +118,14 @@ BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, cons |
98 | 118 |
|
99 | 119 | std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync(const std::string& dbName, |
100 | 120 | const std::vector<BatchQueryCommand>& batchParams) { |
| 121 | + // Convert BatchQueryCommand objects on the JS thread and copy any JS-backed |
| 122 | + // ArrayBuffers into native buffers before going off-thread. |
| 123 | + const auto commands = batchParamsToCommands(batchParams); |
| 124 | + const auto copiedCommands = copyArrayBufferParamsForBackground(commands); |
| 125 | + |
101 | 126 | return Promise<BatchQueryResult>::async([=, this]() -> BatchQueryResult { |
102 | | - auto result = executeBatch(dbName, batchParams); |
103 | | - return result; |
| 127 | + auto result = sqliteExecuteBatch(dbName, copiedCommands); |
| 128 | + return BatchQueryResult(result.rowsAffected); |
104 | 129 | }); |
105 | 130 | }; |
106 | 131 |
|
|
0 commit comments