Skip to content
Closed
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not modify existing CHANGELOG entries; this will just confuse people trying to understand older releases because this does not exist in 0.14.1. You can either add a new entry under ## Unreleased or we will write one when cutting the release.

`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])
Expand Down
7 changes: 7 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, readonly is not a binary setting: https://clickhouse.com/docs/operations/settings/permissions-for-queries#readonly

This would be best represented by an enum like this:

pub enum ReadOnly {
    Disabled,
    Enabled,
    WithChangeSettings, // bikeshedding
}

You could make it possible to still pass a bool if you did something like this:

impl Query {
    pub fn readonly(mut self, readonly: impl Into<ReadOnly>) -> Self { ... }
}

impl From<bool> for ReadOnly {
    fn from(readonly: bool) -> Self {
        if readonly {
            Self::Enabled
        } else {
            Self::Disabled
        }
    }
}

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.
///
Expand Down
20 changes: 10 additions & 10 deletions tests/it/query_readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -115,15 +115,15 @@ 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<String> = run_fetch_optional(query).await;
assert_eq!(
disabled_readonly_value.as_deref(),
Some("0"),
"`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<String> = run_fetch_optional(query).await;
assert_eq!(
same_readonly_value.as_deref(),
Expand All @@ -141,15 +141,15 @@ 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<String> = run_fetch_all(query).await;
assert_eq!(
disabled_readonly_value,
vec!["0"],
"`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<String> = run_fetch_all(query).await;
assert_eq!(
same_readonly_value,
Expand Down
Loading