mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for sql parser
This commit is contained in:
@@ -582,7 +582,6 @@ public class Lexer {
|
||||
np = bp;
|
||||
sp = 0;
|
||||
scanChar();
|
||||
sp++;
|
||||
|
||||
if (ch == '*') {
|
||||
scanChar();
|
||||
@@ -620,7 +619,7 @@ public class Lexer {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch == '\r') {
|
||||
if (ch == '\n') {
|
||||
scanChar();
|
||||
sp++;
|
||||
break;
|
||||
|
||||
@@ -34,6 +34,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCastExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLExistsExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
|
||||
@@ -449,9 +450,20 @@ public class SQLExprParser extends SQLParser {
|
||||
throw new IllegalArgumentException("expr");
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.OF) {
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
String name = ((SQLIdentifierExpr) expr).getName();
|
||||
if ("CURRENT".equalsIgnoreCase(name)) {
|
||||
lexer.nextToken();
|
||||
SQLName cursorName = this.name();
|
||||
return new SQLCurrentOfCursorExpr(cursorName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.DOT) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
expr = dotRest(expr);
|
||||
return primaryRest(expr);
|
||||
} else if (lexer.token() == Token.COLONEQ) {
|
||||
@@ -1035,21 +1047,25 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLDataType parseDataType() throws ParserException {
|
||||
|
||||
|
||||
if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
SQLName typeExpr = name();
|
||||
String typeName = typeExpr.toString();
|
||||
|
||||
SQLDataType dataType = new SQLDataTypeImpl(typeName);
|
||||
parseDataTypeRest(dataType);
|
||||
return dataType;
|
||||
}
|
||||
|
||||
protected void parseDataTypeRest(SQLDataType dataType) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
exprList(dataType.getArguments());
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
return dataType;
|
||||
}
|
||||
|
||||
protected SQLDataType parseCharType() throws ParserException {
|
||||
@@ -1108,7 +1124,7 @@ public class SQLExprParser extends SQLParser {
|
||||
throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SQLColumnDefinition parseColumn() {
|
||||
SQLColumnDefinition column = new SQLColumnDefinition();
|
||||
column.setName(name());
|
||||
@@ -1116,7 +1132,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
return parseColumnRest(column);
|
||||
}
|
||||
|
||||
|
||||
public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
|
||||
if (lexer.token() == Token.DEFAULT) {
|
||||
lexer.nextToken();
|
||||
@@ -1133,7 +1149,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
return column;
|
||||
}
|
||||
|
||||
|
||||
public SQLPrimaryKey parsePrimaryKey() {
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
|
||||
@@ -114,19 +114,8 @@ public class SQLStatementParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.TABLE) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLDropTableStatement stmt = new SQLDropTableStatement();
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.getTableNames().add(name);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
SQLDropTableStatement stmt = parseDropTable(false);
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
@@ -159,11 +148,31 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLDropTableStatement parseDropTable(boolean acceptDrop) {
|
||||
if (acceptDrop) {
|
||||
accept(Token.DROP);
|
||||
}
|
||||
accept(Token.TABLE);
|
||||
|
||||
SQLDropTableStatement stmt = new SQLDropTableStatement();
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.getTableNames().add(name);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLStatement parseTruncate() {
|
||||
accept(Token.TRUNCATE);
|
||||
accept(Token.TABLE);
|
||||
SQLTruncateStatement stmt = new SQLTruncateStatement();
|
||||
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.getTableNames().add(name);
|
||||
@@ -187,27 +196,27 @@ public class SQLStatementParser extends SQLParser {
|
||||
parseInsert0(insertStatement);
|
||||
return insertStatement;
|
||||
}
|
||||
|
||||
|
||||
protected void parseInsert0(SQLInsertInto insertStatement) {
|
||||
parseInsert0(insertStatement, true);
|
||||
}
|
||||
|
||||
protected void parseInsert0_hinits(SQLInsertInto insertStatement) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected void parseInsert0(SQLInsertInto insertStatement, boolean acceptSubQuery) {
|
||||
accept(Token.INTO);
|
||||
|
||||
|
||||
SQLName tableName = this.exprParser.name();
|
||||
insertStatement.setTableName(tableName);
|
||||
|
||||
|
||||
if (lexer.token() == Token.LITERAL_ALIAS) {
|
||||
insertStatement.setAlias(as());
|
||||
}
|
||||
|
||||
|
||||
parseInsert0_hinits(insertStatement);
|
||||
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
insertStatement.setAlias(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
@@ -283,6 +292,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLStatement parseCreate() throws ParserException {
|
||||
char mark_ch = lexer.current();
|
||||
int mark_bp = lexer.bp();
|
||||
|
||||
accept(Token.CREATE);
|
||||
|
||||
Token token = lexer.token();
|
||||
@@ -294,15 +306,29 @@ public class SQLStatementParser extends SQLParser {
|
||||
return parseCreateIndex(false);
|
||||
} else if (identifierEquals("SEQUENCE")) {
|
||||
return parseCreateSequence(false);
|
||||
} else if (token == Token.OR) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("REPLACE");
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.reset(mark_bp, mark_ch, Token.CREATE);
|
||||
return parseCreateProcedure();
|
||||
}
|
||||
|
||||
//lexer.reset(mark_bp, mark_ch, Token.CREATE);
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
public SQLStatement parseCreateProcedure() {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
public SQLStatement parseCreateSequence(boolean acceptCreate) {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
|
||||
public SQLStatement parseCreateIndex(boolean acceptCreate) {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
@@ -403,9 +429,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
public SQLCommentStatement parseComment() {
|
||||
accept(Token.COMMENT);
|
||||
SQLCommentStatement stmt = new SQLCommentStatement();
|
||||
|
||||
|
||||
accept(Token.ON);
|
||||
|
||||
|
||||
if (lexer.token() == Token.TABLE) {
|
||||
stmt.setType(SQLCommentStatement.Type.TABLE);
|
||||
lexer.nextToken();
|
||||
@@ -413,12 +439,12 @@ public class SQLStatementParser extends SQLParser {
|
||||
stmt.setType(SQLCommentStatement.Type.COLUMN);
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
|
||||
stmt.setOn(this.exprParser.name());
|
||||
|
||||
|
||||
accept(Token.IS);
|
||||
stmt.setComment(this.exprParser.expr());
|
||||
|
||||
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,6 +172,8 @@ public enum Token {
|
||||
LOOP("LOOP"),
|
||||
GOTO("GOTO"),
|
||||
COMMIT("COMMIT"),
|
||||
ROLLBACK("ROLLBACK"),
|
||||
SAVEPOINT("SAVEPOINT"),
|
||||
|
||||
// transact-sql
|
||||
TOP("TOP"),
|
||||
|
||||
Reference in New Issue
Block a user