Merge pull request #1008 from wenshao/master

sql format support lowercase
This commit is contained in:
wenshao
2015-12-12 20:54:46 +08:00
15 changed files with 1201 additions and 1052 deletions
@@ -60,6 +60,8 @@ import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
public class SQLUtils {
public static FormatOption DEFAULT_FORMAT_OPTION = new FormatOption();
public static FormatOption DEFAULT_LCASE_FORMAT_OPTION = new FormatOption(false);
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
@@ -104,10 +106,21 @@ public class SQLUtils {
String sql = out.toString();
return sql;
}
public static String toMySqlString(SQLObject sqlObject) {
return toMySqlString(sqlObject, null);
}
public static String toMySqlString(SQLObject sqlObject, FormatOption option) {
StringBuilder out = new StringBuilder();
sqlObject.accept(new MySqlOutputVisitor(out));
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
String sql = out.toString();
return sql;
@@ -128,6 +141,10 @@ public class SQLUtils {
public static String formatOdps(String sql) {
return format(sql, JdbcUtils.ODPS);
}
public static String formatOdps(String sql, FormatOption option) {
return format(sql, JdbcUtils.ODPS, option);
}
public static String formatSQLServer(String sql) {
return format(sql, JdbcUtils.SQL_SERVER);
@@ -140,10 +157,22 @@ public class SQLUtils {
String sql = out.toString();
return sql;
}
public static String toPGString(SQLObject sqlObject) {
return toPGString(sqlObject, null);
}
public static String toPGString(SQLObject sqlObject, FormatOption option) {
StringBuilder out = new StringBuilder();
sqlObject.accept(new PGOutputVisitor(out));
PGOutputVisitor visitor = new PGOutputVisitor(out);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
String sql = out.toString();
return sql;
@@ -223,17 +252,25 @@ public class SQLUtils {
}
public static String format(String sql, String dbType) {
return format(sql, dbType, null);
return format(sql, dbType, null, null);
}
public static String format(String sql, String dbType, FormatOption option) {
return format(sql, dbType, null, option);
}
public static String format(String sql, String dbType, List<Object> parameters) {
return format(sql, dbType, parameters, null);
}
public static String format(String sql, String dbType, List<Object> parameters) {
public static String format(String sql, String dbType, List<Object> parameters, FormatOption option) {
try {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
parser.setKeepComments(true);
List<SQLStatement> statementList = parser.parseStatementList();
return toSQLString(statementList, dbType, parameters);
return toSQLString(statementList, dbType, parameters, option);
} catch (ParserException ex) {
LOG.warn("format error", ex);
return sql;
@@ -243,13 +280,22 @@ public class SQLUtils {
public static String toSQLString(List<SQLStatement> statementList, String dbType) {
return toSQLString(statementList, dbType, null);
}
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters) {
return toSQLString(statementList, dbType, parameters, null);
}
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters, FormatOption option) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
if (parameters != null) {
visitor.setParameters(parameters);
}
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
for (int i = 0; i < statementList.size(); i++) {
SQLStatement stmt = statementList.get(i);
@@ -592,4 +638,25 @@ public class SQLUtils {
queryBlock.getSelectList().add(selectItem);
selectItem.setParent(selectItem);
}
public static class FormatOption {
private boolean ucase = true;
public FormatOption() {
}
public FormatOption(boolean ucase) {
this.ucase = ucase;
}
public boolean isUppCase() {
return ucase;
}
public void setUppCase(boolean val) {
this.ucase = val;
}
}
}
@@ -76,6 +76,7 @@ public enum SQLBinaryOperator {
}
public final String name;
public final String name_lcase;
public final int priority;
SQLBinaryOperator(){
@@ -84,6 +85,7 @@ public enum SQLBinaryOperator {
SQLBinaryOperator(String name, int priority){
this.name = name;
this.name_lcase = name.toLowerCase();
this.priority = priority;
}
@@ -121,9 +121,11 @@ public class SQLJoinTableSource extends SQLTableSourceImpl {
CROSS_APPLY("CROSS APPLY");
public final String name;
public final String name_lcase;
JoinType(String name){
this.name = name;
this.name_lcase = name.toLowerCase();
}
public static String toString(JoinType joinType) {
@@ -20,9 +20,11 @@ public enum SQLUnionOperator {
DISTINCT("UNION DISTINCT");
public final String name;
public final String name_lcase;
private SQLUnionOperator(String name){
this.name = name;
this.name_lcase = name.toLowerCase();
}
public String toString() {
@@ -23,9 +23,11 @@ public interface MySqlIndexHint extends MySqlHint {
;
public final String name;
public final String name_lcase;
Option(String name) {
this.name = name;
this.name_lcase = name.toLowerCase();
}
}
}
@@ -28,5 +28,11 @@ public enum MySqlIntervalUnit {
SECOND, SECOND_MICROSECOND,
MICROSECOND
MICROSECOND;
public final String name_lcase;
private MySqlIntervalUnit() {
this.name_lcase = name().toLowerCase();
}
}
File diff suppressed because it is too large Load Diff
@@ -108,7 +108,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor implemen
}
if (isNumber) {
String realName = name.substring(0, pos);
print(realName);
print0(realName);
incrementReplaceCunt();
return false;
}
@@ -127,12 +127,12 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor implemen
if (numberCount > 1) {
int numPos = name.length() - numberCount;
String realName = name.substring(0, numPos);
print(realName);
print0(realName);
incrementReplaceCunt();
return false;
}
}
print(name);
print0(name);
return false;
}
@@ -200,7 +200,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor implemen
protected void printValuesList(MySqlInsertStatement x) {
List<ValuesClause> valuesList = x.getValuesList();
print("VALUES ");
print0(ucase ? "VALUES " : "values ");
incrementIndent();
valuesList.get(0).accept(this);
decrementIndent();
@@ -79,21 +79,21 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
public boolean visit(OdpsCreateTableStatement x) {
if (x.isIfNotExiists()) {
print("CREATE TABLE IF NOT EXISTS ");
print0(ucase ? "CREATE TABLE IF NOT EXISTS " : "create table if not exists ");
} else {
print("CREATE TABLE ");
print0(ucase ? "CREATE TABLE " : "create table ");
}
x.getName().accept(this);
if (x.getLike() != null) {
print(" LIKE ");
print0(ucase ? " LIKE " : " like ");
x.getLike().accept(this);
}
int size = x.getTableElementList().size();
if (size > 0) {
print(" (");
print0(" (");
incrementIndent();
println();
for (int i = 0; i < size; ++i) {
@@ -101,7 +101,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
element.accept(this);
if (i != size - 1) {
print(",");
print(',');
}
if (this.isPrettyFormat() && element.hasAfterComment()) {
print(' ');
@@ -114,19 +114,19 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
decrementIndent();
println();
print(")");
print(')');
}
if (x.getComment() != null) {
println();
print("COMMENT ");
print0(ucase ? "COMMENT " : "comment ");
x.getComment().accept(this);
}
int partitionSize = x.getPartitionColumns().size();
if (partitionSize > 0) {
println();
print("PARTITIONED BY (");
print0(ucase ? "PARTITIONED BY (" : "partitioned by (");
incrementIndent();
println();
for (int i = 0; i < partitionSize; ++i) {
@@ -147,18 +147,18 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
decrementIndent();
println();
print(")");
print(')');
}
if (x.getLifecycle() != null) {
println();
print("LIFECYCLE ");
print0(ucase ? "LIFECYCLE " : "lifecycle ");
x.getLifecycle().accept(this);
}
if (x.getSelect() != null) {
println();
print("AS");
print0(ucase ? "AS" : "as");
println();
x.getSelect().accept(this);
}
@@ -185,14 +185,14 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsInsertStatement x) {
if (x.getFrom() != null) {
print("FROM (");
print0(ucase ? "FROM (" : "from (");
incrementIndent();
println();
x.getFrom().getSelect().accept(this);
decrementIndent();
println();
print(") ");
print(x.getFrom().getAlias());
print0(") ");
print0(x.getFrom().getAlias());
println();
}
@@ -218,29 +218,29 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
println();
}
if (x.isOverwrite()) {
print("INSERT OVERWRITE TABLE ");
print0(ucase ? "INSERT OVERWRITE TABLE " : "insert overwrite table ");
} else {
print("INSERT INTO TABLE ");
print0(ucase ? "INSERT INTO TABLE " : "insert into table ");
}
x.getTableSource().accept(this);
int partitions = x.getPartitions().size();
if (partitions > 0) {
print(" PARTITION (");
print0(ucase ? " PARTITION (" : " partition (");
for (int i = 0; i < partitions; ++i) {
if (i != 0) {
print(", ");
print0(", ");
}
SQLAssignItem assign = x.getPartitions().get(i);
assign.getTarget().accept(this);
if (assign.getValue() != null) {
print("=");
print('=');
assign.getValue().accept(this);
}
}
print(")");
print(')');
}
println();
x.getQuery().accept(this);
@@ -250,7 +250,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
public boolean visit(SQLCaseExpr x) {
incrementIndent();
print("CASE ");
print0(ucase ? "CASE " : "case ");
if (x.getValueExpr() != null) {
x.getValueExpr().accept(this);
println();
@@ -263,13 +263,13 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (x.getElseExpr() != null) {
println();
print("ELSE ");
print0(ucase ? "ELSE " : "else ");
x.getElseExpr().accept(this);
}
decrementIndent();
println();
print("END");
print0(ucase ? "END" : "end");
return false;
}
@@ -277,7 +277,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
public boolean visit(SQLSelectGroupByClause x) {
int itemSize = x.getItems().size();
if (itemSize > 0) {
print("GROUP BY ");
print0(ucase ? "GROUP BY " : "group by ");
incrementIndent();
for (int i = 0; i < itemSize; ++i) {
if (i != 0) {
@@ -290,7 +290,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (x.getHaving() != null) {
println();
print("HAVING ");
print0(ucase ? "HAVING " : "having ");
x.getHaving().accept(this);
}
return false;
@@ -309,7 +309,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
println();
print(", ");
print0(", ");
}
selectItem.accept(this);
@@ -324,17 +324,17 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(SQLSubqueryTableSource x) {
print("(");
print('(');
incrementIndent();
println();
x.getSelect().accept(this);
decrementIndent();
println();
print(")");
print(')');
if (x.getAlias() != null) {
print(' ');
print(x.getAlias());
print0(x.getAlias());
}
return false;
@@ -345,31 +345,31 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
x.getLeft().accept(this);
if (x.getJoinType() == JoinType.COMMA) {
print(",");
print(',');
} else {
println();
printJoinType(x.getJoinType());
}
print(" ");
print(' ');
x.getRight().accept(this);
if (x.getCondition() != null) {
println();
print("ON ");
print0(ucase ? "ON " : "on ");
incrementIndent();
x.getCondition().accept(this);
decrementIndent();
}
if (x.getUsing().size() > 0) {
print(" USING (");
print0(ucase ? " USING (" : " using (");
printAndAccept(x.getUsing(), ", ");
print(")");
print(')');
}
if (x.getAlias() != null) {
print(" AS ");
print(x.getAlias());
print0(ucase ? " AS " : " as ");
print0(x.getAlias());
}
return false;
@@ -385,7 +385,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
x.getExpr().accept(this);
println();
print("AS (");
print0(ucase ? "AS (" : "as (");
int aliasSize = x.getAliasList().size();
if (aliasSize > 5) {
@@ -398,17 +398,17 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (aliasSize > 5) {
println(",");
} else {
print(", ");
print0(", ");
}
}
print(x.getAliasList().get(i));
print0(x.getAliasList().get(i));
}
if (aliasSize > 5) {
decrementIndent();
println();
}
print(")");
print(')');
return false;
}
@@ -420,7 +420,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsShowPartitionsStmt x) {
print("SHOW PARTITIONS ");
print0(ucase ? "SHOW PARTITIONS " : "show partitions ");
x.getTableSource().accept(this);
return false;
}
@@ -432,7 +432,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsShowStatisticStmt x) {
print("SHOW STATISTIC ");
print0(ucase ? "SHOW STATISTIC " : "show statistic ");
x.getTableSource().accept(this);
return false;
}
@@ -444,20 +444,20 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsSetLabelStatement x) {
print("SET LABEL ");
print(x.getLabel());
print(" TO ");
print0(ucase ? "SET LABEL " : "set label ");
print0(x.getLabel());
print0(ucase ? " TO " : " to ");
if (x.getUser() != null) {
print("USER ");
print0(ucase ? "USER " : "user ");
x.getUser().accept(this);
} else if (x.getTable() != null) {
print("TABLE ");
print0(ucase ? "TABLE " : "table ");
x.getTable().accept(this);
if (x.getColumns().size() > 0) {
print("(");
print('(');
printAndAccept(x.getColumns(), ", ");
print(")");
print(')');
}
}
@@ -476,7 +476,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
println();
}
print("SELECT ");
print0(ucase ? "SELECT " : "select ");
List<SQLHint> hints = x.getHintsDirect();
if (hints != null) {
@@ -485,24 +485,24 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
print0(ucase ? "ALL " : "all ");
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
print0(ucase ? "DISTINCT " : "distinct ");
} else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) {
print("UNIQUE ");
print0(ucase ? "UNIQUE " : "unique ");
}
printSelectList(x.getSelectList());
if (x.getFrom() != null) {
println();
print("FROM ");
print0(ucase ? "FROM " : "from ");
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
println();
print("WHERE ");
print0(ucase ? "WHERE " : "where ");
if (x.getWhere().hasBeforeComment() && isPrettyFormat()) {
printlnComments(x.getWhere().getBeforeCommentsDirect());
}
@@ -527,18 +527,18 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (x.getDistributeBy() != null) {
println();
print("DISTRIBUTE BY ");
print0(ucase ? "DISTRIBUTE BY " : "distribute by ");
x.getDistributeBy().accept(this);
if (!x.getSortBy().isEmpty()) {
print(" SORT BY ");
print0(ucase ? " SORT BY " : " sort by ");
printAndAccept(x.getSortBy(), ", ");
}
}
if (x.getLimit() != null) {
println();
print("LIMIT ");
print0(ucase ? "LIMIT " : "limit ");
x.getLimit().accept(this);
}
@@ -548,7 +548,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
public boolean visit(SQLOrderBy x) {
int itemSize = x.getItems().size();
if (itemSize > 0) {
print("ORDER BY ");
print0(ucase ? "ORDER BY " : "order by ");
incrementIndent();
for (int i = 0; i < itemSize; ++i) {
if (i != 0) {
@@ -569,16 +569,16 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsAnalyzeTableStatement x) {
print("ANALYZE TABLE ");
print0(ucase ? "ANALYZE TABLE " : "analyze table ");
x.getTable().accept(this);
if (x.getPartition().size() > 0) {
print(" PARTITION (");
print0(ucase ? " PARTITION (" : " partition (");
printAndAccept(x.getPartition(), ", ");
print(")");
print(')');
}
print(" COMPUTE STATISTICS");
print0(ucase ? " COMPUTE STATISTICS" : " compute statistics");
return false;
}
@@ -590,9 +590,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsAddStatisticStatement x) {
print("ADD STATISTIC ");
print0(ucase ? "ADD STATISTIC " : "add statistic ");
x.getTable().accept(this);
print(" ");
print(' ');
x.getStatisticClause().accept(this);
return false;
@@ -605,9 +605,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsRemoveStatisticStatement x) {
print("REMOVE STATISTIC ");
print0(ucase ? "REMOVE STATISTIC " : "remove statistic ");
x.getTable().accept(this);
print(" ");
print(' ');
x.getStatisticClause().accept(this);
return false;
@@ -620,7 +620,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.TableCount x) {
print("TABLE_COUNT");
print0(ucase ? "TABLE_COUNT" : "table_count");
return false;
}
@@ -631,7 +631,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.ExpressionCondition x) {
print("EXPRESSION_CONDITION ");
print0(ucase ? "EXPRESSION_CONDITION " : "expression_condition ");
x.getExpr().accept(this);
return false;
}
@@ -643,7 +643,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.NullValue x) {
print("NULL_VALUE ");
print0(ucase ? "NULL_VALUE " : "null_value ");
x.getColumn().accept(this);
return false;
}
@@ -655,7 +655,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.ColumnSum x) {
print("COLUMN_SUM ");
print0(ucase ? "COLUMN_SUM " : "column_sum ");
x.getColumn().accept(this);
return false;
}
@@ -667,7 +667,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.ColumnMax x) {
print("COLUMN_MAX ");
print0(ucase ? "COLUMN_MAX " : "column_max ");
x.getColumn().accept(this);
return false;
}
@@ -679,7 +679,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsStatisticClause.ColumnMin x) {
print("COLUMN_MIN ");
print0(ucase ? "COLUMN_MIN " : "column_min ");
x.getColumn().accept(this);
return false;
}
@@ -691,19 +691,19 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsReadStatement x) {
print("READ ");
print0(ucase ? "READ " : "read ");
x.getTable().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
print0(" (");
printAndAccept(x.getColumns(), ", ");
print(")");
print(')');
}
if (x.getPartition().size() > 0) {
print(" PARTITION (");
print0(ucase ? " PARTITION (" : " partition (");
printAndAccept(x.getPartition(), ", ");
print(")");
print(')');
}
if (x.getRowCount() != null) {
@@ -717,33 +717,34 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
public boolean visit(SQLMethodInvokeExpr x) {
if (x.getOwner() != null) {
x.getOwner().accept(this);
print(":");
print(':');
}
printFunctionName(x.getMethodName());
print("(");
print('(');
printAndAccept(x.getParameters(), ", ");
print(")");
print(')');
return false;
}
protected void printJoinType(JoinType joinType) {
if (joinType.equals(JoinType.LEFT_OUTER_JOIN)) {
print("LEFT OUTER JOIN");
print0(ucase ? "LEFT OUTER JOIN" : "left outer join");
} else if (joinType.equals(JoinType.RIGHT_OUTER_JOIN)) {
print("RIGHT OUTER JOIN");
print0(ucase ? "RIGHT OUTER JOIN" : "right outer join");
} else if (joinType.equals(JoinType.FULL_OUTER_JOIN)) {
print("FULL OUTER JOIN");
print0(ucase ? "FULL OUTER JOIN" : "full outer join");
} else {
print(JoinType.toString(joinType));
print0(ucase ? joinType.name : joinType.name_lcase);
}
}
public boolean visit(SQLDataType x) {
print(x.getName().toUpperCase());
String dataTypeName = x.getName();
print0(ucase ? dataTypeName.toUpperCase() : dataTypeName.toLowerCase());
if (x.getArguments().size() > 0) {
print("(");
print('(');
printAndAccept(x.getArguments(), ", ");
print(")");
print(')');
}
return false;
@@ -752,9 +753,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
protected void printFunctionName(String name) {
String upperName = name.toUpperCase();
if (builtInFunctions.contains(upperName)) {
print(upperName);
print0(ucase ? upperName : name);
} else {
print(name);
print0(name);
}
}
@@ -765,14 +766,14 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsShowGrantsStmt x) {
print("SHOW GRANTS");
print0(ucase ? "SHOW GRANTS" : "show grants");
if (x.getUser() != null) {
print(" FOR ");
print0(ucase ? " FOR " : " for ");
x.getUser().accept(this);
}
if (x.getObjectType() != null) {
print(" ON TYPE ");
print0(ucase ? " ON TYPE " : " on type ");
x.getObjectType().accept(this);
}
return false;
@@ -785,7 +786,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsListStmt x) {
print("LIST ");
print0(ucase ? "LIST " : "list ");
if (x.getObject() != null) {
x.getObject().accept(this);
}
@@ -799,44 +800,44 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsGrantStmt x) {
print("GRANT ");
print0(ucase ? "GRANT " : "grant ");
if (x.isSuper()) {
print("SUPER ");
print0(ucase ? "SUPER " : "super ");
}
if (x.isLabel()) {
print("LABEL ");
print0(ucase ? "LABEL " : "label ");
x.getLabel().accept(this);;
} else {
printAndAccept(x.getPrivileges(), ", ");
}
if (x.getOn() != null) {
print(" ON ");
print0(ucase ? " ON " : " on ");
if (x.getObjectType() != null) {
print(x.getObjectType().name());
print0(ucase ? x.getObjectType().name() : x.getObjectType().name().toLowerCase());
print(' ');
}
x.getOn().accept(this);
if (x.getColumns().size() > 0) {
print("(");
print('(');
printAndAccept(x.getColumns(), ", ");
print(")");
print(')');
}
}
if (x.getTo() != null) {
print(" TO ");
print0(ucase ? " TO " : " to ");
if (x.getSubjectType() != null) {
print(x.getSubjectType().name());
print0(x.getSubjectType().name());
print(' ');
}
x.getTo().accept(this);
}
if (x.getExpire() != null) {
print(" WITH EXP ");
print0(ucase ? " WITH EXP " : " with exp ");
x.getExpire().accept(this);
}
@@ -850,9 +851,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
@Override
public boolean visit(OdpsDescStmt x) {
print("DESC ");
print0(ucase ? "DESC " : "desc ");
if (x.getObjectType() != null) {
print(x.getObjectType().name());
print0(x.getObjectType().name());
print(' ');
}
@@ -861,9 +862,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
}
if (x.getPartition().size() > 0) {
print(" PARTITION (");
print0(ucase ? " PARTITION (" : " partition (");
printAndAccept(x.getPartition(), ", ");
print(")");
print(')');
}
return false;
}
@@ -105,7 +105,7 @@ public class OracleToMySqlOutputVisitor extends OracleOutputVisitor {
if (limit != null) {
select.accept(this);
println();
print("LIMIT ");
print0(ucase ? "LIMIT " : "limit ");
print(limit);
return false;
}
@@ -60,16 +60,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(WindowClause x) {
print("WINDOW ");
print0(ucase ? "WINDOW " : "window ");
x.getName().accept(this);
print(" AS ");
print0(ucase ? " AS " : " as ");
for (int i = 0; i < x.getDefinition().size(); ++i) {
if (i != 0) {
println(", ");
}
print("(");
print('(');
x.getDefinition().get(i).accept(this);
print(")");
print(')');
}
return false;
}
@@ -81,14 +81,14 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(FetchClause x) {
print("FETCH ");
print0(ucase ? "FETCH " : "fetch ");
if (FetchClause.Option.FIRST.equals(x.getOption())) {
print("FIRST ");
print0(ucase ? "FIRST " : "first ");
} else if (FetchClause.Option.NEXT.equals(x.getOption())) {
print("NEXT ");
print0(ucase ? "NEXT " : "next ");
}
x.getCount().accept(this);
print(" ROWS ONLY");
print0(ucase ? " ROWS ONLY" : " rows only");
return false;
}
@@ -99,11 +99,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(ForClause x) {
print("FOR ");
print0(ucase ? "FOR " : "for ");
if (ForClause.Option.UPDATE.equals(x.getOption())) {
print("UPDATE ");
print0(ucase ? "UPDATE " : "update ");
} else if (ForClause.Option.SHARE.equals(x.getOption())) {
print("SHARE ");
print0(ucase ? "SHARE " : "share ");
}
if (x.getOf().size() > 0) {
@@ -116,7 +116,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
}
if (x.isNoWait()) {
print(" NOWAIT");
print0(ucase ? " NOWAIT" : " nowait");
}
return false;
@@ -132,20 +132,20 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
x.getName().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
print0(" (");
printAndAccept(x.getColumns(), ", ");
print(")");
print(')');
}
println();
print("AS");
print0(ucase ? "AS" : "as");
println();
print("(");
print('(');
incrementIndent();
println();
x.getQuery().accept(this);
decrementIndent();
println();
print(")");
print(')');
return false;
}
@@ -157,9 +157,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGWithClause x) {
print("WITH");
print0(ucase ? "WITH" : "with");
if (x.isRecursive()) {
print(" RECURSIVE ");
print0(ucase ? " RECURSIVE " : " recursive ");
}
incrementIndent();
println();
@@ -174,15 +174,15 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
}
print("SELECT ");
print0(ucase ? "SELECT " : "select ");
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
print0(ucase ? "ALL " : "all ");
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
print0(ucase ? "DISTINCT " : "distinct ");
if (x.getDistinctOn() != null && x.getDistinctOn().size() > 0) {
print("ON ");
print0(ucase ? "ON " : "on ");
printAndAccept(x.getDistinctOn(), ", ");
}
}
@@ -193,21 +193,22 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
if (x.getIntoOption() != null) {
print(x.getIntoOption().name());
print(" ");
print(' ');
}
print0(ucase ? "INTO " : "into ");
x.getInto().accept(this);
}
if (x.getFrom() != null) {
println();
print("FROM ");
print0(ucase ? "FROM " : "from ");
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
println();
print("WHERE ");
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -229,7 +230,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getLimit() != null) {
println();
print("LIMIT ");
print0(ucase ? "LIMIT " : "limit ");
x.getLimit().accept(this);
}
@@ -248,26 +249,26 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(SQLTruncateStatement x) {
print("TRUNCATE TABLE ");
print0(ucase ? "TRUNCATE TABLE " : "truncate table ");
if (x.isOnly()) {
print("ONLY ");
print0(ucase ? "ONLY " : "only ");
}
printlnAndAccept(x.getTableSources(), ", ");
if (x.getRestartIdentity() != null) {
if (x.getRestartIdentity().booleanValue()) {
print(" RESTART IDENTITY");
print0(ucase ? " RESTART IDENTITY" : " restart identity");
} else {
print(" CONTINUE IDENTITY");
print0(ucase ? " CONTINUE IDENTITY" : " continue identity");
}
}
if (x.getCascade() != null) {
if (x.getCascade().booleanValue()) {
print(" CASCADE");
print0(ucase ? " CASCADE" : " cascade");
} else {
print(" RESTRICT");
print0(ucase ? " RESTRICT" : " restrict");
}
}
return false;
@@ -285,28 +286,28 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
}
print("DELETE FROM ");
print0(ucase ? "DELETE FROM " : "delete from ");
if (x.isOnly()) {
print("ONLY ");
print0(ucase ? "ONLY " : "only ");
}
x.getTableName().accept(this);
if (x.getAlias() != null) {
print(" AS ");
print0(ucase ? " AS " : " as ");
print(x.getAlias());
}
if (x.getUsing().size() > 0) {
println();
print("USING ");
print0(ucase ? "USING " : "using ");
printAndAccept(x.getUsing(), ", ");
}
if (x.getWhere() != null) {
println();
print("WHERE ");
print0(ucase ? "WHERE " : "where ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
@@ -315,7 +316,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.isReturning()) {
println();
print("RETURNING *");
print0(ucase ? "RETURNING *" : "returning *");
}
return false;
@@ -333,30 +334,30 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
}
print("INSERT INTO ");
print0(ucase ? "INSERT INTO " : "insert into ");
x.getTableSource().accept(this);
if (x.getColumns().size() > 0) {
incrementIndent();
println();
print("(");
print('(');
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
if (i % 5 == 0) {
println();
}
print(", ");
print0(", ");
}
x.getColumns().get(i).accept(this);
}
print(")");
print(')');
decrementIndent();
}
if (x.getValues() != null) {
println();
print("VALUES ");
print0(ucase ? "VALUES " : "values ");
printlnAndAccept(x.getValuesList(), ", ");
} else {
if (x.getQuery() != null) {
@@ -367,7 +368,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getReturning() != null) {
println();
print("RETURNING ");
print0(ucase ? "RETURNING " : "returning ");
x.getReturning().accept(this);
}
@@ -401,32 +402,32 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
}
print("UPDATE ");
print0(ucase ? "UPDATE " : "update ");
if (x.isOnly()) {
print("ONLY ");
print0(ucase ? "ONLY " : "only ");
}
x.getTableSource().accept(this);
println();
print("SET ");
print0(ucase ? "SET " : "set ");
for (int i = 0, size = x.getItems().size(); i < size; ++i) {
if (i != 0) {
print(", ");
print0(", ");
}
x.getItems().get(i).accept(this);
}
if (x.getFrom() != null) {
println();
print("FROM ");
print0(ucase ? "FROM " : "from ");
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
println();
print("WHERE ");
print0(ucase ? "WHERE " : "where ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
@@ -435,7 +436,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getReturning().size() > 0) {
println();
print("RETURNING ");
print0(ucase ? "RETURNING " : "returning ");
printAndAccept(x.getReturning(), ", ");
}
@@ -455,7 +456,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGParameter x) {
x.getName().accept(this);
print(" ");
print(' ');
x.getDataType().accept(this);
@@ -467,7 +468,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
x.getExpr().accept(this);
if (x.getAlias() != null) {
print(" AS ");
print0(ucase ? " AS " : " as ");
print(x.getAlias());
}
@@ -489,7 +490,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
public boolean visit(PGLimit x) {
x.getRowCount().accept(this);
if (x.getOffset() != null) {
print(" OFFSET ");
print0(ucase ? " OFFSET " : " offset ");
x.getOffset().accept(this);
}
return false;
@@ -508,7 +509,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGTypeCastExpr x) {
x.getExpr().accept(this);
print("::");
print0("::");
x.getDataType().accept(this);
return false;
}
@@ -520,9 +521,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGValuesQuery x) {
print("VALUES(");
print0(ucase ? "VALUES(" : "values(");
printAndAccept(x.getValues(), ", ");
print(")");
print(')');
return false;
}
@@ -533,17 +534,17 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGExtractExpr x) {
print("EXTRACT (");
print0(ucase ? "EXTRACT (" : "extract (");
print(x.getField().name());
print(" FROM ");
print0(ucase ? " FROM " : " from ");
x.getSource().accept(this);
print(")");
print(')');
return false;
}
@Override
public boolean visit(PGBoxExpr x) {
print("BOX ");
print0(ucase ? "BOX " : "box ");
x.getValue().accept(this);
return false;
}
@@ -555,7 +556,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGPointExpr x) {
print("POINT ");
print0(ucase ? "POINT " : "point ");
x.getValue().accept(this);
return false;
}
@@ -567,7 +568,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGMacAddrExpr x) {
print("macaddr ");
print0("macaddr ");
x.getValue().accept(this);
return false;
}
@@ -579,7 +580,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGInetExpr x) {
print("inet ");
print0("inet ");
x.getValue().accept(this);
return false;
}
@@ -591,7 +592,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGCidrExpr x) {
print("cidr ");
print0("cidr ");
x.getValue().accept(this);
return false;
}
@@ -603,7 +604,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGPolygonExpr x) {
print("polygon ");
print0("polygon ");
x.getValue().accept(this);
return false;
}
@@ -615,7 +616,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGCircleExpr x) {
print("circle ");
print0("circle ");
x.getValue().accept(this);
return false;
}
@@ -627,7 +628,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGLineSegmentsExpr x) {
print("lseg ");
print0("lseg ");
x.getValue().accept(this);
return false;
}
@@ -639,7 +640,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGIntervalExpr x) {
print("INTERVAL ");
print0(ucase ? "INTERVAL " : "interval ");
x.getValue().accept(this);
return true;
}
@@ -651,7 +652,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(SQLBinaryExpr x) {
print("B'");
print0(ucase ? "B'" : "b'");
print(x.getValue());
print('\'');
@@ -665,7 +666,7 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
@Override
public boolean visit(PGShowStatement x) {
print("SHOW ");
print0(ucase ? "SHOW " : "show ");
x.getExpr().accept(this);
return false;
}
File diff suppressed because it is too large Load Diff
@@ -68,5 +68,14 @@ public class MySqlSelectTest_14 extends MysqlTest {
+ "\n\tFROM t2"//
+ "\n\t)", //
output);
String output_lcase = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *" //
+ "\nfrom t1"//
+ "\nwhere column1 = ("//
+ "\n\tselect column1"//
+ "\n\tfrom t2"//
+ "\n\t)", //
output_lcase);
}
}
@@ -29,5 +29,10 @@ public class OdpsSelectTest4 extends TestCase {
+ "\nFROM dual", SQLUtils.formatOdps(sql));
}
public void test_distribute_by_lcase() throws Exception {
String sql = "select total_day_cnt * EXP(-datediff(to_date('20150819', 'yyyymmdd'), last_time, 'dd') / 60) from dual";//
Assert.assertEquals("select total_day_cnt * EXP(-datediff(to_date('20150819', 'yyyymmdd'), last_time, 'dd') / 60)"
+ "\nfrom dual", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
@@ -32,13 +33,13 @@ public class PGSelectIntoTest extends PGTest {
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
statemen.accept(visitor);
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
@@ -49,5 +50,17 @@ public class PGSelectIntoTest extends PGTest {
Assert.assertTrue(visitor.getColumns().contains(new Column("films", "*")));
Assert.assertTrue(visitor.getColumns().contains(new Column("films", "date_prod")));
String result = SQLUtils.toPGString(stmt);
Assert.assertEquals(result,"SELECT *" //
+ "\nINTO films_recent" //
+ "\nFROM films" //
+ "\nWHERE date_prod >= '2002-01-01'");
String result_lcase = SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals(result_lcase,"select *" //
+ "\ninto films_recent" //
+ "\nfrom films" //
+ "\nwhere date_prod >= '2002-01-01'");
}
}