pub type SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> = QueryBuilder<'a, Schema, State, Table, Marker, Row, Grouped>;Expand description
A PostgreSQL SELECT being built: a QueryBuilder
in one of the Select* states.
State limits which clause can come next. Table is the source added
last (a bare-table .join(...) derives its ON condition from it).
Marker records the selection, the FROM/JOIN scope and the sources each
clause reads; Row is the decoded row type; Grouped lists the
GROUP BY columns. With these the compiler rejects columns that are not
in scope or not grouped, at the method that runs the query.
§Examples
use drizzle::core::desc;
use drizzle::core::expr::eq;
let query = db
.select((user.id, user.name))
.from(user)
.r#where(eq(user.name, "Alice"))
.order_by(desc(user.id))
.limit(10)
.offset(20);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id", "users"."name" FROM "users" WHERE "users"."name" = $1 ORDER BY "users"."id" DESC LIMIT $2 OFFSET $3"#
);Aliased Type§
pub struct SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> {
pub sql: SQL<'a, PostgresValue<'a>>,
/* private fields */
}Fields§
§sql: SQL<'a, PostgresValue<'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 view, an aliased table, a derived
table (.alias(...)), or a CTE.
§Examples
let query = db.select(()).from(user);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id", "users"."name", "users"."email" 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 an inner JOIN ... ON ....
Pass (source, condition), or a bare table to join on its foreign key
to the previous table. The tables the condition reads are checked
against the query’s scope at compile time, where the query is run
(.all(), .get(), .rows()).
Other join kinds have their own methods:
left_join, right_join, full_join, inner_join, their _outer
forms, natural_* joins, *_join_using, and the *_lateral joins.
§Examples
use drizzle::core::expr::eq;
let query = db
.select((user.name, post.title))
.from(user)
.join((post, eq(post.author_id, user.id)));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name", "posts"."title" FROM "users" JOIN "posts" ON "posts"."author_id" = "users"."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 of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
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 join of the kind named by the method.
The database joins on every column name both sides share, so
this takes only a table or other source, with 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 join of the kind named by the method, with an ON condition.
Pass (source, condition), or a bare table to join on its
foreign key to the previous table. An outer join makes the
columns of its nullable side (the joined source for LEFT,
the earlier sources for RIGHT, both for FULL) decode as
Option.
Sourcepub fn left_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, LeftJoin>>::Marker, <M as JoinStep<R, U, LeftJoin>>::Row, G>
pub fn left_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, LeftJoin>>::Marker, <M as JoinStep<R, U, LeftJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn left_outer_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, LeftJoin>>::Marker, <M as JoinStep<R, U, LeftJoin>>::Row, G>
pub fn left_outer_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, LeftJoin>>::Marker, <M as JoinStep<R, U, LeftJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn right_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, RightJoin>>::Marker, <M as JoinStep<R, U, RightJoin>>::Row, G>
pub fn right_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, RightJoin>>::Marker, <M as JoinStep<R, U, RightJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn right_outer_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, RightJoin>>::Marker, <M as JoinStep<R, U, RightJoin>>::Row, G>
pub fn right_outer_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, RightJoin>>::Marker, <M as JoinStep<R, U, RightJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn full_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, FullJoin>>::Marker, <M as JoinStep<R, U, FullJoin>>::Row, G>
pub fn full_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, FullJoin>>::Marker, <M as JoinStep<R, U, FullJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn full_outer_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, FullJoin>>::Marker, <M as JoinStep<R, U, FullJoin>>::Row, G>
pub fn full_outer_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, FullJoin>>::Marker, <M as JoinStep<R, U, FullJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn inner_join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, InnerJoin>>::Marker, <M as JoinStep<R, U, InnerJoin>>::Row, G>
pub fn inner_join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, InnerJoin>>::Marker, <M as JoinStep<R, U, InnerJoin>>::Row, G>
Adds a join of the kind named by the method, with
USING (columns): joins on equal values of same-named columns.
Pass bare column names, such as SQL::ident("id"); see
join_using.
Sourcepub fn join_using<U: PostgresTable<'a>>(
self,
table: U,
columns: impl ToSQL<'a, PostgresValue<'a>>,
) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, InnerJoin>>::Marker, <M as JoinStep<R, U, InnerJoin>>::Row, G>
pub fn join_using<U: PostgresTable<'a>>( self, table: U, columns: impl ToSQL<'a, PostgresValue<'a>>, ) -> SelectBuilder<'a, S, SelectJoinSet, U, <M as JoinStep<R, U, InnerJoin>>::Marker, <M as JoinStep<R, U, InnerJoin>>::Row, G>
Adds JOIN table USING (columns), joining on equal values of
same-named columns.
columns is rendered as given, and PostgreSQL requires bare
names here: pass SQL::ident("id"), not a table column, which
renders qualified.
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: every row of the left side paired with every row
of source.
For backwards compatibility, (source, condition) is also accepted and
renders the equivalent INNER JOIN ... ON ....
§Examples
let query = db.select((user.name, post.title)).from(user).cross_join(post);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name", "posts"."title" FROM "users" CROSS JOIN "posts""#
);Sourcepub fn inner_join_lateral<J>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>>::Row, G>where
J: LateralArg<'a, PostgresValue<'a>>,
M: JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>,
pub fn inner_join_lateral<J>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>>::Row, G>where
J: LateralArg<'a, PostgresValue<'a>>,
M: JoinStep<R, J::JoinedTable, Lateral<InnerJoin>, J::OnSources>,
Adds INNER JOIN LATERAL (subquery) AS alias ON condition.
A lateral subquery can refer to columns of the sources joined before
it. Pass (derived_table, condition), where the derived table comes
from .alias(tag) on a SELECT.
Sourcepub fn left_join_lateral<J, SelectionProof>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources>>::Row, G>where
J: LateralArg<'a, PostgresValue<'a>>,
M: JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources> + LeftLateralSelection<SelectionProof>,
pub fn left_join_lateral<J, SelectionProof>(
self,
arg: J,
) -> SelectBuilder<'a, S, SelectJoinSet, J::JoinedTable, <M as JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources>>::Marker, <M as JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources>>::Row, G>where
J: LateralArg<'a, PostgresValue<'a>>,
M: JoinStep<R, J::JoinedTable, Lateral<LeftJoin>, J::OnSources> + LeftLateralSelection<SelectionProof>,
Adds LEFT JOIN LATERAL (subquery) AS alias ON condition.
Like inner_join_lateral, but keeps
left rows with no match. With select(()) the lateral source decodes
as an Option of its row; an explicit column list may only read
sources joined before the lateral join.
Sourcepub fn cross_join_lateral<Source>(
self,
source: Source,
) -> SelectBuilder<'a, S, SelectJoinSet, Source::JoinedTable, <M as JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>>::Marker, <M as JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>>::Row, G>where
Source: LateralSource<'a, PostgresValue<'a>>,
M: JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>,
pub fn cross_join_lateral<Source>(
self,
source: Source,
) -> SelectBuilder<'a, S, SelectJoinSet, Source::JoinedTable, <M as JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>>::Marker, <M as JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>>::Row, G>where
Source: LateralSource<'a, PostgresValue<'a>>,
M: JoinStep<R, Source::JoinedTable, Lateral<InnerJoin>>,
Adds CROSS JOIN LATERAL (subquery) AS alias, with no ON condition.
The subquery runs once per left row and can refer to its columns.
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 condition.
The condition must be boolean, such as eq(...), and(...) or a
boolean column. Every column it uses must come from a source in
FROM or a join. The compiler checks this where the query is run
(.all(), .get(), .rows()): a column of another table fails there
with “X is not in this query’s FROM/JOIN scope”.
§Examples
use drizzle::core::expr::{and, eq, gt};
let query = db
.select(user.id)
.from(user)
.r#where(and(eq(user.name, "Alice"), gt(user.id, 10)));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id" FROM "users" WHERE ("users"."name" = $1 AND "users"."id" > $2)"#
);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 list: one expression or a tuple of them.
Non-aggregate columns in the SELECT list must appear in the
GROUP BY list, with one exception: grouping by a table’s
single-column primary key determines the whole row, so any column of
that table may be selected without being listed (PostgreSQL allows
this too).
§Examples
use drizzle::core::expr::{count, gt};
let query = db
.select((post.author_id, count(post.id)))
.from(post)
.group_by(post.author_id)
.having(gt(count(post.id), 5));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "posts"."author_id", COUNT ("posts"."id") FROM "posts" GROUP BY "posts"."author_id" HAVING COUNT ("posts"."id")> $1"#
);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 condition, which filters groups after GROUP BY.
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 ORDER BY.
Pass a column (ascending by default), asc(col) / desc(col), or a
tuple of them. An array also works when every term reads the same
table; terms over different tables go in a tuple.
§Examples
use drizzle::core::{asc, desc};
let query = db
.select(user.name)
.from(user)
.order_by([asc(user.name), desc(user.id)]);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" ORDER BY "users"."name" ASC, "users"."id" DESC"#
);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, PostgresValue<'a>>,
pub fn order_by<TOrderBy>(
self,
expressions: TOrderBy,
) -> SelectBuilder<'a, S, SelectOrderSet, T, M, R, G>where
TOrderBy: ToSQL<'a, PostgresValue<'a>>,
Adds ORDER BY to a compound (UNION / INTERSECT / EXCEPT) query.
The terms sort the combined output, so column references render
unqualified ("name", not "users"."name"), as PostgreSQL requires.
§Examples
let query = db
.select(user.name)
.from(user)
.union_all(db.select(post.title).from(post))
.order_by(user.name);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" UNION ALL SELECT "posts"."title" FROM "posts" ORDER BY "name""#
);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, PostgresValue<'a>>,
pub fn limit<P>(
self,
limit: P,
) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>where
P: PaginationArg<'a, PostgresValue<'a>>,
Adds LIMIT n, returning at most n rows.
n is a non-negative integer (bound as a parameter) or a placeholder.
§Examples
let query = db.select(user.id).from(user).limit(5);
assert_eq!(query.to_sql().sql(), r#"SELECT "users"."id" FROM "users" LIMIT $1"#);§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<Offset>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Offset>,
Sourcepub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, PostgresValue<'a>>,
pub fn offset<P>(
self,
offset: P,
) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>where
P: PaginationArg<'a, PostgresValue<'a>>,
Adds OFFSET n, skipping the first n rows.
n is a non-negative integer (bound as a parameter) or a placeholder.
§Examples
let query = db.select(user.id).from(user).limit(10).offset(20);
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id" FROM "users" LIMIT $1 OFFSET $2"#
);§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<Source>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Source>,
Sourcepub fn alias<Tag, AggProof>(
self,
_tag: Tag,
) -> Derived<'a, PostgresValue<'a>, Tag, <M as DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T>>::Projection, Self>where
Tag: Tag,
M: DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T> + MarkerAggValidFor<G, AggProof>,
<M as DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T>>::Projection: DerivedProjection<Tag>,
pub fn alias<Tag, AggProof>(
self,
_tag: Tag,
) -> Derived<'a, PostgresValue<'a>, Tag, <M as DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T>>::Projection, Self>where
Tag: Tag,
M: DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T> + MarkerAggValidFor<G, AggProof>,
<M as DerivedSelection<'a, PostgresValue<'a>, PostgresSchemaType, T>>::Projection: DerivedProjection<Tag>,
Turns this SELECT into a derived table, (SELECT ...) AS tag.
The result can be used in .from(...) or a lateral join; read its
columns with .fields().
§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, PostgresSchemaType, PostgresValue<'a>>>::Aliased<Tag>, Self>
pub fn into_cte<Tag: Tag + 'static>( self, ) -> CTEView<'a, <T as SQLTable<'a, PostgresSchemaType, PostgresValue<'a>>>::Aliased<Tag>, Self>
Turns this SELECT into a common table expression named Tag::NAME.
The result has the same columns as the FROM table. Pass it to
QueryBuilder::with, and read its columns through its .table field;
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 decode to the same row type, which the compiler
checks. The union_all, intersect, intersect_all, except and
except_all methods work the same way.
§Examples
let query = db
.select(user.name)
.from(user)
.union(db.select(post.title).from(post));
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."name" FROM "users" UNION SELECT "posts"."title" FROM "posts""#
);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 duplicates.
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 the rows present in both queries (INTERSECT), without duplicates.
Sourcepub fn intersect_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 intersect_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>,
Keeps the rows present in both queries (INTERSECT ALL), with duplicates.
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 the rows of this query that are not in other (EXCEPT), without duplicates.
Sourcepub fn except_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 except_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>,
Keeps the rows of this query that are not in other (EXCEPT ALL), with duplicates.
Source§impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Simple>,
impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>where
State: ClauseAllowed<Simple>,
Sourcepub fn for_update(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
pub fn for_update(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
Adds FOR UPDATE, locking the selected rows against updates and
deletes by other transactions until this one ends.
Follow with nowait or skip_locked
to change what happens when a row is already locked.
§Examples
let query = db.select(user.id).from(user).for_update().skip_locked();
assert_eq!(
query.to_sql().sql(),
r#"SELECT "users"."id" FROM "users" FOR UPDATE SKIP LOCKED"#
);Adds FOR SHARE: other transactions can still read and share-lock the
rows, but cannot update or delete them.
Sourcepub fn for_no_key_update(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
pub fn for_no_key_update(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
Adds FOR NO KEY UPDATE: like FOR UPDATE, but does not block
FOR KEY SHARE locks (for example, foreign-key checks).
Adds FOR KEY SHARE: blocks deletes and key changes, but allows other
updates.
Sourcepub fn for_update_of<U: PostgresTable<'a>>(
self,
table: U,
) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
pub fn for_update_of<U: PostgresTable<'a>>( self, table: U, ) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>
Adds FOR UPDATE OF table, locking rows of that table only.
§Examples
use drizzle::core::expr::eq;
let query = db
.select((user.id, post.id))
.from(user)
.join((post, eq(post.author_id, user.id)))
.for_update_of(user);
assert!(query.to_sql().sql().ends_with(r#"FOR UPDATE OF "users""#));Adds FOR SHARE OF table, locking rows of that table only.
Source§impl<S, T, M, R, G> SelectBuilder<'_, S, SelectForSet, T, M, R, G>
impl<S, T, M, R, G> SelectBuilder<'_, S, SelectForSet, T, M, R, G>
Sourcepub fn nowait(self) -> Self
pub fn nowait(self) -> Self
Adds NOWAIT: the query fails at once instead of waiting when a row
is already locked.
Sourcepub fn skip_locked(self) -> Self
pub fn skip_locked(self) -> Self
Adds SKIP LOCKED: rows already locked by another transaction are
left out of the result instead of waited for.
Trait Implementations§
Source§impl<'a, S, State, T, M, R, G> Expr<'a, PostgresValue<'a>> for SelectBuilder<'a, S, State, T, M, R, G>
impl<'a, S, State, T, M, R, G> Expr<'a, PostgresValue<'a>> for SelectBuilder<'a, S, State, T, M, R, G>
Source§type SQLType = <M as SubqueryType<'a, PostgresValue<'a>>>::SQLType
type SQLType = <M as SubqueryType<'a, PostgresValue<'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.