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

支持包装类型的 JDBC 字段 #282

Merged
merged 3 commits into from
Jan 27, 2025
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
9 changes: 6 additions & 3 deletions today-beans/src/main/java/infra/beans/FieldBeanProperty.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,8 +37,11 @@ public final class FieldBeanProperty extends BeanProperty {
@Serial
private static final long serialVersionUID = 1L;

private final boolean writeable;

FieldBeanProperty(Field field) {
super(field);
this.writeable = !Modifier.isFinal(field.getModifiers());
}

@Override
Expand Down Expand Up @@ -68,7 +71,7 @@ public boolean isSynthetic() {

@Override
public boolean isReadOnly() {
return Modifier.isFinal(field.getModifiers());
return !writeable;
}

@Override
Expand All @@ -78,7 +81,7 @@ public boolean isReadable() {

@Override
public boolean isWriteable() {
return !isReadOnly();
return writeable;
}

@Override
Expand Down
54 changes: 32 additions & 22 deletions today-jdbc/src/main/java/infra/jdbc/ObjectPropertySetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -23,6 +23,7 @@
import infra.beans.BeanProperty;
import infra.core.conversion.ConversionException;
import infra.core.conversion.ConversionService;
import infra.jdbc.type.SmartTypeHandler;
import infra.jdbc.type.TypeHandler;
import infra.lang.Assert;
import infra.lang.Nullable;
Expand All @@ -34,30 +35,28 @@
* @see #setTo(Object, ResultSet, int)
* @since 2021/1/7 22:49
*/

public class ObjectPropertySetter {

@Nullable
private final PropertyPath propertyPath;

private final BeanProperty beanProperty; // cache

private final TypeHandler<?> typeHandler;

private final ConversionService conversionService;

@Nullable
private final PrimitiveTypeNullHandler primitiveTypeNullHandler;

public ObjectPropertySetter(
@Nullable PropertyPath propertyPath, BeanProperty beanProperty, RepositoryManager manager) {
this(propertyPath, beanProperty,
manager.getConversionService(),
manager.getTypeHandler(beanProperty),
manager.getPrimitiveTypeNullHandler());
public ObjectPropertySetter(@Nullable PropertyPath propertyPath, BeanProperty beanProperty, RepositoryManager manager) {
this(propertyPath, beanProperty, manager.getConversionService(),
manager.getTypeHandler(beanProperty), manager.getPrimitiveTypeNullHandler());
}

public ObjectPropertySetter(
@Nullable PropertyPath propertyPath, BeanProperty beanProperty,
ConversionService conversionService, TypeHandler<?> typeHandler,
@Nullable PrimitiveTypeNullHandler primitiveTypeNullHandler) {
public ObjectPropertySetter(@Nullable PropertyPath propertyPath, BeanProperty beanProperty,
ConversionService conversionService, TypeHandler<?> typeHandler, @Nullable PrimitiveTypeNullHandler primitiveTypeNullHandler) {
Assert.notNull(typeHandler, "TypeHandler is required");
Assert.notNull(beanProperty, "BeanProperty is required");
Assert.notNull(conversionService, "ConversionService is required");
Expand All @@ -76,23 +75,34 @@ public ObjectPropertySetter(
* @param columnIndex current column index
* @throws SQLException when data fetch failed
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setTo(Object obj, ResultSet resultSet, int columnIndex) throws SQLException {
Object result = getResult(resultSet, columnIndex);
if (result == null && beanProperty.isPrimitive()) {
if (primitiveTypeNullHandler != null) {
if (beanProperty.isWriteable()) {
Object result = getResult(resultSet, columnIndex);
if (result == null && beanProperty.isPrimitive()) {
if (primitiveTypeNullHandler != null) {
if (propertyPath != null) {
obj = propertyPath.getNestedObject(obj);
}
primitiveTypeNullHandler.handleNull(beanProperty, obj);
}
}
else {
if (propertyPath != null) {
obj = propertyPath.getNestedObject(obj);
propertyPath.set(obj, result);
}
else {
beanProperty.setValue(obj, result);
}
primitiveTypeNullHandler.handleNull(beanProperty, obj);
}
}
else if (typeHandler instanceof SmartTypeHandler handler) {
Object value = beanProperty.getValue(obj);
handler.applyResult(value, resultSet, columnIndex);
}
else {
if (propertyPath != null) {
propertyPath.set(obj, result);
}
else {
beanProperty.setValue(obj, result);
}
throw new IllegalStateException("Entity property %s.%s is not writable"
.formatted(beanProperty.getDeclaringClass(), beanProperty.getName()));
}
}

Expand Down
3 changes: 2 additions & 1 deletion today-jdbc/src/main/java/infra/jdbc/ResultSetIterator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -374,6 +374,7 @@ protected RuntimeException handleReadError(SQLException ex) {
return new PersistenceException("Database read error: " + ex.getMessage(), ex);
}

@Nullable
protected abstract T readNext(ResultSet resultSet) throws SQLException;

static final class ResultSetValue<T> {
Expand Down
6 changes: 3 additions & 3 deletions today-jdbc/src/main/java/infra/jdbc/type/AnyTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -39,8 +39,8 @@ public AnyTypeHandler(Class<T> type) {
}

@Override
public void setParameter(PreparedStatement ps, int parameterIndex, @Nullable T parameter) throws SQLException {
ps.setObject(parameterIndex, parameter);
public void setParameter(PreparedStatement ps, int parameterIndex, @Nullable T arg) throws SQLException {
ps.setObject(parameterIndex, arg);
}

@Nullable
Expand Down
12 changes: 6 additions & 6 deletions today-jdbc/src/main/java/infra/jdbc/type/BaseTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -40,21 +40,21 @@
public abstract class BaseTypeHandler<T> implements TypeHandler<T> {

@Override
public void setParameter(PreparedStatement ps, int parameterIndex, @Nullable T parameter) throws SQLException {
if (parameter == null) {
public void setParameter(PreparedStatement ps, int parameterIndex, @Nullable T arg) throws SQLException {
if (arg == null) {
setNullParameter(ps, parameterIndex);
}
else {
setNonNullParameter(ps, parameterIndex, parameter);
setNonNullParameter(ps, parameterIndex, arg);
}
}

public void setNullParameter(PreparedStatement ps, int parameterIndex) throws SQLException {
ps.setObject(parameterIndex, null);
}

public void setNonNullParameter(PreparedStatement ps, int parameterIndex, T parameter) throws SQLException {
ps.setObject(parameterIndex, parameter);
public void setNonNullParameter(PreparedStatement ps, int parameterIndex, T arg) throws SQLException {
ps.setObject(parameterIndex, arg);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,8 +31,8 @@
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter) throws SQLException {
ps.setBigDecimal(i, parameter);
public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal arg) throws SQLException {
ps.setBigDecimal(i, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,8 +32,8 @@
public class BigIntegerTypeHandler extends BaseTypeHandler<BigInteger> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter) throws SQLException {
ps.setBigDecimal(i, new BigDecimal(parameter));
public void setNonNullParameter(PreparedStatement ps, int i, BigInteger arg) throws SQLException {
ps.setBigDecimal(i, new BigDecimal(arg));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,8 +30,8 @@
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter) throws SQLException {
ps.setBoolean(i, parameter);
public void setNonNullParameter(PreparedStatement ps, int i, Boolean arg) throws SQLException {
ps.setBoolean(i, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,8 +30,8 @@
public class ByteArrayTypeHandler extends BaseTypeHandler<byte[]> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter) throws SQLException {
ps.setBytes(i, parameter);
public void setNonNullParameter(PreparedStatement ps, int i, byte[] arg) throws SQLException {
ps.setBytes(i, arg);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions today-jdbc/src/main/java/infra/jdbc/type/ByteTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,8 +30,8 @@
public class ByteTypeHandler extends BaseTypeHandler<Byte> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Byte parameter) throws SQLException {
ps.setByte(i, parameter);
public void setNonNullParameter(PreparedStatement ps, int i, Byte arg) throws SQLException {
ps.setByte(i, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,8 +31,8 @@
public class BytesInputStreamTypeHandler extends BaseTypeHandler<InputStream> {

@Override
public void setNonNullParameter(PreparedStatement ps, int parameterIndex, InputStream parameter) throws SQLException {
ps.setBinaryStream(parameterIndex, parameter);
public void setNonNullParameter(PreparedStatement ps, int parameterIndex, InputStream arg) throws SQLException {
ps.setBinaryStream(parameterIndex, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,8 +32,8 @@
public class CharacterTypeHandler extends BaseTypeHandler<Character> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Character parameter) throws SQLException {
ps.setString(i, parameter.toString());
public void setNonNullParameter(PreparedStatement ps, int i, Character arg) throws SQLException {
ps.setString(i, arg.toString());
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions today-jdbc/src/main/java/infra/jdbc/type/DateTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,8 +32,8 @@
public class DateTypeHandler extends BaseTypeHandler<Date> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter) throws SQLException {
ps.setTimestamp(i, new Timestamp(parameter.getTime()));
public void setNonNullParameter(PreparedStatement ps, int i, Date arg) throws SQLException {
ps.setTimestamp(i, new Timestamp(arg.getTime()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,8 +30,8 @@
public class DoubleTypeHandler extends BaseTypeHandler<Double> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Double parameter) throws SQLException {
ps.setDouble(i, parameter);
public void setNonNullParameter(PreparedStatement ps, int i, Double arg) throws SQLException {
ps.setDouble(i, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,8 +34,8 @@
public class DurationTypeHandler extends BaseTypeHandler<Duration> {

@Override
public void setNonNullParameter(PreparedStatement ps, int parameterIndex, Duration parameter) throws SQLException {
ps.setLong(parameterIndex, parameter.toNanos());
public void setNonNullParameter(PreparedStatement ps, int parameterIndex, Duration arg) throws SQLException {
ps.setLong(parameterIndex, arg.toNanos());
}

@Nullable
Expand Down
Loading
Loading