1#[derive(Debug, Clone, PartialEq)]
3pub enum Statement {
4 Query(QueryExpr),
5 Insert(InsertExpr),
6 UpdateQuery(UpdateExpr),
7 DeleteQuery(DeleteExpr),
8 CreateType(CreateTypeExpr),
9 AlterTable(AlterTableExpr),
10 DropTable(DropTableExpr),
11 CreateView(CreateViewExpr),
12 RefreshView(RefreshViewExpr),
13 DropView(DropViewExpr),
14 Union(UnionExpr),
15 Upsert(UpsertExpr),
16 Explain(Box<Statement>),
17 Begin,
18 Commit,
19 Rollback,
20}
21
22#[derive(Debug, Clone, PartialEq)]
24pub struct AlterTableExpr {
25 pub table: String,
26 pub action: AlterAction,
27}
28
29#[derive(Debug, Clone, PartialEq)]
31pub enum AlterAction {
32 AddColumn {
33 name: String,
34 type_name: String,
35 required: bool,
36 },
37 DropColumn {
38 name: String,
39 },
40 AddIndex {
43 column: String,
44 },
45}
46
47#[derive(Debug, Clone, PartialEq)]
49pub struct DropTableExpr {
50 pub table: String,
51}
52
53#[derive(Debug, Clone, PartialEq)]
55pub struct CreateViewExpr {
56 pub name: String,
57 pub query: QueryExpr,
58 pub query_text: String,
60}
61
62#[derive(Debug, Clone, PartialEq)]
64pub struct RefreshViewExpr {
65 pub name: String,
66}
67
68#[derive(Debug, Clone, PartialEq)]
70pub struct DropViewExpr {
71 pub name: String,
72}
73
74#[derive(Debug, Clone, PartialEq)]
76pub struct UnionExpr {
77 pub left: Box<Statement>,
78 pub right: Box<Statement>,
79 pub all: bool,
81}
82
83#[derive(Debug, Clone, PartialEq)]
85pub struct QueryExpr {
86 pub source: String,
87 pub alias: Option<String>,
91 pub joins: Vec<JoinClause>,
95 pub filter: Option<Expr>,
96 pub order: Option<OrderClause>,
97 pub limit: Option<Expr>,
98 pub offset: Option<Expr>,
99 pub projection: Option<Vec<ProjectionField>>,
100 pub aggregation: Option<AggregateExpr>,
101 pub distinct: bool,
102 pub group_by: Option<GroupByClause>,
103}
104
105#[derive(Debug, Clone, PartialEq)]
107pub struct GroupByClause {
108 pub keys: Vec<String>,
109 pub having: Option<Expr>,
110}
111
112#[derive(Debug, Clone, PartialEq)]
117pub struct JoinClause {
118 pub kind: JoinKind,
119 pub source: String,
120 pub alias: Option<String>,
121 pub on: Option<Expr>,
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub enum JoinKind {
127 Inner,
128 LeftOuter,
129 RightOuter,
130 Cross,
131}
132
133#[derive(Debug, Clone, PartialEq)]
134pub struct ProjectionField {
135 pub alias: Option<String>,
136 pub expr: Expr,
137}
138
139#[derive(Debug, Clone, PartialEq)]
140pub struct OrderClause {
141 pub keys: Vec<OrderKey>,
142}
143
144#[derive(Debug, Clone, PartialEq)]
145pub struct OrderKey {
146 pub field: String,
147 pub descending: bool,
148}
149
150#[derive(Debug, Clone, PartialEq)]
151pub struct InsertExpr {
152 pub target: String,
153 pub rows: Vec<Vec<Assignment>>,
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub struct UpdateExpr {
160 pub source: String,
161 pub filter: Option<Expr>,
162 pub assignments: Vec<Assignment>,
163}
164
165#[derive(Debug, Clone, PartialEq)]
166pub struct DeleteExpr {
167 pub source: String,
168 pub filter: Option<Expr>,
169}
170
171#[derive(Debug, Clone, PartialEq)]
172pub struct Assignment {
173 pub field: String,
174 pub value: Expr,
175}
176
177#[derive(Debug, Clone, PartialEq)]
178pub struct UpsertExpr {
180 pub target: String,
181 pub key_column: String,
182 pub assignments: Vec<Assignment>,
183 pub on_conflict: Vec<Assignment>,
186}
187
188#[derive(Debug, Clone, PartialEq)]
189pub struct CreateTypeExpr {
190 pub name: String,
191 pub fields: Vec<FieldDef>,
192}
193
194#[derive(Debug, Clone, PartialEq)]
195pub struct FieldDef {
196 pub name: String,
197 pub type_name: String,
198 pub required: bool,
199}
200
201#[derive(Debug, Clone, PartialEq)]
202pub struct AggregateExpr {
203 pub function: AggFunc,
204 pub field: Option<String>,
205}
206
207#[derive(Debug, Clone, Copy, PartialEq)]
208pub enum AggFunc {
209 Count,
210 CountDistinct,
211 Avg,
212 Sum,
213 Min,
214 Max,
215}
216
217#[derive(Debug, Clone, Copy, PartialEq)]
219pub enum WindowFunc {
220 RowNumber,
221 Rank,
222 DenseRank,
223 Sum,
224 Avg,
225 Count,
226 Min,
227 Max,
228}
229
230#[derive(Debug, Clone, Copy, PartialEq)]
232pub enum ScalarFn {
233 Upper,
234 Lower,
235 Length,
236 Trim,
237 Substring, Concat, Abs,
241 Round, Ceil,
243 Floor,
244 Sqrt,
245 Pow, Now, Extract, DateAdd, DateDiff, }
252
253#[derive(Debug, Clone, Copy, PartialEq)]
255pub enum CastType {
256 Int,
257 Float,
258 Str,
259 Bool,
260 DateTime,
261}
262
263#[derive(Debug, Clone, PartialEq)]
265pub enum Expr {
266 Field(String),
267 QualifiedField {
272 qualifier: String,
273 field: String,
274 },
275 Literal(Literal),
276 Param(String),
277 BinaryOp(Box<Expr>, BinOp, Box<Expr>),
278 UnaryOp(UnaryOp, Box<Expr>),
279 FunctionCall(AggFunc, Box<Expr>),
280 ScalarFunc(ScalarFn, Vec<Expr>),
282 Coalesce(Box<Expr>, Box<Expr>),
283 InList {
285 expr: Box<Expr>,
286 list: Vec<Expr>,
287 negated: bool,
288 },
289 InSubquery {
292 expr: Box<Expr>,
293 subquery: Box<QueryExpr>,
294 negated: bool,
295 },
296 ExistsSubquery {
300 subquery: Box<QueryExpr>,
301 negated: bool,
302 },
303 Case {
305 whens: Vec<(Box<Expr>, Box<Expr>)>,
306 else_expr: Option<Box<Expr>>,
307 },
308 Window {
310 function: WindowFunc,
311 args: Vec<Expr>,
312 partition_by: Vec<String>,
313 order_by: Vec<OrderKey>,
314 },
315 Cast(Box<Expr>, CastType),
317 Null,
319}
320
321#[derive(Debug, Clone, PartialEq)]
322pub enum Literal {
323 Int(i64),
324 Float(f64),
325 String(String),
326 Bool(bool),
327}
328
329#[derive(Debug, Clone, Copy, PartialEq)]
330pub enum BinOp {
331 Eq,
332 Neq,
333 Lt,
334 Gt,
335 Lte,
336 Gte,
337 And,
338 Or,
339 Add,
340 Sub,
341 Mul,
342 Div,
343 Like,
344}
345
346#[derive(Debug, Clone, Copy, PartialEq)]
347pub enum UnaryOp {
348 Not,
349 Exists,
350 NotExists,
351 IsNull,
352 IsNotNull,
353}