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

Get string issue for uniqueidentifier #423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}