1use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub enum Statement {
9 Select(SelectStatement),
11}
12
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15pub struct SelectStatement {
16 pub projection: Vec<SelectItem>,
18 pub from: Option<TableReference>,
20 pub selection: Option<Expr>,
22 pub group_by: Vec<Expr>,
24 pub having: Option<Expr>,
26 pub order_by: Vec<OrderByExpr>,
28 pub limit: Option<usize>,
30 pub offset: Option<usize>,
32}
33
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub enum SelectItem {
37 Wildcard,
39 QualifiedWildcard(String),
41 Expr {
43 expr: Expr,
45 alias: Option<String>,
47 },
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub enum TableReference {
53 Table {
55 name: String,
57 alias: Option<String>,
59 },
60 Join {
62 left: Box<TableReference>,
64 right: Box<TableReference>,
66 join_type: JoinType,
68 on: Option<Expr>,
70 },
71 Subquery {
73 query: Box<SelectStatement>,
75 alias: String,
77 },
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82pub enum JoinType {
83 Inner,
85 Left,
87 Right,
89 Full,
91 Cross,
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
97pub enum Expr {
98 Column {
100 table: Option<String>,
102 name: String,
104 },
105 Literal(Literal),
107 BinaryOp {
109 left: Box<Expr>,
111 op: BinaryOperator,
113 right: Box<Expr>,
115 },
116 UnaryOp {
118 op: UnaryOperator,
120 expr: Box<Expr>,
122 },
123 Function {
125 name: String,
127 args: Vec<Expr>,
129 },
130 Case {
132 operand: Option<Box<Expr>>,
134 when_then: Vec<(Expr, Expr)>,
136 else_result: Option<Box<Expr>>,
138 },
139 Cast {
141 expr: Box<Expr>,
143 data_type: DataType,
145 },
146 IsNull(Box<Expr>),
148 IsNotNull(Box<Expr>),
150 InList {
152 expr: Box<Expr>,
154 list: Vec<Expr>,
156 negated: bool,
158 },
159 Between {
161 expr: Box<Expr>,
163 low: Box<Expr>,
165 high: Box<Expr>,
167 negated: bool,
169 },
170 Subquery(Box<SelectStatement>),
172 Wildcard,
174}
175
176#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
178pub enum Literal {
179 Null,
181 Boolean(bool),
183 Integer(i64),
185 Float(f64),
187 String(String),
189}
190
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
193pub enum BinaryOperator {
194 Plus,
196 Minus,
198 Multiply,
200 Divide,
202 Modulo,
204 Eq,
206 NotEq,
208 Lt,
210 LtEq,
212 Gt,
214 GtEq,
216 And,
218 Or,
220 Concat,
222 Like,
224 NotLike,
226}
227
228#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
230pub enum UnaryOperator {
231 Minus,
233 Not,
235}
236
237#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
239pub struct OrderByExpr {
240 pub expr: Expr,
242 pub asc: bool,
244 pub nulls_first: bool,
246}
247
248#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
250pub enum DataType {
251 Boolean,
253 Int8,
255 Int16,
257 Int32,
259 Int64,
261 UInt8,
263 UInt16,
265 UInt32,
267 UInt64,
269 Float32,
271 Float64,
273 String,
275 Binary,
277 Timestamp,
279 Date,
281 Geometry,
283}
284
285impl fmt::Display for Statement {
286 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287 match self {
288 Statement::Select(select) => write!(f, "{}", select),
289 }
290 }
291}
292
293impl fmt::Display for SelectStatement {
294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295 write!(f, "SELECT ")?;
296 for (i, item) in self.projection.iter().enumerate() {
297 if i > 0 {
298 write!(f, ", ")?;
299 }
300 write!(f, "{}", item)?;
301 }
302 if let Some(from) = &self.from {
303 write!(f, " FROM {}", from)?;
304 }
305 if let Some(selection) = &self.selection {
306 write!(f, " WHERE {}", selection)?;
307 }
308 if !self.group_by.is_empty() {
309 write!(f, " GROUP BY ")?;
310 for (i, expr) in self.group_by.iter().enumerate() {
311 if i > 0 {
312 write!(f, ", ")?;
313 }
314 write!(f, "{}", expr)?;
315 }
316 }
317 if let Some(having) = &self.having {
318 write!(f, " HAVING {}", having)?;
319 }
320 if !self.order_by.is_empty() {
321 write!(f, " ORDER BY ")?;
322 for (i, expr) in self.order_by.iter().enumerate() {
323 if i > 0 {
324 write!(f, ", ")?;
325 }
326 write!(f, "{}", expr)?;
327 }
328 }
329 if let Some(limit) = self.limit {
330 write!(f, " LIMIT {}", limit)?;
331 }
332 if let Some(offset) = self.offset {
333 write!(f, " OFFSET {}", offset)?;
334 }
335 Ok(())
336 }
337}
338
339impl fmt::Display for SelectItem {
340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341 match self {
342 SelectItem::Wildcard => write!(f, "*"),
343 SelectItem::QualifiedWildcard(table) => write!(f, "{}.*", table),
344 SelectItem::Expr { expr, alias } => {
345 write!(f, "{}", expr)?;
346 if let Some(alias) = alias {
347 write!(f, " AS {}", alias)?;
348 }
349 Ok(())
350 }
351 }
352 }
353}
354
355impl fmt::Display for TableReference {
356 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
357 match self {
358 TableReference::Table { name, alias } => {
359 write!(f, "{}", name)?;
360 if let Some(alias) = alias {
361 write!(f, " AS {}", alias)?;
362 }
363 Ok(())
364 }
365 TableReference::Join {
366 left,
367 right,
368 join_type,
369 on,
370 } => {
371 write!(f, "{} {} JOIN {}", left, join_type, right)?;
372 if let Some(on) = on {
373 write!(f, " ON {}", on)?;
374 }
375 Ok(())
376 }
377 TableReference::Subquery { query, alias } => {
378 write!(f, "({}) AS {}", query, alias)
379 }
380 }
381 }
382}
383
384impl fmt::Display for JoinType {
385 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386 match self {
387 JoinType::Inner => write!(f, "INNER"),
388 JoinType::Left => write!(f, "LEFT"),
389 JoinType::Right => write!(f, "RIGHT"),
390 JoinType::Full => write!(f, "FULL"),
391 JoinType::Cross => write!(f, "CROSS"),
392 }
393 }
394}
395
396impl fmt::Display for Expr {
397 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
398 match self {
399 Expr::Column { table, name } => {
400 if let Some(table) = table {
401 write!(f, "{}.{}", table, name)
402 } else {
403 write!(f, "{}", name)
404 }
405 }
406 Expr::Literal(lit) => write!(f, "{}", lit),
407 Expr::BinaryOp { left, op, right } => {
408 write!(f, "({} {} {})", left, op, right)
409 }
410 Expr::UnaryOp { op, expr } => write!(f, "({} {})", op, expr),
411 Expr::Function { name, args } => {
412 write!(f, "{}(", name)?;
413 for (i, arg) in args.iter().enumerate() {
414 if i > 0 {
415 write!(f, ", ")?;
416 }
417 write!(f, "{}", arg)?;
418 }
419 write!(f, ")")
420 }
421 Expr::Case {
422 operand,
423 when_then,
424 else_result,
425 } => {
426 write!(f, "CASE")?;
427 if let Some(operand) = operand {
428 write!(f, " {}", operand)?;
429 }
430 for (when, then) in when_then {
431 write!(f, " WHEN {} THEN {}", when, then)?;
432 }
433 if let Some(else_result) = else_result {
434 write!(f, " ELSE {}", else_result)?;
435 }
436 write!(f, " END")
437 }
438 Expr::Cast { expr, data_type } => {
439 write!(f, "CAST({} AS {:?})", expr, data_type)
440 }
441 Expr::IsNull(expr) => write!(f, "{} IS NULL", expr),
442 Expr::IsNotNull(expr) => write!(f, "{} IS NOT NULL", expr),
443 Expr::InList {
444 expr,
445 list,
446 negated,
447 } => {
448 write!(f, "{}", expr)?;
449 if *negated {
450 write!(f, " NOT")?;
451 }
452 write!(f, " IN (")?;
453 for (i, item) in list.iter().enumerate() {
454 if i > 0 {
455 write!(f, ", ")?;
456 }
457 write!(f, "{}", item)?;
458 }
459 write!(f, ")")
460 }
461 Expr::Between {
462 expr,
463 low,
464 high,
465 negated,
466 } => {
467 write!(f, "{}", expr)?;
468 if *negated {
469 write!(f, " NOT")?;
470 }
471 write!(f, " BETWEEN {} AND {}", low, high)
472 }
473 Expr::Subquery(query) => write!(f, "({})", query),
474 Expr::Wildcard => write!(f, "*"),
475 }
476 }
477}
478
479impl fmt::Display for Literal {
480 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481 match self {
482 Literal::Null => write!(f, "NULL"),
483 Literal::Boolean(b) => write!(f, "{}", b),
484 Literal::Integer(i) => write!(f, "{}", i),
485 Literal::Float(fl) => write!(f, "{}", fl),
486 Literal::String(s) => write!(f, "'{}'", s),
487 }
488 }
489}
490
491impl fmt::Display for BinaryOperator {
492 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493 match self {
494 BinaryOperator::Plus => write!(f, "+"),
495 BinaryOperator::Minus => write!(f, "-"),
496 BinaryOperator::Multiply => write!(f, "*"),
497 BinaryOperator::Divide => write!(f, "/"),
498 BinaryOperator::Modulo => write!(f, "%"),
499 BinaryOperator::Eq => write!(f, "="),
500 BinaryOperator::NotEq => write!(f, "<>"),
501 BinaryOperator::Lt => write!(f, "<"),
502 BinaryOperator::LtEq => write!(f, "<="),
503 BinaryOperator::Gt => write!(f, ">"),
504 BinaryOperator::GtEq => write!(f, ">="),
505 BinaryOperator::And => write!(f, "AND"),
506 BinaryOperator::Or => write!(f, "OR"),
507 BinaryOperator::Concat => write!(f, "||"),
508 BinaryOperator::Like => write!(f, "LIKE"),
509 BinaryOperator::NotLike => write!(f, "NOT LIKE"),
510 }
511 }
512}
513
514impl fmt::Display for UnaryOperator {
515 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
516 match self {
517 UnaryOperator::Minus => write!(f, "-"),
518 UnaryOperator::Not => write!(f, "NOT"),
519 }
520 }
521}
522
523impl fmt::Display for OrderByExpr {
524 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
525 write!(f, "{}", self.expr)?;
526 if self.asc {
527 write!(f, " ASC")?;
528 } else {
529 write!(f, " DESC")?;
530 }
531 if self.nulls_first {
532 write!(f, " NULLS FIRST")?;
533 } else {
534 write!(f, " NULLS LAST")?;
535 }
536 Ok(())
537 }
538}