toasty_core/stmt/
select.rs1use super::{Node, Path, Query, Returning, Source, SourceModel, Statement, Visit, VisitMut};
2use crate::{
3 schema::db::TableId,
4 stmt::{ExprSet, Filter},
5};
6
7#[derive(Debug, Clone, PartialEq)]
25pub struct Select {
26 pub returning: Returning,
28
29 pub source: Source,
32
33 pub filter: Filter,
35}
36
37impl Select {
38 pub fn new(source: impl Into<Source>, filter: impl Into<Filter>) -> Self {
41 Self {
42 returning: Returning::Model { include: vec![] },
43 source: source.into(),
44 filter: filter.into(),
45 }
46 }
47
48 pub(crate) fn include(&mut self, path: impl Into<Path>) {
54 match &mut self.returning {
55 Returning::Model { include } => include.push(path.into()),
56 _ => panic!("Expected Returning::Model for include operation"),
57 }
58 }
59
60 pub fn add_filter(&mut self, filter: impl Into<Filter>) {
62 self.filter.add_filter(filter);
63 }
64}
65
66impl Statement {
67 pub fn query_select(&self) -> Option<&Select> {
70 self.as_query().and_then(|query| query.body.as_select())
71 }
72
73 #[track_caller]
79 pub fn query_select_unwrap(&self) -> &Select {
80 match self {
81 Statement::Query(query) => match &query.body {
82 ExprSet::Select(select) => select,
83 _ => panic!("expected `Select`; actual={self:#?}"),
84 },
85 _ => panic!("expected `Select`; actual={self:#?}"),
86 }
87 }
88}
89
90impl Query {
91 pub fn into_select(self) -> Select {
97 self.body.into_select()
98 }
99}
100
101impl ExprSet {
102 pub fn as_select(&self) -> Option<&Select> {
104 match self {
105 Self::Select(expr) => Some(expr),
106 _ => None,
107 }
108 }
109
110 #[track_caller]
113 pub fn as_select_unwrap(&self) -> &Select {
114 self.as_select()
115 .unwrap_or_else(|| panic!("expected `Select`; actual={self:#?}"))
116 }
117
118 pub fn as_select_mut(&mut self) -> Option<&mut Select> {
121 match self {
122 Self::Select(expr) => Some(expr),
123 _ => None,
124 }
125 }
126
127 #[track_caller]
130 pub fn as_select_mut_unwrap(&mut self) -> &mut Select {
131 match self {
132 Self::Select(select) => select,
133 _ => panic!("expected `Select`; actual={self:#?}"),
134 }
135 }
136
137 #[track_caller]
143 pub fn into_select(self) -> Select {
144 match self {
145 Self::Select(expr) => *expr,
146 _ => todo!(),
147 }
148 }
149
150 pub fn is_select(&self) -> bool {
152 matches!(self, Self::Select(_))
153 }
154}
155
156impl From<Select> for Statement {
157 fn from(value: Select) -> Self {
158 Self::Query(value.into())
159 }
160}
161
162impl From<Select> for Query {
163 fn from(value: Select) -> Self {
164 Self::builder(value).build()
165 }
166}
167
168impl From<TableId> for Select {
169 fn from(value: TableId) -> Self {
170 Self::new(Source::table(value), true)
171 }
172}
173
174impl From<SourceModel> for Select {
175 fn from(value: SourceModel) -> Self {
176 Self::new(Source::Model(value), true)
177 }
178}
179
180impl Node for Select {
181 fn visit<V: Visit>(&self, mut visit: V) {
182 visit.visit_stmt_select(self);
183 }
184
185 fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
186 visit.visit_stmt_select_mut(self);
187 }
188}