NamedTable.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package org.ssssssss.magicapi.modules.table;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.ssssssss.magicapi.exception.MagicAPIException;
  4. import org.ssssssss.magicapi.modules.BoundSql;
  5. import org.ssssssss.magicapi.modules.SQLModule;
  6. import org.ssssssss.script.annotation.Comment;
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. public class NamedTable {
  10. String tableName;
  11. SQLModule sqlModule;
  12. String primary;
  13. Map<String, Object> columns = new HashMap<>();
  14. List<String> fields = new ArrayList<>();
  15. Where where = new Where(this);
  16. public NamedTable(String tableName, SQLModule sqlModule) {
  17. this.tableName = tableName;
  18. this.sqlModule = sqlModule;
  19. }
  20. @Comment("设置主键名,update时使用")
  21. public NamedTable primary(String primary) {
  22. this.primary = primary;
  23. return this;
  24. }
  25. @Comment("拼接where")
  26. public Where where() {
  27. return where;
  28. }
  29. @Comment("设置单列的值")
  30. public NamedTable column(@Comment("列名") String key, @Comment("值") Object value) {
  31. this.columns.put(key, value);
  32. return this;
  33. }
  34. @Comment("设置查询的列,如`columns('a','b','c')`")
  35. public NamedTable columns(@Comment("各项列") String... columns) {
  36. if (columns != null) {
  37. for (String column : columns) {
  38. column(column);
  39. }
  40. }
  41. return this;
  42. }
  43. @Comment("设置查询的列,如`columns(['a','b','c'])`")
  44. public NamedTable columns(Collection<String> columns) {
  45. if (columns != null) {
  46. columns.stream().filter(StringUtils::isNotBlank).forEach(this.fields::add);
  47. }
  48. return this;
  49. }
  50. @Comment("设置查询的列,如`column('a')`")
  51. public NamedTable column(String column) {
  52. if (StringUtils.isNotBlank(column)) {
  53. this.fields.add(column);
  54. }
  55. return this;
  56. }
  57. private List<Map.Entry<String, Object>> filterNotBlanks() {
  58. return this.columns.entrySet().stream()
  59. .filter(it -> StringUtils.isNotBlank(Objects.toString(it.getValue(), "")))
  60. .collect(Collectors.toList());
  61. }
  62. @Comment("执行插入")
  63. public int insert() {
  64. return insert(null);
  65. }
  66. @Comment("执行插入")
  67. public int insert(@Comment("各项列和值") Map<String, Object> data) {
  68. if (data != null) {
  69. this.columns.putAll(data);
  70. }
  71. List<Map.Entry<String, Object>> entries = filterNotBlanks();
  72. if (entries.isEmpty()) {
  73. throw new MagicAPIException("参数不能为空");
  74. }
  75. StringBuilder builder = new StringBuilder();
  76. builder.append("insert into ");
  77. builder.append(tableName);
  78. builder.append("(");
  79. builder.append(StringUtils.join(entries.stream().map(Map.Entry::getKey).toArray(), ","));
  80. builder.append(") values (");
  81. builder.append(StringUtils.join(Collections.nCopies(entries.size(), "?"), ","));
  82. builder.append(")");
  83. return sqlModule.update(new BoundSql(builder.toString(), entries.stream().map(Map.Entry::getValue).collect(Collectors.toList()), sqlModule));
  84. }
  85. @Comment("保存到表中,当主键有值时则修改,否则插入")
  86. public int save() {
  87. return this.save(null);
  88. }
  89. @Comment("保存到表中,当主键有值时则修改,否则插入")
  90. public int save(@Comment("各项列和值") Map<String, Object> data) {
  91. if (StringUtils.isBlank(this.primary)) {
  92. throw new MagicAPIException("请设置主键");
  93. }
  94. if (this.columns.get(this.primary) != null || (data != null && data.get(this.primary) != null)) {
  95. return update(data);
  96. }
  97. return insert(data);
  98. }
  99. @Comment("执行`select`查询")
  100. public List<Map<String, Object>> select() {
  101. return sqlModule.select(buildSelect());
  102. }
  103. @Comment("执行`selectOne`查询")
  104. public Map<String, Object> selectOne() {
  105. return sqlModule.selectOne(buildSelect());
  106. }
  107. private BoundSql buildSelect() {
  108. StringBuilder builder = new StringBuilder();
  109. builder.append("select ");
  110. if (this.fields.isEmpty()) {
  111. builder.append("*");
  112. } else {
  113. builder.append(StringUtils.join(this.fields, ","));
  114. }
  115. builder.append(" from ").append(tableName);
  116. List<Object> params = new ArrayList<>();
  117. if (!where.isEmpty()) {
  118. builder.append(where.getSql());
  119. params.addAll(where.getParams());
  120. }
  121. return new BoundSql(builder.toString(), params, sqlModule);
  122. }
  123. @Comment("执行分页查询")
  124. public Object page() {
  125. return sqlModule.page(buildSelect());
  126. }
  127. @Comment("执行update语句")
  128. public int update() {
  129. return update(null);
  130. }
  131. @Comment("执行update语句")
  132. public int update(@Comment("各项列和值") Map<String, Object> data) {
  133. if (null != data) {
  134. this.columns.putAll(data);
  135. }
  136. Object primaryValue = null;
  137. if (StringUtils.isNotBlank(this.primary)) {
  138. primaryValue = this.columns.remove(this.primary);
  139. }
  140. List<Map.Entry<String, Object>> entries = filterNotBlanks();
  141. if (entries.isEmpty()) {
  142. throw new MagicAPIException("要修改的列不能为空");
  143. }
  144. StringBuilder builder = new StringBuilder();
  145. builder.append("update ");
  146. builder.append(tableName);
  147. builder.append(" set ");
  148. List<Object> params = new ArrayList<>();
  149. for (int i = 0,size = entries.size(); i < size; i++) {
  150. Map.Entry<String, Object> entry = entries.get(i);
  151. builder.append(entry.getKey()).append(" = ?");
  152. params.add(entry.getValue());
  153. if(i + 1 < size){
  154. builder.append(",");
  155. }
  156. }
  157. if (!where.isEmpty()) {
  158. builder.append(where.getSql());
  159. params.addAll(where.getParams());
  160. } else if(primaryValue != null){
  161. builder.append(" where ").append(this.primary).append(" = ?");
  162. params.add(primaryValue);
  163. }else{
  164. throw new MagicAPIException("主键值不能为空");
  165. }
  166. return sqlModule.update(new BoundSql(builder.toString(), params, sqlModule));
  167. }
  168. }