1
1
use crate :: parser:: errors:: ParsingError ;
2
- use crate :: {
3
- BinaryMatchingExpression , EscapeExpression , Expression , Keyword , LikeExpressionType , Parser ,
4
- } ;
2
+ use crate :: { Expression , Keyword , LikeExpressionType , Parser } ;
5
3
6
4
use super :: ExpressionParser ;
7
5
@@ -28,96 +26,56 @@ impl LikeExpressionParser for Parser<'_> {
28
26
escape_expression = Some ( Box :: new ( self . parse_expression ( ) ?) ) ;
29
27
}
30
28
31
- let matching_expression: BinaryMatchingExpression = {
32
- match escape_expression {
33
- Some ( escape_expression) => BinaryMatchingExpression :: Like (
34
- LikeExpressionType :: EscapeExpression ( EscapeExpression {
35
- expression : Box :: new ( pattern) ,
36
- escape_expression : Some ( escape_expression) ,
37
- } ) ,
38
- ) ,
39
- None => BinaryMatchingExpression :: Like ( LikeExpressionType :: Expression ( Box :: new (
40
- pattern,
41
- ) ) ) ,
42
- }
29
+ let like_expression = LikeExpressionType {
30
+ expression : Box :: new ( expression) ,
31
+ not : is_not,
32
+ like_expression : Box :: new ( pattern) ,
33
+ escape_expression,
43
34
} ;
44
35
45
- let matching_expression = if is_not {
46
- BinaryMatchingExpression :: Not ( Box :: new ( matching_expression) )
47
- } else {
48
- matching_expression
49
- } ;
50
-
51
- Ok ( Expression :: BinaryMatchingExpression (
52
- Box :: new ( expression) ,
53
- matching_expression,
54
- ) )
36
+ Ok ( Expression :: LikeExpression ( like_expression) )
55
37
}
56
38
}
57
39
58
40
#[ cfg( test) ]
59
41
mod like_expression_tests {
60
42
use crate :: parser:: test_utils:: run_sunny_day_test;
61
43
use crate :: select:: test_utils:: select_expr;
62
- use crate :: { BinaryMatchingExpression , EscapeExpression , Expression , LikeExpressionType } ;
44
+ use crate :: { Expression , LikeExpressionType } ;
63
45
64
46
use crate :: parser:: expression:: test_utils:: * ;
65
47
66
- fn like_expr (
67
- expression : Expression ,
68
- like_expression_type : LikeExpressionType ,
69
- inverted : bool ,
70
- ) -> Expression {
71
- let binary_matching_expression = if inverted {
72
- BinaryMatchingExpression :: Not ( Box :: new ( BinaryMatchingExpression :: Like (
73
- like_expression_type,
74
- ) ) )
75
- } else {
76
- BinaryMatchingExpression :: Like ( like_expression_type)
77
- } ;
78
-
79
- Expression :: BinaryMatchingExpression ( Box :: new ( expression) , binary_matching_expression)
48
+ fn like_expr ( expr : Expression , like_expr : Expression ) -> LikeExpressionType {
49
+ LikeExpressionType {
50
+ expression : Box :: new ( expr) ,
51
+ not : false ,
52
+ like_expression : Box :: new ( like_expr) ,
53
+ escape_expression : None ,
54
+ }
80
55
}
81
56
82
57
#[ test]
83
58
fn like_expr_test ( ) {
84
- run_sunny_day_test (
85
- "SELECT 1 LIKE 'a%';" ,
86
- select_expr ( like_expr (
87
- numeric_expr ( "1" ) ,
88
- LikeExpressionType :: Expression ( Box :: new ( string_expr ( "'a%'" ) ) ) ,
89
- false ,
90
- ) )
91
- . into ( ) ,
92
- ) ;
59
+ let expr = like_expr ( numeric_expr ( "1" ) , string_expr ( "'a%'" ) ) ;
60
+ run_sunny_day_test ( "SELECT 1 LIKE 'a%';" , select_expr ( expr. into ( ) ) . into ( ) ) ;
93
61
}
94
62
95
63
#[ test]
96
64
fn not_like_expr ( ) {
97
- run_sunny_day_test (
98
- "SELECT 1 NOT LIKE 'a%';" ,
99
- select_expr ( like_expr (
100
- numeric_expr ( "1" ) ,
101
- LikeExpressionType :: Expression ( Box :: new ( string_expr ( "'a%'" ) ) ) ,
102
- true ,
103
- ) )
104
- . into ( ) ,
105
- ) ;
65
+ let mut expr = like_expr ( numeric_expr ( "1" ) , string_expr ( "'a%'" ) ) ;
66
+ expr. not = true ;
67
+
68
+ run_sunny_day_test ( "SELECT 1 NOT LIKE 'a%';" , select_expr ( expr. into ( ) ) . into ( ) ) ;
106
69
}
107
70
108
71
#[ test]
109
72
fn like_with_escape_expr_test ( ) {
73
+ let mut like_expr = like_expr ( numeric_expr ( "1" ) , string_expr ( "'a%'" ) ) ;
74
+ like_expr. escape_expression = Some ( Box :: new ( string_expr ( "'b'" ) ) ) ;
75
+
110
76
run_sunny_day_test (
111
77
"SELECT 1 LIKE 'a%' ESCAPE 'b';" ,
112
- select_expr ( like_expr (
113
- numeric_expr ( "1" ) ,
114
- LikeExpressionType :: EscapeExpression ( EscapeExpression {
115
- expression : Box :: new ( string_expr ( "'a%'" ) ) ,
116
- escape_expression : Some ( Box :: new ( string_expr ( "'b'" ) ) ) ,
117
- } ) ,
118
- false ,
119
- ) )
120
- . into ( ) ,
78
+ select_expr ( like_expr. into ( ) ) . into ( ) ,
121
79
) ;
122
80
}
123
81
}
0 commit comments