SqlParameterSource interface exposes parameter names

Includes default implementations of getSqlType and getTypeName.

Issue: SPR-16361
This commit is contained in:
Juergen Hoeller
2018-01-09 22:19:46 +01:00
parent b2322e58d9
commit e43439c6c3
5 changed files with 61 additions and 29 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -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.
* <p>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;
}
}

View File

@@ -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<String, String> extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) {
Map<String, String> 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);
}
}