Skip to content

Commit 2e57c26

Browse files
authored
docs: add a server-side params example (#288)
1 parent ee299f6 commit 2e57c26

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ If something is missing, or you found a mistake in one of these examples, please
1414
- [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
1515
- [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`.
1616
- [clickhouse_settings.rs](clickhouse_settings.rs) - applying various ClickHouse settings on the query level
17+
- [server_side_params.rs](server_side_params.rs) - parametrized queries with server-side parameters
1718

1819
### Data types
1920

examples/server_side_params.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use clickhouse::{error::Result, Client};
2+
3+
/// An example of using server-side query parameters.
4+
///
5+
/// In most cases, this is the preferred method over the client-side binding
6+
/// via [`clickhouse::query::Query::bind`].
7+
///
8+
/// See also: https://clickhouse.com/docs/sql-reference/syntax#defining-and-using-query-parameters
9+
10+
#[tokio::main]
11+
async fn main() -> Result<()> {
12+
let client = Client::default().with_url("http://localhost:8123");
13+
14+
let result = client
15+
.query(
16+
"
17+
SELECT {tbl:Identifier}.{col:Identifier}
18+
FROM {db:Identifier}.{tbl:Identifier}
19+
WHERE {col:Identifier} < {val:UInt64}
20+
",
21+
)
22+
.param("db", "system")
23+
.param("tbl", "numbers")
24+
.param("col", "number")
25+
.param("val", 3u64)
26+
.fetch_all::<u64>()
27+
.await?;
28+
29+
println!("Parametrized query output: {:?}", result);
30+
Ok(())
31+
}

0 commit comments

Comments
 (0)