SqlUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 java.util.List;
  18. import com.baomidou.mybatisplus.enums.SqlLike;
  19. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  20. import com.baomidou.mybatisplus.plugins.pagination.Pagination;
  21. import com.baomidou.mybatisplus.plugins.parser.ISqlParser;
  22. import com.baomidou.mybatisplus.plugins.parser.SqlInfo;
  23. /**
  24. * <p>
  25. * SqlUtils工具类
  26. * </p>
  27. *
  28. * @author Caratacus
  29. * @Date 2016-11-13
  30. */
  31. public class SqlUtils {
  32. private final static SqlFormatter sqlFormatter = new SqlFormatter();
  33. public final static String SQL_BASE_COUNT = "SELECT COUNT(1) FROM ( %s ) TOTAL";
  34. public static ISqlParser COUNT_SQL_PARSER = null;
  35. private static Class<ISqlParser> DEFAULT_CLASS = null;
  36. static {
  37. try {
  38. DEFAULT_CLASS = (Class<ISqlParser>) Class.forName("com.baomidou.mybatisplus.plugins.pagination.optimize.JsqlParserCountOptimize");
  39. } catch (ClassNotFoundException e) {
  40. //skip
  41. }
  42. }
  43. /**
  44. * <p>
  45. * 获取CountOptimize
  46. * </p>
  47. *
  48. * @param sqlParser Count SQL 解析类
  49. * @param originalSql 需要计算Count SQL
  50. * @return SqlInfo
  51. */
  52. public static SqlInfo getCountOptimize(ISqlParser sqlParser, String originalSql) {
  53. // COUNT SQL 解析器
  54. if (null == COUNT_SQL_PARSER) {
  55. if (null != sqlParser) {
  56. // 用户自定义 COUNT SQL 解析
  57. COUNT_SQL_PARSER = sqlParser;
  58. } else {
  59. // 默认 JsqlParser 优化 COUNT
  60. try {
  61. // TODO: 2017/11/20 这里我改动了下.
  62. COUNT_SQL_PARSER = DEFAULT_CLASS.newInstance();
  63. } catch (Exception e) {
  64. throw new MybatisPlusException(e);
  65. }
  66. }
  67. }
  68. return COUNT_SQL_PARSER.optimizeSql(null, originalSql);
  69. }
  70. /**
  71. * 查询SQL拼接Order By
  72. *
  73. * @param originalSql 需要拼接的SQL
  74. * @param page page对象
  75. * @param orderBy 是否需要拼接Order By
  76. * @return
  77. */
  78. public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
  79. if (orderBy && page.isOpenSort()) {
  80. StringBuilder buildSql = new StringBuilder(originalSql);
  81. String ascStr = concatOrderBuilder(page.getAscs(), " ASC");
  82. String descStr = concatOrderBuilder(page.getDescs(), " DESC");
  83. if (StringUtils.isNotEmpty(ascStr) && StringUtils.isNotEmpty(descStr)) {
  84. ascStr += ", ";
  85. }
  86. if (StringUtils.isNotEmpty(ascStr) || StringUtils.isNotEmpty(descStr)) {
  87. buildSql.append(" ORDER BY ").append(ascStr).append(descStr);
  88. }
  89. return buildSql.toString();
  90. }
  91. return originalSql;
  92. }
  93. /**
  94. * 拼接多个排序方法
  95. *
  96. * @param columns
  97. * @param orderWord
  98. */
  99. private static String concatOrderBuilder(List<String> columns, String orderWord) {
  100. if (CollectionUtils.isNotEmpty(columns)) {
  101. StringBuilder builder = new StringBuilder(16);
  102. for (int i = 0; i < columns.size(); ) {
  103. String cs = columns.get(i);
  104. if (StringUtils.isNotEmpty(cs)) {
  105. builder.append(cs).append(orderWord);
  106. }
  107. if (++i != columns.size() && StringUtils.isNotEmpty(cs)) {
  108. builder.append(", ");
  109. }
  110. }
  111. return builder.toString();
  112. }
  113. return StringUtils.EMPTY;
  114. }
  115. /**
  116. * 格式sql
  117. *
  118. * @param boundSql
  119. * @param format
  120. * @return
  121. */
  122. public static String sqlFormat(String boundSql, boolean format) {
  123. if (format) {
  124. return sqlFormatter.format(boundSql);
  125. } else {
  126. return boundSql.replaceAll("[\\s]+", " ");
  127. }
  128. }
  129. /**
  130. * <p>
  131. * 用%连接like
  132. * </p>
  133. *
  134. * @param str 原字符串
  135. * @return
  136. */
  137. public static String concatLike(String str, SqlLike type) {
  138. StringBuilder builder = new StringBuilder(str.length() + 3);
  139. switch (type) {
  140. case LEFT:
  141. builder.append("%").append(str);
  142. break;
  143. case RIGHT:
  144. builder.append(str).append("%");
  145. break;
  146. case CUSTOM:
  147. builder.append(str);
  148. break;
  149. default:
  150. builder.append("%").append(str).append("%");
  151. }
  152. return builder.toString();
  153. }
  154. }