QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<D: Dialect> { /* private fields */ }
Expand description

SQL query builder with dialect support.

Implementations§

Source§

impl<D: Dialect> QueryBuilder<D>

Source

pub fn new(dialect: D, table: impl Into<String>) -> Self

Create a new query builder for the given table.

§Panics

Panics if the table name is not a valid SQL identifier.

Source

pub fn fields(self, fields: &[&str]) -> Self

Set the fields to SELECT.

§Panics

Panics if any field name is not a valid SQL identifier.

Source

pub fn computed( self, alias: impl Into<String>, expression: impl Into<String>, ) -> Self

Add a computed field to the SELECT clause.

§Example
.computed("full_name", "first_name || ' ' || last_name")
.computed("line_total", "quantity * price")
§Panics

Panics if alias is not a valid SQL identifier or expression contains dangerous patterns (comments, semicolons, SQL keywords).

§Security

WARNING: Only use with trusted expressions from code, never with user input.

Source

pub fn aggregate(self, agg: Aggregate) -> Self

Add an aggregation to the SELECT clause.

Source

pub fn count(self) -> Self

Add a COUNT(*) aggregation.

Source

pub fn sum(self, field: impl Into<String>) -> Self

Add a SUM(field) aggregation.

Source

pub fn avg(self, field: impl Into<String>) -> Self

Add an AVG(field) aggregation.

Source

pub fn min(self, field: impl Into<String>) -> Self

Add a MIN(field) aggregation.

Source

pub fn max(self, field: impl Into<String>) -> Self

Add a MAX(field) aggregation.

Source

pub fn filter( self, field: impl Into<String>, op: Operator, value: Value, ) -> Self

Add a filter condition.

§Panics

Panics if the field name is not a valid SQL identifier.

Source

pub fn filter_expr(self, expr: FilterExpr) -> Self

Set a compound filter expression (replaces simple filters for WHERE clause).

Source

pub fn and(self, filters: Vec<FilterExpr>) -> Self

Add an AND compound filter.

Source

pub fn or(self, filters: Vec<FilterExpr>) -> Self

Add an OR compound filter.

Source

pub fn group_by(self, fields: &[&str]) -> Self

Add GROUP BY fields.

§Panics

Panics if any field name is not a valid SQL identifier.

Source

pub fn having(self, expr: FilterExpr) -> Self

Add a HAVING clause (for filtering aggregated results).

Source

pub fn sort(self, field: impl Into<String>, dir: SortDir) -> Self

Add a sort field.

§Panics

Panics if the field name is not a valid SQL identifier.

Source

pub fn sorts(self, sorts: &[SortField]) -> Self

Add multiple sort fields.

Source

pub fn page(self, page: u32, limit: u32) -> Self

Set pagination with page number (1-indexed) and limit.

Source

pub fn limit_offset(self, limit: u32, offset: u32) -> Self

Set explicit limit and offset.

Source

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

Set a limit without offset.

Source

pub fn after_cursor(self, cursor: impl IntoCursor) -> Self

Paginate after this cursor (forward pagination).

This method accepts flexible input types for great DX:

  • &Cursor - when you have an already-parsed cursor
  • &str - automatically decodes the base64 cursor
  • Option<&str> - perfect for req.query("after") results

If the cursor is invalid or None, it’s silently ignored. This makes it safe to pass req.query("after") directly.

Source

pub fn before_cursor(self, cursor: impl IntoCursor) -> Self

Paginate before this cursor (backward pagination).

This method accepts flexible input types for great DX:

  • &Cursor - when you have an already-parsed cursor
  • &str - automatically decodes the base64 cursor
  • Option<&str> - perfect for req.query("before") results

If the cursor is invalid or None, it’s silently ignored.

Source

pub fn build(self) -> QueryResult

Build the SQL query and parameters.

Trait Implementations§

Source§

impl<D: Debug + Dialect> Debug for QueryBuilder<D>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D> Freeze for QueryBuilder<D>
where D: Freeze,

§

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

§

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

§

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

§

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

§

impl<D> UnwindSafe for QueryBuilder<D>
where D: 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> 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, 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.