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

Fixed issue with throwing error message for unsupported datatype. #450

Merged
merged 7 commits into from
Aug 29, 2017
3 changes: 2 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ static final Object convertTemporalToObject(JDBCType jdbcType,
}
// Convert the calendar value (in local time) to the desired Java object type.
switch (jdbcType.category) {
case BINARY: {
case BINARY:
case SQL_VARIANT: {
switch (ssType) {
case DATE: {
// Per JDBC spec, the time part of java.sql.Date values is initialized to midnight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,12 @@ protected Object[][] getContents() {
{"R_jaasConfigurationNamePropertyDescription", "Login configuration file for Kerberos authentication."},
{"R_AKVKeyNotFound", "Key not found: {0}"},
{"R_SQLVariantSupport", "SQL_VARIANT datatype is not supported in pre-SQL 2008 version."},
{"R_invalidProbbytes", "SQL_VARIANT: invalid probBytes for {0} type."},
{"R_invalidStringValue", "SQL_VARIANT does not support string values more than 8000 length."},
{"R_invalidValueForTVPWithSQLVariant", "Inserting null value with column type sql_variant in TVP is not supported."},
{"R_sslProtocolPropertyDescription", "SSL protocol label from TLS, TLSv1, TLSv1.1 & TLSv1.2. The default is TLS."},
{"R_invalidSSLProtocol", "SSL Protocol {0} label is not valid. Only TLS, TLSv1, TLSv1.1 & TLSv1.2 are supported."},
{"R_invalidProbbytes", "SQL_VARIANT: invalid probBytes for {0} type."},
{"R_invalidStringValue", "SQL_VARIANT does not support string values more than 8000 length."},
{"R_invalidValueForTVPWithSQLVariant", "Inserting null value with column type sql_variant in TVP is not supported."},
{"R_invalidDataTypeSupportForSQLVariant", "Unexpected TDS type ' '{0}' ' in SQL_VARIANT."},
{"R_sslProtocolPropertyDescription", "SSL protocol label from TLS, TLSv1, TLSv1.1 & TLSv1.2. The default is TLS."},
{"R_invalidSSLProtocol", "SSL Protocol {0} label is not valid. Only TLS, TLSv1, TLSv1.1 & TLSv1.2 are supported."},

};
}
11 changes: 6 additions & 5 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -4238,7 +4238,6 @@ else if (TDSType.NVARCHAR == baseType)
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidProbbytes"));
throw new SQLServerException(form.format(new Object[] {baseType}), null, 0, null);
}
jdbcType = JDBCType.CHAR; // The reason we use char is to return nanoseconds
if (internalVariant.isBaseTypeTimeValue()) {
jdbcType = JDBCType.TIMESTAMP;
}
Expand Down Expand Up @@ -4290,10 +4289,12 @@ else if (TDSType.BIGVARBINARY == baseType)
convertedValue = tdsReader.readGUID(expectedValueLength, jdbcType, streamGetterArgs.streamType);
break;

// Unknown SSType should have already been rejected by TypeInfo.setFromTDS()
default:
assert false : "Unexpected TDSType in Sql-Variant " + baseType;
break;
// Unsupported TdsType should throw error message
default: {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidDataTypeSupportForSQLVariant"));
throw new SQLServerException(form.format(new Object[] {baseType}), null, 0, null);
}

}
return convertedValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public void bulkCopyTestTime() throws SQLException {

rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
rs.next();
assertEquals("" + rs.getObject(1).toString(), "12:26:27.15"); // TODO
assertEquals("" + rs.getObject(1).toString(), "12:26:27");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void readTime() throws SQLException {
createAndPopulateTable("time(3)", value);
rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
rs.next();
assertEquals("" + rs.getObject(1).toString(), "12:26:27.123"); // TODO
assertEquals("" + rs.getObject(1).toString(), "12:26:27");
}

/**
Expand Down Expand Up @@ -640,7 +641,7 @@ public void callableStatementOutputDateTest() throws SQLException {
@Test
public void callableStatementOutputTimeTest() throws SQLException {
String value = "12:26:27.123345";
String returnValue = "12:26:27.123";
String returnValue = "12:26:27";
Utils.dropTableIfExists(tableName, stmt);
stmt.executeUpdate("create table " + tableName + " (col1 sql_variant)");
stmt.executeUpdate("INSERT into " + tableName + " values (CAST ('" + value + "' AS " + "time(3)" + "))");
Expand All @@ -652,7 +653,7 @@ public void callableStatementOutputTimeTest() throws SQLException {
CallableStatement cs = con.prepareCall(" {call " + inputProc + " (?) }");
cs.registerOutParameter(1, microsoft.sql.Types.SQL_VARIANT, 3);
cs.execute();
assertEquals(cs.getString(1), String.valueOf(returnValue));
assertEquals(String.valueOf(returnValue), "" + cs.getObject(1));
if (null != cs) {
cs.close();
}
Expand Down Expand Up @@ -804,6 +805,42 @@ public void readSeveralRows() throws SQLException {

}

/**
* Tests unsupported type
*
* @throws SQLException
*/
@Test
public void testUnsupportedDatatype() throws SQLException {
rs = (SQLServerResultSet) stmt.executeQuery("select cast(cast('2017-08-16 17:31:09.995 +07:00' as datetimeoffset) as sql_variant)");
rs.next();
try {
rs.getObject(1);
fail("Should have thrown unssuported tds type exception");
}
catch (Exception e) {
assertTrue(e.getMessage().equalsIgnoreCase("Unexpected TDS type DATETIMEOFFSETN in SQL_VARIANT."));
}
if (null != rs) {
rs.close();
}
}

/**
* Tests that the returning class of base type time in sql_variant is correct.
*
* @throws SQLException
*
*/
@Test
public void testTimeClassAsSqlVariant() throws SQLException {
rs = (SQLServerResultSet) stmt.executeQuery("select cast(cast('17:31:09.995' as time(3)) as sql_variant)");
rs.next();
Object object = rs.getObject(1);
assertEquals(object.getClass(), java.sql.Time.class);
;
}

private boolean parseByte(byte[] expectedData,
byte[] retrieved) {
assertTrue(Arrays.equals(expectedData, Arrays.copyOf(retrieved, expectedData.length)), " unexpected BINARY value, expected");
Expand Down