Skip to content

Commit

Permalink
Always use POST; disable readonly setting by default
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn committed Dec 3, 2024
1 parent 9951588 commit 117849d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
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.).
///
/// 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

0 comments on commit 117849d

Please sign in to comment.