@@ -53,7 +53,7 @@ pub type SQLIdent = String;
53
53
/// (e.g. boolean vs string), so the caller must handle expressions of
54
54
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
55
55
#[ derive( Debug , Clone , PartialEq , Hash ) ]
56
- pub enum ASTNode {
56
+ pub enum Expr {
57
57
/// Identifier e.g. table name or column name
58
58
SQLIdentifier ( SQLIdent ) ,
59
59
/// Unqualified wildcard (`*`). SQL allows this in limited contexts, such as:
@@ -69,55 +69,52 @@ pub enum ASTNode {
69
69
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
70
70
SQLCompoundIdentifier ( Vec < SQLIdent > ) ,
71
71
/// `IS NULL` expression
72
- SQLIsNull ( Box < ASTNode > ) ,
72
+ SQLIsNull ( Box < Expr > ) ,
73
73
/// `IS NOT NULL` expression
74
- SQLIsNotNull ( Box < ASTNode > ) ,
74
+ SQLIsNotNull ( Box < Expr > ) ,
75
75
/// `[ NOT ] IN (val1, val2, ...)`
76
76
SQLInList {
77
- expr : Box < ASTNode > ,
78
- list : Vec < ASTNode > ,
77
+ expr : Box < Expr > ,
78
+ list : Vec < Expr > ,
79
79
negated : bool ,
80
80
} ,
81
81
/// `[ NOT ] IN (SELECT ...)`
82
82
SQLInSubquery {
83
- expr : Box < ASTNode > ,
83
+ expr : Box < Expr > ,
84
84
subquery : Box < SQLQuery > ,
85
85
negated : bool ,
86
86
} ,
87
87
/// `<expr> [ NOT ] BETWEEN <low> AND <high>`
88
88
SQLBetween {
89
- expr : Box < ASTNode > ,
89
+ expr : Box < Expr > ,
90
90
negated : bool ,
91
- low : Box < ASTNode > ,
92
- high : Box < ASTNode > ,
91
+ low : Box < Expr > ,
92
+ high : Box < Expr > ,
93
93
} ,
94
94
/// Binary operation e.g. `1 + 1` or `foo > bar`
95
95
SQLBinaryOp {
96
- left : Box < ASTNode > ,
96
+ left : Box < Expr > ,
97
97
op : SQLBinaryOperator ,
98
- right : Box < ASTNode > ,
98
+ right : Box < Expr > ,
99
99
} ,
100
100
/// Unary operation e.g. `NOT foo`
101
101
SQLUnaryOp {
102
102
op : SQLUnaryOperator ,
103
- expr : Box < ASTNode > ,
103
+ expr : Box < Expr > ,
104
104
} ,
105
105
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
106
- SQLCast {
107
- expr : Box < ASTNode > ,
108
- data_type : SQLType ,
109
- } ,
106
+ SQLCast { expr : Box < Expr > , data_type : SQLType } ,
110
107
SQLExtract {
111
108
field : SQLDateTimeField ,
112
- expr : Box < ASTNode > ,
109
+ expr : Box < Expr > ,
113
110
} ,
114
111
/// `expr COLLATE collation`
115
112
SQLCollate {
116
- expr : Box < ASTNode > ,
113
+ expr : Box < Expr > ,
117
114
collation : SQLObjectName ,
118
115
} ,
119
116
/// Nested expression e.g. `(foo > bar)` or `(1)`
120
- SQLNested ( Box < ASTNode > ) ,
117
+ SQLNested ( Box < Expr > ) ,
121
118
/// SQLValue
122
119
SQLValue ( Value ) ,
123
120
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -128,10 +125,10 @@ pub enum ASTNode {
128
125
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
129
126
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
130
127
SQLCase {
131
- operand : Option < Box < ASTNode > > ,
132
- conditions : Vec < ASTNode > ,
133
- results : Vec < ASTNode > ,
134
- else_result : Option < Box < ASTNode > > ,
128
+ operand : Option < Box < Expr > > ,
129
+ conditions : Vec < Expr > ,
130
+ results : Vec < Expr > ,
131
+ else_result : Option < Box < Expr > > ,
135
132
} ,
136
133
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
137
134
/// `WHERE EXISTS (SELECT ...)`.
@@ -141,16 +138,16 @@ pub enum ASTNode {
141
138
SQLSubquery ( Box < SQLQuery > ) ,
142
139
}
143
140
144
- impl ToString for ASTNode {
141
+ impl ToString for Expr {
145
142
fn to_string ( & self ) -> String {
146
143
match self {
147
- ASTNode :: SQLIdentifier ( s) => s. to_string ( ) ,
148
- ASTNode :: SQLWildcard => "*" . to_string ( ) ,
149
- ASTNode :: SQLQualifiedWildcard ( q) => q. join ( "." ) + ".*" ,
150
- ASTNode :: SQLCompoundIdentifier ( s) => s. join ( "." ) ,
151
- ASTNode :: SQLIsNull ( ast) => format ! ( "{} IS NULL" , ast. as_ref( ) . to_string( ) ) ,
152
- ASTNode :: SQLIsNotNull ( ast) => format ! ( "{} IS NOT NULL" , ast. as_ref( ) . to_string( ) ) ,
153
- ASTNode :: SQLInList {
144
+ Expr :: SQLIdentifier ( s) => s. to_string ( ) ,
145
+ Expr :: SQLWildcard => "*" . to_string ( ) ,
146
+ Expr :: SQLQualifiedWildcard ( q) => q. join ( "." ) + ".*" ,
147
+ Expr :: SQLCompoundIdentifier ( s) => s. join ( "." ) ,
148
+ Expr :: SQLIsNull ( ast) => format ! ( "{} IS NULL" , ast. as_ref( ) . to_string( ) ) ,
149
+ Expr :: SQLIsNotNull ( ast) => format ! ( "{} IS NOT NULL" , ast. as_ref( ) . to_string( ) ) ,
150
+ Expr :: SQLInList {
154
151
expr,
155
152
list,
156
153
negated,
@@ -160,7 +157,7 @@ impl ToString for ASTNode {
160
157
if * negated { "NOT " } else { "" } ,
161
158
comma_separated_string( list)
162
159
) ,
163
- ASTNode :: SQLInSubquery {
160
+ Expr :: SQLInSubquery {
164
161
expr,
165
162
subquery,
166
163
negated,
@@ -170,7 +167,7 @@ impl ToString for ASTNode {
170
167
if * negated { "NOT " } else { "" } ,
171
168
subquery. to_string( )
172
169
) ,
173
- ASTNode :: SQLBetween {
170
+ Expr :: SQLBetween {
174
171
expr,
175
172
negated,
176
173
low,
@@ -182,32 +179,32 @@ impl ToString for ASTNode {
182
179
low. to_string( ) ,
183
180
high. to_string( )
184
181
) ,
185
- ASTNode :: SQLBinaryOp { left, op, right } => format ! (
182
+ Expr :: SQLBinaryOp { left, op, right } => format ! (
186
183
"{} {} {}" ,
187
184
left. as_ref( ) . to_string( ) ,
188
185
op. to_string( ) ,
189
186
right. as_ref( ) . to_string( )
190
187
) ,
191
- ASTNode :: SQLUnaryOp { op, expr } => {
188
+ Expr :: SQLUnaryOp { op, expr } => {
192
189
format ! ( "{} {}" , op. to_string( ) , expr. as_ref( ) . to_string( ) )
193
190
}
194
- ASTNode :: SQLCast { expr, data_type } => format ! (
191
+ Expr :: SQLCast { expr, data_type } => format ! (
195
192
"CAST({} AS {})" ,
196
193
expr. as_ref( ) . to_string( ) ,
197
194
data_type. to_string( )
198
195
) ,
199
- ASTNode :: SQLExtract { field, expr } => {
196
+ Expr :: SQLExtract { field, expr } => {
200
197
format ! ( "EXTRACT({} FROM {})" , field. to_string( ) , expr. to_string( ) )
201
198
}
202
- ASTNode :: SQLCollate { expr, collation } => format ! (
199
+ Expr :: SQLCollate { expr, collation } => format ! (
203
200
"{} COLLATE {}" ,
204
201
expr. as_ref( ) . to_string( ) ,
205
202
collation. to_string( )
206
203
) ,
207
- ASTNode :: SQLNested ( ast) => format ! ( "({})" , ast. as_ref( ) . to_string( ) ) ,
208
- ASTNode :: SQLValue ( v) => v. to_string ( ) ,
209
- ASTNode :: SQLFunction ( f) => f. to_string ( ) ,
210
- ASTNode :: SQLCase {
204
+ Expr :: SQLNested ( ast) => format ! ( "({})" , ast. as_ref( ) . to_string( ) ) ,
205
+ Expr :: SQLValue ( v) => v. to_string ( ) ,
206
+ Expr :: SQLFunction ( f) => f. to_string ( ) ,
207
+ Expr :: SQLCase {
211
208
operand,
212
209
conditions,
213
210
results,
@@ -228,16 +225,16 @@ impl ToString for ASTNode {
228
225
}
229
226
s + " END"
230
227
}
231
- ASTNode :: SQLExists ( s) => format ! ( "EXISTS ({})" , s. to_string( ) ) ,
232
- ASTNode :: SQLSubquery ( s) => format ! ( "({})" , s. to_string( ) ) ,
228
+ Expr :: SQLExists ( s) => format ! ( "EXISTS ({})" , s. to_string( ) ) ,
229
+ Expr :: SQLSubquery ( s) => format ! ( "({})" , s. to_string( ) ) ,
233
230
}
234
231
}
235
232
}
236
233
237
234
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
238
235
#[ derive( Debug , Clone , PartialEq , Hash ) ]
239
236
pub struct SQLWindowSpec {
240
- pub partition_by : Vec < ASTNode > ,
237
+ pub partition_by : Vec < Expr > ,
241
238
pub order_by : Vec < SQLOrderByExpr > ,
242
239
pub window_frame : Option < SQLWindowFrame > ,
243
240
}
@@ -374,14 +371,14 @@ pub enum SQLStatement {
374
371
/// Column assignments
375
372
assignments : Vec < SQLAssignment > ,
376
373
/// WHERE
377
- selection : Option < ASTNode > ,
374
+ selection : Option < Expr > ,
378
375
} ,
379
376
/// DELETE
380
377
SQLDelete {
381
378
/// FROM
382
379
table_name : SQLObjectName ,
383
380
/// WHERE
384
- selection : Option < ASTNode > ,
381
+ selection : Option < Expr > ,
385
382
} ,
386
383
/// CREATE VIEW
387
384
SQLCreateView {
@@ -604,7 +601,7 @@ impl ToString for SQLObjectName {
604
601
#[ derive( Debug , Clone , PartialEq , Hash ) ]
605
602
pub struct SQLAssignment {
606
603
pub id : SQLIdent ,
607
- pub value : ASTNode ,
604
+ pub value : Expr ,
608
605
}
609
606
610
607
impl ToString for SQLAssignment {
@@ -617,7 +614,7 @@ impl ToString for SQLAssignment {
617
614
#[ derive( Debug , Clone , PartialEq , Hash ) ]
618
615
pub struct SQLFunction {
619
616
pub name : SQLObjectName ,
620
- pub args : Vec < ASTNode > ,
617
+ pub args : Vec < Expr > ,
621
618
pub over : Option < SQLWindowSpec > ,
622
619
// aggregate functions may specify eg `COUNT(DISTINCT x)`
623
620
pub distinct : bool ,
0 commit comments