Skip to main content

SelectBuilder

Type Alias SelectBuilder 

Source
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>

Source

pub fn from<T>( self, query: T, ) -> SelectBuilder<'a, S, SelectFromSet, T, FromMarker<M, T>, <M as ResolveRow<T>>::Row>
where T: ToSQL<'a, PostgresValue<'a>> + ScopeEntry, M: ResolveRow<T>,

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>,

Source

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>
where M: JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>,

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""#
);
Source

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>
where M: JoinStep<R, J::JoinedTable, InnerJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, LeftJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, LeftJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, LeftJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, RightJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, RightJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, RightJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, FullJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, FullJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, FullJoin>,

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.

Source

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>
where M: JoinStep<R, J::JoinedTable, InnerJoin, J::OnSources>,

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.

Source

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>
where M: JoinStep<R, U, LeftJoin>,

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.

Source

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>
where M: JoinStep<R, U, LeftJoin>,

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.

Source

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>
where M: JoinStep<R, U, RightJoin>,

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.

Source

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>
where M: JoinStep<R, U, RightJoin>,

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.

Source

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>
where M: JoinStep<R, U, FullJoin>,

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.

Source

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>
where M: JoinStep<R, U, FullJoin>,

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.

Source

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>
where M: JoinStep<R, U, InnerJoin>,

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.

Source

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>
where M: JoinStep<R, U, InnerJoin>,

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.

Source

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>
where M: JoinStep<R, Arg::JoinedTable, InnerJoin, Arg::OnSources>,

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""#
);
Source

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.

Source

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.

Source

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>,

Source

pub fn where<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectWhereSet, T, <M as HasScope>::With<E::Sources>, R, G>
where M: HasScope, E: Expr<'a, PostgresValue<'a>>, E::SQLType: BooleanLike,

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>,

Source

pub fn group_by<Gr>( self, columns: Gr, ) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<Gr::Sources>, R, Gr::Columns>
where M: HasScope, Gr: IntoGroupBy<'a, PostgresValue<'a>>,

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>,

Source

pub fn having<E>( self, condition: E, ) -> SelectBuilder<'a, S, SelectGroupSet, T, <M as HasScope>::With<E::Sources>, R, G>
where M: HasScope, E: Expr<'a, PostgresValue<'a>>, E::SQLType: BooleanLike,

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>,

Source

pub fn order_by<TOrderBy>( self, expressions: TOrderBy, ) -> SelectBuilder<'a, S, SelectOrderSet, T, <M as HasScope>::With<TOrderBy::Sources>, R, G>
where M: HasScope, TOrderBy: ToSQL<'a, PostgresValue<'a>> + ExprSources,

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>

Source

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>,

Source

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>,

Source

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>,

Source

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>

Source

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>,

Source

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""#
);
Source

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.

Source

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.

Source

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.

Source

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.

Source

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>,

Source

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"#
);
Source

pub fn for_share(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>

Adds FOR SHARE: other transactions can still read and share-lock the rows, but cannot update or delete them.

Source

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).

Source

pub fn for_key_share(self) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>

Adds FOR KEY SHARE: blocks deletes and key changes, but allows other updates.

Source

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""#));
Source

pub fn for_share_of<U: PostgresTable<'a>>( self, table: U, ) -> SelectBuilder<'a, S, SelectForSet, T, M, R, G>

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>

Source

pub fn nowait(self) -> Self

Adds NOWAIT: the query fails at once instead of waiting when a row is already locked.

Source

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>

Source§

type SQLType = <M as SubqueryType<'a, PostgresValue<'a>>>::SQLType

The SQL data type this expression evaluates to.
Source§

type Nullable = Null

Whether this expression can be NULL.
Source§

type Aggregate = Scalar

Whether this is an aggregate (COUNT, SUM) or scalar expression.
Source§

fn to_expr_sql(&self) -> SQL<'a, V>

Renders this value as a scalar expression, borrowing it. Read more
Source§

fn into_expr_sql(self) -> SQL<'a, V>
where Self: Sized,

Renders this value as a scalar expression, consuming it.
Source§

fn to_condition_sql(&self) -> Option<SQL<'a, V>>

Renders this value as one element of a ConditionList. Read more
Source§

fn into_condition_sql(self) -> Option<SQL<'a, V>>
where Self: Sized,

Consuming counterpart of Expr::to_condition_sql.
Source§

impl<S, State, T, M, R, G> ExprSources for SelectBuilder<'_, S, State, T, M, R, G>
where M: SelectSources,

Source§

type Sources = <M as SelectSources>::Sources

Type-level tree with one Src leaf per column read.
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>

Source§

type State = State

The builder state of the converted query.
Source§

type Table = T

The FROM table of the converted query.
Source§

fn into_select(self) -> SelectBuilder<'a, S, State, T, M, R>

Returns the query as a SelectBuilder.
Source§

impl<S, State: ClauseAllowed<Source>, T, M, R, G> SelectQuery for SelectBuilder<'_, S, State, T, M, R, G>