From 23383f2b923d027d35701d8a0e5cea29dabc7db5 Mon Sep 17 00:00:00 2001 From: Shawn Sun Date: Mon, 31 Jul 2017 12:19:43 -0700 Subject: [PATCH 1/3] fix issue when calling getString() on unique identifier parameter --- .../sqlserver/jdbc/SQLServerCallableStatement.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java index e5eaa7ccf..43cf593ef 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java @@ -472,8 +472,16 @@ public int getInt(String sCol) throws SQLServerException { public String getString(int index) throws SQLServerException { loggerExternal.entering(getClassNameLogging(), "getString", index); checkClosed(); + + Object objectValue = null; + if (JDBCType.GUID == getterGetParam(index).getJdbcType()) { + objectValue = getValue(index, JDBCType.GUID); + } + else { + objectValue = getValue(index, JDBCType.CHAR); + } + String value = null; - Object objectValue = getValue(index, JDBCType.CHAR); if (null != objectValue) { value = objectValue.toString(); } From 6c7359f7cffebb78b5a4be5f32a9b5345481a323 Mon Sep 17 00:00:00 2001 From: Shawn Sun Date: Mon, 31 Jul 2017 13:42:47 -0700 Subject: [PATCH 2/3] add test --- .../CallableStatementTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/test/java/com/microsoft/sqlserver/jdbc/callablestatement/CallableStatementTest.java diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/callablestatement/CallableStatementTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/callablestatement/CallableStatementTest.java new file mode 100644 index 000000000..da24e42d0 --- /dev/null +++ b/src/test/java/com/microsoft/sqlserver/jdbc/callablestatement/CallableStatementTest.java @@ -0,0 +1,108 @@ +package com.microsoft.sqlserver.jdbc.callablestatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.UUID; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement; +import com.microsoft.sqlserver.testframework.AbstractTest; +import com.microsoft.sqlserver.testframework.Utils; + +/** + * Test CallableStatement + */ +@RunWith(JUnitPlatform.class) +public class CallableStatementTest extends AbstractTest { + private static String tableNameGUID = "uniqueidentifier_Table"; + private static String outputProcedureNameGUID = "uniqueidentifier_SP"; + + private static Connection connection = null; + private static Statement stmt = null; + + /** + * Setup before test + * + * @throws SQLException + */ + @BeforeAll + public static void setupTest() throws SQLException { + connection = DriverManager.getConnection(connectionString); + stmt = connection.createStatement(); + + Utils.dropTableIfExists(tableNameGUID, stmt); + Utils.dropProcedureIfExists(outputProcedureNameGUID, stmt); + + createGUIDTable(); + createGUIDStoredProcedure(); + } + + /** + * Tests CallableStatement.getString() with uniqueidentifier parameter + * + * @throws SQLException + */ + @Test + public void getStringGUIDTest() throws SQLException { + + SQLServerCallableStatement callableStatement = null; + try { + String sql = "{call " + outputProcedureNameGUID + "(?)}"; + + callableStatement = (SQLServerCallableStatement) connection.prepareCall(sql); + + UUID originalValue = UUID.randomUUID(); + + callableStatement.registerOutParameter(1, microsoft.sql.Types.GUID); + callableStatement.setObject(1, originalValue.toString(), microsoft.sql.Types.GUID); + + callableStatement.execute(); + + String retrievedValue = callableStatement.getString(1); + + assertEquals(originalValue.toString().toLowerCase(), retrievedValue.toLowerCase()); + + } + finally { + if (null != callableStatement) { + callableStatement.close(); + } + } + } + + /** + * Cleanup after test + * + * @throws SQLException + */ + @AfterAll + public static void cleanup() throws SQLException { + Utils.dropTableIfExists(tableNameGUID, stmt); + Utils.dropProcedureIfExists(outputProcedureNameGUID, stmt); + if (null != stmt) { + stmt.close(); + } + if (null != connection) { + connection.close(); + } + } + + private static void createGUIDStoredProcedure() throws SQLException { + String sql = "CREATE PROCEDURE " + outputProcedureNameGUID + "(@p1 uniqueidentifier OUTPUT) AS SELECT @p1 = c1 FROM " + tableNameGUID + ";"; + stmt.execute(sql); + } + + private static void createGUIDTable() throws SQLException { + String sql = "CREATE TABLE " + tableNameGUID + " (c1 uniqueidentifier null)"; + stmt.execute(sql); + } +} From 133afdcbb0711ee89f24b71bbdfdb167c5602ebe Mon Sep 17 00:00:00 2001 From: Shawn Sun Date: Mon, 31 Jul 2017 15:57:54 -0700 Subject: [PATCH 3/3] better fix --- .../sqlserver/jdbc/SQLServerCallableStatement.java | 10 +--------- .../java/com/microsoft/sqlserver/jdbc/dtv.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java index 43cf593ef..e5eaa7ccf 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerCallableStatement.java @@ -472,16 +472,8 @@ public int getInt(String sCol) throws SQLServerException { public String getString(int index) throws SQLServerException { loggerExternal.entering(getClassNameLogging(), "getString", index); checkClosed(); - - Object objectValue = null; - if (JDBCType.GUID == getterGetParam(index).getJdbcType()) { - objectValue = getValue(index, JDBCType.GUID); - } - else { - objectValue = getValue(index, JDBCType.CHAR); - } - String value = null; + Object objectValue = getValue(index, JDBCType.CHAR); if (null != objectValue) { value = objectValue.toString(); } diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java index c836c031d..49e5abc54 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java @@ -1617,10 +1617,16 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException { switch (javaType) { case STRING: if (JDBCType.GUID == jdbcType) { - if (value instanceof String) - value = UUID.fromString((String) value); - byte[] bArray = Util.asGuidByteArray((UUID) value); - op.execute(this, bArray); + if (null != cryptoMeta) { + if (value instanceof String) { + value = UUID.fromString((String) value); + } + byte[] bArray = Util.asGuidByteArray((UUID) value); + op.execute(this, bArray); + } + else { + op.execute(this, String.valueOf(value)); + } } else if (jdbcType.SQL_VARIANT == jdbcType) { op.execute(this, String.valueOf(value));