1use powdb_storage::types::Value;
2
3#[derive(Debug, Clone, PartialEq)]
5pub enum Statement {
6 Query(QueryExpr),
7 Insert(InsertExpr),
8 UpdateQuery(UpdateExpr),
9 DeleteQuery(DeleteExpr),
10 CreateType(CreateTypeExpr),
11 AlterTable(AlterTableExpr),
12 DropTable(DropTableExpr),
13 CreateView(CreateViewExpr),
14 RefreshView(RefreshViewExpr),
15 DropView(DropViewExpr),
16 Union(UnionExpr),
17 Upsert(UpsertExpr),
18 Explain(Box<Statement>),
19 Begin,
20 Commit,
21 Rollback,
22}
23
24#[derive(Debug, Clone, PartialEq)]
26pub struct AlterTableExpr {
27 pub table: String,
28 pub action: AlterAction,
29}
30
31#[derive(Debug, Clone, PartialEq)]
33pub enum AlterAction {
34 AddColumn {
35 name: String,
36 type_name: String,
37 required: bool,
38 },
39 DropColumn {
40 name: String,
41 },
42 AddIndex {
45 column: String,
46 },
47 AddUnique {
52 column: String,
53 },
54}
55
56#[derive(Debug, Clone, PartialEq)]
58pub struct DropTableExpr {
59 pub table: String,
60}
61
62#[derive(Debug, Clone, PartialEq)]
64pub struct CreateViewExpr {
65 pub name: String,
66 pub query: QueryExpr,
67 pub query_text: String,
69}
70
71#[derive(Debug, Clone, PartialEq)]
73pub struct RefreshViewExpr {
74 pub name: String,
75}
76
77#[derive(Debug, Clone, PartialEq)]
79pub struct DropViewExpr {
80 pub name: String,
81}
82
83#[derive(Debug, Clone, PartialEq)]
85pub struct UnionExpr {
86 pub left: Box<Statement>,
87 pub right: Box<Statement>,
88 pub all: bool,
90}
91
92#[derive(Debug, Clone, PartialEq)]
94pub struct QueryExpr {
95 pub source: String,
96 pub alias: Option<String>,
100 pub joins: Vec<JoinClause>,
104 pub filter: Option<Expr>,
105 pub order: Option<OrderClause>,
106 pub limit: Option<Expr>,
107 pub offset: Option<Expr>,
108 pub projection: Option<Vec<ProjectionField>>,
109 pub aggregation: Option<AggregateExpr>,
110 pub distinct: bool,
111 pub group_by: Option<GroupByClause>,
112}
113
114#[derive(Debug, Clone, PartialEq)]
116pub struct GroupByClause {
117 pub keys: Vec<String>,
118 pub having: Option<Expr>,
119}
120
121#[derive(Debug, Clone, PartialEq)]
126pub struct JoinClause {
127 pub kind: JoinKind,
128 pub source: String,
129 pub alias: Option<String>,
130 pub on: Option<Expr>,
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135pub enum JoinKind {
136 Inner,
137 LeftOuter,
138 RightOuter,
139 Cross,
140}
141
142#[derive(Debug, Clone, PartialEq)]
143pub struct ProjectionField {
144 pub alias: Option<String>,
145 pub expr: Expr,
146}
147
148#[derive(Debug, Clone, PartialEq)]
149pub struct OrderClause {
150 pub keys: Vec<OrderKey>,
151}
152
153#[derive(Debug, Clone, PartialEq)]
154pub struct OrderKey {
155 pub field: String,
156 pub descending: bool,
157}
158
159#[derive(Debug, Clone, PartialEq)]
160pub struct InsertExpr {
161 pub target: String,
162 pub rows: Vec<Vec<Assignment>>,
165 pub returning: bool,
168}
169
170#[derive(Debug, Clone, PartialEq)]
171pub struct UpdateExpr {
172 pub source: String,
173 pub filter: Option<Expr>,
174 pub assignments: Vec<Assignment>,
175 pub returning: bool,
178}
179
180#[derive(Debug, Clone, PartialEq)]
181pub struct DeleteExpr {
182 pub source: String,
183 pub filter: Option<Expr>,
184 pub returning: bool,
187}
188
189#[derive(Debug, Clone, PartialEq)]
190pub struct Assignment {
191 pub field: String,
192 pub value: Expr,
193}
194
195#[derive(Debug, Clone, PartialEq)]
196pub struct UpsertExpr {
198 pub target: String,
199 pub key_column: String,
200 pub assignments: Vec<Assignment>,
201 pub on_conflict: Vec<Assignment>,
204}
205
206#[derive(Debug, Clone, PartialEq)]
207pub struct CreateTypeExpr {
208 pub name: String,
209 pub fields: Vec<FieldDef>,
210}
211
212#[derive(Debug, Clone, PartialEq)]
213pub struct FieldDef {
214 pub name: String,
215 pub type_name: String,
216 pub required: bool,
217 pub unique: bool,
220 pub default: Option<Literal>,
223 pub auto: bool,
226}
227
228#[derive(Debug, Clone, PartialEq)]
229pub struct AggregateExpr {
230 pub function: AggFunc,
231 pub field: Option<String>,
232}
233
234#[derive(Debug, Clone, Copy, PartialEq)]
235pub enum AggFunc {
236 Count,
237 CountDistinct,
238 Avg,
239 Sum,
240 Min,
241 Max,
242}
243
244#[derive(Debug, Clone, Copy, PartialEq)]
246pub enum WindowFunc {
247 RowNumber,
248 Rank,
249 DenseRank,
250 Sum,
251 Avg,
252 Count,
253 Min,
254 Max,
255}
256
257#[derive(Debug, Clone, Copy, PartialEq)]
259pub enum ScalarFn {
260 Upper,
261 Lower,
262 Length,
263 Trim,
264 Substring, Concat, Abs,
268 Round, Ceil,
270 Floor,
271 Sqrt,
272 Pow, Now, Extract, DateAdd, DateDiff, }
279
280#[derive(Debug, Clone, Copy, PartialEq)]
282pub enum CastType {
283 Int,
284 Float,
285 Str,
286 Bool,
287 DateTime,
288 Uuid,
289 Bytes,
290}
291
292#[derive(Debug, Clone, PartialEq)]
294pub enum Expr {
295 Field(String),
296 QualifiedField {
301 qualifier: String,
302 field: String,
303 },
304 Literal(Literal),
305 Param(String),
306 BinaryOp(Box<Expr>, BinOp, Box<Expr>),
307 UnaryOp(UnaryOp, Box<Expr>),
308 FunctionCall(AggFunc, Box<Expr>),
309 ScalarFunc(ScalarFn, Vec<Expr>),
311 Coalesce(Box<Expr>, Box<Expr>),
312 InList {
314 expr: Box<Expr>,
315 list: Vec<Expr>,
316 negated: bool,
317 },
318 InSubquery {
321 expr: Box<Expr>,
322 subquery: Box<QueryExpr>,
323 negated: bool,
324 },
325 ExistsSubquery {
329 subquery: Box<QueryExpr>,
330 negated: bool,
331 },
332 Case {
334 whens: Vec<(Box<Expr>, Box<Expr>)>,
335 else_expr: Option<Box<Expr>>,
336 },
337 Window {
339 function: WindowFunc,
340 args: Vec<Expr>,
341 partition_by: Vec<String>,
342 order_by: Vec<OrderKey>,
343 },
344 Cast(Box<Expr>, CastType),
346 ValueLit(Value),
351 Null,
353}
354
355#[derive(Debug, Clone, PartialEq)]
356pub enum Literal {
357 Int(i64),
358 Float(f64),
359 String(String),
360 Bool(bool),
361}
362
363#[derive(Debug, Clone, PartialEq)]
371pub enum ParamValue {
372 Null,
373 Int(i64),
374 Float(f64),
375 Bool(bool),
376 Str(String),
377}
378
379#[derive(Debug, Clone, Copy, PartialEq)]
380pub enum BinOp {
381 Eq,
382 Neq,
383 Lt,
384 Gt,
385 Lte,
386 Gte,
387 And,
388 Or,
389 Add,
390 Sub,
391 Mul,
392 Div,
393 Like,
394}
395
396#[derive(Debug, Clone, Copy, PartialEq)]
397pub enum UnaryOp {
398 Not,
399 Exists,
400 NotExists,
401 IsNull,
402 IsNotNull,
403}