Skip to content

Commit 2e03f12

Browse files
committed
PR review; initial named parameter and index parameter restrictions
1 parent e771daf commit 2e03f12

File tree

8 files changed

+547
-3
lines changed

8 files changed

+547
-3
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,5 @@ CallableStatement prepareCall(String sql, int nType, int nConcur, int nHold,
496496
* @param calcBigDecimalScale
497497
* A boolean that indicates if the driver should calculate scale from inputted big decimal values.
498498
*/
499-
void setCalcBigDecimalScale(boolean computeBigDecimal);
499+
void setCalcBigDecimalScale(boolean calcBigDecimalScale);
500500
}

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ private Parameter getterGetParam(int index) throws SQLServerException {
470470

471471
// Check for valid index
472472
if (index < 1 || index > inOutParam.length) {
473+
if (!connection.getUseFlexibleCallableStatements()) {
474+
SQLServerException.makeFromDriverError(connection, this,
475+
SQLServerException.getErrString("R_unknownOutputParameter"), SQLSTATE_07009, false);
476+
}
473477
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidOutputParameter"));
474478
Object[] msgArgs = {index};
475479
SQLServerException.makeFromDriverError(connection, this, form.format(msgArgs), SQLSTATE_07009, false);
@@ -1412,6 +1416,12 @@ private String stripLeadingAtSign(String columnName) {
14121416
* @return the index
14131417
*/
14141418
private int findColumn(String columnName, CallableStatementGetterSetterMethod method) throws SQLServerException {
1419+
isSetByName = true;
1420+
if (!connection.getUseFlexibleCallableStatements() && isSetByName && isSetByIndex) {
1421+
SQLServerException.makeFromDriverError(connection, this,
1422+
SQLServerException.getErrString("R_noNamedAndIndexedParameters"), null, false);
1423+
}
1424+
14151425
// If inOutParam is null, likely the statement was closed beforehand.
14161426
if (null == inOutParam) {
14171427
SQLServerException.makeFromDriverError(connection, this,

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7842,8 +7842,11 @@ public boolean getUseFlexibleCallableStatements() {
78427842
* Sets whether or not sp_sproc_columns will be used for parameter name lookup.
78437843
*
78447844
* @param useFlexibleCallableStatements
7845-
* When set to true, sp_sproc_columns is not used for parameter name lookup
7846-
* in callable statements.
7845+
* When set to false, sp_sproc_columns is not used for parameter name lookup
7846+
* in callable statements. This eliminates a round trip to the server but imposes limitations
7847+
* on how parameters are set. When set to false, applications must either reference
7848+
* parameters by name or by index, not both. Parameters must also be set in the same
7849+
* order as the stored procedure definition.
78477850
*/
78487851
public void setUseFlexibleCallableStatements(boolean useFlexibleCallableStatements) {
78497852
this.useFlexibleCallableStatements = useFlexibleCallableStatements;

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 87 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ protected Object[][] getContents() {
144144
{"R_stringNotInHex", "The string is not in a valid hex format."},
145145
{"R_unknownType", "The Java type {0} is not a supported type."},
146146
{"R_physicalConnectionIsClosed", "The physical connection is closed for this pooled connection."},
147+
{"R_noNamedAndIndexedParameters", "Detected uncompliant use of both named and indexed parameters while 'useFlexibleCallableStatements=false'. It is suggested to either exclusively use named parameters or indexed parameters."},
148+
{"R_unknownOutputParameter", "Cannot acquire output parameter value by name. No parameter index was associated with the output parameter name. If acquiring output parameter by name, verify that the output parameter was initially registered by name."},
147149
{"R_invalidDataSourceReference", "Invalid DataSource reference."},
148150
{"R_cantGetColumnValueFromDeletedRow", "Cannot get a value from a deleted row."},
149151
{"R_cantGetUpdatedColumnValue", "Updated columns cannot be accessed until updateRow() or cancelRowUpdates() has been called."},

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ final boolean wasExecuted() {
149149
*/
150150
boolean isCloseOnCompletion = false;
151151

152+
/** Checks if the callable statement's parameters are set by name **/
153+
protected boolean isSetByName = false;
154+
155+
/** Checks if the prepared statement's parameters were set by index **/
156+
protected boolean isSetByIndex = false;
157+
152158
/**
153159
* Currently executing or most recently executed TDSCommand (statement cmd, server cursor cmd, ...) subject to
154160
* cancellation through Statement.cancel.

src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected Object[][] getContents() {
5656
{"R_createDropAlterTableFailed", "Create/drop/alter table with preparedStatement failed!"},
5757
{"R_grantFailed", "grant table with preparedStatement failed!"},
5858
{"R_connectionIsClosed", "The connection is closed."},
59+
{"R_noNamedAndIndexedParameters", "Detected uncompliant use of both named and indexed parameters while 'useFlexibleCallableStatements=false'. It is suggested to either exclusively use named parameters or indexed parameters."},
60+
{"R_unknownOutputParameter", "Cannot acquire output parameter value by name. No parameter index was associated with the output parameter name. If acquiring output parameter by name, verify that the output parameter was initially registered by name."},
5961
{"R_ConnectionURLNull", "The connection URL is null."},
6062
{"R_connectionIsNotClosed", "The connection is not closed."},
6163
{"R_invalidExceptionMessage", "Invalid exception message"},

0 commit comments

Comments
 (0)