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