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,
impl<E> Query<E>where
E: Entity,
Sourcepub fn new() -> Query<E>
pub fn new() -> Query<E>
An unfiltered query over E’s table. Prefer repo::query for brevity.
Sourcepub fn where_eq(self, column: &str, value: impl Into<Value>) -> Query<E>
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).
Sourcepub fn where_like(self, column: &str, pattern: &str) -> Query<E>
pub fn where_like(self, column: &str, pattern: &str) -> Query<E>
column LIKE pattern (use %/_ wildcards).
Sourcepub fn where_not_like(self, column: &str, pattern: &str) -> Query<E>
pub fn where_not_like(self, column: &str, pattern: &str) -> Query<E>
column NOT LIKE pattern.
Sourcepub fn where_in<V>(
self,
column: &str,
values: impl IntoIterator<Item = V>,
) -> Query<E>
pub fn where_in<V>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Query<E>
column IN (values). An empty set matches nothing.
Sourcepub fn where_not_in<V>(
self,
column: &str,
values: impl IntoIterator<Item = V>,
) -> Query<E>
pub fn where_not_in<V>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Query<E>
column NOT IN (values).
Sourcepub fn where_null(self, column: &str) -> Query<E>
pub fn where_null(self, column: &str) -> Query<E>
column IS NULL.
Sourcepub fn where_not_null(self, column: &str) -> Query<E>
pub fn where_not_null(self, column: &str) -> Query<E>
column IS NOT NULL.
Sourcepub fn filter(self, cond: Condition) -> Query<E>
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)).
Sourcepub fn join(
self,
foreign_table: &str,
local_col: &str,
foreign_col: &str,
) -> Query<E>
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.
Sourcepub fn left_join(
self,
foreign_table: &str,
local_col: &str,
foreign_col: &str,
) -> Query<E>
pub fn left_join( self, foreign_table: &str, local_col: &str, foreign_col: &str, ) -> Query<E>
As join but a LEFT JOIN.
Sourcepub fn order_by_asc(self, column: &str) -> Query<E>
pub fn order_by_asc(self, column: &str) -> Query<E>
ORDER BY column ASC.
Sourcepub fn order_by_desc(self, column: &str) -> Query<E>
pub fn order_by_desc(self, column: &str) -> Query<E>
ORDER BY column DESC.
Sourcepub fn all<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<Vec<E>, Error>> + 'awhere
X: Executor,
E: 'a,
pub fn all<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<Vec<E>, Error>> + 'awhere
X: Executor,
E: 'a,
Run the query and decode every matching row into E.
Sourcepub fn first<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<Option<E>, Error>> + 'awhere
X: Executor,
E: 'a,
pub fn first<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<Option<E>, Error>> + 'awhere
X: Executor,
E: 'a,
Run with LIMIT 1 and decode the first match, if any.
Sourcepub fn count<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
X: Executor,
E: 'a,
pub fn count<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
X: Executor,
E: 'a,
SELECT COUNT(*) under the same filters and joins.
Sourcepub fn update<'a, X>(
self,
exec: &'a X,
set: Vec<(&str, Value)>,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
X: Executor,
pub fn update<'a, X>(
self,
exec: &'a X,
set: Vec<(&str, Value)>,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
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?;Sourcepub fn increment<'a, X>(
self,
exec: &'a X,
column: &str,
by: i64,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
X: Executor,
pub fn increment<'a, X>(
self,
exec: &'a X,
column: &str,
by: i64,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
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.
Sourcepub fn delete<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
X: Executor,
pub fn delete<'a, X>(
self,
exec: &'a X,
) -> impl Future<Output = Result<u64, Error>> + 'awhere
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.
Sourcepub fn first_or_create<'a, X>(
self,
exec: &'a X,
default: E,
) -> impl Future<Output = Result<E, Error>> + 'awhere
X: Executor,
E: 'a,
pub fn first_or_create<'a, X>(
self,
exec: &'a X,
default: E,
) -> impl Future<Output = Result<E, Error>> + 'awhere
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.