Skip to main content

mdql_core/
query_ast.rs

1//! AST types for the MDQL SQL subset.
2
3// ── AST nodes ──────────────────────────────────────────────────────────────
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum ArithOp {
7    Add,
8    Sub,
9    Mul,
10    Div,
11    Mod,
12}
13
14#[derive(Debug, Clone, PartialEq)]
15pub enum Expr {
16    Literal(SqlValue),
17    Column(String),
18    BinaryOp { left: Box<Expr>, op: ArithOp, right: Box<Expr> },
19    UnaryMinus(Box<Expr>),
20    Case { whens: Vec<(WhereClause, Box<Expr>)>, else_expr: Option<Box<Expr>> },
21    DateAdd { date: Box<Expr>, days: Box<Expr> },
22    DateDiff { left: Box<Expr>, right: Box<Expr> },
23    CurrentDate,
24    CurrentTimestamp,
25    Aggregate { func: AggFunc, arg: String, arg_expr: Option<Box<Expr>> },
26}
27
28impl Expr {
29    pub fn as_column(&self) -> Option<&str> {
30        if let Expr::Column(name) = self { Some(name) } else { None }
31    }
32
33    pub fn display_name(&self) -> String {
34        match self {
35            Expr::Literal(SqlValue::Int(n)) => n.to_string(),
36            Expr::Literal(SqlValue::Float(f)) => f.to_string(),
37            Expr::Literal(SqlValue::String(s)) => format!("'{}'", s),
38            Expr::Literal(SqlValue::Null) => "NULL".to_string(),
39            Expr::Literal(SqlValue::List(_)) => "list".to_string(),
40            Expr::Column(name) => name.clone(),
41            Expr::BinaryOp { left, op, right } => {
42                let op_str = match op {
43                    ArithOp::Add => "+",
44                    ArithOp::Sub => "-",
45                    ArithOp::Mul => "*",
46                    ArithOp::Div => "/",
47                    ArithOp::Mod => "%",
48                };
49                format!("{} {} {}", left.display_name(), op_str, right.display_name())
50            }
51            Expr::UnaryMinus(inner) => format!("-{}", inner.display_name()),
52            Expr::Case { .. } => "CASE".to_string(),
53            Expr::DateAdd { date, days } => format!("DATE_ADD({}, {})", date.display_name(), days.display_name()),
54            Expr::DateDiff { left, right } => format!("DATEDIFF({}, {})", left.display_name(), right.display_name()),
55            Expr::CurrentDate => "CURRENT_DATE".to_string(),
56            Expr::CurrentTimestamp => "CURRENT_TIMESTAMP".to_string(),
57            Expr::Aggregate { func, arg, .. } => {
58                let func_name = match func {
59                    AggFunc::Count => "COUNT",
60                    AggFunc::Sum => "SUM",
61                    AggFunc::Avg => "AVG",
62                    AggFunc::Min => "MIN",
63                    AggFunc::Max => "MAX",
64                };
65                format!("{}({})", func_name, arg)
66            }
67        }
68    }
69
70    pub fn contains_aggregate(&self) -> bool {
71        match self {
72            Expr::Aggregate { .. } => true,
73            Expr::BinaryOp { left, right, .. } => {
74                left.contains_aggregate() || right.contains_aggregate()
75            }
76            Expr::UnaryMinus(inner) => inner.contains_aggregate(),
77            Expr::Case { whens, else_expr } => {
78                whens.iter().any(|(_, e)| e.contains_aggregate())
79                    || else_expr.as_ref().map_or(false, |e| e.contains_aggregate())
80            }
81            _ => false,
82        }
83    }
84}
85
86#[derive(Debug, Clone, PartialEq)]
87pub struct OrderSpec {
88    pub column: String,
89    pub expr: Option<Expr>,
90    pub descending: bool,
91}
92
93#[derive(Debug, Clone, PartialEq)]
94pub enum CmpOp {
95    Eq,
96    Ne,
97    Lt,
98    Gt,
99    Le,
100    Ge,
101    Like,
102    NotLike,
103    In,
104    IsNull,
105    IsNotNull,
106}
107
108#[derive(Debug, Clone, PartialEq)]
109pub enum BoolOpKind {
110    And,
111    Or,
112}
113
114#[derive(Debug, Clone, PartialEq)]
115pub struct Comparison {
116    pub column: String,
117    pub op: CmpOp,
118    pub value: Option<SqlValue>,
119    pub left_expr: Option<Expr>,
120    pub right_expr: Option<Expr>,
121}
122
123#[derive(Debug, Clone, PartialEq)]
124pub struct BoolOp {
125    pub op: BoolOpKind,
126    pub left: Box<WhereClause>,
127    pub right: Box<WhereClause>,
128}
129
130#[derive(Debug, Clone, PartialEq)]
131pub enum WhereClause {
132    Comparison(Comparison),
133    BoolOp(BoolOp),
134}
135
136#[derive(Debug, Clone, PartialEq)]
137pub enum SqlValue {
138    String(String),
139    Int(i64),
140    Float(f64),
141    Null,
142    List(Vec<SqlValue>),
143}
144
145#[derive(Debug, Clone, PartialEq)]
146pub enum JoinType {
147    Inner,
148    Left,
149}
150
151#[derive(Debug, Clone, PartialEq)]
152pub struct JoinClause {
153    pub join_type: JoinType,
154    pub table: String,
155    pub alias: Option<String>,
156    pub condition: WhereClause,
157}
158
159#[derive(Debug, Clone, PartialEq)]
160pub enum AggFunc {
161    Count,
162    Sum,
163    Avg,
164    Min,
165    Max,
166}
167
168#[derive(Debug, Clone, PartialEq)]
169pub enum SelectExpr {
170    Column(String),
171    Aggregate { func: AggFunc, arg: String, arg_expr: Option<Expr>, alias: Option<String> },
172    Expr { expr: Expr, alias: Option<String> },
173}
174
175impl SelectExpr {
176    pub fn output_name(&self) -> String {
177        match self {
178            SelectExpr::Column(name) => name.clone(),
179            SelectExpr::Aggregate { func, arg, alias, .. } => {
180                if let Some(a) = alias {
181                    a.clone()
182                } else {
183                    let func_name = match func {
184                        AggFunc::Count => "COUNT",
185                        AggFunc::Sum => "SUM",
186                        AggFunc::Avg => "AVG",
187                        AggFunc::Min => "MIN",
188                        AggFunc::Max => "MAX",
189                    };
190                    format!("{}({})", func_name, arg)
191                }
192            }
193            SelectExpr::Expr { expr, alias } => {
194                alias.clone().unwrap_or_else(|| expr.display_name())
195            }
196        }
197    }
198
199    pub fn is_aggregate(&self) -> bool {
200        match self {
201            SelectExpr::Aggregate { .. } => true,
202            SelectExpr::Expr { expr, .. } => expr.contains_aggregate(),
203            _ => false,
204        }
205    }
206}
207
208#[derive(Debug, Clone, PartialEq)]
209pub struct SelectQuery {
210    pub columns: ColumnList,
211    pub table: String,
212    pub table_alias: Option<String>,
213    pub subquery: Option<Box<SelectQuery>>,
214    pub joins: Vec<JoinClause>,
215    pub where_clause: Option<WhereClause>,
216    pub group_by: Option<Vec<String>>,
217    pub having: Option<WhereClause>,
218    pub order_by: Option<Vec<OrderSpec>>,
219    pub limit: Option<i64>,
220}
221
222#[derive(Debug, Clone, PartialEq)]
223pub enum ColumnList {
224    All,
225    Named(Vec<SelectExpr>),
226}
227
228#[derive(Debug, Clone, PartialEq)]
229pub struct InsertQuery {
230    pub table: String,
231    pub columns: Vec<String>,
232    pub values: Vec<SqlValue>,
233}
234
235#[derive(Debug, Clone, PartialEq)]
236pub struct UpdateQuery {
237    pub table: String,
238    pub assignments: Vec<(String, SqlValue)>,
239    pub where_clause: Option<WhereClause>,
240}
241
242#[derive(Debug, Clone, PartialEq)]
243pub enum DeleteMode {
244    Default,
245    Cascade,
246    Restrict,
247}
248
249#[derive(Debug, Clone, PartialEq)]
250pub struct DeleteQuery {
251    pub table: String,
252    pub where_clause: Option<WhereClause>,
253    pub mode: DeleteMode,
254}
255
256#[derive(Debug, Clone, PartialEq)]
257pub struct AlterRenameFieldQuery {
258    pub table: String,
259    pub old_name: String,
260    pub new_name: String,
261}
262
263#[derive(Debug, Clone, PartialEq)]
264pub struct AlterDropFieldQuery {
265    pub table: String,
266    pub field_name: String,
267}
268
269#[derive(Debug, Clone, PartialEq)]
270pub struct AlterMergeFieldsQuery {
271    pub table: String,
272    pub sources: Vec<String>,
273    pub into: String,
274}
275
276#[derive(Debug, Clone, PartialEq)]
277pub struct CreateViewQuery {
278    pub view_name: String,
279    pub columns: Option<Vec<String>>,
280    pub query: Box<SelectQuery>,
281}
282
283#[derive(Debug, Clone, PartialEq)]
284pub struct DropViewQuery {
285    pub view_name: String,
286}
287
288#[derive(Debug, Clone, PartialEq)]
289pub enum Statement {
290    Select(SelectQuery),
291    Insert(InsertQuery),
292    Update(UpdateQuery),
293    Delete(DeleteQuery),
294    AlterRename(AlterRenameFieldQuery),
295    AlterDrop(AlterDropFieldQuery),
296    AlterMerge(AlterMergeFieldsQuery),
297    CreateView(CreateViewQuery),
298    DropView(DropViewQuery),
299}
300
301impl Statement {
302    pub fn table_name(&self) -> &str {
303        match self {
304            Statement::Select(q) => &q.table,
305            Statement::Insert(q) => &q.table,
306            Statement::Update(q) => &q.table,
307            Statement::Delete(q) => &q.table,
308            Statement::AlterRename(q) => &q.table,
309            Statement::AlterDrop(q) => &q.table,
310            Statement::AlterMerge(q) => &q.table,
311            Statement::CreateView(q) => &q.view_name,
312            Statement::DropView(q) => &q.view_name,
313        }
314    }
315}
316
317pub fn where_clause_to_sql(clause: &WhereClause) -> String {
318    match clause {
319        WhereClause::BoolOp(bop) => {
320            let left = where_clause_to_sql(&bop.left);
321            let right = where_clause_to_sql(&bop.right);
322            let op = match bop.op {
323                BoolOpKind::And => "AND",
324                BoolOpKind::Or => "OR",
325            };
326            format!("{} {} {}", left, op, right)
327        }
328        WhereClause::Comparison(cmp) => {
329            let op_str = match cmp.op {
330                CmpOp::Eq => "=",
331                CmpOp::Ne => "!=",
332                CmpOp::Lt => "<",
333                CmpOp::Gt => ">",
334                CmpOp::Le => "<=",
335                CmpOp::Ge => ">=",
336                CmpOp::Like => "LIKE",
337                CmpOp::NotLike => "NOT LIKE",
338                CmpOp::In => "IN",
339                CmpOp::IsNull => "IS NULL",
340                CmpOp::IsNotNull => "IS NOT NULL",
341            };
342            if matches!(cmp.op, CmpOp::IsNull | CmpOp::IsNotNull) {
343                if let Some(ref expr) = cmp.left_expr {
344                    return format!("{} {}", expr.display_name(), op_str);
345                }
346                return format!("{} {}", cmp.column, op_str);
347            }
348            if let (Some(ref left), Some(ref right)) = (&cmp.left_expr, &cmp.right_expr) {
349                return format!("{} {} {}", left.display_name(), op_str, right.display_name());
350            }
351            match &cmp.value {
352                Some(SqlValue::String(s)) => format!("{} {} '{}'", cmp.column, op_str, s),
353                Some(SqlValue::Int(n)) => format!("{} {} {}", cmp.column, op_str, n),
354                Some(SqlValue::Float(f)) => format!("{} {} {}", cmp.column, op_str, f),
355                Some(SqlValue::Null) => format!("{} {} NULL", cmp.column, op_str),
356                Some(SqlValue::List(items)) => {
357                    let vals: Vec<String> = items.iter().map(|v| match v {
358                        SqlValue::String(s) => format!("'{}'", s),
359                        SqlValue::Int(n) => n.to_string(),
360                        SqlValue::Float(f) => f.to_string(),
361                        _ => "NULL".to_string(),
362                    }).collect();
363                    format!("{} {} ({})", cmp.column, op_str, vals.join(", "))
364                }
365                None => format!("{} {}", cmp.column, op_str),
366            }
367        }
368    }
369}