spacetimedb_sql_parser_2/ast/
mod.rs1use std::fmt::{Display, Formatter};
2
3use sqlparser::ast::Ident;
4
5pub mod sql;
6pub mod sub;
7
8#[derive(Debug)]
10pub enum SqlFrom<Ast> {
11 Expr(RelExpr<Ast>, Option<SqlIdent>),
12 Join(RelExpr<Ast>, SqlIdent, Vec<SqlJoin<Ast>>),
13}
14
15#[derive(Debug)]
17pub enum RelExpr<Ast> {
18 Var(SqlIdent),
19 Ast(Box<Ast>),
20}
21
22#[derive(Debug)]
24pub struct SqlJoin<Ast> {
25 pub expr: RelExpr<Ast>,
26 pub alias: SqlIdent,
27 pub on: Option<SqlExpr>,
28}
29
30#[derive(Debug)]
32pub struct ProjectElem(pub ProjectExpr, pub Option<SqlIdent>);
33
34#[derive(Debug)]
36pub enum ProjectExpr {
37 Var(SqlIdent),
38 Field(SqlIdent, SqlIdent),
39}
40
41#[derive(Debug)]
43pub enum Project {
44 Star(Option<SqlIdent>),
47 Exprs(Vec<ProjectElem>),
49}
50
51#[derive(Debug)]
53pub enum SqlExpr {
54 Lit(SqlLiteral),
56 Var(SqlIdent),
58 Field(SqlIdent, SqlIdent),
60 Bin(Box<SqlExpr>, Box<SqlExpr>, BinOp),
62}
63
64#[derive(Debug, Clone)]
67pub struct SqlIdent(pub Box<str>);
68
69impl From<Ident> for SqlIdent {
71 fn from(Ident { value, .. }: Ident) -> Self {
72 SqlIdent(value.into_boxed_str())
73 }
74}
75
76#[derive(Debug)]
78pub enum SqlLiteral {
79 Bool(bool),
81 Hex(Box<str>),
83 Num(Box<str>),
85 Str(Box<str>),
87}
88
89#[derive(Debug, Clone, Copy)]
91pub enum BinOp {
92 Eq,
93 Ne,
94 Lt,
95 Gt,
96 Lte,
97 Gte,
98 And,
99 Or,
100}
101
102impl Display for BinOp {
103 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
104 match self {
105 Self::Eq => write!(f, "="),
106 Self::Ne => write!(f, "<>"),
107 Self::Lt => write!(f, "<"),
108 Self::Gt => write!(f, ">"),
109 Self::Lte => write!(f, "<="),
110 Self::Gte => write!(f, ">="),
111 Self::And => write!(f, "AND"),
112 Self::Or => write!(f, "OR"),
113 }
114 }
115}