使用 P6Spy 来格式化 SQL 语句,支持 Hibernate 和 iBATIS

时间:2023-03-09 15:32:01
使用 P6Spy 来格式化 SQL 语句,支持 Hibernate 和 iBATIS

事情起因

在处理一个查询小功能的时候,自认为 SQL 语句和传参均正确,然而查询结果无匹配数据,在查看 Hibernate 自带 SQL 语句输出的时候带着问好感觉有点不爽,特别是想复制 SQL 语句到数据库客户端去调试时,还要手动复制参数值,麻烦得很。

所以我希望能做到输出 SQL 语句的时候能够把相关的参数值填在对应的位置,假如我需要复制到数据库客户端进行测试,也很方便。

说干就干,开始查找资料,了解到 Hibernate 自带的输出功能是无法实现我的需求,同时也轻易地看到了 P6Spy,这想想起来,以前处理的一个项目中,曾经用过 P6Spy,由于不是我架构的,当时我不太理解 P6Spy 的作用,只知道 有个 spy.properties 文件很熟悉。非工科出身的程序员就是这样不够系统化。

接下来记录我自己的处理步骤,以及相关参考的文章。

一、引入依赖

所看到很多资料都提到去官网下载 jar 文件,但官网其实已经用不了,现在是放在 GitHub,地址为:https://github.com/p6spy/p6spy,直接找到 maven 的引用,放到项目的 pom.xml 中即可。(maven工程专用)

        <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.0.0</version>
</dependency>

二、自定义输出格式

需要自己实现一个类,覆盖默认方法,然后在 spy.properties 中配置使用。

出于安全需要,我改为使用 logger 在 debug 模式下输出。

package cn.wuxia.ueq.ucm.view.base.support;

import cn.wuxia.common.util.StringUtil;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.StdoutLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* Created by luodengxiong on 2017/04/25.
*/
public class P6SpyLogger extends StdoutLogger { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); /**
* 重写输出方法
* @param connectionId 连接id
* @param now 当前时间
* @param elapsed 执行时长,包括执行 SQL 和处理结果集的时间(可以参考来调优)
* @param category 语句分类,statement、resultset 等
* @param prepared 查询语句。可能是 prepared statement,表现为 select * from table1 where c1=?,问号参数形式
* @param sql 含参数值的查询语句,如 select * from from table1 where c1=7
*/
@Override
public void logSQL(int connectionId, String now, long elapsed, Category category, String prepared, String sql) {
if(!Category.COMMIT.equals(category) && !prepared.startsWith("select count("))
this.logText(this.strategy.formatMessage(connectionId, now, elapsed, category.toString(), "-prepared-", sql)); } @Override
public void logText(String text) {
StringBuilder sb = new StringBuilder();
//匹配到最后一个|作为分隔符
String[] arrString = text.split("\\|(?![^\\|]*\\|)");
if (arrString.length > 1) {
sb.append(arrString[0]);
//去最后一段语句做替换进行格式化
String sss=arrString[1].trim();
if(StringUtil.isNotBlank(sss) && !";".equalsIgnoreCase(sss)){
String sql = new SQLFormatter().format(arrString[1]);
sb.append("\r\n");
sb.append(sql);
sb.append("\r\n");
}else {
sb.append(sss);
}
//this.getStream().println(sb.toString());
logger.debug(sb.toString());
} else {
//this.getStream().println(text);
logger.debug(text);
}
arrString = null; } }

三、配置 spy.properties

经过几次摸索,才配置到最适合我自己的需要。主要是指定数据库驱动(我用的是 MySQL)日期格式化、使用单行还是多行,指定输出方式(控制台、log文件等),因为我只是 debug 的时候用的,所以直接输出到控制台了,其他方式我没有去试。

#################################################################
# P6Spy Options File #
# See documentation for detailed instructions #
# http://p6spy.github.io/p6spy/2.0/configandusage.html #
################################################################# #################################################################
# MODULES #
# #
# Module list adapts the modular functionality of P6Spy. #
# Only modules listed are active. #
# (default is com.p6spy.engine.logging.P6LogFactory and #
# com.p6spy.engine.spy.P6SpyFactory) #
# Please note that the core module (P6SpyFactory) can't be #
# deactivated. #
# Unlike the other properties, activation of the changes on #
# this one requires reload. #
#################################################################
#modulelist=com.p6spy.engine.spy.P6SpyFactory,com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory ################################################################
# CORE (P6SPY) PROPERTIES #
################################################################ # A comma separated list of JDBC drivers to load and register.
# (default is empty)
#
# Note: This is normally only needed when using P6Spy in an
# application server environment with a JNDI data source or when
# using a JDBC driver that does not implement the JDBC 4.0 API
# (specifically automatic registration).
driverlist=com.mysql.jdbc.Driver
# for flushing per statement
# (default is false)
#autoflush = false # sets the date format using Java's SimpleDateFormat routine.
# In case property is not set, miliseconds since 1.1.1970 (unix time) is used (default is empty)
dateformat=yyyy-MM-dd HH:mm:ss # prints a stack trace for every statement logged
#stacktrace=false
# if stacktrace=true, specifies the stack trace to print
#stacktraceclass= # determines if property file should be reloaded
# Please note: reload means forgetting all the previously set
# settings (even those set during runtime - via JMX)
# and starting with the clean table
# (default is false)
#reloadproperties=false # determines how often should be reloaded in seconds
# (default is 60)
#reloadpropertiesinterval=60 # specifies the appender to use for logging
# Please note: reload means forgetting all the previously set
# settings (even those set during runtime - via JMX)
# and starting with the clean table
# (only the properties read from the configuration file)
# (default is com.p6spy.engine.spy.appender.FileLogger)
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
#appender=com.p6spy.engine.spy.appender.StdoutLogger
#appender=com.p6spy.engine.spy.appender.FileLogger
#自定义的SQL格式化输出
appender=cn.wuxia.ueq.ucm.view.base.support.P6SpyLogger # name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log)
# (used for com.p6spy.engine.spy.appender.FileLogger only)
# (default is spy.log)
#logfile = spy.log # append to the p6spy log file. if this is set to false the
# log file is truncated every time. (file logger only)
# (default is true)
#append=true # class to use for formatting log messages (default is: com.p6spy.engine.spy.appender.SingleLineFormat)
logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
#logMessageFormat=com.p6spy.engine.spy.appender.MultiLineFormat # format that is used for logging of the date/time/... (has to be compatible with java.text.SimpleDateFormat)
# (default is dd-MMM-yy)
databaseDialectDateFormat=yyyy-MM-dd HH:mm:ss # whether to expose options via JMX or not
# (default is true)
#jmx=true # if exposing options via jmx (see option: jmx), what should be the prefix used?
# jmx naming pattern constructed is: com.p6spy(.<jmxPrefix>)?:name=<optionsClassName>
# please note, if there is already such a name in use it would be unregistered first (the last registered wins)
# (default is none)
#jmxPrefix= # if set to true, the execution time will be measured in nanoseconds as opposed to milliseconds
# (default is false)
#useNanoTime=false #################################################################
# DataSource replacement #
# #
# Replace the real DataSource class in your application server #
# configuration with the name com.p6spy.engine.spy.P6DataSource #
# (that provides also connection pooling and xa support). #
# then add the JNDI name and class name of the real #
# DataSource here #
# #
# Values set in this item cannot be reloaded using the #
# reloadproperties variable. Once it is loaded, it remains #
# in memory until the application is restarted. #
# #
#################################################################
#realdatasource=/RealMySqlDS
#realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource #################################################################
# DataSource properties #
# #
# If you are using the DataSource support to intercept calls #
# to a DataSource that requires properties for proper setup, #
# define those properties here. Use name value pairs, separate #
# the name and value with a semicolon, and separate the #
# pairs with commas. #
# #
# The example shown here is for mysql #
# #
#################################################################
#realdatasourceproperties=port;3306,serverName;myhost,databaseName;jbossdb,foo;bar #################################################################
# JNDI DataSource lookup #
# #
# If you are using the DataSource support outside of an app #
# server, you will probably need to define the JNDI Context #
# environment. #
# #
# If the P6Spy code will be executing inside an app server then #
# do not use these properties, and the DataSource lookup will #
# use the naming context defined by the app server. #
# #
# The two standard elements of the naming environment are #
# jndicontextfactory and jndicontextproviderurl. If you need #
# additional elements, use the jndicontextcustom property. #
# You can define multiple properties in jndicontextcustom, #
# in name value pairs. Separate the name and value with a #
# semicolon, and separate the pairs with commas. #
# #
# The example shown here is for a standalone program running on #
# a machine that is also running JBoss, so the JDNI context #
# is configured for JBoss (3.0.4). #
# #
# (by default all these are empty) #
#################################################################
#jndicontextfactory=org.jnp.interfaces.NamingContextFactory
#jndicontextproviderurl=localhost:1099
#jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.nameing:org.jnp.interfaces #jndicontextfactory=com.ibm.websphere.naming.WsnInitialContextFactory
#jndicontextproviderurl=iiop://localhost:900 ################################################################
# P6 LOGGING SPECIFIC PROPERTIES #
################################################################ # filter what is logged
# please note this is a precondition for usage of: include/exclude/sqlexpression
# (default is false)
#filter=false # comma separated list of strings to include
# please note that special characters escaping (used in java) has to be done for the provided regular expression
# (default is empty)
#include =
# comma separated list of strings to exclude
# (default is empty)
#exclude = # sql expression to evaluate if using regex
# please note that special characters escaping (used in java) has to be done for the provided regular expression
# (default is empty)
#sqlexpression = #list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
# (default is info,debug,result,resultset,batch)
#excludecategories=info,debug,result,resultset,batch # Execution threshold applies to the standard logging of P6Spy.
# While the standard logging logs out every statement
# regardless of its execution time, this feature puts a time
# condition on that logging. Only statements that have taken
# longer than the time specified (in milliseconds) will be
# logged. This way it is possible to see only statements that
# have exceeded some high water mark.
# This time is reloadable.
#
# executionThreshold=integer time (milliseconds)
# (default is 0)
#executionThreshold= ################################################################
# P6 OUTAGE SPECIFIC PROPERTIES #
################################################################
# Outage Detection
#
# This feature detects long-running statements that may be indicative of
# a database outage problem. If this feature is turned on, it will log any
# statement that surpasses the configurable time boundary during its execution.
# When this feature is enabled, no other statements are logged except the long
# running statements. The interval property is the boundary time set in seconds.
# For example, if this is set to 2, then any statement requiring at least 2
# seconds will be logged. Note that the same statement will continue to be logged
# for as long as it executes. So if the interval is set to 2, and the query takes
# 11 seconds, it will be logged 5 times (at the 2, 4, 6, 8, 10 second intervals).
#
# outagedetection=true|false
# outagedetectioninterval=integer time (seconds)
#
# (default is false)
#outagedetection=false
# (default is 60)
#outagedetectioninterval=30

四、修改原数据库配置,改了这个才起作用

#jdbc.driver=com.mysql.jdbc.Driver
jdbc.driver=com.p6spy.engine.spy.P6SpyDriver
#jdbc.url=jdbc:mysql://localhost:3306/dbname
jdbc.url=jdbc:p6spy:mysql://localhost:3306/dbname

五、效果:

select
order0_.order_no as col_0_0_
from
o_order order0_
where
order0_.user_id='s8FvofYDTvWeqwxMat_3Dw'
and order0_.status<>'_0'
and order0_.status<>'_4'
and order0_.status='_2' limit 10

六、参考资料

格式化p6spy的输出日志
sql格式化工具(从hibernate中弄出来的)
用 p6spy 来观察 Java 程序中执行的所有 SQL 语句
附:
package cn.wuxia.ueq.ucm.view.base.support;

/**
* Created by luodengxiong on 2017/04/25.
*/ import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.StringTokenizer; public class SQLFormatter { private static final Set<String> BEGIN_CLAUSES = new HashSet<String>();
private static final Set<String> END_CLAUSES = new HashSet<String>();
private static final Set<String> LOGICAL = new HashSet<String>();
private static final Set<String> QUANTIFIERS = new HashSet<String>();
private static final Set<String> DML = new HashSet<String>();
private static final Set<String> MISC = new HashSet<String>();
public static final String WHITESPACE = " \n\r\f\t"; static {
BEGIN_CLAUSES.add("left");
BEGIN_CLAUSES.add("right");
BEGIN_CLAUSES.add("inner");
BEGIN_CLAUSES.add("outer");
BEGIN_CLAUSES.add("group");
BEGIN_CLAUSES.add("order"); END_CLAUSES.add("where");
END_CLAUSES.add("set");
END_CLAUSES.add("having");
END_CLAUSES.add("join");
END_CLAUSES.add("from");
END_CLAUSES.add("by");
END_CLAUSES.add("join");
END_CLAUSES.add("into");
END_CLAUSES.add("union"); LOGICAL.add("and");
LOGICAL.add("or");
LOGICAL.add("when");
LOGICAL.add("else");
LOGICAL.add("end"); QUANTIFIERS.add("in");
QUANTIFIERS.add("all");
QUANTIFIERS.add("exists");
QUANTIFIERS.add("some");
QUANTIFIERS.add("any"); DML.add("insert");
DML.add("update");
DML.add("delete"); MISC.add("select");
MISC.add("on");
} static final String indentString = " ";
static final String initial = "\n "; public String format(String source) {
return new FormatProcess(source).perform();
} private static class FormatProcess {
boolean beginLine = true;
boolean afterBeginBeforeEnd = false;
boolean afterByOrSetOrFromOrSelect = false;
boolean afterValues = false;
boolean afterOn = false;
boolean afterBetween = false;
boolean afterInsert = false;
int inFunction = 0;
int parensSinceSelect = 0;
private LinkedList<Integer> parenCounts = new LinkedList<Integer>();
private LinkedList<Boolean> afterByOrFromOrSelects = new LinkedList<Boolean>(); int indent = 1; StringBuilder result = new StringBuilder();
StringTokenizer tokens;
String lastToken;
String token;
String lcToken; public FormatProcess(String sql) {
tokens = new StringTokenizer(
sql,
"()+*/-=<>'`\"[]," + WHITESPACE,
true
);
} public String perform() { result.append(initial); while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
lcToken = token.toLowerCase(); if ("'".equals(token)) {
String t;
do {
t = tokens.nextToken();
token += t;
}
while (!"'".equals(t) && tokens.hasMoreTokens()); // cannot handle single quotes
} else if ("\"".equals(token)) {
String t;
do {
t = tokens.nextToken();
token += t;
}
while (!"\"".equals(t));
} if (afterByOrSetOrFromOrSelect && ",".equals(token)) {
commaAfterByOrFromOrSelect();
} else if (afterOn && ",".equals(token)) {
commaAfterOn();
} else if ("(".equals(token)) {
openParen();
} else if (")".equals(token)) {
closeParen();
} else if (BEGIN_CLAUSES.contains(lcToken)) {
beginNewClause();
} else if (END_CLAUSES.contains(lcToken)) {
endNewClause();
} else if ("select".equals(lcToken)) {
select();
} else if (DML.contains(lcToken)) {
updateOrInsertOrDelete();
} else if ("values".equals(lcToken)) {
values();
} else if ("on".equals(lcToken)) {
on();
} else if (afterBetween && lcToken.equals("and")) {
misc();
afterBetween = false;
} else if (LOGICAL.contains(lcToken)) {
logical();
} else if (isWhitespace(token)) {
white();
} else {
misc();
} if (!isWhitespace(token)) {
lastToken = lcToken;
} }
return result.toString();
} private void commaAfterOn() {
out();
indent--;
newline();
afterOn = false;
afterByOrSetOrFromOrSelect = true;
} private void commaAfterByOrFromOrSelect() {
out();
newline();
} private void logical() {
if ("end".equals(lcToken)) {
indent--;
}
newline();
out();
beginLine = false;
} private void on() {
indent++;
afterOn = true;
newline();
out();
beginLine = false;
} private void misc() {
out();
if ("between".equals(lcToken)) {
afterBetween = true;
}
if (afterInsert) {
newline();
afterInsert = false;
} else {
beginLine = false;
if ("case".equals(lcToken)) {
indent++;
}
}
} private void white() {
if (!beginLine) {
result.append(" ");
}
} private void updateOrInsertOrDelete() {
out();
indent++;
beginLine = false;
if ("update".equals(lcToken)) {
newline();
}
if ("insert".equals(lcToken)) {
afterInsert = true;
}
} @SuppressWarnings({"UnnecessaryBoxing"})
private void select() {
out();
indent++;
newline();
parenCounts.addLast(Integer.valueOf(parensSinceSelect));
afterByOrFromOrSelects.addLast(Boolean.valueOf(afterByOrSetOrFromOrSelect));
parensSinceSelect = 0;
afterByOrSetOrFromOrSelect = true;
} private void out() {
result.append(token);
} private void endNewClause() {
if (!afterBeginBeforeEnd) {
indent--;
if (afterOn) {
indent--;
afterOn = false;
}
newline();
}
out();
if (!"union".equals(lcToken)) {
indent++;
}
newline();
afterBeginBeforeEnd = false;
afterByOrSetOrFromOrSelect = "by".equals(lcToken)
|| "set".equals(lcToken)
|| "from".equals(lcToken);
} private void beginNewClause() {
if (!afterBeginBeforeEnd) {
if (afterOn) {
indent--;
afterOn = false;
}
indent--;
newline();
}
out();
beginLine = false;
afterBeginBeforeEnd = true;
} private void values() {
indent--;
newline();
out();
indent++;
newline();
afterValues = true;
} @SuppressWarnings({"UnnecessaryUnboxing"})
private void closeParen() {
parensSinceSelect--;
if (parensSinceSelect < 0) {
indent--;
parensSinceSelect = parenCounts.removeLast().intValue();
afterByOrSetOrFromOrSelect = afterByOrFromOrSelects.removeLast().booleanValue();
}
if (inFunction > 0) {
inFunction--;
out();
} else {
if (!afterByOrSetOrFromOrSelect) {
indent--;
newline();
}
out();
}
beginLine = false;
} private void openParen() {
if (isFunctionName(lastToken) || inFunction > 0) {
inFunction++;
}
beginLine = false;
if (inFunction > 0) {
out();
} else {
out();
if (!afterByOrSetOrFromOrSelect) {
indent++;
newline();
beginLine = true;
}
}
parensSinceSelect++;
} private static boolean isFunctionName(String tok) {
final char begin = tok.charAt(0);
final boolean isIdentifier = Character.isJavaIdentifierStart(begin) || '"' == begin;
return isIdentifier &&
!LOGICAL.contains(tok) &&
!END_CLAUSES.contains(tok) &&
!QUANTIFIERS.contains(tok) &&
!DML.contains(tok) &&
!MISC.contains(tok);
} private static boolean isWhitespace(String token) {
return WHITESPACE.indexOf(token) >= 0;
} private void newline() {
result.append("\n");
for (int i = 0; i < indent; i++) {
result.append(indentString);
}
beginLine = true;
}
} public static void main(String[] args) {
String sql = new SQLFormatter().format("select * from t_sss");
System.out.println(sql);
} }