SqlUtils.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.parser.AbstractSqlParser;
  19. import com.baomidou.mybatisplus.parser.SqlInfo;
  20. import com.baomidou.mybatisplus.plugins.pagination.Pagination;
  21. import com.baomidou.mybatisplus.plugins.pagination.optimize.JsqlParserCountOptimize;
  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 AbstractSqlParser COUNT_SQL_PARSER = null;
  34. /**
  35. * <p>
  36. * 获取CountOptimize
  37. * </p>
  38. *
  39. * @param sqlParser Count SQL 解析类
  40. * @param originalSql 需要计算Count SQL
  41. * @return SqlInfo
  42. */
  43. public static SqlInfo getCountOptimize(AbstractSqlParser sqlParser, String originalSql) {
  44. // COUNT SQL 解析器
  45. if (null == COUNT_SQL_PARSER) {
  46. if (null != sqlParser) {
  47. // 用户自定义 COUNT SQL 解析
  48. COUNT_SQL_PARSER = sqlParser;
  49. } else {
  50. // 默认 JsqlParser 优化 COUNT
  51. COUNT_SQL_PARSER = new JsqlParserCountOptimize();
  52. }
  53. }
  54. return COUNT_SQL_PARSER.optimizeSql(originalSql, null);
  55. }
  56. /**
  57. * 查询SQL拼接Order By
  58. *
  59. * @param originalSql 需要拼接的SQL
  60. * @param page page对象
  61. * @param orderBy 是否需要拼接Order By
  62. * @return
  63. */
  64. public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
  65. if (orderBy && StringUtils.isNotEmpty(page.getOrderByField()) && page.isOpenSort()) {
  66. StringBuilder buildSql = new StringBuilder(originalSql);
  67. buildSql.append(" ORDER BY ").append(page.getOrderByField());
  68. buildSql.append(page.isAsc() ? " ASC " : " DESC ");
  69. return buildSql.toString();
  70. }
  71. return originalSql;
  72. }
  73. /**
  74. * 格式sql
  75. *
  76. * @param boundSql
  77. * @param format
  78. * @return
  79. */
  80. public static String sqlFormat(String boundSql, boolean format) {
  81. if (format) {
  82. return sqlFormatter.format(boundSql);
  83. } else {
  84. return boundSql.replaceAll("[\\s]+", " ");
  85. }
  86. }
  87. /**
  88. * <p>
  89. * 用%连接like
  90. * </p>
  91. *
  92. * @param str 原字符串
  93. * @return
  94. */
  95. public static String concatLike(String str, SqlLike type) {
  96. StringBuilder builder = new StringBuilder(str.length() + 3);
  97. switch (type) {
  98. case LEFT:
  99. builder.append("%").append(str);
  100. break;
  101. case RIGHT:
  102. builder.append(str).append("%");
  103. break;
  104. case CUSTOM:
  105. builder.append(str);
  106. break;
  107. default:
  108. builder.append("%").append(str).append("%");
  109. }
  110. return builder.toString();
  111. }
  112. }