mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. for issue #2079
This commit is contained in:
@@ -89,7 +89,7 @@ public class SQLExprParser extends SQLParser {
|
||||
if (token == Token.COMMA) {
|
||||
return expr;
|
||||
} else if (token == Token.EQ) {
|
||||
expr = equalityRest(expr);
|
||||
expr = equalityRest2(expr);
|
||||
expr = andRest(expr);
|
||||
expr = orRest(expr);
|
||||
return expr;
|
||||
@@ -1668,6 +1668,53 @@ public class SQLExprParser extends SQLParser {
|
||||
return equalityRest(expr);
|
||||
}
|
||||
|
||||
private SQLExpr equalityRest2(SQLExpr expr) {
|
||||
SQLExpr rightExp;
|
||||
lexer.nextToken();
|
||||
try {
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
rightExp = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.AND
|
||||
|| lexer.token == Token.RPAREN
|
||||
|| lexer.token == Token.EOF
|
||||
|| lexer.token == Token.OR) {
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
}
|
||||
|
||||
rightExp = primaryRest(rightExp);
|
||||
rightExp = bitXorRest(rightExp);
|
||||
rightExp = multiplicativeRest(rightExp);
|
||||
rightExp = additiveRest(rightExp);
|
||||
rightExp = shiftRest(rightExp);
|
||||
rightExp = bitAndRest(rightExp);
|
||||
rightExp = bitOrRest(rightExp);
|
||||
rightExp = inRest(rightExp);
|
||||
rightExp = relationalRest(rightExp);
|
||||
} else {
|
||||
rightExp = bitOr();
|
||||
}
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr colonExpr = expr();
|
||||
rightExp = new SQLBinaryOpExpr(rightExp, SQLBinaryOperator.Assignment, colonExpr, dbType);
|
||||
}
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
|
||||
if (lexer.token == Token.IS || lexer.token == Token.BETWEEN) {
|
||||
expr = relationalRest(expr);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
|
||||
public SQLExpr equalityRest(SQLExpr expr) {
|
||||
SQLExpr rightExp;
|
||||
|
||||
@@ -1728,7 +1775,18 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
SQLExpr item = this.expr();
|
||||
SQLExpr item;
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
item = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextToken();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
item = this.primaryRest(item);
|
||||
item = this.exprRest(item);
|
||||
}
|
||||
} else {
|
||||
item = this.expr();
|
||||
}
|
||||
|
||||
item.setParent(inListExpr);
|
||||
targetList.add(item);
|
||||
if (lexer.token == Token.COMMA) {
|
||||
@@ -3049,15 +3107,39 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
public SQLLimit parseLimit() {
|
||||
if (lexer.token == Token.LIMIT) {
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenValue();
|
||||
|
||||
SQLLimit limit = new SQLLimit();
|
||||
|
||||
SQLExpr temp = this.expr();
|
||||
SQLExpr temp;
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
temp = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextTokenComma();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.EOF && lexer.token != Token.IDENTIFIER) {
|
||||
temp = this.primaryRest(temp);
|
||||
temp = this.exprRest(temp);
|
||||
}
|
||||
} else {
|
||||
temp = this.expr();
|
||||
}
|
||||
|
||||
if (lexer.token == (Token.COMMA)) {
|
||||
limit.setOffset(temp);
|
||||
lexer.nextToken();
|
||||
limit.setRowCount(this.expr());
|
||||
lexer.nextTokenValue();
|
||||
|
||||
SQLExpr rowCount;
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
rowCount = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextToken();
|
||||
if (lexer.token != Token.EOF && lexer.token != Token.IDENTIFIER) {
|
||||
rowCount = this.primaryRest(rowCount);
|
||||
rowCount = this.exprRest(rowCount);
|
||||
}
|
||||
} else {
|
||||
rowCount = this.expr();
|
||||
}
|
||||
|
||||
limit.setRowCount(rowCount);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.OFFSET)) {
|
||||
limit.setRowCount(temp);
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -585,7 +585,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
|
||||
if ((tableSource.getAlias() == null) || (tableSource.getAlias().length() == 0)) {
|
||||
if (tableSource.getAlias() == null || tableSource.getAlias().length() == 0) {
|
||||
Token token = lexer.token;
|
||||
long hash;
|
||||
if (token != Token.LEFT
|
||||
@@ -599,6 +599,11 @@ public class SQLSelectParser extends SQLParser {
|
||||
String alias = tableAlias();
|
||||
if (alias != null) {
|
||||
tableSource.setAlias(alias);
|
||||
|
||||
if (lexer.token == Token.WHERE) {
|
||||
return tableSource;
|
||||
}
|
||||
|
||||
return parseTableSourceRest(tableSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3164,6 +3164,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
expr = new SQLIntegerExpr(lexer.integerValue());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
} else if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
if (optimizedForParameterized) {
|
||||
expr = new SQLVariantRefExpr("?");
|
||||
@@ -3172,6 +3176,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
expr = new SQLCharExpr(lexer.stringVal());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
} else if (lexer.token() == Token.LITERAL_NCHARS) {
|
||||
if (optimizedForParameterized) {
|
||||
expr = new SQLVariantRefExpr("?");
|
||||
@@ -3180,6 +3187,20 @@ public class SQLStatementParser extends SQLParser {
|
||||
expr = new SQLNCharExpr(lexer.stringVal());
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
} else if (lexer.token == Token.NULL) {
|
||||
if (optimizedForParameterized) {
|
||||
expr = new SQLVariantRefExpr("?");
|
||||
values.incrementReplaceCount();
|
||||
} else {
|
||||
expr = new SQLNullExpr();
|
||||
}
|
||||
lexer.nextTokenComma();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
expr = this.exprParser.exprRest(expr);
|
||||
}
|
||||
} else {
|
||||
expr = exprParser.expr();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.parser;
|
||||
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
@@ -22,8 +24,19 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public class SymbolTable {
|
||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||
private static final boolean JVM_16;
|
||||
|
||||
public static SymbolTable global = new SymbolTable(8192);
|
||||
static {
|
||||
String version = null;
|
||||
try {
|
||||
version = System.getProperty("java.specification.version");
|
||||
} catch (Throwable error) {
|
||||
// skip
|
||||
}
|
||||
JVM_16 = "1.6".equals(version);
|
||||
}
|
||||
|
||||
public static SymbolTable global = new SymbolTable(32768);
|
||||
|
||||
private final Entry[] entries;
|
||||
private final int indexMask;
|
||||
@@ -42,12 +55,16 @@ public class SymbolTable {
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
String str = subString(buffer, offset, len);
|
||||
String str = JVM_16
|
||||
? subString(buffer, offset, len)
|
||||
: buffer.substring(offset, offset + len);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
String str = subString(buffer, offset, len);
|
||||
String str = JVM_16
|
||||
? subString(buffer, offset, len)
|
||||
: buffer.substring(offset, offset + len);
|
||||
entry = new Entry(hash, len, str);
|
||||
entries[bucket] = entry;
|
||||
return str;
|
||||
|
||||
Reference in New Issue
Block a user