|
@@ -1,4 +1,4 @@
|
|
-/*
|
|
|
|
|
|
+/**
|
|
* Copyright (c) 2011-2014, hubin (jobob@qq.com).
|
|
* Copyright (c) 2011-2014, hubin (jobob@qq.com).
|
|
* <p>
|
|
* <p>
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
@@ -18,7 +18,12 @@ package com.baomidou.mybatisplus.extension.plugins;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Proxy;
|
|
import java.lang.reflect.Proxy;
|
|
import java.sql.Statement;
|
|
import java.sql.Statement;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Comparator;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.List;
|
|
import java.util.Properties;
|
|
import java.util.Properties;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
import org.apache.ibatis.logging.Log;
|
|
import org.apache.ibatis.logging.Log;
|
|
@@ -34,12 +39,12 @@ import org.apache.ibatis.reflection.SystemMetaObject;
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
import org.apache.ibatis.session.ResultHandler;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
|
|
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
|
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
* 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
|
|
* 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
|
|
@@ -57,7 +62,6 @@ public class PerformanceInterceptor implements Interceptor {
|
|
private static final String DruidPooledPreparedStatement = "com.alibaba.druid.pool.DruidPooledPreparedStatement";
|
|
private static final String DruidPooledPreparedStatement = "com.alibaba.druid.pool.DruidPooledPreparedStatement";
|
|
private static final String T4CPreparedStatement = "oracle.jdbc.driver.T4CPreparedStatement";
|
|
private static final String T4CPreparedStatement = "oracle.jdbc.driver.T4CPreparedStatement";
|
|
private static final String OraclePreparedStatementWrapper = "oracle.jdbc.driver.OraclePreparedStatementWrapper";
|
|
private static final String OraclePreparedStatementWrapper = "oracle.jdbc.driver.OraclePreparedStatementWrapper";
|
|
- private static final String HikariPreparedStatementWrapper = "com.zaxxer.hikari.pool.HikariProxyPreparedStatement";
|
|
|
|
/**
|
|
/**
|
|
* SQL 执行最大时长,超过自动停止运行,有助于发现问题。
|
|
* SQL 执行最大时长,超过自动停止运行,有助于发现问题。
|
|
*/
|
|
*/
|
|
@@ -84,11 +88,19 @@ public class PerformanceInterceptor implements Interceptor {
|
|
} else {
|
|
} else {
|
|
statement = (Statement) firstArg;
|
|
statement = (Statement) firstArg;
|
|
}
|
|
}
|
|
|
|
+ MetaObject stmtMetaObj = SystemMetaObject.forObject(statement);
|
|
try {
|
|
try {
|
|
- statement = (Statement) SystemMetaObject.forObject(statement).getValue("stmt.statement");
|
|
|
|
|
|
+ statement = (Statement) stmtMetaObj.getValue("stmt.statement");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// do nothing
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
+ if (stmtMetaObj.hasGetter("delegate")) {//Hikari
|
|
|
|
+ try {
|
|
|
|
+ statement = (Statement) stmtMetaObj.getValue("delegate");
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
String originalSql = null;
|
|
String originalSql = null;
|
|
String stmtClassName = statement.getClass().getName();
|
|
String stmtClassName = statement.getClass().getName();
|
|
@@ -128,23 +140,14 @@ public class PerformanceInterceptor implements Interceptor {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
//ignore
|
|
//ignore
|
|
}
|
|
}
|
|
- } else if (HikariPreparedStatementWrapper.equals(stmtClassName)) {
|
|
|
|
- try {
|
|
|
|
- Object sqlStatement = SystemMetaObject.forObject(statement).getValue("delegate.sqlStatement");
|
|
|
|
- if (sqlStatement != null) {
|
|
|
|
- originalSql = sqlStatement.toString();
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- //ignore
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
if (originalSql == null) {
|
|
if (originalSql == null) {
|
|
originalSql = statement.toString();
|
|
originalSql = statement.toString();
|
|
}
|
|
}
|
|
-
|
|
|
|
- int index = originalSql.indexOf(':');
|
|
|
|
|
|
+ originalSql = originalSql.replaceAll("[\\s]+", " ");
|
|
|
|
+ int index = indexOfSqlStart(originalSql);
|
|
if (index > 0) {
|
|
if (index > 0) {
|
|
- originalSql = originalSql.substring(index + 1, originalSql.length());
|
|
|
|
|
|
+ originalSql = originalSql.substring(index, originalSql.length());
|
|
}
|
|
}
|
|
|
|
|
|
// 计算执行 SQL 耗时
|
|
// 计算执行 SQL 耗时
|
|
@@ -233,4 +236,26 @@ public class PerformanceInterceptor implements Interceptor {
|
|
}
|
|
}
|
|
return getMethodRegular(clazz.getSuperclass(), methodName);
|
|
return getMethodRegular(clazz.getSuperclass(), methodName);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取sql语句开头部分
|
|
|
|
+ *
|
|
|
|
+ * @param sql
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private int indexOfSqlStart(String sql) {
|
|
|
|
+ String upperCaseSql = sql.toUpperCase();
|
|
|
|
+ Set<Integer> set = new HashSet<>();
|
|
|
|
+ set.add(upperCaseSql.indexOf("SELECT "));
|
|
|
|
+ set.add(upperCaseSql.indexOf("UPDATE "));
|
|
|
|
+ set.add(upperCaseSql.indexOf("INSERT "));
|
|
|
|
+ set.add(upperCaseSql.indexOf("DELETE "));
|
|
|
|
+ set.remove(-1);
|
|
|
|
+ if (CollectionUtils.isEmpty(set)) {
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ List<Integer> list = new ArrayList<>(set);
|
|
|
|
+ list.sort(Comparator.naturalOrder());
|
|
|
|
+ return list.get(0);
|
|
|
|
+ }
|
|
}
|
|
}
|