vantage_expressions/traits/
selectable.rs1use std::fmt::Debug;
2
3use super::expressive::Expressive;
4use crate::{Expression, ExpressiveEnum};
5
6#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct Order {
14 pub ascending: bool,
15 pub nulls: Option<Nulls>,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum Nulls {
21 First,
22 Last,
23}
24
25#[allow(non_upper_case_globals)]
26impl Order {
27 pub const Asc: Order = Order {
28 ascending: true,
29 nulls: None,
30 };
31 pub const Desc: Order = Order {
32 ascending: false,
33 nulls: None,
34 };
35
36 pub fn nulls_last(self) -> Self {
37 Self {
38 nulls: Some(Nulls::Last),
39 ..self
40 }
41 }
42
43 pub fn nulls_first(self) -> Self {
44 Self {
45 nulls: Some(Nulls::First),
46 ..self
47 }
48 }
49
50 pub fn suffix(&self) -> &'static str {
52 match (self.ascending, self.nulls) {
53 (true, None) => "",
54 (false, None) => " DESC",
55 (true, Some(Nulls::Last)) => " NULLS LAST",
56 (true, Some(Nulls::First)) => " NULLS FIRST",
57 (false, Some(Nulls::Last)) => " DESC NULLS LAST",
58 (false, Some(Nulls::First)) => " DESC NULLS FIRST",
59 }
60 }
61}
62
63pub trait Selectable<T, C = Expression<T>>: Send + Sync + Debug + Clone {
80 fn add_source(&mut self, source: impl Into<SourceRef<T>>, alias: Option<String>);
83
84 fn add_field(&mut self, field: impl Into<String>);
86
87 fn add_expression(&mut self, expression: impl Expressive<T>);
89
90 fn add_where_condition(&mut self, condition: impl Into<C>);
92
93 fn set_distinct(&mut self, distinct: bool);
95
96 fn add_order_by(&mut self, order: impl Into<C>, direction: Order);
98
99 fn add_group_by(&mut self, expression: impl Expressive<T>);
101
102 fn set_limit(&mut self, limit: Option<i64>, skip: Option<i64>);
104
105 fn clear_fields(&mut self);
107
108 fn clear_where_conditions(&mut self);
110
111 fn clear_order_by(&mut self);
113
114 fn clear_group_by(&mut self);
116
117 fn has_fields(&self) -> bool;
119
120 fn has_where_conditions(&self) -> bool;
122
123 fn has_order_by(&self) -> bool;
125
126 fn has_group_by(&self) -> bool;
128
129 fn is_distinct(&self) -> bool;
131
132 fn get_limit(&self) -> Option<i64>;
134
135 fn get_skip(&self) -> Option<i64>;
137
138 fn as_field(&self, field: impl Into<String>) -> Expression<T>;
142
143 fn as_count(&self) -> Expression<T>;
145
146 fn as_sum(&self, column: impl Expressive<T>) -> Expression<T>;
148
149 fn as_max(&self, column: impl Expressive<T>) -> Expression<T>;
151
152 fn as_min(&self, column: impl Expressive<T>) -> Expression<T>;
154
155 fn with_source(mut self, source: impl Into<SourceRef<T>>) -> Self
159 where
160 Self: Sized,
161 {
162 Self::add_source(&mut self, source, None);
163 self
164 }
165
166 fn with_source_as(mut self, source: impl Into<SourceRef<T>>, alias: impl Into<String>) -> Self
168 where
169 Self: Sized,
170 {
171 Self::add_source(&mut self, source, Some(alias.into()));
172 self
173 }
174
175 fn with_condition(mut self, condition: impl Into<C>) -> Self
177 where
178 Self: Sized,
179 {
180 Self::add_where_condition(&mut self, condition);
181 self
182 }
183
184 fn with_order(mut self, order: impl Into<C>, direction: Order) -> Self
186 where
187 Self: Sized,
188 {
189 Self::add_order_by(&mut self, order, direction);
190 self
191 }
192
193 fn with_field(mut self, field: impl Into<String>) -> Self
195 where
196 Self: Sized,
197 {
198 Self::add_field(&mut self, field);
199 self
200 }
201
202 fn with_expression(mut self, expression: impl Expressive<T>) -> Self
204 where
205 Self: Sized,
206 {
207 Self::add_expression(&mut self, expression);
208 self
209 }
210
211 fn with_group_by(mut self, expression: impl Expressive<T>) -> Self
213 where
214 Self: Sized,
215 {
216 Self::add_group_by(&mut self, expression);
217 self
218 }
219
220 fn with_distinct(mut self, distinct: bool) -> Self
222 where
223 Self: Sized,
224 {
225 Self::set_distinct(&mut self, distinct);
226 self
227 }
228
229 fn with_limit(mut self, limit: Option<i64>, skip: Option<i64>) -> Self
231 where
232 Self: Sized,
233 {
234 Self::set_limit(&mut self, limit, skip);
235 self
236 }
237}
238
239pub struct SourceRef<T>(ExpressiveEnum<T>);
241
242impl<T> SourceRef<T> {
243 pub fn into_expressive_enum(self) -> ExpressiveEnum<T> {
244 self.0
245 }
246}
247
248impl<T> From<&str> for SourceRef<T>
249where
250 T: From<String>,
251{
252 fn from(value: &str) -> Self {
253 SourceRef(ExpressiveEnum::Scalar(T::from(value.to_string())))
254 }
255}
256
257impl<T> From<String> for SourceRef<T>
258where
259 T: From<String>,
260{
261 fn from(value: String) -> Self {
262 SourceRef(ExpressiveEnum::Scalar(T::from(value)))
263 }
264}
265
266impl<T> From<Expression<T>> for SourceRef<T> {
267 fn from(value: Expression<T>) -> Self {
268 SourceRef(ExpressiveEnum::Nested(value))
269 }
270}