This commit is contained in:
温少
2012-03-08 15:55:29 +08:00
parent 1ac46c46f9
commit b9c3d145c6
20 changed files with 353 additions and 180 deletions
@@ -90,137 +90,138 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
return false;
}
public boolean visit(MySqlSelectQueryBlock select) {
if (select.getOrderBy() != null) {
select.getOrderBy().setParent(select);
public boolean visit(MySqlSelectQueryBlock x) {
if (x.getOrderBy() != null) {
x.getOrderBy().setParent(x);
}
print("SELECT ");
if (SQLSetQuantifier.ALL == select.getDistionOption()) print("ALL ");
else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) print("DISTINCT ");
else if (SQLSetQuantifier.DISTINCTROW == select.getDistionOption()) {
if (SQLSetQuantifier.ALL == x.getDistionOption()) print("ALL ");
else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) print("DISTINCT ");
else if (SQLSetQuantifier.DISTINCTROW == x.getDistionOption()) {
print("DISTINCTROW ");
}
if (select.isHignPriority()) {
if (x.isHignPriority()) {
print("HIGH_PRIORITY ");
}
if (select.isSmallResult()) {
if (x.isSmallResult()) {
print("SQL_SMALL_RESULT ");
}
if (select.isBigResult()) {
if (x.isBigResult()) {
print("SQL_BIG_RESULT ");
}
if (select.isBufferResult()) {
if (x.isBufferResult()) {
print("SQL_BUFFER_RESULT ");
}
if (select.getCache() != null) {
if (select.getCache().booleanValue()) {
if (x.getCache() != null) {
if (x.getCache().booleanValue()) {
print("SQL_CACHE ");
} else {
print("SQL_NO_CACHE ");
}
}
if (select.isCalcFoundRows()) {
if (x.isCalcFoundRows()) {
print("SQL_CALC_FOUND_ROWS ");
}
printSelectList(select.getSelectList());
printSelectList(x.getSelectList());
if (select.getOutFile() != null) {
if (x.getOutFile() != null) {
println();
print("INTO OUTFILE ");
select.getOutFile().accept(this);
if (select.getOutFileCharset() != null) {
x.getOutFile().accept(this);
if (x.getOutFileCharset() != null) {
print(" CHARACTER SET ");
print(select.getOutFileCharset());
print(x.getOutFileCharset());
}
if (select.getOutFileColumnsTerminatedBy() != null || select.getOutFileColumnsEnclosedBy() != null
|| select.getOutFileColumnsEscaped() != null) {
if (x.getOutFileColumnsTerminatedBy() != null || x.getOutFileColumnsEnclosedBy() != null
|| x.getOutFileColumnsEscaped() != null) {
print(" COLUMNS");
if (select.getOutFileColumnsTerminatedBy() != null) {
if (x.getOutFileColumnsTerminatedBy() != null) {
print(" TERMINATED BY ");
select.getOutFileColumnsTerminatedBy().accept(this);
x.getOutFileColumnsTerminatedBy().accept(this);
}
if (select.getOutFileColumnsEnclosedBy() != null) {
if (select.isOutFileColumnsEnclosedOptionally()) {
if (x.getOutFileColumnsEnclosedBy() != null) {
if (x.isOutFileColumnsEnclosedOptionally()) {
print(" OPTIONALLY");
}
print(" ENCLOSED BY ");
select.getOutFileColumnsEnclosedBy().accept(this);
x.getOutFileColumnsEnclosedBy().accept(this);
}
if (select.getOutFileColumnsEscaped() != null) {
if (x.getOutFileColumnsEscaped() != null) {
print(" ESCAPED BY ");
select.getOutFileColumnsEscaped().accept(this);
x.getOutFileColumnsEscaped().accept(this);
}
}
if (select.getOutFileLinesStartingBy() != null || select.getOutFileLinesTerminatedBy() != null) {
if (x.getOutFileLinesStartingBy() != null || x.getOutFileLinesTerminatedBy() != null) {
print(" LINES");
if (select.getOutFileLinesStartingBy() != null) {
if (x.getOutFileLinesStartingBy() != null) {
print(" STARTING BY ");
select.getOutFileLinesStartingBy().accept(this);
x.getOutFileLinesStartingBy().accept(this);
}
if (select.getOutFileLinesTerminatedBy() != null) {
if (x.getOutFileLinesTerminatedBy() != null) {
print(" TERMINATED BY ");
select.getOutFileLinesTerminatedBy().accept(this);
x.getOutFileLinesTerminatedBy().accept(this);
}
}
}
if (select.getFrom() != null) {
if (x.getFrom() != null) {
println();
print("FROM ");
select.getFrom().accept(this);
x.getFrom().accept(this);
}
if (select.getWhere() != null) {
if (x.getWhere() != null) {
println();
print("WHERE ");
select.getWhere().accept(this);
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
if (select.getGroupBy() != null) {
if (x.getGroupBy() != null) {
println();
select.getGroupBy().accept(this);
x.getGroupBy().accept(this);
}
if (select.getOrderBy() != null) {
if (x.getOrderBy() != null) {
println();
select.getOrderBy().accept(this);
x.getOrderBy().accept(this);
}
if (select.getLimit() != null) {
if (x.getLimit() != null) {
println();
select.getLimit().accept(this);
x.getLimit().accept(this);
}
if (select.getProcedureName() != null) {
if (x.getProcedureName() != null) {
print(" PROCEDURE ");
select.getProcedureName().accept(this);
if (select.getProcedureArgumentList().size() > 0) {
x.getProcedureName().accept(this);
if (x.getProcedureArgumentList().size() > 0) {
print("(");
printAndAccept(select.getProcedureArgumentList(), ", ");
printAndAccept(x.getProcedureArgumentList(), ", ");
print(")");
}
}
if (select.isForUpdate()) {
if (x.isForUpdate()) {
println();
print("FOR UPDATE");
}
if (select.isLockInShareMode()) {
if (x.isLockInShareMode()) {
println();
print("LOCK IN SHARE MODE");
}
@@ -675,6 +676,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1036,6 +1038,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1067,6 +1070,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1089,6 +1093,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1141,6 +1146,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -274,6 +274,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -438,30 +439,40 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
x.getLeft().accept(this);
if (x.getJoinType() == JoinType.COMMA) {
print(",");
print(", ");
x.getRight().accept(this);
} else {
print(" ");
print(JoinType.toString(x.getJoinType()));
}
print(" ");
x.getRight().accept(this);
if (x.getCondition() != null) {
print(" ON ");
x.getCondition().accept(this);
print(" ");
}
if (x.getUsing().size() > 0) {
print(" USING (");
printAndAccept(x.getUsing(), ", ");
print(")");
}
if (x.getFlashback() != null) {
boolean isRoot = x.getParent() instanceof SQLSelectQueryBlock;
if (isRoot) {
incrementIndent();
}
println();
x.getFlashback().accept(this);
print(JoinType.toString(x.getJoinType()));
print(" ");
x.getRight().accept(this);
if (isRoot) {
decrementIndent();
}
if (x.getCondition() != null) {
print(" ON ");
x.getCondition().accept(this);
print(" ");
}
if (x.getUsing().size() > 0) {
print(" USING (");
printAndAccept(x.getUsing(), ", ");
print(")");
}
if (x.getFlashback() != null) {
println();
x.getFlashback().accept(this);
}
}
return false;
@@ -529,58 +540,60 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
return super.visit(select);
}
public boolean visit(OracleSelectQueryBlock select) {
public boolean visit(OracleSelectQueryBlock x) {
print("SELECT ");
if (SQLSetQuantifier.ALL == select.getDistionOption()) {
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
} else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) {
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
} else if (SQLSetQuantifier.UNIQUE == select.getDistionOption()) {
} else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) {
print("UNIQUE ");
}
if (select.getHints().size() > 0) {
if (x.getHints().size() > 0) {
print("/*+");
printAndAccept(select.getHints(), ", ");
printAndAccept(x.getHints(), ", ");
print("*/ ");
}
printSelectList(select.getSelectList());
printSelectList(x.getSelectList());
if (select.getInto() != null) {
if (x.getInto() != null) {
println();
print("INTO ");
select.getInto().accept(this);
x.getInto().accept(this);
}
println();
print("FROM ");
if (select.getFrom() == null) {
if (x.getFrom() == null) {
print("DUAL");
} else {
select.getFrom().accept(this);
x.getFrom().setParent(x);
x.getFrom().accept(this);
}
if (select.getWhere() != null) {
if (x.getWhere() != null) {
println();
print("WHERE ");
select.getWhere().accept(this);
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
if (select.getHierachicalQueryClause() != null) {
if (x.getHierachicalQueryClause() != null) {
println();
select.getHierachicalQueryClause().accept(this);
x.getHierachicalQueryClause().accept(this);
}
if (select.getGroupBy() != null) {
if (x.getGroupBy() != null) {
println();
select.getGroupBy().accept(this);
x.getGroupBy().accept(this);
}
if (select.getModelClause() != null) {
if (x.getModelClause() != null) {
println();
select.getModelClause().accept(this);
x.getModelClause().accept(this);
}
return false;
@@ -603,18 +616,15 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
public boolean visit(OracleSelectSubqueryTableSource x) {
print("(");
incrementIndent();
x.getSelect().accept(this);
println();
x.getSelect().accept(this);
decrementIndent();
println();
print(")");
if (x.getPivot() != null) {
incrementIndent();
println();
x.getPivot().accept(this);
decrementIndent();
}
if (x.getFlashback() != null) {
@@ -655,17 +665,13 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
}
if (x.getSampleClause() != null) {
println();
print(" ");
x.getSampleClause().accept(this);
}
if (x.getPivot() != null) {
incrementIndent();
println();
x.getPivot().accept(this);
decrementIndent();
}
if (x.getFlashback() != null) {
@@ -812,6 +818,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1496,6 +1503,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
incrementIndent();
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -1504,6 +1512,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
incrementIndent();
println();
print("DELETE WHERE ");
x.getDeleteWhere().setParent(x);
x.getDeleteWhere().accept(this);
decrementIndent();
}
@@ -1530,6 +1539,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
incrementIndent();
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -179,6 +179,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -305,6 +306,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -435,6 +437,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -11,38 +11,39 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
super(appender);
}
public boolean visit(SQLServerSelectQueryBlock select) {
public boolean visit(SQLServerSelectQueryBlock x) {
print("SELECT ");
if (SQLSetQuantifier.ALL == select.getDistionOption()) {
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
} else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) {
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
} else if (SQLSetQuantifier.UNIQUE == select.getDistionOption()) {
} else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) {
print("UNIQUE ");
}
if (select.getTop() != null) {
select.getTop().accept(this);
if (x.getTop() != null) {
x.getTop().accept(this);
}
printSelectList(select.getSelectList());
printSelectList(x.getSelectList());
if (select.getFrom() != null) {
if (x.getFrom() != null) {
println();
print("FROM ");
select.getFrom().accept(this);
x.getFrom().accept(this);
}
if (select.getWhere() != null) {
if (x.getWhere() != null) {
println();
print("WHERE ");
select.getWhere().accept(this);
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
if (select.getGroupBy() != null) {
if (x.getGroupBy() != null) {
print(" ");
select.getGroupBy().accept(this);
x.getGroupBy().accept(this);
}
return false;
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.expr.SQLAllExpr;
import com.alibaba.druid.sql.ast.expr.SQLAnyExpr;
import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
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;
@@ -53,11 +54,12 @@ import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
import com.alibaba.druid.sql.ast.expr.SQLSomeExpr;
import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.NotNullConstraint;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLCallStatement;
import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
@@ -66,7 +68,6 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
@@ -200,12 +201,31 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLBinaryOpExpr x) {
SQLObject parent = x.getParent();
boolean isRoot = parent instanceof SQLSelectQueryBlock;
boolean relational = x.getOperator() == SQLBinaryOperator.BooleanAnd
|| x.getOperator() == SQLBinaryOperator.BooleanOr;
if (isRoot && relational) {
incrementIndent();
}
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr left = (SQLBinaryOpExpr) x.getLeft();
boolean leftRational = left.getOperator() == SQLBinaryOperator.BooleanAnd
|| left.getOperator() == SQLBinaryOperator.BooleanOr;
if (left.getOperator().priority > x.getOperator().priority) {
if (leftRational) {
incrementIndent();
}
print('(');
left.accept(this);
print(')');
if (leftRational) {
decrementIndent();
}
} else {
left.accept(this);
}
@@ -213,16 +233,38 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
x.getLeft().accept(this);
}
print(" ");
if (isRoot && relational) {
decrementIndent();
}
if (relational) {
if (isRoot) {
incrementIndent();
}
println();
} else {
print(" ");
}
print(x.getOperator().name);
print(" ");
if (x.getRight() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight();
boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd
|| right.getOperator() == SQLBinaryOperator.BooleanOr;
if (right.getOperator().priority >= x.getOperator().priority) {
if (rightRational) {
incrementIndent();
}
print('(');
right.accept(this);
print(')');
if (rightRational) {
decrementIndent();
}
} else {
right.accept(this);
}
@@ -230,6 +272,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
x.getRight().accept(this);
}
if (isRoot && relational) {
decrementIndent();
}
return false;
}
@@ -446,34 +492,35 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
return false;
}
public boolean visit(SQLSelectQueryBlock select) {
public boolean visit(SQLSelectQueryBlock x) {
print("SELECT ");
if (SQLSetQuantifier.ALL == select.getDistionOption()) {
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
} else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) {
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
} else if (SQLSetQuantifier.UNIQUE == select.getDistionOption()) {
} else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) {
print("UNIQUE ");
}
printSelectList(select.getSelectList());
printSelectList(x.getSelectList());
if (select.getFrom() != null) {
if (x.getFrom() != null) {
println();
print("FROM ");
select.getFrom().accept(this);
x.getFrom().accept(this);
}
if (select.getWhere() != null) {
if (x.getWhere() != null) {
println();
print("WHERE ");
select.getWhere().accept(this);
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
if (select.getGroupBy() != null) {
if (x.getGroupBy() != null) {
print(" ");
select.getGroupBy().accept(this);
x.getGroupBy().accept(this);
}
return false;
@@ -567,13 +614,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
print(' ');
item.accept(this);
}
if (x.getEnable() != null) {
if (x.getEnable().booleanValue()) {
print(" ENABLE");
}
}
return false;
}
@@ -589,6 +636,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -660,6 +708,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}