GenericTokenParser

  1. /**
  2. * Copyright 2009-2019 the original author or authors.
  3. * <p>
  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. * <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,
  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 org.apache.ibatis.parsing;
  17. /**
  18. * @author Clinton Begin
  19. */
  20. public class GenericTokenParser {
  21. /**
  22. * 开始标记
  23. */
  24. private final String openToken;
  25. /**
  26. * 结束标记
  27. */
  28. private final String closeToken;
  29. /**
  30. * 标记处理器
  31. */
  32. private final TokenHandler handler;
  33. public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
  34. this.openToken = openToken;
  35. this.closeToken = closeToken;
  36. this.handler = handler;
  37. }
  38. /**
  39. * 核心处理方法 , 看测试类{@link org.apache.ibatis.parsing.GenericTokenParserTest}
  40. * @param text
  41. * @return
  42. */
  43. public String parse(String text) {
  44. // 判断是否空
  45. if (text == null || text.isEmpty()) {
  46. return "";
  47. }
  48. // search open token
  49. // 判断 openToken 所在的位置-1不存在
  50. int start = text.indexOf(openToken);
  51. if (start == -1) {
  52. return text;
  53. }
  54. char[] src = text.toCharArray();
  55. int offset = 0;
  56. final StringBuilder builder = new StringBuilder();
  57. StringBuilder expression = null;
  58. // 循环处理 assertEquals("James T Kirk reporting.", parser.parse("${first_name} ${initial} ${last_name} reporting."));
  59. // 将${} 转换成正常文本
  60. while (start > -1) {
  61. if (start > 0 && src[start - 1] == '\\') {
  62. // `\` 忽略这个参数
  63. // this open token is escaped. remove the backslash and continue.
  64. builder.append(src, offset, start - offset - 1).append(openToken);
  65. // offset 重新计算进行下一步循环
  66. offset = start + openToken.length();
  67. } else {
  68. // found open token. let's search close token.
  69. if (expression == null) {
  70. expression = new StringBuilder();
  71. } else {
  72. expression.setLength(0);
  73. }
  74. builder.append(src, offset, start - offset);
  75. offset = start + openToken.length();
  76. int end = text.indexOf(closeToken, offset);
  77. while (end > -1) {
  78. if (end > offset && src[end - 1] == '\\') {
  79. // 遇到`\`该参数不需要处理
  80. // this close token is escaped. remove the backslash and continue.
  81. expression.append(src, offset, end - offset - 1).append(closeToken);
  82. // 计算offset重新推算替换的字符串
  83. offset = end + closeToken.length();
  84. end = text.indexOf(closeToken, offset);
  85. } else {
  86. expression.append(src, offset, end - offset);
  87. break;
  88. }
  89. }
  90. if (end == -1) {
  91. // end == -1 closeToken 不存在,获取后面的所有字符串, openToken - closeToken 之间的内容
  92. // close token was not found.
  93. builder.append(src, start, src.length - start);
  94. offset = src.length;
  95. } else {
  96. // closeToken存在 继续执行
  97. builder.append(handler.handleToken(expression.toString()));
  98. offset = end + closeToken.length();
  99. }
  100. }
  101. start = text.indexOf(openToken, offset);
  102. }
  103. if (offset < src.length) {
  104. builder.append(src, offset, src.length - offset);
  105. }
  106. // 返回的是一个替换后的sql脚本
  107. return builder.toString();
  108. }
  109. }
  • 一个具体的例子org.apache.ibatis.builder.SqlSourceBuilder.ParameterMappingTokenHandler
    • 具体类org.apache.ibatis.builder.SqlSourceBuilder
  1. /**
  2. * ? 的来源
  3. *
  4. * @param content
  5. * @return
  6. */
  7. @Override
  8. public String handleToken(String content) {
  9. parameterMappings.add(buildParameterMapping(content));
  10. return "?";
  11. }
  1. /**
  2. * sql 参数类型 , 返回值
  3. *
  4. * <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  5. * <!--@mbg.generated-->
  6. * select
  7. * <include refid="Base_Column_List" />
  8. * from hs_sell
  9. * where ID = #{id,jdbcType=INTEGER}
  10. * </select>
  11. * => 替换成问号
  12. * select
  13. * <p>
  14. * <p>
  15. * ID, USER_ID, GOOD_ID, PRICE, `SIZE`, COMPANY_ID, GROUP_ID, VERSION, DELETED, CREATE_USER,
  16. * CREATE_TIME, UPDATE_USER, UPDATE_TIME, WORK_ORDER_ID
  17. * <p>
  18. * from hs_sell
  19. * where ID = ?
  20. *
  21. * @param originalSql sql文本
  22. * @param parameterType 默认 object
  23. * @param additionalParameters
  24. * @return
  25. */
  26. public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) {
  27. ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters);
  28. // org.apache.ibatis.builder.SqlSourceBuilder.ParameterMappingTokenHandler.handleToken
  29. GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
  30. String sql = parser.parse(originalSql);
  31. return new StaticSqlSource(configuration, sql, handler.getParameterMappings());
  32. }

image-20191219100446796