spacetimedb_sql_parser_2/ast/
sql.rs

1use super::{Project, SqlExpr, SqlFrom, SqlIdent, SqlLiteral};
2
3/// The AST for the SQL DML and query language
4pub enum SqlAst {
5    /// SELECT ...
6    Query(QueryAst),
7    /// INSERT INTO ...
8    Insert(SqlInsert),
9    /// UPDATE ...
10    Update(SqlUpdate),
11    /// DELETE FROM ...
12    Delete(SqlDelete),
13    /// SET var TO ...
14    Set(SqlSet),
15    /// SHOW var
16    Show(SqlShow),
17}
18
19/// The AST for the SQL query language
20pub struct QueryAst {
21    pub query: SqlSetOp,
22    pub order: Vec<OrderByElem>,
23    pub limit: Option<SqlLiteral>,
24}
25
26/// Set operations in the SQL query language
27pub enum SqlSetOp {
28    /// SELECT
29    Select(SqlSelect),
30    /// ORDER/LIMIT
31    Query(Box<QueryAst>),
32    /// UNION
33    Union(Box<SqlSetOp>, Box<SqlSetOp>, bool),
34    /// EXCEPT
35    Minus(Box<SqlSetOp>, Box<SqlSetOp>, bool),
36}
37
38/// A SELECT statement in the SQL query language
39pub struct SqlSelect {
40    pub project: Project,
41    pub distinct: bool,
42    pub from: SqlFrom<QueryAst>,
43    pub filter: Option<SqlExpr>,
44}
45
46/// ORDER BY cols [ ASC | DESC ]
47pub struct OrderByElem(pub SqlExpr, pub bool);
48
49/// INSERT INTO table cols VALUES literals
50pub struct SqlInsert {
51    pub table: SqlIdent,
52    pub fields: Vec<SqlIdent>,
53    pub values: SqlValues,
54}
55
56/// VALUES literals
57pub struct SqlValues(pub Vec<Vec<SqlLiteral>>);
58
59/// UPDATE table SET cols [ WHERE predicate ]
60pub struct SqlUpdate {
61    pub table: SqlIdent,
62    pub assignments: Vec<SqlSet>,
63    pub filter: Option<SqlExpr>,
64}
65
66/// DELETE FROM table [ WHERE predicate ]
67pub struct SqlDelete {
68    pub table: SqlIdent,
69    pub filter: Option<SqlExpr>,
70}
71
72/// SET var '=' literal
73pub struct SqlSet(pub SqlIdent, pub SqlLiteral);
74
75/// SHOW var
76pub struct SqlShow(pub SqlIdent);