Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-312] NPE correction on resultset.getDate() on null timestamp wh…
…en using binary protocol
  • Loading branch information
rusher committed Jun 13, 2016
1 parent 2f9b528 commit b12536f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Expand Up @@ -878,15 +878,16 @@ private String getString(byte[] rawBytes, ColumnInformation columnInfo, Calendar
if (isBinaryEncoded) {
try {
Date date = getDate(rawBytes, columnInfo, cal);
return date == null ? null : date.toString();
return (date == null) ? null : date.toString();
} catch (ParseException e) {
}
}
break;
case YEAR:
if (options.yearIsDateType) {
try {
return getDate(rawBytes, columnInfo, cal).toString();
Date date = getDate(rawBytes, columnInfo, cal);
return (date == null) ? null : date.toString();
} catch (ParseException e) {
//eat exception
}
Expand All @@ -898,18 +899,19 @@ private String getString(byte[] rawBytes, ColumnInformation columnInfo, Calendar
case TIMESTAMP:
case DATETIME:
try {
return getTimestamp(rawBytes, columnInfo, cal).toString();
Timestamp timestamp = getTimestamp(rawBytes, columnInfo, cal);
return (timestamp == null) ? null : timestamp.toString();
} catch (ParseException e) {
}
break;
case DECIMAL:
return getBigDecimal(rawBytes, columnInfo).toString();
case OLDDECIMAL:
BigDecimal bigDecimal = getBigDecimal(rawBytes, columnInfo);
return (bigDecimal == null ) ? null : bigDecimal.toString();
case GEOMETRY:
return new String(rawBytes);
case NULL:
return null;
case OLDDECIMAL:
return getBigDecimal(rawBytes, columnInfo).toString();
default:
return new String(rawBytes, StandardCharsets.UTF_8);
}
Expand Down Expand Up @@ -3257,7 +3259,8 @@ private Date binaryDate(byte[] rawBytes, ColumnInformation columnInfo, Calendar
switch (columnInfo.getType()) {
case TIMESTAMP:
case DATETIME:
return new Date(getTimestamp(rawBytes, columnInfo, cal).getTime());
Timestamp timestamp = getTimestamp(rawBytes, columnInfo, cal);
return (timestamp == null) ? null : new Date(timestamp.getTime());
default:
if (rawBytes.length == 0) {
return null;
Expand Down Expand Up @@ -3305,7 +3308,7 @@ private Time binaryTime(byte[] rawBytes, ColumnInformation columnInfo, Calendar
case TIMESTAMP:
case DATETIME:
Timestamp ts = binaryTimestamp(rawBytes, columnInfo, cal);
return new Time(ts.getTime());
return (ts == null) ? null : new Time(ts.getTime());
case DATE:
Calendar tmpCalendar = Calendar.getInstance();
tmpCalendar.clear();
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/mariadb/jdbc/DateTest.java
Expand Up @@ -535,4 +535,35 @@ public void nullDateString() throws Throwable {
}
}

/**
* Conj-317 : null pointer exception on getDate on null timestamp.
*/
@Test
public void nullDateFromTimestamp() throws Throwable {

createTable("nulltimestamp", "ts timestamp(6) NULL ");
Statement stmt = sharedConnection.createStatement();
try {
stmt.execute("INSERT INTO nulltimestamp (ts) VALUES ('0000-00-00'), (null)");

PreparedStatement pst = sharedConnection.prepareStatement("SELECT * FROM nulltimestamp WHERE 1 = ?");
pst.setInt(1, 1);
ResultSet rs = pst.executeQuery();
Assert.assertTrue(rs.next());
Assert.assertNull(rs.getString(1));
Assert.assertNull(rs.getDate(1));
Assert.assertNull(rs.getTimestamp(1));
Assert.assertNull(rs.getTime(1));

Assert.assertTrue(rs.next());
Assert.assertNull(rs.getString(1));
Assert.assertNull(rs.getDate(1));
Assert.assertNull(rs.getTimestamp(1));
Assert.assertNull(rs.getTime(1));

} catch (SQLDataException sqldataException) {
//'0000-00-00' doesn't work anymore on mysql 5.7.
}
}

}

0 comments on commit b12536f

Please sign in to comment.