Skip to content

Commit 30cb49b

Browse files
committed
Merge branch 'pr/21'
2 parents 9e4208c + 712ce17 commit 30cb49b

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public abstract class AbstractRowsEventDataDeserializer<T extends EventData> imp
7575
private Long invalidDateAndTimeRepresentation;
7676
private boolean microsecondsPrecision;
7777
private boolean deserializeCharAndBinaryAsByteArray;
78+
private boolean deserializeIntegerAsByteArray;
7879

7980
public AbstractRowsEventDataDeserializer(Map<Long, TableMapEventData> tableMapEventByTableId) {
8081
this.tableMapEventByTableId = tableMapEventByTableId;
@@ -97,6 +98,10 @@ void setDeserializeCharAndBinaryAsByteArray(boolean value) {
9798
this.deserializeCharAndBinaryAsByteArray = value;
9899
}
99100

101+
void setDeserializeIntegerAsByteArray(boolean deserializeIntegerAsByteArray) {
102+
this.deserializeIntegerAsByteArray = deserializeIntegerAsByteArray;
103+
}
104+
100105
protected Serializable[] deserializeRow(long tableId, BitSet includedColumns, ByteArrayInputStream inputStream)
101106
throws IOException {
102107
TableMapEventData tableMapEvent = tableMapEventByTableId.get(tableId);
@@ -203,22 +208,37 @@ protected Serializable deserializeBit(int meta, ByteArrayInputStream inputStream
203208
}
204209

205210
protected Serializable deserializeTiny(ByteArrayInputStream inputStream) throws IOException {
211+
if (deserializeIntegerAsByteArray) {
212+
return inputStream.read(1);
213+
}
206214
return (int) ((byte) inputStream.readInteger(1));
207215
}
208216

209217
protected Serializable deserializeShort(ByteArrayInputStream inputStream) throws IOException {
218+
if (deserializeIntegerAsByteArray) {
219+
return inputStream.read(2);
220+
}
210221
return (int) ((short) inputStream.readInteger(2));
211222
}
212223

213224
protected Serializable deserializeInt24(ByteArrayInputStream inputStream) throws IOException {
225+
if (deserializeIntegerAsByteArray) {
226+
return inputStream.read(3);
227+
}
214228
return (inputStream.readInteger(3) << 8) >> 8;
215229
}
216230

217231
protected Serializable deserializeLong(ByteArrayInputStream inputStream) throws IOException {
232+
if (deserializeIntegerAsByteArray) {
233+
return inputStream.read(4);
234+
}
218235
return inputStream.readInteger(4);
219236
}
220237

221238
protected Serializable deserializeLongLong(ByteArrayInputStream inputStream) throws IOException {
239+
if (deserializeIntegerAsByteArray) {
240+
return inputStream.read(8);
241+
}
222242
return inputStream.readLong(8);
223243
}
224244

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ private void ensureCompatibility(EventDataDeserializer eventDataDeserializer) {
200200
deserializer.setDeserializeCharAndBinaryAsByteArray(
201201
compatibilitySet.contains(CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY)
202202
);
203+
deserializer.setDeserializeIntegerAsByteArray(
204+
compatibilitySet.contains(CompatibilityMode.INTEGER_AS_BYTE_ARRAY)
205+
);
203206
}
204207
}
205208

@@ -350,7 +353,11 @@ public enum CompatibilityMode {
350353
*
351354
* <p>This option is going to be enabled by default starting from [email protected].
352355
*/
353-
CHAR_AND_BINARY_AS_BYTE_ARRAY
356+
CHAR_AND_BINARY_AS_BYTE_ARRAY,
357+
/**
358+
* Return TINY/SHORT/INT24/LONG/LONGLONG values as byte[]|s (instead of int|s).
359+
*/
360+
INTEGER_AS_BYTE_ARRAY
354361
}
355362

356363
/**

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientIntegrationTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,51 @@ public void testDeserializationOfDateAndTimeAsLong() throws Exception {
421421
}
422422
}
423423

424+
@Test
425+
public void testDeserializationOfIntegerAsByteArray() throws Exception {
426+
final BinaryLogClient client = new BinaryLogClient(slave.hostname, slave.port,
427+
slave.username, slave.password);
428+
EventDeserializer eventDeserializer = new EventDeserializer();
429+
eventDeserializer.setCompatibilityMode(CompatibilityMode.INTEGER_AS_BYTE_ARRAY);
430+
client.setEventDeserializer(eventDeserializer);
431+
client.connect(DEFAULT_TIMEOUT);
432+
try {
433+
Serializable[] result;
434+
435+
result = writeAndCaptureRow("tinyint unsigned", "0", "1", "255");
436+
assertEquals(result[0], 0);
437+
assertEquals(result[1], 1);
438+
assertEquals(result[2], -1);
439+
440+
441+
result = writeAndCaptureRow("tinyint", "-128", "-1", "0", "1", "127");
442+
assertEquals(result[0], -128);
443+
assertEquals(result[1], -1);
444+
assertEquals(result[2], 0);
445+
assertEquals(result[3], 1);
446+
assertEquals(result[4], 127);
447+
448+
result = writeAndCaptureRow("smallint unsigned", "0", "1", "65535");
449+
assertEquals(result[0], 0);
450+
assertEquals(result[1], 1);
451+
assertEquals(result[2], -1);
452+
453+
result = writeAndCaptureRow("smallint", "-32768", "-1", "0", "1", "32767");
454+
assertEquals(result[0], -32768);
455+
assertEquals(result[1], -1);
456+
assertEquals(result[2], 0);
457+
assertEquals(result[3], 1);
458+
assertEquals(result[4], 32767);
459+
460+
result = writeAndCaptureRow("mediumint unsigned", "0", "1", "16777215");
461+
assertEquals(result[0], 0);
462+
assertEquals(result[1], 1);
463+
assertEquals(result[2], -1);
464+
} finally {
465+
client.disconnect();
466+
}
467+
}
468+
424469
@Test
425470
public void testDeserializationOfDateAndTimeAsLongMicrosecondsPrecision() throws Exception {
426471
final BinaryLogClient client = new BinaryLogClient(slave.hostname, slave.port,

0 commit comments

Comments
 (0)