Skip to content

Commit 8f3d534

Browse files
fix: always reset statement on error
1 parent 24a1989 commit 8f3d534

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/addon.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,16 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) {
482482

483483
int r = sqlite3_step(stmt->handle_);
484484

485+
AutoResetStatement auto_reset(stmt, is_get.Value());
486+
485487
// No more rows
486488
if (r == SQLITE_DONE) {
487-
stmt->Reset();
489+
auto_reset.Reset();
488490
return Napi::Value();
489491
}
490492

491-
AutoResetStatement _(stmt, is_get.Value());
492493
if (r != SQLITE_ROW) {
494+
auto_reset.Reset();
493495
return stmt->db_->ThrowSqliteError(env, r);
494496
}
495497

@@ -498,6 +500,7 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) {
498500
// In pluck mode - return the value of the first column
499501
if (stmt->is_pluck_) {
500502
if (column_count != 1) {
503+
auto_reset.Reset();
501504
NAPI_THROW(Napi::Error::New(env, "Invalid column count for pluck"),
502505
Napi::Value());
503506
}
@@ -714,6 +717,11 @@ Napi::Value Statement::GetColumnValue(Napi::Env env, int column) {
714717
return Napi::Value();
715718
}
716719

720+
void AutoResetStatement::Reset() {
721+
stmt_->Reset();
722+
enabled_ = false;
723+
}
724+
717725
AutoResetStatement::~AutoResetStatement() {
718726
if (enabled_) {
719727
stmt_->Reset();

src/addon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class AutoResetStatement {
5353

5454
~AutoResetStatement();
5555

56+
// Force reset statement now and clear `enabled_`
57+
void Reset();
58+
5659
private:
5760
Statement* stmt_;
5861
bool enabled_;

0 commit comments

Comments
 (0)