Skip to content

Commit

Permalink
feat: 增加Extensible接口,可实现此接口来拓展字段.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 20, 2024
1 parent 3bd056c commit 9aa29a9
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.hswebframework.ezorm.core;

import com.fasterxml.jackson.annotation.JsonAnySetter;

import java.util.Map;

/**
* 可扩展的对象,用于动态拓展实体字段属性
*
* @author zhouhao
* @since 4.1.3
*/
public interface Extensible {

/**
* 获取所有扩展属性
* @return 扩展属性
*/
@JsonAnySetter
Map<String, Object> getExtensions();

/**
* 获取扩展属性
* @param property 属性名
* @return 属性值
*/
Object getExtension(String property);

/**
* 设置扩展属性
* @param property 属性名
* @param value 属性值
*/
@JsonAnySetter
void setExtension(String property, Object value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.core.ObjectPropertyOperator;
import org.hswebframework.ezorm.core.RuntimeDefaultValue;
import org.hswebframework.ezorm.rdb.events.ContextKey;
import org.hswebframework.ezorm.rdb.events.ContextKeyValue;
import org.hswebframework.ezorm.rdb.events.ContextKeys;
import org.hswebframework.ezorm.rdb.executor.NullValue;
import org.hswebframework.ezorm.rdb.executor.wrapper.ResultWrapper;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.EntityPropertyDescriptor;
import org.hswebframework.ezorm.rdb.mapping.LazyEntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.MappingFeatureType;
import org.hswebframework.ezorm.rdb.mapping.events.EventResultOperator;
Expand All @@ -22,18 +20,12 @@
import org.hswebframework.ezorm.rdb.operator.builder.fragments.NativeSql;
import org.hswebframework.ezorm.rdb.operator.dml.insert.InsertOperator;
import org.hswebframework.ezorm.rdb.operator.dml.insert.InsertResultOperator;
import org.hswebframework.ezorm.rdb.operator.dml.upsert.SaveOrUpdateOperator;
import org.hswebframework.ezorm.rdb.operator.dml.upsert.SaveResultOperator;
import org.hswebframework.ezorm.rdb.operator.dml.upsert.UpsertOperator;
import reactor.core.publisher.Mono;
import org.hswebframework.ezorm.rdb.utils.PropertyUtils;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -138,24 +130,27 @@ protected Collection<E> tryMergeDuplicate(Collection<E> data) {
}

protected E merge(E older, E newer) {
ObjectPropertyOperator opt = GlobalConfig.getPropertyOperator();
for (String property : getProperties()) {
Object newerVal = opt.getProperty(newer, property).orElse(null);

Object newerVal = getProperty(newer, property);
if (newerVal != null) {
continue;
}
opt.getProperty(older, property)
.ifPresent(olderValue -> opt.setProperty(newer, property, olderValue));

newerVal = getProperty(older, property);
if (newerVal != null) {
setProperty(newer, property, newerVal);
}
}
return newer;
}

private Object getProperty(E data, String property) {
return GlobalConfig
.getPropertyOperator()
.getProperty(data, property)
.orElse(null);
void setProperty(E entity, String property, Object value) {
PropertyUtils.setProperty(entity, property, value, mapping);
}

Object getProperty(E entity, String property) {
return PropertyUtils.getProperty(entity, property, mapping).orElse(null);
}

protected SaveResultOperator doSave(Collection<E> data) {
Expand Down Expand Up @@ -213,7 +208,7 @@ protected InsertResultOperator doInsert(E data) {
}

private Object getInsertColumnValue(E data, String property, BiConsumer<String, Object> whenDefaultValue) {
Object value = GlobalConfig.getPropertyOperator().getProperty(data, property).orElse(null);
Object value = getProperty(data, property);
if (value == null) {
value = mapping.getColumnByProperty(property)
.flatMap(RDBColumnMetadata::generateDefaultValue)
Expand All @@ -222,7 +217,7 @@ private Object getInsertColumnValue(E data, String property, BiConsumer<String,
whenDefaultValue.accept(property, value);
//回填
if (!(value instanceof NativeSql)) {
GlobalConfig.getPropertyOperator().setProperty(data, property, value);
setProperty(data, property, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package org.hswebframework.ezorm.rdb.mapping.defaults;

import org.hswebframework.ezorm.core.NestConditional;
import org.hswebframework.ezorm.core.ObjectPropertyOperator;
import org.hswebframework.ezorm.core.SimpleNestConditional;
import org.hswebframework.ezorm.core.*;
import org.hswebframework.ezorm.core.param.Param;
import org.hswebframework.ezorm.core.param.QueryParam;
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.core.param.UpdateParam;
import org.hswebframework.ezorm.rdb.events.ContextKeyValue;
import org.hswebframework.ezorm.rdb.executor.NullValue;
import org.hswebframework.ezorm.rdb.mapping.DSLUpdate;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.MappingFeatureType;
import org.hswebframework.ezorm.rdb.mapping.events.EventResultOperator;
import org.hswebframework.ezorm.rdb.mapping.events.MappingContextKeys;
import org.hswebframework.ezorm.rdb.mapping.events.MappingEventTypes;
Expand All @@ -21,7 +17,7 @@
import org.hswebframework.ezorm.rdb.metadata.RDBTableMetadata;
import org.hswebframework.ezorm.rdb.operator.dml.update.UpdateOperator;
import org.hswebframework.ezorm.rdb.operator.dml.update.UpdateResultOperator;
import org.hswebframework.ezorm.rdb.operator.dml.upsert.SaveResultOperator;
import org.hswebframework.ezorm.rdb.utils.PropertyUtils;

import java.sql.JDBCType;
import java.util.*;
Expand Down Expand Up @@ -115,12 +111,12 @@ private void applyColumns(E instance) {
.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())
.forEach(e -> PropertyUtils
.getProperty(instance, e.getValue(), mapping)
.ifPresent(val -> this.set(e.getKey(), val)));
}


@Override
public ME includes(String... properties) {
includes.addAll(Arrays.asList(properties));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.MappingFeatureType;
import org.hswebframework.ezorm.rdb.metadata.RDBTableMetadata;
import org.hswebframework.ezorm.rdb.utils.PropertyUtils;

import java.util.Optional;
import java.util.function.Supplier;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void wrapColumn(ColumnWrapperContext<E> context) {
.map(columnMetadata -> columnMetadata.decode(context.getResult()))
.orElseGet(context::getResult);
if (value != null) {
GlobalConfig.getPropertyOperator().setProperty(context.getRowInstance(), property, value);
PropertyUtils.setProperty(context.getRowInstance(), property, value,mapping);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.hswebframework.ezorm.rdb.mapping.wrapper;

import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.rdb.executor.wrapper.ColumnWrapperContext;
import org.hswebframework.ezorm.rdb.executor.wrapper.ResultWrapper;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.EntityPropertyDescriptor;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.metadata.key.ForeignKeyMetadata;

public class NestedEntityResultWrapper<E> implements ResultWrapper<E, E> {
Expand Down Expand Up @@ -37,18 +40,31 @@ public void wrapColumn(ColumnWrapperContext<E> context) {
}
ForeignKeyMetadata metadata = mapping.getTable().getForeignKey(table).orElse(null);
if (null != metadata) {
Object value = metadata
.getTarget()
.getColumn(column)
.map(m -> m.decode(context.getResult()))
.orElseGet(context::getResult);
GlobalConfig.getPropertyOperator().setProperty(nestInstance, column, value);
RDBColumnMetadata col = metadata.getTarget().getColumn(column).orElse(null);

Object val = col == null ? context.getResult() : col.decode(context.getResult());

setProperty(col, context.getRowInstance(), label, val);
}
} else {
GlobalConfig.getPropertyOperator().setProperty(context.getRowInstance(), label, context.getResult());
setProperty(mapping.getColumnByProperty(label).orElse(null),
context.getRowInstance(),
label,
context.getResult());
}
}

protected void setProperty(RDBColumnMetadata col, E instance, String label, Object val) {
if (instance instanceof Extensible && (col == null || !col
.getFeature(EntityPropertyDescriptor.ID)
.isPresent())) {
((Extensible) instance).setExtension(label, val);
} else {
GlobalConfig.getPropertyOperator().setProperty(instance, label, val);
}

}

@Override
public boolean completedWrapRow(E result) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ public BuildParameterQueryOperator(String from) {

@Override
public QueryOperator select(Collection<String> columns) {

columns.stream()
.map(SelectColumn::of)
.forEach(parameter.getSelect()::add);
for (String column : columns) {
parameter.getSelect().add(SelectColumn.of(column));
}
return this;
}

@Override
public QueryOperator select(String... columns) {
return select(Arrays.asList(columns));
for (String column : columns) {
parameter.getSelect().add(SelectColumn.of(column));
}
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.hswebframework.ezorm.rdb.utils;

import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.core.ObjectPropertyOperator;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.EntityPropertyDescriptor;

import java.util.Optional;

public class PropertyUtils {

public static Optional<Object> getProperty(Object entity, String property, EntityColumnMapping mapping) {
ObjectPropertyOperator opt = GlobalConfig.getPropertyOperator();
if (entity instanceof Extensible && isExtensibleColumn(property, mapping)) {
return Optional.ofNullable(((Extensible) entity).getExtension(property));
}
return opt.getProperty(entity, property);
}

public static boolean isExtensibleColumn(String property, EntityColumnMapping mapping) {
return mapping
.getColumnByProperty(property)
.map(c -> !c.getFeature(EntityPropertyDescriptor.ID).isPresent())
.orElse(true);
}

public static void setProperty(Object entity, String property, Object value, EntityColumnMapping mapping) {
if (entity instanceof Extensible && isExtensibleColumn(property, mapping)) {
((Extensible) entity).setExtension(property, value);
} else {
GlobalConfig.getPropertyOperator().setProperty(entity, property, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,15 @@ public void init() {

operator.ddl()
.createOrAlter(table)
.addColumn("ext_name")
.varchar(32)
.commit()
.commit()
.reactive()
.block();
table.<EntityColumnMapping>getFeatureNow(MappingFeatureType.columnPropertyMapping.createFeatureId(BasicTestEntity.class))
.reload();

EntityResultWrapper<BasicTestEntity> wrapper = new EntityResultWrapper<>(BasicTestEntity::new);
wrapper.setMapping(table
.<EntityColumnMapping>getFeature(MappingFeatureType.columnPropertyMapping.createFeatureId(BasicTestEntity.class))
Expand All @@ -144,6 +150,7 @@ public void init() {
repository = new DefaultReactiveRepository<>(operator, table, BasicTestEntity.class, wrapper);
addressRepository = operator.dml().createReactiveRepository("test_address");


}

@Test
Expand Down Expand Up @@ -268,6 +275,8 @@ public void testInsertMerge() {
.stateEnum(StateEnum.enabled)
.build();

second.setExtension("ext_name", "test");

repository
.insert(Flux.just(first, second))
.as(StepVerifier::create)
Expand All @@ -277,8 +286,9 @@ public void testInsertMerge() {
repository
.createQuery()
.where(BasicTestEntity::getId, first.getId())
.select("id", "name")
.select("id", "name","ext_name")
.fetch()
.doOnNext(System.out::println)
.map(BasicTestEntity::getName)
.as(StepVerifier::create)
.expectNext(second.getName())
Expand Down Expand Up @@ -578,7 +588,7 @@ public void testEnums() {
}

@Test
public void testUpdateByNative(){
public void testUpdateByNative() {
BasicTestEntity entity = BasicTestEntity
.builder()
.id("testUpdateByNative")
Expand All @@ -593,13 +603,13 @@ public void testUpdateByNative(){
.verifyComplete();

repository.createUpdate()
.set(BasicTestEntity::getState, NativeSql.of("state + 1"))
.where(entity::getId)
.lte(BasicTestEntity::getState,NativeSql.of("state + 1"))
.execute()
.as(StepVerifier::create)
.expectNext(1)
.verifyComplete();
.set(BasicTestEntity::getState, NativeSql.of("state + 1"))
.where(entity::getId)
.lte(BasicTestEntity::getState, NativeSql.of("state + 1"))
.execute()
.as(StepVerifier::create)
.expectNext(1)
.verifyComplete();


}
Expand Down
Loading

0 comments on commit 9aa29a9

Please sign in to comment.