pub struct QueryBuilder<M = ()> { /* private fields */ }Expand description
Query builder for constructing database queries
Implementations§
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn insert_into(self, table: &str) -> Self
pub fn insert_into(self, table: &str) -> Self
Start an INSERT query
Sourcepub fn delete_from(self, table: &str) -> Self
pub fn delete_from(self, table: &str) -> Self
Start a DELETE query
Sourcepub fn set<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn set<T: Into<Value>>(self, column: &str, value: T) -> Self
Set a column value (for INSERT/UPDATE)
Sourcepub fn set_values(self, values: Vec<(String, Value)>) -> Self
pub fn set_values(self, values: Vec<(String, Value)>) -> Self
Set multiple values at once
Source§impl<M: Model> QueryBuilder<M>
impl<M: Model> QueryBuilder<M>
Sourcepub async fn get(self, pool: &Pool<Postgres>) -> ModelResult<Vec<M>>
pub async fn get(self, pool: &Pool<Postgres>) -> ModelResult<Vec<M>>
Execute query and return models
Sourcepub async fn chunk<F>(
self,
pool: &Pool<Postgres>,
chunk_size: i64,
callback: F,
) -> ModelResult<()>
pub async fn chunk<F>( self, pool: &Pool<Postgres>, chunk_size: i64, callback: F, ) -> ModelResult<()>
Execute query with chunking for large datasets
Sourcepub async fn get_raw(self, pool: &Pool<Postgres>) -> ModelResult<Vec<Value>>
pub async fn get_raw(self, pool: &Pool<Postgres>) -> ModelResult<Vec<Value>>
Execute query and return raw SQL results (for complex aggregations)
Sourcepub async fn first(self, pool: &Pool<Postgres>) -> ModelResult<Option<M>>
pub async fn first(self, pool: &Pool<Postgres>) -> ModelResult<Option<M>>
Execute query and return first model
Sourcepub async fn first_or_fail(self, pool: &Pool<Postgres>) -> ModelResult<M>
pub async fn first_or_fail(self, pool: &Pool<Postgres>) -> ModelResult<M>
Execute query and return first model or error
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn join(self, table: &str, left_col: &str, right_col: &str) -> Self
pub fn join(self, table: &str, left_col: &str, right_col: &str) -> Self
Add INNER JOIN to the query
Sourcepub fn left_join(self, table: &str, left_col: &str, right_col: &str) -> Self
pub fn left_join(self, table: &str, left_col: &str, right_col: &str) -> Self
Add LEFT JOIN to the query
Sourcepub fn right_join(self, table: &str, left_col: &str, right_col: &str) -> Self
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>
impl<M> QueryBuilder<M>
Sourcepub fn order_by_desc(self, column: &str) -> Self
pub fn order_by_desc(self, column: &str) -> Self
Add ORDER BY clause (descending)
Sourcepub fn having_eq<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn having_eq<T: Into<Value>>(self, column: &str, value: T) -> Self
Add HAVING clause with equals
Sourcepub fn having<T: Into<Value>>(
self,
column: &str,
operator: QueryOperator,
value: T,
) -> Self
pub fn having<T: Into<Value>>( self, column: &str, operator: QueryOperator, value: T, ) -> Self
Add HAVING clause with custom operator and value
Sourcepub fn having_raw(self, raw_condition: &str) -> Self
pub fn having_raw(self, raw_condition: &str) -> Self
Add raw HAVING clause
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn paginate_cursor<T: Into<Value>>(
self,
cursor_column: &str,
cursor_value: Option<T>,
per_page: i64,
direction: OrderDirection,
) -> Self
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>
impl<M> QueryBuilder<M>
Sourcepub fn bindings(&self) -> Vec<Value>
pub fn bindings(&self) -> Vec<Value>
Get parameter bindings (for prepared statements) Enhanced to support subqueries and complex conditions
Sourcepub fn clone_for_subquery(&self) -> Self
pub fn clone_for_subquery(&self) -> Self
Clone this query builder for use in subqueries
Sourcepub fn complexity_score(&self) -> u32
pub fn complexity_score(&self) -> u32
Get query complexity score for performance monitoring
Source§impl<M> QueryBuilder<M>
Performance-optimized SQL generation with caching
impl<M> QueryBuilder<M>
Performance-optimized SQL generation with caching
Sourcepub fn generate_placeholders_cached(count: usize) -> String
pub fn generate_placeholders_cached(count: usize) -> String
Generate parameter placeholders with caching for better performance
Sourcepub fn generate_sequential_placeholders(
start_index: usize,
count: usize,
) -> String
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
Sourcepub fn to_sql_optimized(&self) -> String
pub fn to_sql_optimized(&self) -> String
Optimized SQL generation with pre-allocated capacity
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn select_distinct(self, fields: &str) -> Self
pub fn select_distinct(self, fields: &str) -> Self
Add SELECT DISTINCT to the query
Sourcepub fn select_count(self, column: &str, alias: Option<&str>) -> Self
pub fn select_count(self, column: &str, alias: Option<&str>) -> Self
Add aggregate functions to SELECT
Sourcepub fn select_sum(self, column: &str, alias: Option<&str>) -> Self
pub fn select_sum(self, column: &str, alias: Option<&str>) -> Self
Add SUM aggregate
Sourcepub fn select_avg(self, column: &str, alias: Option<&str>) -> Self
pub fn select_avg(self, column: &str, alias: Option<&str>) -> Self
Add AVG aggregate
Sourcepub fn select_min(self, column: &str, alias: Option<&str>) -> Self
pub fn select_min(self, column: &str, alias: Option<&str>) -> Self
Add MIN aggregate
Sourcepub fn select_max(self, column: &str, alias: Option<&str>) -> Self
pub fn select_max(self, column: &str, alias: Option<&str>) -> Self
Add MAX aggregate
Sourcepub fn select_raw(self, expression: &str) -> Self
pub fn select_raw(self, expression: &str) -> Self
Add custom SELECT expression
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn where_ne<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn where_ne<T: Into<Value>>(self, column: &str, value: T) -> Self
Add WHERE condition with not equal
Sourcepub fn where_gt<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn where_gt<T: Into<Value>>(self, column: &str, value: T) -> Self
Add WHERE condition with greater than
Sourcepub fn where_gte<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn where_gte<T: Into<Value>>(self, column: &str, value: T) -> Self
Add WHERE condition with greater than or equal
Sourcepub fn where_lt<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn where_lt<T: Into<Value>>(self, column: &str, value: T) -> Self
Add WHERE condition with less than
Sourcepub fn where_lte<T: Into<Value>>(self, column: &str, value: T) -> Self
pub fn where_lte<T: Into<Value>>(self, column: &str, value: T) -> Self
Add WHERE condition with less than or equal
Sourcepub fn where_like(self, column: &str, pattern: &str) -> Self
pub fn where_like(self, column: &str, pattern: &str) -> Self
Add WHERE condition with LIKE
Sourcepub fn where_not_like(self, column: &str, pattern: &str) -> Self
pub fn where_not_like(self, column: &str, pattern: &str) -> Self
Add WHERE condition with NOT LIKE
Sourcepub fn where_condition<T: Into<Value>>(
self,
column: &str,
operator: &str,
value: T,
) -> Self
pub fn where_condition<T: Into<Value>>( self, column: &str, operator: &str, value: T, ) -> Self
Add WHERE condition with custom operator
Sourcepub fn where_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self
pub fn where_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self
Add WHERE condition with IN
Sourcepub fn where_not_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self
pub fn where_not_in<T: Into<Value>>(self, column: &str, values: Vec<T>) -> Self
Add WHERE condition with NOT IN
Sourcepub fn where_null(self, column: &str) -> Self
pub fn where_null(self, column: &str) -> Self
Add WHERE condition with IS NULL
Sourcepub fn where_not_null(self, column: &str) -> Self
pub fn where_not_null(self, column: &str) -> Self
Add WHERE condition with IS NOT NULL
Sourcepub fn where_between<T: Into<Value>>(
self,
column: &str,
start: T,
end: T,
) -> Self
pub fn where_between<T: Into<Value>>( self, column: &str, start: T, end: T, ) -> Self
Add WHERE condition with BETWEEN
Sourcepub fn where_subquery<T>(
self,
column: &str,
operator: QueryOperator,
subquery: QueryBuilder<T>,
) -> Self
pub fn where_subquery<T>( self, column: &str, operator: QueryOperator, subquery: QueryBuilder<T>, ) -> Self
Add a subquery in the WHERE clause
Sourcepub fn where_exists<T>(self, subquery: QueryBuilder<T>) -> Self
pub fn where_exists<T>(self, subquery: QueryBuilder<T>) -> Self
Add EXISTS subquery condition
Sourcepub fn where_not_exists<T>(self, subquery: QueryBuilder<T>) -> Self
pub fn where_not_exists<T>(self, subquery: QueryBuilder<T>) -> Self
Add NOT EXISTS subquery condition
Source§impl<M> QueryBuilder<M>
impl<M> QueryBuilder<M>
Sourcepub fn to_sql_with_params(&self) -> (String, Vec<String>)
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
Sourcepub fn to_sql_with_params_secure(
&self,
) -> Result<(String, Vec<String>), ModelError>
pub fn to_sql_with_params_secure( &self, ) -> Result<(String, Vec<String>), ModelError>
Generate SQL with security validation - returns Result for proper error handling
Trait Implementations§
Source§impl<M> Clone for QueryBuilder<M>
impl<M> Clone for QueryBuilder<M>
Source§impl<M: Debug> Debug for QueryBuilder<M>
impl<M: Debug> Debug for QueryBuilder<M>
Source§impl<M> Default for QueryBuilder<M>
impl<M> Default for QueryBuilder<M>
Source§impl<M> QueryBuilderWithMethods<M> for QueryBuilder<M>
Implementation for base QueryBuilder to support eager loading
impl<M> QueryBuilderWithMethods<M> for QueryBuilder<M>
Implementation for base QueryBuilder to support eager loading
Source§fn with(self, relation: &str) -> QueryBuilderWithEagerLoading<M>
fn with(self, relation: &str) -> QueryBuilderWithEagerLoading<M>
Source§fn with_where<F>(
self,
relation: &str,
constraint: F,
) -> QueryBuilderWithEagerLoading<M>
fn with_where<F>( self, relation: &str, constraint: F, ) -> QueryBuilderWithEagerLoading<M>
Source§fn with_when(
self,
condition: bool,
relation: &str,
) -> QueryBuilderWithEagerLoading<M>
fn with_when( self, condition: bool, relation: &str, ) -> QueryBuilderWithEagerLoading<M>
Source§fn with_count(self, relation: &str) -> QueryBuilderWithEagerLoading<M>
fn with_count(self, relation: &str) -> QueryBuilderWithEagerLoading<M>
Source§fn with_count_where<F>(
self,
alias: &str,
relation: &str,
constraint: F,
) -> QueryBuilderWithEagerLoading<M>
fn with_count_where<F>( self, alias: &str, relation: &str, constraint: F, ) -> QueryBuilderWithEagerLoading<M>
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> 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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