From e43439c6c336888917fd99c937e8abe7f4ae41ce Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 9 Jan 2018 22:19:46 +0100 Subject: [PATCH] SqlParameterSource interface exposes parameter names Includes default implementations of getSqlType and getTypeName. Issue: SPR-16361 --- .../BeanPropertySqlParameterSource.java | 36 +++++++++++-------- .../namedparam/EmptySqlParameterSource.java | 8 ++++- .../namedparam/MapSqlParameterSource.java | 9 ++++- .../core/namedparam/SqlParameterSource.java | 24 +++++++++++-- .../namedparam/SqlParameterSourceUtils.java | 13 +++---- 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.java index bd345c9e71..931d597a8d 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,26 @@ public class BeanPropertySqlParameterSource extends AbstractSqlParameterSource { } } + /** + * Derives a default SQL type from the corresponding property type. + * @see org.springframework.jdbc.core.StatementCreatorUtils#javaTypeToSqlParameterType + */ + @Override + public int getSqlType(String paramName) { + int sqlType = super.getSqlType(paramName); + if (sqlType != TYPE_UNKNOWN) { + return sqlType; + } + Class propType = this.beanWrapper.getPropertyType(paramName); + return StatementCreatorUtils.javaTypeToSqlParameterType(propType); + } + + @Override + @Nullable + public String[] getParameterNames() { + return getReadablePropertyNames(); + } + /** * Provide access to the property names of the wrapped bean. * Uses support provided in the {@link PropertyAccessor} interface. @@ -92,18 +112,4 @@ public class BeanPropertySqlParameterSource extends AbstractSqlParameterSource { return this.propertyNames; } - /** - * Derives a default SQL type from the corresponding property type. - * @see org.springframework.jdbc.core.StatementCreatorUtils#javaTypeToSqlParameterType - */ - @Override - public int getSqlType(String paramName) { - int sqlType = super.getSqlType(paramName); - if (sqlType != TYPE_UNKNOWN) { - return sqlType; - } - Class propType = this.beanWrapper.getPropertyType(paramName); - return StatementCreatorUtils.javaTypeToSqlParameterType(propType); - } - } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/EmptySqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/EmptySqlParameterSource.java index 7a25fd0d3c..febb952d16 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/EmptySqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/EmptySqlParameterSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,4 +54,10 @@ public class EmptySqlParameterSource implements SqlParameterSource { return null; } + @Override + @Nullable + public String[] getParameterNames() { + return null; + } + } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java index 29300163f7..9d7133862e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/MapSqlParameterSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import java.util.Map; import org.springframework.jdbc.core.SqlParameterValue; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * {@link SqlParameterSource} implementation that holds a given Map of parameters. @@ -163,4 +164,10 @@ public class MapSqlParameterSource extends AbstractSqlParameterSource { return this.values.get(paramName); } + @Override + @Nullable + public String[] getParameterNames() { + return StringUtils.toStringArray(this.values.keySet()); + } + } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSource.java index 21e6bc57a9..41b6c7fa7a 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,7 +73,9 @@ public interface SqlParameterSource { * or {@code TYPE_UNKNOWN} if not known * @see #TYPE_UNKNOWN */ - int getSqlType(String paramName); + default int getSqlType(String paramName) { + return TYPE_UNKNOWN; + } /** * Determine the type name for the specified named parameter. @@ -82,6 +84,22 @@ public interface SqlParameterSource { * or {@code null} if not known */ @Nullable - String getTypeName(String paramName); + default String getTypeName(String paramName) { + return null; + } + + /** + * Extract all available parameter names if possible. + *

This is an optional operation, primarily for use with + * {@link org.springframework.jdbc.core.simple.SimpleJdbcInsert} + * and {@link org.springframework.jdbc.core.simple.SimpleJdbcCall}. + * @return the array of parameter names, or {@code null} if not determinable + * @since 5.0.3 + * @see SqlParameterSourceUtils#extractCaseInsensitiveParameterNames + */ + @Nullable + default String[] getParameterNames() { + return null; + } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java index 0d4c6dbe97..af7c6dca94 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -117,14 +117,9 @@ public abstract class SqlParameterSourceUtils { */ public static Map extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) { Map caseInsensitiveParameterNames = new HashMap<>(); - if (parameterSource instanceof BeanPropertySqlParameterSource) { - String[] propertyNames = ((BeanPropertySqlParameterSource) parameterSource).getReadablePropertyNames(); - for (String name : propertyNames) { - caseInsensitiveParameterNames.put(name.toLowerCase(), name); - } - } - else if (parameterSource instanceof MapSqlParameterSource) { - for (String name : ((MapSqlParameterSource) parameterSource).getValues().keySet()) { + String[] paramNames = parameterSource.getParameterNames(); + if (paramNames != null) { + for (String name : paramNames) { caseInsensitiveParameterNames.put(name.toLowerCase(), name); } }