toasty_core/stmt/
query.rs

1use super::{
2    Delete, ExprSet, Limit, Node, OrderBy, Path, Returning, Select, Source, Statement, Update,
3    UpdateTarget, Values, Visit, VisitMut, With,
4};
5use crate::stmt::{self, Filter};
6
7/// A query statement that reads data from the database.
8///
9/// `Query` wraps a set expression body (typically a [`Select`]) with optional
10/// ordering, limits, CTEs, and row-level locks. It is the read-side counterpart
11/// to [`Insert`], [`Update`], and [`Delete`].
12///
13/// # Examples
14///
15/// ```ignore
16/// use toasty_core::stmt::{Query, Values, ExprSet};
17///
18/// // A unit query that returns one empty row
19/// let q = Query::unit();
20/// assert!(matches!(q.body, ExprSet::Values(_)));
21/// assert!(!q.single);
22/// ```
23#[derive(Debug, Clone, PartialEq)]
24pub struct Query {
25    /// Optional common table expressions (CTEs) for this query.
26    pub with: Option<With>,
27
28    /// The body of the query. Either `SELECT`, `UNION`, `VALUES`, or possibly
29    /// other types of queries depending on database support.
30    pub body: ExprSet,
31
32    /// When `true`, the query returns a single record instead of a list.
33    ///
34    /// This is semantically different from `LIMIT 1`: it indicates there can
35    /// only ever be one matching result. The return type becomes `Record`
36    /// instead of `List`.
37    pub single: bool,
38
39    /// Optional `ORDER BY` clause.
40    pub order_by: Option<OrderBy>,
41
42    /// Optional `LIMIT` and `OFFSET` clause.
43    pub limit: Option<Limit>,
44
45    /// Row-level locks (`FOR UPDATE`, `FOR SHARE`).
46    pub locks: Vec<Lock>,
47}
48
49/// A row-level lock to acquire when executing a query.
50///
51/// Corresponds to SQL's `FOR UPDATE` and `FOR SHARE` clauses. Only meaningful
52/// for SQL databases that support row-level locking.
53///
54/// # Examples
55///
56/// ```ignore
57/// use toasty_core::stmt::Lock;
58///
59/// let lock = Lock::Update;
60/// ```
61#[derive(Debug, Clone, PartialEq)]
62pub enum Lock {
63    /// `FOR UPDATE` -- acquire an exclusive lock on matched rows.
64    Update,
65    /// `FOR SHARE` -- acquire a shared lock on matched rows.
66    Share,
67}
68
69/// Builder for constructing [`Query`] instances with optional clauses.
70///
71/// Created via [`Query::builder`]. Allows chaining calls to add CTEs, filters,
72/// returning clauses, and locks before calling [`build`](QueryBuilder::build).
73///
74/// # Examples
75///
76/// ```ignore
77/// use toasty_core::stmt::{Query, Select, Source, Filter};
78///
79/// let select = Select::new(Source::from(ModelId(0)), Filter::ALL);
80/// let query = Query::builder(select).build();
81/// ```
82#[derive(Debug)]
83pub struct QueryBuilder {
84    query: Query,
85}
86
87impl Query {
88    /// Creates a new query with the given body and default options (no ordering,
89    /// no limit, not single, no locks).
90    pub fn new(body: impl Into<ExprSet>) -> Self {
91        Self {
92            with: None,
93            body: body.into(),
94            single: false,
95            order_by: None,
96            limit: None,
97            locks: vec![],
98        }
99    }
100
101    /// Creates a new query that returns exactly one record (`single = true`).
102    pub fn new_single(body: impl Into<ExprSet>) -> Self {
103        Self {
104            with: None,
105            body: body.into(),
106            single: true,
107            order_by: None,
108            limit: None,
109            locks: vec![],
110        }
111    }
112
113    /// Creates a new `SELECT` query from a source and filter.
114    pub fn new_select(source: impl Into<Source>, filter: impl Into<Filter>) -> Self {
115        Self::builder(Select::new(source, filter)).build()
116    }
117
118    /// Returns a [`QueryBuilder`] initialized with the given body.
119    pub fn builder(body: impl Into<ExprSet>) -> QueryBuilder {
120        QueryBuilder {
121            query: Query::new(body),
122        }
123    }
124
125    /// Creates a unit query that produces one empty row (empty `VALUES`).
126    pub fn unit() -> Self {
127        Query::new(Values::default())
128    }
129
130    /// Creates a query whose body is a `VALUES` expression.
131    pub fn values(values: impl Into<Values>) -> Self {
132        Self {
133            with: None,
134            body: ExprSet::Values(values.into()),
135            single: false,
136            order_by: None,
137            limit: None,
138            locks: vec![],
139        }
140    }
141
142    /// Converts this query into an [`Update`] statement targeting the same
143    /// source. The query must have a `SELECT` body with a model source.
144    pub fn update(self) -> Update {
145        let ExprSet::Select(select) = &self.body else {
146            todo!("stmt={self:#?}");
147        };
148
149        assert!(select.source.is_model());
150
151        stmt::Update {
152            target: UpdateTarget::Query(Box::new(self)),
153            assignments: stmt::Assignments::default(),
154            filter: Filter::default(),
155            condition: stmt::Condition::default(),
156            returning: None,
157        }
158    }
159
160    /// Converts this query into a [`Delete`] statement. The query body must
161    /// be a `SELECT`.
162    pub fn delete(self) -> Delete {
163        match self.body {
164            ExprSet::Select(select) => Delete {
165                from: select.source,
166                filter: select.filter,
167                returning: None,
168            },
169            _ => todo!("{self:#?}"),
170        }
171    }
172
173    /// Adds a filter to this query's `SELECT` body.
174    ///
175    /// # Panics
176    ///
177    /// Panics if the query body is not a `SELECT`.
178    pub fn add_filter(&mut self, filter: impl Into<Filter>) {
179        self.body.as_select_mut_unwrap().add_filter(filter);
180    }
181
182    /// Adds an association include path to this query's `SELECT` body.
183    pub fn include(&mut self, path: impl Into<Path>) {
184        match &mut self.body {
185            ExprSet::Select(body) => body.include(path),
186            _ => todo!(),
187        }
188    }
189}
190
191impl Statement {
192    /// Returns `true` if this statement is a [`Query`].
193    pub fn is_query(&self) -> bool {
194        matches!(self, Statement::Query(_))
195    }
196
197    /// Attempts to return a reference to an inner [`Query`].
198    ///
199    /// * If `self` is a [`Statement::Query`], a reference to the inner [`Query`] is
200    ///   returned wrapped in [`Some`].
201    /// * Else, [`None`] is returned.
202    pub fn as_query(&self) -> Option<&Query> {
203        match self {
204            Self::Query(query) => Some(query),
205            _ => None,
206        }
207    }
208
209    /// Returns a mutable reference to the inner [`Query`], if this is a query statement.
210    ///
211    /// * If `self` is a [`Statement::Query`], a mutable reference to the inner [`Query`] is
212    ///   returned wrapped in [`Some`].
213    /// * Else, [`None`] is returned.
214    pub fn as_query_mut(&mut self) -> Option<&mut Query> {
215        match self {
216            Self::Query(query) => Some(query),
217            _ => None,
218        }
219    }
220
221    /// Consumes `self` and attempts to return the inner [`Query`].
222    ///
223    /// Returns `None` if `self` is not a [`Statement::Query`].
224    pub fn into_query(self) -> Option<Query> {
225        match self {
226            Self::Query(query) => Some(query),
227            _ => None,
228        }
229    }
230
231    /// Consumes `self` and returns the inner [`Query`].
232    ///
233    /// # Panics
234    ///
235    /// If `self` is not a [`Statement::Query`].
236    #[track_caller]
237    pub fn into_query_unwrap(self) -> Query {
238        match self {
239            Self::Query(query) => query,
240            v => panic!("expected `Query`, found {v:#?}"),
241        }
242    }
243}
244
245impl From<Query> for Statement {
246    fn from(value: Query) -> Self {
247        Self::Query(value)
248    }
249}
250
251impl Node for Query {
252    fn visit<V: Visit>(&self, mut visit: V) {
253        visit.visit_stmt_query(self);
254    }
255
256    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
257        visit.visit_stmt_query_mut(self);
258    }
259}
260
261impl QueryBuilder {
262    /// Sets the `WITH` (CTE) clause for the query being built.
263    pub fn with(mut self, with: impl Into<With>) -> Self {
264        self.query.with = Some(with.into());
265        self
266    }
267
268    /// Sets the row-level locks for the query being built.
269    pub fn locks(mut self, locks: impl Into<Vec<Lock>>) -> Self {
270        self.query.locks = locks.into();
271        self
272    }
273
274    /// Sets the filter on the query's `SELECT` body.
275    ///
276    /// # Panics
277    ///
278    /// Panics if the query body is not a `SELECT`.
279    pub fn filter(mut self, filter: impl Into<Filter>) -> Self {
280        let filter = filter.into();
281
282        match &mut self.query.body {
283            ExprSet::Select(select) => {
284                select.filter = filter;
285            }
286            _ => todo!("query={self:#?}"),
287        }
288
289        self
290    }
291
292    /// Sets the returning clause on the query's `SELECT` body.
293    pub fn returning(mut self, returning: impl Into<Returning>) -> Self {
294        let returning = returning.into();
295
296        match &mut self.query.body {
297            ExprSet::Select(select) => {
298                select.returning = returning;
299            }
300            _ => todo!(),
301        }
302
303        self
304    }
305
306    /// Consumes this builder and returns the constructed [`Query`].
307    pub fn build(self) -> Query {
308        self.query
309    }
310}
311
312impl From<QueryBuilder> for Query {
313    fn from(value: QueryBuilder) -> Self {
314        value.build()
315    }
316}