Skip to content

Commit

Permalink
Get rid of execute literal method
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Mar 11, 2024
1 parent d290e4a commit d9bee65
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 73 deletions.
71 changes: 10 additions & 61 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ BridgeResult opsqlite_attach(std::string const &mainDBName,
std::string dbPath = get_db_path(databaseToAttach, docPath);
std::string statement = "ATTACH DATABASE '" + dbPath + "' AS " + alias;

BridgeResult result = opsqlite_execute_literal(mainDBName, statement);
BridgeResult result =
opsqlite_execute(mainDBName, statement, nullptr, nullptr, nullptr);

if (result.type == SQLiteError) {
return {
Expand All @@ -108,7 +109,8 @@ BridgeResult opsqlite_detach(std::string const &mainDBName,
* sqliteExecuteLiteral will do that.
* */
std::string statement = "DETACH DATABASE " + alias;
BridgeResult result = opsqlite_execute_literal(mainDBName, statement);
BridgeResult result =
opsqlite_execute(mainDBName, statement, nullptr, nullptr, nullptr);
if (result.type == SQLiteError) {
return BridgeResult{
.type = SQLiteError,
Expand Down Expand Up @@ -526,7 +528,7 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
bool isConsuming = true;
bool isFailed = false;

int result = SQLITE_OK;
int step = SQLITE_OK;

do {
const char *queryStr =
Expand Down Expand Up @@ -562,13 +564,14 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
std::string column_name, column_declared_type;

while (isConsuming) {
result = sqlite3_step(statement);
step = sqlite3_step(statement);

switch (result) {
switch (step) {
case SQLITE_ROW: {
if (results == NULL) {
if (results == nullptr) {
break;
}

std::vector<JSVariant> row;

i = 0;
Expand Down Expand Up @@ -649,7 +652,7 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,

return {.type = SQLiteError,
.message =
"[op-sqlite] SQLite error code: " + std::to_string(result) +
"[op-sqlite] SQLite error code: " + std::to_string(step) +
", description: " + std::string(errorMessage) +
".\nSee SQLite error codes reference: "
"https://www.sqlite.org/rescode.html"};
Expand All @@ -663,60 +666,6 @@ opsqlite_execute_raw(std::string const &dbName, std::string const &query,
.insertId = static_cast<double>(latestInsertRowId)};
}

/// Executes without returning any results, Useful for performance critical
/// operations
BridgeResult opsqlite_execute_literal(std::string const &dbName,
std::string const &query) {
check_db_open(dbName);

sqlite3 *db = dbMap[dbName];
sqlite3_stmt *statement;

int statementStatus =
sqlite3_prepare_v2(db, query.c_str(), -1, &statement, NULL);

if (statementStatus != SQLITE_OK) {
const char *message = sqlite3_errmsg(db);
return {SQLiteError,
"[op-sqlite] SQL statement error: " + std::string(message), 0};
}

bool isConsuming = true;
bool isFailed = false;

int result;
std::string column_name;

while (isConsuming) {
result = sqlite3_step(statement);

switch (result) {
case SQLITE_ROW:
isConsuming = true;
break;

case SQLITE_DONE:
isConsuming = false;
break;

default:
isFailed = true;
isConsuming = false;
}
}

sqlite3_finalize(statement);

if (isFailed) {
const char *message = sqlite3_errmsg(db);
return {SQLiteError,
"[op-sqlite] SQL execution error: " + std::string(message), 0};
}

int changedRowCount = sqlite3_changes(db);
return {SQLiteOk, "", changedRowCount};
}

void opsqlite_close_all() {
for (auto const &x : dbMap) {
// Interrupt will make all pending operations to fail with SQLITE_INTERRUPT
Expand Down
3 changes: 0 additions & 3 deletions cpp/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ BridgeResult opsqlite_execute_raw(std::string const &dbName,
const std::vector<JSVariant> *params,
std::vector<std::vector<JSVariant>> *results);

BridgeResult opsqlite_execute_literal(std::string const &dbName,
std::string const &query);

void opsqlite_close_all();

BridgeResult opsqlite_register_update_hook(std::string const &dbName,
Expand Down
9 changes: 5 additions & 4 deletions cpp/sqlbatchexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ BatchResult sqliteExecuteBatch(std::string dbName,

try {
int affectedRows = 0;
opsqlite_execute_literal(dbName, "BEGIN EXCLUSIVE TRANSACTION");
opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr,
nullptr);
for (int i = 0; i < commandCount; i++) {
auto command = commands->at(i);
// We do not provide a datastructure to receive query data because we
// don't need/want to handle this results in a batch execution
auto result = opsqlite_execute(dbName, command.sql, command.params.get(),
nullptr, nullptr);
if (result.type == SQLiteError) {
opsqlite_execute_literal(dbName, "ROLLBACK");
opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
return BatchResult{
.type = SQLiteError,
.message = result.message,
Expand All @@ -74,14 +75,14 @@ BatchResult sqliteExecuteBatch(std::string dbName,
affectedRows += result.affectedRows;
}
}
opsqlite_execute_literal(dbName, "COMMIT");
opsqlite_execute(dbName, "COMMIT", nullptr, nullptr, nullptr);
return BatchResult{
.type = SQLiteOk,
.affectedRows = affectedRows,
.commands = static_cast<int>(commandCount),
};
} catch (std::exception &exc) {
opsqlite_execute_literal(dbName, "ROLLBACK");
opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
return BatchResult{
.type = SQLiteError,
.message = exc.what(),
Expand Down
12 changes: 7 additions & 5 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,14 @@ BatchResult importSQLFile(std::string dbName, std::string fileLocation) {
try {
int affectedRows = 0;
int commands = 0;
opsqlite_execute_literal(dbName, "BEGIN EXCLUSIVE TRANSACTION");
opsqlite_execute(dbName, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr,
nullptr);
while (std::getline(sqFile, line, '\n')) {
if (!line.empty()) {
BridgeResult result = opsqlite_execute_literal(dbName, line);
BridgeResult result =
opsqlite_execute(dbName, line, nullptr, nullptr, nullptr);
if (result.type == SQLiteError) {
opsqlite_execute_literal(dbName, "ROLLBACK");
opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
sqFile.close();
return {SQLiteError, result.message, 0, commands};
} else {
Expand All @@ -220,11 +222,11 @@ BatchResult importSQLFile(std::string dbName, std::string fileLocation) {
}
}
sqFile.close();
opsqlite_execute_literal(dbName, "COMMIT");
opsqlite_execute(dbName, "COMMIT", nullptr, nullptr, nullptr);
return {SQLiteOk, "", affectedRows, commands};
} catch (...) {
sqFile.close();
opsqlite_execute_literal(dbName, "ROLLBACK");
opsqlite_execute(dbName, "ROLLBACK", nullptr, nullptr, nullptr);
return {SQLiteError,
"[op-sqlite][loadSQLFile] Unexpected error, transaction was "
"rolledback",
Expand Down

0 comments on commit d9bee65

Please sign in to comment.