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

Always use POST for queries; disable readonly setting by default #184

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
34 changes: 7 additions & 27 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use crate::{
Client,
};

const MAX_QUERY_LEN_TO_USE_GET: usize = 8192;

pub use crate::cursor::RowCursor;

#[must_use]
Expand Down Expand Up @@ -43,7 +41,7 @@ impl Query {
/// [`Identifier`], will be appropriately escaped.
///
/// All possible errors will be returned as [`Error::InvalidParams`]
/// during query execution (`execute()`, `fetch()` etc).
/// during query execution (`execute()`, `fetch()` etc.).
slvrtrn marked this conversation as resolved.
Show resolved Hide resolved
///
/// WARNING: This means that the query must not have any extra `?`, even if
/// they are in a string literal! Use `??` to have plain `?` in query.
Expand All @@ -58,7 +56,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.
Expand Down Expand Up @@ -86,7 +84,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 +130,7 @@ impl Query {
Ok(result)
}

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

let mut url =
Expand All @@ -144,19 +142,6 @@ impl Query {
pairs.append_pair("database", database);
}

let use_post = !read_only || query.len() > MAX_QUERY_LEN_TO_USE_GET;

let (method, body, content_length) = if use_post {
if read_only {
pairs.append_pair("readonly", "1");
}
let len = query.len();
(Method::POST, RequestBody::full(query), len)
} else {
pairs.append_pair("query", &query);
(Method::GET, RequestBody::empty(), 0)
};

if self.client.compression.is_lz4() {
pairs.append_pair("compress", "1");
}
Expand All @@ -166,14 +151,9 @@ impl Query {
}
drop(pairs);

let mut builder = Request::builder().method(method).uri(url.as_str());
let mut builder = Request::builder().method(Method::POST).uri(url.as_str());
builder = with_request_headers(builder, &self.client.headers, &self.client.products_info);

if content_length == 0 {
builder = builder.header(CONTENT_LENGTH, "0");
} else {
builder = builder.header(CONTENT_LENGTH, content_length.to_string());
}
builder = builder.header(CONTENT_LENGTH, query.len().to_string());

if let Some(user) = &self.client.user {
builder = builder.header("X-ClickHouse-User", user);
Expand All @@ -184,7 +164,7 @@ impl Query {
}

let request = builder
.body(body)
.body(RequestBody::full(query))
.map_err(|err| Error::InvalidParams(Box::new(err)))?;

let future = self.client.http.request(request);
Expand Down
4 changes: 0 additions & 4 deletions src/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ enum Message {
}

impl RequestBody {
pub(crate) fn empty() -> Self {
Self(Inner::Full(Bytes::new()))
}

pub(crate) fn full(content: String) -> Self {
Self(Inner::Full(Bytes::from(content)))
}
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