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 assignments: Vec<Assignment>,
154}
155
156#[derive(Debug, Clone, PartialEq)]
157pub struct UpdateExpr {
158 pub source: String,
159 pub filter: Option<Expr>,
160 pub assignments: Vec<Assignment>,
161}
162
163#[derive(Debug, Clone, PartialEq)]
164pub struct DeleteExpr {
165 pub source: String,
166 pub filter: Option<Expr>,
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub struct Assignment {
171 pub field: String,
172 pub value: Expr,
173}
174
175#[derive(Debug, Clone, PartialEq)]
176pub struct UpsertExpr {
178 pub target: String,
179 pub key_column: String,
180 pub assignments: Vec<Assignment>,
181 pub on_conflict: Vec<Assignment>,
184}
185
186#[derive(Debug, Clone, PartialEq)]
187pub struct CreateTypeExpr {
188 pub name: String,
189 pub fields: Vec<FieldDef>,
190}
191
192#[derive(Debug, Clone, PartialEq)]
193pub struct FieldDef {
194 pub name: String,
195 pub type_name: String,
196 pub required: bool,
197}
198
199#[derive(Debug, Clone, PartialEq)]
200pub struct AggregateExpr {
201 pub function: AggFunc,
202 pub field: Option<String>,
203}
204
205#[derive(Debug, Clone, Copy, PartialEq)]
206pub enum AggFunc {
207 Count,
208 CountDistinct,
209 Avg,
210 Sum,
211 Min,
212 Max,
213}
214
215#[derive(Debug, Clone, Copy, PartialEq)]
217pub enum WindowFunc {
218 RowNumber,
219 Rank,
220 DenseRank,
221 Sum,
222 Avg,
223 Count,
224 Min,
225 Max,
226}
227
228#[derive(Debug, Clone, Copy, PartialEq)]
230pub enum ScalarFn {
231 Upper,
232 Lower,
233 Length,
234 Trim,
235 Substring, Concat, Abs,
239 Round, Ceil,
241 Floor,
242 Sqrt,
243 Pow, Now, Extract, DateAdd, DateDiff, }
250
251#[derive(Debug, Clone, Copy, PartialEq)]
253pub enum CastType {
254 Int,
255 Float,
256 Str,
257 Bool,
258 DateTime,
259}
260
261#[derive(Debug, Clone, PartialEq)]
263pub enum Expr {
264 Field(String),
265 QualifiedField {
270 qualifier: String,
271 field: String,
272 },
273 Literal(Literal),
274 Param(String),
275 BinaryOp(Box<Expr>, BinOp, Box<Expr>),
276 UnaryOp(UnaryOp, Box<Expr>),
277 FunctionCall(AggFunc, Box<Expr>),
278 ScalarFunc(ScalarFn, Vec<Expr>),
280 Coalesce(Box<Expr>, Box<Expr>),
281 InList {
283 expr: Box<Expr>,
284 list: Vec<Expr>,
285 negated: bool,
286 },
287 InSubquery {
290 expr: Box<Expr>,
291 subquery: Box<QueryExpr>,
292 negated: bool,
293 },
294 ExistsSubquery {
298 subquery: Box<QueryExpr>,
299 negated: bool,
300 },
301 Case {
303 whens: Vec<(Box<Expr>, Box<Expr>)>,
304 else_expr: Option<Box<Expr>>,
305 },
306 Window {
308 function: WindowFunc,
309 args: Vec<Expr>,
310 partition_by: Vec<String>,
311 order_by: Vec<OrderKey>,
312 },
313 Cast(Box<Expr>, CastType),
315 Null,
317}
318
319#[derive(Debug, Clone, PartialEq)]
320pub enum Literal {
321 Int(i64),
322 Float(f64),
323 String(String),
324 Bool(bool),
325}
326
327#[derive(Debug, Clone, Copy, PartialEq)]
328pub enum BinOp {
329 Eq,
330 Neq,
331 Lt,
332 Gt,
333 Lte,
334 Gte,
335 And,
336 Or,
337 Add,
338 Sub,
339 Mul,
340 Div,
341 Like,
342}
343
344#[derive(Debug, Clone, Copy, PartialEq)]
345pub enum UnaryOp {
346 Not,
347 Exists,
348 NotExists,
349 IsNull,
350 IsNotNull,
351}