bug fixed for sql parser.

This commit is contained in:
wenshao
2017-07-06 03:07:35 +08:00
parent 717726badd
commit 1e0ce32284
33 changed files with 744 additions and 142 deletions
@@ -73,6 +73,9 @@ public class SQLBlockStatement extends SQLStatementImpl {
}
public void setException(SQLStatement exception) {
if (exception != null) {
exception.setParent(this);
}
this.exception = exception;
}
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLName;
@@ -42,12 +42,14 @@ public class MySqlCursorDeclareStatement extends MySqlStatementImpl{
}
public void setSelect(SQLSelectStatement select) {
if (select != null) {
select.setParent(this);
}
this.select = select;
}
@Override
public void accept0(MySqlASTVisitor visitor) {
// TODO Auto-generated method stub
if (visitor.visit(this)) {
acceptChild(visitor, select);
}
@@ -3619,6 +3619,7 @@ public class MySqlStatementParser extends SQLStatementParser {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();
elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);
accept(Token.THEN);
this.parseStatementList(elseIf.getStatements(), -1, elseIf);
@@ -178,7 +178,9 @@ public class OdpsSelectParser extends SQLSelectParser {
protected SQLTableSource parseLateralView(SQLTableSource tableSource) {
accept(Token.VIEW);
tableSource.setAlias(null);
if ("LATERAL".equalsIgnoreCase(tableSource.getAlias())) {
tableSource.setAlias(null);
}
OdpsLateralViewTableSource lateralViewTabSrc = new OdpsLateralViewTableSource();
lateralViewTabSrc.setTableSource(tableSource);
@@ -58,6 +58,9 @@ public class OracleForStatement extends OracleStatementImpl {
}
public void setRange(SQLExpr range) {
if (range != null) {
range.setParent(this);
}
this.range = range;
}
@@ -740,6 +740,13 @@ public class OracleStatementParser extends SQLStatementParser {
return stmt;
}
public SQLCreateFunctionStatement parseCreateFunction() {
accept(Token.CREATE);
SQLCreateFunctionStatement stmt = (SQLCreateFunctionStatement) parseFunction();
stmt.setCreate(true);
return stmt;
}
public SQLStatement parseFunction() {
SQLCreateFunctionStatement stmt = new SQLCreateFunctionStatement();
stmt.setDbType(dbType);
@@ -893,6 +900,7 @@ public class OracleStatementParser extends SQLStatementParser {
SQLIfStatement.ElseIf elseIf = new SQLIfStatement.ElseIf();
elseIf.setCondition(this.exprParser.expr());
elseIf.setParent(stmt);
accept(Token.THEN);
this.parseStatementList(elseIf.getStatements(), -1, stmt);
@@ -903,6 +911,8 @@ public class OracleStatementParser extends SQLStatementParser {
if (lexer.token() == Token.ELSE) {
lexer.nextToken();
SQLIfStatement.Else elseItem = new SQLIfStatement.Else();
this.parseStatementList(elseItem.getStatements(), -1, elseItem);
stmt.setElseItem(elseItem);
@@ -1865,7 +1865,10 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
x.getIndex().accept(this);
print0(ucase ? " IN " : " in ");
x.getRange().accept(this);
SQLExpr range = x.getRange();
range.accept(this);
if (!all) {
println();
print0(ucase ? "LOOP" : "loop");
@@ -1840,6 +1840,10 @@ public class SQLStatementParser extends SQLParser {
SQLCreateProcedureStatement stmt = parseCreateProcedure();
stmt.setCreate(true);
return stmt;
} else if (token == Token.FUNCTION) {
lexer.reset(markBp, markChar, Token.CREATE);
SQLStatement stmt = this.parseCreateFunction();
return stmt;
} else if (identifierEquals("BITMAP")) {
lexer.reset(markBp, markChar, Token.CREATE);
return parseCreateIndex(true);
@@ -1851,6 +1855,10 @@ public class SQLStatementParser extends SQLParser {
throw new ParserException("TODO " + lexer.token());
}
public SQLCreateFunctionStatement parseCreateFunction() {
throw new ParserException("TODO " + lexer.token());
}
public SQLStatement parseCreateMaterializedView() {
accept(Token.CREATE);
acceptIdentifier("MATERIALIZED");
@@ -36,12 +36,22 @@ import java.util.concurrent.ConcurrentSkipListMap;
* Created by wenshao on 03/06/2017.
*/
public class SchemaRepository {
private String name;
private final Map<String, SchemaObject> objects = new ConcurrentSkipListMap<String, SchemaObject>();
private final Map<String, SchemaObject> functions = new ConcurrentSkipListMap<String, SchemaObject>();
private final SchemaVisitor visitor = new SchemaVisitor();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SchemaObject findTable(String tableName) {
String lowerName = tableName.toLowerCase();
SchemaObject object = objects.get(lowerName);
@@ -144,6 +154,16 @@ public class SchemaRepository {
}
return tableObject;
}
if (expr instanceof SQLPropertyExpr) {
String tableName = ((SQLPropertyExpr) expr).getName();
tableObject = findTable(tableName);
if (tableObject != null) {
exprTableSource.setSchemaObject(tableObject);
}
return tableObject;
}
}
return null;
}
@@ -222,6 +242,16 @@ public class SchemaRepository {
if (tableSource instanceof SQLExprTableSource) {
return findTable(tableSource, tableSource.computeAlias());
}
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
SchemaObject table = findTable(join.getLeft(), expr);
if (table == null) {
table = findTable(join.getRight(), expr);
}
return table;
}
return null;
}
@@ -38,6 +38,7 @@ import com.alibaba.druid.sql.ast.statement.SQLWhileStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSegmentAttributes;
import com.alibaba.druid.sql.ast.statement.SQLDeclareStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreatePackageStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleForStatement;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
@@ -263,21 +264,55 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
}
}
private static int paramCount(SQLExpr x) {
if (x instanceof SQLMethodInvokeExpr) {
List<SQLExpr> params = ((SQLMethodInvokeExpr) x).getParameters();
int paramCount = 1;
for (SQLExpr param : params) {
paramCount += paramCount(param);
}
return paramCount;
}
if (x instanceof SQLAggregateExpr) {
List<SQLExpr> params = ((SQLAggregateExpr) x).getArguments();
int paramCount = 1;
for (SQLExpr param : params) {
paramCount += paramCount(param);
}
return paramCount;
}
if (x instanceof SQLBinaryOpExpr) {
return paramCount(((SQLBinaryOpExpr) x).getLeft())
+ paramCount(((SQLBinaryOpExpr) x).getRight());
}
return 1;
}
protected void printSelectList(List<SQLSelectItem> selectList) {
incrementIndent();
for (int i = 0, lineItemCount = 0, size = selectList.size()
; i < size; ++i, ++lineItemCount) {
SQLSelectItem selectItem = selectList.get(i);
SQLExpr selectItemExpr = selectItem.getExpr();
int paramCount = paramCount(selectItemExpr);
if (selectItemExpr instanceof SQLMethodInvokeExpr
|| selectItemExpr instanceof SQLAggregateExpr
|| selectItemExpr instanceof SQLBinaryOpExpr) {
lineItemCount += (paramCount - 1);
}
if (i != 0) {
SQLSelectItem preSelectItem = selectList.get(i - 1);
if (preSelectItem.getAfterCommentsDirect() != null) {
lineItemCount = 0;
println();
} else if (selectItemExpr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) selectItemExpr;
int paramCount = methodInvokeExpr.getParameters().size();
lineItemCount += paramCount;
} else if (selectItemExpr instanceof SQLMethodInvokeExpr
|| selectItemExpr instanceof SQLAggregateExpr
|| selectItemExpr instanceof SQLBinaryOpExpr) {
if (lineItemCount >= selectListNumberOfLine) {
lineItemCount = paramCount;
println();
@@ -1198,19 +1233,20 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
parent = parent.getParent();
}
if (parent instanceof SQLStatement) {
if (parent instanceof ValuesClause) {
println();
print('(');
x.getSubQuery().accept(this);
print(')');
println();
} else if (parent instanceof SQLStatement
&& !(parent instanceof OracleForStatement)) {
incrementIndent();
println();
x.getSubQuery().accept(this);
decrementIndent();
} else if (parent instanceof ValuesClause) {
println();
print('(');
x.getSubQuery().accept(this);
print(')');
println();
} else if (parent instanceof SQLOpenStatement) {
x.getSubQuery().accept(this);
} else {