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

Add support for JavaType.UUID #2412

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/DataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.OffsetTime;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.UUID;


enum TDSType {
Expand Down Expand Up @@ -424,6 +425,7 @@ enum JavaType {
TVP(com.microsoft.sqlserver.jdbc.TVP.class, JDBCType.TVP),
GEOMETRY(Geometry.class, JDBCType.GEOMETRY),
GEOGRAPHY(Geography.class, JDBCType.GEOGRAPHY),
UUID(UUID.class, JDBCType.GUID),

INPUTSTREAM(InputStream.class, JDBCType.UNKNOWN) {
// InputStreams are either ASCII or binary
Expand Down Expand Up @@ -582,7 +584,9 @@ enum SetterConversionAE {
TIMESTAMP(JavaType.TIMESTAMP, EnumSet.of(JDBCType.TIME, // This is needed to send nanoseconds to the driver as
// setTime() is only milliseconds
JDBCType.TIMESTAMP, // This is datetime2
JDBCType.DATETIME, JDBCType.SMALLDATETIME));
JDBCType.DATETIME, JDBCType.SMALLDATETIME)),

UUID(JavaType.UUID, EnumSet.of(JDBCType.GUID));

private final EnumSet<JDBCType> to;
private final JavaType from;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,15 @@ else if ((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSe
op.execute(this, (Clob) value);
break;

case UUID:
if (null != cryptoMeta) {
byte[] bArray = Util.asGuidByteArray((UUID) value);
op.execute(this, bArray);
} else {
op.execute(this, (UUID) value);
}
break;

case INPUTSTREAM:
op.execute(this, (InputStream) value);
break;
Expand Down
141 changes: 115 additions & 26 deletions src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.logging.LogManager;

import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -103,6 +104,7 @@ enum ColumnType {
{"Nchar", "nchar(30) COLLATE Latin1_General_BIN2", "NCHAR"},
{"Nvarchar", "nvarchar(60) COLLATE Latin1_General_BIN2", "NCHAR"},
{"NvarcharMax", "nvarchar(max) COLLATE Latin1_General_BIN2", "LONGNVARCHAR"},
{"UniqueidentifierString", "uniqueidentifier", "GUIDSTRING"},
{"Uniqueidentifier", "uniqueidentifier", "GUID"},
{"Varchar8000", "varchar(8000) COLLATE Latin1_General_BIN2", "CHAR"},
{"Nvarchar4000", "nvarchar(4000) COLLATE Latin1_General_BIN2", "NCHAR"},};
Expand Down Expand Up @@ -451,7 +453,7 @@ protected static String[] createCharValues(boolean nullable) {
String nvarchar4000 = RandomData.generateNCharTypes("4000", nullable, encrypted);

String[] values = {char20.trim(), varchar50, varcharmax, nchar30, nvarchar60, nvarcharmax, Constants.UID,
varchar8000, nvarchar4000};
Constants.UID, varchar8000, nvarchar4000};

return values;
}
Expand Down Expand Up @@ -814,7 +816,7 @@ protected static void populateBinaryNullCase() throws SQLException {
*/
protected static void populateCharNormalCase(String[] charValues) throws SQLException {
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString, AEInfo);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
Expand Down Expand Up @@ -850,23 +852,34 @@ protected static void populateCharNormalCase(String[] charValues) throws SQLExce
pstmt.setNString(i, charValues[5]);
}

// uniqueidentifier
// uniqueidentifier as String
for (int i = 19; i <= 21; i++) {
if (null == charValues[6]) {
pstmt.setUniqueIdentifier(i, null);
} else {
pstmt.setUniqueIdentifier(i, Constants.UID);
pstmt.setUniqueIdentifier(i, charValues[6]);
}
}

// varchar8000
// uniqueidentifier
for (int i = 22; i <= 24; i++) {
pstmt.setString(i, charValues[7]);
if (null == charValues[7]) {
pstmt.setUniqueIdentifier(i, null);
} else {
// cannot override setUniqueIdentifier to accept UUID parameter without breaking compatibility
// falling back to testing UUID string parameter
pstmt.setUniqueIdentifier(i, charValues[7]);
}
}

// nvarchar4000
// varchar8000
for (int i = 25; i <= 27; i++) {
pstmt.setNString(i, charValues[8]);
pstmt.setString(i, charValues[8]);
}

// nvarchar4000
for (int i = 28; i <= 30; i++) {
pstmt.setNString(i, charValues[9]);
}

pstmt.execute();
Expand All @@ -881,7 +894,7 @@ protected static void populateCharNormalCase(String[] charValues) throws SQLExce
*/
protected static void populateCharSetObject(String[] charValues) throws SQLException {
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString, AEInfo);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
Expand Down Expand Up @@ -917,25 +930,97 @@ protected static void populateCharSetObject(String[] charValues) throws SQLExcep
pstmt.setObject(i, charValues[5], java.sql.Types.LONGNVARCHAR);
}

// uniqueidentifier
// uniqueidentifier as String
for (int i = 19; i <= 21; i++) {
pstmt.setObject(i, charValues[6], microsoft.sql.Types.GUID);
}

// varchar8000
// uniqueidentifier
for (int i = 22; i <= 24; i++) {
pstmt.setObject(i, charValues[7]);
pstmt.setObject(i, charValues[7] == null ? null : UUID.fromString(charValues[7]), microsoft.sql.Types.GUID);
}

// nvarchar4000
// varchar8000
for (int i = 25; i <= 27; i++) {
pstmt.setObject(i, charValues[8], java.sql.Types.NCHAR);
pstmt.setObject(i, charValues[8]);
}

// nvarchar4000
for (int i = 28; i <= 30; i++) {
pstmt.setObject(i, charValues[9], java.sql.Types.NCHAR);
}

pstmt.execute();
}
}

/**
* Populate char data with null data.
*
* @throws SQLException
*/
protected static void populateCharSetObjectNull() throws SQLException {
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString, AEInfo);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// char
for (int i = 1; i <= 3; i++) {
pstmt.setObject(i, null, java.sql.Types.CHAR);
}

// varchar
for (int i = 4; i <= 6; i++) {
pstmt.setObject(i, null, java.sql.Types.VARCHAR);
}

// varchar(max)
for (int i = 7; i <= 9; i++) {
pstmt.setObject(i, null, java.sql.Types.LONGVARCHAR);
}

// nchar
for (int i = 10; i <= 12; i++) {
pstmt.setObject(i, null, java.sql.Types.NCHAR);
}

// nvarchar
for (int i = 13; i <= 15; i++) {
pstmt.setObject(i, null, java.sql.Types.NVARCHAR);
}

// nvarchar(max)
for (int i = 16; i <= 18; i++) {
pstmt.setObject(i, null, java.sql.Types.LONGNVARCHAR);
}

// uniqueidentifier as String
for (int i = 19; i <= 21; i++) {
pstmt.setObject(i, null, microsoft.sql.Types.GUID);
}

// uniqueidentifier
for (int i = 22; i <= 24; i++) {
pstmt.setObject(i, null, microsoft.sql.Types.GUID);
}

// varchar8000
for (int i = 25; i <= 27; i++) {
pstmt.setObject(i, null, java.sql.Types.VARCHAR);
}

// nvarchar4000
for (int i = 28; i <= 30; i++) {
pstmt.setObject(i, null, java.sql.Types.NCHAR);
}

pstmt.execute();
}
}

/**
* Populate char data using set object with JDBC types.
*
Expand All @@ -944,7 +1029,7 @@ protected static void populateCharSetObject(String[] charValues) throws SQLExcep
*/
protected static void populateCharSetObjectWithJDBCTypes(String[] charValues) throws SQLException {
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString, AEInfo);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
Expand Down Expand Up @@ -980,19 +1065,24 @@ protected static void populateCharSetObjectWithJDBCTypes(String[] charValues) th
pstmt.setObject(i, charValues[5], JDBCType.LONGNVARCHAR);
}

// uniqueidentifier
// uniqueidentifier as String
for (int i = 19; i <= 21; i++) {
pstmt.setObject(i, charValues[6], microsoft.sql.Types.GUID);
}

// varchar8000
// uniqueidentifier
for (int i = 22; i <= 24; i++) {
pstmt.setObject(i, charValues[7], JDBCType.VARCHAR);
pstmt.setObject(i, UUID.fromString(charValues[7]), microsoft.sql.Types.GUID);
}

// vnarchar4000
// varchar8000
for (int i = 25; i <= 27; i++) {
pstmt.setObject(i, charValues[8], JDBCType.NVARCHAR);
pstmt.setObject(i, charValues[8], JDBCType.VARCHAR);
}

// vnarchar4000
for (int i = 28; i <= 30; i++) {
pstmt.setObject(i, charValues[9], JDBCType.NVARCHAR);
}

pstmt.execute();
Expand All @@ -1006,7 +1096,7 @@ protected static void populateCharSetObjectWithJDBCTypes(String[] charValues) th
*/
protected static void populateCharNullCase() throws SQLException {
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString, AEInfo);
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
Expand All @@ -1032,19 +1122,18 @@ protected static void populateCharNullCase() throws SQLException {
pstmt.setNull(i, java.sql.Types.NVARCHAR);
}

// uniqueidentifier
for (int i = 19; i <= 21; i++) {
// uniqueidentifier as String, uniqueidentifier
for (int i = 19; i <= 24; i++) {
pstmt.setNull(i, microsoft.sql.Types.GUID);

}

// varchar8000
for (int i = 22; i <= 24; i++) {
for (int i = 25; i <= 27; i++) {
pstmt.setNull(i, java.sql.Types.VARCHAR);
}

// nvarchar4000
for (int i = 25; i <= 27; i++) {
for (int i = 28; i <= 30; i++) {
pstmt.setNull(i, java.sql.Types.NVARCHAR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ private void testInputProcedure2(String sql) throws SQLException {
callableStatement.setNString(4, charValues[3]);
callableStatement.setNString(5, charValues[4]);
callableStatement.setNString(6, charValues[5]);
callableStatement.setString(7, charValues[7]);
callableStatement.setNString(8, charValues[8]);
callableStatement.setString(7, charValues[8]);
callableStatement.setNString(8, charValues[9]);

try (SQLServerResultSet rs = (SQLServerResultSet) callableStatement.executeQuery()) {
rs.next();
Expand All @@ -712,8 +712,8 @@ private void testInputProcedure2(String sql) throws SQLException {
assertEquals(rs.getString(4).trim(), charValues[3], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(5).trim(), charValues[4], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(6).trim(), charValues[5], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(7).trim(), charValues[7], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(8).trim(), charValues[8], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(7).trim(), charValues[8], TestResource.getResource("R_inputParamFailed"));
assertEquals(rs.getString(8).trim(), charValues[9], TestResource.getResource("R_inputParamFailed"));
}
} catch (Exception e) {
fail(e.getMessage());
Expand Down Expand Up @@ -1534,10 +1534,10 @@ private void testOutputProcedureCharInorder(String sql) throws SQLException {
assertEquals(nvarcharValuemax, charValues[5], TestResource.getResource("R_outputParamFailed"));

String varcharValue8000 = callableStatement.getString(8).trim();
assertEquals(varcharValue8000, charValues[7], TestResource.getResource("R_outputParamFailed"));
assertEquals(varcharValue8000, charValues[8], TestResource.getResource("R_outputParamFailed"));

String nvarcharValue4000 = callableStatement.getNString(9).trim();
assertEquals(nvarcharValue4000, charValues[8], TestResource.getResource("R_outputParamFailed"));
assertEquals(nvarcharValue4000, charValues[9], TestResource.getResource("R_outputParamFailed"));

} catch (Exception e) {
fail(e.getMessage());
Expand Down Expand Up @@ -1587,10 +1587,10 @@ private void testOutputProcedureCharInorderObject(String sql) throws SQLExceptio
assertEquals(nvarcharValuemax.trim(), charValues[5], TestResource.getResource("R_outputParamFailed"));

String varcharValue8000 = (String) callableStatement.getObject(8);
assertEquals(varcharValue8000, charValues[7], TestResource.getResource("R_outputParamFailed"));
assertEquals(varcharValue8000, charValues[8], TestResource.getResource("R_outputParamFailed"));

String nvarcharValue4000 = (String) callableStatement.getObject(9);
assertEquals(nvarcharValue4000, charValues[8], TestResource.getResource("R_outputParamFailed"));
assertEquals(nvarcharValue4000, charValues[9], TestResource.getResource("R_outputParamFailed"));

} catch (Exception e) {
fail(e.getMessage());
Expand Down
Loading
Loading