DynamicTableNameParser.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus.extension.parsers;
  17. import com.baomidou.mybatisplus.core.parser.ISqlParser;
  18. import com.baomidou.mybatisplus.core.parser.SqlInfo;
  19. import com.baomidou.mybatisplus.core.toolkit.TableNameParser;
  20. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  21. import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
  22. import lombok.Data;
  23. import lombok.experimental.Accessors;
  24. import org.apache.ibatis.reflection.MetaObject;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * 动态表名 SQL 解析器
  30. *
  31. * @author jobob
  32. * @since 2019-04-23
  33. * @deprecated 3.4.0 @2020-07-30 use {@link MybatisPlusInterceptor} {@link DynamicTableNameInnerInterceptor}
  34. */
  35. @Data
  36. @Accessors(chain = true)
  37. @Deprecated
  38. public class DynamicTableNameParser implements ISqlParser {
  39. private Map<String, ITableNameHandler> tableNameHandlerMap;
  40. /**
  41. * 进行 SQL 表名名替换
  42. *
  43. * @param metaObject 元对象
  44. * @param sql SQL 语句
  45. * @return 返回解析后的 SQL 信息
  46. */
  47. @Override
  48. public SqlInfo parser(MetaObject metaObject, String sql) {
  49. // fix-issue:https://gitee.com/baomidou/mybatis-plus/issues/I1K7Q1
  50. // Assert.isFalse(CollectionUtils.isEmpty(tableNameHandlerMap), "tableNameHandlerMap is empty.");
  51. if (allowProcess(metaObject)) {
  52. TableNameParser parser = new TableNameParser(sql);
  53. List<TableNameParser.SqlToken> names = new ArrayList<>();
  54. parser.accept(names::add);
  55. StringBuilder builder = new StringBuilder();
  56. int last = 0;
  57. for (TableNameParser.SqlToken name : names) {
  58. int start = name.getStart();
  59. if (start != last) {
  60. builder.append(sql, last, start);
  61. String value = name.getValue();
  62. ITableNameHandler handler = tableNameHandlerMap.get(value);
  63. if (handler != null) {
  64. builder.append(handler.dynamicTableName(metaObject, sql, value));
  65. } else {
  66. builder.append(value);
  67. }
  68. }
  69. last = name.getEnd();
  70. }
  71. if (last != sql.length()) {
  72. builder.append(sql.substring(last));
  73. }
  74. return SqlInfo.of(builder.toString());
  75. }
  76. return null;
  77. }
  78. /**
  79. * 判断是否允许执行
  80. * <p>例如:逻辑删除只解析 delete , update 操作</p>
  81. *
  82. * @param metaObject 元对象
  83. * @return true
  84. */
  85. public boolean allowProcess(MetaObject metaObject) {
  86. return true;
  87. }
  88. }