diff --git a/CHANGELOG.md b/CHANGELOG.md index d7e0cba..cdc1ebd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Optimize `RowCursor` by reusing buffer capacity where possible. ([#340]) * All `Query::fetch*` methods will always use POST instead of GET. It is now allowed to change `readonly` value via - `Query::with_option`. ([#342]) + `Query::readonly`. ([#342]) * In case of a schema mismatch, the client now emits `clickhouse::error::Error::SchemaMismatch` instead of panicking. ([#346]) * Removed [replace_with], [static_assertions], and [sealed] from the crate dependencies. ([#353]) diff --git a/src/query.rs b/src/query.rs index 25f21af..7254e1c 100644 --- a/src/query.rs +++ b/src/query.rs @@ -239,6 +239,13 @@ impl Query { self.client.add_option(name, value); self } + + /// Set read-only option for this query. + pub fn readonly(mut self, enabled: bool) -> Self { + let value = if enabled { "1" } else { "0" }; + self.client.options.insert(settings::READONLY.to_string(), value.to_string()); + self + } /// Specify server side parameter for query. /// diff --git a/tests/it/query_readonly.rs b/tests/it/query_readonly.rs index a4de5e6..da4f9bd 100644 --- a/tests/it/query_readonly.rs +++ b/tests/it/query_readonly.rs @@ -45,14 +45,14 @@ async fn test_fetch(client: &Client) { "initial `fetch` readonly setting value should be 1" ); - let query = select_readonly_setting_query(client).with_option("readonly", "0"); + let query = select_readonly_setting_query(client).readonly(false); let disabled_readonly_row = run_fetch(query).await; assert_eq!( disabled_readonly_row.value, "0", "`fetch` modified readonly setting value should be 0" ); - let query = select_readonly_setting_query(client).with_option("readonly", "1"); + let query = select_readonly_setting_query(client).readonly(true); let same_readonly_row = run_fetch(query).await; assert_eq!( same_readonly_row.value, "1", @@ -68,14 +68,14 @@ async fn test_fetch_bytes(client: &Client) { "initial `fetch_bytes` readonly setting value should be 1" ); - let query = select_readonly_setting_query(client).with_option("readonly", "0"); + let query = select_readonly_setting_query(client).readonly(false); let disabled_readonly_value = run_fetch_bytes(query).await; assert_eq!( disabled_readonly_value, b"0\n", "`fetch_bytes` modified readonly setting value should be 0" ); - let query = select_readonly_setting_query(client).with_option("readonly", "1"); + let query = select_readonly_setting_query(client).readonly(true); let same_readonly_value = run_fetch_bytes(query).await; assert_eq!( same_readonly_value, b"1\n", @@ -91,14 +91,14 @@ async fn test_fetch_one(client: &Client) { "initial `fetch_one` readonly setting value should be 1" ); - let query = select_readonly_setting_query(client).with_option("readonly", "0"); + let query = select_readonly_setting_query(client).readonly(false); let disabled_readonly_value: String = run_fetch_one(query).await; assert_eq!( disabled_readonly_value, "0", "`fetch_one` modified readonly setting value should be 0" ); - let query = select_readonly_setting_query(client).with_option("readonly", "1"); + let query = select_readonly_setting_query(client).readonly(true); let same_readonly_value: String = run_fetch_one(query).await; assert_eq!( same_readonly_value, "1", @@ -115,7 +115,7 @@ async fn test_fetch_optional(client: &Client) { "initial `fetch_optional` readonly setting value should be 1" ); - let query = select_readonly_setting_query(client).with_option("readonly", "0"); + let query = select_readonly_setting_query(client).readonly(false); let disabled_readonly_value: Option = run_fetch_optional(query).await; assert_eq!( disabled_readonly_value.as_deref(), @@ -123,7 +123,7 @@ async fn test_fetch_optional(client: &Client) { "`fetch_optional` modified readonly setting value should be 0" ); - let query = select_readonly_setting_query(client).with_option("readonly", "1"); + let query = select_readonly_setting_query(client).readonly(true); let same_readonly_value: Option = run_fetch_optional(query).await; assert_eq!( same_readonly_value.as_deref(), @@ -141,7 +141,7 @@ async fn test_fetch_all(client: &Client) { "initial `fetch_all` readonly setting value should be 1" ); - let query = select_readonly_setting_query(client).with_option("readonly", "0"); + let query = select_readonly_setting_query(client).readonly(false); let disabled_readonly_value: Vec = run_fetch_all(query).await; assert_eq!( disabled_readonly_value, @@ -149,7 +149,7 @@ async fn test_fetch_all(client: &Client) { "`fetch_all` modified readonly setting value should be 0" ); - let query = select_readonly_setting_query(client).with_option("readonly", "1"); + let query = select_readonly_setting_query(client).readonly(true); let same_readonly_value: Vec = run_fetch_all(query).await; assert_eq!( same_readonly_value,