1#[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 struct JoinClause {
147 pub table: String,
148 pub alias: Option<String>,
149 pub left_col: String,
150 pub right_col: String,
151}
152
153#[derive(Debug, Clone, PartialEq)]
154pub enum AggFunc {
155 Count,
156 Sum,
157 Avg,
158 Min,
159 Max,
160}
161
162#[derive(Debug, Clone, PartialEq)]
163pub enum SelectExpr {
164 Column(String),
165 Aggregate { func: AggFunc, arg: String, arg_expr: Option<Expr>, alias: Option<String> },
166 Expr { expr: Expr, alias: Option<String> },
167}
168
169impl SelectExpr {
170 pub fn output_name(&self) -> String {
171 match self {
172 SelectExpr::Column(name) => name.clone(),
173 SelectExpr::Aggregate { func, arg, alias, .. } => {
174 if let Some(a) = alias {
175 a.clone()
176 } else {
177 let func_name = match func {
178 AggFunc::Count => "COUNT",
179 AggFunc::Sum => "SUM",
180 AggFunc::Avg => "AVG",
181 AggFunc::Min => "MIN",
182 AggFunc::Max => "MAX",
183 };
184 format!("{}({})", func_name, arg)
185 }
186 }
187 SelectExpr::Expr { expr, alias } => {
188 alias.clone().unwrap_or_else(|| expr.display_name())
189 }
190 }
191 }
192
193 pub fn is_aggregate(&self) -> bool {
194 match self {
195 SelectExpr::Aggregate { .. } => true,
196 SelectExpr::Expr { expr, .. } => expr.contains_aggregate(),
197 _ => false,
198 }
199 }
200}
201
202#[derive(Debug, Clone, PartialEq)]
203pub struct SelectQuery {
204 pub columns: ColumnList,
205 pub table: String,
206 pub table_alias: Option<String>,
207 pub subquery: Option<Box<SelectQuery>>,
208 pub joins: Vec<JoinClause>,
209 pub where_clause: Option<WhereClause>,
210 pub group_by: Option<Vec<String>>,
211 pub having: Option<WhereClause>,
212 pub order_by: Option<Vec<OrderSpec>>,
213 pub limit: Option<i64>,
214}
215
216#[derive(Debug, Clone, PartialEq)]
217pub enum ColumnList {
218 All,
219 Named(Vec<SelectExpr>),
220}
221
222#[derive(Debug, Clone, PartialEq)]
223pub struct InsertQuery {
224 pub table: String,
225 pub columns: Vec<String>,
226 pub values: Vec<SqlValue>,
227}
228
229#[derive(Debug, Clone, PartialEq)]
230pub struct UpdateQuery {
231 pub table: String,
232 pub assignments: Vec<(String, SqlValue)>,
233 pub where_clause: Option<WhereClause>,
234}
235
236#[derive(Debug, Clone, PartialEq)]
237pub struct DeleteQuery {
238 pub table: String,
239 pub where_clause: Option<WhereClause>,
240}
241
242#[derive(Debug, Clone, PartialEq)]
243pub struct AlterRenameFieldQuery {
244 pub table: String,
245 pub old_name: String,
246 pub new_name: String,
247}
248
249#[derive(Debug, Clone, PartialEq)]
250pub struct AlterDropFieldQuery {
251 pub table: String,
252 pub field_name: String,
253}
254
255#[derive(Debug, Clone, PartialEq)]
256pub struct AlterMergeFieldsQuery {
257 pub table: String,
258 pub sources: Vec<String>,
259 pub into: String,
260}
261
262#[derive(Debug, Clone, PartialEq)]
263pub struct CreateViewQuery {
264 pub view_name: String,
265 pub columns: Option<Vec<String>>,
266 pub query: Box<SelectQuery>,
267}
268
269#[derive(Debug, Clone, PartialEq)]
270pub struct DropViewQuery {
271 pub view_name: String,
272}
273
274#[derive(Debug, Clone, PartialEq)]
275pub enum Statement {
276 Select(SelectQuery),
277 Insert(InsertQuery),
278 Update(UpdateQuery),
279 Delete(DeleteQuery),
280 AlterRename(AlterRenameFieldQuery),
281 AlterDrop(AlterDropFieldQuery),
282 AlterMerge(AlterMergeFieldsQuery),
283 CreateView(CreateViewQuery),
284 DropView(DropViewQuery),
285}
286
287impl Statement {
288 pub fn table_name(&self) -> &str {
289 match self {
290 Statement::Select(q) => &q.table,
291 Statement::Insert(q) => &q.table,
292 Statement::Update(q) => &q.table,
293 Statement::Delete(q) => &q.table,
294 Statement::AlterRename(q) => &q.table,
295 Statement::AlterDrop(q) => &q.table,
296 Statement::AlterMerge(q) => &q.table,
297 Statement::CreateView(q) => &q.view_name,
298 Statement::DropView(q) => &q.view_name,
299 }
300 }
301}