Skip to content

Commit 6482770

Browse files
committed
BUG26029811: Improve error thrown when using an invalid parameter in bind()
The X DevAPI does not support binding anonymous parameters, but it is possible to pass a single parameter to the bind() function in Connector/Python. This parameter must be a JSON string or a DbDoc object. This patch improves the error thrown when the user is using a parameter that is not a JSON string or DdDoc. A test case was added for regression.
1 parent dbec227 commit 6482770

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lib/mysqlx/statement.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,20 @@ def bind(self, *args):
260260
self._bindings.append({"name": args[0], "value": args[1]})
261261
return self
262262

263-
def _bind_single(self, object):
264-
if isinstance(object, DbDoc):
265-
self.bind(str(object))
266-
elif isinstance(object, STRING_TYPES):
267-
dict = json.loads(object)
268-
for key in dict.keys():
269-
self.bind(key, dict[key])
263+
def _bind_single(self, obj):
264+
if isinstance(obj, DbDoc):
265+
self.bind(str(obj))
266+
elif isinstance(obj, STRING_TYPES):
267+
try:
268+
res = json.loads(obj)
269+
if not isinstance(res, dict):
270+
raise ValueError
271+
except ValueError:
272+
raise ProgrammingError("Invalid JSON string to bind")
273+
for key in res.keys():
274+
self.bind(key, res[key])
275+
else:
276+
raise ProgrammingError("Invalid JSON string or object to bind")
270277

271278
def execute(self):
272279
"""Execute the statement.

tests/test_mysqlx_crud.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,11 @@ def test_parameter_binding(self):
10301030
self.assertEqual(1, len(docs))
10311031
self.assertEqual("Wilma", docs[0]["name"])
10321032

1033+
# Binding anonymous parameters are not allowed in crud operations
1034+
self.assertRaises(mysqlx.ProgrammingError,
1035+
collection.find("$.age = ?").bind, 42)
1036+
self.assertRaises(mysqlx.ProgrammingError,
1037+
collection.find("$.name = ?").bind, "Fred")
10331038
self.schema.drop_collection(collection_name)
10341039

10351040
def test_unicode_parameter_binding(self):

0 commit comments

Comments
 (0)