Skip to main content

ddb_core/parser/
ast.rs

1use serde::{Deserialize, Serialize};
2
3/// Abstract Syntax Tree representation of SQL queries
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub enum Statement {
6    Select(SelectStatement),
7    Insert(InsertStatement),
8    Update(UpdateStatement),
9    Delete(DeleteStatement),
10    Upsert(UpsertStatement),
11    CreateTable(CreateTableStatement),
12    DropTable(DropTableStatement),
13    Use(UseStatement),
14    Show(ShowStatement),
15    Describe(String), // Describe table_name
16    Set(SetStatement),
17    Begin,
18    Commit,
19    Rollback,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23pub struct SelectStatement {
24    pub distinct: bool,
25    pub columns: Vec<SelectColumn>,
26    pub from: Option<String>,
27    pub joins: Vec<JoinClause>,
28    pub where_clause: Option<Expression>,
29    pub group_by: Vec<String>,
30    pub having: Option<Expression>,
31    pub order_by: Vec<OrderByColumn>,
32    pub limit: Option<Limit>,
33}
34
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct JoinClause {
37    pub join_type: JoinType,
38    pub table: String,
39    pub on_condition: Expression,
40}
41
42#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
43pub enum JoinType {
44    Inner,
45    Left,
46    Right,
47    Full,
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub enum SelectColumn {
52    Wildcard,
53    Column { name: String, alias: Option<String> },
54    Function { name: String, args: Vec<Expression>, alias: Option<String> },
55}
56
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct OrderByColumn {
59    pub column: String,
60    pub direction: OrderDirection,
61}
62
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub enum OrderDirection {
65    Asc,
66    Desc,
67}
68
69#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
70pub struct Limit {
71    pub offset: Option<usize>,
72    pub count: usize,
73}
74
75#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
76pub struct InsertStatement {
77    pub table: String,
78    pub columns: Vec<String>,
79    pub values: Vec<Vec<Expression>>,
80}
81
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
83pub struct UpsertStatement {
84    pub table: String,
85    pub columns: Vec<String>,
86    pub values: Vec<Vec<Expression>>,
87    pub key_column: String, // Column to check for duplicates
88}
89
90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
91pub struct UpdateStatement {
92    pub table: String,
93    pub assignments: Vec<(String, Expression)>,
94    pub where_clause: Option<Expression>,
95}
96
97#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
98pub struct DeleteStatement {
99    pub table: String,
100    pub where_clause: Option<Expression>,
101}
102
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104pub struct CreateTableStatement {
105    pub name: String,
106    pub columns: Vec<ColumnDefinition>,
107    pub file_path: String,
108    pub delimiter: Option<char>,
109    pub data_starts_on: Option<usize>,
110    pub comment_char: Option<char>,
111    pub quote_char: Option<char>,
112    pub if_not_exists: bool,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub struct ColumnDefinition {
117    pub name: String,
118    pub data_type: ColumnType,
119    pub nullable: bool,
120}
121
122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
123pub enum ColumnType {
124    Integer,
125    Float,
126    String,
127    Boolean,
128    Date,
129    DateTime,
130    Time,
131}
132
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct DropTableStatement {
135    pub name: String,
136    pub if_exists: bool,
137}
138
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
140pub struct UseStatement {
141    pub database: String,
142}
143
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub enum ShowStatement {
146    Tables,
147    Columns(String),
148    Variables,
149    OutputModules,
150}
151
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
153pub struct SetStatement {
154    pub variable: String,
155    pub value: Expression,
156}
157
158/// Expression types for WHERE clauses and values
159#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
160pub enum Expression {
161    Literal(Literal),
162    Column(String),
163    SystemVariable(String), // e.g., @@VERSION, @@ROWCOUNT
164    Function { name: String, args: Vec<Expression> },
165    BinaryOp { left: Box<Expression>, op: BinaryOperator, right: Box<Expression> },
166    UnaryOp { op: UnaryOperator, operand: Box<Expression> },
167}
168
169#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
170pub enum Literal {
171    String(String),
172    Number(f64),
173    Integer(i64),
174    Boolean(bool),
175    Null,
176}
177
178#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
179pub enum BinaryOperator {
180    Equal,
181    NotEqual,
182    GreaterThan,
183    GreaterEqual,
184    LessThan,
185    LessEqual,
186    Like,
187    And,
188    Or,
189}
190
191#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
192pub enum UnaryOperator {
193    Not,
194    IsNull,
195}