Skip to content

Commit 93ac046

Browse files
author
Arthur Gautier
committed
Fix handling of JSON data
Introduced in 5.7.8 Fixes julien-duponchelle#181 Signed-off-by: Arthur Gautier <[email protected]>
1 parent 990fc08 commit 93ac046

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

pymysqlreplication/constants/FIELD_TYPE.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
TIMESTAMP2 = 17
4242
DATETIME2 = 18
4343
TIME2 = 19
44+
JSON = 245 # Introduced in 5.7.8
4445
NEWDECIMAL = 246
4546
ENUM = 247
4647
SET = 248

pymysqlreplication/row_event.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import struct
44
import decimal
55
import datetime
6+
import json
67

78
from pymysql.util import byte2int
89
from pymysql.charset import charset_to_encoding
@@ -167,6 +168,10 @@ def _read_column_data(self, cols_bitmap):
167168
elif column.type == FIELD_TYPE.GEOMETRY:
168169
values[name] = self.packet.read_length_coded_pascal_string(
169170
column.length_size)
171+
elif column.type == FIELD_TYPE.JSON:
172+
data = self.packet.read_length_coded_pascal_string(
173+
column.length_size))
174+
values[name] = json.loads(data)
170175
else:
171176
raise NotImplementedError("Unknown MySQL column type: %d" %
172177
(column.type))

pymysqlreplication/tests/test_data_type.py

+18
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,24 @@ def test_geometry(self):
413413
event = self.create_and_insert_value(create_query, insert_query)
414414
self.assertEqual(event.rows[0]["values"]["test"], b'\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?')
415415

416+
def test_json(self):
417+
if self.isMySQL57():
418+
# https://dev.mysql.com/doc/refman/5.7/en/migrating-to-year4.html
419+
self.skipTest("YEAR(2) is unsupported in mysql 5.7")
420+
create_query = "CREATE TABLE test (id int, value json);"
421+
insert_query = """INSERT INTO test (id, value) VALUES (1, '{"my_key": "my_val"}');"""
422+
event = self.create_and_insert_value(create_query, insert_query)
423+
self.assertEqual(event.rows[0]["values"]["value"], '{"my_key": "my_val"}')
424+
425+
def test_json_unicode(self):
426+
if self.isMySQL57():
427+
# https://dev.mysql.com/doc/refman/5.7/en/migrating-to-year4.html
428+
self.skipTest("YEAR(2) is unsupported in mysql 5.7")
429+
create_query = "CREATE TABLE test (id int, value json);"
430+
insert_query = u"""INSERT INTO test (id, value) VALUES (1, '{"miam": "🍔"}');"""
431+
event = self.create_and_insert_value(create_query, insert_query)
432+
self.assertEqual(event.rows[0]["values"]["value"]["miam"], u'🍔')
433+
416434
def test_null(self):
417435
create_query = "CREATE TABLE test ( \
418436
test TINYINT NULL DEFAULT NULL, \

0 commit comments

Comments
 (0)