Skip to content

Commit fbd0be2

Browse files
author
Thomas Risberg
committed
added convenience execute method that takes vararg of objects in the order of the parameters plus the corresponding executeFunction/executeObject methods (SPR-4739)
1 parent 7c05312 commit fbd0be2

File tree

5 files changed

+268
-193
lines changed

5 files changed

+268
-193
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataContext.java

+12
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
540540
return matchedParameters;
541541
}
542542

543+
public Map<String,?> matchInParameterValuesWithCallParameters(Object[] parameterValues) {
544+
Map<String, Object> matchedParameters = new HashMap<String, Object>(parameterValues.length);
545+
int i = 0;
546+
for (SqlParameter parameter : this.callParameters) {
547+
if (parameter.isInputValueProvided()) {
548+
String parameterName = parameter.getName();
549+
matchedParameters.put(parameterName, parameterValues[i++]);
550+
}
551+
}
552+
return matchedParameters;
553+
}
554+
543555
/**
544556
* Build the call string based on configuration and metadata information.
545557
* @return the call string to be used

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java

+21
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ protected Map<String, Object> doExecute(SqlParameterSource parameterSource) {
341341
return executeCallInternal(params);
342342
}
343343

344+
/**
345+
* Method that provides execution of the call using the passed in array of parameters
346+
* @param args array of parameter values; order must match the order declared for the stored procedure
347+
* @return Map of out parameters
348+
*/
349+
protected Map<String, Object> doExecute(Object[] args) {
350+
checkCompiled();
351+
Map<String, ?> params = matchInParameterValuesWithCallParameters(args);
352+
return executeCallInternal(params);
353+
}
354+
344355
/**
345356
* Method that provides execution of the call using the passed in Map of parameters
346357
* @param args Map of parameter name and values
@@ -385,6 +396,16 @@ protected Map<String, Object> matchInParameterValuesWithCallParameters(SqlParame
385396
return this.callMetaDataContext.matchInParameterValuesWithCallParameters(parameterSource);
386397
}
387398

399+
/**
400+
* Match the provided in parameter values with registered parameters and
401+
* parameters defined via metadata processing.
402+
* @param args the parameter values provided as an array
403+
* @return Map with parameter names and values
404+
*/
405+
private Map<String,?> matchInParameterValuesWithCallParameters(Object[] args) {
406+
return this.callMetaDataContext.matchInParameterValuesWithCallParameters(args);
407+
}
408+
388409
/**
389410
* Match the provided in parameter values with registered parameters and
390411
* parameters defined via metadata processing.

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCall.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ public SimpleJdbcCall withoutProcedureColumnMetaDataAccess() {
131131
return this;
132132
}
133133

134+
@SuppressWarnings("unchecked")
135+
public <T> T executeFunction(Class<T> returnType, Object... args) {
136+
return (T) doExecute(args).get(getScalarOutParameterName());
137+
}
134138

135139
@SuppressWarnings("unchecked")
136140
public <T> T executeFunction(Class<T> returnType, Map<String, Object> args) {
@@ -142,6 +146,11 @@ public <T> T executeFunction(Class<T> returnType, SqlParameterSource args) {
142146
return (T) doExecute(args).get(getScalarOutParameterName());
143147
}
144148

149+
@SuppressWarnings("unchecked")
150+
public <T> T executeObject(Class<T> returnType, Object... args) {
151+
return (T) doExecute(args).get(getScalarOutParameterName());
152+
}
153+
145154
@SuppressWarnings("unchecked")
146155
public <T> T executeObject(Class<T> returnType, Map<String, Object> args) {
147156
return (T) doExecute(args).get(getScalarOutParameterName());
@@ -152,8 +161,8 @@ public <T> T executeObject(Class<T> returnType, SqlParameterSource args) {
152161
return (T) doExecute(args).get(getScalarOutParameterName());
153162
}
154163

155-
public Map<String, Object> execute() {
156-
return doExecute(new HashMap<String, Object>());
164+
public Map<String, Object> execute(Object... args) {
165+
return doExecute(args);
157166
}
158167

159168
public Map<String, Object> execute(Map<String, Object> args) {

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcCallOperations.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ public interface SimpleJdbcCallOperations {
101101
SimpleJdbcCallOperations withoutProcedureColumnMetaDataAccess();
102102

103103

104+
/**
105+
* Execute the stored function and return the results obtained as an Object of the specified return type.
106+
* @param returnType the type of the value to return
107+
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
108+
* be provided in the same order as the parameters are defined for the stored procedure.
109+
*/
110+
<T> T executeFunction(Class<T> returnType, Object... args);
111+
104112
/**
105113
* Execute the stored function and return the results obtained as an Object of the specified return type.
106114
* @param returnType the type of the value to return
@@ -115,6 +123,16 @@ public interface SimpleJdbcCallOperations {
115123
*/
116124
<T> T executeFunction(Class<T> returnType, SqlParameterSource args);
117125

126+
/**
127+
* Execute the stored procedure and return the single out parameter as an Object of the specified return type.
128+
* In the case where there are multiple out parameters, the first one is returned and additional out parameters
129+
* are ignored.
130+
* @param returnType the type of the value to return
131+
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
132+
* be provided in the same order as the parameters are defined for the stored procedure.
133+
*/
134+
<T> T executeObject(Class<T> returnType, Object... args);
135+
118136
/**
119137
* Execute the stored procedure and return the single out parameter as an Object of the specified return type.
120138
* In the case where there are multiple out parameters, the first one is returned and additional out parameters
@@ -134,10 +152,12 @@ public interface SimpleJdbcCallOperations {
134152
<T> T executeObject(Class<T> returnType, SqlParameterSource args);
135153

136154
/**
137-
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..
155+
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations.
156+
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
157+
* be provided in the same order as the parameters are defined for the stored procedure.
138158
* @return map of output params.
139159
*/
140-
Map<String, Object> execute();
160+
Map<String, Object> execute(Object... args);
141161

142162
/**
143163
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..

0 commit comments

Comments
 (0)