Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ If something is missing, or you found a mistake in one of these examples, please
- [async_insert.rs](async_insert.rs) - using the server-side batching via the [asynchronous inserts](https://clickhouse.com/docs/en/optimize/asynchronous-inserts) ClickHouse feature
- [clickhouse_cloud.rs](clickhouse_cloud.rs) - using the client with ClickHouse Cloud, highlighting a few relevant settings (`wait_end_of_query`, `select_sequential_consistency`). Cargo features: requires `rustls-tls`; the code also works with `native-tls`.
- [clickhouse_settings.rs](clickhouse_settings.rs) - applying various ClickHouse settings on the query level
- [server_side_params.rs](server_side_params.rs) - parametrized queries with server-side parameters

### Data types

Expand Down
31 changes: 31 additions & 0 deletions examples/server_side_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use clickhouse::{error::Result, Client};

/// An example of using server-side query parameters.
///
/// In most cases, this is the preferred method over the client-side binding
/// via [`clickhouse::query::Query::bind`].
///
/// See also: https://clickhouse.com/docs/sql-reference/syntax#defining-and-using-query-parameters

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

let result = client
.query(
"
SELECT {tbl:Identifier}.{col:Identifier}
FROM {db:Identifier}.{tbl:Identifier}
WHERE {col:Identifier} < {val:UInt64}
",
)
.param("db", "system")
.param("tbl", "numbers")
.param("col", "number")
.param("val", 3u64)
.fetch_all::<u64>()
.await?;

println!("Parametrized query output: {:?}", result);
Ok(())
}