Skip to content

Commit

Permalink
refactor: 优化查询逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jun 28, 2024
1 parent 0ef1207 commit ef9faed
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,34 @@ protected ContextKeyValue<?>[] getDefaultContextKeyValue(ContextKeyValue<?>... k
public String[] getProperties() {
if (properties == null) {
properties = mapping
.getColumnPropertyMapping()
.entrySet()
.stream()
.filter(kv -> getTable().getColumn(kv.getKey()).isPresent())
.map(Map.Entry::getValue)
.toArray(String[]::new);
.getColumnPropertyMapping()
.entrySet()
.stream()
.filter(kv -> getTable().getColumn(kv.getKey()).isPresent())
.map(Map.Entry::getValue)
.toArray(String[]::new);
}
return properties;
}

protected String getIdColumn() {
if (idColumn == null) {
this.idColumn = getTable()
.getColumns()
.stream()
.filter(RDBColumnMetadata::isPrimaryKey)
.findFirst()
.map(RDBColumnMetadata::getName)
.orElseThrow(() -> new UnsupportedOperationException("id column not exists"));
.getColumns()
.stream()
.filter(RDBColumnMetadata::isPrimaryKey)
.findFirst()
.map(RDBColumnMetadata::getName)
.orElseThrow(() -> new UnsupportedOperationException("id column not exists"));
}
return idColumn;
}

protected void initMapping(Class<E> entityType) {

this.mapping = LazyEntityColumnMapping.of(() -> getTable()
.<EntityColumnMapping>findFeature(MappingFeatureType.columnPropertyMapping.createFeatureId(entityType))
.orElseThrow(() -> new UnsupportedOperationException("unsupported columnPropertyMapping feature for "+entityType)));
this.mapping = LazyEntityColumnMapping
.of(() -> getTable().findFeatureNow(MappingFeatureType.columnPropertyMapping.createFeatureId(entityType)));

defaultContextKeyValue.add(MappingContextKeys.columnMapping(mapping));
}

Expand Down Expand Up @@ -153,9 +153,9 @@ protected E merge(E older, E newer) {

private Object getProperty(E data, String property) {
return GlobalConfig
.getPropertyOperator()
.getProperty(data, property)
.orElse(null);
.getPropertyOperator()
.getProperty(data, property)
.orElse(null);
}

protected SaveResultOperator doSave(Collection<E> data) {
Expand All @@ -164,25 +164,25 @@ protected SaveResultOperator doSave(Collection<E> data) {
UpsertOperator upsert = operator.dml().upsert(table);

return EventResultOperator.create(
() -> {
upsert.columns(getProperties());
List<String> ignore = new ArrayList<>();
for (E e : _data) {
upsert.values(Stream.of(getProperties())
.map(property -> getInsertColumnValue(e, property, (prop, val) -> ignore.add(prop)))
.toArray());
}
upsert.ignoreUpdate(ignore.toArray(new String[0]));
return upsert.execute();
},
SaveResultOperator.class,
table,
MappingEventTypes.save_before,
MappingEventTypes.save_after,
getDefaultContextKeyValue(instance(_data),
type("batch"),
tableMetadata(table),
upsert(upsert))
() -> {
upsert.columns(getProperties());
List<String> ignore = new ArrayList<>();
for (E e : _data) {
upsert.values(Stream.of(getProperties())
.map(property -> getInsertColumnValue(e, property, (prop, val) -> ignore.add(prop)))
.toArray());
}
upsert.ignoreUpdate(ignore.toArray(new String[0]));
return upsert.execute();
},
SaveResultOperator.class,
table,
MappingEventTypes.save_before,
MappingEventTypes.save_after,
getDefaultContextKeyValue(instance(_data),
type("batch"),
tableMetadata(table),
upsert(upsert))
);
}

Expand All @@ -191,23 +191,23 @@ protected InsertResultOperator doInsert(E data) {
InsertOperator insert = operator.dml().insert(table);

return EventResultOperator.create(
() -> {
for (Map.Entry<String, String> entry : mapping.getColumnPropertyMapping().entrySet()) {
String column = entry.getKey();
String property = entry.getValue();
insert.value(column, getInsertColumnValue(data, property));
}
return insert.execute();
},
InsertResultOperator.class,
table,
MappingEventTypes.insert_before,
MappingEventTypes.insert_after,
getDefaultContextKeyValue(
instance(data),
type("single"),
tableMetadata(table),
insert(insert))
() -> {
for (Map.Entry<String, String> entry : mapping.getColumnPropertyMapping().entrySet()) {
String column = entry.getKey();
String property = entry.getValue();
insert.value(column, getInsertColumnValue(data, property));
}
return insert.execute();
},
InsertResultOperator.class,
table,
MappingEventTypes.insert_before,
MappingEventTypes.insert_after,
getDefaultContextKeyValue(
instance(data),
type("single"),
tableMetadata(table),
insert(insert))
);

}
Expand Down Expand Up @@ -241,25 +241,25 @@ protected InsertResultOperator doInsert(Collection<E> batch) {
InsertOperator insert = operator.dml().insert(table);

return EventResultOperator.create(
() -> {
insert.columns(getProperties());
() -> {
insert.columns(getProperties());

for (E e : _data) {
insert.values(Stream.of(getProperties())
.map(property -> getInsertColumnValue(e, property))
.toArray());
}
return insert.execute();
},
InsertResultOperator.class,
table,
MappingEventTypes.insert_before,
MappingEventTypes.insert_after,
getDefaultContextKeyValue(
instance(_data),
type("batch"),
tableMetadata(table),
insert(insert))
for (E e : _data) {
insert.values(Stream.of(getProperties())
.map(property -> getInsertColumnValue(e, property))
.toArray());
}
return insert.execute();
},
InsertResultOperator.class,
table,
MappingEventTypes.insert_before,
MappingEventTypes.insert_after,
getDefaultContextKeyValue(
instance(_data),
type("batch"),
tableMetadata(table),
insert(insert))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.metadata.TableOrViewMetadata;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
Expand Down Expand Up @@ -44,10 +45,14 @@ public Optional<RDBColumnMetadata> getColumnByName(String columnName) {
@Override
public Map<String, String> getColumnPropertyMapping() {
return metadata
.get()
.getColumns()
.stream()
.collect(Collectors.toMap(RDBColumnMetadata::getName, RDBColumnMetadata::getAlias));
.get()
.getColumns()
.stream()
.collect(Collectors.toMap(
RDBColumnMetadata::getName,
RDBColumnMetadata::getAlias,
(a, b) -> b,
LinkedHashMap::new));
}

@Override
Expand Down

0 comments on commit ef9faed

Please sign in to comment.