sql format support lower case

This commit is contained in:
高铁
2015-12-12 20:54:02 +08:00
parent a84c53d4fb
commit de0715bd9c
11 changed files with 691 additions and 621 deletions
@@ -61,7 +61,7 @@ import com.alibaba.druid.util.StringUtils;
public class SQLUtils {
public static FormatOption DEFAULT_FORMAT_OPTION = new FormatOption();
public static FormatOption DEFAULT_UPPCASE_FORMAT_OPTION = new FormatOption();
public static FormatOption DEFAULT_LCASE_FORMAT_OPTION = new FormatOption(false);
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
@@ -106,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;
@@ -146,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;
@@ -269,9 +292,10 @@ public class SQLUtils {
visitor.setParameters(parameters);
}
if (option != null) {
visitor.setUppCase(option.isUppCase());
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
for (int i = 0; i < statementList.size(); i++) {
SQLStatement stmt = statementList.get(i);
@@ -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();
@@ -753,9 +753,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
protected void printFunctionName(String name) {
String upperName = name.toUpperCase();
if (builtInFunctions.contains(upperName)) {
print(ucase ? upperName : name);
print0(ucase ? upperName : name);
} else {
print(name);
print0(name);
}
}
@@ -830,7 +830,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
if (x.getTo() != null) {
print0(ucase ? " TO " : " to ");
if (x.getSubjectType() != null) {
print(x.getSubjectType().name());
print0(x.getSubjectType().name());
print(' ');
}
x.getTo().accept(this);
@@ -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;
}
@@ -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);
}
}
@@ -18,7 +18,6 @@ package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.SQLUtils.FormatOption;
import junit.framework.TestCase;
@@ -33,10 +32,7 @@ public class OdpsSelectTest4 extends TestCase {
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";//
FormatOption option = new FormatOption();
option.setUppCase(false);;
Assert.assertEquals("select total_day_cnt * EXP(-datediff(to_date('20150819', 'yyyymmdd'), last_time, 'dd') / 60)"
+ "\nfrom dual", SQLUtils.formatOdps(sql, option));
+ "\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'");
}
}