Skip to content

Commit 68f47dc

Browse files
committed
fixes #123
1 parent 26e1c0a commit 68f47dc

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class LikeExpression extends BinaryExpression {
2828

2929
private boolean not = false;
3030
private String escape = null;
31+
private boolean caseInsensitive = false;
3132

3233
@Override
3334
public boolean isNot() {
@@ -45,7 +46,7 @@ public void accept(ExpressionVisitor expressionVisitor) {
4546

4647
@Override
4748
public String getStringExpression() {
48-
return ((not) ? "NOT " : "") + "LIKE";
49+
return ((not) ? "NOT " : "") + (caseInsensitive?"ILIKE":"LIKE");
4950
}
5051

5152
@Override
@@ -65,4 +66,12 @@ public String getEscape() {
6566
public void setEscape(String escape) {
6667
this.escape = escape;
6768
}
69+
70+
public boolean isCaseInsensitive() {
71+
return caseInsensitive;
72+
}
73+
74+
public void setCaseInsensitive(boolean caseInsensitive) {
75+
this.caseInsensitive = caseInsensitive;
76+
}
6877
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void visit(JdbcParameter jdbcParameter) {
182182

183183
@Override
184184
public void visit(LikeExpression likeExpression) {
185-
visitBinaryExpression(likeExpression, " LIKE ");
185+
visitBinaryExpression(likeExpression, likeExpression.isCaseInsensitive()?" ILIKE ":" LIKE ");
186186
String escape = likeExpression.getEscape();
187187
if (escape != null) {
188188
buffer.append(" ESCAPE '").append(escape).append('\'');

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
116116
| <K_INTO:"INTO">
117117
| <K_NULL:"NULL">
118118
| <K_LIKE:"LIKE">
119+
| <K_ILIKE:"ILIKE">
119120
| <K_DROP:"DROP">
120121
| <K_JOIN:"JOIN">
121122
| <K_LEFT:"LEFT">
@@ -1454,7 +1455,7 @@ Expression LikeExpression() :
14541455
}
14551456
{
14561457
leftExpression=SimpleExpression()
1457-
[<K_NOT> { result.setNot(true); } ] <K_LIKE> rightExpression=SimpleExpression()
1458+
[<K_NOT> { result.setNot(true); } ] ( <K_LIKE> | <K_ILIKE> { result.setCaseInsensitive(true); } ) rightExpression=SimpleExpression()
14581459
[<K_ESCAPE> token=<S_CHAR_LITERAL> { result.setEscape((new StringValue(token.image)).getValue()); }]
14591460
{
14601461
result.setLeftExpression(leftExpression);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ public void testLike() throws JSQLParserException {
819819
assertEquals("test", ((StringValue) ((LikeExpression) plainSelect.getWhere()).getRightExpression()).getValue());
820820
assertEquals("test2", ((LikeExpression) plainSelect.getWhere()).getEscape());
821821
}
822+
823+
public void testIlike() throws JSQLParserException {
824+
String statement = "SELECT col1 FROM table1 WHERE col1 ILIKE '%hello%'";
825+
assertSqlCanBeParsedAndDeparsed(statement);
826+
}
822827

823828
public void testSelectOrderHaving() throws JSQLParserException {
824829
String statement = "SELECT units, count(units) AS num FROM currency GROUP BY units HAVING count(units) > 1 ORDER BY num";

0 commit comments

Comments
 (0)