Skip to content

Commit 97331f7

Browse files
committed
Replaces java.sql.Types constants with java.sql.SQLType values.
java.sql.Types constants are int values and therefore make it tedious to read and debug the code. SQLType values are mostly enum constants which are much nicer to use. Original pull request #1142
1 parent a132d43 commit 97331f7

File tree

11 files changed

+157
-81
lines changed

11 files changed

+157
-81
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BasicJdbcConverter.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.sql.JDBCType;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22+
import java.sql.SQLType;
2223
import java.util.Map;
2324
import java.util.Optional;
2425

@@ -171,10 +172,15 @@ private Class<?> getReferenceColumnType(RelationalPersistentProperty property) {
171172
* @see org.springframework.data.jdbc.core.convert.JdbcConverter#getSqlType(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)
172173
*/
173174
@Override
175+
public SQLType getTargetSqlType(RelationalPersistentProperty property) {
176+
return JdbcUtil.targetSqlTypeFor(getColumnType(property));
177+
}
178+
179+
@Override
180+
@Deprecated
174181
public int getSqlType(RelationalPersistentProperty property) {
175182
return JdbcUtil.sqlTypeFor(getColumnType(property));
176183
}
177-
178184
/*
179185
* (non-Javadoc)
180186
* @see org.springframework.data.jdbc.core.convert.JdbcConverter#getColumnType(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)
@@ -275,12 +281,19 @@ private boolean canWriteAsJdbcValue(@Nullable Object value) {
275281
return customWriteTarget.isPresent() && customWriteTarget.get().isAssignableFrom(JdbcValue.class);
276282
}
277283

278-
/*
284+
@Override
285+
@Deprecated
286+
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int sqlType) {
287+
return writeJdbcValue(value, columnType, JdbcUtil.jdbcTypeFor(sqlType));
288+
}
289+
290+
291+
/*
279292
* (non-Javadoc)
280293
* @see org.springframework.data.jdbc.core.convert.JdbcConverter#writeValue(java.lang.Object, java.lang.Class, int)
281294
*/
282295
@Override
283-
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int sqlType) {
296+
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, SQLType sqlType) {
284297

285298
JdbcValue jdbcValue = tryToConvertToJdbcValue(value);
286299
if (jdbcValue != null) {
@@ -290,7 +303,8 @@ public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int
290303
Object convertedValue = writeValue(value, ClassTypeInformation.from(columnType));
291304

292305
if (convertedValue == null || !convertedValue.getClass().isArray()) {
293-
return JdbcValue.of(convertedValue, JdbcUtil.jdbcTypeFor(sqlType));
306+
307+
return JdbcValue.of(convertedValue, sqlType);
294308
}
295309

296310
Class<?> componentType = convertedValue.getClass().getComponentType();

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;
1919

20-
import java.sql.JDBCType;
2120
import java.sql.ResultSet;
21+
import java.sql.SQLType;
2222
import java.util.ArrayList;
2323
import java.util.Collections;
2424
import java.util.HashSet;
@@ -546,17 +546,17 @@ private IdentifierProcessing getIdentifierProcessing() {
546546
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource,
547547
RelationalPersistentProperty property, @Nullable Object value, SqlIdentifier name) {
548548

549-
addConvertedValue(parameterSource, value, name, converter.getColumnType(property), converter.getSqlType(property));
549+
addConvertedValue(parameterSource, value, name, converter.getColumnType(property), converter.getTargetSqlType(property));
550550
}
551551

552552
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource, SqlIdentifier name, Object value,
553553
Class<?> javaType) {
554554

555-
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.sqlTypeFor(javaType));
555+
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.targetSqlTypeFor(javaType));
556556
}
557557

558558
private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nullable Object value,
559-
SqlIdentifier paramName, Class<?> javaType, int sqlType) {
559+
SqlIdentifier paramName, Class<?> javaType, SQLType sqlType) {
560560

561561
JdbcValue jdbcValue = converter.writeJdbcValue( //
562562
value, //
@@ -567,7 +567,7 @@ private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nu
567567
parameterSource.addValue( //
568568
paramName, //
569569
jdbcValue.getValue(), //
570-
JdbcUtil.sqlTypeFor(jdbcValue.getJdbcType()));
570+
jdbcValue.getJdbcType().getVendorTypeNumber());
571571
}
572572

573573
private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource parameterSource,
@@ -578,15 +578,15 @@ private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource param
578578
for (Object id : values) {
579579

580580
Class<?> columnType = converter.getColumnType(property);
581-
int sqlType = converter.getSqlType(property);
581+
SQLType sqlType = converter.getTargetSqlType(property);
582582

583583
jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
584584
convertedIds.add(jdbcValue.getValue());
585585
}
586586

587587
Assert.state(jdbcValue != null, "JdbcValue must be not null at this point. Please report this as a bug.");
588588

589-
JDBCType jdbcType = jdbcValue.getJdbcType();
589+
SQLType jdbcType = jdbcValue.getJdbcType();
590590
int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();
591591

592592
parameterSource.addValue(paramName, convertedIds, typeNumber);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultJdbcTypeFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.jdbc.core.convert;
1717

1818
import java.sql.Array;
19-
import java.sql.JDBCType;
19+
import java.sql.SQLType;
2020

2121
import org.springframework.data.jdbc.core.dialect.JdbcArrayColumns;
2222
import org.springframework.data.jdbc.support.JdbcUtil;
@@ -68,7 +68,7 @@ public Array createArray(Object[] value) {
6868

6969
Class<?> componentType = arrayColumns.getArrayType(value.getClass());
7070

71-
JDBCType jdbcType = JdbcUtil.jdbcTypeFor(componentType);
71+
SQLType jdbcType = JdbcUtil.targetSqlTypeFor(componentType);
7272
Assert.notNull(jdbcType, () -> String.format("Couldn't determine JDBCType for %s", componentType));
7373
String typeName = arrayColumns.getArrayTypeName(jdbcType);
7474

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcConverter.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jdbc.core.convert;
1717

1818
import java.sql.ResultSet;
19+
import java.sql.SQLType;
1920

2021
import org.springframework.data.jdbc.core.mapping.JdbcValue;
2122
import org.springframework.data.relational.core.conversion.RelationalConverter;
@@ -39,12 +40,24 @@ public interface JdbcConverter extends RelationalConverter {
3940
* to JDBC parameters.
4041
*
4142
* @param value a value as it is used in the object model. May be {@code null}.
42-
* @param type {@link TypeInformation} into which the value is to be converted. Must not be {@code null}.
43+
* @param type {@literal Class} into which the value is to be converted. Must not be {@code null}.
4344
* @param sqlType the type constant from {@link java.sql.Types} to be used if non is specified by a converter.
4445
* @return The converted value wrapped in a {@link JdbcValue}. Guaranteed to be not {@literal null}.
4546
*/
4647
JdbcValue writeJdbcValue(@Nullable Object value, Class<?> type, int sqlType);
4748

49+
/**
50+
* Convert a property value into a {@link JdbcValue} that contains the converted value and information how to bind it
51+
* to JDBC parameters.
52+
*
53+
* @param value a value as it is used in the object model. May be {@code null}.
54+
* @param type {@literal Class} into which the value is to be converted. Must not be {@code null}.
55+
* @param sqlType the {@link SQLType} to be used if non is specified by a converter.
56+
* @return The converted value wrapped in a {@link JdbcValue}. Guaranteed to be not {@literal null}.
57+
* @since 2.4
58+
*/
59+
JdbcValue writeJdbcValue(@Nullable Object value, Class<?> type, SQLType sqlType);
60+
4861
/**
4962
* Read the current row from {@link ResultSet} to an {@link RelationalPersistentEntity#getType() entity}.
5063
*
@@ -73,7 +86,7 @@ public interface JdbcConverter extends RelationalConverter {
7386
* top-level array type (e.g. {@code String[][]} returns {@code String[]}).
7487
*
7588
* @return a {@link Class} that is suitable for usage with JDBC drivers.
76-
* @see org.springframework.data.jdbc.support.JdbcUtil#sqlTypeFor(Class)
89+
* @see org.springframework.data.jdbc.support.JdbcUtil#targetSqlTypeFor(Class)
7790
* @since 2.0
7891
*/
7992
Class<?> getColumnType(RelationalPersistentProperty property);
@@ -85,5 +98,9 @@ public interface JdbcConverter extends RelationalConverter {
8598
* @see java.sql.Types
8699
* @since 2.0
87100
*/
101+
SQLType getTargetSqlType(RelationalPersistentProperty property);
102+
103+
@Deprecated
88104
int getSqlType(RelationalPersistentProperty property);
105+
89106
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcValue.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jdbc.core.convert;
1717

1818
import java.sql.JDBCType;
19+
import java.sql.SQLType;
1920
import java.util.Objects;
2021

2122
import org.springframework.lang.Nullable;
@@ -32,11 +33,11 @@
3233
@Deprecated
3334
public final class JdbcValue extends org.springframework.data.jdbc.core.mapping.JdbcValue {
3435

35-
private JdbcValue(@Nullable Object value, @Nullable JDBCType jdbcType) {
36+
private JdbcValue(@Nullable Object value, @Nullable SQLType jdbcType) {
3637
super(value, jdbcType);
3738
}
3839

39-
public static JdbcValue of(@Nullable Object value, @Nullable JDBCType jdbcType) {
40+
public static JdbcValue of(@Nullable Object value, @Nullable SQLType jdbcType) {
4041
return new JdbcValue(value, jdbcType);
4142
}
4243
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/JdbcValue.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jdbc.core.mapping;
1717

1818
import java.sql.JDBCType;
19+
import java.sql.SQLType;
1920
import java.util.Objects;
2021

2122
import org.springframework.lang.Nullable;
@@ -31,15 +32,15 @@
3132
public class JdbcValue {
3233

3334
private final Object value;
34-
private final JDBCType jdbcType;
35+
private final SQLType jdbcType;
3536

36-
protected JdbcValue(@Nullable Object value, @Nullable JDBCType jdbcType) {
37+
protected JdbcValue(@Nullable Object value, @Nullable SQLType jdbcType) {
3738

3839
this.value = value;
3940
this.jdbcType = jdbcType;
4041
}
4142

42-
public static JdbcValue of(@Nullable Object value, @Nullable JDBCType jdbcType) {
43+
public static JdbcValue of(@Nullable Object value, @Nullable SQLType jdbcType) {
4344
return new JdbcValue(value, jdbcType);
4445
}
4546

@@ -49,7 +50,7 @@ public Object getValue() {
4950
}
5051

5152
@Nullable
52-
public JDBCType getJdbcType() {
53+
public SQLType getJdbcType() {
5354
return this.jdbcType;
5455
}
5556

0 commit comments

Comments
 (0)