Skip to content

libsql: add Statement::raw_bind_parameter() - #2338

Open
AbdeldjalilChougui wants to merge 1 commit into
tursodatabase:mainfrom
AbdeldjalilChougui:statement-raw-bind-parameter
Open

AbdeldjalilChougui wants to merge 1 commit into
tursodatabase:mainfrom
AbdeldjalilChougui:statement-raw-bind-parameter

Conversation

@AbdeldjalilChougui

Copy link
Copy Markdown

Closes #1696.

Adds rusqlite's low-level binding API to libsql::Statement, as requested in the issue, for language bindings that receive parameters one at a time:

let mut stmt = conn.prepare("INSERT INTO users (id, name) VALUES (?1, ?2)").await?;
stmt.raw_bind_parameter(1, 1)?;
stmt.raw_bind_parameter(2, "Alice")?;
stmt.raw_execute().await?;          // also: raw_query()
stmt.reset();
stmt.raw_bind_parameter(1, 2)?;     // bindings are kept; change only what differs
stmt.raw_execute().await?;
stmt.clear_bindings();

Design

The 2024 attempt stalled on how to bind values for hrana and replication connections, since only the local backend has a real sqlite3_bind_*. This PR avoids that: the values are stored in the Statement wrapper (raw_params: Vec<Value>) and passed to the backend as Params::Positional by raw_execute() / raw_query(). So it works the same on local, remote (hrana), replica and sync connections, with no backend-specific code.

  • Named parameters (:id, @id, $id) are bound by index, as in rusqlite. parameter_name() maps an index to its name.
  • Unbound parameters are NULL. On each run, every parameter the statement reports is bound, unbound ones as NULL. Without that, a value from an earlier run would stay in the local SQLite statement after clear_bindings(), because bind() with Params::None doesn't touch existing bindings.
  • Index checks: index 0 → Error::Misuse. An index above parameter_count() → Error::Misuse, but only when the statement reports its parameters. Remote hrana statements report parameter_count() from bound args, which is 0 right after prepare, so only the lower bound can be checked there.
  • crate::Statement gained a field, so the 6 places that built it with a struct literal now call Statement::new(inner).

Tests

There are four new tests in tests/integration_tests.rs:

  • binding, then re-running with one value changed;
  • named parameters, with an unbound parameter read back as NULL;
  • out-of-range indexes;
  • clear_bindings().

cargo test -p libsql --test integration_tests: 35 passed, 1 ignored. cargo fmt --check is clean. The core-only build also compiles.

Add the rusqlite-style low-level binding API: raw_bind_parameter() binds
one value by its 1-based index, raw_execute() and raw_query() run the
statement with those values, and clear_bindings() drops them.

The values live in the Statement wrapper and are passed to the backend
as positional params, so the API works the same for local, remote,
replica and sync connections. Every reported parameter is bound on each
run, unbound ones as NULL, so a value from an earlier run never stays
in the underlying SQLite statement. Statements are now built through
Statement::new().

Closes tursodatabase#1696
@AbdeldjalilChougui
AbdeldjalilChougui marked this pull request as ready for review November 24, 2026 22:29
@AbdeldjalilChougui

Copy link
Copy Markdown
Author

The 4 failing checks (CR SQLite C/Rust Tests, Extensions Tests, golang-bindings) are the runner bitrot described in #2337: the cr-sqlite bindgen build and the Go LTO link fail on current ubuntu-latest. This PR only touches libsql/src and libsql/tests; the Rust checks pass.

This branch has not been deployed

No deployments
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.

Implement Statement::raw_bind_parameter() API

1 participant