AbstractSqlParserHandler.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * https://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.extension.handlers;
  17. import java.util.List;
  18. import org.apache.ibatis.executor.statement.StatementHandler;
  19. import org.apache.ibatis.reflection.MetaObject;
  20. import org.apache.ibatis.reflection.SystemMetaObject;
  21. import com.baomidou.mybatisplus.core.parser.ISqlParser;
  22. import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
  23. import com.baomidou.mybatisplus.core.parser.SqlInfo;
  24. import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
  25. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  26. import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
  27. import lombok.Data;
  28. import lombok.experimental.Accessors;
  29. /**
  30. * SQL 解析处理器
  31. *
  32. * @author hubin
  33. * @since 2016-08-31
  34. */
  35. @Data
  36. @Accessors(chain = true)
  37. public abstract class AbstractSqlParserHandler {
  38. private List<ISqlParser> sqlParserList;
  39. private ISqlParserFilter sqlParserFilter;
  40. /**
  41. * 拦截 SQL 解析执行
  42. */
  43. protected void sqlParser(MetaObject metaObject) {
  44. Object originalObject = metaObject.getOriginalObject();
  45. StatementHandler statementHandler = PluginUtils.realTarget(originalObject);
  46. metaObject = SystemMetaObject.forObject(statementHandler);
  47. if (null != metaObject) {
  48. if (null != this.sqlParserFilter && this.sqlParserFilter.doFilter(metaObject)) {
  49. return;
  50. }
  51. // SQL 解析
  52. if (CollectionUtils.isNotEmpty(this.sqlParserList)) {
  53. // @SqlParser(filter = true) 跳过该方法解析
  54. if (SqlParserHelper.getSqlParserInfo(metaObject)) {
  55. return;
  56. }
  57. // 标记是否修改过 SQL
  58. int flag = 0;
  59. String originalSql = (String) metaObject.getValue(PluginUtils.DELEGATE_BOUNDSQL_SQL);
  60. for (ISqlParser sqlParser : this.sqlParserList) {
  61. if (sqlParser.doFilter(metaObject, originalSql)) {
  62. SqlInfo sqlInfo = sqlParser.parser(metaObject, originalSql);
  63. if (null != sqlInfo) {
  64. originalSql = sqlInfo.getSql();
  65. ++flag;
  66. }
  67. }
  68. }
  69. if (flag >= 1) {
  70. metaObject.setValue(PluginUtils.DELEGATE_BOUNDSQL_SQL, originalSql);
  71. }
  72. }
  73. }
  74. }
  75. }