Skip to content

Commit 8eaa4d2

Browse files
Issue #420 Like Expression with Escape Expression (#1406)
* Issue #420 Like Expression with Escape Expression Fixes issue #420 * CheckStyle compliance
1 parent 9adab8d commit 8eaa4d2

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

src/main/java/net/sf/jsqlparser/expression/StringValue.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import java.util.Arrays;
1313
import java.util.List;
14+
import java.util.Objects;
15+
1416
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1517

1618
/**
@@ -95,4 +97,21 @@ public StringValue withValue(String value) {
9597
this.setValue(value);
9698
return this;
9799
}
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+
}
98117
}

src/main/java/net/sf/jsqlparser/expression/operators/relational/LikeExpression.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class LikeExpression extends BinaryExpression {
1717

1818
private boolean not = false;
19-
private String escape = null;
19+
private Expression escapeExpression = null;
2020
private boolean caseInsensitive = false;
2121

2222
public boolean isNot() {
@@ -40,19 +40,19 @@ public String getStringExpression() {
4040
@Override
4141
public String toString() {
4242
String retval = getLeftExpression() + " " + (not ? "NOT " : "") + getStringExpression() + " " + getRightExpression();
43-
if (escape != null) {
44-
retval += " ESCAPE " + "'" + escape + "'";
43+
if (escapeExpression != null) {
44+
retval += " ESCAPE " + escapeExpression ;
4545
}
4646

4747
return retval;
4848
}
4949

50-
public String getEscape() {
51-
return escape;
50+
public Expression getEscape() {
51+
return escapeExpression;
5252
}
5353

54-
public void setEscape(String escape) {
55-
this.escape = escape;
54+
public void setEscape(Expression escapeExpression) {
55+
this.escapeExpression = escapeExpression;
5656
}
5757

5858
public boolean isCaseInsensitive() {
@@ -63,7 +63,7 @@ public void setCaseInsensitive(boolean caseInsensitive) {
6363
this.caseInsensitive = caseInsensitive;
6464
}
6565

66-
public LikeExpression withEscape(String escape) {
66+
public LikeExpression withEscape(Expression escape) {
6767
this.setEscape(escape);
6868
return this;
6969
}

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ public void visit(JdbcParameter jdbcParameter) {
264264
public void visit(LikeExpression likeExpression) {
265265
visitBinaryExpression(likeExpression,
266266
(likeExpression.isNot() ? " NOT" : "") + (likeExpression.isCaseInsensitive() ? " ILIKE " : " LIKE "));
267-
String escape = likeExpression.getEscape();
267+
Expression escape = likeExpression.getEscape();
268268
if (escape != null) {
269-
buffer.append(" ESCAPE '").append(escape).append('\'');
269+
buffer.append(" ESCAPE ");
270+
likeExpression.getEscape().accept(this);
270271
}
271272
}
272273

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3198,10 +3198,11 @@ Expression LikeExpression(Expression leftExpression) #LikeExpression:
31983198
{
31993199
LikeExpression result = new LikeExpression();
32003200
Expression rightExpression = null;
3201+
Expression escape;
32013202
}
32023203
{
32033204
[<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); }]
32053206
{
32063207
result.setLeftExpression(leftExpression);
32073208
result.setRightExpression(rightExpression);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ public void testLike() throws JSQLParserException {
14461446
plainSelect = (PlainSelect) select.getSelectBody();
14471447
assertEquals("test", ((StringValue) ((LikeExpression) plainSelect.getWhere()).
14481448
getRightExpression()).getValue());
1449-
assertEquals("test2", ((LikeExpression) plainSelect.getWhere()).getEscape());
1449+
assertEquals(new StringValue("test2"), ((LikeExpression) plainSelect.getWhere()).getEscape());
14501450
}
14511451

14521452
@Test

0 commit comments

Comments
 (0)