SqlExplainInterceptor.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.plugins;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.Properties;
  21. import org.apache.ibatis.builder.StaticSqlSource;
  22. import org.apache.ibatis.executor.Executor;
  23. import org.apache.ibatis.logging.Log;
  24. import org.apache.ibatis.logging.LogFactory;
  25. import org.apache.ibatis.mapping.BoundSql;
  26. import org.apache.ibatis.mapping.MappedStatement;
  27. import org.apache.ibatis.mapping.SqlCommandType;
  28. import org.apache.ibatis.plugin.Interceptor;
  29. import org.apache.ibatis.plugin.Intercepts;
  30. import org.apache.ibatis.plugin.Invocation;
  31. import org.apache.ibatis.plugin.Plugin;
  32. import org.apache.ibatis.plugin.Signature;
  33. import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
  34. import org.apache.ibatis.session.Configuration;
  35. import com.baomidou.mybatisplus.enums.DBType;
  36. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  37. import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
  38. import com.baomidou.mybatisplus.toolkit.StringUtils;
  39. import com.baomidou.mybatisplus.toolkit.VersionUtils;
  40. /**
  41. * <p>
  42. * SQL 执行分析拦截器【 目前只支持 MYSQL-5.6.3 以上版本 】
  43. * </p>
  44. *
  45. * @author hubin
  46. * @Date 2016-08-16
  47. */
  48. @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
  49. public class SqlExplainInterceptor implements Interceptor {
  50. private static final Log logger = LogFactory.getLog(SqlExplainInterceptor.class);
  51. /**
  52. * Mysql支持分析SQL的最小版本
  53. */
  54. private final String minMySQLVersion = "5.6.3";
  55. /**
  56. * 发现执行全表 delete update 语句是否停止执行
  57. */
  58. private boolean stopProceed = false;
  59. public Object intercept(Invocation invocation) throws Throwable {
  60. /**
  61. * 处理 DELETE UPDATE 语句
  62. */
  63. MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
  64. if (ms.getSqlCommandType() == SqlCommandType.DELETE || ms.getSqlCommandType() == SqlCommandType.UPDATE) {
  65. Executor executor = (Executor) invocation.getTarget();
  66. Configuration configuration = ms.getConfiguration();
  67. Object parameter = invocation.getArgs()[1];
  68. BoundSql boundSql = ms.getBoundSql(parameter);
  69. Connection connection = executor.getTransaction().getConnection();
  70. String databaseVersion = connection.getMetaData().getDatabaseProductVersion();
  71. if (GlobalConfigUtils.getDbType(configuration).equals(DBType.MYSQL)
  72. && VersionUtils.compare(minMySQLVersion, databaseVersion)) {
  73. logger.warn("Warn: Your mysql version needs to be greater than '5.6.3' to execute of Sql Explain!");
  74. return invocation.proceed();
  75. }
  76. /**
  77. * 执行 SQL 分析
  78. */
  79. sqlExplain(configuration, ms, boundSql, connection, parameter);
  80. }
  81. return invocation.proceed();
  82. }
  83. /**
  84. * <p>
  85. * 判断是否执行 SQL
  86. * </p>
  87. *
  88. * @param configuration
  89. * @param mappedStatement
  90. * @param boundSql
  91. * @param connection
  92. * @param parameter
  93. * @return
  94. * @throws Exception
  95. */
  96. protected void sqlExplain(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql,
  97. Connection connection, Object parameter) {
  98. StringBuilder explain = new StringBuilder("EXPLAIN ");
  99. explain.append(boundSql.getSql());
  100. String sqlExplain = explain.toString();
  101. StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings());
  102. MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource,
  103. SqlCommandType.SELECT);
  104. builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType())
  105. .statementType(mappedStatement.getStatementType());
  106. MappedStatement query_statement = builder.build();
  107. DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql);
  108. try (PreparedStatement stmt = connection.prepareStatement(sqlExplain)) {
  109. handler.setParameters(stmt);
  110. try (ResultSet rs = stmt.executeQuery()) {
  111. while (rs.next()) {
  112. if (!"Using where".equals(rs.getString("Extra"))) {
  113. if (this.isStopProceed()) {
  114. throw new MybatisPlusException("Error: Full table operation is prohibited. SQL: " + boundSql.getSql());
  115. }
  116. break;
  117. }
  118. }
  119. }
  120. } catch (Exception e) {
  121. throw new MybatisPlusException(e);
  122. }
  123. }
  124. public Object plugin(Object target) {
  125. if (target instanceof Executor) {
  126. return Plugin.wrap(target, this);
  127. }
  128. return target;
  129. }
  130. public void setProperties(Properties prop) {
  131. String stopProceed = prop.getProperty("stopProceed");
  132. if (StringUtils.isNotEmpty(stopProceed)) {
  133. this.stopProceed = Boolean.valueOf(stopProceed);
  134. }
  135. }
  136. public boolean isStopProceed() {
  137. return stopProceed;
  138. }
  139. public void setStopProceed(boolean stopProceed) {
  140. this.stopProceed = stopProceed;
  141. }
  142. }