-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathsqliteExecuteBatch.cpp
More file actions
69 lines (60 loc) · 2.34 KB
/
sqliteExecuteBatch.cpp
File metadata and controls
69 lines (60 loc) · 2.34 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
/**
* SQL Batch execution implementation using default sqliteBridge implementation
*/
#include "sqliteExecuteBatch.hpp"
#include "NitroSQLiteException.hpp"
#include "operations.hpp"
#include <utility>
namespace margelo::rnnitrosqlite {
std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryCommand>& batchParams) {
auto commands = std::vector<BatchQuery>();
for (auto& command : batchParams) {
if (command.params) {
using ParamsVec = SQLiteQueryParams;
using NestedParamsVec = std::vector<ParamsVec>;
if (std::holds_alternative<NestedParamsVec>(*command.params)) {
// This arguments is an array of arrays, like a batch update of a single sql command.
for (const auto& params : std::get<NestedParamsVec>(*command.params)) {
commands.push_back(BatchQuery{command.query, ParamsVec(params)});
}
} else {
commands.push_back(BatchQuery{command.query, std::move(std::get<ParamsVec>(*command.params))});
}
} else {
commands.push_back(BatchQuery{command.query, std::nullopt});
}
}
return commands;
}
SQLiteOperationResult sqliteExecuteBatch(const std::string& dbName, const std::vector<BatchQuery>& commands) {
size_t commandCount = commands.size();
if (commandCount <= 0) {
throw NitroSQLiteException(NitroSQLiteExceptionType::NoBatchCommandsProvided, "No SQL batch commands provided");
}
try {
int rowsAffected = 0;
sqliteExecuteLiteral(dbName, "BEGIN EXCLUSIVE TRANSACTION");
for (int i = 0; i < commandCount; i++) {
const auto command = commands.at(i);
// We do not provide a datas tructure to receive query data because we don't need/want to handle this results in a batch execution
auto results = SQLiteQueryResults();
auto metadata = std::optional<SQLiteQueryTableMetadata>(std::nullopt);
try {
auto result = sqliteExecute(dbName, command.sql, command.params);
rowsAffected += result.rowsAffected;
} catch (NitroSQLiteException& e) {
sqliteExecuteLiteral(dbName, "ROLLBACK");
throw e;
}
}
sqliteExecuteLiteral(dbName, "COMMIT");
return {
.rowsAffected = rowsAffected,
.commands = (int)commandCount,
};
} catch (NitroSQLiteException& e) {
sqliteExecuteLiteral(dbName, "ROLLBACK");
throw e;
}
}
} // namespace margelo::rnnitrosqlite