File tree Expand file tree Collapse file tree 6 files changed +61
-12
lines changed
jjtree/net/sf/jsqlparser/parser
test/java/net/sf/jsqlparser Expand file tree Collapse file tree 6 files changed +61
-12
lines changed Original file line number Diff line number Diff line change 11
11
12
12
import java .util .Arrays ;
13
13
import java .util .List ;
14
+ import java .util .Objects ;
15
+
14
16
import net .sf .jsqlparser .parser .ASTNodeAccessImpl ;
15
17
16
18
/**
@@ -95,4 +97,21 @@ public StringValue withValue(String value) {
95
97
this .setValue (value );
96
98
return this ;
97
99
}
100
+
101
+ @ Override
102
+ public boolean equals (Object o ) {
103
+ if (this == o ) {
104
+ return true ;
105
+ }
106
+ if (o == null || getClass () != o .getClass ()) {
107
+ return false ;
108
+ }
109
+ StringValue that = (StringValue ) o ;
110
+ return Objects .equals (value , that .value ) && Objects .equals (prefix , that .prefix );
111
+ }
112
+
113
+ @ Override
114
+ public int hashCode () {
115
+ return Objects .hash (value , prefix );
116
+ }
98
117
}
Original file line number Diff line number Diff line change 16
16
public class LikeExpression extends BinaryExpression {
17
17
18
18
private boolean not = false ;
19
- private String escape = null ;
19
+ private Expression escapeExpression = null ;
20
20
private boolean caseInsensitive = false ;
21
21
22
22
public boolean isNot () {
@@ -40,19 +40,19 @@ public String getStringExpression() {
40
40
@ Override
41
41
public String toString () {
42
42
String retval = getLeftExpression () + " " + (not ? "NOT " : "" ) + getStringExpression () + " " + getRightExpression ();
43
- if (escape != null ) {
44
- retval += " ESCAPE " + "'" + escape + "'" ;
43
+ if (escapeExpression != null ) {
44
+ retval += " ESCAPE " + escapeExpression ;
45
45
}
46
46
47
47
return retval ;
48
48
}
49
49
50
- public String getEscape () {
51
- return escape ;
50
+ public Expression getEscape () {
51
+ return escapeExpression ;
52
52
}
53
53
54
- public void setEscape (String escape ) {
55
- this .escape = escape ;
54
+ public void setEscape (Expression escapeExpression ) {
55
+ this .escapeExpression = escapeExpression ;
56
56
}
57
57
58
58
public boolean isCaseInsensitive () {
@@ -63,7 +63,7 @@ public void setCaseInsensitive(boolean caseInsensitive) {
63
63
this .caseInsensitive = caseInsensitive ;
64
64
}
65
65
66
- public LikeExpression withEscape (String escape ) {
66
+ public LikeExpression withEscape (Expression escape ) {
67
67
this .setEscape (escape );
68
68
return this ;
69
69
}
Original file line number Diff line number Diff line change @@ -264,9 +264,10 @@ public void visit(JdbcParameter jdbcParameter) {
264
264
public void visit (LikeExpression likeExpression ) {
265
265
visitBinaryExpression (likeExpression ,
266
266
(likeExpression .isNot () ? " NOT" : "" ) + (likeExpression .isCaseInsensitive () ? " ILIKE " : " LIKE " ));
267
- String escape = likeExpression .getEscape ();
267
+ Expression escape = likeExpression .getEscape ();
268
268
if (escape != null ) {
269
- buffer .append (" ESCAPE '" ).append (escape ).append ('\'' );
269
+ buffer .append (" ESCAPE " );
270
+ likeExpression .getEscape ().accept (this );
270
271
}
271
272
}
272
273
Original file line number Diff line number Diff line change @@ -3198,10 +3198,11 @@ Expression LikeExpression(Expression leftExpression) #LikeExpression:
3198
3198
{
3199
3199
LikeExpression result = new LikeExpression();
3200
3200
Expression rightExpression = null;
3201
+ Expression escape;
3201
3202
}
3202
3203
{
3203
3204
[<K_NOT> { result.setNot(true); } ] ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
3204
- [<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue() ); }]
3205
+ [<K_ESCAPE> escape=Expression() { result.setEscape(escape ); }]
3205
3206
{
3206
3207
result.setLeftExpression(leftExpression);
3207
3208
result.setRightExpression(rightExpression);
Original file line number Diff line number Diff line change
1
+ /*-
2
+ * #%L
3
+ * JSQLParser library
4
+ * %%
5
+ * Copyright (C) 2004 - 2021 JSQLParser
6
+ * %%
7
+ * Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8
+ * #L%
9
+ */
10
+
11
+ package net .sf .jsqlparser .expression ;
12
+
13
+ import net .sf .jsqlparser .JSQLParserException ;
14
+ import net .sf .jsqlparser .test .TestUtils ;
15
+ import org .junit .Test ;
16
+
17
+ /**
18
+ *
19
+ * @author <a href="mailto:[email protected] ">Andreas Reichel</a>
20
+ */
21
+ public class LikeExpressionTest {
22
+ @ Test
23
+ public void testLikeWithEscapeExpressionIssue420 () throws JSQLParserException {
24
+ TestUtils .assertExpressionCanBeParsedAndDeparsed ("a LIKE ?1 ESCAPE ?2" , true );
25
+
26
+ TestUtils .assertSqlCanBeParsedAndDeparsed ("select * from dual where a LIKE ?1 ESCAPE ?2" , true );
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -1446,7 +1446,7 @@ public void testLike() throws JSQLParserException {
1446
1446
plainSelect = (PlainSelect ) select .getSelectBody ();
1447
1447
assertEquals ("test" , ((StringValue ) ((LikeExpression ) plainSelect .getWhere ()).
1448
1448
getRightExpression ()).getValue ());
1449
- assertEquals ("test2" , ((LikeExpression ) plainSelect .getWhere ()).getEscape ());
1449
+ assertEquals (new StringValue ( "test2" ) , ((LikeExpression ) plainSelect .getWhere ()).getEscape ());
1450
1450
}
1451
1451
1452
1452
@ Test
You can’t perform that action at this time.
0 commit comments