spacetimedb_sql_parser/ast/
sql.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use super::{Project, SqlExpr, SqlFrom, SqlIdent, SqlLiteral};

/// The AST for the SQL DML and query language
pub enum SqlAst {
    /// SELECT ...
    Query(QueryAst),
    /// INSERT INTO ...
    Insert(SqlInsert),
    /// UPDATE ...
    Update(SqlUpdate),
    /// DELETE FROM ...
    Delete(SqlDelete),
    /// SET var TO ...
    Set(SqlSet),
    /// SHOW var
    Show(SqlShow),
}

/// The AST for the SQL query language
pub struct QueryAst {
    pub query: SqlSetOp,
    pub order: Vec<OrderByElem>,
    pub limit: Option<SqlLiteral>,
}

/// Set operations in the SQL query language
pub enum SqlSetOp {
    /// SELECT
    Select(SqlSelect),
    /// ORDER/LIMIT
    Query(Box<QueryAst>),
    /// UNION
    Union(Box<SqlSetOp>, Box<SqlSetOp>, bool),
    /// EXCEPT
    Minus(Box<SqlSetOp>, Box<SqlSetOp>, bool),
}

/// A SELECT statement in the SQL query language
pub struct SqlSelect {
    pub project: Project,
    pub distinct: bool,
    pub from: SqlFrom<QueryAst>,
    pub filter: Option<SqlExpr>,
}

/// ORDER BY cols [ ASC | DESC ]
pub struct OrderByElem(pub SqlExpr, pub bool);

/// INSERT INTO table cols VALUES literals
pub struct SqlInsert {
    pub table: SqlIdent,
    pub fields: Vec<SqlIdent>,
    pub values: SqlValues,
}

/// VALUES literals
pub struct SqlValues(pub Vec<Vec<SqlLiteral>>);

/// UPDATE table SET cols [ WHERE predicate ]
pub struct SqlUpdate {
    pub table: SqlIdent,
    pub assignments: Vec<SqlSet>,
    pub filter: Option<SqlExpr>,
}

/// DELETE FROM table [ WHERE predicate ]
pub struct SqlDelete {
    pub table: SqlIdent,
    pub filter: Option<SqlExpr>,
}

/// SET var '=' literal
pub struct SqlSet(pub SqlIdent, pub SqlLiteral);

/// SHOW var
pub struct SqlShow(pub SqlIdent);