QueryBuilder

Struct QueryBuilder 

Source
pub struct QueryBuilder<M = ()> { /* private fields */ }
Expand description

Query builder for constructing database queries

Implementations§

Source§

impl<M> QueryBuilder<M>

Source

pub fn new() -> Self

Create a new query builder

Source§

impl<M> QueryBuilder<M>

Source

pub fn insert_into(self, table: &str) -> Self

Start an INSERT query

Source

pub fn update(self, table: &str) -> Self

Start an UPDATE query

Source

pub fn delete_from(self, table: &str) -> Self

Start a DELETE query

Source

pub fn set<T: Into<Value>>(self, column: &str, value: T) -> Self

Set a column value (for INSERT/UPDATE)

Source

pub fn set_null(self, column: &str) -> Self

Set a column to NULL (for INSERT/UPDATE)

Source

pub fn set_values(self, values: Vec<(String, Value)>) -> Self

Set multiple values at once

Source

pub fn upsert( self, table: &str, conflict_columns: Vec<&str>, ) -> UpsertBuilder<M>

Upsert operation (INSERT … ON CONFLICT UPDATE)

Source§

impl<M: Model> QueryBuilder<M>

Source

pub async fn get(self, pool: &Pool<Postgres>) -> ModelResult<Vec<M>>

Execute query and return models

Source

pub async fn chunk<F>( self, pool: &Pool<Postgres>, chunk_size: i64, callback: F, ) -> ModelResult<()>
where F: FnMut(Vec<M>) -> Result<(), ModelError>,

Execute query with chunking for large datasets

Source

pub async fn get_raw(self, pool: &Pool<Postgres>) -> ModelResult<Vec<Value>>

Execute query and return raw SQL results (for complex aggregations)

Source

pub async fn first(self, pool: &Pool<Postgres>) -> ModelResult<Option<M>>

Execute query and return first model

Source

pub async fn first_or_fail(self, pool: &Pool<Postgres>) -> ModelResult<M>

Execute query and return first model or error

Source

pub async fn count(self, pool: &Pool<Postgres>) -> ModelResult<i64>

Count query results

Source

pub async fn aggregate( self, pool: &Pool<Postgres>, ) -> ModelResult<Option<Value>>

Execute aggregation query and return single result

Source§

impl<M> QueryBuilder<M>

Source

pub fn join(self, table: &str, left_col: &str, right_col: &str) -> Self

Add INNER JOIN to the query

Source

pub fn left_join(self, table: &str, left_col: &str, right_col: &str) -> Self

Add LEFT JOIN to the query

Source

pub fn right_join(self, table: &str, left_col: &str, right_col: &str) -> Self

Add RIGHT JOIN to the query

Source§

impl<M> QueryBuilder<M>

Source

pub fn order_by(self, column: &str) -> Self

Add ORDER BY clause (ascending)

Source

pub fn order_by_desc(self, column: &str) -> Self

Add ORDER BY clause (descending)

Source

pub fn group_by(self, column: &str) -> Self

Add GROUP BY clause

Source

pub fn having_eq<T: Into<Value>>(self, column: &str, value: T) -> Self

Add HAVING clause with equals

Source

pub fn having<T: Into<Value>>( self, column: &str, operator: QueryOperator, value: T, ) -> Self

Add HAVING clause with custom operator and value

Source

pub fn having_raw(self, raw_condition: &str) -> Self

Add raw HAVING clause

Source§

impl<M> QueryBuilder<M>

Source

pub fn limit(self, count: i64) -> Self

Add LIMIT clause

Source

pub fn offset(self, count: i64) -> Self

Add OFFSET clause

Source

pub fn paginate(self, per_page: i64, page: i64) -> Self

Add pagination (LIMIT + OFFSET)

Source

pub fn paginate_cursor<T: Into<Value>>( self, cursor_column: &str, cursor_value: Option<T>, per_page: i64, direction: OrderDirection, ) -> Self

Cursor-based pagination (for better performance on large datasets)

Source§

impl<M> QueryBuilder<M>

Source

pub fn bindings(&self) -> Vec<Value>

Get parameter bindings (for prepared statements) Enhanced to support subqueries and complex conditions

Source

pub fn clone_for_subquery(&self) -> Self

Clone this query builder for use in subqueries

Source

pub fn optimize(self) -> Self

Optimize query by analyzing conditions

Source

pub fn complexity_score(&self) -> u32

Get query complexity score for performance monitoring

Source§

impl<M> QueryBuilder<M>

Performance-optimized SQL generation with caching

Source

pub fn generate_placeholders_cached(count: usize) -> String

Generate parameter placeholders with caching for better performance

Source

pub fn generate_sequential_placeholders( start_index: usize, count: usize, ) -> String

Generate sequential parameter placeholders starting from a specific index Used for proper parameter ordering in complex queries

Source

pub fn to_sql_optimized(&self) -> String

Optimized SQL generation with pre-allocated capacity

Source§

impl<M> QueryBuilder<M>

Source

pub fn reset(&mut self)

Reset query builder to default state for reuse

Source§

impl<M> QueryBuilder<M>

Source

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

Add SELECT fields to the query

Source

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

Add SELECT DISTINCT to the query

Source

pub fn from(self, table: &str) -> Self

Set the FROM table

Source

pub fn select_count(self, column: &str, alias: Option<&str>) -> Self

Add aggregate functions to SELECT

Source

pub fn select_sum(self, column: &str, alias: Option<&str>) -> Self

Add SUM aggregate

Source

pub fn select_avg(self, column: &str, alias: Option<&str>) -> Self

Add AVG aggregate

Source

pub fn select_min(self, column: &str, alias: Option<&str>) -> Self

Add MIN aggregate

Source

pub fn select_max(self, column: &str, alias: Option<&str>) -> Self

Add MAX aggregate

Source

pub fn select_raw(self, expression: &str) -> Self

Add custom SELECT expression

Source§

impl<M> QueryBuilder<M>

Source

pub fn where_eq<T>(self, column: &str, value: T) -> Self
where T: Into<Value>,

Add WHERE condition with equality

Source

pub fn where_ne<T: Into<Value>>(self, column: &str, value: T) -> Self

Add WHERE condition with not equal

Source

pub fn where_gt<T: Into<Value>>(self, column: &str, value: T) -> Self

Add WHERE condition with greater than

Source

pub fn where_gte<T: Into<Value>>(self, column: &str, value: T) -> Self

Add WHERE condition with greater than or equal

Source

pub fn where_lt<T: Into<Value>>(self, column: &str, value: T) -> Self

Add WHERE condition with less than

Source

pub fn where_lte<T: Into<Value>>(self, column: &str, value: T) -> Self

Add WHERE condition with less than or equal

Source

pub fn where_like(self, column: &str, pattern: &str) -> Self

Add WHERE condition with LIKE

Source

pub fn where_not_like(self, column: &str, pattern: &str) -> Self

Add WHERE condition with NOT LIKE

Source

pub fn where_condition<T: Into<Value>>( self, column: &str, operator: &str, value: T, ) -> Self

Add WHERE condition with custom operator

Source

pub fn where_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self

Add WHERE condition with IN

Source

pub fn where_not_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self

Add WHERE condition with NOT IN

Source

pub fn where_null(self, column: &str) -> Self

Add WHERE condition with IS NULL

Source

pub fn where_not_null(self, column: &str) -> Self

Add WHERE condition with IS NOT NULL

Source

pub fn where_between<T: Into<Value>>( self, column: &str, start: T, end: T, ) -> Self

Add WHERE condition with BETWEEN

Source

pub fn where_raw(self, raw_condition: &str) -> Self

Add raw WHERE condition for complex cases

Source

pub fn where_subquery<T>( self, column: &str, operator: QueryOperator, subquery: QueryBuilder<T>, ) -> Self

Add a subquery in the WHERE clause

Source

pub fn where_exists<T>(self, subquery: QueryBuilder<T>) -> Self

Add EXISTS subquery condition

Source

pub fn where_not_exists<T>(self, subquery: QueryBuilder<T>) -> Self

Add NOT EXISTS subquery condition

Source§

impl<M> QueryBuilder<M>

Source

pub fn to_sql_with_params(&self) -> (String, Vec<String>)

Generate SQL from query with parameter placeholders and return parameters This method includes basic identifier escaping for security

Source

pub fn to_sql_with_params_secure( &self, ) -> Result<(String, Vec<String>), ModelError>

Generate SQL with security validation - returns Result for proper error handling

Source

pub fn to_sql(&self) -> String

Convert the query to SQL string (for backwards compatibility)

Trait Implementations§

Source§

impl<M> Clone for QueryBuilder<M>

Source§

fn clone(&self) -> Self

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<M: Debug> Debug for QueryBuilder<M>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<M> Default for QueryBuilder<M>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<M> QueryBuilderWithMethods<M> for QueryBuilder<M>
where M: Model + Send + Sync,

Implementation for base QueryBuilder to support eager loading

Source§

fn with(self, relation: &str) -> QueryBuilderWithEagerLoading<M>

Add a relationship to eagerly load
Source§

fn with_where<F>( self, relation: &str, constraint: F, ) -> QueryBuilderWithEagerLoading<M>

Add a relationship with constraints
Source§

fn with_when( self, condition: bool, relation: &str, ) -> QueryBuilderWithEagerLoading<M>

Add conditional eager loading
Source§

fn with_count(self, relation: &str) -> QueryBuilderWithEagerLoading<M>

Load relationship counts without loading the relationships
Source§

fn with_count_where<F>( self, alias: &str, relation: &str, constraint: F, ) -> QueryBuilderWithEagerLoading<M>

Load relationship counts with constraints

Auto Trait Implementations§

§

impl<M> Freeze for QueryBuilder<M>

§

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

§

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

§

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

§

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

§

impl<M> UnwindSafe for QueryBuilder<M>
where M: 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> 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> 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.
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<T> ErasedDestructor for T
where T: 'static,