Where.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package org.ssssssss.magicapi.modules.table;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.ssssssss.script.annotation.Comment;
  4. import org.ssssssss.script.functions.StreamExtension;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.function.Function;
  10. public class Where {
  11. private final List<String> tokens = new ArrayList<>();
  12. private final List<Object> params = new ArrayList<>();
  13. private final NamedTable namedTable;
  14. private final boolean needWhere;
  15. public Where(NamedTable namedTable) {
  16. this(namedTable, true);
  17. }
  18. public Where(NamedTable namedTable, boolean needWhere) {
  19. this.namedTable = namedTable;
  20. this.needWhere = needWhere;
  21. }
  22. void appendAnd() {
  23. remove();
  24. tokens.add("and");
  25. }
  26. void appendOr() {
  27. remove();
  28. tokens.add("or");
  29. }
  30. List<Object> getParams() {
  31. return params;
  32. }
  33. void remove() {
  34. int size = tokens.size();
  35. while (size > 0) {
  36. String token = tokens.get(size - 1);
  37. if ("and".equalsIgnoreCase(token) || "or".equalsIgnoreCase(token)) {
  38. tokens.remove(size - 1);
  39. size--;
  40. }else {
  41. break;
  42. }
  43. }
  44. while(size > 0){
  45. String token = tokens.get(0);
  46. if ("and".equalsIgnoreCase(token) || "or".equalsIgnoreCase(token)) {
  47. tokens.remove(0);
  48. size--;
  49. }else {
  50. break;
  51. }
  52. }
  53. }
  54. boolean isEmpty() {
  55. return tokens.isEmpty();
  56. }
  57. void append(String value) {
  58. tokens.add(value);
  59. }
  60. String getSql() {
  61. remove();
  62. if (isEmpty()) {
  63. return "";
  64. }
  65. return (needWhere ? " where " : "") + StringUtils.join(tokens, " ");
  66. }
  67. @Comment("等于`=`,如:`eq('name', '老王') ---> name = '老王'`")
  68. public Where eq(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  69. return eq(true, column, value);
  70. }
  71. @Comment("等于`=`,如:`eq('name', '老王') ---> name = '老王'`")
  72. public Where eq(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  73. if (condition) {
  74. tokens.add(column);
  75. if (value == null) {
  76. append(" is null");
  77. } else {
  78. params.add(value);
  79. append(" = ?");
  80. }
  81. appendAnd();
  82. }
  83. return this;
  84. }
  85. @Comment("不等于`<>`,如:`ne('name', '老王') ---> name <> '老王'`")
  86. public Where ne(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  87. return ne(true, column, value);
  88. }
  89. @Comment("不等于`<>`,如:`ne('name', '老王') ---> name <> '老王'`")
  90. public Where ne(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  91. if (condition) {
  92. append(column);
  93. if (value == null) {
  94. append("is not null");
  95. } else {
  96. params.add(value);
  97. append("<> ?");
  98. }
  99. appendAnd();
  100. }
  101. return this;
  102. }
  103. private Where append(boolean append, String column, String condition, Object value) {
  104. if (append) {
  105. append(column);
  106. append(condition);
  107. appendAnd();
  108. params.add(value);
  109. }
  110. return this;
  111. }
  112. @Comment("小于`<`,如:`lt('age', 18) ---> age < 18")
  113. public Where lt(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  114. return lt(true, column, value);
  115. }
  116. @Comment("小于`<`,如:`lt('age', 18) ---> age < 18")
  117. public Where lt(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  118. return append(condition, column, " < ?", value);
  119. }
  120. @Comment("小于等于`<=`,如:`lte('age', 18) ---> age <= 18")
  121. public Where lte(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  122. return lte(true, column, value);
  123. }
  124. @Comment("小于等于`<=`,如:`lte('age', 18) ---> age <= 18")
  125. public Where lte(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  126. return append(condition, column, " <= ?", value);
  127. }
  128. @Comment("大于`>`,如:`get('age', 18) ---> age > 18")
  129. public Where gt(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  130. return gt(true, column, value);
  131. }
  132. @Comment("大于`>`,如:`get('age', 18) ---> age > 18")
  133. public Where gt(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  134. return append(condition, column, " > ?", value);
  135. }
  136. @Comment("大于等于`>=`,如:`get('age', 18) ---> age >= 18")
  137. public Where gte(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  138. return gte(true, column, value);
  139. }
  140. @Comment("大于等于`>=`,如:`get('age', 18) ---> age >= 18")
  141. public Where gte(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  142. return append(condition, column, " >= ?", value);
  143. }
  144. @Comment("`in`,如:`in('age', [1,2,3]) ---> age in (1,2,3)")
  145. public Where in(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  146. return in(true, column, value);
  147. }
  148. @Comment("`in`,如:`in('age', [1,2,3]) ---> age in (1,2,3)")
  149. public Where in(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  150. if (condition && value != null) {
  151. List<Object> objects = StreamExtension.arrayLikeToList(value);
  152. if (objects.size() > 0) {
  153. append(column);
  154. append(" in (");
  155. append(StringUtils.join(",", Collections.nCopies(objects.size(), "?")));
  156. append(")");
  157. appendAnd();
  158. params.addAll(objects);
  159. }
  160. }
  161. return this;
  162. }
  163. @Comment("`not in`,如:`notIn('age', [1,2,3]) ---> age not in (1,2,3)")
  164. public Where notIn(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  165. return notIn(true, column, value);
  166. }
  167. @Comment("`not in`,如:`notIn('age', [1,2,3]) ---> age not in (1,2,3)")
  168. public Where notIn(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  169. if (condition && value != null) {
  170. List<Object> objects = StreamExtension.arrayLikeToList(value);
  171. if (objects.size() > 0) {
  172. append(column);
  173. append("not in (");
  174. append(StringUtils.join(",", Collections.nCopies(objects.size(), "?")));
  175. append(")");
  176. appendAnd();
  177. params.addAll(objects);
  178. }
  179. }
  180. return this;
  181. }
  182. @Comment("`like`,如:`like('name', '%王%') ---> name like '%王%'")
  183. public Where like(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  184. return like(true, column, value);
  185. }
  186. @Comment("`like`,如:`like('name', '%王%') ---> name like '%王%'")
  187. public Where like(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  188. return append(condition, column, "like ?", value);
  189. }
  190. @Comment("`not like`,如:`notLike('name', '%王%') ---> name not like '%王%'")
  191. public Where notLike(@Comment("数据库中的列名") String column, @Comment("值")Object value) {
  192. return notLike(true, column, value);
  193. }
  194. @Comment("`not like` ,如:`notLike('name', '%王%') ---> name not like '%王%'")
  195. public Where notLike(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column, @Comment("值")Object value) {
  196. return append(condition, column, "not like ?", value);
  197. }
  198. @Comment("`is null`,如:`isNull('name') ---> name is null")
  199. public Where isNull(@Comment("数据库中的列名") String column) {
  200. return isNull(true, column);
  201. }
  202. @Comment("`is null`,如:`isNull('name') ---> name is null")
  203. public Where isNull(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column) {
  204. if (condition) {
  205. append(column);
  206. append("is null");
  207. appendAnd();
  208. }
  209. return this;
  210. }
  211. @Comment("`is not null`,如:`isNotNull('name') ---> name is not null")
  212. public Where isNotNull(@Comment("数据库中的列名") String column) {
  213. return isNotNull(true, column);
  214. }
  215. @Comment("`is not null`,如:`isNotNull('name') ---> name is not null")
  216. public Where isNotNull(@Comment("判断表达式,当为true时拼接条件") boolean condition, @Comment("数据库中的列名") String column) {
  217. if (condition) {
  218. append(column);
  219. append("is not null");
  220. appendAnd();
  221. }
  222. return this;
  223. }
  224. @Comment("拼接`or`")
  225. public Where or() {
  226. appendOr();
  227. return this;
  228. }
  229. @Comment("拼接`and`")
  230. public Where and() {
  231. appendAnd();
  232. return this;
  233. }
  234. @Comment("`and`嵌套,如and(it => it.eq('name','李白').ne('status','正常') --> and (name = '李白' and status <> '正常')")
  235. public Where and(Function<Object[], Where> function) {
  236. return and(true, function);
  237. }
  238. @Comment("`and`嵌套,如and(it => it.eq('name','李白').ne('status','正常') --> and (name = '李白' and status <> '正常')")
  239. public Where and(@Comment("判断表达式,当为true时拼接条件") boolean condition, Function<Object[], Where> function) {
  240. if (condition) {
  241. Where expr = function.apply(new Object[]{new Where(this.namedTable, false)});
  242. this.params.addAll(expr.params);
  243. append("(");
  244. append(expr.getSql());
  245. append(")");
  246. appendAnd();
  247. }
  248. return this;
  249. }
  250. @Comment("保存到表中,当主键有值时则修改,否则插入")
  251. public Object save() {
  252. return namedTable.save();
  253. }
  254. @Comment("保存到表中,当主键有值时则修改,否则插入")
  255. public Object save(@Comment("各项列和值") Map<String, Object> data) {
  256. return namedTable.save(data);
  257. }
  258. @Comment("执行插入语句,返回主键")
  259. public Object insert() {
  260. return namedTable.insert();
  261. }
  262. @Comment("执行插入语句,返回主键")
  263. public Object insert(@Comment("各项列和值") Map<String, Object> data) {
  264. return namedTable.insert(data);
  265. }
  266. @Comment("执行update语句")
  267. public int update() {
  268. return namedTable.update();
  269. }
  270. @Comment("执行update语句")
  271. public int update(@Comment("各项列和值") Map<String, Object> data) {
  272. return namedTable.update(data);
  273. }
  274. @Comment("执行分页查询")
  275. public Object page() {
  276. return namedTable.page();
  277. }
  278. @Comment("执行select查询")
  279. public List<Map<String, Object>> select() {
  280. return namedTable.select();
  281. }
  282. @Comment("执行selectOne查询")
  283. public Map<String, Object> selectOne() {
  284. return namedTable.selectOne();
  285. }
  286. }