fix: always reset statement on error

This commit is contained in:
Fedor Indutny 2025-05-12 11:49:05 -07:00
parent 24a198971b
commit 8f3d534645
2 changed files with 13 additions and 2 deletions

View File

@ -482,14 +482,16 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) {
int r = sqlite3_step(stmt->handle_);
AutoResetStatement auto_reset(stmt, is_get.Value());
// No more rows
if (r == SQLITE_DONE) {
stmt->Reset();
auto_reset.Reset();
return Napi::Value();
}
AutoResetStatement _(stmt, is_get.Value());
if (r != SQLITE_ROW) {
auto_reset.Reset();
return stmt->db_->ThrowSqliteError(env, r);
}
@ -498,6 +500,7 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) {
// In pluck mode - return the value of the first column
if (stmt->is_pluck_) {
if (column_count != 1) {
auto_reset.Reset();
NAPI_THROW(Napi::Error::New(env, "Invalid column count for pluck"),
Napi::Value());
}
@ -714,6 +717,11 @@ Napi::Value Statement::GetColumnValue(Napi::Env env, int column) {
return Napi::Value();
}
void AutoResetStatement::Reset() {
stmt_->Reset();
enabled_ = false;
}
AutoResetStatement::~AutoResetStatement() {
if (enabled_) {
stmt_->Reset();

View File

@ -53,6 +53,9 @@ class AutoResetStatement {
~AutoResetStatement();
// Force reset statement now and clear `enabled_`
void Reset();
private:
Statement* stmt_;
bool enabled_;