druid 1.2.0, improved sql parser.

This commit is contained in:
高铁
2020-10-05 10:29:16 +08:00
parent 2a2fa0fc4b
commit f15e88e2f7
2522 changed files with 173974 additions and 71240 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2017 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.
@@ -15,16 +15,23 @@
*/
package com.alibaba.druid.sql.parser;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.StringUtils;
import java.util.TimeZone;
public class SQLParser {
protected final Lexer lexer;
protected String dbType;
protected DbType dbType;
public SQLParser(String sql, String dbType){
public SQLParser(String sql, DbType dbType, SQLParserFeature... features){
this(new Lexer(sql, null, dbType), dbType);
for (SQLParserFeature feature : features) {
config(feature, true);
}
this.lexer.nextToken();
}
@@ -34,9 +41,12 @@ public class SQLParser {
public SQLParser(Lexer lexer){
this(lexer, null);
if (dbType == null) {
dbType = lexer.dbType;
}
}
public SQLParser(Lexer lexer, String dbType){
public SQLParser(Lexer lexer, DbType dbType){
this.lexer = lexer;
this.dbType = dbType;
}
@@ -45,7 +55,7 @@ public class SQLParser {
return lexer;
}
public String getDbType() {
public DbType getDbType() {
return dbType;
}
@@ -103,6 +113,7 @@ public class SQLParser {
case COMMA:
case WHERE:
case INNER:
case ON:
return ident;
default:
lexer.reset(mark);
@@ -135,8 +146,7 @@ public class SQLParser {
return ident;
} else if (hash == FnvHash.Constants.DISTRIBUTE
|| hash == FnvHash.Constants.SORT
|| hash == FnvHash.Constants.CLUSTER
) {
|| hash == FnvHash.Constants.CLUSTER) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token == Token.BY) {
@@ -148,6 +158,76 @@ public class SQLParser {
}
}
if (!must) {
switch (token) {
case LEFT:
case RIGHT:
case INNER:
case FULL: {
Lexer.SavePoint mark = lexer.mark();
String strVal = lexer.stringVal();
lexer.nextToken();
if (lexer.token == Token.OUTER
|| lexer.token == Token.JOIN
|| lexer.identifierEquals(FnvHash.Constants.ANTI)
|| lexer.identifierEquals(FnvHash.Constants.SEMI)) {
lexer.reset(mark);
break;
} else {
return strVal;
}
}
case IN: {
Lexer.SavePoint mark = lexer.mark();
String strVal = lexer.stringVal();
lexer.nextToken();
switch (lexer.token) {
case WHERE:
case RPAREN: {
return strVal;
}
default:
lexer.reset(mark);
break;
}
break;
}
case FOR:
case GRANT:
if (dbType == DbType.odps){
String strVal = lexer.stringVal();
lexer.nextToken();
return strVal;
}
break;
case TABLE:
if (dbType == DbType.odps){
Lexer.SavePoint mark = lexer.mark();
String strVal = lexer.stringVal();
lexer.nextToken();
switch (lexer.token) {
case FROM:
case GROUP:
return strVal;
default:
lexer.reset(mark);
break;
}
}
break;
case SHOW:
case REPEAT:
case USE:
case OUT: {
String strVal = lexer.stringVal();
lexer.nextToken();
return strVal;
}
default:
break;
}
}
return this.as();
}
@@ -161,7 +241,16 @@ public class SQLParser {
}
if (token == Token.AS) {
lexer.nextToken();
lexer.nextTokenAlias();
if (lexer.token == Token.LPAREN) {
return null;
}
// for oracle
if (dbType == DbType.oracle && (lexer.token == Token.COMMA || lexer.token == Token.FROM)) {
return null;
}
alias = lexer.stringVal();
lexer.nextToken();
@@ -202,9 +291,63 @@ public class SQLParser {
case DO:
case STORE:
case MOD:
case ANY:
case BEGIN:
case CAST:
case COMPUTE:
case ESCAPE:
case FULL:
case MERGE:
case OPEN:
case SOME:
case TRUNCATE:
case UNTIL:
case VIEW:
case KILL:
case COMMENT:
alias = lexer.stringVal();
lexer.nextToken();
break;
case INTERSECT:
case EXCEPT:
case DESC:
case MINUS: {
alias = lexer.stringVal();
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() != Token.COMMA
&& lexer.token() != Token.RPAREN) {
alias = null;
lexer.reset(mark);
}
break;
}
case CHECK:
case INDEX:
case ALL:
case INNER:
case CLOSE:
case VALUES:
if (dbType == DbType.odps) {
alias = lexer.stringVal();
lexer.nextToken();
break;
}
break;
case GROUP:
case ORDER:
if (dbType == DbType.odps) {
Lexer.SavePoint mark = lexer.mark();
alias = lexer.stringVal();
lexer.nextToken();
if (lexer.token == Token.BY) {
lexer.reset(mark);
alias = null;
}
break;
}
break;
default:
break;
}
@@ -221,6 +364,9 @@ public class SQLParser {
break;
}
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && alias != null && alias.length() > 1) {
alias = StringUtils.removeNameQuotes(alias);
}
return alias;
}
@@ -240,7 +386,7 @@ public class SQLParser {
case KEY:
case INDEX:
case CASE:
case MODEL:
// case MODEL:
case PCTFREE:
case INITRANS:
case MAXTRANS:
@@ -304,6 +450,47 @@ public class SQLParser {
case TYPE:
case CLOSE:
case OPEN:
case ANY:
case CAST:
case COMPUTE:
case ESCAPE:
case INTERSECT:
case MERGE:
case MINUS:
case SOME:
case TRUNCATE:
case UNTIL:
case VIEW:
case FUNCTION:
case DESC:
case KILL:
case SHOW:
case NULL:
case ORDER:
case GROUP:
case ALL:
case CONSTRAINT:
case INNER:
case LEFT:
case RIGHT:
case VALUES:
case SCHEMA:
case PARTITION:
case UPDATE:
case DO:
case REPEAT:
case DEFAULT:
case LIKE:
case IS:
case UNIQUE:
case CHECK:
case INOUT:
case DECLARE:
case TABLE:
case TRIGGER:
case IN:
case OUT:
case BY:
alias = lexer.stringVal();
lexer.nextToken();
return alias;
@@ -320,12 +507,27 @@ public class SQLParser {
protected void printError(Token token) {
String arround;
if (lexer.mark >= 0 && (lexer.text.length() > lexer.mark + 30)) {
int begin, end;
if (lexer.mark - 5 > 0) {
arround = lexer.text.substring(lexer.mark - 5, lexer.mark + 30);
begin = lexer.mark - 5;
end = lexer.mark + 30;
} else {
arround = lexer.text.substring(lexer.mark, lexer.mark + 30);
begin = lexer.mark;
end = lexer.mark + 30;
}
if (begin < 10) {
begin = 0;
} else {
for (int i = 1; i < 10 && i < begin; ++i) {
char ch = lexer.text.charAt(begin - i);
if (ch == ' ' || ch == '\n') {
begin = begin - i + 1;
}
}
}
arround = lexer.text.substring(begin, end);
} else if (lexer.mark >= 0) {
if (lexer.mark - 5 > 0) {
arround = lexer.text.substring(lexer.mark - 5);
@@ -340,8 +542,20 @@ public class SQLParser {
// ParserException("syntax error, error arround:'"+arround+"',expect "
// + token + ", actual " + lexer.token + " "
// + lexer.stringVal() + ", pos " + this.lexer.pos());
throw new ParserException("syntax error, error in :'" + arround + "', expect " + token + ", actual "
+ lexer.token + " " + lexer.info());
StringBuilder buf = new StringBuilder()
.append("syntax error, error in :'")
.append(arround);
if (token != lexer.token) {
buf.append("', expect ")
.append(token.name)
.append(", actual ")
.append(lexer.token.name);
}
buf.append(", ")
.append(
lexer.info());
throw new ParserException(buf.toString());
}
public void accept(Token token) {
@@ -383,11 +597,20 @@ public class SQLParser {
this.lexer.config(feature, state);
}
public TimeZone getTimeZone() {
return lexer.getTimeZone();
}
public void setTimeZone(TimeZone timeZone) {
this.lexer.setTimeZone(timeZone);
}
public final boolean isEnabled(SQLParserFeature feature) {
return lexer.isEnabled(feature);
}
protected SQLCreateTableStatement newCreateStatement() {
return new SQLCreateTableStatement(getDbType());
return new SQLCreateTableStatement(dbType);
}
}