Skip to content

Commit 4b42bc3

Browse files
committed
Added CompatibilityMode.INVALID_DATE_AND_TIME_AS_ZERO (EventDeserializer) (shyiko#210)
1 parent 7f08e82 commit 4b42bc3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +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;
7576
private boolean microsecondsPrecision;
7677
private boolean deserializeCharAndBinaryAsByteArray;
7778

@@ -83,6 +84,10 @@ void setDeserializeDateAndTimeAsLong(boolean value) {
8384
this.deserializeDateAndTimeAsLong = value;
8485
}
8586

87+
void setDeserializeInvalidDateAndTimeAsZero(boolean value) {
88+
this.deserializeInvalidDateAndTimeAsZero = value;
89+
}
90+
8691
void setMicrosecondsPrecision(boolean value) {
8792
this.microsecondsPrecision = value;
8893
}
@@ -409,7 +414,7 @@ protected byte[] deserializeJson(int meta, ByteArrayInputStream inputStream) thr
409414
protected Long asUnixTime(int year, int month, int day, int hour, int minute, int second, int millis) {
410415
// https://dev.mysql.com/doc/refman/5.0/en/datetime.html
411416
if (year == 0 || month == 0 || day == 0) {
412-
return null;
417+
return deserializeInvalidDateAndTimeAsZero ? 0L : null;
413418
}
414419
return UnixTime.from(year, month, day, hour, minute, second, millis);
415420
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ private void ensureCompatibility(EventDataDeserializer eventDataDeserializer) {
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+
);
166169
deserializer.setDeserializeCharAndBinaryAsByteArray(
167170
compatibilitySet.contains(CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY)
168171
);
@@ -226,6 +229,7 @@ public EventDataDeserializer getEventDataDeserializer(EventType eventType) {
226229
/**
227230
* @see CompatibilityMode#DATE_AND_TIME_AS_LONG
228231
* @see CompatibilityMode#DATE_AND_TIME_AS_LONG_MICRO
232+
* @see CompatibilityMode#INVALID_DATE_AND_TIME_AS_ZERO
229233
* @see CompatibilityMode#CHAR_AND_BINARY_AS_BYTE_ARRAY
230234
*/
231235
public enum CompatibilityMode {
@@ -241,6 +245,13 @@ public enum CompatibilityMode {
241245
* Same as {@link CompatibilityMode#DATE_AND_TIME_AS_LONG} but values are returned in microseconds.
242246
*/
243247
DATE_AND_TIME_AS_LONG_MICRO,
248+
/**
249+
* Return 0 instead of null if year/month/day is 0.
250+
* Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2.
251+
*
252+
* <p>This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0.
253+
*/
254+
INVALID_DATE_AND_TIME_AS_ZERO,
244255
/**
245256
* Return CHAR/VARCHAR/BINARY/VARBINARY values as byte[]|s (instead of String|s).
246257
*

0 commit comments

Comments
 (0)