Skip to main content

Select

Struct Select 

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

Source

pub fn filter(self, cond: impl IntoCondition) -> Self

Adds a WHERE condition, ANDed with the previous ones.

Source

pub fn filter_option(self, cond: Option<impl IntoCondition>) -> Self

Adds a WHERE condition only when cond is Some.

Source

pub fn related_to<M: ModelTrait>(self, rel: &RelationDef, model: &M) -> Self

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.

Source

pub fn order_by(self, column: E::Column, order: Order) -> Self

Adds ORDER BY column in the given direction.

Source

pub fn order_by_asc(self, column: E::Column) -> Self

Adds ORDER BY column ASC.

Source

pub fn order_by_desc(self, column: E::Column) -> Self

Adds ORDER BY column DESC.

Source

pub fn order_by_expr(self, expr: Expr, order: Order) -> Self

Adds ORDER BY expr in the given direction.

Source

pub fn limit(self, limit: u64) -> Self

Sets LIMIT.

Source

pub fn offset(self, offset: u64) -> Self

Sets OFFSET.

Source

pub fn distinct(self) -> Self

Makes the query SELECT DISTINCT.

Source

pub fn group_by(self, column: E::Column) -> Self

Adds GROUP BY column.

Source

pub fn group_by_expr(self, expr: Expr) -> Self

Adds GROUP BY expr.

Source

pub fn having(self, cond: impl IntoCondition) -> Self

Adds a HAVING condition, ANDed with the previous ones.

Source

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.

Source

pub fn column(self, column: E::Column) -> Self

Adds a column of E to the select list.

Source

pub fn column_as(self, column: E::Column, alias: impl IntoIden) -> Self

Adds a column of E to the select list under alias.

Source

pub fn expr(self, expr: Expr) -> Self

Adds an expression to the select list.

Source

pub fn expr_as(self, expr: Expr, alias: impl IntoIden) -> Self

Adds an aliased expression to the select list.

Source

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.

Source

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

Source

pub fn join_rev(self, kind: JoinType, rel: &RelationDef) -> Self

Joins the entity declaring rel, following the relation in reverse.

Source

pub fn inner_join<R: EntityTrait>(self, _: R) -> Self
where 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.

Source

pub fn left_join<R: EntityTrait>(self, _: R) -> Self
where 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.

Source

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

Source

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.

Source

pub fn into_model<M: FromQueryResult>(self) -> Selector<M>

Decodes rows into M instead of the entity model.

Source

pub fn into_partial_model<P: PartialModelTrait>(self) -> Selector<P>

Replaces the select list with the columns P reads and decodes rows into it.

Source

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.

Source

pub fn into_json(self) -> Selector<Value>

Available on crate feature with-json only.

Decodes rows into JSON objects keyed by column name.

Source

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.

Source

pub fn as_query(&self) -> &Select

The underlying SQL builder.

Source

pub fn query_mut(&mut self) -> &mut Select

Mutable access to the underlying SQL builder, for clauses this API does not expose.

Source

pub fn into_query(self) -> Select

Takes the underlying SQL builder out of the select.

Source

pub fn build(&self) -> Statement

Renders the statement.

Source

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.

Source

pub async fn all<C: ConnectionTrait>(self, db: &C) -> Result<Vec<E::Model>>

Fetches every row.

§Errors

Returns DbErr::Driver when the query fails or a row cannot be decoded.

Source

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.

Source

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.

Source

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.

Source

pub fn paginate<C: ConnectionTrait>( self, db: &C, page_size: u64, ) -> Paginator<'_, C, E::Model>

Paginates with page_size rows per page.

Source

pub fn cursor_by<const N: usize>( self, columns: [E::Column; N], ) -> Cursor<E, E::Model>

Paginates by key over columns, most significant first; see Cursor.

Any ordering already on the query is replaced by the key order.

Trait Implementations§

Source§

impl<E: Clone + EntityTrait> Clone for Select<E>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug + EntityTrait> Debug for Select<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Select<E>
where PhantomData<E>: Freeze,

§

impl<E> RefUnwindSafe for Select<E>

§

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>

§

impl<E> UnwindSafe for Select<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more