mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
odps sql format support lowercase option
This commit is contained in:
@@ -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_UPPCASE_FORMAT_OPTION = new FormatOption();
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
|
||||
|
||||
@@ -128,6 +130,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);
|
||||
@@ -223,17 +229,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 +257,21 @@ 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) {
|
||||
visitor.setUppCase(option.isUppCase());
|
||||
}
|
||||
|
||||
for (int i = 0; i < statementList.size(); i++) {
|
||||
SQLStatement stmt = statementList.get(i);
|
||||
@@ -592,4 +614,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user