Skip to content

Commit b8ec2ac

Browse files
committed
BUG#23617483: Fix parameter binding for unicode strings
Parameter binding using unicode strings was not supported. This patch fixes this issue by converting unicode strings to bytes when building the string scalar value. A test was added for regression.
1 parent b0d624d commit b8ec2ac

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/mysqlx/expr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def build_int_scalar(i):
171171

172172

173173
def build_string_scalar(s):
174+
if isinstance(s, unicode):
175+
s = bytes(bytearray(s, "utf-8"))
174176
return Scalar(type=Scalar.V_STRING, v_string=Scalar.String(value=s))
175177

176178

tests/test_mysqlx_crud.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
# MySQL Connector/Python - MySQL driver written in Python.
23
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
34

@@ -333,6 +334,26 @@ def test_parameter_binding(self):
333334
self.assertEqual(1, len(docs))
334335
self.assertEqual("Wilma", docs[0]["name"])
335336

337+
def test_unicode_parameter_binding(self):
338+
collection_name = "collection_test"
339+
collection = self.schema.create_collection(collection_name)
340+
result = collection.add(
341+
{"name": u"José", "age": 21},
342+
{"name": u"João", "age": 28},
343+
{"name": u"Célia", "age": 42},
344+
).execute()
345+
result = collection.find("name == :name").bind("name", u"José") \
346+
.execute()
347+
docs = result.fetch_all()
348+
self.assertEqual(1, len(docs))
349+
self.assertEqual(u"José", docs[0]["name"])
350+
351+
result = collection.find("$.name = :name").bind(u'{"name": "João"}') \
352+
.execute()
353+
docs = result.fetch_all()
354+
self.assertEqual(1, len(docs))
355+
self.assertEqual(u"João", docs[0]["name"])
356+
336357
def test_array_insert(self):
337358
collection_name = "collection_test"
338359
collection = self.schema.create_collection(collection_name)

0 commit comments

Comments
 (0)