Skip to content

Commit 332d1e7

Browse files
committed
Merge pull request #60 from skomlaebri/feature/MySQLRegex
Support for REGEXP [BINARY] operators
2 parents cb674bf + 75aeded commit 332d1e7

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
4646
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
4747
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
48+
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
4849
import net.sf.jsqlparser.schema.Column;
4950
import net.sf.jsqlparser.statement.select.SubSelect;
5051

@@ -145,4 +146,6 @@ public interface ExpressionVisitor {
145146
void visit(RegExpMatchOperator rexpr);
146147

147148
void visit(JsonExpression jsonExpr);
149+
150+
void visit(RegExpMySQLOperator regExpMySQLOperator);
148151
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,9 @@ protected void visitBinaryExpression(BinaryExpression expr) {
307307
public void visit(JsonExpression jsonExpr) {
308308
visit(jsonExpr.getColumn());
309309
}
310+
311+
@Override
312+
public void visit(RegExpMySQLOperator expr) {
313+
visitBinaryExpression(expr);
314+
}
310315
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.expression.operators.relational;
23+
24+
import net.sf.jsqlparser.expression.BinaryExpression;
25+
import net.sf.jsqlparser.expression.ExpressionVisitor;
26+
27+
public class RegExpMySQLOperator extends BinaryExpression {
28+
29+
private RegExpMatchOperatorType operatorType;
30+
31+
public RegExpMySQLOperator(RegExpMatchOperatorType operatorType) {
32+
if (operatorType == null) {
33+
throw new NullPointerException();
34+
}
35+
this.operatorType = operatorType;
36+
}
37+
38+
public RegExpMatchOperatorType getOperatorType() {
39+
return operatorType;
40+
}
41+
42+
@Override
43+
public void accept(ExpressionVisitor expressionVisitor) {
44+
expressionVisitor.visit(this);
45+
}
46+
47+
@Override
48+
public String getStringExpression() {
49+
switch (operatorType) {
50+
case MATCH_CASESENSITIVE:
51+
return "REGEXP BINARY";
52+
case MATCH_CASEINSENSITIVE:
53+
return "REGEXP";
54+
default:
55+
}
56+
return null;
57+
}
58+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,14 @@ public void visit(RegExpMatchOperator rexpr) {
462462
visitBinaryExpression(rexpr);
463463
}
464464

465+
@Override
466+
public void visit(RegExpMySQLOperator rexpr) {
467+
visitBinaryExpression(rexpr);
468+
}
469+
465470
@Override
466471
public void visit(JsonExpression jsonExpr) {
467472
}
473+
474+
468475
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,15 @@ public void visit(RegExpMatchOperator rexpr) {
501501
visitBinaryExpression(rexpr, " " + rexpr.getStringExpression() + " ");
502502
}
503503

504+
@Override
505+
public void visit(RegExpMySQLOperator rexpr) {
506+
visitBinaryExpression(rexpr, " " + rexpr.getStringExpression() + " ");
507+
}
508+
504509
@Override
505510
public void visit(JsonExpression jsonExpr) {
506511
buffer.append(jsonExpr.toString());
507512
}
513+
514+
508515
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
195195
| <K_CURRENT: "CURRENT">
196196
| <K_ROW: "ROW">
197197
| <K_RETURNING: "RETURNING">
198+
| <K_BINARY: "BINARY">
199+
| <K_REGEXP: "REGEXP">
198200
}
199201

200202
TOKEN : /* Numeric Constants */
@@ -1278,6 +1280,8 @@ Expression RegularCondition():
12781280
| "!=" { result = new NotEqualsTo("!="); }
12791281
| "@@" { result = new Matches(); }
12801282
| "~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASESENSITIVE); }
1283+
| LOOKAHEAD(2) <K_REGEXP> <K_BINARY> { result = new RegExpMySQLOperator(RegExpMatchOperatorType.MATCH_CASESENSITIVE); }
1284+
| <K_REGEXP> { result = new RegExpMySQLOperator(RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
12811285
| "~*" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
12821286
| "!~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.NOT_MATCH_CASESENSITIVE); }
12831287
| "!~*" { result = new RegExpMatchOperator(RegExpMatchOperatorType.NOT_MATCH_CASEINSENSITIVE); }

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,16 @@ public void testRegexpLike2() throws JSQLParserException {
13251325
String stmt = "SELECT CASE WHEN REGEXP_LIKE(first_name, '^Ste(v|ph)en$') THEN 1 ELSE 2 END FROM mytable";
13261326
assertSqlCanBeParsedAndDeparsed(stmt);
13271327
}
1328+
1329+
public void testRegexpMySQL() throws JSQLParserException {
1330+
String stmt = "SELECT * FROM mytable WHERE first_name REGEXP '^Ste(v|ph)en$'";
1331+
assertSqlCanBeParsedAndDeparsed(stmt);
1332+
}
1333+
1334+
public void testRegexpBinaryMySQL() throws JSQLParserException {
1335+
String stmt = "SELECT * FROM mytable WHERE first_name REGEXP BINARY '^Ste(v|ph)en$'";
1336+
assertSqlCanBeParsedAndDeparsed(stmt);
1337+
}
13281338

13291339
public void testBooleanFunction1() throws JSQLParserException {
13301340
String stmt = "SELECT * FROM mytable WHERE test_func(col1)";

0 commit comments

Comments
 (0)