clickhouse.rs icon indicating copy to clipboard operation
clickhouse.rs copied to clipboard

Support server-side parameter binding

Open slvrtrn opened this issue 1 year ago • 3 comments

See the docs.

For example:

SELECT plus({val1: Int32}, {val2: Int32}) AS result

We already have .bind, though, for client-side substitutions; so maybe it can be done as

client
    .query("SELECT plus({val1: Int32}, {val2: Int32}) AS result")
    .with_param(42)
    .with_param(144)
    .fetch_one::<i32>()
    .await?;

Theoretically, we can even do something like this as well:

client
    .query("SELECT plus({?}, {?}) AS result")
    .with_param("val1", 42)
    .with_param("val2", 144)
    .fetch_one::<i32>()
    .await?;

slvrtrn avatar Sep 05 '24 13:09 slvrtrn

what about

client
    .query("SELECT plus({val1: Int32}, {val2: Int32}) AS result")
    .with_param("val1", 42)
    .with_param("val2", 144)
    .fetch_one::<i32>()
    .await?;

mshustov avatar Sep 10 '24 13:09 mshustov

It actually works now as:

use clickhouse::{error::Result, Client};

#[tokio::main]
async fn main() -> Result<()> {
    let client = Client::default()
        .with_url("http://localhost:8123");

    let result = client
        .query("SELECT plus({val1: Int32}, {val2: Int32}) AS result")
        .with_option("param_val1", "42")
        .with_option("param_val2", "144")
        .fetch_one::<u64>()
        .await?;
    println!("Result: {result:?}");

    Ok(())
}

But the user has to (properly) serialize the values on the application side.

slvrtrn avatar Sep 10 '24 15:09 slvrtrn

Can create generic method over serializable types

serprex avatar Sep 11 '24 16:09 serprex

@serprex closed by https://github.com/ClickHouse/clickhouse-rs/pull/159?

mshustov avatar Dec 03 '24 09:12 mshustov