Skip to content

Commit 73a6b33

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 14c5e3f commit 73a6b33

File tree

10 files changed

+156
-77
lines changed

10 files changed

+156
-77
lines changed

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

+20-2
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

@@ -163,10 +164,15 @@ private Class<?> getReferenceColumnType(RelationalPersistentProperty property) {
163164
}
164165

165166
@Override
167+
public SQLType getTargetSqlType(RelationalPersistentProperty property) {
168+
return JdbcUtil.targetSqlTypeFor(getColumnType(property));
169+
}
170+
171+
@Override
172+
@Deprecated
166173
public int getSqlType(RelationalPersistentProperty property) {
167174
return JdbcUtil.sqlTypeFor(getColumnType(property));
168175
}
169-
170176
@Override
171177
public Class<?> getColumnType(RelationalPersistentProperty property) {
172178
return doGetColumnType(property);
@@ -256,7 +262,18 @@ private boolean canWriteAsJdbcValue(@Nullable Object value) {
256262
}
257263

258264
@Override
265+
@Deprecated
259266
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int sqlType) {
267+
return writeJdbcValue(value, columnType, JdbcUtil.jdbcTypeFor(sqlType));
268+
}
269+
270+
271+
/*
272+
* (non-Javadoc)
273+
* @see org.springframework.data.jdbc.core.convert.JdbcConverter#writeValue(java.lang.Object, java.lang.Class, int)
274+
*/
275+
@Override
276+
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, SQLType sqlType) {
260277

261278
JdbcValue jdbcValue = tryToConvertToJdbcValue(value);
262279
if (jdbcValue != null) {
@@ -266,7 +283,8 @@ public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, int
266283
Object convertedValue = writeValue(value, ClassTypeInformation.from(columnType));
267284

268285
if (convertedValue == null || !convertedValue.getClass().isArray()) {
269-
return JdbcValue.of(convertedValue, JdbcUtil.jdbcTypeFor(sqlType));
286+
287+
return JdbcValue.of(convertedValue, sqlType);
270288
}
271289

272290
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;
@@ -474,17 +474,17 @@ private IdentifierProcessing getIdentifierProcessing() {
474474
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource,
475475
RelationalPersistentProperty property, @Nullable Object value, SqlIdentifier name) {
476476

477-
addConvertedValue(parameterSource, value, name, converter.getColumnType(property), converter.getSqlType(property));
477+
addConvertedValue(parameterSource, value, name, converter.getColumnType(property), converter.getTargetSqlType(property));
478478
}
479479

480480
private void addConvertedPropertyValue(SqlIdentifierParameterSource parameterSource, SqlIdentifier name, Object value,
481481
Class<?> javaType) {
482482

483-
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.sqlTypeFor(javaType));
483+
addConvertedValue(parameterSource, value, name, javaType, JdbcUtil.targetSqlTypeFor(javaType));
484484
}
485485

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

489489
JdbcValue jdbcValue = converter.writeJdbcValue( //
490490
value, //
@@ -495,7 +495,7 @@ private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nu
495495
parameterSource.addValue( //
496496
paramName, //
497497
jdbcValue.getValue(), //
498-
JdbcUtil.sqlTypeFor(jdbcValue.getJdbcType()));
498+
jdbcValue.getJdbcType().getVendorTypeNumber());
499499
}
500500

501501
private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource parameterSource,
@@ -506,15 +506,15 @@ private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource param
506506
for (Object id : values) {
507507

508508
Class<?> columnType = converter.getColumnType(property);
509-
int sqlType = converter.getSqlType(property);
509+
SQLType sqlType = converter.getTargetSqlType(property);
510510

511511
jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
512512
convertedIds.add(jdbcValue.getValue());
513513
}
514514

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

517-
JDBCType jdbcType = jdbcValue.getJdbcType();
517+
SQLType jdbcType = jdbcValue.getJdbcType();
518518
int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();
519519

520520
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.support.JdbcUtil;
2222
import org.springframework.jdbc.core.ConnectionCallback;
@@ -67,7 +67,7 @@ public Array createArray(Object[] value) {
6767

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

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

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/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)