From 9d0486c9bc64fe865fe9f3b975aa51aaa581c952 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 14 Aug 2017 09:18:42 -0700 Subject: [PATCH 1/3] fixed issue with sql_variant --- src/main/java/com/microsoft/sqlserver/jdbc/dtv.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java index 5030c22c9..adb1fb67b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java @@ -4179,7 +4179,6 @@ else if (TDSType.BIGCHAR == baseType) jdbcType = JDBCType.CHAR; collation = tdsReader.readCollation(); typeInfo.setSQLCollation(collation); - typeInfo.setSSLenType(SSLenType.USHORTLENTYPE); maxLength = tdsReader.readUnsignedShort(); typeInfo.setMaxLength(maxLength); if (maxLength > DataTypes.SHORT_VARTYPE_MAX_BYTES) @@ -4205,7 +4204,6 @@ else if (TDSType.NVARCHAR == baseType) jdbcType = JDBCType.NVARCHAR; collation = tdsReader.readCollation(); typeInfo.setSQLCollation(collation); - typeInfo.setSSLenType(SSLenType.USHORTLENTYPE); maxLength = tdsReader.readUnsignedShort(); if (maxLength > DataTypes.SHORT_VARTYPE_MAX_BYTES || 0 != maxLength % 2) tdsReader.throwInvalidTDS(); From dd74c9d87dd9c7753af267fec4def2d77352a595 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Tue, 15 Aug 2017 12:40:48 -0700 Subject: [PATCH 2/3] do not override the maxlength type in sql_variant when reading the values --- src/main/java/com/microsoft/sqlserver/jdbc/dtv.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java index adb1fb67b..f9d245dbf 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/dtv.java @@ -3104,7 +3104,7 @@ public void apply(TypeInfo typeInfo, */ public void apply(TypeInfo typeInfo, TDSReader tdsReader) throws SQLServerException { - typeInfo.ssLenType = SSLenType.LONGLENTYPE; //Variant type should be LONGLENTYPE length. + typeInfo.ssLenType = SSLenType.LONGLENTYPE; //sql_variant type should be LONGLENTYPE length. typeInfo.maxLength = tdsReader.readInt(); typeInfo.ssType = SSType.SQL_VARIANT; } @@ -4115,7 +4115,6 @@ else if (TDSType.NUMERICN == baseType) case MONEY4: jdbcType = JDBCType.SMALLMONEY; - typeInfo.setMaxLength(4); precision = Long.toString(Long.MAX_VALUE).length(); typeInfo.setPrecision(precision); scale = 4; @@ -4128,7 +4127,6 @@ else if (TDSType.NUMERICN == baseType) case MONEY8: jdbcType = JDBCType.MONEY; - typeInfo.setMaxLength(8); precision = Long.toString(Long.MAX_VALUE).length(); scale = 4; typeInfo.setPrecision(precision); @@ -4180,7 +4178,6 @@ else if (TDSType.BIGCHAR == baseType) collation = tdsReader.readCollation(); typeInfo.setSQLCollation(collation); maxLength = tdsReader.readUnsignedShort(); - typeInfo.setMaxLength(maxLength); if (maxLength > DataTypes.SHORT_VARTYPE_MAX_BYTES) tdsReader.throwInvalidTDS(); typeInfo.setDisplaySize(maxLength); @@ -4270,7 +4267,6 @@ else if (TDSType.BIGVARBINARY == baseType) jdbcType = JDBCType.VARBINARY; maxLength = tdsReader.readUnsignedShort(); internalVariant.setMaxLength(maxLength); - typeInfo.setMaxLength(maxLength); if (maxLength > DataTypes.SHORT_VARTYPE_MAX_BYTES) tdsReader.throwInvalidTDS(); typeInfo.setDisplaySize(2 * maxLength); From e2075715f661dfa96907e7c60c0b7a51489e9fe8 Mon Sep 17 00:00:00 2001 From: Afsaneh Rafighi Date: Mon, 28 Aug 2017 21:41:01 -0700 Subject: [PATCH 3/3] added test --- .../jdbc/datatypes/SQLVariantResultSetTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLVariantResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLVariantResultSetTest.java index bf14063a2..dc076f9de 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLVariantResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLVariantResultSetTest.java @@ -803,6 +803,20 @@ public void readSeveralRows() throws SQLException { } } + + /** + * Test retrieving values with varchar and integer as basetype + * @throws SQLException + */ + @Test + public void readVarcharInteger() throws SQLException { + Object expected[] = {"abc", 42}; + int index = 0; + rs = (SQLServerResultSet) stmt.executeQuery("SELECT cast('abc' as sql_variant) UNION ALL SELECT cast(42 as sql_variant)"); + while (rs.next()) { + assertEquals(rs.getObject(1), expected[index++]); + } + } private boolean parseByte(byte[] expectedData, byte[] retrieved) {