#33033 wallet, sqlite: Encapsulate SQLite statements in a RAII class

full analysis

https://github.com/bitcoin/bitcoin/pull/33033 · achow101 · +230/-212 in 2 files, 10 commits · labels: none

Goal

  • Prevent manual resource handling mistakes in the wallet database backend
  • Lays groundwork for more complex database queries and upcoming schema updates

This pull request introduces `SQLiteStatement`, an RAII wrapper around `sqlite3_stmt` pointers in the wallet's SQLite backend. It replaces manual preparation and finalization of SQLite statements across `SQLiteBatch` and `SQLiteCursor` with member functions for binding, stepping, resetting, and retrieving column data.

Problem: SQLite prepared statements are C pointers requiring manual allocation and finalization, which is error-prone and clutters wallet database code, especially as upcoming work introduces more complex SQL statements.

Category: Wallet (#20 of 84)

P3 · cleanup

  • P3 because this is an internal database refactoring with no direct user-facing change
  • Improves memory safety and code hygiene in the wallet database layer
  • Unblocks upcoming schema work such as storing transactions in a separate table

P3 because this is an internal refactoring that improves memory safety and code hygiene in the SQLite database layer. While it does not change user-facing behavior directly, it lays the groundwork for more complex queries and schema redesigns such as #33034.

Membership: Touches src/wallet/sqlite.h and src/wallet/sqlite.cpp, altering how the wallet manages SQLite database statements and cursors.

Factors: security/stability 1, bug 0, performance 0, user value 0, leverage 2

Reviewability: Ready

  • Ready for review: merges cleanly, CI is passing, and prior feedback has been addressed

The branch merges cleanly, CI is passing, and all reviewer concerns from previous rounds have been addressed.

Author status: Active; addressed all feedback from the latest round of review and force-pushed updates on 2026-08-26.

Resolved concerns:

  • Exception handling and return types when reading pragmas and executing steps (vasild, achow101)
  • Checking for uncompiled trailing SQL in `sqlite3_prepare_v2` via `pzTail` (Eunovo, pablomartin4btc, achow101)
  • Type safety when binding and retrieving column blobs or strings (maflcko, vasild, achow101)

Agreement: Strong

  • Strong consensus on using automatic resource management for database statements
  • Concept approval without detailed objection (rkrux)
  • Approved after verifying error handling and lifetime semantics (vasild)
  • Code review approval on recent revisions (pablomartin4btc)

Strong consensus on using an RAII wrapper for SQLite statements; reviewers ACKed after earlier feedback was resolved

Multiple reviewers supported the concept and approach. Specific questions regarding error propagation and column typing were resolved across iterations.

  • rkrux Concept ACKed the introduction of the RAII statement class
  • vasild provided Approach ACK and approved the PR after reviewing error handling and lifetime semantics
  • pablomartin4btc ACKed commit 1682818a7b19 with minor nits and observations
  • achow101 addressed feedback from Eunovo and pablomartin4btc in the 2026-08-26 update

Review verdicts (DrahtBot): 0 (+3)

Dependencies

Enables:

  • #33034 wallet: Store transactions in a separate sqlite table

Files

0 lines under test/bench/ci.

  • src/wallet/sqlite.cpp +206/-197
  • src/wallet/sqlite.h +24/-15

Card

This PR wraps SQLite statement pointers in an RAII class `SQLiteStatement` to manage allocation, binding, column extraction, and finalization automatically within the wallet. It eliminates boilerplate and manual cleanup across `SQLiteBatch` and `SQLiteCursor`, laying necessary groundwork for more complex queries in upcoming wallet schema improvements such as #33034. The change has strong reviewer support and is ready for re-ACKs following recent updates addressing minor feedback.

Data

dossier JSON · extract JSON · model openrouter/google/gemini-3.8-flash, generated 2026-09-17T15:49, confidence high, input hash d54a3b618b9a7f67