Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<'a, T, DB>
where 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>) -> QueryBuilder<'a, T, DB>

Creates a new QueryBuilder using the provided Executor.

Source

pub fn filter(self, condition: impl Into<String>) -> QueryBuilder<'a, T, DB>

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>) -> QueryBuilder<'a, T, DB>

Adds a raw SQL filter condition to the query.

Source

pub fn filter_eq( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

Adds an equality filter (column = value).

Source

pub fn filter_ne( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

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

Source

pub fn filter_lt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

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

Source

pub fn filter_lte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

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

Source

pub fn filter_gt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

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

Source

pub fn filter_gte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

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

Source

pub fn filter_like( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> QueryBuilder<'a, T, DB>

Adds a LIKE filter (column LIKE value).

Source

pub fn filter_is_null( self, column: impl Into<ColumnRef>, ) -> QueryBuilder<'a, T, DB>

Filters rows where the column IS NULL.

Source

pub fn filter_is_not_null( self, column: impl Into<ColumnRef>, ) -> QueryBuilder<'a, T, DB>

Filters rows where the column IS NOT NULL.

Source

pub fn filter_in<I, V>( self, column: impl Into<ColumnRef>, values: I, ) -> QueryBuilder<'a, T, DB>
where I: IntoIterator<Item = V>, V: Into<BindValue>,

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

Source

pub fn limit(self, limit: i32) -> QueryBuilder<'a, T, DB>

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

Source

pub fn offset(self, offset: i32) -> QueryBuilder<'a, T, DB>

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

Source

pub fn include(self, relation: impl Into<String>) -> QueryBuilder<'a, T, DB>

Eager loads a related model.

Source

pub fn with_deleted(self) -> QueryBuilder<'a, T, DB>

Includes soft-deleted records in the results.

Source

pub fn allow_unsafe(self) -> QueryBuilder<'a, T, DB>

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

Source

pub fn fast(self) -> QueryBuilder<'a, T, DB>

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

Source

pub fn unsafe_fast(self) -> QueryBuilder<'a, T, DB>

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

Source

pub fn ultra_fast(self) -> QueryBuilder<'a, T, DB>

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) -> QueryBuilder<'a, T, DB>

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

Source

pub fn unprepared(self) -> QueryBuilder<'a, T, DB>

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, <DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>, &'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>, &'c str: for<'c> ColumnIndex<<DB as Database>::Row>, <DB as Database>::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>,

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<Pin<Box<dyn Stream<Item = Result<T, Error>> + Send + 'a>>, 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>,

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>,

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> Debug for QueryBuilder<'a, T, DB>
where DB: Database,

Source§

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

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,