Skip to content

Commit

Permalink
improved postgresql sql parser support. support '@@' operator. issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 18, 2016
1 parent 2a64573 commit c3681f1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
Expand Up @@ -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),
Expand Down
Expand Up @@ -23,6 +23,7 @@
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 {

Expand Down Expand Up @@ -65,13 +66,15 @@ 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);
}

public PGLexer(String input){
super(input);
super.keywods = DEFAULT_PG_KEYWORDS;
super.dbType = JdbcConstants.POSTGRESQL;
}

protected void scanString() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/alibaba/druid/sql/parser/Lexer.java
Expand Up @@ -39,6 +39,7 @@
import java.util.Arrays;
import java.util.List;

import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.StringUtils;

/**
Expand Down Expand Up @@ -84,6 +85,8 @@ public class Lexer {

protected int lines = 0;

protected String dbType;

public Lexer(String input){
this(input, null);
}
Expand Down Expand Up @@ -984,6 +987,12 @@ public void scanVariable() {
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++;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java
Expand Up @@ -1456,6 +1456,20 @@ public SQLExpr relationalRest(SQLExpr expr) {
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);
Expand Down Expand Up @@ -1536,6 +1550,13 @@ public SQLExpr notRationalRest(SQLExpr expr) {
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());
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/alibaba/druid/sql/parser/Token.java
Expand Up @@ -169,8 +169,9 @@ public enum Token {
RETURNING("RETURNING"),
COMMENT("COMMENT"),
OVER("OVER"),
TYPE("TYPE"),

TYPE("TYPE"),
ILIKE("ILIKE"),

// oracle
START("START"),
PRIOR("PRIOR"),
Expand Down Expand Up @@ -328,6 +329,7 @@ public enum Token {
LTLT("<<"),
GTGT(">>"),
MONKEYS_AT("@"),
MONKEYS_AT_AT("@@"),
POUND("#"),
POUNDGT("#>"),
POUNDGTGT("#>>")
Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.postgresql;

import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import org.junit.Assert;

import java.util.List;

public class PGSelectTest30 extends PGTest {

public void test_0() throws Exception {
String sql = "SELECT 'a fat cat sat on a mat'::tsvector @@ 'cat & rat'::tsquery";

PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);

Assert.assertEquals("SELECT 'a fat cat sat on a mat'::tsvector @@ 'cat & rat'::tsquery", SQLUtils.toPGString(stmt));

Assert.assertEquals("select 'a fat cat sat on a mat'::tsvector @@ 'cat & rat'::tsquery", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));

Assert.assertEquals(1, statementList.size());

PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);

// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());

Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getTables().size());
}
}

0 comments on commit c3681f1

Please sign in to comment.