1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use super::{Node, Path, Query, Returning, Source, SourceModel, Statement, Visit, VisitMut};
use crate::{
schema::db::TableId,
stmt::{ExprSet, Filter},
};
/// A `SELECT` expression within a query body.
///
/// Represents the combination of a data source, a filter (WHERE clause), and a
/// projection (RETURNING/SELECT list). This is the most common query body type.
///
/// At the model level, the source is a model with optional association includes.
/// After lowering, the source becomes a table with joins.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{Select, Source, Filter};
/// use toasty_core::schema::app::ModelId;
///
/// let select = Select::new(Source::from(ModelId(0)), Filter::ALL);
/// assert!(select.source.is_model());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Select {
/// The projection (what columns/fields to return).
pub returning: Returning,
/// The data source (`FROM` clause). At the model level this is a model
/// reference; at the table level this is a table with joins.
pub source: Source,
/// The filter (`WHERE` clause).
pub filter: Filter,
}
impl Select {
/// Creates a new `Select` with the given source and filter, defaulting to
/// a model-level returning clause with no includes.
pub fn new(source: impl Into<Source>, filter: impl Into<Filter>) -> Self {
Self {
returning: Returning::Model { include: vec![] },
source: source.into(),
filter: filter.into(),
}
}
/// Adds an association include path to the returning clause.
///
/// # Panics
///
/// Panics if the returning clause is not `Returning::Model`.
pub(crate) fn include(&mut self, path: impl Into<Path>) {
match &mut self.returning {
Returning::Model { include } => include.push(path.into()),
_ => panic!("Expected Returning::Model for include operation"),
}
}
/// Adds an additional filter, AND-ing it with any existing filter.
pub fn add_filter(&mut self, filter: impl Into<Filter>) {
self.filter.add_filter(filter);
}
}
impl Statement {
/// If this is a query with a `SELECT` body, returns a reference to that
/// [`Select`]. Returns `None` otherwise.
pub fn query_select(&self) -> Option<&Select> {
self.as_query().and_then(|query| query.body.as_select())
}
/// Returns a reference to this statement's inner [`Select`].
///
/// # Panics
///
/// Panics if this is not a query statement with a `SELECT` body.
#[track_caller]
pub fn query_select_unwrap(&self) -> &Select {
match self {
Statement::Query(query) => match &query.body {
ExprSet::Select(select) => select,
_ => panic!("expected `Select`; actual={self:#?}"),
},
_ => panic!("expected `Select`; actual={self:#?}"),
}
}
}
impl Query {
/// Consumes this query and returns the inner [`Select`].
///
/// # Panics
///
/// Panics if the query body is not a `SELECT`.
pub fn into_select(self) -> Select {
self.body.into_select()
}
}
impl ExprSet {
/// Returns a reference to the inner [`Select`] if this is a `Select` variant.
pub fn as_select(&self) -> Option<&Select> {
match self {
Self::Select(expr) => Some(expr),
_ => None,
}
}
/// Returns a reference to the inner [`Select`], panicking if this is not a
/// `Select` variant.
#[track_caller]
pub fn as_select_unwrap(&self) -> &Select {
self.as_select()
.unwrap_or_else(|| panic!("expected `Select`; actual={self:#?}"))
}
/// Returns a mutable reference to the inner [`Select`] if this is a
/// `Select` variant.
pub fn as_select_mut(&mut self) -> Option<&mut Select> {
match self {
Self::Select(expr) => Some(expr),
_ => None,
}
}
/// Returns a mutable reference to the inner [`Select`], panicking if this
/// is not a `Select` variant.
#[track_caller]
pub fn as_select_mut_unwrap(&mut self) -> &mut Select {
match self {
Self::Select(select) => select,
_ => panic!("expected `Select`; actual={self:#?}"),
}
}
/// Consumes this `ExprSet` and returns the inner [`Select`].
///
/// # Panics
///
/// Panics if this is not a `Select` variant.
#[track_caller]
pub fn into_select(self) -> Select {
match self {
Self::Select(expr) => *expr,
_ => todo!(),
}
}
/// Returns `true` if this is a `Select` variant.
pub fn is_select(&self) -> bool {
matches!(self, Self::Select(_))
}
}
impl From<Select> for Statement {
fn from(value: Select) -> Self {
Self::Query(value.into())
}
}
impl From<Select> for Query {
fn from(value: Select) -> Self {
Self::builder(value).build()
}
}
impl From<TableId> for Select {
fn from(value: TableId) -> Self {
Self::new(Source::table(value), true)
}
}
impl From<SourceModel> for Select {
fn from(value: SourceModel) -> Self {
Self::new(Source::Model(value), true)
}
}
impl Node for Select {
fn visit<V: Visit>(&self, mut visit: V) {
visit.visit_stmt_select(self);
}
fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
visit.visit_stmt_select_mut(self);
}
}