Skip to content

Commit

Permalink
Support for SQL Server named parameter binding (aligned with Sybase)
Browse files Browse the repository at this point in the history
Closes gh-26072
See gh-30231
  • Loading branch information
jhoeller committed Dec 8, 2023
1 parent 748dd94 commit afc1f37
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public String createCallString() {
protected String createParameterBinding(SqlParameter parameter) {
Assert.state(this.metaDataProvider != null, "No CallMetaDataProvider available");

return (isNamedBinding() ? this.metaDataProvider.namedParamBindingToUse(parameter.getName()) : "?");
return (isNamedBinding() ? this.metaDataProvider.namedParameterBindingToUse(parameter.getName()) : "?");
}

private static String lowerCase(@Nullable String paramName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* {@link org.springframework.jdbc.core.simple.SimpleJdbcCall}.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @author Giuseppe Milicia
* @since 2.5
*/
public interface CallMetaDataProvider {
Expand All @@ -55,6 +57,12 @@ public interface CallMetaDataProvider {
void initializeWithProcedureColumnMetaData(DatabaseMetaData databaseMetaData, @Nullable String catalogName,
@Nullable String schemaName, @Nullable String procedureName) throws SQLException;

/**
* Get the call parameter meta-data that is currently used.
* @return a List of {@link CallParameterMetaData}
*/
List<CallParameterMetaData> getCallParameterMetaData();

/**
* Provide any modification of the procedure name passed in to match the meta-data currently used.
* <p>This could include altering the case.
Expand Down Expand Up @@ -100,6 +108,14 @@ void initializeWithProcedureColumnMetaData(DatabaseMetaData databaseMetaData, @N
@Nullable
String parameterNameToUse(@Nullable String parameterName);

/**
* Return the name of the named parameter to use for binding the given parameter name.
* @param parameterName the name of the parameter to bind
* @return the name of the named parameter to use for binding the given parameter name
* @since 6.1.2
*/
String namedParameterBindingToUse(@Nullable String parameterName);

/**
* Create a default out parameter based on the provided meta-data.
* <p>This is used when no explicit parameter declaration has been made.
Expand Down Expand Up @@ -164,12 +180,6 @@ void initializeWithProcedureColumnMetaData(DatabaseMetaData databaseMetaData, @N
*/
boolean byPassReturnParameter(String parameterName);

/**
* Get the call parameter meta-data that is currently used.
* @return a List of {@link CallParameterMetaData}
*/
List<CallParameterMetaData> getCallParameterMetaData();

/**
* Does the database support the use of catalog name in procedure calls?
*/
Expand All @@ -180,11 +190,4 @@ void initializeWithProcedureColumnMetaData(DatabaseMetaData databaseMetaData, @N
*/
boolean isSupportsSchemasInProcedureCalls();

/**
* Returns the name of the named parameter to use for binding the given parameter name.
* @param paramName the name of the parameter to bind
* @return the name of the named parameter to use for binding the given parameter name,
*/
String namedParamBindingToUse(@Nullable String paramName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public String parameterNameToUse(@Nullable String parameterName) {
}

@Override
public boolean byPassReturnParameter(String parameterName) {
return false;
public String namedParameterBindingToUse(@Nullable String parameterName) {
return parameterName + " => ?";
}

@Override
Expand Down Expand Up @@ -215,6 +215,11 @@ public boolean isProcedureColumnMetaDataUsed() {
return this.procedureColumnMetaDataUsed;
}

@Override
public boolean byPassReturnParameter(String parameterName) {
return false;
}


/**
* Specify whether the database supports the use of catalog name in procedure calls.
Expand Down Expand Up @@ -246,16 +251,6 @@ public boolean isSupportsSchemasInProcedureCalls() {
return this.supportsSchemasInProcedureCalls;
}

/**
* Returns the name of the named parameter to use for binding the given parameter name.
* @param paramName the name of the parameter to bind
* @return the name of the named parameter to use for binding the given parameter name,
*/
@Override
public String namedParamBindingToUse(@Nullable String paramName) {
return paramName + " => ?";
}

/**
* Specify whether the database uses upper case for identifiers.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -26,6 +26,7 @@
* This class is intended for internal use by the Simple JDBC classes.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 2.5
*/
public class SqlServerCallMetaDataProvider extends GenericCallMetaDataProvider {
Expand Down Expand Up @@ -54,6 +55,11 @@ else if (parameterName.length() > 1 && parameterName.startsWith(REMOVABLE_COLUMN
}
}

@Override
public String namedParameterBindingToUse(@Nullable String parameterName) {
return parameterName + " = ?";
}

@Override
public boolean byPassReturnParameter(String parameterName) {
return RETURN_VALUE_NAME.equals(parameterName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -26,6 +26,7 @@
* This class is intended for internal use by the Simple JDBC classes.
*
* @author Thomas Risberg
* @author Giuseppe Milicia
* @since 2.5
*/
public class SybaseCallMetaDataProvider extends GenericCallMetaDataProvider {
Expand Down Expand Up @@ -55,14 +56,14 @@ else if (parameterName.length() > 1 && parameterName.startsWith(REMOVABLE_COLUMN
}

@Override
public boolean byPassReturnParameter(String parameterName) {
return (RETURN_VALUE_NAME.equals(parameterName) ||
RETURN_VALUE_NAME.equals(parameterNameToUse(parameterName)));
public String namedParameterBindingToUse(@Nullable String parameterName) {
return parameterName + " = ?";
}

@Override
public String namedParamBindingToUse(@Nullable String paramName) {
return paramName + " = ?";
public boolean byPassReturnParameter(String parameterName) {
return (RETURN_VALUE_NAME.equals(parameterName) ||
RETURN_VALUE_NAME.equals(parameterNameToUse(parameterName)));
}

}

0 comments on commit afc1f37

Please sign in to comment.