pub type SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> = QueryBuilder<'a, Schema, State, Table, Marker, Row, Grouped>;Expand description
Builds a SELECT query specifically for SQLite.
SelectBuilder provides a type-safe, fluent API for constructing SELECT statements
with compile-time verification of query structure and table relationships.
§Type Parameters
Schema: The database schema type, ensuring only valid tables can be referencedState: The current builder state, enforcing proper query construction orderTable: The primary table being queried (when applicable)
§Query Building Flow
- Start with
QueryBuilder::select()to specify columns - Add
from()to specify the source table - Optionally add joins, conditions, grouping, ordering, and limits
§Basic Usage
use drizzle::sqlite::prelude::*;
use drizzle::sqlite::builder::QueryBuilder;
#[SQLiteTable(name = "users")]
struct User {
#[column(primary)]
id: i32,
name: String,
email: Option<String>,
}
#[derive(SQLiteSchema)]
struct Schema {
user: User,
}
let builder = QueryBuilder::new::<Schema>();
let Schema { user } = Schema::new();
// Basic SELECT
let query = builder.select(user.name).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users""#);
// SELECT with WHERE clause
use drizzle::core::expr::gt;
let query = builder
.select((user.id, user.name))
.from(user)
.r#where(gt(user.id, 10));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."id" > ?"#
);§Advanced Queries
let query = builder
.select((user.name, post.title))
.from(user)
.join((post, eq(user.id, post.user_id)));let query = builder
.select(user.name)
.from(user)
.order_by(asc(user.name))
.limit(10);Aliased Type§
pub struct SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> {
pub sql: SQL<'a, SQLiteValue<'a>>,
/* private fields */
}Fields§
§sql: SQL<'a, SQLiteValue<'a>>Implementations§
Source§impl<'a, S, M> SelectBuilder<'a, S, SelectInitial, (), M>
impl<'a, S, M> SelectBuilder<'a, S, SelectInitial, (), M>
Sourcepub fn from<T>(
self,
query: T,
) -> SelectBuilder<'a, S, SelectFromSet, T, Scoped<M, Cons<T, Nil>>, <M as ResolveRow<T>>::Row>
pub fn from<T>( self, query: T, ) -> SelectBuilder<'a, S, SelectFromSet, T, Scoped<M, Cons<T, Nil>>, <M as ResolveRow<T>>::Row>
Specifies the table or subquery to select FROM.
This method transitions the builder from the initial state to the FROM state, enabling subsequent WHERE, JOIN, ORDER BY, and other clauses.
The row type R is resolved from the select marker M and the table T
via the ResolveRow trait.
§Examples
// Select from a table
let query = builder.select(user.name).from(user);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users""#);Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: JoinAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: JoinAllowed,
Sourcepub fn join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterJoin<R, J::JoinedTable>>::NewRow, G>
pub fn join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterJoin<R, J::JoinedTable>>::NewRow, G>
Adds an INNER JOIN clause to the query.
Joins another table to the current query using the specified condition. The joined table must be part of the schema and the condition should relate columns from both tables.
let query = builder
.select((user.name, post.title))
.from(user)
.join((post, eq(user.id, post.user_id)));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name", "posts"."title" FROM "users" JOIN "posts" ON "users"."id" = "posts"."user_id""#
);pub fn natural_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_left_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterLeftJoin<R, J::JoinedTable>>::NewRow, G>
pub fn left_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterLeftJoin<R, J::JoinedTable>>::NewRow, G>
pub fn left_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterLeftJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_left_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterLeftJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_right_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterRightJoin<R, J::JoinedTable>>::NewRow, G>
pub fn right_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterRightJoin<R, J::JoinedTable>>::NewRow, G>
pub fn right_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterRightJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_right_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterRightJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_full_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterFullJoin<R, J::JoinedTable>>::NewRow, G>
pub fn full_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterFullJoin<R, J::JoinedTable>>::NewRow, G>
pub fn full_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterFullJoin<R, J::JoinedTable>>::NewRow, G>
pub fn natural_full_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterFullJoin<R, J::JoinedTable>>::NewRow, G>
pub fn inner_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterJoin<R, J::JoinedTable>>::NewRow, G>
pub fn cross_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as ScopePush<J::JoinedTable>>::Out, <M as AfterJoin<R, J::JoinedTable>>::NewRow, G>
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: WhereAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: WhereAllowed,
Sourcepub fn where<E>(
self,
condition: E,
) -> SelectBuilder<'a, S, SelectWhereSet, T, M, R, G>
pub fn where<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectWhereSet, T, M, R, G>
Adds a WHERE clause to filter query results.
// Single condition
let query = builder
.select(user.name)
.from(user)
.r#where(gt(user.id, 10));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" WHERE "users"."id" > ?"#
);
// Multiple conditions
let query = builder
.select(user.name)
.from(user)
.r#where(and(gt(user.id, 10), eq(user.name, "Alice")));Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: GroupByAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: GroupByAllowed,
Sourcepub fn group_by<Gr>(
self,
columns: Gr,
) -> SelectBuilder<'a, S, SelectGroupSet, T, M, R, Gr::Columns>where
Gr: IntoGroupBy<'a, SQLiteValue<'a>>,
pub fn group_by<Gr>(
self,
columns: Gr,
) -> SelectBuilder<'a, S, SelectGroupSet, T, M, R, Gr::Columns>where
Gr: IntoGroupBy<'a, SQLiteValue<'a>>,
Adds a GROUP BY clause to the query.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: HavingAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: HavingAllowed,
Sourcepub fn having<E>(
self,
condition: E,
) -> SelectBuilder<'a, S, SelectGroupSet, T, M, R, G>
pub fn having<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectGroupSet, T, M, R, G>
Adds a HAVING clause after GROUP BY.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: OrderByAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: OrderByAllowed,
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T, M, R, G>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T, M, R, G>where
TOrderBy: ToSQL<'a, SQLiteValue<'a>>,
Sorts the query results.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: LimitAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: LimitAllowed,
Sourcepub fn limit(
self,
limit: usize,
) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>
pub fn limit( self, limit: usize, ) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>
Limits the number of rows returned.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: OffsetAllowed,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: OffsetAllowed,
Sourcepub fn offset(
self,
offset: usize,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>
pub fn offset( self, offset: usize, ) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>
Sets the offset for the query results.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
Sourcepub fn into_cte<Tag: Tag + 'static>(
self,
) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased<Tag>, Self>
pub fn into_cte<Tag: Tag + 'static>( self, ) -> CTEView<'a, <T as SQLTable<'a, SQLiteSchemaType, SQLiteValue<'a>>>::Aliased<Tag>, Self>
Converts this SELECT query into a typed CTE using alias tag name.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ExecutableState,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ExecutableState,
Sourcepub fn union(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn union( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using UNION.
Sourcepub fn union_all(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn union_all( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using UNION ALL.
Sourcepub fn intersect(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn intersect( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using INTERSECT.
Sourcepub fn intersect_all(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn intersect_all( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using INTERSECT ALL.
Sourcepub fn except(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn except( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using EXCEPT.
Sourcepub fn except_all(
self,
other: impl IntoSelect<'a, S, M, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
pub fn except_all( self, other: impl IntoSelect<'a, S, M, R>, ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
Combines this query with another using EXCEPT ALL.