mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved postgresql sql parser support. support '@@' operator. issue #1391
This commit is contained in:
@@ -53,7 +53,11 @@ public enum SQLBinaryOperator {
|
||||
LessThanOrGreater("<>", 110),
|
||||
|
||||
Like("LIKE", 110),
|
||||
NotLike("NOT LIKE", 110),
|
||||
NotLike("NOT LIKE", 110),
|
||||
|
||||
ILike("ILIKE", 110),
|
||||
NotILike("NOT ILIKE", 110),
|
||||
AT_AT("@@", 110), // postgresql textsearch
|
||||
|
||||
RLike("RLIKE", 110),
|
||||
NotRLike("NOT RLIKE", 110),
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import com.alibaba.druid.sql.parser.Keywords;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class PGLexer extends Lexer {
|
||||
|
||||
@@ -65,6 +66,7 @@ public class PGLexer extends Lexer {
|
||||
map.put("ARRAY", Token.ARRAY);
|
||||
map.put("IF", Token.IF);
|
||||
map.put("TYPE", Token.TYPE);
|
||||
map.put("ILIKE", Token.ILIKE);
|
||||
|
||||
DEFAULT_PG_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
@@ -72,6 +74,7 @@ public class PGLexer extends Lexer {
|
||||
public PGLexer(String input){
|
||||
super(input);
|
||||
super.keywods = DEFAULT_PG_KEYWORDS;
|
||||
super.dbType = JdbcConstants.POSTGRESQL;
|
||||
}
|
||||
|
||||
protected void scanString() {
|
||||
|
||||
@@ -39,6 +39,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -84,6 +85,8 @@ public class Lexer {
|
||||
|
||||
protected int lines = 0;
|
||||
|
||||
protected String dbType;
|
||||
|
||||
public Lexer(String input){
|
||||
this(input, null);
|
||||
}
|
||||
@@ -984,6 +987,12 @@ public class Lexer {
|
||||
char ch;
|
||||
|
||||
if (charAt(pos + 1) == '@') {
|
||||
if (JdbcConstants.POSTGRESQL.equalsIgnoreCase(dbType)) {
|
||||
pos += 2;
|
||||
token = Token.MONKEYS_AT_AT;
|
||||
this.ch = charAt(++pos);
|
||||
return;
|
||||
}
|
||||
ch = charAt(++pos);
|
||||
|
||||
bufPos++;
|
||||
|
||||
@@ -1456,6 +1456,20 @@ public class SQLExprParser extends SQLParser {
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RLike, rightExp, getDbType());
|
||||
} else if (lexer.token() == Token.ILIKE) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.ILike, rightExp, getDbType());
|
||||
} else if (lexer.token() == Token.MONKEYS_AT_AT) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.AT_AT, rightExp, getDbType());
|
||||
} else if (lexer.token() == (Token.NOT)) {
|
||||
lexer.nextToken();
|
||||
expr = notRationalRest(expr);
|
||||
@@ -1536,6 +1550,13 @@ public class SQLExprParser extends SQLParser {
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRLike, rightExp, getDbType());
|
||||
} else if (lexer.token() == Token.ILIKE) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotILike, rightExp, getDbType());
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
@@ -169,8 +169,9 @@ public enum Token {
|
||||
RETURNING("RETURNING"),
|
||||
COMMENT("COMMENT"),
|
||||
OVER("OVER"),
|
||||
TYPE("TYPE"),
|
||||
|
||||
TYPE("TYPE"),
|
||||
ILIKE("ILIKE"),
|
||||
|
||||
// oracle
|
||||
START("START"),
|
||||
PRIOR("PRIOR"),
|
||||
@@ -328,6 +329,7 @@ public enum Token {
|
||||
LTLT("<<"),
|
||||
GTGT(">>"),
|
||||
MONKEYS_AT("@"),
|
||||
MONKEYS_AT_AT("@@"),
|
||||
POUND("#"),
|
||||
POUNDGT("#>"),
|
||||
POUNDGTGT("#>>")
|
||||
|
||||
Reference in New Issue
Block a user