Skip to content

Commit 96c824d

Browse files
authored
Fixed timestamp zone format (#2497) (#2498)
* Fixed timestamp zone format * Fixed test, closed RS
1 parent 6a317fa commit 96c824d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/dtv.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,6 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
16141614
op.execute(this, ((Geometry) value).serialize());
16151615
} else if (JDBCType.GEOGRAPHY == jdbcType) {
16161616
op.execute(this, ((Geography) value).serialize());
1617-
} else if (JDBCType.TIMESTAMP == jdbcType) {
1618-
op.execute(this, Timestamp.valueOf((String) value));
16191617
} else {
16201618
if (null != cryptoMeta) {
16211619
// if streaming types check for allowed data length in AE

src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/PreparedStatementTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414
import java.lang.reflect.Field;
1515
import java.sql.BatchUpdateException;
16+
import java.sql.PreparedStatement;
1617
import java.sql.ResultSet;
1718
import java.sql.SQLException;
1819
import java.sql.Statement;
20+
import java.sql.Types;
21+
import java.text.SimpleDateFormat;
22+
import java.time.Instant;
23+
import java.util.Date;
1924
import java.util.Random;
2025
import java.util.UUID;
2126
import java.util.concurrent.CountDownLatch;
@@ -31,6 +36,7 @@
3136
import com.microsoft.sqlserver.jdbc.TestUtils;
3237

3338
import org.junit.jupiter.api.AfterEach;
39+
import org.junit.jupiter.api.Assertions;
3440
import org.junit.jupiter.api.BeforeAll;
3541
import org.junit.jupiter.api.BeforeEach;
3642
import org.junit.jupiter.api.Tag;
@@ -51,6 +57,7 @@ public class PreparedStatementTest extends AbstractTest {
5157
final static String tableName2 = RandomUtil.getIdentifier("tableTestStatementPoolingInternal2");
5258
final static String tableName3 = RandomUtil.getIdentifier("tableTestPreparedStatementWithSpPrepare");
5359
final static String tableName4 = RandomUtil.getIdentifier("tableTestPreparedStatementWithMultipleParams");
60+
final static String tableName5 = RandomUtil.getIdentifier("tableTestPreparedStatementWithTimestamp");
5461

5562
@BeforeAll
5663
public static void setupTests() throws Exception {
@@ -482,6 +489,43 @@ public void testPrepareRace() throws Exception {
482489
}
483490
}
484491

492+
@Test
493+
public void testTimestampStringTimeZoneFormat() throws SQLException {
494+
String SELECT_SQL = "SELECT id, created_date, deleted_date FROM "
495+
+ AbstractSQLGenerator.escapeIdentifier(tableName5) + " WHERE id = ?";
496+
String INSERT_SQL = "INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName5)
497+
+ " (id, created_date, deleted_date) VALUES (?, ?, ?)";
498+
String DATE_FORMAT_WITH_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
499+
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_WITH_Z);
500+
501+
try (SQLServerConnection con = (SQLServerConnection) getConnection()) {
502+
executeSQL(con, "create table " + AbstractSQLGenerator.escapeIdentifier(tableName5)
503+
+ "(id int, created_date datetime2, deleted_date datetime2)");
504+
}
505+
506+
try (PreparedStatement selectStatement = connection.prepareCall(SELECT_SQL);
507+
PreparedStatement insertStatement = connection.prepareCall(INSERT_SQL);) {
508+
Date createdDate = Date.from(Instant.parse("2024-01-16T05:12:00Z"));
509+
Date deletedDate = Date.from(Instant.parse("2024-01-16T06:34:00Z"));
510+
int id = 1;
511+
512+
insertStatement.setInt(1, id);
513+
insertStatement.setObject(2, sdf.format(createdDate.getTime()), Types.TIMESTAMP);
514+
insertStatement.setObject(3, sdf.format(deletedDate.getTime()), Types.TIMESTAMP);
515+
516+
insertStatement.executeUpdate();
517+
518+
selectStatement.setInt(1, id);
519+
520+
try (ResultSet result = selectStatement.executeQuery()) {
521+
result.next();
522+
Assertions.assertEquals(id, result.getInt("id"));
523+
Assertions.assertEquals(createdDate, new Date(result.getTimestamp("created_date").getTime()));
524+
Assertions.assertEquals(deletedDate, new Date(result.getTimestamp("deleted_date").getTime()));
525+
}
526+
}
527+
}
528+
485529
/**
486530
* Test handling of the two configuration knobs related to prepared statement handling.
487531
*
@@ -880,6 +924,7 @@ private static void dropTables() throws Exception {
880924
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName2), stmt);
881925
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName3), stmt);
882926
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName4), stmt);
927+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName5), stmt);
883928
}
884929
}
885930

0 commit comments

Comments
 (0)