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