Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recognize CallableStatement parameter names with leading '@' #495

Merged
merged 2 commits into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,12 @@ public NClob getNClob(String parameterName) throws SQLException {
if (paramNames != null)
l = paramNames.size();

// handle `@name` as well as `name`, since `@name` is what's returned
// by DatabaseMetaData#getProcedureColumns
if (columnName.startsWith("@")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add the check at this line

if (!columnName.startsWith("@"))
                sParam = sParam.substring(1, sParam.length());

The reason I am proposing this is that to have the original column name when we throw the exception at this line Otherwise looks great! thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v-afrafi - Good point. Your change would actually have to be made in two places, so I tweaked it a bit.

columnName = columnName.substring(1, columnName.length());
}

// In order to be as accurate as possible when locating parameter name
// indexes, as well as be deterministic when running on various client
// locales, we search for parameter names using the following scheme:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.microsoft.sqlserver.jdbc.callablestatement;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
Expand All @@ -17,6 +20,7 @@

import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Utils;

Expand All @@ -28,6 +32,7 @@ public class CallableStatementTest extends AbstractTest {
private static String tableNameGUID = "uniqueidentifier_Table";
private static String outputProcedureNameGUID = "uniqueidentifier_SP";
private static String setNullProcedureName = "CallableStatementTest_setNull_SP";
private static String inputParamsProcedureName = "CallableStatementTest_inputParams_SP";

private static Connection connection = null;
private static Statement stmt = null;
Expand All @@ -45,10 +50,12 @@ public static void setupTest() throws SQLException {
Utils.dropTableIfExists(tableNameGUID, stmt);
Utils.dropProcedureIfExists(outputProcedureNameGUID, stmt);
Utils.dropProcedureIfExists(setNullProcedureName, stmt);
Utils.dropProcedureIfExists(inputParamsProcedureName, stmt);

createGUIDTable();
createGUIDStoredProcedure();
createSetNullPreocedure();
createSetNullProcedure();
createInputParamsProcedure();
}

/**
Expand Down Expand Up @@ -133,6 +140,44 @@ public void getSetNullWithTypeVarchar() throws SQLException {
}
}


/**
* recognize parameter names with and without leading '@'
*
* @throws SQLException
*/
@Test
public void inputParamsTest() throws SQLException {
String call = "{CALL " + inputParamsProcedureName + " (?,?)}";
ResultSet rs = null;

// the historical way: no leading '@', parameter names respected (not positional)
CallableStatement cs1 = connection.prepareCall(call);
cs1.setString("p2", "bar");
cs1.setString("p1", "foo");
rs = cs1.executeQuery();
rs.next();
assertEquals("foobar", rs.getString(1));

// the "new" way: leading '@', parameter names still respected (not positional)
CallableStatement cs2 = connection.prepareCall(call);
cs2.setString("@p2", "world!");
cs2.setString("@p1", "Hello ");
rs = cs2.executeQuery();
rs.next();
assertEquals("Hello world!", rs.getString(1));

// sanity check: unrecognized parameter name
CallableStatement cs3 = connection.prepareCall(call);
try {
cs3.setString("@whatever", "junk");
fail("SQLServerException should have been thrown");
} catch (SQLServerException sse) {
// expected
}

}

/**
* Cleanup after test
*
Expand All @@ -143,6 +188,7 @@ public static void cleanup() throws SQLException {
Utils.dropTableIfExists(tableNameGUID, stmt);
Utils.dropProcedureIfExists(outputProcedureNameGUID, stmt);
Utils.dropProcedureIfExists(setNullProcedureName, stmt);
Utils.dropProcedureIfExists(inputParamsProcedureName, stmt);

if (null != stmt) {
stmt.close();
Expand All @@ -162,7 +208,20 @@ private static void createGUIDTable() throws SQLException {
stmt.execute(sql);
}

private static void createSetNullPreocedure() throws SQLException {
private static void createSetNullProcedure() throws SQLException {
stmt.execute("create procedure " + setNullProcedureName + " (@p1 nvarchar(255), @p2 nvarchar(255) output) as select @p2=@p1 return 0");
}

private static void createInputParamsProcedure() throws SQLException {
String sql =
"CREATE PROCEDURE [dbo].[CallableStatementTest_inputParams_SP] " +
" @p1 nvarchar(max) = N'parameter1', " +
" @p2 nvarchar(max) = N'parameter2' " +
"AS " +
"BEGIN " +
" SET NOCOUNT ON; " +
" SELECT @p1 + @p2 AS result; " +
"END ";
stmt.execute(sql);
}
}