Skip to content

Commit 6b82c4b

Browse files
committed
BUG26160876: Fix issue when using empty condition in Collection.remove() and Table.delete()
1 parent d5a63bc commit 6b82c4b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/mysqlx/statement.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ def where(self, condition):
178178
"""
179179
self._has_where = True
180180
self._where = condition
181-
expr = ExprParser(condition, not self._doc_based)
182-
self._where_expr = expr.expr()
181+
try:
182+
expr = ExprParser(condition, not self._doc_based)
183+
self._where_expr = expr.expr()
184+
except ValueError:
185+
raise ProgrammingError("Invalid condition")
183186
self._binding_map = expr.placeholder_name_to_position
184187
return self
185188

@@ -463,7 +466,7 @@ def execute(self):
463466
mysqlx.Result: Result object.
464467
"""
465468
if not self._has_where:
466-
raise ProgrammingError("No condition was found for modify.")
469+
raise ProgrammingError("No condition was found for modify")
467470
return self._connection.update(self)
468471

469472

@@ -667,7 +670,7 @@ def execute(self):
667670
mysqlx.Result: Result object
668671
"""
669672
if not self._has_where:
670-
raise ProgrammingError("No condition was found for update.")
673+
raise ProgrammingError("No condition was found for update")
671674
return self._connection.update(self)
672675

673676

@@ -687,7 +690,7 @@ def execute(self):
687690
mysqlx.Result: Result object.
688691
"""
689692
if not self._has_where:
690-
raise ProgrammingError("No condition was found for remove.")
693+
raise ProgrammingError("No condition was found for remove")
691694
return self._connection.delete(self)
692695

693696

@@ -711,7 +714,7 @@ def execute(self):
711714
mysqlx.Result: Result object.
712715
"""
713716
if not self._has_where:
714-
raise ProgrammingError("No condition was found for delete.")
717+
raise ProgrammingError("No condition was found for delete")
715718
return self._connection.delete(self)
716719

717720

@@ -875,7 +878,7 @@ def defined_as(self, statement):
875878
if not isinstance(statement, SelectStatement) and \
876879
not isinstance(statement, STRING_TYPES):
877880
raise ProgrammingError("The statement must be an instance of "
878-
"SelectStatement or a SQL string.")
881+
"SelectStatement or a SQL string")
879882
self._defined_as = copy.copy(statement) # Prevent modifications
880883
return self
881884

tests/test_mysqlx_crud.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ def test_remove(self):
826826
# Collection.remove() is not allowed without a condition
827827
result = collection.remove()
828828
self.assertRaises(mysqlx.ProgrammingError, result.execute)
829+
result = collection.remove("")
830+
self.assertRaises(mysqlx.ProgrammingError, result.execute)
831+
self.assertRaises(mysqlx.ProgrammingError, collection.remove, " ")
829832

830833
self.schema.drop_collection(collection_name)
831834

@@ -1294,6 +1297,10 @@ def test_delete(self):
12941297
# Table.delete() is not allowed without a condition
12951298
result = table.delete()
12961299
self.assertRaises(mysqlx.ProgrammingError, result.execute)
1300+
result = table.delete("")
1301+
self.assertRaises(mysqlx.ProgrammingError, result.execute)
1302+
self.assertRaises(mysqlx.ProgrammingError, table.delete, " ")
1303+
12971304
self.schema.drop_table(table_name)
12981305

12991306
def test_count(self):

0 commit comments

Comments
 (0)