@@ -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,55 @@ 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
106
SQLCast {
107
- expr : Box < ASTNode > ,
107
+ expr : Box < Expr > ,
108
108
data_type : SQLType ,
109
109
} ,
110
110
SQLExtract {
111
111
field : SQLDateTimeField ,
112
- expr : Box < ASTNode > ,
112
+ expr : Box < Expr > ,
113
113
} ,
114
114
/// `expr COLLATE collation`
115
115
SQLCollate {
116
- expr : Box < ASTNode > ,
116
+ expr : Box < Expr > ,
117
117
collation : SQLObjectName ,
118
118
} ,
119
119
/// Nested expression e.g. `(foo > bar)` or `(1)`
120
- SQLNested ( Box < ASTNode > ) ,
120
+ SQLNested ( Box < Expr > ) ,
121
121
/// SQLValue
122
122
SQLValue ( Value ) ,
123
123
/// Scalar function call e.g. `LEFT(foo, 5)`
@@ -128,10 +128,10 @@ pub enum ASTNode {
128
128
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
129
129
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
130
130
SQLCase {
131
- operand : Option < Box < ASTNode > > ,
132
- conditions : Vec < ASTNode > ,
133
- results : Vec < ASTNode > ,
134
- else_result : Option < Box < ASTNode > > ,
131
+ operand : Option < Box < Expr > > ,
132
+ conditions : Vec < Expr > ,
133
+ results : Vec < Expr > ,
134
+ else_result : Option < Box < Expr > > ,
135
135
} ,
136
136
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
137
137
/// `WHERE EXISTS (SELECT ...)`.
@@ -141,16 +141,16 @@ pub enum ASTNode {
141
141
SQLSubquery ( Box < SQLQuery > ) ,
142
142
}
143
143
144
- impl ToString for ASTNode {
144
+ impl ToString for Expr {
145
145
fn to_string ( & self ) -> String {
146
146
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 {
147
+ Expr :: SQLIdentifier ( s) => s. to_string ( ) ,
148
+ Expr :: SQLWildcard => "*" . to_string ( ) ,
149
+ Expr :: SQLQualifiedWildcard ( q) => q. join ( "." ) + ".*" ,
150
+ Expr :: SQLCompoundIdentifier ( s) => s. join ( "." ) ,
151
+ Expr :: SQLIsNull ( ast) => format ! ( "{} IS NULL" , ast. as_ref( ) . to_string( ) ) ,
152
+ Expr :: SQLIsNotNull ( ast) => format ! ( "{} IS NOT NULL" , ast. as_ref( ) . to_string( ) ) ,
153
+ Expr :: SQLInList {
154
154
expr,
155
155
list,
156
156
negated,
@@ -160,7 +160,7 @@ impl ToString for ASTNode {
160
160
if * negated { "NOT " } else { "" } ,
161
161
comma_separated_string( list)
162
162
) ,
163
- ASTNode :: SQLInSubquery {
163
+ Expr :: SQLInSubquery {
164
164
expr,
165
165
subquery,
166
166
negated,
@@ -170,7 +170,7 @@ impl ToString for ASTNode {
170
170
if * negated { "NOT " } else { "" } ,
171
171
subquery. to_string( )
172
172
) ,
173
- ASTNode :: SQLBetween {
173
+ Expr :: SQLBetween {
174
174
expr,
175
175
negated,
176
176
low,
@@ -182,32 +182,32 @@ impl ToString for ASTNode {
182
182
low. to_string( ) ,
183
183
high. to_string( )
184
184
) ,
185
- ASTNode :: SQLBinaryOp { left, op, right } => format ! (
185
+ Expr :: SQLBinaryOp { left, op, right } => format ! (
186
186
"{} {} {}" ,
187
187
left. as_ref( ) . to_string( ) ,
188
188
op. to_string( ) ,
189
189
right. as_ref( ) . to_string( )
190
190
) ,
191
- ASTNode :: SQLUnaryOp { op, expr } => {
191
+ Expr :: SQLUnaryOp { op, expr } => {
192
192
format ! ( "{} {}" , op. to_string( ) , expr. as_ref( ) . to_string( ) )
193
193
}
194
- ASTNode :: SQLCast { expr, data_type } => format ! (
194
+ Expr :: SQLCast { expr, data_type } => format ! (
195
195
"CAST({} AS {})" ,
196
196
expr. as_ref( ) . to_string( ) ,
197
197
data_type. to_string( )
198
198
) ,
199
- ASTNode :: SQLExtract { field, expr } => {
199
+ Expr :: SQLExtract { field, expr } => {
200
200
format ! ( "EXTRACT({} FROM {})" , field. to_string( ) , expr. to_string( ) )
201
201
}
202
- ASTNode :: SQLCollate { expr, collation } => format ! (
202
+ Expr :: SQLCollate { expr, collation } => format ! (
203
203
"{} COLLATE {}" ,
204
204
expr. as_ref( ) . to_string( ) ,
205
205
collation. to_string( )
206
206
) ,
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 {
207
+ Expr :: SQLNested ( ast) => format ! ( "({})" , ast. as_ref( ) . to_string( ) ) ,
208
+ Expr :: SQLValue ( v) => v. to_string ( ) ,
209
+ Expr :: SQLFunction ( f) => f. to_string ( ) ,
210
+ Expr :: SQLCase {
211
211
operand,
212
212
conditions,
213
213
results,
@@ -228,16 +228,16 @@ impl ToString for ASTNode {
228
228
}
229
229
s + " END"
230
230
}
231
- ASTNode :: SQLExists ( s) => format ! ( "EXISTS ({})" , s. to_string( ) ) ,
232
- ASTNode :: SQLSubquery ( s) => format ! ( "({})" , s. to_string( ) ) ,
231
+ Expr :: SQLExists ( s) => format ! ( "EXISTS ({})" , s. to_string( ) ) ,
232
+ Expr :: SQLSubquery ( s) => format ! ( "({})" , s. to_string( ) ) ,
233
233
}
234
234
}
235
235
}
236
236
237
237
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
238
238
#[ derive( Debug , Clone , PartialEq , Hash ) ]
239
239
pub struct SQLWindowSpec {
240
- pub partition_by : Vec < ASTNode > ,
240
+ pub partition_by : Vec < Expr > ,
241
241
pub order_by : Vec < SQLOrderByExpr > ,
242
242
pub window_frame : Option < SQLWindowFrame > ,
243
243
}
@@ -374,14 +374,14 @@ pub enum SQLStatement {
374
374
/// Column assignments
375
375
assignments : Vec < SQLAssignment > ,
376
376
/// WHERE
377
- selection : Option < ASTNode > ,
377
+ selection : Option < Expr > ,
378
378
} ,
379
379
/// DELETE
380
380
SQLDelete {
381
381
/// FROM
382
382
table_name : SQLObjectName ,
383
383
/// WHERE
384
- selection : Option < ASTNode > ,
384
+ selection : Option < Expr > ,
385
385
} ,
386
386
/// CREATE VIEW
387
387
SQLCreateView {
@@ -604,7 +604,7 @@ impl ToString for SQLObjectName {
604
604
#[ derive( Debug , Clone , PartialEq , Hash ) ]
605
605
pub struct SQLAssignment {
606
606
pub id : SQLIdent ,
607
- pub value : ASTNode ,
607
+ pub value : Expr ,
608
608
}
609
609
610
610
impl ToString for SQLAssignment {
@@ -617,7 +617,7 @@ impl ToString for SQLAssignment {
617
617
#[ derive( Debug , Clone , PartialEq , Hash ) ]
618
618
pub struct SQLFunction {
619
619
pub name : SQLObjectName ,
620
- pub args : Vec < ASTNode > ,
620
+ pub args : Vec < Expr > ,
621
621
pub over : Option < SQLWindowSpec > ,
622
622
// aggregate functions may specify eg `COUNT(DISTINCT x)`
623
623
pub distinct : bool ,
0 commit comments