Skip to content

Commit

Permalink
refactor: 优化数据类型处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 19, 2023
1 parent 2c9fadb commit 0a2c7e6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
}
}
customColumn(descriptor, field, metadata, annotations);

if(metadata.getType().isNumber() || !metadata.getType().isLengthSupport()){
metadata.setLength(metadata.getPrecision());
}

tableMetadata.addColumn(metadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,20 @@ public String getQuoteName() {
return getDialect().quote(getName());
}

public void setJdbcType(SQLType jdbcType, Class javaType) {
public void setJdbcType(SQLType jdbcType, Class<?> javaType) {
this.javaType = javaType;
setType(JdbcDataType.of(jdbcType, javaType));
}

public int getPrecision(int defaultPrecision) {
return precision <= 0 ? defaultPrecision : precision;
if (precision <= 0) {
if (length <= 0) {
return defaultPrecision;
} else {
return length;
}
}
return precision;
}

public void setType(DataType dataType) {
Expand All @@ -138,7 +145,7 @@ public void setType(DataType dataType) {
}

@Override
public Class getJavaType() {
public Class<?> getJavaType() {
if (javaType == null && type != null) {
return javaType = type.getJavaType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class DefaultDialect implements Dialect {

protected Map<String, DataType> dataTypeMapping = new HashMap<>();

protected Map<Class, JDBCType> classJDBCTypeMapping = new HashMap<>();
protected Map<Class<?>, JDBCType> classJDBCTypeMapping = new HashMap<>();

protected void registerDataType(String symbol, DataType dataType) {
dataTypeMapping.put(symbol, dataType instanceof DataTypeBuilder ? dataType : DataType.builder(dataType, meta -> symbol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Slf4j
public abstract class RDBTableMetadataParser implements TableMetadataParser {

static ContextView logContext = Context.of(Logger.class,log).readOnly();
static ContextView logContext = Context.of(Logger.class, log).readOnly();

protected RDBSchemaMetadata schema;

Expand Down Expand Up @@ -327,6 +327,11 @@ protected void applyColumnInfo(RDBColumnMetadata column, Record record) {
.map(String::toLowerCase)
.map(getDialect()::convertDataType)
.ifPresent(column::setType);
if (column.getType() != null && column.getType().isNumber()) {
if (!record.get("data_precision").isPresent()) {
column.setPrecision(column.getLength());
}
}

column.findFeature(ValueCodecFactory.ID)
.flatMap(factory -> factory.createValueCodec(column))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public SqlServerDialect() {
addDataTypeBuilder(JDBCType.BIGINT, (meta) -> "bigint");
addDataTypeBuilder(JDBCType.OTHER, (meta) -> "other");
addDataTypeBuilder(JDBCType.REAL, (meta) -> "real");
addDataTypeBuilder(JDBCType.BOOLEAN, (meta) -> "bit");
addDataTypeBuilder(JDBCType.BIT, (meta) -> "bit");

registerDataType("longnvarchar", DataType.builder(JdbcDataType.of(JDBCType.LONGNVARCHAR, String.class), c -> "text"));
registerDataType("longvarchar", DataType.builder(JdbcDataType.of(JDBCType.LONGVARCHAR, String.class), c -> "text"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public OracleDialect() {
addDataTypeBuilder(JDBCType.TIMESTAMP, (meta) -> "timestamp");
addDataTypeBuilder(JDBCType.TIME, (meta) -> "timestamp");
addDataTypeBuilder(JDBCType.DATE, (meta) -> "date");
addDataTypeBuilder(JDBCType.TINYINT, (meta) -> "number(2)");
addDataTypeBuilder(JDBCType.SMALLINT, (meta) -> "number(5)");
addDataTypeBuilder(JDBCType.INTEGER, (meta) -> "number(20)");
addDataTypeBuilder(JDBCType.TINYINT, (meta) -> "number(" + meta.getPrecision(2) + ")");
addDataTypeBuilder(JDBCType.SMALLINT, (meta) ->"number(" + meta.getPrecision(5) + ")");
addDataTypeBuilder(JDBCType.INTEGER, (meta) -> "number(" + meta.getPrecision(20) + ")");
addDataTypeBuilder(JDBCType.DOUBLE, (meta) -> "binary_double");
addDataTypeBuilder(JDBCType.CLOB, (meta) -> "clob");
addDataTypeBuilder(JDBCType.LONGNVARCHAR, (meta) -> "clob");
Expand All @@ -32,9 +32,12 @@ public OracleDialect() {
addDataTypeBuilder(JDBCType.BIGINT, (meta) -> "number(38,0)");
addDataTypeBuilder(JDBCType.OTHER, (meta) -> "other");
addDataTypeBuilder(JDBCType.BOOLEAN, (meta) -> "number(1)");
addDataTypeBuilder(JDBCType.TINYINT, (meta) -> "number(1)");
addDataTypeBuilder(JDBCType.LONGVARCHAR, (meta) -> "clob");
addDataTypeBuilder(JDBCType.LONGVARBINARY, (meta) -> "blob");

classJDBCTypeMapping.put(Boolean.class,JDBCType.TINYINT);

registerDataType("number", DataType
.builder(DataType.jdbc(JDBCType.NUMERIC, BigDecimal.class),
column -> StringUtils.concat("number(", column.getPrecision(38), ",", column.getScale(), ")")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public PostgresqlDialect() {
addDataTypeBuilder(JDBCType.LONGVARBINARY, (meta) -> "bytea");
addDataTypeBuilder(JDBCType.LONGVARCHAR, (meta) -> "text");
addDataTypeBuilder(JDBCType.BLOB, (meta) -> "bytea");
addDataTypeBuilder(JDBCType.BIGINT, (meta) -> "int8");
addDataTypeBuilder(JDBCType.DOUBLE, (meta) -> "float8");
addDataTypeBuilder(JDBCType.FLOAT, (meta) -> "float4");
addDataTypeBuilder(JDBCType.INTEGER, (meta) -> "int4");
addDataTypeBuilder(JDBCType.NUMERIC, (meta) -> StringUtils.concat("numeric(", meta.getPrecision(32), ",", meta.getScale(), ")"));
addDataTypeBuilder(JDBCType.DECIMAL, (meta) -> StringUtils.concat("decimal(", meta.getPrecision(32), ",", meta.getScale(), ")"));
Expand All @@ -52,6 +52,7 @@ public PostgresqlDialect() {
registerDataType("int2", JdbcDataType.of(JDBCType.SMALLINT, Byte.class));
registerDataType("int", JdbcDataType.of(JDBCType.INTEGER, Integer.class));
registerDataType("float8", JdbcDataType.of(JDBCType.DOUBLE, Double.class));
registerDataType("float4", JdbcDataType.of(JDBCType.FLOAT, Float.class));
registerDataType("money", JdbcDataType.of(JDBCType.DECIMAL, BigDecimal.class));
registerDataType("bool", JdbcDataType.of(JDBCType.BOOLEAN, Boolean.class));
registerDataType("character", JdbcDataType.of(JDBCType.VARCHAR, String.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;

public class AnnotationUtils {
Expand Down Expand Up @@ -42,8 +39,8 @@ public static Optional<Field> getFiledByDescriptor(Class<?> type, PropertyDescri
}

public static Set<Annotation> getAnnotations(Class<?> entityClass, PropertyDescriptor descriptor) {
Set<Annotation> annotations = new HashSet<>();
Set<Class<? extends Annotation>> types = new HashSet<>();
Set<Annotation> annotations = new LinkedHashSet<>();
Set<Class<? extends Annotation>> types = new LinkedHashSet<>();

Consumer<Annotation[]> annoConsumer = (ann) -> {
for (Annotation annotation : ann) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ public void testReactivePager() {
}

@Test
public void testInsertMerge(){
public void testInsertMerge() {

BasicTestEntity first= BasicTestEntity
BasicTestEntity first = BasicTestEntity
.builder()
.id("test_merge")
.balance(1000L)
Expand All @@ -207,7 +207,7 @@ public void testInsertMerge(){
.stateEnum(StateEnum.enabled)
.build();

BasicTestEntity second= BasicTestEntity
BasicTestEntity second = BasicTestEntity
.builder()
.id("test_merge")
.balance(1000L)
Expand All @@ -219,15 +219,15 @@ public void testInsertMerge(){
.build();

repository
.insert(Flux.just(first,second))
.insert(Flux.just(first, second))
.as(StepVerifier::create)
.expectNext(1)
.verifyComplete();

repository
.createQuery()
.where(BasicTestEntity::getId,first.getId())
.select("id","name")
.where(BasicTestEntity::getId, first.getId())
.select("id", "name")
.fetch()
.map(BasicTestEntity::getName)
.as(StepVerifier::create)
Expand All @@ -248,6 +248,7 @@ public void testInsertDuplicate() {
.tags(Arrays.asList("a", "b", "c", "d"))
.state((byte) 1)
.stateEnum(StateEnum.enabled)
.enabled(true)
.build())
.collectList()
.as(repository::insertBatch)
Expand Down Expand Up @@ -309,6 +310,7 @@ public void testReactiveRepositorySave() {
.state((byte) 1)
.addressId("test")
.stateEnum(StateEnum.enabled)
.enabled(true)
.build();

repository.save(Mono.just(entity))
Expand Down Expand Up @@ -355,15 +357,18 @@ public void testReactiveRepositorySave() {

@Test
public void testRepositoryCurd() {
BasicTestEntity entity = BasicTestEntity.builder()
.id("test_id")
.balance(1000L)
.name("test")
.tags(new ArrayList<>(Arrays.asList("a", "b", "c", "d")))
.createTime(new Date())
.state((byte) 1)
.addressId("test")
.build();
BasicTestEntity entity = BasicTestEntity
.builder()
.id("test_id")
.balance(1000L)
.name("test")
.tags(new ArrayList<>(Arrays.asList("a", "b", "c", "d")))
.createTime(new Date())
.state((byte) 1)
.addressId("test")
.enabled(true)
.floatVal(1.2F)
.build();

addressRepository.insert(Mono.just(Record.newRecord().putValue("id", "test").putValue("name", "test_address")))
.as(StepVerifier::create)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class BasicTestEntity implements Serializable {
@Column
private Double doubleVal;

@Column
private Float floatVal;


@Column(scale = 1)
private BigDecimal bigDecimal;

Expand Down Expand Up @@ -71,6 +75,10 @@ public class BasicTestEntity implements Serializable {
@Column(name = "address_id")
private String addressId;

@Column(name = "bool_type")
private Boolean enabled;


@JoinColumn(name = "address_id")
private Address address;
}

0 comments on commit 0a2c7e6

Please sign in to comment.