Skip to main content

Query

Struct Query 

Source
pub struct Query<E> { /* private fields */ }
Expand description

A builder for reads (and WHERE-scoped deletes) over E. Conditions are AND-combined; use filter to add an OR group.

Implementations§

Source§

impl<E> Query<E>
where E: Entity,

Source

pub fn new() -> Query<E>

An unfiltered query over E’s table. Prefer repo::query for brevity.

Source

pub fn where_eq(self, column: &str, value: impl Into<Value>) -> Query<E>

column = value. An equality on a shard-encoded column also fixes the query’s shard route (transparently).

Source

pub fn where_ne(self, column: &str, value: impl Into<Value>) -> Query<E>

column <> value.

Source

pub fn where_gt(self, column: &str, value: impl Into<Value>) -> Query<E>

column > value.

Source

pub fn where_gte(self, column: &str, value: impl Into<Value>) -> Query<E>

column >= value.

Source

pub fn where_lt(self, column: &str, value: impl Into<Value>) -> Query<E>

column < value.

Source

pub fn where_lte(self, column: &str, value: impl Into<Value>) -> Query<E>

column <= value.

Source

pub fn where_like(self, column: &str, pattern: &str) -> Query<E>

column LIKE pattern (use %/_ wildcards).

Source

pub fn where_not_like(self, column: &str, pattern: &str) -> Query<E>

column NOT LIKE pattern.

Source

pub fn where_in<V>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Query<E>
where V: Into<Value>,

column IN (values). An empty set matches nothing.

Source

pub fn where_not_in<V>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Query<E>
where V: Into<Value>,

column NOT IN (values).

Source

pub fn where_null(self, column: &str) -> Query<E>

column IS NULL.

Source

pub fn where_not_null(self, column: &str) -> Query<E>

column IS NOT NULL.

Source

pub fn filter(self, cond: Condition) -> Query<E>

Add a raw sea_query::Cond (AND-combined with the rest). The escape hatch for OR groups and any predicate the helpers don’t cover: .filter(Cond::any().add(a).add(b)).

Source

pub fn join( self, foreign_table: &str, local_col: &str, foreign_col: &str, ) -> Query<E>

INNER JOIN foreign_table ON E.local_col = foreign_table.foreign_col. For filtering/selecting E only — the joined table is not decoded.

Source

pub fn left_join( self, foreign_table: &str, local_col: &str, foreign_col: &str, ) -> Query<E>

As join but a LEFT JOIN.

Source

pub fn order_by_asc(self, column: &str) -> Query<E>

ORDER BY column ASC.

Source

pub fn order_by_desc(self, column: &str) -> Query<E>

ORDER BY column DESC.

Source

pub fn limit(self, n: u64) -> Query<E>

LIMIT n.

Source

pub fn offset(self, n: u64) -> Query<E>

OFFSET n.

Source

pub fn all<'a, X>( self, exec: &'a X, ) -> impl Future<Output = Result<Vec<E>, Error>> + 'a
where X: Executor, E: 'a,

Run the query and decode every matching row into E.

Source

pub fn first<'a, X>( self, exec: &'a X, ) -> impl Future<Output = Result<Option<E>, Error>> + 'a
where X: Executor, E: 'a,

Run with LIMIT 1 and decode the first match, if any.

Source

pub fn count<'a, X>( self, exec: &'a X, ) -> impl Future<Output = Result<u64, Error>> + 'a
where X: Executor, E: 'a,

SELECT COUNT(*) under the same filters and joins.

Source

pub fn update<'a, X>( self, exec: &'a X, set: Vec<(&str, Value)>, ) -> impl Future<Output = Result<u64, Error>> + 'a
where X: Executor,

UPDATE the given column = value pairs on every row matching the filters — a partial update (only the listed columns), unlike repo::update which writes the whole row. Joins are ignored. Returns rows affected.

ⓘ
repo::query::<Token>()
    .where_eq("id", id)
    .update(&db, vec![("last_used_at", now.into())]).await?;
Source

pub fn increment<'a, X>( self, exec: &'a X, column: &str, by: i64, ) -> impl Future<Output = Result<u64, Error>> + 'a
where X: Executor,

Atomically add by to column (SET column = column + by) on every matching row — a column-relative update no value-binding can express (e.g. attempts = attempts + 1). Returns rows affected.

Source

pub fn delete<'a, X>( self, exec: &'a X, ) -> impl Future<Output = Result<u64, Error>> + 'a
where X: Executor,

DELETE every row matching the filters. Joins are ignored (portable DELETE takes no joins); express the condition with WHERE/subqueries instead. Returns rows affected.

Source

pub fn first_or_create<'a, X>( self, exec: &'a X, default: E, ) -> impl Future<Output = Result<E, Error>> + 'a
where X: Executor, E: 'a,

Return the first match, or insert default and return it. Not atomic — two racing callers can both insert; add a UNIQUE constraint on the lookup column(s) if you need the database to arbitrate.

Trait Implementations§

Source§

impl<E> Default for Query<E>
where E: Entity,

Source§

fn default() -> Query<E>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<E> !RefUnwindSafe for Query<E>

§

impl<E> !Send for Query<E>

§

impl<E> !Sync for Query<E>

§

impl<E> !UnwindSafe for Query<E>

§

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

§

impl<E> Unpin for Query<E>
where PhantomData<E>: Unpin,

§

impl<E> UnsafeUnpin for Query<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<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, 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<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