diff --git a/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java b/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java index f4afd2ecb4..fee3f8bc37 100644 --- a/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java +++ b/src/main/java/com/alibaba/druid/filter/logging/LogFilter.java @@ -38,11 +38,12 @@ import com.alibaba.druid.sql.SQLUtils; import com.alibaba.druid.sql.SQLUtils.FormatOption; import com.alibaba.druid.util.JdbcUtils; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + /** * @author wenshao [szujobs@hotmail.com] */ public abstract class LogFilter extends FilterEventAdapter implements LogFilterMBean { - protected String dataSourceLoggerName = "druid.sql.DataSource"; protected String connectionLoggerName = "druid.sql.Connection"; protected String statementLoggerName = "druid.sql.Statement"; @@ -81,7 +82,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM private boolean resultSetLogEnabled = true; private boolean resultSetLogErrorEnabled = true; - private FormatOption statementSqlFormatOption = SQLUtils.DEFAULT_LCASE_FORMAT_OPTION; + private FormatOption statementSqlFormatOption = new FormatOption(false, true); private boolean statementLogSqlPrettyFormat = false; protected DataSourceProxy dataSource; @@ -565,8 +566,7 @@ public abstract class LogFilter extends FilterEventAdapter implements LogFilterM } String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType(); - String formattedSql = SQLUtils.format(sql, dbType, parameters, this.statementSqlFormatOption, - this.statementLogSqlPrettyFormat); + String formattedSql = SQLUtils.format(sql, dbType, parameters, this.statementSqlFormatOption); statementLog("{conn-" + statement.getConnectionProxy().getId() + ", " + stmtId(statement) + "} executed. " + formattedSql); } diff --git a/src/main/java/com/alibaba/druid/sql/SQLUtils.java b/src/main/java/com/alibaba/druid/sql/SQLUtils.java index 7d1e147e7e..b89d82e989 100644 --- a/src/main/java/com/alibaba/druid/sql/SQLUtils.java +++ b/src/main/java/com/alibaba/druid/sql/SQLUtils.java @@ -57,25 +57,25 @@ import com.alibaba.druid.util.StringUtils; import com.alibaba.druid.util.Utils; public class SQLUtils { - public static FormatOption DEFAULT_FORMAT_OPTION = new FormatOption(); - public static FormatOption DEFAULT_LCASE_FORMAT_OPTION = new FormatOption(false); + public static FormatOption DEFAULT_FORMAT_OPTION = new FormatOption(true, true); + public static FormatOption DEFAULT_LCASE_FORMAT_OPTION = new FormatOption(false, true); private final static Log LOG = LogFactory.getLog(SQLUtils.class); public static String toSQLString(SQLObject sqlObject, String dbType) { - return toSQLString(sqlObject, dbType, null, false); + return toSQLString(sqlObject, dbType, null); } - - public static String toSQLString(SQLObject sqlObject, String dbType, FormatOption option, boolean prettyFormat) { + + public static String toSQLString(SQLObject sqlObject, String dbType, FormatOption option) { StringBuilder out = new StringBuilder(); SQLASTOutputVisitor visitor = createOutputVisitor(out, dbType); - + if (option == null) { option = DEFAULT_FORMAT_OPTION; } visitor.setUppCase(option.isUppCase()); - visitor.setPrettyFormat(prettyFormat); - + visitor.setPrettyFormat(option.isPrettyFormat()); + sqlObject.accept(visitor); String sql = out.toString(); @@ -91,19 +91,19 @@ public class SQLUtils { } public static String toOdpsString(SQLObject sqlObject) { - return toOdpsString(sqlObject, null, false); - } - - public static String toOdpsString(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.ODPS, option, prettyFormat); - } - - public static String toMySqlString(SQLObject sqlObject) { - return toMySqlString(sqlObject, null, false); + return toOdpsString(sqlObject, null); } - public static String toMySqlString(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.MYSQL, option, prettyFormat); + public static String toOdpsString(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.ODPS, option); + } + + public static String toMySqlString(SQLObject sqlObject) { + return toMySqlString(sqlObject, null); + } + + public static String toMySqlString(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.MYSQL, option); } public static SQLExpr toMySqlExpr(String sql) { @@ -113,7 +113,7 @@ public class SQLUtils { public static String formatMySql(String sql) { return format(sql, JdbcConstants.MYSQL); } - + public static String formatMySql(String sql, FormatOption option) { return format(sql, JdbcConstants.MYSQL, option); } @@ -121,7 +121,7 @@ public class SQLUtils { public static String formatOracle(String sql) { return format(sql, JdbcConstants.ORACLE); } - + public static String formatOracle(String sql, FormatOption option) { return format(sql, JdbcConstants.ORACLE, option); } @@ -129,7 +129,7 @@ public class SQLUtils { public static String formatOdps(String sql) { return format(sql, JdbcConstants.ODPS); } - + public static String formatOdps(String sql, FormatOption option) { return format(sql, JdbcConstants.ODPS, option); } @@ -139,37 +139,37 @@ public class SQLUtils { } public static String toOracleString(SQLObject sqlObject) { - return toOracleString(sqlObject, null, false); - } - - public static String toOracleString(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.ORACLE, option, prettyFormat); - } - - public static String toPGString(SQLObject sqlObject) { - return toPGString(sqlObject, null, false); + return toOracleString(sqlObject, null); } - public static String toPGString(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.POSTGRESQL, option, prettyFormat); + public static String toOracleString(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.ORACLE, option); + } + + public static String toPGString(SQLObject sqlObject) { + return toPGString(sqlObject, null); + } + + public static String toPGString(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.POSTGRESQL, option); } public static String toDB2String(SQLObject sqlObject) { - return toDB2String(sqlObject, null, false); + return toDB2String(sqlObject, null); } - - public static String toDB2String(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.DB2, option, prettyFormat); + + public static String toDB2String(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.DB2, option); } public static String toSQLServerString(SQLObject sqlObject) { - return toSQLServerString(sqlObject, null, false); + return toSQLServerString(sqlObject, null); } - - public static String toSQLServerString(SQLObject sqlObject, FormatOption option, boolean prettyFormat) { - return toSQLString(sqlObject, JdbcConstants.SQL_SERVER, option, prettyFormat); + + public static String toSQLServerString(SQLObject sqlObject, FormatOption option) { + return toSQLString(sqlObject, JdbcConstants.SQL_SERVER, option); } - + public static String formatPGSql(String sql, FormatOption option) { return format(sql, JdbcConstants.POSTGRESQL, option); } @@ -184,29 +184,29 @@ public class SQLUtils { return expr; } - + public static SQLSelectOrderByItem toOrderByItem(String sql, String dbType) { SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType); SQLSelectOrderByItem orderByItem = parser.parseSelectOrderByItem(); - + if (parser.getLexer().token() != Token.EOF) { throw new ParserException("illegal sql expr : " + sql); } - + return orderByItem; } - + public static SQLUpdateSetItem toUpdateSetItem(String sql, String dbType) { SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType); SQLUpdateSetItem updateSetItem = parser.parseUpdateSetItem(); - + if (parser.getLexer().token() != Token.EOF) { throw new ParserException("illegal sql expr : " + sql); } - + return updateSetItem; } - + public static SQLSelectItem toSelectItem(String sql, String dbType) { SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType); SQLSelectItem selectItem = parser.parseSelectItem(); @@ -228,25 +228,25 @@ public class SQLUtils { } public static String format(String sql, String dbType) { - return format(sql, dbType, null, null, false); - } - - public static String format(String sql, String dbType, FormatOption option) { - return format(sql, dbType, null, option, false); - } - - public static String format(String sql, String dbType, List parameters) { - return format(sql, dbType, parameters, null, false); + return format(sql, dbType, null, null); } - public static String format(String sql, String dbType, List parameters, FormatOption option, boolean prettyFormat) { + 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 parameters) { + return format(sql, dbType, parameters, null); + } + + public static String format(String sql, String dbType, List parameters, FormatOption option) { try { SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); parser.setKeepComments(true); - + List statementList = parser.parseStatementList(); - return toSQLString(statementList, dbType, parameters, option, null, prettyFormat); + return toSQLString(statementList, dbType, parameters, option); } catch (ParserException ex) { LOG.warn("format error", ex); return sql; @@ -256,36 +256,35 @@ public class SQLUtils { public static String toSQLString(List statementList, String dbType) { return toSQLString(statementList, dbType, (List) null); } - + public static String toSQLString(List statementList, String dbType, FormatOption option) { return toSQLString(statementList, dbType, null, option); } - + public static String toSQLString(List statementList, String dbType, List parameters) { - return toSQLString(statementList, dbType, parameters, DEFAULT_LCASE_FORMAT_OPTION, null, false); + return toSQLString(statementList, dbType, parameters, null, null); } public static String toSQLString(List statementList, String dbType, List parameters, FormatOption option) { - return toSQLString(statementList, dbType, parameters, option, null, false); + return toSQLString(statementList, dbType, parameters, option, null); } public static String toSQLString(List statementList , String dbType , List parameters , FormatOption option - , Map tableMapping - , boolean prettyFormat) { + , Map tableMapping) { 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()); - visitor.setPrettyFormat(prettyFormat); + visitor.setPrettyFormat(option.isPrettyFormat()); if (tableMapping != null) { visitor.setTableMapping(tableMapping); @@ -293,10 +292,10 @@ public class SQLUtils { for (int i = 0; i < statementList.size(); i++) { SQLStatement stmt = statementList.get(i); - + if (i > 0) { visitor.print(";"); - + SQLStatement preStmt = statementList.get(i - 1); List comments = preStmt.getAfterCommentsDirect(); if (comments != null){ @@ -309,7 +308,7 @@ public class SQLUtils { } } visitor.println(); - + if (!(stmt instanceof SQLSetStatement)) { visitor.println(); } @@ -323,7 +322,7 @@ public class SQLUtils { } } stmt.accept(visitor); - + if (i == statementList.size() - 1) { Boolean semi = (Boolean) stmt.getAttribute("format.semi"); if (semi != null && semi.booleanValue()) { @@ -332,7 +331,7 @@ public class SQLUtils { // } visitor.print(";"); } - + List comments = stmt.getAfterCommentsDirect(); if (comments != null){ for (int j = 0; j < comments.size(); ++j) { @@ -348,12 +347,12 @@ public class SQLUtils { return out.toString(); } - + public static SQLASTOutputVisitor createOutputVisitor(Appendable out, String dbType) { return createFormatOutputVisitor(out, null, dbType); } - public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, // + public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, // List statementList, // String dbType) { if (JdbcConstants.ORACLE.equals(dbType) || JdbcConstants.ALI_ORACLE.equals(dbType)) { @@ -365,8 +364,8 @@ public class SQLUtils { } if (JdbcConstants.MYSQL.equals(dbType) // - || JdbcConstants.MARIADB.equals(dbType) // - || JdbcConstants.H2.equals(dbType)) { + || JdbcConstants.MARIADB.equals(dbType) // + || JdbcConstants.H2.equals(dbType)) { return new MySqlOutputVisitor(out); } @@ -385,10 +384,10 @@ public class SQLUtils { if (JdbcConstants.ODPS.equals(dbType)) { return new OdpsOutputVisitor(out); } - + return new SQLASTOutputVisitor(out, dbType); } - + @Deprecated public static SchemaStatVisitor createSchemaStatVisitor(List statementList, String dbType) { return createSchemaStatVisitor(dbType); @@ -400,8 +399,8 @@ public class SQLUtils { } if (JdbcConstants.MYSQL.equals(dbType) || // - JdbcConstants.MARIADB.equals(dbType) || // - JdbcConstants.H2.equals(dbType)) { + JdbcConstants.MARIADB.equals(dbType) || // + JdbcConstants.H2.equals(dbType)) { return new MySqlSchemaStatVisitor(); } @@ -416,7 +415,7 @@ public class SQLUtils { if (JdbcConstants.DB2.equals(dbType)) { return new DB2SchemaStatVisitor(); } - + if (JdbcConstants.ODPS.equals(dbType)) { return new OdpsSchemaStatVisitor(); } @@ -516,7 +515,7 @@ public class SQLUtils { } if (op != SQLBinaryOperator.BooleanAnd // - && op != SQLBinaryOperator.BooleanOr) { + && op != SQLBinaryOperator.BooleanOr) { throw new IllegalArgumentException("add condition not support : " + op); } @@ -573,7 +572,7 @@ public class SQLUtils { throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName()); } - + public static SQLExpr buildCondition(SQLBinaryOperator op, SQLExpr condition, boolean left, SQLExpr where) { if (where == null) { return condition; @@ -581,17 +580,17 @@ public class SQLUtils { SQLBinaryOpExpr newCondition; if (left) { - newCondition = new SQLBinaryOpExpr(condition, op, where); + newCondition = new SQLBinaryOpExpr(condition, op, where); } else { newCondition = new SQLBinaryOpExpr(where, op, condition); } return newCondition; } - + public static String addSelectItem(String selectSql, String expr, String alias, String dbType) { return addSelectItem(selectSql, expr, alias, false, dbType); } - + public static String addSelectItem(String selectSql, String expr, String alias, boolean first, String dbType) { List stmtList = parseStatements(selectSql, dbType); @@ -611,12 +610,12 @@ public class SQLUtils { return toSQLString(stmt, dbType); } - + public static void addSelectItem(SQLStatement stmt, SQLExpr expr, String alias, boolean first) { if (expr == null) { return; } - + if (stmt instanceof SQLSelectStatement) { SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery(); if (query instanceof SQLSelectQueryBlock) { @@ -628,28 +627,34 @@ public class SQLUtils { return; } - + throw new IllegalArgumentException("add selectItem not support " + stmt.getClass().getName()); } - + public static void addSelectItem(SQLSelectQueryBlock queryBlock, SQLExpr expr, String alias, boolean first) { SQLSelectItem selectItem = new SQLSelectItem(expr, alias); queryBlock.getSelectList().add(selectItem); selectItem.setParent(selectItem); } - + public static class FormatOption { private boolean ucase = true; - + private boolean prettyFormat = false; + public FormatOption() { - + } - + public FormatOption(boolean ucase) { this.ucase = ucase; } + public FormatOption(boolean ucase, boolean prettyFormat) { + this.ucase = ucase; + this.prettyFormat = prettyFormat; + } + public boolean isUppCase() { return ucase; } @@ -657,11 +662,19 @@ public class SQLUtils { public void setUppCase(boolean val) { this.ucase = val; } + + public boolean isPrettyFormat() { + return prettyFormat; + } + + public void setPrettyFormat(boolean prettyFormat) { + this.prettyFormat = prettyFormat; + } } public static String refactor(String sql, String dbType, Map tableMapping) { List stmtList = parseStatements(sql, dbType); - return SQLUtils.toSQLString(stmtList, dbType, null, DEFAULT_LCASE_FORMAT_OPTION, tableMapping, false); + return SQLUtils.toSQLString(stmtList, dbType, null, null, tableMapping); } public static boolean containsIndexDDL(String sql, String dbType) {