Skip to main content

basalt/sql/
ast.rs

1use crate::types::{ColumnType, Value};
2
3#[allow(clippy::large_enum_variant)]
4#[derive(Debug, Clone, PartialEq)]
5pub enum Statement {
6    CreateTable {
7        name: String,
8        if_not_exists: bool,
9        columns: Vec<ColumnDef>,
10    },
11    DropTable {
12        name: String,
13        if_exists: bool,
14    },
15    CreateIndex {
16        name: String,
17        table: String,
18        column: String,
19        unique: bool,
20        if_not_exists: bool,
21    },
22    DropIndex {
23        name: String,
24        if_exists: bool,
25    },
26    Insert {
27        table: String,
28        columns: Option<Vec<String>>,
29        rows: Vec<Vec<Expr>>,
30    },
31    InsertSelect {
32        table: String,
33        columns: Option<Vec<String>>,
34        query: Box<Statement>,
35    },
36    Select {
37        distinct: bool,
38        columns: SelectItems,
39        from: String,
40        from_alias: Option<String>,
41        joins: Vec<JoinClause>,
42        where_clause: Option<Expr>,
43        group_by: Vec<Expr>,
44        having: Option<Expr>,
45        order_by: Vec<(String, bool)>, // col, ascending
46        order_by_exprs: Vec<(Expr, bool)>,
47        limit: Option<u64>,
48        offset: Option<u64>,
49    },
50    Update {
51        table: String,
52        assignments: Vec<(String, Expr)>,
53        where_clause: Option<Expr>,
54    },
55    Delete {
56        table: String,
57        where_clause: Option<Expr>,
58    },
59    Begin,
60    Commit,
61    Rollback,
62    Checkpoint,
63    Explain(Box<Statement>),
64}
65
66#[derive(Debug, Clone, PartialEq)]
67pub struct JoinClause {
68    pub kind: JoinKind,
69    pub table: String,
70    pub alias: Option<String>,
71    pub on: Option<Expr>,
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum JoinKind {
76    Inner,
77    Left,
78    Right,
79    Full,
80    Cross,
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub enum SelectItems {
85    Star,
86    List(Vec<Expr>),
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub struct ColumnDef {
91    pub name: String,
92    pub ty: ColumnType,
93    pub primary_key: bool,
94    pub not_null: bool,
95    pub unique: bool,
96}
97
98#[derive(Debug, Clone, PartialEq)]
99pub enum Expr {
100    Literal(Value),
101    Column(String),
102    QualifiedWildcard(String),
103    ColumnRef {
104        relation: String,
105        column: String,
106    },
107    Alias {
108        expr: Box<Expr>,
109        alias: String,
110    },
111    Function {
112        name: String,
113        args: Vec<Expr>,
114        distinct: bool,
115    },
116    Binary {
117        left: Box<Expr>,
118        op: BinOp,
119        right: Box<Expr>,
120    },
121    Unary {
122        op: UnaryOp,
123        expr: Box<Expr>,
124    },
125    IsNull {
126        expr: Box<Expr>,
127        negated: bool,
128    },
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum BinOp {
133    Add,
134    Sub,
135    Mul,
136    Div,
137    Mod,
138    Eq,
139    NotEq,
140    Lt,
141    LtEq,
142    Gt,
143    GtEq,
144    And,
145    Or,
146}
147
148#[derive(Debug, Clone, Copy, PartialEq, Eq)]
149pub enum UnaryOp {
150    Not,
151    Neg,
152}