pub struct Select<E: EntityTrait> { /* private fields */ }Expand description
A SELECT on one entity, decoded into its model.
Implementations§
Source§impl<E: EntityTrait> Select<E>
impl<E: EntityTrait> Select<E>
Sourcepub fn filter(self, cond: impl IntoCondition) -> Self
pub fn filter(self, cond: impl IntoCondition) -> Self
Adds a WHERE condition, ANDed with the previous ones.
Sourcepub fn filter_option(self, cond: Option<impl IntoCondition>) -> Self
pub fn filter_option(self, cond: Option<impl IntoCondition>) -> Self
Adds a WHERE condition only when cond is Some.
Restricts the query to the rows of E related to model through
rel, whose from_tbl is the model’s table and to_tbl is E’s.
This is the explicit form of ModelTrait::find_related for a
relation that is not the one Related names: a second relation to
the same entity, or the children side of a self-referencing relation.
Sourcepub fn order_by(self, column: E::Column, order: Order) -> Self
pub fn order_by(self, column: E::Column, order: Order) -> Self
Adds ORDER BY column in the given direction.
Sourcepub fn order_by_asc(self, column: E::Column) -> Self
pub fn order_by_asc(self, column: E::Column) -> Self
Adds ORDER BY column ASC.
Sourcepub fn order_by_desc(self, column: E::Column) -> Self
pub fn order_by_desc(self, column: E::Column) -> Self
Adds ORDER BY column DESC.
Sourcepub fn order_by_expr(self, expr: Expr, order: Order) -> Self
pub fn order_by_expr(self, expr: Expr, order: Order) -> Self
Adds ORDER BY expr in the given direction.
Sourcepub fn group_by_expr(self, expr: Expr) -> Self
pub fn group_by_expr(self, expr: Expr) -> Self
Adds GROUP BY expr.
Sourcepub fn having(self, cond: impl IntoCondition) -> Self
pub fn having(self, cond: impl IntoCondition) -> Self
Adds a HAVING condition, ANDed with the previous ones.
Sourcepub fn select_only(self) -> Self
pub fn select_only(self) -> Self
Clears the select list, to be rebuilt with column,
column_as or expr_as and
decoded with into_model or
into_tuple.
Sourcepub fn column_as(self, column: E::Column, alias: impl IntoIden) -> Self
pub fn column_as(self, column: E::Column, alias: impl IntoIden) -> Self
Adds a column of E to the select list under alias.
Sourcepub fn expr_as(self, expr: Expr, alias: impl IntoIden) -> Self
pub fn expr_as(self, expr: Expr, alias: impl IntoIden) -> Self
Adds an aliased expression to the select list.
Sourcepub fn join(self, kind: JoinType, rel: &RelationDef) -> Self
pub fn join(self, kind: JoinType, rel: &RelationDef) -> Self
Joins the target of rel, a relation declared on E.
The joined table is aliased table_n when it is already part of the
query, as for a self-referencing relation; use
join_as to choose the alias.
Sourcepub fn join_as(
self,
kind: JoinType,
rel: &RelationDef,
alias: &'static str,
) -> Self
pub fn join_as( self, kind: JoinType, rel: &RelationDef, alias: &'static str, ) -> Self
Joins the target of rel under alias, so that filters can qualify
its columns with Expr::col((alias, "column")).
Sourcepub fn join_rev(self, kind: JoinType, rel: &RelationDef) -> Self
pub fn join_rev(self, kind: JoinType, rel: &RelationDef) -> Self
Joins the entity declaring rel, following the relation in reverse.
Sourcepub fn inner_join<R: EntityTrait>(self, _: R) -> Selfwhere
E: Related<R>,
pub fn inner_join<R: EntityTrait>(self, _: R) -> Selfwhere
E: Related<R>,
Adds INNER JOIN to R through the relation E declares to it,
joining the junction table first for a many-to-many relation.
Sourcepub fn left_join<R: EntityTrait>(self, _: R) -> Selfwhere
E: Related<R>,
pub fn left_join<R: EntityTrait>(self, _: R) -> Selfwhere
E: Related<R>,
Adds LEFT JOIN to R through the relation E declares to it,
joining the junction table first for a many-to-many relation.
Also selects the related R row through a LEFT JOIN.
The select list is rebuilt with every column of E aliased
A_<col> and every column of R aliased B_<col>, so that both
models can be decoded from one row even when they share column names.
For a many-to-many relation a row is produced per junction entry.
Selects every E row together with all of its related R rows.
The statement is the one of find_also_related;
the rows are folded so that each E model appears once, followed by
the related models in row order.
Sourcepub fn find_also_linked<L>(self, link: &L) -> SelectTwo<E, L::ToEntity>where
L: Linked<FromEntity = E>,
pub fn find_also_linked<L>(self, link: &L) -> SelectTwo<E, L::ToEntity>where
L: Linked<FromEntity = E>,
Also selects the row at the end of the chain link, through LEFT JOINs.
Every table on the path is joined; only the first and the last are
selected, as A_<col> and B_<col>.
Sourcepub fn find_with_linked<L>(self, link: &L) -> SelectTwoMany<E, L::ToEntity>where
L: Linked<FromEntity = E>,
pub fn find_with_linked<L>(self, link: &L) -> SelectTwoMany<E, L::ToEntity>where
L: Linked<FromEntity = E>,
Selects every E row together with all the rows at the end of link.
Sourcepub fn into_model<M: FromQueryResult>(self) -> Selector<M>
pub fn into_model<M: FromQueryResult>(self) -> Selector<M>
Decodes rows into M instead of the entity model.
Sourcepub fn into_partial_model<P: PartialModelTrait>(self) -> Selector<P>
pub fn into_partial_model<P: PartialModelTrait>(self) -> Selector<P>
Replaces the select list with the columns P reads and decodes rows
into it.
Sourcepub fn into_tuple<T: FromQueryResult>(self) -> Selector<T>
pub fn into_tuple<T: FromQueryResult>(self) -> Selector<T>
Decodes rows into a tuple, by position in the select list.
Meant to follow select_only with as many
items as the tuple has fields.
Sourcepub fn into_json(self) -> Selector<Value>
Available on crate feature with-json only.
pub fn into_json(self) -> Selector<Value>
with-json only.Decodes rows into JSON objects keyed by column name.
Sourcepub fn from_raw_sql(self, statement: Statement) -> RawSelector<E::Model>
pub fn from_raw_sql(self, statement: Statement) -> RawSelector<E::Model>
Runs statement as given and decodes its rows into the entity model.
The builder’s own clauses are discarded; this is the escape hatch for
a SELECT the API cannot express.
Sourcepub fn query_mut(&mut self) -> &mut Select
pub fn query_mut(&mut self) -> &mut Select
Mutable access to the underlying SQL builder, for clauses this API does not expose.
Sourcepub fn into_query(self) -> Select
pub fn into_query(self) -> Select
Takes the underlying SQL builder out of the select.
Sourcepub async fn one<C: ConnectionTrait>(self, db: &C) -> Result<Option<E::Model>>
pub async fn one<C: ConnectionTrait>(self, db: &C) -> Result<Option<E::Model>>
Fetches the first row, adding LIMIT 1.
§Errors
Returns DbErr::Driver when the query fails or the row cannot be decoded.
Sourcepub async fn stream<C: StreamTrait>(
self,
db: &C,
) -> Result<ModelStream<'_, E::Model>>
pub async fn stream<C: StreamTrait>( self, db: &C, ) -> Result<ModelStream<'_, E::Model>>
Streams rows as they are read.
§Errors
Returns DbErr::Driver when the query cannot be started; decoding
failures surface as items of the stream.
Sourcepub async fn count<C: ConnectionTrait>(self, db: &C) -> Result<u64>
pub async fn count<C: ConnectionTrait>(self, db: &C) -> Result<u64>
Counts the rows this query would return.
§Errors
Returns DbErr::RecordNotFound when the count query returns no
row; DbErr::Driver when the query fails.
Sourcepub async fn exists<C: ConnectionTrait>(self, db: &C) -> Result<bool>
pub async fn exists<C: ConnectionTrait>(self, db: &C) -> Result<bool>
Whether this query would return at least one row, through
SELECT EXISTS (query).
§Errors
Returns DbErr::RecordNotFound when the probe returns no row;
DbErr::Driver when the query fails.
Trait Implementations§
Auto Trait Implementations§
impl<E> Freeze for Select<E>where
PhantomData<E>: Freeze,
impl<E> RefUnwindSafe for Select<E>where
PhantomData<E>: RefUnwindSafe,
impl<E> Send for Select<E>where
PhantomData<E>: Send,
impl<E> Sync for Select<E>where
PhantomData<E>: Sync,
impl<E> Unpin for Select<E>where
PhantomData<E>: Unpin,
impl<E> UnsafeUnpin for Select<E>where
PhantomData<E>: UnsafeUnpin,
impl<E> UnwindSafe for Select<E>where
PhantomData<E>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more