diff --git a/CHANGELOG.md b/CHANGELOG.md index 8511848..5c918fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,12 +18,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - query/cursor: improve performance of `RowCursor::next()` ([#169]). +- query: exposed method to control the `readonly` query parameter ([#181]). ### Fixed - mock: work with the advanced time via `tokio::time::advance()` ([#165]). [#165]: https://github.com/ClickHouse/clickhouse-rs/pull/165 [#169]: https://github.com/ClickHouse/clickhouse-rs/pull/169 +[#181]: https://github.com/ClickHouse/clickhouse-rs/pull/181 ## [0.13.0] - 2024-09-27 ### Added diff --git a/src/query.rs b/src/query.rs index eba1915..44ee23f 100644 --- a/src/query.rs +++ b/src/query.rs @@ -22,6 +22,7 @@ pub use crate::cursor::RowCursor; pub struct Query { client: Client, sql: SqlBuilder, + is_read_only: bool, } impl Query { @@ -29,6 +30,7 @@ impl Query { Self { client: client.clone(), sql: SqlBuilder::new(template), + is_read_only: true, } } @@ -58,7 +60,7 @@ impl Query { /// Executes the query. pub async fn execute(self) -> Result<()> { - self.do_execute(false)?.finish().await + self.do_execute()?.finish().await } /// Executes the query, returning a [`RowCursor`] to obtain results. @@ -86,7 +88,7 @@ impl Query { self.sql.bind_fields::(); self.sql.append(" FORMAT RowBinary"); - let response = self.do_execute(true)?; + let response = self.do_execute()?; Ok(RowCursor::new(response)) } @@ -132,7 +134,14 @@ impl Query { Ok(result) } - pub(crate) fn do_execute(self, read_only: bool) -> Result { + /// Sets the read only option. + pub fn read_only(mut self, is_read_only: bool) -> Self { + self.is_read_only = is_read_only; + + self + } + + pub(crate) fn do_execute(self) -> Result { let query = self.sql.finish()?; let mut url = @@ -144,10 +153,10 @@ impl Query { pairs.append_pair("database", database); } - let use_post = !read_only || query.len() > MAX_QUERY_LEN_TO_USE_GET; + let use_post = !self.is_read_only || query.len() > MAX_QUERY_LEN_TO_USE_GET; let (method, body, content_length) = if use_post { - if read_only { + if self.is_read_only { pairs.append_pair("readonly", "1"); } let len = query.len(); diff --git a/src/watch.rs b/src/watch.rs index 1914399..7ee180f 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -254,7 +254,7 @@ async fn init_cursor(client: &Client, params: &WatchParams) -> Result