Skip to content

Commit e5e488d

Browse files
committed
PostgresSQL regular expression match operators
1 parent 259dd56 commit e5e488d

File tree

8 files changed

+103
-24
lines changed

8 files changed

+103
-24
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ The generated hierarchy can be navigated using the Visitor Pattern.
2222

2323
## Extensions Version 0.8.7
2424

25-
* Added support for PostgreSQL regular expression case sensitive match condition ~.
25+
* Added support for PostgreSQL regular expression match operators.
2626

2727
```sql
28-
SELECT a, b FROM foo WHERE a ~ '[help].*'
28+
SELECT a, b FROM foo WHERE a ~ '[help].*';
29+
SELECT a, b FROM foo WHERE a ~* '[help].*'
30+
SELECT a, b FROM foo WHERE a !~ '[help].*'
31+
SELECT a, b FROM foo WHERE a !~* '[help].*'
2932
```
3033

3134
## Extensions Version 0.8.6

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
4545
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
4646
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
47-
import net.sf.jsqlparser.expression.operators.relational.RegExpCaseSensitiveMatch;
47+
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
4848
import net.sf.jsqlparser.schema.Column;
4949
import net.sf.jsqlparser.statement.select.SubSelect;
5050

@@ -142,5 +142,5 @@ public interface ExpressionVisitor {
142142

143143
void visit(OracleHierarchicalExpression oexpr);
144144

145-
void visit(RegExpCaseSensitiveMatch rexpr);
145+
void visit(RegExpMatchOperator rexpr);
146146
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 RegExpMatchOperator extends BinaryExpression {
28+
29+
private RegExpMatchOperatorType operatorType;
30+
31+
public RegExpMatchOperator(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 "~";
52+
case MATCH_CASEINSENSITIVE:
53+
return "~*";
54+
case NOT_MATCH_CASESENSITIVE:
55+
return "!~";
56+
case NOT_MATCH_CASEINSENSITIVE:
57+
return "!~*";
58+
}
59+
return null;
60+
}
61+
}

src/main/java/net/sf/jsqlparser/expression/operators/relational/RegExpCaseSensitiveMatch.java renamed to src/main/java/net/sf/jsqlparser/expression/operators/relational/RegExpMatchOperatorType.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%
2121
*/
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 RegExpCaseSensitiveMatch extends BinaryExpression {
2822

29-
@Override
30-
public void accept(ExpressionVisitor expressionVisitor) {
31-
expressionVisitor.visit(this);
32-
}
23+
package net.sf.jsqlparser.expression.operators.relational;
3324

34-
@Override
35-
public String getStringExpression() {
36-
return "~";
37-
}
25+
/**
26+
* PostgresSQL match operators.
27+
* @author toben
28+
*/
29+
public enum RegExpMatchOperatorType {
30+
MATCH_CASESENSITIVE,
31+
MATCH_CASEINSENSITIVE,
32+
NOT_MATCH_CASESENSITIVE,
33+
NOT_MATCH_CASEINSENSITIVE
3834
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
7474
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
7575
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
76-
import net.sf.jsqlparser.expression.operators.relational.RegExpCaseSensitiveMatch;
76+
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
7777
import net.sf.jsqlparser.schema.Column;
7878
import net.sf.jsqlparser.schema.Table;
7979
import net.sf.jsqlparser.statement.delete.Delete;
@@ -509,7 +509,7 @@ public void visit(OracleHierarchicalExpression oexpr) {
509509
}
510510

511511
@Override
512-
public void visit(RegExpCaseSensitiveMatch rexpr) {
512+
public void visit(RegExpMatchOperator rexpr) {
513513
visitBinaryExpression(rexpr);
514514
}
515515
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public void visit(OracleHierarchicalExpression oexpr) {
489489
}
490490

491491
@Override
492-
public void visit(RegExpCaseSensitiveMatch rexpr) {
493-
visitBinaryExpression(rexpr, " ~ ");
492+
public void visit(RegExpMatchOperator rexpr) {
493+
visitBinaryExpression(rexpr, " " + rexpr.getStringExpression() + " ");
494494
}
495495
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
9898
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
9999
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
100100
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
101-
import net.sf.jsqlparser.expression.operators.relational.RegExpCaseSensitiveMatch;
101+
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
102+
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperatorType;
102103
import net.sf.jsqlparser.expression.operators.relational.InExpression;
103104
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
104105
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
@@ -1334,7 +1335,10 @@ Expression RegularCondition():
13341335
| "<=" { result = new MinorThanEquals(); }
13351336
| ("<>" | "!=") { result = new NotEqualsTo(); }
13361337
| "@@" { result = new Matches(); }
1337-
| "~" { result = new RegExpCaseSensitiveMatch(); }
1338+
| "~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASESENSITIVE); }
1339+
| "~*" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
1340+
| "!~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.NOT_MATCH_CASESENSITIVE); }
1341+
| "!~*" { result = new RegExpMatchOperator(RegExpMatchOperatorType.NOT_MATCH_CASEINSENSITIVE); }
13381342
)
13391343
rightExpression=ComparisonItem()
13401344

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,4 +1153,19 @@ public void testPostgreSQLRegExpCaseSensitiveMatch() throws JSQLParserException
11531153
String stmt= "SELECT a, b FROM foo WHERE a ~ '[help].*'";
11541154
assertSqlCanBeParsedAndDeparsed(stmt);
11551155
}
1156+
1157+
public void testPostgreSQLRegExpCaseSensitiveMatch2() throws JSQLParserException {
1158+
String stmt= "SELECT a, b FROM foo WHERE a ~* '[help].*'";
1159+
assertSqlCanBeParsedAndDeparsed(stmt);
1160+
}
1161+
1162+
public void testPostgreSQLRegExpCaseSensitiveMatch3() throws JSQLParserException {
1163+
String stmt= "SELECT a, b FROM foo WHERE a !~ '[help].*'";
1164+
assertSqlCanBeParsedAndDeparsed(stmt);
1165+
}
1166+
1167+
public void testPostgreSQLRegExpCaseSensitiveMatch4() throws JSQLParserException {
1168+
String stmt= "SELECT a, b FROM foo WHERE a !~* '[help].*'";
1169+
assertSqlCanBeParsedAndDeparsed(stmt);
1170+
}
11561171
}

0 commit comments

Comments
 (0)