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

Support for datetimeoffset sql_variant #1673

Merged
merged 2 commits into from
Jan 14, 2022
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
8 changes: 8 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,14 @@ private Object readSqlVariant(int intbaseType, int cbPropsActual, int valueLengt
convertedValue = tdsReader.readDateTime2(expectedValueLength, typeInfo, cal, jdbcType);
break;

case DATETIMEOFFSETN:
jdbcType = JDBCType.DATETIMEOFFSET;
scale = tdsReader.readUnsignedByte();
typeInfo.setScale(scale);
internalVariant.setScale(scale);
convertedValue = tdsReader.readDateTimeOffset(expectedValueLength, typeInfo, jdbcType);
break;

case BIGBINARY: // e.g binary20, binary 512, binary 8000 -> reads as bigbinary
case BIGVARBINARY:
if (cbPropsActual != sqlVariantProbBytes.BIGBINARY.getIntValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.sql.Statement;
import java.util.Arrays;

import microsoft.sql.DateTimeOffset;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -1026,22 +1027,18 @@ public void readVarcharInteger() throws SQLException {
}

/**
* Tests unsupported type
* Tests returning class of base type datetimeoffset in sql_variant is correct
*
* @throws SQLException
*/
@Test
public void testUnsupportedDatatype() throws SQLException {
public void testDateTimeOffsetAsSqlVariant() throws SQLException {
try (Connection con = getConnection(); Statement stmt = con.createStatement();
SQLServerResultSet 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(TestResource.getResource("R_expectedExceptionNotThrown"));
} catch (Exception e) {
assertTrue(e.getMessage().equalsIgnoreCase("Unexpected TDS type DATETIMEOFFSETN in SQL_VARIANT."));
}
Object object = rs.getObject(1);
assertEquals(object.getClass(), DateTimeOffset.class);
}
}

Expand Down