Skip to content

Commit 0bb0b8d

Browse files
authored
Merge pull request #658 from D3Hunter/json-store-as-string
store json as string in rows events
2 parents 06f9327 + 7fb6db3 commit 0bb0b8d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

replication/row_event.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,11 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{
11511151
// Refer: https://github.com/shyiko/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java#L404
11521152
length = int(FixedLengthInt(data[0:meta]))
11531153
n = length + int(meta)
1154-
v, err = e.decodeJsonBinary(data[meta:n])
1154+
var d []byte
1155+
d, err = e.decodeJsonBinary(data[meta:n])
1156+
if err == nil {
1157+
v = hack.String(d)
1158+
}
11551159
case MYSQL_TYPE_GEOMETRY:
11561160
// MySQL saves Geometry as Blob in binlog
11571161
// Seem that the binary format is SRID (4 bytes) + WKB, outer can use

replication/row_event_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -699,40 +699,40 @@ func (_ *testDecodeSuite) TestJsonCompatibility(c *C) {
699699
rows.Rows = nil
700700
err = rows.Decode(data)
701701
c.Assert(err, IsNil)
702-
c.Assert(rows.Rows[0][2], DeepEquals, []uint8("{}"))
702+
c.Assert(rows.Rows[0][2], DeepEquals, "{}")
703703

704704
// after MySQL 5.7.22
705705
data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x01\x00\x0c\x00\x0b\x00\x01\x00\x05\xd2\x04a")
706706
rows.Rows = nil
707707
err = rows.Decode(data)
708708
c.Assert(err, IsNil)
709-
c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{}"))
710-
c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{\"a\":1234}"))
709+
c.Assert(rows.Rows[1][2], DeepEquals, "{}")
710+
c.Assert(rows.Rows[2][2], DeepEquals, "{\"a\":1234}")
711711

712712
data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x01\x00\x0c\x00\x0b\x00\x01\x00\x05\xd2\x04a\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00")
713713
rows.Rows = nil
714714
err = rows.Decode(data)
715715
c.Assert(err, IsNil)
716-
c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{\"a\":1234}"))
717-
c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{}"))
716+
c.Assert(rows.Rows[1][2], DeepEquals, "{\"a\":1234}")
717+
c.Assert(rows.Rows[2][2], DeepEquals, "{}")
718718

719719
// before MySQL 5.7.22
720720
rows.ignoreJSONDecodeErr = true
721721
data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x01\x00\x0c\x00\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x01\x00\x0c\x00\x0b\x00\x01\x00\x05\xd2\x04a")
722722
rows.Rows = nil
723723
err = rows.Decode(data)
724724
c.Assert(err, IsNil)
725-
c.Assert(rows.Rows[1][2], DeepEquals, []uint8("null"))
726-
c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{\"a\":1234}"))
725+
c.Assert(rows.Rows[1][2], DeepEquals, "null")
726+
c.Assert(rows.Rows[2][2], DeepEquals, "{\"a\":1234}")
727727

728728
rows.ignoreJSONDecodeErr = false
729729
data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x05\xd2\x04a\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00")
730730
rows.Rows = nil
731731
err = rows.Decode(data)
732732
c.Assert(err, IsNil)
733733
// this value is wrong in binlog, but can be parsed without error
734-
c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{}"))
735-
c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{}"))
734+
c.Assert(rows.Rows[1][2], DeepEquals, "{}")
735+
c.Assert(rows.Rows[2][2], DeepEquals, "{}")
736736
}
737737

738738
func (_ *testDecodeSuite) TestDecodeDatetime2(c *C) {

0 commit comments

Comments
 (0)