mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for oracle sql parser
This commit is contained in:
@@ -296,11 +296,24 @@ public class Lexer {
|
||||
case '@':
|
||||
scanVariable();
|
||||
return;
|
||||
case '-':
|
||||
int subNextChar = buf[bp + 1];
|
||||
if (subNextChar == '-') {
|
||||
scanComment();
|
||||
if ((token() == Token.LINE_COMMENT || token() == Token.MULTI_LINE_COMMENT) && skipComment) {
|
||||
sp = 0;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
scanOperator();
|
||||
}
|
||||
return;
|
||||
case '/':
|
||||
int nextChar = buf[bp + 1];
|
||||
if (nextChar == '/' || nextChar == '*') {
|
||||
scanComment();
|
||||
if ((token() == Token.LINE_COMMENT || token() == Token.MULTI_LINE_COMMENT) && skipComment) {
|
||||
sp = 0;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
@@ -856,6 +869,20 @@ public class Lexer {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public int bp() {
|
||||
return this.bp;
|
||||
}
|
||||
|
||||
public char current() {
|
||||
return this.ch;
|
||||
}
|
||||
|
||||
public void reset(int mark, char mark_ch, Token token) {
|
||||
this.bp = mark;
|
||||
this.ch = mark_ch;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public final String numberString() {
|
||||
return new String(buf, np, sp);
|
||||
|
||||
@@ -209,6 +209,9 @@ public class SQLExprParser extends SQLParser {
|
||||
case TIME:
|
||||
case ZONE:
|
||||
case LOG:
|
||||
case LIMIT:
|
||||
case SCHEMA:
|
||||
case AT:
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
break;
|
||||
@@ -372,6 +375,13 @@ public class SQLExprParser extends SQLParser {
|
||||
case INTERVAL:
|
||||
sqlExpr = parseInterval();
|
||||
break;
|
||||
case COLON:
|
||||
lexer.nextToken();
|
||||
if (lexer.token == Token.LITERAL_ALIAS) {
|
||||
sqlExpr = new SQLVariantRefExpr(":\"" + lexer.stringVal() + "\"");
|
||||
lexer.nextToken();
|
||||
}
|
||||
break;
|
||||
case ANY:
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
@@ -416,7 +426,7 @@ public class SQLExprParser extends SQLParser {
|
||||
sqlExpr = allExpr;
|
||||
break;
|
||||
default:
|
||||
throw new ParserException("ERROR. token : " + tok);
|
||||
throw new ParserException("ERROR. token : " + tok + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
return primaryRest(sqlExpr);
|
||||
@@ -438,39 +448,56 @@ public class SQLExprParser extends SQLParser {
|
||||
if (lexer.token() == Token.DOT) {
|
||||
lexer.nextToken();
|
||||
expr = dotRest(expr);
|
||||
return primaryRest(expr);
|
||||
} else if (lexer.token() == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp);
|
||||
} else {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
if (expr instanceof SQLIdentifierExpr || expr instanceof SQLDefaultExpr) {
|
||||
String method_name = expr.toString();
|
||||
lexer.nextToken();
|
||||
|
||||
if (isAggreateFunction(method_name)) {
|
||||
SQLAggregateExpr aggregateExpr = parseAggregateExpr(method_name);
|
||||
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(method_name);
|
||||
if (lexer.token() != Token.RPAREN) {
|
||||
exprList(methodInvokeExpr.getParameters());
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return primaryRest(methodInvokeExpr);
|
||||
}
|
||||
|
||||
throw new ParserException("not support token:" + lexer.token());
|
||||
return methodRest(expr, true);
|
||||
}
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
|
||||
if (acceptLPAREN) {
|
||||
accept(Token.LPAREN);
|
||||
}
|
||||
|
||||
if (expr instanceof SQLName || expr instanceof SQLDefaultExpr) {
|
||||
String method_name;
|
||||
|
||||
SQLMethodInvokeExpr methodInvokeExpr;
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
method_name = ((SQLPropertyExpr) expr).getName();
|
||||
methodInvokeExpr = new SQLMethodInvokeExpr(method_name);
|
||||
methodInvokeExpr.setOwner(((SQLPropertyExpr) expr).getOwner());
|
||||
} else {
|
||||
method_name = expr.toString();
|
||||
methodInvokeExpr = new SQLMethodInvokeExpr(method_name);
|
||||
}
|
||||
|
||||
if (isAggreateFunction(method_name)) {
|
||||
SQLAggregateExpr aggregateExpr = parseAggregateExpr(method_name);
|
||||
|
||||
return aggregateExpr;
|
||||
}
|
||||
|
||||
if (lexer.token() != Token.RPAREN) {
|
||||
exprList(methodInvokeExpr.getParameters());
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return primaryRest(methodInvokeExpr);
|
||||
}
|
||||
|
||||
throw new ParserException("not support token:" + lexer.token());
|
||||
}
|
||||
|
||||
protected SQLExpr dotRest(SQLExpr expr) {
|
||||
if (lexer.token() == Token.STAR) {
|
||||
lexer.nextToken();
|
||||
@@ -478,7 +505,8 @@ public class SQLExprParser extends SQLParser {
|
||||
} else {
|
||||
String name;
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS) {
|
||||
if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS
|
||||
|| lexer.token() == Token.LITERAL_ALIAS) {
|
||||
name = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.getKeywods().containsValue(lexer.token())) {
|
||||
@@ -569,16 +597,17 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
SQLName name = new SQLIdentifierExpr(identName);
|
||||
|
||||
if (lexer.token() == Token.DOT) {
|
||||
while (lexer.token() == Token.DOT) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
if (lexer.token() == Token.KEY) {
|
||||
name = new SQLPropertyExpr(name, "KEY");
|
||||
lexer.nextToken();
|
||||
return name;
|
||||
}
|
||||
|
||||
if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.IDENTIFIER) {
|
||||
if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.IDENTIFIER
|
||||
&& (!lexer.getKeywods().containsValue(lexer.token()))) {
|
||||
throw new ParserException("error, " + lexer.token());
|
||||
}
|
||||
|
||||
@@ -612,6 +641,7 @@ public class SQLExprParser extends SQLParser {
|
||||
} else {
|
||||
aggregateExpr = new SQLAggregateExpr(method_name, 1);
|
||||
}
|
||||
|
||||
exprList(aggregateExpr.getArguments());
|
||||
|
||||
accept(Token.RPAREN);
|
||||
@@ -695,7 +725,7 @@ public class SQLExprParser extends SQLParser {
|
||||
return equalityRest(expr);
|
||||
}
|
||||
|
||||
public final SQLExpr equalityRest(SQLExpr expr) throws ParserException {
|
||||
public SQLExpr equalityRest(SQLExpr expr) throws ParserException {
|
||||
SQLExpr rightExp;
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
@@ -997,13 +1027,13 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLDataType parseDataType() throws ParserException {
|
||||
if (lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException();
|
||||
if (lexer.token() != Token.DATE && lexer.token() != Token.TIMESTAMP && lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException("unexpect token : " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
|
||||
String typeName = lexer.stringVal();
|
||||
SQLName typeExpr = name();
|
||||
String typeName = typeExpr.toString();
|
||||
|
||||
lexer.nextToken();
|
||||
SQLDataType dataType = new SQLDataTypeImpl(typeName);
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -65,7 +65,8 @@ public class SQLParser {
|
||||
return alias;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.YEAR || lexer.token() == Token.DAY || lexer.token() == Token.KEY || lexer.token() == Token.CASE) {
|
||||
if (lexer.token() == Token.YEAR || lexer.token() == Token.DAY || lexer.token() == Token.KEY
|
||||
|| lexer.token() == Token.CASE || lexer.token() == Token.TIMESTAMP) {
|
||||
alias = lexer.token.name();
|
||||
lexer.nextToken();
|
||||
return alias;
|
||||
@@ -92,7 +93,9 @@ public class SQLParser {
|
||||
} else if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
alias = "'" + lexer.stringVal() + "'";
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.YEAR || lexer.token() == Token.DAY || lexer.token() == Token.CASE) {
|
||||
} else if (lexer.token() == Token.YEAR || lexer.token() == Token.DAY || lexer.token() == Token.TIME
|
||||
|| lexer.token() == Token.MONTH || lexer.token() == Token.CASE || lexer.token() == Token.AT
|
||||
|| lexer.token() == Token.TIMESTAMP) {
|
||||
alias = lexer.token.name();
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCallStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
@@ -36,6 +37,7 @@ import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleSelectParser;
|
||||
|
||||
public class SQLStatementParser extends SQLParser {
|
||||
|
||||
@@ -250,7 +252,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLSetStatement parseSet() throws ParserException {
|
||||
public SQLStatement parseSet() throws ParserException {
|
||||
accept(Token.SET);
|
||||
SQLSetStatement stmt = new SQLSetStatement();
|
||||
|
||||
@@ -299,7 +301,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new SQLSelectParser(this.lexer);
|
||||
return new OracleSelectParser(this.lexer);
|
||||
}
|
||||
|
||||
public SQLUpdateStatement parseUpdateStatement() throws ParserException {
|
||||
@@ -383,4 +385,25 @@ public class SQLStatementParser extends SQLParser {
|
||||
return createView;
|
||||
}
|
||||
|
||||
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();
|
||||
} else if (lexer.token() == Token.COLUMN) {
|
||||
stmt.setType(SQLCommentStatement.Type.COLUMN);
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
stmt.setOn(this.exprParser.name());
|
||||
|
||||
accept(Token.IS);
|
||||
stmt.setComment(this.exprParser.expr());
|
||||
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ public enum Token {
|
||||
SCHEMA("SCHEMA"),
|
||||
CAST("CAST"),
|
||||
USER("USER"),
|
||||
COLUMN("COLUMN"),
|
||||
|
||||
AND("AND"),
|
||||
OR("OR"),
|
||||
@@ -128,6 +129,7 @@ public enum Token {
|
||||
USING("USING"),
|
||||
CURRENT("CURRENT"),
|
||||
RETURNING("RETURNING"),
|
||||
COMMENT("COMMENT"),
|
||||
|
||||
// oracle
|
||||
START("START"),
|
||||
@@ -157,10 +159,15 @@ public enum Token {
|
||||
WAIT("WAIT"),
|
||||
ADVISE("ADVISE"),
|
||||
SESSION("SESSION"),
|
||||
PROCEDURE("PROCEDURE"),
|
||||
AT("AT"),
|
||||
LOCAL("LOCAL"),
|
||||
TIME("TIME"),
|
||||
ZONE("ZONE"),
|
||||
SYSDATE("SYSDATE"),
|
||||
DECLARE("DECLARE"),
|
||||
EXCEPTION("EXCEPTION"),
|
||||
GRANT("GRANT"),
|
||||
|
||||
// transact-sql
|
||||
TOP("TOP"),
|
||||
|
||||
Reference in New Issue
Block a user