Skip to content

Commit 1afb5e7

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 1afb5e7

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-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

+16
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,22 @@ 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 not self.isMySQL57():
418+
self.skipTest("Json is only supported in mysql 5.7")
419+
create_query = "CREATE TABLE test (id int, value json);"
420+
insert_query = """INSERT INTO test (id, value) VALUES (1, '{"my_key": "my_val"}');"""
421+
event = self.create_and_insert_value(create_query, insert_query)
422+
self.assertEqual(event.rows[0]["values"]["value"], '{"my_key": "my_val"}')
423+
424+
def test_json_unicode(self):
425+
if not self.isMySQL57():
426+
self.skipTest("Json is only supported in mysql 5.7")
427+
create_query = "CREATE TABLE test (id int, value json);"
428+
insert_query = u"""INSERT INTO test (id, value) VALUES (1, '{"miam": "🍔"}');"""
429+
event = self.create_and_insert_value(create_query, insert_query)
430+
self.assertEqual(event.rows[0]["values"]["value"]["miam"], u'🍔')
431+
416432
def test_null(self):
417433
create_query = "CREATE TABLE test ( \
418434
test TINYINT NULL DEFAULT NULL, \

0 commit comments

Comments
 (0)