SqlUtils.java 4.1 KB

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