mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved parser performance.
This commit is contained in:
-4
@@ -117,10 +117,6 @@ public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlO
|
||||
return procedureArgumentList;
|
||||
}
|
||||
|
||||
public void setProcedureArgumentList(List<SQLExpr> procedureArgumentList) {
|
||||
this.procedureArgumentList = procedureArgumentList;
|
||||
}
|
||||
|
||||
public boolean isHignPriority() {
|
||||
return hignPriority;
|
||||
}
|
||||
|
||||
@@ -52,21 +52,32 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class MySqlExprParser extends SQLExprParser {
|
||||
import java.util.Arrays;
|
||||
|
||||
public static String[] AGGREGATE_FUNCTIONS = { "AVG", "COUNT", "GROUP_CONCAT", "MAX", "MIN", "STDDEV", "SUM" };
|
||||
public class MySqlExprParser extends SQLExprParser {
|
||||
public final static String[] AGGREGATE_FUNCTIONS;
|
||||
|
||||
public final static long[] AGGREGATE_FUNCTIONS_CODES;
|
||||
|
||||
static {
|
||||
String[] strings = { "AVG", "COUNT", "GROUP_CONCAT", "MAX", "MIN", "STDDEV", "SUM" };
|
||||
AGGREGATE_FUNCTIONS_CODES = FNVUtils.fnv_64_lower(strings, true);
|
||||
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
|
||||
for (String str : strings) {
|
||||
long hash = FNVUtils.fnv_64_lower(str);
|
||||
int index = Arrays.binarySearch(AGGREGATE_FUNCTIONS_CODES, hash);
|
||||
AGGREGATE_FUNCTIONS[index] = str;
|
||||
}
|
||||
}
|
||||
|
||||
public MySqlExprParser(Lexer lexer){
|
||||
super(lexer, JdbcConstants.MYSQL);
|
||||
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
|
||||
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
|
||||
}
|
||||
|
||||
public MySqlExprParser(String sql){
|
||||
@@ -74,6 +85,11 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public MySqlExprParser(String sql, SQLParserFeature... features){
|
||||
this(new MySqlLexer(sql, features));
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public MySqlExprParser(String sql, boolean keepComments){
|
||||
this(new MySqlLexer(sql, true, keepComments));
|
||||
this.lexer.nextToken();
|
||||
@@ -85,51 +101,6 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public SQLExpr relationalRest(SQLExpr expr) {
|
||||
if (lexer.identifierEquals(FNVUtils.REGEXP)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
return super.relationalRest(expr);
|
||||
}
|
||||
|
||||
public SQLExpr multiplicativeRest(SQLExpr expr) {
|
||||
final Token token = lexer.token();
|
||||
if (token == Token.IDENTIFIER && "MOD".equalsIgnoreCase(lexer.stringVal())) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp, JdbcConstants.MYSQL);
|
||||
} else if (token == Token.DIV) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = bitXor();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.DIV, rightExp, getDbType());
|
||||
expr = multiplicativeRest(expr);
|
||||
}
|
||||
|
||||
return super.multiplicativeRest(expr);
|
||||
}
|
||||
|
||||
public SQLExpr notRationalRest(SQLExpr expr) {
|
||||
if (lexer.identifierEquals("REGEXP")) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
return super.notRationalRest(expr);
|
||||
}
|
||||
|
||||
public SQLExpr primary() {
|
||||
final Token tok = lexer.token();
|
||||
|
||||
@@ -143,10 +114,6 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
}
|
||||
|
||||
switch (tok) {
|
||||
case LITERAL_ALIAS:
|
||||
String aliasValue = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
return primaryRest(new SQLCharExpr(aliasValue));
|
||||
case VARIANT:
|
||||
SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
@@ -176,11 +143,6 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
SQLUnaryExpr binaryExpr = new SQLUnaryExpr(SQLUnaryOperator.BINARY, expr());
|
||||
return primaryRest(binaryExpr);
|
||||
}
|
||||
case CACHE:
|
||||
case GROUP:
|
||||
String strVal = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
return primaryRest(new SQLIdentifierExpr(strVal));
|
||||
default:
|
||||
return super.primary();
|
||||
}
|
||||
@@ -215,7 +177,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
|
||||
MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
|
||||
mysqlCharExpr.setCharset(identExpr.getName());
|
||||
if (lexer.identifierEquals("COLLATE")) {
|
||||
if (lexer.identifierEquals(FNVUtils.COLLATE)) {
|
||||
lexer.nextToken();
|
||||
|
||||
String collate = lexer.stringVal();
|
||||
@@ -290,181 +252,14 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
|
||||
String ident = identExpr.getName();
|
||||
|
||||
if ("EXTRACT".equalsIgnoreCase(ident)) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
|
||||
String unitVal = lexer.stringVal();
|
||||
MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
|
||||
lexer.nextToken();
|
||||
|
||||
accept(Token.FROM);
|
||||
|
||||
SQLExpr value = expr();
|
||||
|
||||
MySqlExtractExpr extract = new MySqlExtractExpr();
|
||||
extract.setValue(value);
|
||||
extract.setUnit(unit);
|
||||
accept(Token.RPAREN);
|
||||
|
||||
expr = extract;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if ("SUBSTRING".equalsIgnoreCase(ident)) {
|
||||
lexer.nextToken();
|
||||
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
|
||||
for (;;) {
|
||||
SQLExpr param = expr();
|
||||
methodInvokeExpr.addParameter(param);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
} else if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
SQLExpr from = expr();
|
||||
methodInvokeExpr.addParameter(from);
|
||||
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr forExpr = expr();
|
||||
methodInvokeExpr.addParameter(forExpr);
|
||||
}
|
||||
break;
|
||||
} else if (lexer.token() == Token.RPAREN) {
|
||||
break;
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
expr = methodInvokeExpr;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if ("TRIM".equalsIgnoreCase(ident)) {
|
||||
lexer.nextToken();
|
||||
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
String flagVal = lexer.stringVal();
|
||||
if ("LEADING".equalsIgnoreCase(flagVal)) {
|
||||
lexer.nextToken();
|
||||
methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
|
||||
} else if ("BOTH".equalsIgnoreCase(flagVal)) {
|
||||
lexer.nextToken();
|
||||
methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
|
||||
} else if ("TRAILING".equalsIgnoreCase(flagVal)) {
|
||||
lexer.nextToken();
|
||||
methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
|
||||
}
|
||||
}
|
||||
|
||||
SQLExpr param = expr();
|
||||
methodInvokeExpr.addParameter(param);
|
||||
|
||||
if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
SQLExpr from = expr();
|
||||
methodInvokeExpr.putAttribute("FROM", from);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
expr = methodInvokeExpr;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if ("MATCH".equalsIgnoreCase(ident)) {
|
||||
lexer.nextToken();
|
||||
MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
|
||||
|
||||
if (lexer.token() == Token.RPAREN) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
acceptIdentifier("AGAINST");
|
||||
|
||||
accept(Token.LPAREN);
|
||||
SQLExpr against = primary();
|
||||
matchAgainstExpr.setAgainst(against);
|
||||
|
||||
if (lexer.token() == Token.IN) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals("NATURAL")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("LANGUAGE");
|
||||
acceptIdentifier("MODE");
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("QUERY");
|
||||
acceptIdentifier("EXPANSION");
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
|
||||
} else {
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
|
||||
}
|
||||
} else if (lexer.identifierEquals("BOOLEAN")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("MODE");
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
} else if (lexer.token() == Token.WITH) {
|
||||
throw new ParserException("TODO. " + lexer.info());
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
expr = matchAgainstExpr;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if (("CONVERT".equalsIgnoreCase(ident))||("CHAR".equalsIgnoreCase(ident))) {
|
||||
lexer.nextToken();
|
||||
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
|
||||
|
||||
if (lexer.token() != Token.RPAREN) {
|
||||
exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FNVUtils.USING)) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
String charset = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
methodInvokeExpr.putAttribute("USING", charset);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
expr = methodInvokeExpr;
|
||||
|
||||
return primaryRest(expr);
|
||||
} else if ("POSITION".equalsIgnoreCase(ident)) {
|
||||
accept(Token.LPAREN);
|
||||
SQLExpr subStr = this.primary();
|
||||
accept(Token.IN);
|
||||
SQLExpr str = this.expr();
|
||||
accept(Token.RPAREN);
|
||||
|
||||
SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
|
||||
locate.addParameter(subStr);
|
||||
locate.addParameter(str);
|
||||
|
||||
expr = locate;
|
||||
return primaryRest(expr);
|
||||
}
|
||||
}
|
||||
// if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
|
||||
// SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
|
||||
// String ident = identExpr.getName();
|
||||
//
|
||||
// if ("POSITION".equalsIgnoreCase(ident)) {
|
||||
// return parsePosition();
|
||||
// }
|
||||
// }
|
||||
|
||||
if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
|
||||
lexer.nextToken();
|
||||
@@ -491,6 +286,91 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
protected SQLExpr parsePosition() {
|
||||
|
||||
SQLExpr subStr = this.primary();
|
||||
accept(Token.IN);
|
||||
SQLExpr str = this.expr();
|
||||
accept(Token.RPAREN);
|
||||
|
||||
SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
|
||||
locate.addParameter(subStr);
|
||||
locate.addParameter(str);
|
||||
|
||||
return primaryRest(locate);
|
||||
}
|
||||
|
||||
protected SQLExpr parseExtract() {
|
||||
SQLExpr expr;
|
||||
if (lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
|
||||
String unitVal = lexer.stringVal();
|
||||
MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
|
||||
lexer.nextToken();
|
||||
|
||||
accept(Token.FROM);
|
||||
|
||||
SQLExpr value = expr();
|
||||
|
||||
MySqlExtractExpr extract = new MySqlExtractExpr();
|
||||
extract.setValue(value);
|
||||
extract.setUnit(unit);
|
||||
accept(Token.RPAREN);
|
||||
|
||||
expr = extract;
|
||||
|
||||
return primaryRest(expr);
|
||||
}
|
||||
|
||||
protected SQLExpr parseMatch() {
|
||||
|
||||
MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
|
||||
|
||||
if (lexer.token() == Token.RPAREN) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
acceptIdentifier("AGAINST");
|
||||
|
||||
accept(Token.LPAREN);
|
||||
SQLExpr against = primary();
|
||||
matchAgainstExpr.setAgainst(against);
|
||||
|
||||
if (lexer.token() == Token.IN) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals(FNVUtils.NATURAL)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("LANGUAGE");
|
||||
acceptIdentifier("MODE");
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("QUERY");
|
||||
acceptIdentifier("EXPANSION");
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
|
||||
} else {
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
|
||||
}
|
||||
} else if (lexer.identifierEquals(FNVUtils.BOOLEAN)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("MODE");
|
||||
matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
|
||||
} else {
|
||||
throw new ParserException("syntax error. " + lexer.info());
|
||||
}
|
||||
} else if (lexer.token() == Token.WITH) {
|
||||
throw new ParserException("TODO. " + lexer.info());
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return primaryRest(matchAgainstExpr);
|
||||
}
|
||||
|
||||
public SQLSelectParser createSelectParser() {
|
||||
return new MySqlSelectParser(this);
|
||||
}
|
||||
@@ -557,12 +437,12 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
SQLExpr expr = this.expr();
|
||||
column.setOnUpdate(expr);
|
||||
}
|
||||
if (lexer.identifierEquals("CHARSET")) {
|
||||
if (lexer.identifierEquals(FNVUtils.CHARSET)) {
|
||||
lexer.nextToken();
|
||||
MySqlCharExpr charSetCollateExpr=new MySqlCharExpr();
|
||||
charSetCollateExpr.setCharset(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals("COLLATE")) {
|
||||
if (lexer.identifierEquals(FNVUtils.COLLATE)) {
|
||||
lexer.nextToken();
|
||||
charSetCollateExpr.setCollate(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
@@ -629,45 +509,6 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public SQLExpr orRest(SQLExpr expr) {
|
||||
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.OR || lexer.token() == Token.BARBAR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp, JdbcConstants.MYSQL);
|
||||
} else if (lexer.token() == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, JdbcConstants.MYSQL);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr additiveRest(SQLExpr expr) {
|
||||
if (lexer.token() == Token.PLUS) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = multiplicative();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp, JdbcConstants.MYSQL);
|
||||
expr = additiveRest(expr);
|
||||
} else if (lexer.token() == Token.SUB) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = multiplicative();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp, JdbcConstants.MYSQL);
|
||||
expr = additiveRest(expr);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLAssignItem parseAssignItem() {
|
||||
SQLAssignItem item = new SQLAssignItem();
|
||||
|
||||
@@ -696,12 +537,12 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
SQLExpr varExpr = null;
|
||||
boolean chars = false;
|
||||
final Token token = lexer.token();
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
if (token == Token.IDENTIFIER) {
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DEFAULT) {
|
||||
} else if (token == Token.DEFAULT) {
|
||||
charset = "DEFAULT";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.QUES) {
|
||||
} else if (token == Token.QUES) {
|
||||
varExpr = new SQLVariantRefExpr("?");
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
@@ -780,7 +621,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
|
||||
MySqlPrimaryKey primaryKey = new MySqlPrimaryKey();
|
||||
|
||||
if (lexer.identifierEquals("USING")) {
|
||||
if (lexer.identifierEquals(FNVUtils.USING)) {
|
||||
lexer.nextToken();
|
||||
primaryKey.setIndexType(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
@@ -824,7 +665,7 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
}
|
||||
|
||||
//5.5语法 USING BTREE 放在index 名字后
|
||||
if (lexer.identifierEquals("USING")) {
|
||||
if (lexer.identifierEquals(FNVUtils.USING)) {
|
||||
lexer.nextToken();
|
||||
unique.setIndexType(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -70,6 +70,13 @@ public class MySqlLexer extends Lexer {
|
||||
this(input, true, true);
|
||||
}
|
||||
|
||||
public MySqlLexer(String input, SQLParserFeature... features){
|
||||
this(input, true, true);
|
||||
for (SQLParserFeature feature : features) {
|
||||
config(feature, true);
|
||||
}
|
||||
}
|
||||
|
||||
public MySqlLexer(String input, boolean skipComment, boolean keepComments){
|
||||
super(input, skipComment);
|
||||
this.skipComment = skipComment;
|
||||
@@ -243,24 +250,15 @@ public class MySqlLexer extends Lexer {
|
||||
mark = pos;
|
||||
bufPos = 1;
|
||||
char ch;
|
||||
for (;;) {
|
||||
ch = charAt(++pos);
|
||||
|
||||
if (ch == '`') {
|
||||
bufPos++;
|
||||
ch = charAt(++pos);
|
||||
break;
|
||||
} else if (ch == EOI) {
|
||||
throw new ParserException("illegal identifier. " + info());
|
||||
}
|
||||
|
||||
bufPos++;
|
||||
continue;
|
||||
int quoteIndex = text.indexOf('`', pos + 1);
|
||||
if (quoteIndex == -1) {
|
||||
throw new ParserException("illegal identifier. " + info());
|
||||
}
|
||||
|
||||
pos = quoteIndex + 1;
|
||||
this.ch = charAt(pos);
|
||||
|
||||
stringVal = subString(mark, bufPos);
|
||||
stringVal = text.substring(mark, pos);
|
||||
Token tok = keywods.getKeyword(stringVal);
|
||||
if (tok != null) {
|
||||
token = tok;
|
||||
@@ -305,6 +303,15 @@ public class MySqlLexer extends Lexer {
|
||||
|
||||
this.ch = charAt(pos);
|
||||
|
||||
if (bufPos == 1) {
|
||||
token = Token.IDENTIFIER;
|
||||
stringVal = CharTypes.valueOf(first);
|
||||
if (stringVal == null) {
|
||||
stringVal = Character.toString(first);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Token tok = keywods.getKeyword(hash_lower);
|
||||
if (tok != null) {
|
||||
token = tok;
|
||||
@@ -362,37 +369,38 @@ public class MySqlLexer extends Lexer {
|
||||
bufPos++;
|
||||
}
|
||||
|
||||
int starIndex = pos;
|
||||
|
||||
for (;;) {
|
||||
if (ch == EOI) {
|
||||
starIndex = text.indexOf('*', starIndex);
|
||||
if (starIndex == -1 || starIndex == text.length() - 1) {
|
||||
this.token = Token.ERROR;
|
||||
return;
|
||||
}
|
||||
if (ch == '*' && charAt(pos + 1) == '/') {
|
||||
bufPos += 3;
|
||||
scanChar();
|
||||
scanChar();
|
||||
if (charAt(starIndex + 1) == '/') {
|
||||
if (isHint) {
|
||||
//stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 2);
|
||||
stringVal = this.subString(mark + startHintSp, starIndex - startHintSp - mark);
|
||||
token = Token.HINT;
|
||||
} else {
|
||||
stringVal = this.subString(mark, starIndex + 2 - mark);
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
commentCount++;
|
||||
if (keepComments) {
|
||||
addComment(stringVal);
|
||||
}
|
||||
}
|
||||
pos = starIndex + 2;
|
||||
ch = charAt(pos);
|
||||
break;
|
||||
}
|
||||
|
||||
scanChar();
|
||||
bufPos++;
|
||||
}
|
||||
|
||||
if (isHint) {
|
||||
stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 2);
|
||||
token = Token.HINT;
|
||||
} else {
|
||||
stringVal = subString(mark, bufPos);
|
||||
token = Token.MULTI_LINE_COMMENT;
|
||||
commentCount++;
|
||||
if (keepComments) {
|
||||
addComment(stringVal);
|
||||
}
|
||||
starIndex++;
|
||||
}
|
||||
|
||||
endOfComment = isEOF();
|
||||
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
if (commentHandler != null
|
||||
&& commentHandler.handle(lastToken, stringVal)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -402,6 +410,7 @@ public class MySqlLexer extends Lexer {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (ch == '/' || ch == '-') {
|
||||
scanChar();
|
||||
bufPos++;
|
||||
|
||||
@@ -77,7 +77,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
if (lexer.token() == (Token.LPAREN)) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectQuery select = query();
|
||||
@@ -184,10 +184,10 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
queryBlock.setForUpdate(true);
|
||||
|
||||
if (lexer.identifierEquals("NO_WAIT") || lexer.identifierEquals("NOWAIT")) {
|
||||
if (lexer.identifierEquals(FNVUtils.NO_WAIT) || lexer.identifierEquals(FNVUtils.NOWAIT)) {
|
||||
lexer.nextToken();
|
||||
queryBlock.setNoWait(true);
|
||||
} else if (lexer.identifierEquals("WAIT")) {
|
||||
} else if (lexer.identifierEquals(FNVUtils.WAIT)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr waitTime = this.exprParser.primary();
|
||||
queryBlock.setWaitTime(waitTime);
|
||||
@@ -259,32 +259,32 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.identifierEquals("LOW_PRIORITY")) {
|
||||
if (lexer.identifierEquals(FNVUtils.LOW_PRIORITY)) {
|
||||
lexer.nextToken();
|
||||
update.setLowPriority(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("IGNORE")) {
|
||||
if (lexer.identifierEquals(FNVUtils.IGNORE)) {
|
||||
lexer.nextToken();
|
||||
update.setIgnore(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("COMMIT_ON_SUCCESS")) {
|
||||
if (lexer.identifierEquals(FNVUtils.COMMIT_ON_SUCCESS)) {
|
||||
lexer.nextToken();
|
||||
update.setCommitOnSuccess(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("ROLLBACK_ON_FAIL")) {
|
||||
if (lexer.identifierEquals(FNVUtils.ROLLBACK_ON_FAIL)) {
|
||||
lexer.nextToken();
|
||||
update.setRollBackOnFail(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("QUEUE_ON_PK")) {
|
||||
if (lexer.identifierEquals(FNVUtils.QUEUE_ON_PK)) {
|
||||
lexer.nextToken();
|
||||
update.setQueryOnPk(true);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("TARGET_AFFECT_ROW")) {
|
||||
if (lexer.identifierEquals(FNVUtils.TARGET_AFFECT_ROW)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr targetAffectRow = this.exprParser.expr();
|
||||
update.setTargetAffectRow(targetAffectRow);
|
||||
|
||||
+218
-216
File diff suppressed because it is too large
Load Diff
@@ -78,7 +78,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
public void configFromProperty(Properties properties) {
|
||||
if (isEnabled(VisitorFeature.OutputParameterized)) {
|
||||
if (this.parameterized) {
|
||||
String property = properties.getProperty("druid.parameterized.shardingSupport");
|
||||
if ("true".equals(property)) {
|
||||
this.setShardingSupport(true);
|
||||
@@ -89,7 +89,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
public boolean isShardingSupport() {
|
||||
return isEnabled(VisitorFeature.OutputParameterized)
|
||||
return this.parameterized
|
||||
&& shardingSupport;
|
||||
}
|
||||
|
||||
@@ -174,18 +174,19 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
into.accept(this);
|
||||
}
|
||||
|
||||
SQLTableSource form = x.getFrom();
|
||||
if (form != null) {
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from != null) {
|
||||
println();
|
||||
print0(ucase ? "FROM " : "from ");
|
||||
form.accept(this);
|
||||
|
||||
printTableSource(from);
|
||||
}
|
||||
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
where.accept(this);
|
||||
printExpr(where);
|
||||
}
|
||||
|
||||
printHierarchical(x);
|
||||
@@ -193,7 +194,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
SQLSelectGroupByClause groupBy = x.getGroupBy();
|
||||
if (groupBy != null) {
|
||||
println();
|
||||
groupBy.accept(this);
|
||||
visit(groupBy);
|
||||
}
|
||||
|
||||
SQLOrderBy orderBy = x.getOrderBy();
|
||||
@@ -243,6 +244,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
public boolean visit(SQLColumnDefinition x) {
|
||||
boolean parameterized = this.parameterized;
|
||||
this.parameterized = false;
|
||||
|
||||
x.getName().accept(this);
|
||||
|
||||
SQLDataType dataType = x.getDataType();
|
||||
@@ -300,6 +304,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? " SORTED" : " sorted");
|
||||
}
|
||||
|
||||
this.parameterized = parameterized;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -572,8 +577,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.isEnabled(VisitorFeature.OutputParameterized)
|
||||
&& ParameterizedOutputVisitorUtils.checkParameterize(x)) {
|
||||
if (this.parameterized) {
|
||||
this.appender.append('?');
|
||||
incrementReplaceCunt();
|
||||
if (this instanceof ExportParameterVisitor || this.parameters != null) {
|
||||
@@ -679,77 +683,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
SQLExpr owner = x.getOwner();
|
||||
if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
SQLExpr from = (SQLExpr) x.getAttribute("FROM");
|
||||
if (from != null) {
|
||||
print0(ucase ? " FROM " : " from ");
|
||||
from.accept(this);
|
||||
}
|
||||
|
||||
SQLExpr forExpr = (SQLExpr) x.getAttribute("FOR");
|
||||
if (forExpr != null) {
|
||||
print0(ucase ? " FOR " : " for ");
|
||||
forExpr.accept(this);
|
||||
}
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("TRIM".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
|
||||
String trimType = (String) x.getAttribute("TRIM_TYPE");
|
||||
if (trimType != null) {
|
||||
print0(trimType);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
|
||||
SQLExpr from = (SQLExpr) x.getAttribute("FROM");
|
||||
if (from != null) {
|
||||
print0(ucase ? " FROM " : " from ");
|
||||
from.accept(this);
|
||||
}
|
||||
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (("CONVERT".equalsIgnoreCase(x.getMethodName())) || "CHAR".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
|
||||
String charset = (String) x.getAttribute("USING");
|
||||
if (charset != null) {
|
||||
print0(ucase ? " USING " : " using ");
|
||||
print0(charset);
|
||||
}
|
||||
print(')');
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlIntervalExpr x) {
|
||||
|
||||
@@ -999,7 +932,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
protected void printValuesList(List<SQLInsertStatement.ValuesClause> valuesList) {
|
||||
|
||||
if (isEnabled(VisitorFeature.OutputParameterized)) {
|
||||
if (this.parameterized) {
|
||||
print0(ucase ? "VALUES " : "values ");
|
||||
incrementIndent();
|
||||
valuesList.get(0).accept(this);
|
||||
@@ -2765,8 +2698,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlCharExpr x) {
|
||||
if (isEnabled(VisitorFeature.OutputParameterized)
|
||||
&& ParameterizedOutputVisitorUtils.checkParameterize(x)) {
|
||||
if (this.parameterized) {
|
||||
print('?');
|
||||
incrementReplaceCunt();
|
||||
if (this instanceof ExportParameterVisitor || this.parameters != null) {
|
||||
@@ -3671,4 +3603,17 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
public void endVisit(MySqlFlushStatement x) {
|
||||
|
||||
}
|
||||
|
||||
protected void printQuery(SQLSelectQuery x) {
|
||||
Class<?> clazz = x.getClass();
|
||||
if (clazz == MySqlSelectQueryBlock.class) {
|
||||
visit((MySqlSelectQueryBlock) x);
|
||||
} else if (clazz == SQLSelectQueryBlock.class) {
|
||||
visit((SQLSelectQueryBlock) x);
|
||||
} else if (clazz == SQLUnionQuery.class) {
|
||||
visit((SQLUnionQuery) x);
|
||||
} else {
|
||||
x.accept(this);
|
||||
}
|
||||
}
|
||||
} //
|
||||
|
||||
Reference in New Issue
Block a user