Skip to content

Commit

Permalink
Fix for Bug#116114 (Bug#37067812), Connector/J is writing incorrect v…
Browse files Browse the repository at this point in the history
…alues when passed negative dates.

Change-Id: Id3de67de0ca299492e70ee4c353f10cccf3ced2f
  • Loading branch information
Axyoan Marcelo committed Sep 18, 2024
1 parent 03dedd7 commit 9af2df7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Version 9.1.0

- Fix for Bug#116114 (Bug#37067812), Connector/J is writing incorrect values when passed negative dates.

- Fix for Bug#96623 (Bug#30221117), batch update with rewriteBatchedStatements&useServerPrepStmts send fail request.

- Fix for Bug#114705 (Bug#36539680), Contribution: make trustStorePassword be null if this.trustStoreSettings.keyStorePassword is null.
Expand Down
14 changes: 7 additions & 7 deletions src/main/core-api/java/com/mysql/cj/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public class TimeUtil {
public static final LocalDate DEFAULT_DATE = LocalDate.of(1970, 1, 1);
public static final LocalTime DEFAULT_TIME = LocalTime.of(0, 0);

public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd");
public static final DateTimeFormatter TIME_FORMATTER_NO_FRACT_NO_OFFSET = DateTimeFormatter.ofPattern("HH:mm:ss");
public static final DateTimeFormatter TIME_FORMATTER_WITH_NANOS_NO_OFFSET = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSS");
public static final DateTimeFormatter TIME_FORMATTER_NO_FRACT_WITH_OFFSET = DateTimeFormatter.ofPattern("HH:mm:ssXXX");
public static final DateTimeFormatter TIME_FORMATTER_WITH_NANOS_WITH_OFFSET = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSSXXX");
public static final DateTimeFormatter TIME_FORMATTER_WITH_OPTIONAL_MICROS = new DateTimeFormatterBuilder().appendPattern("HH:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true).toFormatter();
public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_NO_OFFSET = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");
public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssXXX");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSSSSXXX");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_OPTIONAL_MICROS = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd HH:mm:ss")
public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_NO_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSSSS");
public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssXXX");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSSSSXXX");
public static final DateTimeFormatter DATETIME_FORMATTER_WITH_OPTIONAL_MICROS = new DateTimeFormatterBuilder().appendPattern("uuuu-MM-dd HH:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true).toFormatter();

public static final Pattern DATE_LITERAL_WITH_DELIMITERS = Pattern
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/testsuite/regression/StatementRegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
import com.mysql.cj.jdbc.StatementImpl;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import com.mysql.cj.jdbc.exceptions.MySQLTimeoutException;
import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import com.mysql.cj.jdbc.ha.ReplicationConnection;
import com.mysql.cj.jdbc.interceptors.ResultSetScannerInterceptor;
import com.mysql.cj.jdbc.result.CachedResultSetMetaData;
Expand Down Expand Up @@ -13987,4 +13988,26 @@ public SQLException interceptException(Exception sqlEx) {

}

/**
* Tests fix for Bug#116114 (Bug#37067812), Connector/J is writing incorrect values when passed negative dates.
*
* @throws Exception
*/
@Test
public void testBug116114() throws Exception {
createTable("testBug116114", "(d DATE, dt DATETIME)");

LocalDate negativeDate = LocalDate.of(-1, 1, 1);
this.pstmt = this.conn.prepareStatement("INSERT INTO testBug116114 (d) VALUES (?)");
this.pstmt.setObject(1, negativeDate);
assertThrows(MysqlDataTruncation.class, this.pstmt::executeUpdate);
assertThrows(SQLSyntaxErrorException.class, () -> this.stmt.executeUpdate("INSERT INTO testBug116114 (d) VALUES '-0001-01-01'"));

LocalDateTime negativeDateTime = LocalDateTime.of(-1, 1, 1, 1, 1, 1);
this.pstmt = this.conn.prepareStatement("INSERT INTO testBug116114 (dt) VALUES (?)");
this.pstmt.setObject(1, negativeDateTime);
assertThrows(MysqlDataTruncation.class, this.pstmt::executeUpdate);
assertThrows(SQLSyntaxErrorException.class, () -> this.stmt.executeUpdate("INSERT INTO testBug116114 (dt) VALUES '-0001-01-01 01:01:01'"));
}

}

0 comments on commit 9af2df7

Please sign in to comment.