Query

Struct Query 

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: ExprSet

The body of the query. Either SELECT, UNION, VALUES, or possibly other types of queries depending on database support.

§single: bool

When 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§

§

impl Query

pub fn filter(&self) -> Option<&Filter>

Returns a reference to this query’s filter if the body is a SELECT.

pub fn filter_unwrap(&self) -> &Filter

Returns a reference to this query’s filter.

§Panics

Panics if the query body is not a SELECT.

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.

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.

§

impl Query

pub fn new(body: impl Into<ExprSet>) -> Query

Creates a new query with the given body and default options (no ordering, no limit, not single, no locks).

pub fn new_single(body: impl Into<ExprSet>) -> Query

Creates a new query that returns exactly one record (single = true).

pub fn new_select(source: impl Into<Source>, filter: impl Into<Filter>) -> Query

Creates a new SELECT query from a source and filter.

pub fn builder(body: impl Into<ExprSet>) -> QueryBuilder

Returns a [QueryBuilder] initialized with the given body.

pub fn unit() -> Query

Creates a unit query that produces one empty row (empty VALUES).

pub fn values(values: impl Into<Values>) -> Query

Creates a query whose body is a VALUES expression.

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.

pub fn delete(self) -> Delete

Converts this query into a Delete statement. The query body must be a SELECT.

pub fn add_filter(&mut self, filter: impl Into<Filter>)

Adds a filter to this query’s SELECT body.

§Panics

Panics if the query body is not a SELECT.

pub fn include(&mut self, path: impl Into<Path>)

Adds an association include path to this query’s SELECT body.

§

impl Query

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.

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

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.

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

§

impl Query

pub fn into_select(self) -> Select

Consumes this query and returns the inner Select.

§Panics

Panics if the query body is not a SELECT.

Trait Implementations§

§

impl Clone for Query

§

fn clone(&self) -> Query

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Query

§

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

Formats the value using the given formatter. Read more
§

impl From<Query> for InsertTarget

§

fn from(value: Query) -> InsertTarget

Converts to this type from the input type.
§

impl From<QueryBuilder> for Query

§

fn from(value: QueryBuilder) -> Query

Converts to this type from the input type.
§

impl From<Select> for Query

§

fn from(value: Select) -> Query

Converts to this type from the input type.
§

impl From<Values> for Query

§

fn from(value: Values) -> Query

Converts to this type from the input type.
§

impl<'a, T> IntoExprTarget<'a, T> for &'a Query
where T: Resolve,

§

fn into_expr_target(self, schema: &'a T) -> ExprTarget<'a>

Converts self into an ExprTarget using the provided schema.
§

impl Node for Query

§

fn visit<V>(&self, visit: V)
where V: Visit,

Traverses this node with an immutable visitor.
§

fn visit_mut<V>(&mut self, visit: V)
where V: VisitMut,

Traverses this node with a mutable visitor.
§

impl PartialEq for Query

§

fn eq(&self, other: &Query) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl StructuralPartialEq for Query

Auto Trait Implementations§

§

impl Freeze for Query

§

impl RefUnwindSafe for Query

§

impl Send for Query

§

impl Sync for Query

§

impl Unpin for Query

§

impl UnwindSafe for Query

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.