Explorar o código

Merge remote-tracking branch 'origin/3.0' into 3.0

hubin %!s(int64=7) %!d(string=hai) anos
pai
achega
a8e72eec68

+ 7 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlUtils.java

@@ -17,11 +17,11 @@ package com.baomidou.mybatisplus.core.toolkit.sql;
 
 import java.util.List;
 
-import com.baomidou.mybatisplus.core.parser.ISqlParser;
-import com.baomidou.mybatisplus.core.parser.SqlInfo;
 import com.baomidou.mybatisplus.core.enums.SqlLike;
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.core.pagination.Pagination;
+import com.baomidou.mybatisplus.core.parser.ISqlParser;
+import com.baomidou.mybatisplus.core.parser.SqlInfo;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 
@@ -152,10 +152,12 @@ public class SqlUtils {
      */
     public static String sqlFormat(String boundSql, boolean format) {
         if (format) {
-            return SQL_FORMATTER.format(boundSql);
-        } else {
-            return boundSql.replaceAll("[\\s]+", " ");
+            try {
+                return SQL_FORMATTER.format(boundSql);
+            } catch (Exception ignored) {
+            }
         }
+        return boundSql;
     }
 
     /**

+ 41 - 16
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/PerformanceInterceptor.java

@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  * <p>
  * 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.Proxy;
 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.Set;
 
 import org.apache.ibatis.executor.statement.StatementHandler;
 import org.apache.ibatis.logging.Log;
@@ -34,12 +39,12 @@ import org.apache.ibatis.reflection.SystemMetaObject;
 import org.apache.ibatis.session.ResultHandler;
 
 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.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.SystemClock;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 
-
 /**
  * <p>
  * 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
@@ -57,7 +62,6 @@ public class PerformanceInterceptor implements Interceptor {
     private static final String DruidPooledPreparedStatement = "com.alibaba.druid.pool.DruidPooledPreparedStatement";
     private static final String T4CPreparedStatement = "oracle.jdbc.driver.T4CPreparedStatement";
     private static final String OraclePreparedStatementWrapper = "oracle.jdbc.driver.OraclePreparedStatementWrapper";
-    private static final String HikariPreparedStatementWrapper = "com.zaxxer.hikari.pool.HikariProxyPreparedStatement";
     /**
      * SQL 执行最大时长,超过自动停止运行,有助于发现问题。
      */
@@ -84,11 +88,19 @@ public class PerformanceInterceptor implements Interceptor {
         } else {
             statement = (Statement) firstArg;
         }
+        MetaObject stmtMetaObj = SystemMetaObject.forObject(statement);
         try {
-            statement = (Statement) SystemMetaObject.forObject(statement).getValue("stmt.statement");
+            statement = (Statement) stmtMetaObj.getValue("stmt.statement");
         } catch (Exception e) {
             // do nothing
         }
+        if (stmtMetaObj.hasGetter("delegate")) {//Hikari
+            try {
+                statement = (Statement) stmtMetaObj.getValue("delegate");
+            } catch (Exception ignored) {
+
+            }
+        }
 
         String originalSql = null;
         String stmtClassName = statement.getClass().getName();
@@ -128,23 +140,14 @@ public class PerformanceInterceptor implements Interceptor {
             } catch (Exception e) {
                 //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) {
             originalSql = statement.toString();
         }
-
-        int index = originalSql.indexOf(':');
+        originalSql = originalSql.replaceAll("[\\s]+", " ");
+        int index = indexOfSqlStart(originalSql);
         if (index > 0) {
-            originalSql = originalSql.substring(index + 1, originalSql.length());
+            originalSql = originalSql.substring(index, originalSql.length());
         }
 
         // 计算执行 SQL 耗时
@@ -233,4 +236,26 @@ public class PerformanceInterceptor implements Interceptor {
         }
         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);
+    }
 }