Skip to content

Commit

Permalink
refactor: 优化jpa解析逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jul 15, 2024
1 parent ef9faed commit eda71c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import lombok.Setter;
import lombok.SneakyThrows;
import org.hswebframework.ezorm.rdb.mapping.annotation.Comment;
import org.hswebframework.ezorm.rdb.mapping.parser.*;
import org.hswebframework.ezorm.rdb.metadata.RDBDatabaseMetadata;
Expand All @@ -12,6 +13,8 @@

import javax.persistence.Table;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Supplier;

/**
* @see javax.persistence.Column
Expand All @@ -20,8 +23,15 @@
*/
public class JpaEntityTableMetadataParser implements EntityTableMetadataParser {

@Setter
private RDBDatabaseMetadata databaseMetadata;
private Callable<RDBDatabaseMetadata> databaseMetadata;

public void setDatabaseMetadata(Callable<RDBDatabaseMetadata> databaseMetadata) {
this.databaseMetadata = databaseMetadata;
}

public void setDatabaseMetadata(RDBDatabaseMetadata databaseMetadata) {
this.databaseMetadata = () -> databaseMetadata;
}

@Setter
private DataTypeResolver dataTypeResolver = DefaultDataTypeResolver.INSTANCE;
Expand All @@ -30,12 +40,14 @@ public class JpaEntityTableMetadataParser implements EntityTableMetadataParser {
private ValueCodecResolver valueCodecResolver = DefaultValueCodecResolver.COMMONS;


@SneakyThrows
public Optional<RDBTableMetadata> parseTableMetadata(Class<?> entityType) {

Table table = AnnotationUtils.getAnnotation(entityType, Table.class);
if (table == null) {
return Optional.empty();
}
RDBDatabaseMetadata databaseMetadata = this.databaseMetadata.call();
RDBSchemaMetadata schema = databaseMetadata
.getSchema(table.schema())
.orElseGet(databaseMetadata::getCurrentSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ public void process() {
Set<Annotation> annotations = getAnnotations(entityType, descriptor);

getAnnotation(annotations, Column.class)
.ifPresent(column -> handleColumnAnnotation(descriptor, annotations, ColumnInfo.of(column)));
.ifPresent(column -> handleColumnAnnotation(descriptor, annotations, ColumnInfo.of(column)));

getAnnotation(annotations, JoinColumns.class)
.ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column.value())));
.ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column.value())));

getAnnotation(annotations, JoinColumn.class)
.ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column)));
.ifPresent(column -> afterRun.add(() -> handleJoinColumnAnnotation(descriptor, annotations, column)));

}

Expand Down Expand Up @@ -170,13 +170,13 @@ private void handleJoinColumnAnnotation(PropertyDescriptor descriptor, Set<Annot
builder.setTarget(joinTableName);

getAnnotation(annotations, OneToOne.class)
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToOne));
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToOne));
getAnnotation(annotations, OneToMany.class)
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToMay));
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.oneToMay));
getAnnotation(annotations, ManyToMany.class)
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToMay));
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToMay));
getAnnotation(annotations, ManyToOne.class)
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToOne));
.ifPresent(oneToOne -> builder.setAssociationType(AssociationType.manyToOne));


for (JoinColumn joinColumn : column) {
Expand Down Expand Up @@ -256,8 +256,8 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
}
String columnName;
Field field = AnnotationUtils
.getFiledByDescriptor(entityType, descriptor)
.orElse(null);
.getFiledByDescriptor(entityType, descriptor)
.orElse(null);
if (null == field) {
return;
}
Expand Down Expand Up @@ -290,38 +290,38 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
metadata.setColumnDefinition(column.columnDefinition);
}
getAnnotation(annotations, GeneratedValue.class)
.map(gen -> {
if (gen.strategy() == GenerationType.SEQUENCE) {
metadata.setAutoIncrement(true);
metadata.setProperty("seq_name", gen.generator());
return null;
} else {
DefaultValueGenerator<RDBColumnMetadata> generator = LazyDefaultValueGenerator
.of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator())));
return generator.generate(metadata);
}
})
.ifPresent(metadata::setDefaultValue);
.map(gen -> {
if (gen.strategy() == GenerationType.SEQUENCE) {
metadata.setAutoIncrement(true);
metadata.setProperty("seq_name", gen.generator());
return null;
} else {
DefaultValueGenerator<RDBColumnMetadata> generator = LazyDefaultValueGenerator
.of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator())));
return generator.generate(metadata);
}
})
.ifPresent(metadata::setDefaultValue);

getAnnotation(annotations, DefaultValue.class)
.map(gen -> {
if (gen.value().isEmpty()) {
return LazyDefaultValueGenerator
.of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator())))
.generate(metadata);
}
return (RuntimeDefaultValue) gen::value;
})
.ifPresent(metadata::setDefaultValue);
.map(gen -> {
if (gen.value().isEmpty()) {
return LazyDefaultValueGenerator
.of(() -> tableMetadata.findFeatureNow(DefaultValueGenerator.createId(gen.generator())))
.generate(metadata);
}
return (RuntimeDefaultValue) gen::value;
})
.ifPresent(metadata::setDefaultValue);


getAnnotation(annotations, Comment.class)
.map(Comment::value)
.ifPresent(metadata::setComment);
.map(Comment::value)
.ifPresent(metadata::setComment);

getAnnotation(annotations, Upsert.class)
.map(Upsert::insertOnly)
.ifPresent(insertOnly -> metadata.setSaveable(!insertOnly));
.map(Upsert::insertOnly)
.ifPresent(insertOnly -> metadata.setSaveable(!insertOnly));

getAnnotation(annotations, Id.class).ifPresent(id -> metadata.setPrimaryKey(true));

Expand All @@ -330,24 +330,24 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
metadata.addFeature(propertyDescriptor);

ofNullable(dataTypeResolver)
.map(resolver -> resolver.resolve(propertyDescriptor))
.ifPresent(metadata::setType);
.map(resolver -> resolver.resolve(propertyDescriptor))
.ifPresent(metadata::setType);

if (metadata.getType() == null) {
tableMetadata
.getDialect()
.convertSqlType(metadata.getJavaType())
.ifPresent(jdbcType -> metadata.setJdbcType(jdbcType, metadata.getJavaType()));
.getDialect()
.convertSqlType(metadata.getJavaType())
.ifPresent(jdbcType -> metadata.setJdbcType(jdbcType, metadata.getJavaType()));
}

ofNullable(valueCodecResolver)
.map(resolver -> resolver.resolve(propertyDescriptor)
.orElseGet(() -> metadata
.findFeature(ValueCodecFactory.ID)
.flatMap(factory -> factory.createValueCodec(metadata))
.orElse(null)))
.ifPresent(metadata::setValueCodec);
;
.map(resolver -> resolver
.resolve(propertyDescriptor)
.orElseGet(() -> metadata
.findFeature(ValueCodecFactory.ID)
.flatMap(factory -> factory.createValueCodec(metadata))
.orElse(null)))
.ifPresent(metadata::setValueCodec);
if (metadata.getValueCodec() instanceof EnumValueCodec) {
EnumValueCodec codec = ((EnumValueCodec) metadata.getValueCodec());
if (codec.isToMask()) {
Expand All @@ -357,7 +357,9 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
}
customColumn(descriptor, field, metadata, annotations);

if (metadata.getType().sqlTypeIsNumber()) {
if (metadata.getType() == null) {
log.warn("can not resolve column type for {}.{}", entityType.getName(), descriptor.getName());
} else if (metadata.getType().sqlTypeIsNumber()) {
metadata.setLength(metadata.getPrecision());
}

Expand Down

0 comments on commit eda71c9

Please sign in to comment.