mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -83,8 +83,50 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
}
|
||||
|
||||
public MySqlExprParser(String sql, SQLParserFeature... features){
|
||||
this(new MySqlLexer(sql, features));
|
||||
super(new MySqlLexer(sql, features), JdbcConstants.MYSQL);
|
||||
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
|
||||
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
|
||||
if (sql.length() > 6) {
|
||||
char c0 = sql.charAt(0);
|
||||
char c1 = sql.charAt(1);
|
||||
char c2 = sql.charAt(2);
|
||||
char c3 = sql.charAt(3);
|
||||
char c4 = sql.charAt(4);
|
||||
char c5 = sql.charAt(5);
|
||||
char c6 = sql.charAt(6);
|
||||
|
||||
if (c0 == 'S' && c1 == 'E' && c2 == 'L' && c3 == 'E' && c4 == 'C' && c5 == 'T' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.SELECT);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c0 == 's' && c1 == 'e' && c2 == 'l' && c3 == 'e' && c4 == 'c' && c5 == 't' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.SELECT);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c0 == 'I' && c1 == 'N' && c2 == 'S' && c3 == 'E' && c4 == 'R' && c5 == 'T' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.INSERT);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c0 == 'i' && c1 == 'n' && c2 == 's' && c3 == 'e' && c4 == 'r' && c5 == 't' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.INSERT);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c0 == 'U' && c1 == 'P' && c2 == 'D' && c3 == 'A' && c4 == 'T' && c5 == 'E' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.UPDATE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (c0 == 'u' && c1 == 'p' && c2 == 'd' && c3 == 'a' && c4 == 't' && c5 == 'e' && c6 == ' ') {
|
||||
lexer.reset(6, ' ', Token.UPDATE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.lexer.nextToken();
|
||||
|
||||
}
|
||||
|
||||
public MySqlExprParser(String sql, boolean keepComments){
|
||||
|
||||
@@ -74,7 +74,10 @@ public class MySqlLexer extends Lexer {
|
||||
}
|
||||
|
||||
public MySqlLexer(String input, SQLParserFeature... features){
|
||||
this(input, true, true);
|
||||
super(input, true);
|
||||
this.keepComments = true;
|
||||
super.keywods = DEFAULT_MYSQL_KEYWORDS;
|
||||
|
||||
for (SQLParserFeature feature : features) {
|
||||
config(feature, true);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
return;
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenIdent();
|
||||
|
||||
if (lexer.token() == Token.UPDATE) { // taobao returning to urgly syntax
|
||||
updateStmt = this.parseUpdateStatment();
|
||||
|
||||
@@ -2199,7 +2199,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
columnSize++;
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
lexer.nextTokenIdent();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2256,7 +2256,27 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
accept(Token.KEY);
|
||||
accept(Token.UPDATE);
|
||||
|
||||
exprParser.exprList(insertStatement.getDuplicateKeyUpdate(), insertStatement);
|
||||
List<SQLExpr> duplicateKeyUpdate = insertStatement.getDuplicateKeyUpdate();
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
accept(Token.EQ);
|
||||
SQLExpr value;
|
||||
try {
|
||||
value = this.exprParser.expr();
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + name + "=", e);
|
||||
}
|
||||
|
||||
SQLBinaryOpExpr assignment = new SQLBinaryOpExpr(name, SQLBinaryOperator.Equality, value);
|
||||
assignment.setParent(insertStatement);
|
||||
duplicateKeyUpdate.add(assignment);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextTokenIdent();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return insertStatement;
|
||||
|
||||
@@ -942,10 +942,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
protected void printValuesList(List<SQLInsertStatement.ValuesClause> valuesList) {
|
||||
|
||||
if (this.parameterized) {
|
||||
if (this.parameterized && valuesList.size() > 0) {
|
||||
print0(ucase ? "VALUES " : "values ");
|
||||
this.indentCount++;
|
||||
valuesList.get(0).accept(this);
|
||||
visit(valuesList.get(0));
|
||||
this.indentCount--;
|
||||
if (valuesList.size() > 1) {
|
||||
this.incrementReplaceCunt();
|
||||
|
||||
Reference in New Issue
Block a user