Skip to content

Commit

Permalink
Fixed timestamp zone format
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyc committed Aug 22, 2024
1 parent c05521e commit 457f27d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -1614,8 +1614,6 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
op.execute(this, ((Geometry) value).serialize());
} else if (JDBCType.GEOGRAPHY == jdbcType) {
op.execute(this, ((Geography) value).serialize());
} else if (JDBCType.TIMESTAMP == jdbcType) {
op.execute(this, Timestamp.valueOf((String) value));
} else {
if (null != cryptoMeta) {
// if streaming types check for allowed data length in AE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

import java.lang.reflect.Field;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
Expand All @@ -31,6 +36,7 @@
import com.microsoft.sqlserver.jdbc.TestUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
Expand All @@ -51,6 +57,7 @@ public class PreparedStatementTest extends AbstractTest {
final static String tableName2 = RandomUtil.getIdentifier("tableTestStatementPoolingInternal2");
final static String tableName3 = RandomUtil.getIdentifier("tableTestPreparedStatementWithSpPrepare");
final static String tableName4 = RandomUtil.getIdentifier("tableTestPreparedStatementWithMultipleParams");
final static String tableName5 = RandomUtil.getIdentifier("tableTestPreparedStatementWithTimestamp");

@BeforeAll
public static void setupTests() throws Exception {
Expand Down Expand Up @@ -482,6 +489,43 @@ public void testPrepareRace() throws Exception {
}
}

@Test
public void testTimestampStringTimeZoneFormat() throws SQLException {
String SELECT_SQL = "SELECT id, created_date, deleted_date FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName5) + " WHERE id = ?";
String INSERT_SQL = "INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName5)
+ " (id, created_date, deleted_date) VALUES (?, ?, ?)";
String DATE_FORMAT_WITH_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_WITH_Z);

try (SQLServerConnection con = (SQLServerConnection) getConnection()) {
executeSQL(con, "create table " + AbstractSQLGenerator.escapeIdentifier(tableName5)
+ "(id int, created_date datetime2, deleted_date datetime2)");
}

try (PreparedStatement selectStatement = connection.prepareCall(SELECT_SQL);
PreparedStatement insertStatement = connection.prepareCall(INSERT_SQL);) {
Date createdDate = Date.from(Instant.parse("2024-01-16T05:12:00Z"));
Date deletedDate = Date.from(Instant.parse("2024-01-16T06:34:00Z"));
int id = 1;

insertStatement.setInt(1, id);
insertStatement.setObject(2, sdf.format(createdDate.getTime()), Types.TIMESTAMP);
insertStatement.setObject(3, sdf.format(deletedDate.getTime()), Types.TIMESTAMP);

insertStatement.executeUpdate();

selectStatement.setInt(1, id);

ResultSet result = selectStatement.executeQuery();
result.next();

Assertions.assertEquals(id, result.getInt("id"));
Assertions.assertEquals(createdDate, new Date(result.getTimestamp("created_date").getTime()));
Assertions.assertEquals(deletedDate, new Date(result.getTimestamp("deleted_date").getTime()));
}
}

/**
* Test handling of the two configuration knobs related to prepared statement handling.
*
Expand Down Expand Up @@ -880,6 +924,7 @@ private static void dropTables() throws Exception {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName2), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName3), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName4), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName5), stmt);
}
}

Expand Down

0 comments on commit 457f27d

Please sign in to comment.