Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<T> { /* private fields */ }
Expand description

Simplified query builder for ClickHouse.

Provides a fluent API to construct SQL queries safely, ensuring column names belong to the specified table.

§Example

let sql = QueryBuilder::<User>::new()
    .select(User::NAME)
    .filter(User::AGE, Operator::Gt, 18)
    .to_sql();

Implementations§

Source§

impl<T: ClickTable> QueryBuilder<T>

Source

pub fn new() -> Self

Create a new query builder for the given table.

Source

pub fn select<C: TypedColumn<Table = T>>(self, _col: C) -> Self

Add a typed column to the SELECT clause.

Source

pub fn select_raw(self, expr: impl Into<String>) -> Self

Add a raw expression string to the SELECT clause. Useful for aggregate functions like count(), sum(field), etc.

Source

pub fn join<T2: ClickTable, J: JoinType>( self, join_spec: JoinSpec<T, T2, J>, ) -> Self

Add a JOIN clause using a JoinSpec.

§Example
use clicktype::query::{QueryBuilder, inner_join};

let query = QueryBuilder::<User>::new()
    .select(User::NAME)
    .join(inner_join::<User, Order>().on(User::ID, Order::USER_ID))
    .to_sql();
Source

pub fn join_raw(self, join_clause: impl Into<String>) -> Self

Add a raw JOIN string.

Source

pub fn filter<C: TypedColumn<Table = T>, V: SqlValue>( self, _col: C, op: Operator, value: V, ) -> Self

Add a WHERE condition with type-safe column and operator.

The value is automatically converted to a safe SQL string representation.

§Example
.filter(User::ID, Operator::Eq, 42)
.filter(User::NAME, Operator::Eq, "Alice") // Automatically quoted
Source

pub fn filter_raw(self, condition: impl Into<String>) -> Self

Add a raw WHERE condition string.

Source

pub fn order_by<C: TypedColumn<Table = T>>(self, _col: C, desc: bool) -> Self

Add an ORDER BY clause for a specific column.

Source

pub fn order_by_raw(self, expr: impl Into<String>) -> Self

Add a raw string to the ORDER BY clause.

Source

pub fn limit(self, limit: u64) -> Self

Add a LIMIT clause.

Source

pub fn offset(self, offset: u64) -> Self

Add an OFFSET clause.

Source

pub fn group_by<C: TypedColumn<Table = T>>(self, _col: C) -> Self

Add a GROUP BY clause for a typed column.

Source

pub fn group_by_raw(self, expr: impl Into<String>) -> Self

Add a raw string to the GROUP BY clause.

Source

pub fn having(self, expr: impl Into<String>) -> Self

Add a raw HAVING clause.

Source

pub fn to_sql(&self) -> String

Render the final SQL query string.

Trait Implementations§

Source§

impl<T: Clone> Clone for QueryBuilder<T>

Source§

fn clone(&self) -> QueryBuilder<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for QueryBuilder<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for QueryBuilder<T>

§

impl<T> RefUnwindSafe for QueryBuilder<T>
where T: RefUnwindSafe,

§

impl<T> Send for QueryBuilder<T>
where T: Send,

§

impl<T> Sync for QueryBuilder<T>
where T: Sync,

§

impl<T> Unpin for QueryBuilder<T>
where T: Unpin,

§

impl<T> UnwindSafe for QueryBuilder<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.