pub struct Query<M: Model> { /* private fields */ }Expand description
A query on one model’s table. Start it with Model::query() or Query::table(), then
finish it with a terminal method such as get, first or insert.
Implementations§
Source§impl<M: Model> Query<M>
impl<M: Model> Query<M>
Sourcepub fn where_eq(self, column: &str, value: impl Into<Value>) -> Self
pub fn where_eq(self, column: &str, value: impl Into<Value>) -> Self
Adds column = value. A NULL value becomes IS NULL, as in Laravel.
Sourcepub fn or_where_eq(self, column: &str, value: impl Into<Value>) -> Self
pub fn or_where_eq(self, column: &str, value: impl Into<Value>) -> Self
Adds OR column = value.
Sourcepub fn where_op(
self,
column: &str,
op: Operator,
value: impl Into<Value>,
) -> Self
pub fn where_op( self, column: &str, op: Operator, value: impl Into<Value>, ) -> Self
Adds column <op> value. != with a NULL value becomes IS NOT NULL.
Sourcepub fn or_where_op(
self,
column: &str,
op: Operator,
value: impl Into<Value>,
) -> Self
pub fn or_where_op( self, column: &str, op: Operator, value: impl Into<Value>, ) -> Self
Adds OR column <op> value. != with a NULL value becomes IS NOT NULL.
Sourcepub fn where_in<V: Into<Value>>(
self,
column: &str,
values: impl IntoIterator<Item = V>,
) -> Self
pub fn where_in<V: Into<Value>>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Self
Adds column IN (values). An empty list matches no rows, as in Laravel.
Sourcepub fn or_where_in<V: Into<Value>>(
self,
column: &str,
values: impl IntoIterator<Item = V>,
) -> Self
pub fn or_where_in<V: Into<Value>>( self, column: &str, values: impl IntoIterator<Item = V>, ) -> Self
Adds OR column IN (values). An empty list matches no rows.
Sourcepub fn where_null(self, column: &str) -> Self
pub fn where_null(self, column: &str) -> Self
Adds column IS NULL.
Sourcepub fn or_where_null(self, column: &str) -> Self
pub fn or_where_null(self, column: &str) -> Self
Adds OR column IS NULL.
Sourcepub fn where_not_null(self, column: &str) -> Self
pub fn where_not_null(self, column: &str) -> Self
Adds column IS NOT NULL.
Sourcepub fn or_where_not_null(self, column: &str) -> Self
pub fn or_where_not_null(self, column: &str) -> Self
Adds OR column IS NOT NULL.
Sourcepub fn where_group(self, f: impl FnOnce(Self) -> Self) -> Self
pub fn where_group(self, f: impl FnOnce(Self) -> Self) -> Self
Adds a group of conditions joined by AND, in parentheses. The closure gets an empty
query and adds its conditions to it, as Laravel’s where(fn) does:
.where_group(|q| q.where_eq("title", "a").or_where_eq("title", "b"))Only where conditions are kept from the group; order_by, limit and the other
settings are ignored. An empty group adds nothing.
Sourcepub fn or_where_group(self, f: impl FnOnce(Self) -> Self) -> Self
pub fn or_where_group(self, f: impl FnOnce(Self) -> Self) -> Self
Adds a group of conditions joined by OR, in parentheses. See where_group.
Sourcepub fn order_by(self, column: &str, direction: Direction) -> Self
pub fn order_by(self, column: &str, direction: Direction) -> Self
Adds a sort. Later calls sort within the earlier ones.
Sourcepub fn select(self, columns: &[&str]) -> Self
pub fn select(self, columns: &[&str]) -> Self
Chooses the columns that get and first select. Without it, all columns are selected.
Sourcepub fn for_update(self) -> Self
pub fn for_update(self) -> Self
Locks the matching rows (SELECT ... FOR UPDATE). It applies to get, first, find,
count and exists. In a transaction the lock lasts until it ends. On the pool it lasts
only for the statement, in autocommit, as Laravel’s lockForUpdate() does. SQLite has no
row locks, so the clause is left out there, as Laravel’s SQLite grammar does.
Sourcepub async fn get<'a>(self, db: impl Executor<'a>) -> Result<Vec<M>, Error>where
M: DriverRows,
pub async fn get<'a>(self, db: impl Executor<'a>) -> Result<Vec<M>, Error>where
M: DriverRows,
Returns every row that matches.
Sourcepub async fn get_as<'a, T>(self, db: impl Executor<'a>) -> Result<Vec<T>, Error>
pub async fn get_as<'a, T>(self, db: impl Executor<'a>) -> Result<Vec<T>, Error>
Returns every row that matches, decoded as T instead of the model. Use it with select
to read some columns into a tuple or a struct that implements FromRow. The clauses are
the same as get.
let titles = Post::query()
.select(&["id", "title"])
.get_as::<(i64, String)>(&db)
.await?;Sourcepub async fn first_as<'a, T>(
self,
db: impl Executor<'a>,
) -> Result<Option<T>, Error>
pub async fn first_as<'a, T>( self, db: impl Executor<'a>, ) -> Result<Option<T>, Error>
Returns the first row decoded as T, in the order given by order_by. See get_as.
Sourcepub async fn first<'a>(self, db: impl Executor<'a>) -> Result<Option<M>, Error>where
M: DriverRows,
pub async fn first<'a>(self, db: impl Executor<'a>) -> Result<Option<M>, Error>where
M: DriverRows,
Returns the first row in the order given by order_by. Without order_by there is no
order, as with Laravel’s first, so call order_by when the row matters.
Sourcepub async fn first_or_fail<'a>(self, db: impl Executor<'a>) -> Result<M, Error>where
M: DriverRows,
pub async fn first_or_fail<'a>(self, db: impl Executor<'a>) -> Result<M, Error>where
M: DriverRows,
Returns the first row, or an error when none matches.
Sourcepub async fn find<'a>(
self,
db: impl Executor<'a>,
id: impl Into<Value>,
) -> Result<Option<M>, Error>where
M: DriverRows,
pub async fn find<'a>(
self,
db: impl Executor<'a>,
id: impl Into<Value>,
) -> Result<Option<M>, Error>where
M: DriverRows,
Returns the row with the given primary key, or None when there is none.
Sourcepub async fn find_or_fail<'a>(
self,
db: impl Executor<'a>,
id: impl Into<Value>,
) -> Result<M, Error>where
M: DriverRows,
pub async fn find_or_fail<'a>(
self,
db: impl Executor<'a>,
id: impl Into<Value>,
) -> Result<M, Error>where
M: DriverRows,
Returns the row with the given primary key, or an error when there is none.
Sourcepub async fn count<'a>(self, db: impl Executor<'a>) -> Result<i64, Error>
pub async fn count<'a>(self, db: impl Executor<'a>) -> Result<i64, Error>
Counts the rows that match.
Sourcepub async fn exists<'a>(self, db: impl Executor<'a>) -> Result<bool, Error>
pub async fn exists<'a>(self, db: impl Executor<'a>) -> Result<bool, Error>
Returns whether any row matches.
Sourcepub async fn insert<'a>(
self,
db: impl Executor<'a>,
values: &[(&str, Value)],
) -> Result<u64, Error>
pub async fn insert<'a>( self, db: impl Executor<'a>, values: &[(&str, Value)], ) -> Result<u64, Error>
Inserts one row and returns its auto-increment id. The columns must be in FILLABLE.
On PostgreSQL the id comes from RETURNING on the primary key, so the primary key must be
an integer column.
Sourcepub async fn replace<'a>(
self,
db: impl Executor<'a>,
values: &[(&str, Value)],
) -> Result<u64, Error>
pub async fn replace<'a>( self, db: impl Executor<'a>, values: &[(&str, Value)], ) -> Result<u64, Error>
Replaces the row that has the same unique key or primary key as values, or inserts the
row when none has. Returns the rows affected, as the database reports them: MySQL gives 2
when a row was replaced, since it deletes the old row and inserts the new one, and SQLite
gives 1.
This is Diesel’s replace_into, which issues REPLACE INTO on MySQL and SQLite. It is not
Laravel’s upsert, which is INSERT ... ON DUPLICATE KEY UPDATE or ON CONFLICT. The old
row is deleted, so its auto-increment id changes and columns not in values take their
defaults. The columns must be in FILLABLE.
PostgreSQL has no REPLACE, and INSERT ... ON CONFLICT would keep the old row. So on
PostgreSQL this returns Error::Unsupported.
Auto Trait Implementations§
impl<M> !RefUnwindSafe for Query<M>
impl<M> !UnwindSafe for Query<M>
impl<M> Forget for Query<M>
impl<M> Freeze for Query<M>where
PhantomData<M>: Freeze,
impl<M> Move for Query<M>
impl<M> Send for Query<M>where
PhantomData<M>: Send,
impl<M> Sync for Query<M>where
PhantomData<M>: Sync,
impl<M> Unpin for Query<M>where
PhantomData<M>: Unpin,
impl<M> UnsafeUnpin for Query<M>where
PhantomData<M>: UnsafeUnpin,
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
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