Skip to content

Commit

Permalink
Fix issue with executing remaining statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Sep 20, 2024
1 parent 1db946a commit 69a28ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
44 changes: 25 additions & 19 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,49 +393,55 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query,
sqlite3 *db = dbMap[name];

sqlite3_stmt *statement;
const char *errorMessage;
const char *errorMessage = nullptr;
const char *remainingStatement = nullptr;

bool isFailed = false;
int step_result, current_column, column_count, column_type;
bool has_failed = false;
int status, current_column, column_count, column_type;
std::string column_name, column_declared_type;
std::vector<std::string> column_names;
std::vector<std::vector<JSVariant>> rows;
std::vector<JSVariant> row;

do {
const char *queryStr =
const char *query_str =
remainingStatement == nullptr ? query.c_str() : remainingStatement;

int statementStatus =
sqlite3_prepare_v2(db, queryStr, -1, &statement, &remainingStatement);
status =
sqlite3_prepare_v2(db, query_str, -1, &statement, &remainingStatement);

if (statementStatus != SQLITE_OK) {
if (status != SQLITE_OK) {
errorMessage = sqlite3_errmsg(db);
return {.type = SQLiteError,
.message =
"[op-sqlite] SQL prepare error: " + std::string(errorMessage),
.affectedRows = 0};
}

// The statement did not fail to parse but there is nothing to do, just
// skip to the end
if (statement == nullptr) {
continue;
}

if (params != nullptr && !params->empty()) {
opsqlite_bind_statement(statement, params);
}

column_count = sqlite3_column_count(statement);
bool is_consuming = true;
bool is_consuming_rows = true;
double double_value;
const char *string_value;

// Do a first pass to get the column names
for (int i = 0; i < column_count; i++) {
column_name = sqlite3_column_name(statement, i);
column_names.push_back(column_name);
}

double double_value;
const char *string_value;
while (is_consuming) {
step_result = sqlite3_step(statement);
while (is_consuming_rows) {
status = sqlite3_step(statement);

switch (step_result) {
switch (status) {
case SQLITE_ROW:
current_column = 0;
row = std::vector<JSVariant>();
Expand Down Expand Up @@ -488,20 +494,20 @@ BridgeResult opsqlite_execute(std::string const &name, std::string const &query,
break;

case SQLITE_DONE:
is_consuming = false;
is_consuming_rows = false;
break;

default:
isFailed = true;
is_consuming = false;
has_failed = true;
is_consuming_rows = false;
}
}

sqlite3_finalize(statement);
} while (remainingStatement != nullptr &&
strcmp(remainingStatement, "") != 0 && !isFailed);
strcmp(remainingStatement, "") != 0 && !has_failed);

if (isFailed) {
if (has_failed) {
const char *message = sqlite3_errmsg(db);
return {.type = SQLiteError,
.message =
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PODS:
- hermes-engine (0.74.0):
- hermes-engine/Pre-built (= 0.74.0)
- hermes-engine/Pre-built (0.74.0)
- op-sqlite (8.0.0):
- op-sqlite (8.0.2):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1369,7 +1369,7 @@ SPEC CHECKSUMS:
GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3
op-sqlite: d34b2d2f64871a4a626b45af824ef8f6a10cd3d6
op-sqlite: 63af78e9033946d98491201c2c21229891a03b64
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47
RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5
Expand Down
7 changes: 7 additions & 0 deletions example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,5 +654,12 @@ export function queriesTests() {
{name: 'test', content: 'test content'},
]);
});

it('Various queries', async () => {
await db.execute('SELECT 1 ');
await db.execute('SELECT 1 ');
await db.execute('SELECT 1; ', []);
await db.execute('SELECT ?; ', [1]);
});
});
}

0 comments on commit 69a28ca

Please sign in to comment.