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:
@@ -18,15 +18,8 @@ package com.alibaba.druid.sql.visitor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
|
||||
@@ -38,6 +31,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class ParameterizedOutputVisitorUtils {
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface ParameterizedVisitor extends PrintableVisitor {
|
||||
|
||||
void incrementReplaceCunt();
|
||||
|
||||
String getDbType();
|
||||
|
||||
}
|
||||
|
||||
@@ -251,16 +251,25 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
protected void printSelectList(List<SQLSelectItem> selectList) {
|
||||
incrementIndent();
|
||||
for (int i = 0, size = selectList.size(); i < size; ++i) {
|
||||
for (int i = 0, lineItemCount = 0, size = selectList.size()
|
||||
; i < size; ++i, ++lineItemCount) {
|
||||
SQLSelectItem selectItem = selectList.get(i);
|
||||
SQLExpr selectItemExpr = selectItem.getExpr();
|
||||
if (i != 0) {
|
||||
if (i % selectListNumberOfLine == 0) {
|
||||
if (lineItemCount == selectListNumberOfLine
|
||||
|| selectItemExpr instanceof SQLQueryExpr) {
|
||||
lineItemCount = 0;
|
||||
println();
|
||||
}
|
||||
|
||||
print0(", ");
|
||||
}
|
||||
|
||||
selectList.get(i).accept(this);
|
||||
if (selectItem.getClass() == SQLSelectItem.class) {
|
||||
this.visit(selectItem);
|
||||
} else {
|
||||
selectItem.accept(this);
|
||||
}
|
||||
}
|
||||
decrementIndent();
|
||||
}
|
||||
@@ -623,8 +632,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
incrementIndent();
|
||||
println();
|
||||
x.getSubQuery().accept(this);
|
||||
println();
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
return false;
|
||||
}
|
||||
@@ -816,6 +825,23 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public boolean visit(SQLIntegerExpr x) {
|
||||
int val = x.getNumber().intValue();
|
||||
if (val == 1
|
||||
&& JdbcConstants.ORACLE.equals(dbType)) {
|
||||
SQLObject parent = x.getParent();
|
||||
if (parent instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent;
|
||||
SQLExpr left = binaryOpExpr.getLeft();
|
||||
if (left instanceof SQLIdentifierExpr
|
||||
&& binaryOpExpr.getOperator() == SQLBinaryOperator.Equality) {
|
||||
String name = ((SQLIdentifierExpr) left).getName();
|
||||
if ("rownum".equals(name)) {
|
||||
print(1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.parameterized
|
||||
&& ParameterizedOutputVisitorUtils.checkParameterize(x)) {
|
||||
if (!ParameterizedOutputVisitorUtils.checkParameterize(x)) {
|
||||
@@ -1074,8 +1100,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
incrementIndent();
|
||||
println();
|
||||
x.getSubQuery().accept(this);
|
||||
println();
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
}
|
||||
return false;
|
||||
@@ -1984,22 +2010,28 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print(' ');
|
||||
|
||||
SQLTableSource right = x.getRight();
|
||||
boolean isSubquery = false;
|
||||
|
||||
if(right instanceof SQLSubqueryTableSource) {
|
||||
isSubquery = true;
|
||||
}
|
||||
right.accept(this);
|
||||
|
||||
if (x.getCondition() != null) {
|
||||
if (isSubquery) {
|
||||
SQLExpr condition = x.getCondition();
|
||||
if (condition != null) {
|
||||
boolean newLine = false;
|
||||
|
||||
if(right instanceof SQLSubqueryTableSource) {
|
||||
newLine = true;
|
||||
} else if (condition instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOperator op = ((SQLBinaryOpExpr) condition).getOperator();
|
||||
if (op == SQLBinaryOperator.BooleanAnd || op == SQLBinaryOperator.BooleanOr) {
|
||||
newLine = true;
|
||||
}
|
||||
}
|
||||
if (newLine) {
|
||||
println();
|
||||
} else {
|
||||
print(' ');
|
||||
}
|
||||
incrementIndent();
|
||||
print0(ucase ? "ON " : "on ");
|
||||
x.getCondition().accept(this);
|
||||
condition.accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
@@ -2090,8 +2122,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
incrementIndent();
|
||||
println();
|
||||
x.getSubQuery().accept(this);
|
||||
decrementIndent();
|
||||
println();
|
||||
print(')');
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user