spacetimedb_sql_parser_2/ast/
mod.rs

1use std::fmt::{Display, Formatter};
2
3use sqlparser::ast::Ident;
4
5pub mod sql;
6pub mod sub;
7
8/// The FROM clause is either a [RelExpr] or a JOIN
9#[derive(Debug)]
10pub enum SqlFrom<Ast> {
11    Expr(RelExpr<Ast>, Option<SqlIdent>),
12    Join(RelExpr<Ast>, SqlIdent, Vec<SqlJoin<Ast>>),
13}
14
15/// A RelExpr is an expression that produces a relation
16#[derive(Debug)]
17pub enum RelExpr<Ast> {
18    Var(SqlIdent),
19    Ast(Box<Ast>),
20}
21
22/// An inner join in a FROM clause
23#[derive(Debug)]
24pub struct SqlJoin<Ast> {
25    pub expr: RelExpr<Ast>,
26    pub alias: SqlIdent,
27    pub on: Option<SqlExpr>,
28}
29
30/// A projection expression in a SELECT clause
31#[derive(Debug)]
32pub struct ProjectElem(pub ProjectExpr, pub Option<SqlIdent>);
33
34/// A column projection in a SELECT clause
35#[derive(Debug)]
36pub enum ProjectExpr {
37    Var(SqlIdent),
38    Field(SqlIdent, SqlIdent),
39}
40
41/// A SQL SELECT clause
42#[derive(Debug)]
43pub enum Project {
44    /// SELECT *
45    /// SELECT a.*
46    Star(Option<SqlIdent>),
47    /// SELECT a, b
48    Exprs(Vec<ProjectElem>),
49}
50
51/// A scalar SQL expression
52#[derive(Debug)]
53pub enum SqlExpr {
54    /// A constant expression
55    Lit(SqlLiteral),
56    /// Unqualified column ref
57    Var(SqlIdent),
58    /// Qualified column ref
59    Field(SqlIdent, SqlIdent),
60    /// A binary infix expression
61    Bin(Box<SqlExpr>, Box<SqlExpr>, BinOp),
62}
63
64/// A SQL identifier or named reference.
65/// Currently case sensitive.
66#[derive(Debug, Clone)]
67pub struct SqlIdent(pub Box<str>);
68
69/// Case insensitivity should be implemented here if at all
70impl From<Ident> for SqlIdent {
71    fn from(Ident { value, .. }: Ident) -> Self {
72        SqlIdent(value.into_boxed_str())
73    }
74}
75
76/// A SQL constant expression
77#[derive(Debug)]
78pub enum SqlLiteral {
79    /// A boolean constant
80    Bool(bool),
81    /// A hex value like 0xFF or x'FF'
82    Hex(Box<str>),
83    /// An integer or float value
84    Num(Box<str>),
85    /// A string value
86    Str(Box<str>),
87}
88
89/// Binary infix operators
90#[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}