Skip to content

Named parameter support? #605

@ryanschneider

Description

@ryanschneider

As far as I can tell named parameters are not supported, are there any plans to do so? I'm trying to port some duckdb Python code to duckdb-rs but the public API of the code allows named parameters.

Looking at the C API, it seems like this could be supported by iterating through the parameters and calling duckdb_parameter_name on each, then looking up the name in a passed-in HashMap-like trait.

Note that I can almost do this myself, except that duckdb_parameter_name isn't exposed anywhere on Statement. If it were I could do something like:

#[tokio::test]
    async fn test_bind_params() -> Result<(), Error> {
        let conn = duckdb::Connection::open_in_memory().expect("Could not open DB");
        let sql = r#"SELECT $foo > $bar"#;
        let mut named_params: HashMap<String, &dyn duckdb::ToSql> = HashMap::new();
        named_params.insert("foo".to_string(), &42);
        named_params.insert("bar".to_string(), &23);
        let mut stmt = conn.prepare(&sql)?;
        // TODO: build params by calling stmt.get_param_name(index) to find each name to lookup
        let params: Vec<&dyn ToSql> = vec![&42, &23];
        let result: bool = stmt.query_one(params.as_slice(), |row| {row.get(0)})?;
        assert_eq!(result, true);
        Ok(())
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureUsed for auto generate changeloggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions