Skip to content

Commit

Permalink
refactor: 优化update时的值回填.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Apr 7, 2024
1 parent a36c964 commit b9af42a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,38 @@ public <T extends QueryParam> T toQueryParam(Supplier<T> template) {

protected UpdateResultOperator doExecute() {
return EventResultOperator.create(
() -> {
if (null != instance) {
applyColumns(instance);
() -> {
if (null != instance) {
applyColumns(instance);
}
for (Map.Entry<String, Object> entry : tempInstance.entrySet()) {
if (entry.getValue() != null) {
operator.set(entry.getKey(), entry.getValue());
}
return operator
.where(dsl -> terms.forEach(dsl::accept))
.execute();
},
UpdateResultOperator.class,
table,
MappingEventTypes.update_before,
MappingEventTypes.update_after,
contextKeyValues.toArray(new ContextKeyValue[0])
}
return operator
.where(dsl -> terms.forEach(dsl::accept))
.execute();
},
UpdateResultOperator.class,
table,
MappingEventTypes.update_before,
MappingEventTypes.update_after,
contextKeyValues.toArray(new ContextKeyValue[0])
);
}

private void applyColumns(E instance) {
mapping
.getColumnPropertyMapping()
.entrySet()
.stream()
.filter(e -> includes.isEmpty() || includes.contains(e.getKey()) || includes.contains(e.getValue()))
.filter(e -> !excludes.contains(e.getKey()) && !excludes.contains(e.getValue()))
.forEach(e -> GlobalConfig
.getPropertyOperator()
.getProperty(instance, e.getValue())
.ifPresent(val -> this.set(e.getKey(), val)));
.getColumnPropertyMapping()
.entrySet()
.stream()
.filter(e -> includes.isEmpty() || includes.contains(e.getKey()) || includes.contains(e.getValue()))
.filter(e -> !excludes.contains(e.getKey()) && !excludes.contains(e.getValue()))
.forEach(e -> GlobalConfig
.getPropertyOperator()
.getProperty(instance, e.getValue())
.ifPresent(val -> this.set(e.getKey(), val)));
}

@Override
Expand Down Expand Up @@ -214,7 +219,7 @@ public ME accept(Param param) {
if (param instanceof UpdateParam) {
includes.addAll(param.getIncludes());
excludes.addAll(param.getExcludes());
set((E)((UpdateParam<?>) param).getData());
set((E) ((UpdateParam<?>) param).getData());
}
return DSLUpdate.super.accept(param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public UpdateOperator set(String column, Object value) {
UpdateColumn updateColumn = new UpdateColumn();
updateColumn.setColumn(column);
updateColumn.setValue(value);
parameter.getColumns().add(updateColumn);
return this;
return set(updateColumn);
}

@Override
Expand All @@ -30,6 +29,7 @@ public UpdateOperator set(Object entity) {

@Override
public UpdateOperator set(UpdateColumn column) {
parameter.getColumns().remove(column);
parameter.getColumns().add(column);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.hswebframework.ezorm.rdb.operator.dml.update;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.rdb.operator.dml.FunctionColumn;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true,exclude = "value")
public class UpdateColumn extends FunctionColumn {

private Object value;
Expand Down

0 comments on commit b9af42a

Please sign in to comment.