Skip to content

Commit

Permalink
db模块新增更新空值方法。
Browse files Browse the repository at this point in the history
  • Loading branch information
javamxd committed Aug 15, 2021
1 parent 60ebfc4 commit 68f9648
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author
*/
public class NamedTable {
String tableName;

Expand All @@ -38,6 +35,8 @@ public class NamedTable {

boolean useLogic = false;

boolean withBlank = false;

Where where = new Where(this);

public NamedTable(String tableName, SQLModule sqlModule, Function<String, String> rowMapColumnMapper) {
Expand All @@ -54,6 +53,12 @@ public NamedTable logic(){
return this;
}

@Comment("更新空值")
public NamedTable withBlank(){
this.withBlank = true;
return this;
}

@Comment("设置主键名,update时使用")
public NamedTable primary(String primary) {
return primary(primary, null);
Expand Down Expand Up @@ -125,7 +130,10 @@ public NamedTable groupBy(@Comment("要分组的列") String... columns) {
return this;
}

private List<Map.Entry<String, Object>> filterNotBlanks() {
private Collection<Map.Entry<String, Object>> filterNotBlanks() {
if(this.withBlank){
return this.columns.entrySet();
}
return this.columns.entrySet().stream()
.filter(it -> StringUtils.isNotBlank(Objects.toString(it.getValue(), "")))
.collect(Collectors.toList());
Expand All @@ -144,7 +152,7 @@ public Object insert(@Comment("各项列和值") Map<String, Object> data) {
if (this.defaultPrimaryValue != null && StringUtils.isBlank(Objects.toString(this.columns.getOrDefault(this.primary, "")))) {
this.columns.put(this.primary, this.defaultPrimaryValue);
}
List<Map.Entry<String, Object>> entries = filterNotBlanks();
Collection<Map.Entry<String, Object>> entries = filterNotBlanks();
if (entries.isEmpty()) {
throw new MagicAPIException("参数不能为空");
}
Expand Down Expand Up @@ -281,13 +289,8 @@ public int update(@Comment("各项列和值") Map<String, Object> data, @Comment
if (StringUtils.isNotBlank(this.primary)) {
primaryValue = this.columns.remove(this.primary);
}
List<Map.Entry<String, Object>> entries = null;
if (!isUpdateBlank) {
entries = filterNotBlanks();
} else {
entries = new ArrayList<>(this.columns.entrySet());
}

this.withBlank = isUpdateBlank;
List<Map.Entry<String, Object>> entries = new ArrayList<>(filterNotBlanks());
if (entries.isEmpty()) {
throw new MagicAPIException("要修改的列不能为空");
}
Expand Down

0 comments on commit 68f9648

Please sign in to comment.