pub struct Query {
pub with: Option<With>,
pub body: ExprSet,
pub single: bool,
pub order_by: Option<OrderBy>,
pub limit: Option<Limit>,
pub locks: Vec<Lock>,
}Expand description
A query statement that reads data from the database.
Query wraps a set expression body (typically a Select) with optional
ordering, limits, CTEs, and row-level locks. It is the read-side counterpart
to [Insert], Update, and Delete.
§Examples
use toasty_core::stmt::{Query, Values, ExprSet};
// A unit query that returns one empty row
let q = Query::unit();
assert!(matches!(q.body, ExprSet::Values(_)));
assert!(!q.single);Fields§
§with: Option<With>Optional common table expressions (CTEs) for this query.
body: ExprSetThe body of the query. Either SELECT, UNION, VALUES, or possibly
other types of queries depending on database support.
single: boolWhen true, the query returns a single record instead of a list.
This is semantically different from LIMIT 1: it indicates there can
only ever be one matching result. The return type becomes Record
instead of List.
order_by: Option<OrderBy>Optional ORDER BY clause.
limit: Option<Limit>Optional LIMIT and OFFSET clause.
locks: Vec<Lock>Row-level locks (FOR UPDATE, FOR SHARE).
Implementations§
Source§impl Query
impl Query
Sourcepub fn filter(&self) -> Option<&Filter>
pub fn filter(&self) -> Option<&Filter>
Returns a reference to this query’s filter if the body is a SELECT.
Sourcepub fn filter_unwrap(&self) -> &Filter
pub fn filter_unwrap(&self) -> &Filter
Sourcepub fn filter_mut(&mut self) -> Option<&mut Filter>
pub fn filter_mut(&mut self) -> Option<&mut Filter>
Returns a mutable reference to the query’s filter.
Returns None for queries that are not SELECT statements, such as
UNION or VALUES.
Sourcepub fn filter_mut_unwrap(&mut self) -> &mut Filter
pub fn filter_mut_unwrap(&mut self) -> &mut Filter
Returns a mutable reference to the query’s filter.
§Panics
Panics if the query body is not a SELECT statement.
Source§impl Query
impl Query
Sourcepub fn new(body: impl Into<ExprSet>) -> Self
pub fn new(body: impl Into<ExprSet>) -> Self
Creates a new query with the given body and default options (no ordering, no limit, not single, no locks).
Sourcepub fn new_single(body: impl Into<ExprSet>) -> Self
pub fn new_single(body: impl Into<ExprSet>) -> Self
Creates a new query that returns exactly one record (single = true).
Sourcepub fn new_select(source: impl Into<Source>, filter: impl Into<Filter>) -> Self
pub fn new_select(source: impl Into<Source>, filter: impl Into<Filter>) -> Self
Creates a new SELECT query from a source and filter.
Sourcepub fn builder(body: impl Into<ExprSet>) -> QueryBuilder
pub fn builder(body: impl Into<ExprSet>) -> QueryBuilder
Returns a [QueryBuilder] initialized with the given body.
Sourcepub fn values(values: impl Into<Values>) -> Self
pub fn values(values: impl Into<Values>) -> Self
Creates a query whose body is a VALUES expression.
Sourcepub fn update(self) -> Update
pub fn update(self) -> Update
Converts this query into an Update statement targeting the same
source. The query must have a SELECT body with a model source.
Sourcepub fn delete(self) -> Delete
pub fn delete(self) -> Delete
Converts this query into a Delete statement. The query body must
be a SELECT.
Sourcepub fn add_filter(&mut self, filter: impl Into<Filter>)
pub fn add_filter(&mut self, filter: impl Into<Filter>)
Source§impl Query
impl Query
Sourcepub fn returning(&self) -> Option<&Returning>
pub fn returning(&self) -> Option<&Returning>
Returns a reference to this query’s RETURNING clause, if present.
Returns Some only for SELECT queries. Other query types (VALUES,
UNION, etc.) do not have a RETURNING clause.
Sourcepub fn returning_unwrap(&self) -> &Returning
pub fn returning_unwrap(&self) -> &Returning
Returns a reference to this query’s RETURNING clause.
§Panics
Panics if the query does not have a RETURNING clause (i.e., the body
is not a SELECT).
Sourcepub fn returning_mut(&mut self) -> Option<&mut Returning>
pub fn returning_mut(&mut self) -> Option<&mut Returning>
Returns a mutable reference to this query’s RETURNING clause, if present.
Returns Some only for SELECT queries. Other query types (VALUES,
UNION, etc.) do not have a RETURNING clause.
Sourcepub fn returning_mut_unwrap(&mut self) -> &mut Returning
pub fn returning_mut_unwrap(&mut self) -> &mut Returning
Returns a mutable reference to this query’s RETURNING clause.
§Panics
Panics if the query does not have a RETURNING clause (i.e., the body
is not a SELECT).
Trait Implementations§
Source§impl From<Query> for InsertTarget
impl From<Query> for InsertTarget
Source§impl<'a, T: Resolve> IntoExprTarget<'a, T> for &'a Query
impl<'a, T: Resolve> IntoExprTarget<'a, T> for &'a Query
Source§fn into_expr_target(self, schema: &'a T) -> ExprTarget<'a>
fn into_expr_target(self, schema: &'a T) -> ExprTarget<'a>
self into an ExprTarget using the provided schema.