Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed method to control the readonly query parameter #181

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ pub use crate::cursor::RowCursor;
pub struct Query {
client: Client,
sql: SqlBuilder,
is_read_only: bool,
}

impl Query {
pub(crate) fn new(client: &Client, template: &str) -> Self {
Self {
client: client.clone(),
sql: SqlBuilder::new(template),
is_read_only: true,
}
}

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

As it is now set as true by default, won't this fail with DDLs now?

}

/// Executes the query, returning a [`RowCursor`] to obtain results.
Expand Down Expand Up @@ -86,7 +88,7 @@ impl Query {
self.sql.bind_fields::<T>();
self.sql.append(" FORMAT RowBinary");

let response = self.do_execute(true)?;
let response = self.do_execute()?;
Ok(RowCursor::new(response))
}

Expand Down Expand Up @@ -132,7 +134,14 @@ impl Query {
Ok(result)
}

pub(crate) fn do_execute(self, read_only: bool) -> Result<Response> {
/// 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<Response> {
let query = self.sql.finish()?;

let mut url =
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ async fn init_cursor<T>(client: &Client, params: &WatchParams) -> Result<JsonCur

watch_sql.push_str(" FORMAT JSONEachRowWithProgress");

let response = client.query(&watch_sql).do_execute(true)?;
let response = client.query(&watch_sql).do_execute()?;
Ok(JsonCursor::new(response))
}

Expand Down
Loading