MybatisAbstractSQL.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import com.baomidou.mybatisplus.toolkit.CollectionUtils;
  22. import com.baomidou.mybatisplus.toolkit.StringUtils;
  23. /**
  24. * <p>
  25. * 重定义 AbstractSQL ,实现标准TSQL的 查询条件自定义
  26. * </p>
  27. *
  28. * @author yanghu
  29. * @Date 2016-08-22
  30. */
  31. @SuppressWarnings("serial")
  32. public abstract class MybatisAbstractSQL<T> implements Serializable {
  33. private static final String AND = " AND ";
  34. private static final String OR = " OR ";
  35. private static final String AND_NEW = ") \nAND (";
  36. private static final String OR_NEW = ") \nOR (";
  37. /**
  38. * SQL条件
  39. */
  40. private final SQLCondition sql = new SQLCondition();
  41. /**
  42. * 子类泛型实现
  43. *
  44. * @return 泛型实例
  45. */
  46. public abstract T getSelf();
  47. public T WHERE(String conditions) {
  48. sql().where.add(conditions);
  49. sql().lastList = sql().where;
  50. return getSelf();
  51. }
  52. public T OR() {
  53. sql().lastList.add(OR);
  54. return getSelf();
  55. }
  56. public T OR_NEW() {
  57. sql().lastList.add(OR_NEW);
  58. return getSelf();
  59. }
  60. public T AND() {
  61. sql().lastList.add(AND);
  62. return getSelf();
  63. }
  64. public T AND_NEW() {
  65. sql().lastList.add(AND_NEW);
  66. return getSelf();
  67. }
  68. public T GROUP_BY(String columns) {
  69. sql().groupBy.add(columns);
  70. return getSelf();
  71. }
  72. public T HAVING(String conditions) {
  73. sql().having.add(conditions);
  74. sql().lastList = sql().having;
  75. return getSelf();
  76. }
  77. public T ORDER_BY(String columns) {
  78. sql().orderBy.add(columns);
  79. return getSelf();
  80. }
  81. public T LAST(String last) {
  82. sql().last = last;
  83. return getSelf();
  84. }
  85. private SQLCondition sql() {
  86. return sql;
  87. }
  88. @Override
  89. public String toString() {
  90. StringBuilder sb = new StringBuilder();
  91. sql().sql(sb);
  92. return sb.toString();
  93. }
  94. /**
  95. * 查看构造器where是否为空
  96. *
  97. * @return
  98. */
  99. public boolean isEmptyOfWhere() {
  100. return CollectionUtils.isEmpty(sql().where);
  101. }
  102. /**
  103. * SQL连接器
  104. */
  105. private static class SafeAppendable implements Serializable {
  106. private final Appendable appendable;
  107. private boolean empty = true;
  108. public SafeAppendable(Appendable appendable) {
  109. super();
  110. this.appendable = appendable;
  111. }
  112. public SafeAppendable append(CharSequence charSequence) {
  113. try {
  114. if (empty && charSequence.length() > 0) {
  115. empty = false;
  116. }
  117. appendable.append(charSequence);
  118. } catch (IOException e) {
  119. throw new RuntimeException(e);
  120. }
  121. return this;
  122. }
  123. public boolean isEmpty() {
  124. return empty;
  125. }
  126. }
  127. /**
  128. * SQL条件类
  129. */
  130. private static class SQLCondition implements Serializable {
  131. final List<String> where = new ArrayList<>();
  132. final List<String> having = new ArrayList<>();
  133. final List<String> groupBy = new ArrayList<>();
  134. final List<String> orderBy = new ArrayList<>();
  135. final List<String> andOr = new ArrayList<>();
  136. String last = null;
  137. List<String> lastList = new ArrayList<>();
  138. public SQLCondition() {
  139. andOr.add(AND);
  140. andOr.add(OR);
  141. andOr.add(AND_NEW);
  142. andOr.add(OR_NEW);
  143. }
  144. /**
  145. * 构建SQL的条件
  146. *
  147. * @param builder 连接器
  148. * @param keyword TSQL中的关键字
  149. * @param parts SQL条件语句集合
  150. * @param open 起始符号
  151. * @param close 结束符号
  152. * @param conjunction 连接条件
  153. */
  154. private void sqlClause(SafeAppendable builder, String keyword, List<String> parts, String open, String close,
  155. String conjunction) {
  156. parts = clearNull(parts);
  157. if (!parts.isEmpty()) {
  158. if (!builder.isEmpty()) {
  159. builder.append("\n");
  160. }
  161. builder.append(keyword);
  162. builder.append(" ");
  163. builder.append(open);
  164. String last = "__";
  165. for (int i = 0, n = parts.size(); i < n; i++) {
  166. String part = parts.get(i);
  167. if (i > 0) {
  168. if (andOr.contains(part) || andOr.contains(last)) {
  169. builder.append(part);
  170. last = part;
  171. continue;
  172. } else {
  173. builder.append(conjunction);
  174. }
  175. }
  176. builder.append(part);
  177. }
  178. builder.append(close);
  179. }
  180. }
  181. /**
  182. * 清除LIST中的NULL和空字符串
  183. *
  184. * @param parts 原LIST列表
  185. * @return
  186. */
  187. private List<String> clearNull(List<String> parts) {
  188. List<String> temps = new ArrayList<>();
  189. for (String part : parts) {
  190. if (StringUtils.isEmpty(part)) {
  191. continue;
  192. }
  193. temps.add(part);
  194. }
  195. return temps;
  196. }
  197. /**
  198. * 按标准顺序连接并构建SQL
  199. *
  200. * @param builder 连接器
  201. * @return
  202. */
  203. private String buildSQL(SafeAppendable builder) {
  204. sqlClause(builder, "WHERE", where, "(", ")", AND);
  205. sqlClause(builder, "GROUP BY", groupBy, "", "", ", ");
  206. sqlClause(builder, "HAVING", having, "(", ")", AND);
  207. sqlClause(builder, "ORDER BY", orderBy, "", "", ", ");
  208. if (StringUtils.isNotEmpty(last)) {
  209. builder.append(" ");
  210. builder.append(last);
  211. }
  212. return builder.toString();
  213. }
  214. public String sql(Appendable appendable) {
  215. return buildSQL(new SafeAppendable(appendable));
  216. }
  217. }
  218. }