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    Set(SetStatement),
16    Begin,
17    Commit,
18    Rollback,
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22pub struct SelectStatement {
23    pub distinct: bool,
24    pub columns: Vec<SelectColumn>,
25    pub from: Option<String>,
26    pub joins: Vec<JoinClause>,
27    pub where_clause: Option<Expression>,
28    pub group_by: Vec<String>,
29    pub having: Option<Expression>,
30    pub order_by: Vec<OrderByColumn>,
31    pub limit: Option<Limit>,
32}
33
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35pub struct JoinClause {
36    pub join_type: JoinType,
37    pub table: String,
38    pub on_condition: Expression,
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub enum JoinType {
43    Inner,
44    Left,
45    Right,
46    Full,
47}
48
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
50pub enum SelectColumn {
51    Wildcard,
52    Column { name: String, alias: Option<String> },
53    Function { name: String, args: Vec<Expression>, alias: Option<String> },
54}
55
56#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
57pub struct OrderByColumn {
58    pub column: String,
59    pub direction: OrderDirection,
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63pub enum OrderDirection {
64    Asc,
65    Desc,
66}
67
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub struct Limit {
70    pub offset: Option<usize>,
71    pub count: usize,
72}
73
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub struct InsertStatement {
76    pub table: String,
77    pub columns: Vec<String>,
78    pub values: Vec<Vec<Expression>>,
79}
80
81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
82pub struct UpsertStatement {
83    pub table: String,
84    pub columns: Vec<String>,
85    pub values: Vec<Vec<Expression>>,
86    pub key_column: String, // Column to check for duplicates
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct UpdateStatement {
91    pub table: String,
92    pub assignments: Vec<(String, Expression)>,
93    pub where_clause: Option<Expression>,
94}
95
96#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
97pub struct DeleteStatement {
98    pub table: String,
99    pub where_clause: Option<Expression>,
100}
101
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
103pub struct CreateTableStatement {
104    pub name: String,
105    pub columns: Vec<String>,
106    pub file_path: String,
107    pub delimiter: Option<char>,
108}
109
110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
111pub struct DropTableStatement {
112    pub name: String,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub struct UseStatement {
117    pub database: String,
118}
119
120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
121pub enum ShowStatement {
122    Tables,
123    Columns(String),
124    Variables,
125    OutputModules,
126}
127
128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
129pub struct SetStatement {
130    pub variable: String,
131    pub value: Expression,
132}
133
134/// Expression types for WHERE clauses and values
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub enum Expression {
137    Literal(Literal),
138    Column(String),
139    SystemVariable(String), // e.g., @@VERSION, @@ROWCOUNT
140    Function { name: String, args: Vec<Expression> },
141    BinaryOp { left: Box<Expression>, op: BinaryOperator, right: Box<Expression> },
142    UnaryOp { op: UnaryOperator, operand: Box<Expression> },
143}
144
145#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
146pub enum Literal {
147    String(String),
148    Number(f64),
149    Integer(i64),
150    Boolean(bool),
151    Null,
152}
153
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
155pub enum BinaryOperator {
156    Equal,
157    NotEqual,
158    GreaterThan,
159    GreaterEqual,
160    LessThan,
161    LessEqual,
162    Like,
163    And,
164    Or,
165}
166
167#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
168pub enum UnaryOperator {
169    Not,
170    IsNull,
171}