From ccb312a9742bdec8a38def1c9bb2fb0e12941da3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 10 Mar 2010 12:54:52 +0000 Subject: [PATCH] SimpleJdbcCall's "returningResultSet" accepts any plain RowMapper now (SPR-6963) --- .../core/metadata/CallMetaDataContext.java | 7 +++--- .../jdbc/core/simple/AbstractJdbcCall.java | 25 +++++++++++++------ .../jdbc/core/simple/SimpleJdbcCall.java | 14 ++++++++--- .../core/simple/SimpleJdbcCallOperations.java | 22 ++++++++++++---- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java index a0cbe44850..73c785303e 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -24,7 +24,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; - import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -34,8 +33,8 @@ import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlOutParameter; import org.springframework.jdbc.core.SqlParameter; -import org.springframework.jdbc.core.SqlReturnResultSet; import org.springframework.jdbc.core.SqlParameterValue; +import org.springframework.jdbc.core.SqlReturnResultSet; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; import org.springframework.jdbc.support.JdbcUtils; @@ -540,7 +539,7 @@ public class CallMetaDataContext { return matchedParameters; } - public Map matchInParameterValuesWithCallParameters(Object[] parameterValues) { + public Map matchInParameterValuesWithCallParameters(Object[] parameterValues) { Map matchedParameters = new HashMap(parameterValues.length); int i = 0; for (SqlParameter parameter : this.callParameters) { diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java index cbf8990199..0e15b09714 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -30,6 +30,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.core.CallableStatementCreatorFactory; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.core.metadata.CallMetaDataContext; import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -55,7 +56,7 @@ public abstract class AbstractJdbcCall { private final List declaredParameters = new ArrayList(); /** List of RefCursor/ResultSet RowMapper objects */ - private final Map declaredRowMappers = new LinkedHashMap(); + private final Map declaredRowMappers = new LinkedHashMap(); /** * Has this operation been compiled? Compilation means at @@ -203,7 +204,8 @@ public abstract class AbstractJdbcCall { */ public void addDeclaredParameter(SqlParameter parameter) { if (!StringUtils.hasText(parameter.getName())) { - throw new InvalidDataAccessApiUsageException("You must specify a parameter name when declaring parameters for \"" + getProcedureName() + "\""); + throw new InvalidDataAccessApiUsageException( + "You must specify a parameter name when declaring parameters for \"" + getProcedureName() + "\""); } this.declaredParameters.add(parameter); if (logger.isDebugEnabled()) { @@ -212,17 +214,26 @@ public abstract class AbstractJdbcCall { } /** - * Add a {@link org.springframework.jdbc.core.RowMapper} for the specified parameter or column + * Add a {@link org.springframework.jdbc.core.RowMapper} for the specified parameter or column. * @param parameterName name of parameter or column * @param rowMapper the RowMapper implementation to use */ - public void addDeclaredRowMapper(String parameterName, ParameterizedRowMapper rowMapper) { + public void addDeclaredRowMapper(String parameterName, RowMapper rowMapper) { this.declaredRowMappers.put(parameterName, rowMapper); if (logger.isDebugEnabled()) { logger.debug("Added row mapper for [" + getProcedureName() + "]: " + parameterName); } } + /** + * Add a {@link org.springframework.jdbc.core.RowMapper} for the specified parameter or column. + * @deprecated in favor of {@link #addDeclaredRowMapper(String, org.springframework.jdbc.core.RowMapper)} + */ + @Deprecated + public void addDeclaredRowMapper(String parameterName, ParameterizedRowMapper rowMapper) { + addDeclaredRowMapper(parameterName, (RowMapper) rowMapper); + } + /** * Get the call string that should be used based on parameters and meta data */ @@ -279,7 +290,7 @@ public abstract class AbstractJdbcCall { this.callMetaDataContext.initializeMetaData(getJdbcTemplate().getDataSource()); // iterate over the declared RowMappers and register the corresponding SqlParameter - for (Map.Entry entry : this.declaredRowMappers.entrySet()) { + for (Map.Entry entry : this.declaredRowMappers.entrySet()) { SqlParameter resultSetParameter = this.callMetaDataContext.createReturnResultSetParameter(entry.getKey(), entry.getValue()); this.declaredParameters.add(resultSetParameter); @@ -402,7 +413,7 @@ public abstract class AbstractJdbcCall { * @param args the parameter values provided as an array * @return Map with parameter names and values */ - private Map matchInParameterValuesWithCallParameters(Object[] args) { + private Map matchInParameterValuesWithCallParameters(Object[] args) { return this.callMetaDataContext.matchInParameterValuesWithCallParameters(args); } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCall.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCall.java index 770606f365..0311dc4cf3 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCall.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCall.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -17,13 +17,12 @@ package org.springframework.jdbc.core.simple; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; - import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -121,6 +120,15 @@ public class SimpleJdbcCall extends AbstractJdbcCall implements SimpleJdbcCallOp return this; } + public SimpleJdbcCall returningResultSet(String parameterName, RowMapper rowMapper) { + addDeclaredRowMapper(parameterName, rowMapper); + return this; + } + + /** + * @deprecated in favor of {@link #returningResultSet(String, org.springframework.jdbc.core.RowMapper)} + */ + @Deprecated public SimpleJdbcCall returningResultSet(String parameterName, ParameterizedRowMapper rowMapper) { addDeclaredRowMapper(parameterName, rowMapper); return this; diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCallOperations.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCallOperations.java index 5bb794a830..6c2d52d659 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCallOperations.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCallOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -18,6 +18,7 @@ package org.springframework.jdbc.core.simple; import java.util.Map; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -74,7 +75,6 @@ public interface SimpleJdbcCallOperations { * will be used to provide input values. This is different from the StoredProcedure class * which for backwards compatibility reasons allows input values to be provided for parameters declared * as SqlOutParameter. - * * @param sqlParameters the parameters to use * @return the instance of this SimpleJdbcCall */ @@ -85,13 +85,24 @@ public interface SimpleJdbcCallOperations { /** * Used to specify when a ResultSet is returned by the stored procedure and you want it mapped - * by a RowMapper. The results will be returned using the parameter name specified. Multiple + * by a RowMapper. The results will be returned using the parameter name specified. Multiple * ResultSets must be declared in the correct order. If the database you are using uses ref cursors * then the name specified must match the name of the parameter declared for the procedure in the * database. * @param parameterName the name of the returned results and/or the name of the ref cursor parameter * @param rowMapper the RowMapper implementation that will map the data returned for each row * */ + SimpleJdbcCallOperations returningResultSet(String parameterName, RowMapper rowMapper); + + /** + * Used to specify when a ResultSet is returned by the stored procedure and you want it mapped + * by a RowMapper. The results will be returned using the parameter name specified. Multiple + * ResultSets must be declared in the correct order. If the database you are using uses ref cursors + * then the name specified must match the name of the parameter declared for the procedure in the + * database. + * @deprecated in favor of {@link #returningResultSet(String, org.springframework.jdbc.core.RowMapper)} + */ + @Deprecated SimpleJdbcCallOperations returningResultSet(String parameterName, ParameterizedRowMapper rowMapper); /** @@ -104,8 +115,9 @@ public interface SimpleJdbcCallOperations { /** * Execute the stored function and return the results obtained as an Object of the specified return type. * @param returnType the type of the value to return - * @param args optional array containing the in parameter values to be used in the call. Parameter values must - * be provided in the same order as the parameters are defined for the stored procedure. + * @param args optional array containing the in parameter values to be used in the call. + * Parameter values must be provided in the same order as the parameters are defined + * for the stored procedure. */ T executeFunction(Class returnType, Object... args);