gitql_ast/
statement.rs

1use std::any::Any;
2use std::collections::HashMap;
3
4use dyn_clone::DynClone;
5
6use crate::expression::Expr;
7
8pub enum StatementKind {
9    Select,
10    Where,
11    Having,
12    Limit,
13    Offset,
14    OrderBy,
15    GroupBy,
16    AggregateFunction,
17    WindowFunction,
18    Qualify,
19    Into,
20}
21
22dyn_clone::clone_trait_object!(Statement);
23
24pub trait Statement: DynClone {
25    fn kind(&self) -> StatementKind;
26    fn as_any(&self) -> &dyn Any;
27}
28
29#[derive(Clone)]
30pub enum Distinct {
31    None,
32    DistinctAll,
33    DistinctOn(Vec<String>),
34}
35
36#[derive(Clone)]
37pub struct TableSelection {
38    pub table_name: String,
39    pub columns_names: Vec<String>,
40}
41
42#[derive(Clone, PartialEq)]
43pub enum JoinKind {
44    Cross,
45    Inner,
46    Left,
47    Right,
48    Default,
49}
50
51#[derive(Clone)]
52pub enum JoinOperand {
53    /// Used when JOIN is used first time on query, X JOIN Y,
54    OuterAndInner(String, String),
55    /// Used for JOIN that used after first time, JOIN Z
56    Inner(String),
57}
58
59#[derive(Clone)]
60pub struct Join {
61    pub operand: JoinOperand,
62    pub kind: JoinKind,
63    pub predicate: Option<Box<dyn Expr>>,
64}
65
66#[derive(Clone)]
67pub struct SelectStatement {
68    pub table_selections: Vec<TableSelection>,
69    pub joins: Vec<Join>,
70    pub selected_expr_titles: Vec<String>,
71    pub selected_expr: Vec<Box<dyn Expr>>,
72    pub distinct: Distinct,
73}
74
75impl Statement for SelectStatement {
76    fn as_any(&self) -> &dyn Any {
77        self
78    }
79
80    fn kind(&self) -> StatementKind {
81        StatementKind::Select
82    }
83}
84
85#[derive(Clone)]
86pub struct WhereStatement {
87    pub condition: Box<dyn Expr>,
88}
89
90impl Statement for WhereStatement {
91    fn as_any(&self) -> &dyn Any {
92        self
93    }
94
95    fn kind(&self) -> StatementKind {
96        StatementKind::Where
97    }
98}
99
100#[derive(Clone)]
101pub struct HavingStatement {
102    pub condition: Box<dyn Expr>,
103}
104
105impl Statement for HavingStatement {
106    fn as_any(&self) -> &dyn Any {
107        self
108    }
109
110    fn kind(&self) -> StatementKind {
111        StatementKind::Having
112    }
113}
114
115#[derive(Clone)]
116pub struct LimitStatement {
117    pub count: usize,
118}
119
120impl Statement for LimitStatement {
121    fn as_any(&self) -> &dyn Any {
122        self
123    }
124
125    fn kind(&self) -> StatementKind {
126        StatementKind::Limit
127    }
128}
129
130#[derive(Clone)]
131pub struct OffsetStatement {
132    pub start: Box<dyn Expr>,
133}
134
135impl Statement for OffsetStatement {
136    fn as_any(&self) -> &dyn Any {
137        self
138    }
139
140    fn kind(&self) -> StatementKind {
141        StatementKind::Offset
142    }
143}
144
145#[derive(Clone, PartialEq)]
146pub enum SortingOrder {
147    Ascending,
148    Descending,
149}
150
151#[derive(Clone, PartialEq)]
152pub enum NullsOrderPolicy {
153    NullsFirst,
154    NullsLast,
155}
156
157#[derive(Clone)]
158pub struct OrderByStatement {
159    pub arguments: Vec<Box<dyn Expr>>,
160    pub sorting_orders: Vec<SortingOrder>,
161    pub nulls_order_policies: Vec<NullsOrderPolicy>,
162}
163
164impl Statement for OrderByStatement {
165    fn as_any(&self) -> &dyn Any {
166        self
167    }
168
169    fn kind(&self) -> StatementKind {
170        StatementKind::OrderBy
171    }
172}
173
174#[derive(Clone)]
175pub struct GroupByStatement {
176    pub values: Vec<Box<dyn Expr>>,
177    pub has_with_roll_up: bool,
178}
179
180impl Statement for GroupByStatement {
181    fn as_any(&self) -> &dyn Any {
182        self
183    }
184
185    fn kind(&self) -> StatementKind {
186        StatementKind::GroupBy
187    }
188}
189
190#[derive(Clone)]
191pub struct WindowPartitioningClause {
192    pub expr: Box<dyn Expr>,
193}
194
195#[derive(Clone)]
196pub struct WindowOrderingClause {
197    pub order_by: OrderByStatement,
198}
199
200#[derive(Clone)]
201pub struct WindowDefinition {
202    pub name: Option<String>,
203    pub partitioning_clause: Option<WindowPartitioningClause>,
204    pub ordering_clause: Option<WindowOrderingClause>,
205}
206
207#[derive(Clone)]
208pub enum WindowFunctionKind {
209    AggregatedWindowFunction,
210    PureWindowFunction,
211}
212
213#[derive(Clone)]
214pub struct WindowFunction {
215    pub function_name: String,
216    pub arguments: Vec<Box<dyn Expr>>,
217    pub window_definition: WindowDefinition,
218    pub kind: WindowFunctionKind,
219}
220
221#[derive(Clone)]
222pub enum WindowValue {
223    Function(WindowFunction),
224    Expression(Box<dyn Expr>),
225}
226
227#[derive(Clone)]
228pub struct WindowFunctionsStatement {
229    pub window_values: HashMap<String, WindowValue>,
230}
231
232impl Statement for WindowFunctionsStatement {
233    fn as_any(&self) -> &dyn Any {
234        self
235    }
236
237    fn kind(&self) -> StatementKind {
238        StatementKind::WindowFunction
239    }
240}
241
242#[derive(Clone)]
243pub struct QualifyStatement {
244    pub condition: Box<dyn Expr>,
245}
246
247impl Statement for QualifyStatement {
248    fn as_any(&self) -> &dyn Any {
249        self
250    }
251
252    fn kind(&self) -> StatementKind {
253        StatementKind::Where
254    }
255}
256
257#[derive(Clone)]
258pub enum AggregateValue {
259    Expression(Box<dyn Expr>),
260    Function(String, Vec<Box<dyn Expr>>),
261}
262
263#[derive(Clone)]
264pub struct AggregationsStatement {
265    pub aggregations: HashMap<String, AggregateValue>,
266}
267
268impl Statement for AggregationsStatement {
269    fn as_any(&self) -> &dyn Any {
270        self
271    }
272
273    fn kind(&self) -> StatementKind {
274        StatementKind::AggregateFunction
275    }
276}
277
278#[derive(Clone)]
279pub struct IntoStatement {
280    pub file_path: String,
281    pub lines_terminated: String,
282    pub fields_terminated: String,
283    pub enclosed: String,
284}
285
286impl Statement for IntoStatement {
287    fn as_any(&self) -> &dyn Any {
288        self
289    }
290
291    fn kind(&self) -> StatementKind {
292        StatementKind::Into
293    }
294}