Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<'a, T, DB: Database> { /* private fields */ }
Expand description

A type-safe SQL query builder.

QueryBuilder provides a fluent interface for building SELECT, UPDATE, and DELETE queries with support for filtering, pagination, eager loading, and soft deletes.

Implementations§

Source§

impl<'a, T, DB> QueryBuilder<'a, T, DB>
where DB: SqlDialect + HasStatementCache, T: Model<DB>,

Source

pub fn new(executor: Executor<'a, DB>) -> Self

Creates a new QueryBuilder using the provided Executor.

Source

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

Adds a raw SQL filter condition to the query.

§Safety

This method is potentially unsafe and requires calling [allow_unsafe] for the query to execute.

Source

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

Adds a raw SQL filter condition to the query.

Source

pub fn filter_eq( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds an equality filter (column = value).

Source

pub fn filter_ne( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a not-equal filter (column != value).

Source

pub fn filter_lt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a less-than filter (column < value).

Source

pub fn filter_lte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a less-than-or-equal filter (column <= value).

Source

pub fn filter_gt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a greater-than filter (column > value).

Source

pub fn filter_gte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a greater-than-or-equal filter (column >= value).

Source

pub fn filter_like( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self

Adds a LIKE filter (column LIKE value).

Source

pub fn filter_is_null(self, column: impl Into<ColumnRef>) -> Self

Filters rows where the column IS NULL.

Source

pub fn filter_is_not_null(self, column: impl Into<ColumnRef>) -> Self

Filters rows where the column IS NOT NULL.

Source

pub fn filter_in<I, V>(self, column: impl Into<ColumnRef>, values: I) -> Self
where I: IntoIterator<Item = V>, V: Into<BindValue>,

Adds an IN filter (column IN (values...)).

Source

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

Limits the number of rows returned by the query. Sets the maximum number of rows to return.

Source

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

Skips the specified number of rows. Sets the number of rows to skip.

Source

pub fn include(self, relation: impl Into<String>) -> Self

Eager loads a related model.

Source

pub fn with_deleted(self) -> Self

Includes soft-deleted records in the results.

Source

pub fn allow_unsafe(self) -> Self

Explicitly allows potentially unsafe raw filters. Enables execution of queries with raw SQL filters.

Source

pub fn fast(self) -> Self

Enables a fast path that skips logging and metrics for hot queries.

Source

pub fn unsafe_fast(self) -> Self

Enables an unsafe fast path that skips logging, metrics, and safety guards.

Source

pub fn ultra_fast(self) -> Self

Enables the ultra-fast path: skips logging, metrics, safety guards, and eager loading. Note: Any configured includes will be ignored.

Source

pub fn prepared(self) -> Self

Enable prepared statement caching for this query (default: enabled).

Source

pub fn unprepared(self) -> Self

Disable prepared statement caching for this query.

Source

pub fn to_sql(&self) -> String

Returns the SELECT SQL that would be executed for this query.

Source

pub fn to_update_sql(&self, values: &Value) -> Result<String, Error>

Returns the UPDATE SQL that would be executed for this query.

Source

pub fn to_delete_sql(&self) -> String

Returns the DELETE (or soft delete) SQL that would be executed for this query.

Source§

impl<'a, T, DB> QueryBuilder<'a, T, DB>
where DB: SqlDialect, T: Model<DB> + Send, for<'q> <DB as Database>::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c mut <DB as Database>::Connection: Executor<'c, Database = DB>, for<'c> &'c str: ColumnIndex<DB::Row>, DB::Connection: Send, String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB>,

Source

pub async fn all(self) -> Result<Vec<T>, Error>

Executes the query and returns a vector of results.

This method will fetch all rows matching the criteria and then perform eager loading for any included relations.

Source

pub fn stream(self) -> Result<BoxStream<'a, Result<T, Error>>, Error>
where T: 'a,

Executes the query and returns a stream of results.

This is useful for processing large result sets without loading them all into memory.

Source

pub async fn update(self, values: Value) -> Result<u64, Error>
where String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB>,

Executes a bulk update based on the current filters.

§Errors

Returns an error if no filters are provided (unless allow_unsafe is used), or if the values cannot be mapped to the database.

Source

pub async fn update_all(self, values: Value) -> Result<u64, Error>
where String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB>,

Executes a bulk update based on the current filters. Alias for [update].

Source

pub async fn delete(self) -> Result<u64, Error>

Executes a bulk delete based on the current filters.

Source

pub async fn delete_all(self) -> Result<u64, Error>

Executes a bulk delete based on the current filters. Alias for [delete].

Trait Implementations§

Source§

impl<'a, T, DB: Database> Debug for QueryBuilder<'a, T, DB>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T, DB> Freeze for QueryBuilder<'a, T, DB>

§

impl<'a, T, DB> !RefUnwindSafe for QueryBuilder<'a, T, DB>

§

impl<'a, T, DB> Send for QueryBuilder<'a, T, DB>
where T: Send,

§

impl<'a, T, DB> Sync for QueryBuilder<'a, T, DB>
where <DB as Database>::Connection: Sync, T: Sync,

§

impl<'a, T, DB> Unpin for QueryBuilder<'a, T, DB>
where T: Unpin,

§

impl<'a, T, DB> !UnwindSafe for QueryBuilder<'a, T, DB>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more