Apply "instanceof pattern matching" in remainder of spring-jdbc module

See gh-30067
This commit is contained in:
Sam Brannen 2023-03-06 15:18:05 +01:00
parent 8175a3bf09
commit 7f1b81ff53
3 changed files with 35 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -293,11 +293,11 @@ public abstract class StatementCreatorUtils {
private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, private static void setValue(PreparedStatement ps, int paramIndex, int sqlType,
@Nullable String typeName, @Nullable Integer scale, Object inValue) throws SQLException { @Nullable String typeName, @Nullable Integer scale, Object inValue) throws SQLException {
if (inValue instanceof SqlTypeValue) { if (inValue instanceof SqlTypeValue sqlTypeValue) {
((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName); sqlTypeValue.setTypeValue(ps, paramIndex, sqlType, typeName);
} }
else if (inValue instanceof SqlValue) { else if (inValue instanceof SqlValue sqlValue) {
((SqlValue) inValue).setValue(ps, paramIndex); sqlValue.setValue(ps, paramIndex);
} }
else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR ) { else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR ) {
ps.setString(paramIndex, inValue.toString()); ps.setString(paramIndex, inValue.toString());
@ -328,8 +328,8 @@ public abstract class StatementCreatorUtils {
} }
} }
else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) { else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
if (inValue instanceof BigDecimal) { if (inValue instanceof BigDecimal bigDecimal) {
ps.setBigDecimal(paramIndex, (BigDecimal) inValue); ps.setBigDecimal(paramIndex, bigDecimal);
} }
else if (scale != null) { else if (scale != null) {
ps.setObject(paramIndex, inValue, sqlType, scale); ps.setObject(paramIndex, inValue, sqlType, scale);
@ -339,20 +339,20 @@ public abstract class StatementCreatorUtils {
} }
} }
else if (sqlType == Types.BOOLEAN) { else if (sqlType == Types.BOOLEAN) {
if (inValue instanceof Boolean) { if (inValue instanceof Boolean flag) {
ps.setBoolean(paramIndex, (Boolean) inValue); ps.setBoolean(paramIndex, flag);
} }
else { else {
ps.setObject(paramIndex, inValue, Types.BOOLEAN); ps.setObject(paramIndex, inValue, Types.BOOLEAN);
} }
} }
else if (sqlType == Types.DATE) { else if (sqlType == Types.DATE) {
if (inValue instanceof java.util.Date) { if (inValue instanceof java.util.Date date) {
if (inValue instanceof java.sql.Date) { if (inValue instanceof java.sql.Date sqlDate) {
ps.setDate(paramIndex, (java.sql.Date) inValue); ps.setDate(paramIndex, sqlDate);
} }
else { else {
ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime())); ps.setDate(paramIndex, new java.sql.Date(date.getTime()));
} }
} }
else if (inValue instanceof Calendar cal) { else if (inValue instanceof Calendar cal) {
@ -363,12 +363,12 @@ public abstract class StatementCreatorUtils {
} }
} }
else if (sqlType == Types.TIME) { else if (sqlType == Types.TIME) {
if (inValue instanceof java.util.Date) { if (inValue instanceof java.util.Date date) {
if (inValue instanceof java.sql.Time) { if (inValue instanceof java.sql.Time time) {
ps.setTime(paramIndex, (java.sql.Time) inValue); ps.setTime(paramIndex, time);
} }
else { else {
ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime())); ps.setTime(paramIndex, new java.sql.Time(date.getTime()));
} }
} }
else if (inValue instanceof Calendar cal) { else if (inValue instanceof Calendar cal) {
@ -379,12 +379,12 @@ public abstract class StatementCreatorUtils {
} }
} }
else if (sqlType == Types.TIMESTAMP) { else if (sqlType == Types.TIMESTAMP) {
if (inValue instanceof java.util.Date) { if (inValue instanceof java.util.Date date) {
if (inValue instanceof java.sql.Timestamp) { if (inValue instanceof java.sql.Timestamp timestamp) {
ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue); ps.setTimestamp(paramIndex, timestamp);
} }
else { else {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); ps.setTimestamp(paramIndex, new java.sql.Timestamp(date.getTime()));
} }
} }
else if (inValue instanceof Calendar cal) { else if (inValue instanceof Calendar cal) {
@ -460,15 +460,15 @@ public abstract class StatementCreatorUtils {
if (paramValues != null) { if (paramValues != null) {
for (Object inValue : paramValues) { for (Object inValue : paramValues) {
// Unwrap SqlParameterValue first... // Unwrap SqlParameterValue first...
if (inValue instanceof SqlParameterValue) { if (inValue instanceof SqlParameterValue sqlParameterValue) {
inValue = ((SqlParameterValue) inValue).getValue(); inValue = sqlParameterValue.getValue();
} }
// Check for disposable value types // Check for disposable value types
if (inValue instanceof SqlValue) { if (inValue instanceof SqlValue sqlValue) {
((SqlValue) inValue).cleanup(); sqlValue.cleanup();
} }
else if (inValue instanceof DisposableSqlTypeValue) { else if (inValue instanceof DisposableSqlTypeValue disposableSqlTypeValue) {
((DisposableSqlTypeValue) inValue).cleanup(); disposableSqlTypeValue.cleanup();
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -87,8 +87,8 @@ public class MapSqlParameterSource extends AbstractSqlParameterSource {
public MapSqlParameterSource addValue(String paramName, @Nullable Object value) { public MapSqlParameterSource addValue(String paramName, @Nullable Object value) {
Assert.notNull(paramName, "Parameter name must not be null"); Assert.notNull(paramName, "Parameter name must not be null");
this.values.put(paramName, value); this.values.put(paramName, value);
if (value instanceof SqlParameterValue) { if (value instanceof SqlParameterValue sqlParameterValue) {
registerSqlType(paramName, ((SqlParameterValue) value).getSqlType()); registerSqlType(paramName, sqlParameterValue.getSqlType());
} }
return this; return this;
} }
@ -135,8 +135,8 @@ public class MapSqlParameterSource extends AbstractSqlParameterSource {
if (values != null) { if (values != null) {
values.forEach((key, value) -> { values.forEach((key, value) -> {
this.values.put(key, value); this.values.put(key, value);
if (value instanceof SqlParameterValue) { if (value instanceof SqlParameterValue sqlParameterValue) {
registerSqlType(key, ((SqlParameterValue) value).getSqlType()); registerSqlType(key, sqlParameterValue.getSqlType());
} }
}); });
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,12 +59,12 @@ public abstract class SqlParameterSourceUtils {
* @see BeanPropertySqlParameterSource * @see BeanPropertySqlParameterSource
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]) * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[])
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
public static SqlParameterSource[] createBatch(Collection<?> candidates) { public static SqlParameterSource[] createBatch(Collection<?> candidates) {
SqlParameterSource[] batch = new SqlParameterSource[candidates.size()]; SqlParameterSource[] batch = new SqlParameterSource[candidates.size()];
int i = 0; int i = 0;
for (Object candidate : candidates) { for (Object candidate : candidates) {
batch[i] = (candidate instanceof Map ? new MapSqlParameterSource((Map<String, ?>) candidate) : batch[i] = (candidate instanceof Map map ? new MapSqlParameterSource(map) :
new BeanPropertySqlParameterSource(candidate)); new BeanPropertySqlParameterSource(candidate));
i++; i++;
} }