Skip to content

Commit

Permalink
[CONJ-1196] setObject on java.util.Date was considered was a java.sql…
Browse files Browse the repository at this point in the history
….Date and truncate hour/minutes/seconds/ms while it must be considered like a java.sql.Timestamp
  • Loading branch information
rusher committed Nov 6, 2024
1 parent 472581d commit eae949a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean canDecode(ColumnDecoder column, Class<?> type) {
}

public boolean canEncode(Object value) {
return value instanceof Date || java.util.Date.class.equals(value.getClass());
return value instanceof Date;
}

@Override
Expand Down
38 changes: 27 additions & 11 deletions src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public boolean canDecode(ColumnDecoder column, Class<?> type) {
}

public boolean canEncode(Object value) {
return value instanceof Timestamp;
return value instanceof Timestamp || java.util.Date.class.equals(value.getClass());
}

@Override
Expand Down Expand Up @@ -76,16 +76,20 @@ public Timestamp decodeBinary(
public void encodeText(
Writer encoder, Context context, Object val, Calendar providedCal, Long maxLen)
throws IOException {
Timestamp ts = (Timestamp) val;
Calendar cal = providedCal == null ? context.getDefaultCalendar() : providedCal;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(cal.getTimeZone());
String dateString = sdf.format(ts);
String dateString = sdf.format(val);

encoder.writeByte('\'');
encoder.writeAscii(dateString);
int microseconds = 0;
if (val instanceof Timestamp) {
microseconds = ((Timestamp) val).getNanos() / 1000;
} else if (val instanceof java.util.Date) {
microseconds = (int) ((((java.util.Date) val).getTime() % 1000) * 1000);
}

int microseconds = ts.getNanos() / 1000;
if (microseconds > 0) {
if (microseconds % 1000 == 0) {
encoder.writeAscii("." + Integer.toString(microseconds / 1000 + 1000).substring(1));
Expand All @@ -101,12 +105,24 @@ public void encodeText(
public void encodeBinary(
Writer encoder, Context context, Object value, Calendar providedCal, Long maxLength)
throws IOException {
Timestamp ts = (Timestamp) value;

int microseconds = 0;
long timeInMillis = 0;
if (value instanceof Timestamp) {
Timestamp ts = (Timestamp) value;
microseconds = ts.getNanos() / 1000;
timeInMillis = ts.getTime();
} else if (value instanceof java.util.Date) {
java.util.Date dt = (java.util.Date) value;
timeInMillis = dt.getTime();
microseconds = (int) ((timeInMillis % 1000) * 1000);
}

if (providedCal == null) {
Calendar cal = context.getDefaultCalendar();
cal.clear();
cal.setTimeInMillis(ts.getTime());
if (ts.getNanos() == 0) {
cal.setTimeInMillis(timeInMillis);
if (microseconds == 0) {
encoder.writeByte(7); // length
encoder.writeShort((short) cal.get(Calendar.YEAR));
encoder.writeByte((cal.get(Calendar.MONTH) + 1));
Expand All @@ -122,13 +138,13 @@ public void encodeBinary(
encoder.writeByte(cal.get(Calendar.HOUR_OF_DAY));
encoder.writeByte(cal.get(Calendar.MINUTE));
encoder.writeByte(cal.get(Calendar.SECOND));
encoder.writeInt(ts.getNanos() / 1000);
encoder.writeInt(microseconds);
}
} else {
synchronized (providedCal) {
providedCal.clear();
providedCal.setTimeInMillis(ts.getTime());
if (ts.getNanos() == 0) {
providedCal.setTimeInMillis(timeInMillis);
if (microseconds == 0) {
encoder.writeByte(7); // length
encoder.writeShort((short) providedCal.get(Calendar.YEAR));
encoder.writeByte((providedCal.get(Calendar.MONTH) + 1));
Expand All @@ -144,7 +160,7 @@ public void encodeBinary(
encoder.writeByte(providedCal.get(Calendar.HOUR_OF_DAY));
encoder.writeByte(providedCal.get(Calendar.MINUTE));
encoder.writeByte(providedCal.get(Calendar.SECOND));
encoder.writeInt(ts.getNanos() / 1000);
encoder.writeInt(microseconds);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ private void sendParam(Connection con) throws SQLException {
prep.execute();
prep.setTimestamp(1, Timestamp.valueOf("2015-12-12 01:55:12.654"));
prep.execute();
prep.setObject(1, Timestamp.valueOf("2016-12-12 01:55:12"));
prep.setObject(1, new java.util.Date(Timestamp.valueOf("2016-12-18 01:55:12.2").getTime()));
prep.execute();
prep.setObject(1, Timestamp.valueOf("2016-12-12 01:55:12.654"));
prep.execute();
Expand Down Expand Up @@ -1624,9 +1624,12 @@ private void sendParam(Connection con) throws SQLException {
assertTrue(rs.next());
assertEquals(Timestamp.valueOf("2015-12-12 01:55:12.654"), rs.getTimestamp(2));
assertTrue(rs.next());
assertEquals(Timestamp.valueOf("2016-12-12 01:55:12"), rs.getTimestamp(2));
assertEquals(Timestamp.valueOf("2016-12-18 01:55:12.2"), rs.getTimestamp(2));
assertTrue(rs.next());
assertEquals(Timestamp.valueOf("2016-12-12 01:55:12.654"), rs.getTimestamp(2));
assertEquals(
new java.util.Date(Timestamp.valueOf("2016-12-12 01:55:12.654").getTime()),
rs.getObject(2, java.util.Date.class));
assertTrue(rs.next());
assertEquals(Timestamp.from(Instant.ofEpochSecond(10, 654000)), rs.getTimestamp(2));
assertTrue(rs.next());
Expand Down

0 comments on commit eae949a

Please sign in to comment.