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