Skip to content

Commit 2530a83

Browse files
committed
Added CompatibilityMode.INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE (EventDeserializer)
1 parent 4b42bc3 commit 2530a83

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public abstract class AbstractRowsEventDataDeserializer<T extends EventData> imp
7272
private final Map<Long, TableMapEventData> tableMapEventByTableId;
7373

7474
private boolean deserializeDateAndTimeAsLong;
75-
private boolean deserializeInvalidDateAndTimeAsZero;
75+
private Long invalidDateAndTimeRepresentation;
7676
private boolean microsecondsPrecision;
7777
private boolean deserializeCharAndBinaryAsByteArray;
7878

@@ -84,8 +84,9 @@ void setDeserializeDateAndTimeAsLong(boolean value) {
8484
this.deserializeDateAndTimeAsLong = value;
8585
}
8686

87-
void setDeserializeInvalidDateAndTimeAsZero(boolean value) {
88-
this.deserializeInvalidDateAndTimeAsZero = value;
87+
// value to return in case of 0000-00-00 00:00:00, 0000-00-00, etc.
88+
void setInvalidDateAndTimeRepresentation(Long value) {
89+
this.invalidDateAndTimeRepresentation = value;
8990
}
9091

9192
void setMicrosecondsPrecision(boolean value) {
@@ -238,7 +239,7 @@ protected Serializable deserializeNewDecimal(int meta, ByteArrayInputStream inpu
238239
}
239240

240241
private Long castTimestamp(Long timestamp, int fsp) {
241-
if (timestamp != null && microsecondsPrecision) {
242+
if (microsecondsPrecision && timestamp != null && timestamp > -1) {
242243
return timestamp * 1000 + fsp % 1000;
243244
}
244245
return timestamp;
@@ -414,7 +415,7 @@ protected byte[] deserializeJson(int meta, ByteArrayInputStream inputStream) thr
414415
protected Long asUnixTime(int year, int month, int day, int hour, int minute, int second, int millis) {
415416
// https://dev.mysql.com/doc/refman/5.0/en/datetime.html
416417
if (year == 0 || month == 0 || day == 0) {
417-
return deserializeInvalidDateAndTimeAsZero ? 0L : null;
418+
return invalidDateAndTimeRepresentation;
418419
}
419420
return UnixTime.from(year, month, day, hour, minute, second, millis);
420421
}

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,23 @@ private void ensureCompatibility(EventDataDeserializer eventDataDeserializer) {
156156
if (eventDataDeserializer instanceof AbstractRowsEventDataDeserializer) {
157157
AbstractRowsEventDataDeserializer deserializer =
158158
(AbstractRowsEventDataDeserializer) eventDataDeserializer;
159-
deserializer.setDeserializeDateAndTimeAsLong(
159+
boolean deserializeDateAndTimeAsLong =
160160
compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG) ||
161-
compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO)
162-
);
161+
compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO);
162+
deserializer.setDeserializeDateAndTimeAsLong(deserializeDateAndTimeAsLong);
163163
deserializer.setMicrosecondsPrecision(
164164
compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO)
165165
);
166-
deserializer.setDeserializeInvalidDateAndTimeAsZero(
167-
compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_ZERO)
168-
);
166+
if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_ZERO)) {
167+
deserializer.setInvalidDateAndTimeRepresentation(0L);
168+
}
169+
if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE)) {
170+
if (!deserializeDateAndTimeAsLong) {
171+
throw new IllegalArgumentException("INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE requires " +
172+
"DATE_AND_TIME_AS_LONG or DATE_AND_TIME_AS_LONG_MICRO");
173+
}
174+
deserializer.setInvalidDateAndTimeRepresentation(-1L);
175+
}
169176
deserializer.setDeserializeCharAndBinaryAsByteArray(
170177
compatibilitySet.contains(CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY)
171178
);
@@ -248,10 +255,15 @@ public enum CompatibilityMode {
248255
/**
249256
* Return 0 instead of null if year/month/day is 0.
250257
* Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2.
258+
*/
259+
INVALID_DATE_AND_TIME_AS_ZERO,
260+
/**
261+
* Return -1 instead of null if year/month/day is 0.
262+
* Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2.
251263
*
252264
* <p>This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0.
253265
*/
254-
INVALID_DATE_AND_TIME_AS_ZERO,
266+
INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE,
255267
/**
256268
* Return CHAR/VARCHAR/BINARY/VARBINARY values as byte[]|s (instead of String|s).
257269
*

0 commit comments

Comments
 (0)