Skip to content

feat(core): add parameterized query interface to database_backend#559

Merged
kcenon merged 1 commit into
mainfrom
feat/issue-557-prepared-statement-interface
Apr 10, 2026
Merged

feat(core): add parameterized query interface to database_backend#559
kcenon merged 1 commit into
mainfrom
feat/issue-557-prepared-statement-interface

Conversation

@kcenon

@kcenon kcenon commented Apr 10, 2026

Copy link
Copy Markdown
Owner

What

Summary

Adds select_prepared() and execute_prepared() virtual methods to the database_backend interface, establishing the foundation for wire-level prepared statement support. Default implementations fall back to string interpolation for backward compatibility.

Change Type

  • Feature (new functionality)

Affected Components

  • database/core/database_backend.h — New virtual methods + fallback helpers

Why

Problem Solved

All SQL execution currently relies on string-level escaping via value_formatter to prevent SQL injection. This PR establishes the interface for true parameterized queries where SQL and values travel separately. The default fallback ensures zero breakage for existing backends — they compile without changes.

Related Issues

Where

Files Changed

File Type of Change
database/core/database_backend.h Add select_prepared, execute_prepared, expand_params, value_to_sql
docs/README.kr.md Fix pre-existing broken markdown anchors

How

Implementation Details

New virtual methods (with default fallback):

[[nodiscard]] virtual Result<database_result> select_prepared(
    const std::string& query,
    const std::vector<database_value>& params);

[[nodiscard]] virtual VoidResult execute_prepared(
    const std::string& query,
    const std::vector<database_value>& params);

Fallback mechanism:

  • expand_params() substitutes $N (PostgreSQL, reverse order to avoid $1/$10 collision) and ? (SQLite, left-to-right)
  • value_to_sql() converts database_value variant to SQL literal with basic escaping

Future override pattern (Phase 2):

// In postgresql_backend:
Result<database_result> select_prepared(...) override {
    // Use pqxx::work::exec_params() for native binding
}

Breaking Changes

None — new methods have default implementations. Existing backends compile without modification.

Test Plan

  • CI verification: existing tests pass with new interface methods
  • Phase 2 will add prepared-statement-specific tests per backend

Add select_prepared() and execute_prepared() virtual methods with
default fallback implementations that expand placeholders via string
interpolation. This establishes the interface for wire-level prepared
statements while maintaining backward compatibility.

Supports both $N (PostgreSQL-style) and ? (SQLite-style) placeholders
in the fallback. Backends should override these methods with native
prepared statement implementations for true SQL injection protection.

Fix pre-existing broken markdown anchors in docs/README.kr.md.

Part of #557
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results

No comparison reports available. Baseline may not be established yet.

@kcenon kcenon merged commit 921b04a into main Apr 10, 2026
36 checks passed
@kcenon kcenon deleted the feat/issue-557-prepared-statement-interface branch April 10, 2026 05:54
kcenon added a commit that referenced this pull request Apr 13, 2026
Add select_prepared() and execute_prepared() virtual methods with
default fallback implementations that expand placeholders via string
interpolation. This establishes the interface for wire-level prepared
statements while maintaining backward compatibility.

Supports both $N (PostgreSQL-style) and ? (SQLite-style) placeholders
in the fallback. Backends should override these methods with native
prepared statement implementations for true SQL injection protection.

Fix pre-existing broken markdown anchors in docs/README.kr.md.

Part of #557
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant