pub type SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> = QueryBuilder<'a, Schema, State, Table, Marker, Row, Grouped>;Expand description
A SELECT query being built for SQLite.
This is QueryBuilder in one of the Select*
states. Start it with QueryBuilder::select
or select_distinct, then call
from.
§Clause order
Clauses must be added in SQL order. Each method is only available in the states listed here:
| After | You can call |
|---|---|
select | from |
from | joins, where, group_by, order_by, limit, offset, set operations |
| a join | more joins, where, group_by, order_by, limit, set operations |
where | group_by, order_by, limit, set operations |
group_by | having, order_by, limit, set operations |
having | having, order_by, limit, set operations |
order_by | limit, set operations |
limit | offset, set operations |
offset | set operations |
| a set operation | more set operations, order_by, limit, offset |
offset without limit is only offered right after from or a set
operation; elsewhere, add a limit first. Every state after from can
be executed, used as a subquery, or named as a derived table with
alias. Every state except a compound query can become a
CTE with into_cte.
§Examples
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" > ?"#
);Joins:
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""#
);Ordering and pagination:
let query = builder
.select(user.name)
.from(user)
.order_by(asc(user.name))
.limit(10)
.offset(20);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" ORDER BY "users"."name" ASC LIMIT 10 OFFSET 20"#
);§Compile-time checks
A clause added out of order does not compile:
// WHERE cannot follow LIMIT.
let query = builder.select(user.name).from(user).limit(10).r#where(gt(user.id, 1));having needs a group_by first, and a WHERE or HAVING condition must
be a boolean expression. Column references are scope-checked: a query
that names a table missing from its FROM and JOIN clauses is rejected when
it is executed (.all(), .get(), …), used as a derived table, or used
as an INSERT source.
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>>The SQL built so far.
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, FromMarker<M, T>, <M as ResolveRow<T>>::Row>
pub fn from<T>( self, query: T, ) -> SelectBuilder<'a, S, SelectFromSet, T, FromMarker<M, T>, <M as ResolveRow<T>>::Row>
Sets the FROM source: a table, a CTE, or a derived table made with
alias.
The result row type is inferred from the selected columns and this
source. With select(()), the row is the table’s generated select
model.
§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: ClauseAllowed<Join>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Join>,
Sourcepub fn join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Row, G>
pub fn join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Row, G>
Adds a JOIN (an inner join).
Pass (table, condition) for an explicit ON condition, or a bare table
to join on its foreign key to the previous table. A derived table
(see alias) also works in the tuple form.
The other join methods (left_join, right_join, full_join,
inner_join, the _outer and natural_ variants, and
cross_join) take the same arguments. After a
LEFT, RIGHT or FULL join, selected columns of the side that may be
missing decode as Option; with select(()), that side’s whole
model is an Option in the row.
The condition may only read tables already in the query; this is checked when the query is executed.
§Examples
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""#
);Sourcepub fn natural_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin>>::Row, G>
pub fn natural_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin>>::Row, G>
Adds a natural join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn natural_left_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Row, G>
pub fn natural_left_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Row, G>
Adds a natural_left join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn left_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Row, G>
pub fn left_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Row, G>
Adds a left join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn left_outer_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Row, G>
pub fn left_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>>::Row, G>
Adds a left_outer join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn natural_left_outer_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Row, G>
pub fn natural_left_outer_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, LeftJoin>>::Row, G>
Adds a natural_left_outer join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn natural_right_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Row, G>
pub fn natural_right_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Row, G>
Adds a natural_right join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn right_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Row, G>
pub fn right_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Row, G>
Adds a right join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn right_outer_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Row, G>
pub fn right_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>>::Row, G>
Adds a right_outer join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn natural_right_outer_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Row, G>
pub fn natural_right_outer_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, RightJoin>>::Row, G>
Adds a natural_right_outer join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn natural_full_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Row, G>
pub fn natural_full_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Row, G>
Adds a natural_full join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn full_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Row, G>
pub fn full_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Row, G>
Adds a full join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn full_outer_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Row, G>
pub fn full_outer_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>>::Row, G>
Adds a full_outer join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn natural_full_outer_join<J: JoinSource<'a>>(
self,
source: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Row, G>
pub fn natural_full_outer_join<J: JoinSource<'a>>( self, source: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Marker, <M as JoinStep<R, J::JoinedTable, FullJoin>>::Row, G>
Adds a natural_full_outer join (NATURAL).
A natural join matches the columns both sides share by name, so it takes a table or derived table and no ON condition.
Sourcepub fn inner_join<J: JoinArg<'a, T>>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Row, G>
pub fn inner_join<J: JoinArg<'a, T>>( self, arg: J, ) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>>::Row, G>
Adds a inner join.
Pass (table, condition) for an explicit ON condition, or a
bare table to join on its foreign key to the previous table.
See join for an example.
Sourcepub fn cross_join<Arg: CrossJoinArg<'a, T>>(
self,
arg: Arg,
) -> SelectBuilder<'a, S, SelectJoinSet, Arg::JoinedTable, <M as JoinStep<R, Arg::JoinedTable, InnerJoin, Arg::OnSources>>::Marker, <M as JoinStep<R, Arg::JoinedTable, InnerJoin, Arg::OnSources>>::Row, G>
pub fn cross_join<Arg: CrossJoinArg<'a, T>>( self, arg: Arg, ) -> SelectBuilder<'a, S, SelectJoinSet, Arg::JoinedTable, <M as JoinStep<R, Arg::JoinedTable, InnerJoin, Arg::OnSources>>::Marker, <M as JoinStep<R, Arg::JoinedTable, InnerJoin, Arg::OnSources>>::Row, G>
Adds a CROSS JOIN, which pairs every row with every row of arg.
A bare table renders CROSS JOIN. For backwards compatibility,
(table, condition) renders the equivalent INNER JOIN ... ON ....
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: SelectClause<Where>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: SelectClause<Where>,
Sourcepub fn where<E>(
self,
condition: E,
) -> SelectBuilder<'a, S, SelectWhereSet, T, <M as HasScope>::With<E::Sources>, R, G>
pub fn where<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectWhereSet, T, <M as HasScope>::With<E::Sources>, R, G>
Adds a WHERE clause.
The condition must be a boolean expression. Combine conditions with
and and or from drizzle_core::expr.
§Examples
// 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")));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" WHERE ("users"."id" > ? AND "users"."name" = ?)"#
);Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<GroupBy>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<GroupBy>,
Sourcepub fn group_by<Gr>(
self,
columns: Gr,
) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<Gr::Sources>, R, Gr::Columns>
pub fn group_by<Gr>( self, columns: Gr, ) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<Gr::Sources>, R, Gr::Columns>
Adds a GROUP BY clause. Pass one expression or a tuple.
Every selected column that is not inside an aggregate must appear in
the GROUP BY list; this is checked when the query is executed. One
exception: grouping by a table’s single-column primary key determines
the whole row, so any column of that table may be selected. Prefer
.group_by(table.pk) over listing every selected column; it also
lets SQLite read groups in key order instead of sorting them in a
temporary B-tree.
§Examples
let query = builder
.select((post.user_id, count(post.id)))
.from(post)
.group_by(post.user_id)
.having(gt(count(post.id), 5));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "posts"."user_id", COUNT ("posts"."id") FROM "posts" GROUP BY "posts"."user_id" HAVING COUNT ("posts"."id")> ?"#
);Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Having>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Having>,
Sourcepub fn having<E>(
self,
condition: E,
) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<E::Sources>, R, G>
pub fn having<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<E::Sources>, R, G>
Adds a HAVING clause, which filters groups.
Only available after group_by. The condition must
be a boolean expression and may use aggregates. See group_by for an
example.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: SelectClause<OrderBy>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: SelectClause<OrderBy>,
Sourcepub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T, <M as HasScope>::With<TOrderBy::Sources>, R, G>
pub fn order_by<TOrderBy>( self, expressions: TOrderBy, ) -> SelectBuilder<'a, S, SelectOrderSet, T, <M as HasScope>::With<TOrderBy::Sources>, R, G>
Adds an ORDER BY clause.
Pass one ordering term or a tuple. Wrap a column in asc or desc
to set the direction.
§Examples
let query = builder
.select(user.name)
.from(user)
.order_by((desc(user.name), asc(user.id)));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" ORDER BY "users"."name" DESC, "users"."id" ASC"#
);Source§impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G>
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 a compound (UNION / INTERSECT / EXCEPT) result by its
output columns.
Column references are written without their table name, because the
combined rows no longer belong to one table (turso rejects the
qualified form). See union for an example.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Limit>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Limit>,
Sourcepub fn limit<P>(
self,
limit: P,
) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
pub fn limit<P>(
self,
limit: P,
) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
Adds a LIMIT clause.
Pass a non-negative integer, which is written into the SQL, or an
integer placeholder, which is bound when the query runs. See
SelectBuilder for an example.
§Panics
Panics when a signed numeric argument is negative or a numeric value
does not fit in usize.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<StandaloneOffset>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<StandaloneOffset>,
Sourcepub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
pub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
Skips the first offset rows without limiting the row count.
SQLite only accepts OFFSET after a LIMIT, so this renders
LIMIT -1 OFFSET n; a negative limit means no limit. Only available
right after from or a set operation; elsewhere call
limit first.
§Examples
let query = builder.select(user.name).from(user).offset(5);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."name" FROM "users" LIMIT -1 OFFSET 5"#);§Panics
Panics when a signed numeric argument is negative or a numeric value
does not fit in usize.
Source§impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>
impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>
Sourcepub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
pub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, SQLiteValue<'a>>,
Adds an OFFSET clause after LIMIT.
§Panics
Panics when a signed numeric argument is negative or a numeric value
does not fit in usize.
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 alias<Name, AggProof>(
self,
_name: Name,
) -> Derived<'a, SQLiteValue<'a>, Name, <M as DerivedSelection<'a, SQLiteValue<'a>, SQLiteSchemaType, T>>::Projection, Self>where
Name: Tag,
<M as DerivedSelection<'a, SQLiteValue<'a>, SQLiteSchemaType, T>>::Projection: DerivedProjection<Name>,
M: MarkerAggValidFor<G, AggProof>,
pub fn alias<Name, AggProof>(
self,
_name: Name,
) -> Derived<'a, SQLiteValue<'a>, Name, <M as DerivedSelection<'a, SQLiteValue<'a>, SQLiteSchemaType, T>>::Projection, Self>where
Name: Tag,
<M as DerivedSelection<'a, SQLiteValue<'a>, SQLiteSchemaType, T>>::Projection: DerivedProjection<Name>,
M: MarkerAggValidFor<G, AggProof>,
Names this query so it can be used as a derived table in from or a
join.
name is a value of a Tag type; its NAME
becomes the SQL alias. The result exposes the selected columns, so
the outer query can reference them with typed accessors.
§Panics
Panics when the projection contains duplicate output names. Name a
computed expression with drizzle_core::expr::AliasExt::named to
make each output unique.
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>
Turns this SELECT into a common table expression named Tag::NAME.
The result derefs to an aliased copy of the FROM table, so you can
select its columns with the usual field access. Pass it to
QueryBuilder::with and then use it in
from. Not available on a compound query (after a set operation).
See QueryBuilder::with for an example.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Compound>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Compound>,
Sourcepub fn union<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
pub fn union<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
Combines this query with other using UNION, which drops duplicate
rows.
Both queries must select the same row type. After a set operation you
can chain more set operations, then order_by, limit and offset
for the combined result.
§Examples
let query = builder
.select(user.name)
.from(user)
.r#where(eq(user.id, 1))
.union(builder.select(user.name).from(user).r#where(gt(user.id, 100)))
.order_by(asc(user.name));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" WHERE "users"."id" = ? UNION SELECT "users"."name" FROM "users" WHERE "users"."id" > ? ORDER BY "name" ASC"#
);Sourcepub fn union_all<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
pub fn union_all<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
Combines this query with other using UNION ALL, which keeps
duplicate rows. See union.
Sourcepub fn intersect<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
pub fn intersect<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
Keeps only rows that other also returns (INTERSECT). See
union.
Sourcepub fn except<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
pub fn except<M2>(
self,
other: impl IntoSelect<'a, S, M2, R>,
) -> SelectBuilder<'a, S, SelectSetOpSet, T, <M as SetOperand<M2>>::Combined, R, G>where
M: SetOperand<M2>,
Keeps only rows that other does not return (EXCEPT). See
union.
Trait Implementations§
Source§impl<'a, S, State, T, M, R, G> Expr<'a, SQLiteValue<'a>> for SelectBuilder<'a, S, State, T, M, R, G>
impl<'a, S, State, T, M, R, G> Expr<'a, SQLiteValue<'a>> for SelectBuilder<'a, S, State, T, M, R, G>
Source§type SQLType = <M as SubqueryType<'a, SQLiteValue<'a>>>::SQLType
type SQLType = <M as SubqueryType<'a, SQLiteValue<'a>>>::SQLType
Source§fn to_expr_sql(&self) -> SQL<'a, V>
fn to_expr_sql(&self) -> SQL<'a, V>
Source§fn into_expr_sql(self) -> SQL<'a, V>where
Self: Sized,
fn into_expr_sql(self) -> SQL<'a, V>where
Self: Sized,
Source§fn to_condition_sql(&self) -> Option<SQL<'a, V>>
fn to_condition_sql(&self) -> Option<SQL<'a, V>>
ConditionList. Read moreSource§fn into_condition_sql(self) -> Option<SQL<'a, V>>where
Self: Sized,
fn into_condition_sql(self) -> Option<SQL<'a, V>>where
Self: Sized,
Expr::to_condition_sql.Source§impl<S, State, T, M, R, G> ExprSources for SelectBuilder<'_, S, State, T, M, R, G>where
M: SelectSources,
impl<S, State, T, M, R, G> ExprSources for SelectBuilder<'_, S, State, T, M, R, G>where
M: SelectSources,
Source§impl<'a, S, State: ClauseAllowed<Compound>, T, M, R, G> IntoSelect<'a, S, M, R> for SelectBuilder<'a, S, State, T, M, R, G>
impl<'a, S, State: ClauseAllowed<Compound>, T, M, R, G> IntoSelect<'a, S, M, R> for SelectBuilder<'a, S, State, T, M, R, G>
Source§fn into_select(self) -> SelectBuilder<'a, S, State, T, M, R>
fn into_select(self) -> SelectBuilder<'a, S, State, T, M, R>
SelectBuilder.