Skip to content

Commit

Permalink
fixes #887
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Jan 2, 2021
1 parent 085e120 commit 74aab7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@ public class RegExpMySQLOperator extends BinaryExpression {

private RegExpMatchOperatorType operatorType;
private boolean useRLike = false;
private boolean not = false;

public RegExpMySQLOperator(RegExpMatchOperatorType operatorType) {
this(false, operatorType);
}

public RegExpMySQLOperator(boolean not, RegExpMatchOperatorType operatorType) {
if (operatorType == null) {
throw new NullPointerException();
}
this.operatorType = operatorType;
this.not = not;
}

public boolean isNot() {
return not;
}

public void setNot(boolean not) {
this.not = not;
}

public RegExpMatchOperatorType getOperatorType() {
Expand All @@ -45,7 +59,8 @@ public void accept(ExpressionVisitor expressionVisitor) {

@Override
public String getStringExpression() {
return (useRLike ? "RLIKE" : "REGEXP")
return (not?"NOT ":"")
+ (useRLike ? "RLIKE" : "REGEXP")
+ (operatorType == RegExpMatchOperatorType.MATCH_CASESENSITIVE ? " BINARY" : "");
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,7 @@ Expression RegularCondition() #RegularCondition:
int oracleJoin=EqualsTo.NO_ORACLE_JOIN;
int oraclePrior=EqualsTo.NO_ORACLE_PRIOR;
boolean binary = false;
boolean not = false;
}
{
[ LOOKAHEAD(2) <K_PRIOR> { oraclePrior = EqualsTo.ORACLE_PRIOR_START; }]
Expand All @@ -2647,7 +2648,7 @@ Expression RegularCondition() #RegularCondition:
| token=<OP_NOTEQUALSBANG> { result = new NotEqualsTo(token.image); }
| "@@" { result = new Matches(); }
| "~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASESENSITIVE); }
| <K_REGEXP> [ <K_BINARY> { binary=true; } ] { result = new RegExpMySQLOperator(binary?RegExpMatchOperatorType.MATCH_CASESENSITIVE:RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
| [<K_NOT> { not=true; } ] <K_REGEXP> [ <K_BINARY> { binary=true; } ] { result = new RegExpMySQLOperator(not, binary?RegExpMatchOperatorType.MATCH_CASESENSITIVE:RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
| <K_RLIKE> [ <K_BINARY> { binary=true; } ] { result = new RegExpMySQLOperator(binary?RegExpMatchOperatorType.MATCH_CASESENSITIVE:RegExpMatchOperatorType.MATCH_CASEINSENSITIVE).useRLike(); }
| "~*" { result = new RegExpMatchOperator(RegExpMatchOperatorType.MATCH_CASEINSENSITIVE); }
| "!~" { result = new RegExpMatchOperator(RegExpMatchOperatorType.NOT_MATCH_CASESENSITIVE); }
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,16 @@ public void testRegexpMySQL() throws JSQLParserException {
String stmt = "SELECT * FROM mytable WHERE first_name REGEXP '^Ste(v|ph)en$'";
assertSqlCanBeParsedAndDeparsed(stmt);
}

@Test
public void testNotRegexpMySQLIssue887() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE first_name NOT REGEXP '^Ste(v|ph)en$'");
}

@Test
public void testNotRegexpMySQLIssue887_2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM mytable WHERE NOT first_name REGEXP '^Ste(v|ph)en$'");
}

@Test
public void testRegexpBinaryMySQL() throws JSQLParserException {
Expand Down

0 comments on commit 74aab7c

Please sign in to comment.