AbstractSqlParserHandler.java 2.9 KB

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