Skip to content

Commit 301ba01

Browse files
committed
CallMetaDataContext handles 'procedureColumnResult' as return parameter
Issue: SPR-16611 (cherry picked from commit b7c4238)
1 parent 8818176 commit 301ba01

File tree

2 files changed

+91
-70
lines changed

2 files changed

+91
-70
lines changed

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

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import org.springframework.util.StringUtils;
4343

4444
/**
45-
* Class to manage context metadata used for the configuration and execution of the call.
45+
* Class to manage context meta-data used for the configuration
46+
* and execution of a stored procedure call.
4647
*
4748
* @author Thomas Risberg
4849
* @author Juergen Hoeller
@@ -81,13 +82,13 @@ public class CallMetaDataContext {
8182
// Indicates whether this procedure's return value should be included
8283
private boolean returnValueRequired = false;
8384

84-
// Should we access call parameter meta data info or not
85+
// Should we access call parameter meta-data info or not
8586
private boolean accessCallParameterMetaData = true;
8687

8788
// Should we bind parameter by name
8889
private boolean namedBinding;
8990

90-
// The provider of call meta data
91+
// The provider of call meta-data
9192
private CallMetaDataProvider metaDataProvider;
9293

9394

@@ -204,14 +205,14 @@ public boolean isReturnValueRequired() {
204205
}
205206

206207
/**
207-
* Specify whether call parameter metadata should be accessed.
208+
* Specify whether call parameter meta-data should be accessed.
208209
*/
209210
public void setAccessCallParameterMetaData(boolean accessCallParameterMetaData) {
210211
this.accessCallParameterMetaData = accessCallParameterMetaData;
211212
}
212213

213214
/**
214-
* Check whether call parameter metadata should be accessed.
215+
* Check whether call parameter meta-data should be accessed.
215216
*/
216217
public boolean isAccessCallParameterMetaData() {
217218
return this.accessCallParameterMetaData;
@@ -235,8 +236,8 @@ public boolean isNamedBinding() {
235236

236237

237238
/**
238-
* Initialize this class with metadata from the database.
239-
* @param dataSource the DataSource used to retrieve metadata
239+
* Initialize this class with meta-data from the database.
240+
* @param dataSource the DataSource used to retrieve meta-data
240241
*/
241242
public void initializeMetaData(DataSource dataSource) {
242243
this.metaDataProvider = CallMetaDataProviderFactory.createMetaDataProvider(dataSource, this);
@@ -258,7 +259,8 @@ public SqlParameter createReturnResultSetParameter(String parameterName, RowMapp
258259
return new SqlOutParameter(parameterName, this.metaDataProvider.getRefCursorSqlType(), rowMapper);
259260
}
260261
else {
261-
throw new InvalidDataAccessApiUsageException("Return of a ResultSet from a stored procedure is not supported.");
262+
throw new InvalidDataAccessApiUsageException(
263+
"Return of a ResultSet from a stored procedure is not supported");
262264
}
263265
}
264266
}
@@ -275,7 +277,7 @@ public String getScalarOutParameterName() {
275277
if (this.outParameterNames.size() > 1) {
276278
logger.warn("Accessing single output value when procedure has more than one output parameter");
277279
}
278-
return (this.outParameterNames.size() > 0 ? this.outParameterNames.get(0) : null);
280+
return (!this.outParameterNames.isEmpty() ? this.outParameterNames.get(0) : null);
279281
}
280282
}
281283

@@ -287,8 +289,8 @@ public List<SqlParameter> getCallParameters() {
287289
}
288290

289291
/**
290-
* Process the list of parameters provided, and if procedure column metadata is used,
291-
* the parameters will be matched against the metadata information and any missing
292+
* Process the list of parameters provided, and if procedure column meta-data is used,
293+
* the parameters will be matched against the meta-data information and any missing
292294
* ones will be automatically included.
293295
* @param parameters the list of parameters to use as a base
294296
*/
@@ -297,7 +299,7 @@ public void processParameters(List<SqlParameter> parameters) {
297299
}
298300

299301
/**
300-
* Reconcile the provided parameters with available metadata and add new ones where appropriate.
302+
* Reconcile the provided parameters with available meta-data and add new ones where appropriate.
301303
*/
302304
protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters) {
303305
final List<SqlParameter> declaredReturnParams = new ArrayList<SqlParameter>();
@@ -306,9 +308,9 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
306308
List<String> outParamNames = new ArrayList<String>();
307309
List<String> metaDataParamNames = new ArrayList<String>();
308310

309-
// Get the names of the meta data parameters
311+
// Get the names of the meta-data parameters
310312
for (CallParameterMetaData meta : this.metaDataProvider.getCallParameterMetaData()) {
311-
if (meta.getParameterType() != DatabaseMetaData.procedureColumnReturn) {
313+
if (!meta.isReturnParameter()) {
312314
metaDataParamNames.add(meta.getParameterName().toLowerCase());
313315
}
314316
}
@@ -328,15 +330,12 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
328330
declaredParams.put(paramNameToMatch, param);
329331
if (param instanceof SqlOutParameter) {
330332
outParamNames.add(paramName);
331-
if (isFunction() && !metaDataParamNames.contains(paramNameToMatch)) {
332-
if (!returnDeclared) {
333-
if (logger.isDebugEnabled()) {
334-
logger.debug("Using declared out parameter '" + paramName +
335-
"' for function return value");
336-
}
337-
setFunctionReturnName(paramName);
338-
returnDeclared = true;
333+
if (isFunction() && !metaDataParamNames.contains(paramNameToMatch) && !returnDeclared) {
334+
if (logger.isDebugEnabled()) {
335+
logger.debug("Using declared out parameter '" + paramName + "' for function return value");
339336
}
337+
setFunctionReturnName(paramName);
338+
returnDeclared = true;
340339
}
341340
}
342341
}
@@ -345,7 +344,6 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
345344

346345
List<SqlParameter> workParams = new ArrayList<SqlParameter>();
347346
workParams.addAll(declaredReturnParams);
348-
349347
if (!this.metaDataProvider.isProcedureColumnMetaDataUsed()) {
350348
workParams.addAll(declaredParams.values());
351349
return workParams;
@@ -363,12 +361,11 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
363361
paramNameToCheck = this.metaDataProvider.parameterNameToUse(meta.getParameterName()).toLowerCase();
364362
}
365363
String paramNameToUse = this.metaDataProvider.parameterNameToUse(meta.getParameterName());
366-
if (declaredParams.containsKey(paramNameToCheck) ||
367-
(meta.getParameterType() == DatabaseMetaData.procedureColumnReturn && returnDeclared)) {
364+
if (declaredParams.containsKey(paramNameToCheck) || (meta.isReturnParameter() && returnDeclared)) {
368365
SqlParameter param;
369-
if (meta.getParameterType() == DatabaseMetaData.procedureColumnReturn) {
366+
if (meta.isReturnParameter()) {
370367
param = declaredParams.get(getFunctionReturnName());
371-
if (param == null && getOutParameterNames().size() > 0) {
368+
if (param == null && !getOutParameterNames().isEmpty()) {
372369
param = declaredParams.get(getOutParameterNames().get(0).toLowerCase());
373370
}
374371
if (param == null) {
@@ -392,23 +389,24 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
392389
}
393390
}
394391
else {
395-
if (meta.getParameterType() == DatabaseMetaData.procedureColumnReturn) {
392+
if (meta.isReturnParameter()) {
393+
// DatabaseMetaData.procedureColumnReturn or possibly procedureColumnResult
396394
if (!isFunction() && !isReturnValueRequired() &&
397395
this.metaDataProvider.byPassReturnParameter(meta.getParameterName())) {
398396
if (logger.isDebugEnabled()) {
399-
logger.debug("Bypassing metadata return parameter for '" + meta.getParameterName() + "'");
397+
logger.debug("Bypassing meta-data return parameter for '" + meta.getParameterName() + "'");
400398
}
401399
}
402400
else {
403-
String returnNameToUse =(StringUtils.hasLength(meta.getParameterName()) ?
404-
paramNameToUse : getFunctionReturnName());
401+
String returnNameToUse =
402+
(StringUtils.hasLength(meta.getParameterName()) ? paramNameToUse : getFunctionReturnName());
405403
workParams.add(this.metaDataProvider.createDefaultOutParameter(returnNameToUse, meta));
406404
if (isFunction()) {
407405
setFunctionReturnName(returnNameToUse);
408406
outParamNames.add(returnNameToUse);
409407
}
410408
if (logger.isDebugEnabled()) {
411-
logger.debug("Added metadata return parameter for '" + returnNameToUse + "'");
409+
logger.debug("Added meta-data return parameter for '" + returnNameToUse + "'");
412410
}
413411
}
414412
}
@@ -417,22 +415,22 @@ protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters)
417415
workParams.add(this.metaDataProvider.createDefaultOutParameter(paramNameToUse, meta));
418416
outParamNames.add(paramNameToUse);
419417
if (logger.isDebugEnabled()) {
420-
logger.debug("Added metadata out parameter for '" + paramNameToUse + "'");
418+
logger.debug("Added meta-data out parameter for '" + paramNameToUse + "'");
421419
}
422420
}
423421
else if (meta.getParameterType() == DatabaseMetaData.procedureColumnInOut) {
424422
workParams.add(this.metaDataProvider.createDefaultInOutParameter(paramNameToUse, meta));
425423
outParamNames.add(paramNameToUse);
426424
if (logger.isDebugEnabled()) {
427-
logger.debug("Added metadata in out parameter for '" + paramNameToUse + "'");
425+
logger.debug("Added meta-data in-out parameter for '" + paramNameToUse + "'");
428426
}
429427
}
430428
else {
431429
if (this.limitedInParameterNames.isEmpty() ||
432430
limitedInParamNamesMap.containsKey(paramNameToUse.toLowerCase())) {
433431
workParams.add(this.metaDataProvider.createDefaultInParameter(paramNameToUse, meta));
434432
if (logger.isDebugEnabled()) {
435-
logger.debug("Added metadata in parameter for '" + paramNameToUse + "'");
433+
logger.debug("Added meta-data in parameter for '" + paramNameToUse + "'");
436434
}
437435
}
438436
else {
@@ -456,7 +454,7 @@ else if (meta.getParameterType() == DatabaseMetaData.procedureColumnInOut) {
456454
*/
457455
public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameterSource parameterSource) {
458456
// For parameter source lookups we need to provide case-insensitive lookup support
459-
// since the database metadata is not necessarily providing case sensitive parameter names.
457+
// since the database meta-data is not necessarily providing case sensitive parameter names.
460458
Map<String, String> caseInsensitiveParameterNames =
461459
SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource);
462460

@@ -471,31 +469,37 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
471469
}
472470
if (parameterName != null) {
473471
if (parameterSource.hasValue(parameterName)) {
474-
matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, parameterName));
472+
matchedParameters.put(parameterName,
473+
SqlParameterSourceUtils.getTypedValue(parameterSource, parameterName));
475474
}
476475
else {
477476
String lowerCaseName = parameterName.toLowerCase();
478477
if (parameterSource.hasValue(lowerCaseName)) {
479-
matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName));
478+
matchedParameters.put(parameterName,
479+
SqlParameterSourceUtils.getTypedValue(parameterSource, lowerCaseName));
480480
}
481481
else {
482482
String englishLowerCaseName = parameterName.toLowerCase(Locale.ENGLISH);
483483
if (parameterSource.hasValue(englishLowerCaseName)) {
484-
matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, englishLowerCaseName));
484+
matchedParameters.put(parameterName,
485+
SqlParameterSourceUtils.getTypedValue(parameterSource, englishLowerCaseName));
485486
}
486487
else {
487488
String propertyName = JdbcUtils.convertUnderscoreNameToPropertyName(parameterName);
488489
if (parameterSource.hasValue(propertyName)) {
489-
matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
490+
matchedParameters.put(parameterName,
491+
SqlParameterSourceUtils.getTypedValue(parameterSource, propertyName));
490492
}
491493
else {
492494
if (caseInsensitiveParameterNames.containsKey(lowerCaseName)) {
493495
String sourceName = caseInsensitiveParameterNames.get(lowerCaseName);
494-
matchedParameters.put(parameterName, SqlParameterSourceUtils.getTypedValue(parameterSource, sourceName));
496+
matchedParameters.put(parameterName,
497+
SqlParameterSourceUtils.getTypedValue(parameterSource, sourceName));
495498
}
496499
else {
497-
logger.warn("Unable to locate the corresponding parameter value for '" + parameterName +
498-
"' within the parameter values provided: " + caseInsensitiveParameterNames.values());
500+
logger.warn("Unable to locate the corresponding parameter value for '" +
501+
parameterName + "' within the parameter values provided: " +
502+
caseInsensitiveParameterNames.values());
499503
}
500504
}
501505
}
@@ -521,6 +525,7 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
521525
if (!this.metaDataProvider.isProcedureColumnMetaDataUsed()) {
522526
return inParameters;
523527
}
528+
524529
Map<String, String> callParameterNames = new HashMap<String, String>(this.callParameters.size());
525530
for (SqlParameter parameter : this.callParameters) {
526531
if (parameter.isInputValueProvided()) {
@@ -531,6 +536,7 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
531536
}
532537
}
533538
}
539+
534540
Map<String, Object> matchedParameters = new HashMap<String, Object>(inParameters.size());
535541
for (String parameterName : inParameters.keySet()) {
536542
String parameterNameToMatch = this.metaDataProvider.parameterNameToUse(parameterName);
@@ -539,18 +545,19 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
539545
if (logger.isDebugEnabled()) {
540546
Object value = inParameters.get(parameterName);
541547
if (value instanceof SqlParameterValue) {
542-
value = ((SqlParameterValue)value).getValue();
548+
value = ((SqlParameterValue) value).getValue();
543549
}
544550
if (value != null) {
545-
logger.debug("Unable to locate the corresponding IN or IN-OUT parameter for \"" + parameterName +
546-
"\" in the parameters used: " + callParameterNames.keySet());
551+
logger.debug("Unable to locate the corresponding IN or IN-OUT parameter for \"" +
552+
parameterName + "\" in the parameters used: " + callParameterNames.keySet());
547553
}
548554
}
549555
}
550556
else {
551557
matchedParameters.put(callParameterName, inParameters.get(parameterName));
552558
}
553559
}
560+
554561
if (matchedParameters.size() < callParameterNames.size()) {
555562
for (String parameterName : callParameterNames.keySet()) {
556563
String parameterNameToMatch = this.metaDataProvider.parameterNameToUse(parameterName);
@@ -561,6 +568,7 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
561568
}
562569
}
563570
}
571+
564572
if (logger.isDebugEnabled()) {
565573
logger.debug("Matching " + inParameters.keySet() + " with " + callParameterNames.values());
566574
logger.debug("Found match for " + matchedParameters.keySet());
@@ -581,11 +589,11 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
581589
}
582590

583591
/**
584-
* Build the call string based on configuration and metadata information.
592+
* Build the call string based on configuration and meta-data information.
585593
* @return the call string to be used
586594
*/
587595
public String createCallString() {
588-
String callString;
596+
StringBuilder callString;
589597
int parameterCount = 0;
590598
String catalogNameToUse;
591599
String schemaNameToUse;
@@ -601,34 +609,36 @@ public String createCallString() {
601609
catalogNameToUse = this.metaDataProvider.catalogNameToUse(getCatalogName());
602610
schemaNameToUse = this.metaDataProvider.schemaNameToUse(getSchemaName());
603611
}
612+
604613
String procedureNameToUse = this.metaDataProvider.procedureNameToUse(getProcedureName());
605614
if (isFunction() || isReturnValueRequired()) {
606-
callString = "{? = call " +
607-
(StringUtils.hasLength(catalogNameToUse) ? catalogNameToUse + "." : "") +
608-
(StringUtils.hasLength(schemaNameToUse) ? schemaNameToUse + "." : "") +
609-
procedureNameToUse + "(";
615+
callString = new StringBuilder().append("{? = call ").
616+
append(StringUtils.hasLength(catalogNameToUse) ? catalogNameToUse + "." : "").
617+
append(StringUtils.hasLength(schemaNameToUse) ? schemaNameToUse + "." : "").
618+
append(procedureNameToUse).append("(");
610619
parameterCount = -1;
611620
}
612621
else {
613-
callString = "{call " +
614-
(StringUtils.hasLength(catalogNameToUse) ? catalogNameToUse + "." : "") +
615-
(StringUtils.hasLength(schemaNameToUse) ? schemaNameToUse + "." : "") +
616-
procedureNameToUse + "(";
622+
callString = new StringBuilder().append("{call ").
623+
append(StringUtils.hasLength(catalogNameToUse) ? catalogNameToUse + "." : "").
624+
append(StringUtils.hasLength(schemaNameToUse) ? schemaNameToUse + "." : "").
625+
append(procedureNameToUse).append("(");
617626
}
627+
618628
for (SqlParameter parameter : this.callParameters) {
619-
if (!(parameter.isResultsParameter())) {
629+
if (!parameter.isResultsParameter()) {
620630
if (parameterCount > 0) {
621-
callString += ", ";
631+
callString.append(", ");
622632
}
623633
if (parameterCount >= 0) {
624-
callString += createParameterBinding(parameter);
634+
callString.append(createParameterBinding(parameter));
625635
}
626636
parameterCount++;
627637
}
628638
}
629-
callString += ")}";
639+
callString.append(")}");
630640

631-
return callString;
641+
return callString.toString();
632642
}
633643

634644
/**
@@ -638,12 +648,7 @@ public String createCallString() {
638648
* @since 4.2
639649
*/
640650
protected String createParameterBinding(SqlParameter parameter) {
641-
if (isNamedBinding()) {
642-
return parameter.getName() + " => ?";
643-
}
644-
else {
645-
return "?";
646-
}
651+
return (isNamedBinding() ? parameter.getName() + " => ?" : "?");
647652
}
648653

649654
}

0 commit comments

Comments
 (0)