Skip to content
Permalink
Browse files

fix: avoid NPE from getObject(..., Date.class) and getObject(..., Cal…

…endar.class) on null timestamps

fixes #1071
  • Loading branch information
vlsi committed Jan 16, 2018
1 parent 32c5390 commit eb33c4c8e27d6df6ccd9c0a81eb119edeb069d55
@@ -3294,6 +3294,9 @@ public void updateArray(String columnName, Array x) throws SQLException {
//#endif
) {
Timestamp timestampValue = getTimestamp(columnIndex);
if (wasNull()) {
return null;
}
Calendar calendar = Calendar.getInstance(getDefaultCalendar().getTimeZone());
calendar.setTimeInMillis(timestampValue.getTime());
return type.cast(calendar);
@@ -3318,6 +3321,9 @@ public void updateArray(String columnName, Array x) throws SQLException {
} else if (type == java.util.Date.class) {
if (sqlType == Types.TIMESTAMP) {
Timestamp timestamp = getTimestamp(columnIndex);
if (wasNull()) {
return null;
}
return type.cast(new java.util.Date(timestamp.getTime()));
} else {
throw new PSQLException(GT.tr("conversion to {0} from {1} not supported", type, sqlType),
@@ -220,9 +220,8 @@ public void testGetTimestamp() throws SQLException {
@Test
public void testGetJavaUtilDate() throws SQLException {
Statement stmt = _conn.createStatement();
stmt.executeUpdate(TestUtil.insertSQL("table1","timestamp_without_time_zone_column","TIMESTAMP '2004-10-19 10:23:54'"));

ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "timestamp_without_time_zone_column"));
ResultSet rs = stmt.executeQuery("select TIMESTAMP '2004-10-19 10:23:54'::timestamp timestamp_without_time_zone_column"
+ ", null::timestamp null_timestamp");
try {
assertTrue(rs.next());
Calendar calendar = GregorianCalendar.getInstance();
@@ -236,6 +235,7 @@ public void testGetJavaUtilDate() throws SQLException {
java.util.Date expected = new java.util.Date(calendar.getTimeInMillis());
assertEquals(expected, rs.getObject("timestamp_without_time_zone_column", java.util.Date.class));
assertEquals(expected, rs.getObject(1, java.util.Date.class));
assertNull(rs.getObject(2, java.util.Date.class));
} finally {
rs.close();
}
@@ -287,9 +287,9 @@ private void runGetTimestampWithTimeZone(TimeZone timeZone, String zoneString) t
@Test
public void testGetCalendar() throws SQLException {
Statement stmt = _conn.createStatement();
stmt.executeUpdate(TestUtil.insertSQL("table1","timestamp_without_time_zone_column,timestamp_with_time_zone_column","TIMESTAMP '2004-10-19 10:23:54', TIMESTAMP '2004-10-19 10:23:54+02'"));

ResultSet rs = stmt.executeQuery(TestUtil.selectSQL("table1", "timestamp_without_time_zone_column, timestamp_with_time_zone_column"));
ResultSet rs = stmt.executeQuery("select TIMESTAMP '2004-10-19 10:23:54'::timestamp timestamp_without_time_zone_column"
+ ", TIMESTAMP '2004-10-19 10:23:54+02'::timestamp timestamp_with_time_zone_column, null::timestamp null_timestamp");
try {
assertTrue(rs.next());
Calendar calendar = GregorianCalendar.getInstance();
@@ -303,10 +303,12 @@ public void testGetCalendar() throws SQLException {
long expected = calendar.getTimeInMillis();
assertEquals(expected, rs.getObject("timestamp_without_time_zone_column", Calendar.class).getTimeInMillis());
assertEquals(expected, rs.getObject(1, Calendar.class).getTimeInMillis());
assertNull(rs.getObject(3, Calendar.class));
calendar.setTimeZone(TimeZone.getTimeZone("GMT+2:00"));
expected = calendar.getTimeInMillis();
assertEquals(expected, rs.getObject("timestamp_with_time_zone_column", Calendar.class).getTimeInMillis());
assertEquals(expected, rs.getObject(2, Calendar.class).getTimeInMillis());
assertNull(rs.getObject(3, Calendar.class));
} finally {
rs.close();
}

0 comments on commit eb33c4c

Please sign in to comment.
You can’t perform that action at this time.