pub struct QueryBuilder { /* private fields */ }
Expand description
SQL query builder for complex queries
Provides a fluent interface for building SQL queries with support for:
- Column selection and table joins
- WHERE clauses with complex filtering
- GROUP BY and HAVING clauses
- ORDER BY with multiple sort criteria
- LIMIT and OFFSET for pagination
- Aggregate functions (COUNT, SUM, AVG, etc.)
- DISTINCT queries
§Examples
use libsql_orm::{QueryBuilder, FilterOperator, Sort, SortOrder, JoinType};
// Basic query
let query = QueryBuilder::new("users")
.select(vec!["id", "name", "email"])
.r#where(FilterOperator::Eq("is_active".to_string(), Value::Boolean(true)))
.order_by(Sort::new("name", SortOrder::Asc))
.limit(10);
// Query with joins
let joined_query = QueryBuilder::new("posts")
.select(vec!["posts.title", "users.name"])
.join(JoinType::Inner, "users", "users.id = posts.user_id")
.r#where(FilterOperator::Eq("posts.published".to_string(), Value::Boolean(true)));
// Aggregate query
let agg_query = QueryBuilder::new("orders")
.aggregate(Aggregate::Sum, "amount", Some("total_amount"))
.group_by(vec!["user_id"])
.having(FilterOperator::Gt("total_amount".to_string(), Value::Real(1000.0)));
Implementations§
Source§impl QueryBuilder
impl QueryBuilder
Sourcepub fn join(
self,
join_type: JoinType,
table: impl Into<String>,
condition: impl Into<String>,
) -> Self
pub fn join( self, join_type: JoinType, table: impl Into<String>, condition: impl Into<String>, ) -> Self
Add a join clause
Sourcepub fn join_as(
self,
join_type: JoinType,
table: impl Into<String>,
alias: impl Into<String>,
condition: impl Into<String>,
) -> Self
pub fn join_as( self, join_type: JoinType, table: impl Into<String>, alias: impl Into<String>, condition: impl Into<String>, ) -> Self
Add a join clause with alias
Sourcepub fn where(self, filter: FilterOperator) -> Self
pub fn where(self, filter: FilterOperator) -> Self
Add a where clause
Sourcepub fn having(self, filter: FilterOperator) -> Self
pub fn having(self, filter: FilterOperator) -> Self
Add a having clause
Sourcepub fn order_by_multiple(self, sorts: Vec<Sort>) -> Self
pub fn order_by_multiple(self, sorts: Vec<Sort>) -> Self
Add multiple order by clauses
Sourcepub fn aggregate(
self,
function: Aggregate,
column: impl Into<String>,
alias: Option<impl Into<String>>,
) -> Self
pub fn aggregate( self, function: Aggregate, column: impl Into<String>, alias: Option<impl Into<String>>, ) -> Self
Set aggregate function
Sourcepub fn select_all(self) -> Self
pub fn select_all(self) -> Self
Select all columns
Sourcepub fn select_columns(self, columns: &[&str]) -> Self
pub fn select_columns(self, columns: &[&str]) -> Self
Select specific columns
Sourcepub fn select_column(self, column: &str) -> Self
pub fn select_column(self, column: &str) -> Self
Select a single column
Sourcepub fn select_count(self) -> Self
pub fn select_count(self) -> Self
Select count
Sourcepub fn select_aggregate(self, aggregate: &str) -> Self
pub fn select_aggregate(self, aggregate: &str) -> Self
Select aggregate
Sourcepub fn select_distinct(self, column: &str) -> Self
pub fn select_distinct(self, column: &str) -> Self
Select distinct
Sourcepub fn where_condition(
self,
condition: &str,
_params: impl Into<Vec<Value>>,
) -> Self
pub fn where_condition( self, condition: &str, _params: impl Into<Vec<Value>>, ) -> Self
Add where condition
Sourcepub fn with_filter(self, filter: Filter) -> Self
pub fn with_filter(self, filter: Filter) -> Self
Add filter
Sourcepub fn with_filters(self, filters: Vec<Filter>) -> Self
pub fn with_filters(self, filters: Vec<Filter>) -> Self
Add filters
Sourcepub fn with_sorts(self, sorts: Vec<Sort>) -> Self
pub fn with_sorts(self, sorts: Vec<Sort>) -> Self
Add sorts
Sourcepub fn having_condition(
self,
condition: &str,
_params: impl Into<Vec<Value>>,
) -> Self
pub fn having_condition( self, condition: &str, _params: impl Into<Vec<Value>>, ) -> Self
Add having condition
Sourcepub fn where_in(self, field: &str, subquery: QueryBuilder) -> Self
pub fn where_in(self, field: &str, subquery: QueryBuilder) -> Self
Add where in clause
Sourcepub async fn execute_count(&self, db: &Database) -> Result<u64>
pub async fn execute_count(&self, db: &Database) -> Result<u64>
Execute count query
Sourcepub async fn execute_aggregate(&self, db: &Database) -> Result<Vec<Row>>
pub async fn execute_aggregate(&self, db: &Database) -> Result<Vec<Row>>
Execute aggregate query
Sourcepub async fn execute<T>(&self, db: &Database) -> Result<Vec<T>>where
T: DeserializeOwned,
pub async fn execute<T>(&self, db: &Database) -> Result<Vec<T>>where
T: DeserializeOwned,
Execute the query
Sourcepub async fn execute_paginated<T>(
&self,
db: &Database,
pagination: &Pagination,
) -> Result<PaginatedResult<T>>where
T: DeserializeOwned,
pub async fn execute_paginated<T>(
&self,
db: &Database,
pagination: &Pagination,
) -> Result<PaginatedResult<T>>where
T: DeserializeOwned,
Execute the query with pagination
Trait Implementations§
Auto Trait Implementations§
impl Freeze for QueryBuilder
impl RefUnwindSafe for QueryBuilder
impl Send for QueryBuilder
impl Sync for QueryBuilder
impl Unpin for QueryBuilder
impl UnwindSafe for QueryBuilder
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
Mutably borrows from an owned value. Read more