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