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>
impl<'a, T, DB> QueryBuilder<'a, T, DB>
Sourcepub fn new(executor: Executor<'a, DB>) -> Self
pub fn new(executor: Executor<'a, DB>) -> Self
Creates a new QueryBuilder using the provided Executor.
Sourcepub fn filter(self, condition: impl Into<String>) -> Self
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.
Sourcepub fn filter_raw(self, condition: impl Into<String>) -> Self
pub fn filter_raw(self, condition: impl Into<String>) -> Self
Adds a raw SQL filter condition to the query.
Sourcepub fn filter_eq(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_eq( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds an equality filter (column = value).
Sourcepub fn filter_ne(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_ne( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a not-equal filter (column != value).
Sourcepub fn filter_lt(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_lt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a less-than filter (column < value).
Sourcepub fn filter_lte(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_lte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a less-than-or-equal filter (column <= value).
Sourcepub fn filter_gt(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_gt( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a greater-than filter (column > value).
Sourcepub fn filter_gte(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_gte( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a greater-than-or-equal filter (column >= value).
Sourcepub fn filter_like(
self,
column: impl Into<ColumnRef>,
value: impl Into<BindValue>,
) -> Self
pub fn filter_like( self, column: impl Into<ColumnRef>, value: impl Into<BindValue>, ) -> Self
Adds a LIKE filter (column LIKE value).
Sourcepub fn filter_is_null(self, column: impl Into<ColumnRef>) -> Self
pub fn filter_is_null(self, column: impl Into<ColumnRef>) -> Self
Filters rows where the column IS NULL.
Sourcepub fn filter_is_not_null(self, column: impl Into<ColumnRef>) -> Self
pub fn filter_is_not_null(self, column: impl Into<ColumnRef>) -> Self
Filters rows where the column IS NOT NULL.
Sourcepub fn filter_in<I, V>(self, column: impl Into<ColumnRef>, values: I) -> Selfwhere
I: IntoIterator<Item = V>,
V: Into<BindValue>,
pub fn filter_in<I, V>(self, column: impl Into<ColumnRef>, values: I) -> Selfwhere
I: IntoIterator<Item = V>,
V: Into<BindValue>,
Adds an IN filter (column IN (values...)).
Sourcepub fn limit(self, limit: i32) -> Self
pub fn limit(self, limit: i32) -> Self
Limits the number of rows returned by the query. Sets the maximum number of rows to return.
Sourcepub fn offset(self, offset: i32) -> Self
pub fn offset(self, offset: i32) -> Self
Skips the specified number of rows. Sets the number of rows to skip.
Sourcepub fn with_deleted(self) -> Self
pub fn with_deleted(self) -> Self
Includes soft-deleted records in the results.
Sourcepub fn allow_unsafe(self) -> Self
pub fn allow_unsafe(self) -> Self
Explicitly allows potentially unsafe raw filters. Enables execution of queries with raw SQL filters.
Sourcepub fn unsafe_fast(self) -> Self
pub fn unsafe_fast(self) -> Self
Enables an unsafe fast path that skips logging, metrics, and safety guards.
Sourcepub fn ultra_fast(self) -> Self
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.
Sourcepub fn prepared(self) -> Self
pub fn prepared(self) -> Self
Enable prepared statement caching for this query (default: enabled).
Sourcepub fn unprepared(self) -> Self
pub fn unprepared(self) -> Self
Disable prepared statement caching for this query.
Sourcepub fn to_update_sql(&self, values: &Value) -> Result<String, Error>
pub fn to_update_sql(&self, values: &Value) -> Result<String, Error>
Returns the UPDATE SQL that would be executed for this query.
Sourcepub fn to_delete_sql(&self) -> String
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>,
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>,
Sourcepub async fn all(self) -> Result<Vec<T>, Error>
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.
Sourcepub fn stream(self) -> Result<BoxStream<'a, Result<T, Error>>, Error>where
T: 'a,
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.
Sourcepub 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>,
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.
Sourcepub 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>,
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].
Sourcepub async fn delete(self) -> Result<u64, Error>
pub async fn delete(self) -> Result<u64, Error>
Executes a bulk delete based on the current filters.
Sourcepub async fn delete_all(self) -> Result<u64, Error>
pub async fn delete_all(self) -> Result<u64, Error>
Executes a bulk delete based on the current filters. Alias for [delete].
Trait Implementations§
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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