Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加Extensible接口,可实现此接口来拓展字段. #81

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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> extensions();

/**
* 获取扩展属性
*
* @param property 属性名
* @return 属性值
*/
default Object getExtension(String property) {
Map<String, Object> ext = extensions();
return ext == null ? null : ext.get(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 @@ -4,6 +4,8 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;

public class Containers {

public static GenericContainer<?> newMysql(String version) {
Expand Down Expand Up @@ -32,7 +34,7 @@ public static GenericContainer<?> newOracle() {
return new GenericContainer<>(DockerImageName.parse("iatebes/oracle_11g"))
.withEnv("TZ", "Asia/Shanghai")
.withExposedPorts(1521)
.waitingFor(Wait.forLogMessage(".*opened.*", 1));
.waitingFor(Wait.forLogMessage(".*opened.*", 1).withStartupTimeout(Duration.ofMinutes(2)));
}

public static GenericContainer<?> newMSSQL() {
Expand All @@ -42,7 +44,7 @@ public static GenericContainer<?> newMSSQL() {
.withEnv("ACCEPT_EULA","y")
.withEnv("MSSQL_PID","Enterprise")
.withExposedPorts(1433)
.waitingFor(Wait.forLogMessage(".*Service Broker manager has started.*",1));
.waitingFor(Wait.forLogMessage(".*Service Broker manager has started.*",1).withStartupTimeout(Duration.ofMinutes(2)));
}

}
Loading
Loading