Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named parameter API permits partial binding of parameters #1266

Closed
obsgolem opened this issue Dec 8, 2022 · 1 comment · Fixed by #1268
Closed

Named parameter API permits partial binding of parameters #1266

obsgolem opened this issue Dec 8, 2022 · 1 comment · Fixed by #1268

Comments

@obsgolem
Copy link

obsgolem commented Dec 8, 2022

The named parameter API allows you to bind parameters partially, as exemplified in the following code:

use rusqlite::{named_params, Connection};

fn main() {
    let db = Connection::open_in_memory().unwrap();
    db.execute("CREATE TABLE t(x, y)", ()).unwrap();

    let mut stmt = db.prepare("INSERT INTO t (x, y) values (:x, :y)").unwrap();
    stmt.execute(named_params! {":x": 0}).unwrap();
    stmt.execute(named_params! {":y": 1}).unwrap();

    let mut stmt = db.prepare("SELECT * from t").unwrap();
    for _ in stmt
        .query_map((), |f| {
            Ok(println!(
                "{:?}, {:?}",
                f.get_ref("x").unwrap(),
                f.get_ref("y").unwrap()
            ))
        })
        .unwrap()
    {}
}

I would expect this to output

Integer(0), Null
Null, Integer(1)

but instead this outputs

Integer(0), Null
Integer(0), Integer(1)

I would personally expect the API to reset any binding state prior to running any queries.

@gwenn
Copy link
Collaborator

gwenn commented Dec 8, 2022

https://docs.rs/rusqlite/latest/rusqlite/trait.Params.html#named-parameters

Unbound named parameters will be left to the value they previously were bound with, falling back to NULL for parameters which have never been bound.

https://sqlite.org/c3ref/clear_bindings.html

Contrary to the intuition of many, sqlite3_reset() does not reset the bindings on a prepared statement. Use this routine to reset all host parameters to NULL.

And currently you cannot clear bindings.

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 a pull request may close this issue.

2 participants