8000 Fix SELECT CAST(... AS JSON) cause UnicodeError by methane · Pull Request #496 · PyMySQL/PyMySQL · GitHub
[go: up one dir, main page]

Skip to content

Fix SELECT CAST(... AS JSON) cause UnicodeError #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add more test
  • Loading branch information
methane committed Jul 28, 2016
commit 6932b0a7fd095a5f6615a3c64a7caa0eeeaf9d69
7 changes: 0 additions & 7 deletions pymysql/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,6 @@ def convert_set(s):
return set(s.split(","))


def convert_json(b):
# JSON is returned as binary data.
# Decode with utf-8 regardless connection encoding.
return b.decode('utf-8')


def through(x):
return x

Expand Down Expand Up @@ -416,7 +410,6 @@ def convert_characters(connection, field, data):
FIELD_TYPE.VARCHAR: through,
FIELD_TYPE.DECIMAL: Decimal,
FIELD_TYPE.NEWDECIMAL: Decimal,
FIELD_TYPE.JSON: convert_json,
}


Expand Down
5 changes: 5 additions & 0 deletions pymysql/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,17 @@ def test_json(self):
primary key (id)
);""")
cur = conn.cursor()

json_str = u'{"hello": "こんにちは"}'
cur.execute("INSERT INTO test_json (id, `json`) values (42, %s)", (json_str,))
cur.execute("SELECT `json` from `test_json` WHERE `id`=42")
res = cur.fetchone()[0]
self.assertEqual(json.loads(res), json.loads(json_str))

cur.execute("SELECT CAST(%s AS JSON) AS x", (json_str,))
res = cur.fetchone()[0]
self.assertEqual(json.loads(res), json.loads(json_str))


class TestBulkInserts(base.PyMySQLTestCase):

Expand Down
0