SqlUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.toolkit;
  17. import com.baomidou.mybatisplus.enums.Optimize;
  18. import com.baomidou.mybatisplus.enums.SqlLike;
  19. import com.baomidou.mybatisplus.plugins.pagination.Pagination;
  20. import com.baomidou.mybatisplus.plugins.pagination.optimize.AliDruidCountOptimize;
  21. import com.baomidou.mybatisplus.plugins.pagination.optimize.DefaultCountOptimize;
  22. import com.baomidou.mybatisplus.plugins.pagination.optimize.JsqlParserCountOptimize;
  23. import com.baomidou.mybatisplus.parser.AbstractSqlParser;
  24. import com.baomidou.mybatisplus.parser.SqlInfo;
  25. /**
  26. * <p>
  27. * SqlUtils工具类
  28. * </p>
  29. *
  30. * @author Caratacus
  31. * @Date 2016-11-13
  32. */
  33. public class SqlUtils {
  34. private final static SqlFormatter sqlFormatter = new SqlFormatter();
  35. public static final String SQL_BASE_COUNT = "SELECT COUNT(1) FROM ( %s ) TOTAL";
  36. /**
  37. * <p>
  38. * 获取CountOptimize
  39. * </p>
  40. *
  41. * @param sqlParser Count SQL 解析类
  42. * @param originalSql 需要计算Count SQL
  43. * @param optimizeType count优化方式
  44. * @param isOptimizeCount 是否需要优化Count
  45. * @return SqlInfo
  46. */
  47. public static SqlInfo getCountOptimize(AbstractSqlParser sqlParser, String originalSql,
  48. String optimizeType, String dialectType,
  49. boolean isOptimizeCount) {
  50. Optimize opType = Optimize.getOptimizeType(optimizeType);
  51. // COUNT SQL 不优化
  52. if (!isOptimizeCount && Optimize.DEFAULT == opType) {
  53. SqlInfo sqlInfo = SqlInfo.newInstance();
  54. String tempSql = originalSql.replaceAll("(?i)ORDER[\\s]+BY", "ORDER BY");
  55. int orderByIndex = tempSql.toUpperCase().lastIndexOf("ORDER BY");
  56. sqlInfo.setOrderBy(orderByIndex > -1);
  57. sqlInfo.setSql(String.format(SQL_BASE_COUNT, originalSql));
  58. return sqlInfo;
  59. }
  60. // 用户自定义 COUNT SQL 解析
  61. if (null != sqlParser) {
  62. return sqlParser.optimizeSql();
  63. }
  64. // 默认存在的优化类型
  65. switch (opType) {
  66. case ALI_DRUID:
  67. sqlParser = new AliDruidCountOptimize(originalSql, dialectType);
  68. break;
  69. case JSQLPARSER:
  70. sqlParser = new JsqlParserCountOptimize(originalSql, dialectType);
  71. break;
  72. default:
  73. sqlParser = new DefaultCountOptimize(originalSql, dialectType);
  74. break;
  75. }
  76. return sqlParser.optimizeSql();
  77. }
  78. /**
  79. * 查询SQL拼接Order By
  80. *
  81. * @param originalSql 需要拼接的SQL
  82. * @param page page对象
  83. * @param orderBy 是否需要拼接Order By
  84. * @return
  85. */
  86. public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
  87. if (orderBy && StringUtils.isNotEmpty(page.getOrderByField()) && page.isOpenSort()) {
  88. StringBuilder buildSql = new StringBuilder(originalSql);
  89. buildSql.append(" ORDER BY ").append(page.getOrderByField());
  90. buildSql.append(page.isAsc() ? " ASC " : " DESC ");
  91. return buildSql.toString();
  92. }
  93. return originalSql;
  94. }
  95. /**
  96. * 格式sql
  97. *
  98. * @param boundSql
  99. * @param format
  100. * @return
  101. */
  102. public static String sqlFormat(String boundSql, boolean format) {
  103. if (format) {
  104. return sqlFormatter.format(boundSql);
  105. } else {
  106. return boundSql.replaceAll("[\\s]+", " ");
  107. }
  108. }
  109. /**
  110. * <p>
  111. * 用%连接like
  112. * </p>
  113. *
  114. * @param str 原字符串
  115. * @return
  116. */
  117. public static String concatLike(String str, SqlLike type) {
  118. StringBuilder builder = new StringBuilder(str.length() + 3);
  119. switch (type) {
  120. case LEFT:
  121. builder.append("%").append(str);
  122. break;
  123. case RIGHT:
  124. builder.append(str).append("%");
  125. break;
  126. case CUSTOM:
  127. builder.append(str);
  128. break;
  129. default:
  130. builder.append("%").append(str).append("%");
  131. }
  132. return builder.toString();
  133. }
  134. }