Skip to content

Commit a26d665

Browse files
committed
BUG27589450: Remove upsert functionality from WriteStatement class
Currently the upsert() method is part of the WriteStatement class which is inherited by both the AddStatement and InsertStatement classes. However, upserts are currently only supported for add statements, and it will cause an assertion to attempt to execute an insert statement that has enabled upserts. This patch removes the upsert functionality from WriteStatement and adds it to AddStatement class.
1 parent 6fd0148 commit a26d665

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lib/mysqlx/protocol.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ def send_insert(self, stmt):
511511
row["field"].extend([build_expr(value).get_message()])
512512
msg["row"].extend([row.get_message()])
513513

514-
msg["upsert"] = stmt.is_upsert()
514+
if hasattr(stmt, "is_upsert"):
515+
msg["upsert"] = stmt.is_upsert()
515516
self._writer.write_message(
516517
mysqlxpb_enum("Mysqlx.ClientMessages.Type.CRUD_INSERT"), msg)
517518

lib/mysqlx/statement.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ class WriteStatement(Statement):
445445
def __init__(self, target, doc_based):
446446
super(WriteStatement, self).__init__(target, doc_based)
447447
self._values = []
448-
self._upsert = False
449448

450449
def get_values(self):
451450
"""Returns the list of values.
@@ -455,25 +454,6 @@ def get_values(self):
455454
"""
456455
return self._values
457456

458-
def is_upsert(self):
459-
"""Returns `True` if it's an upsert.
460-
461-
Returns:
462-
bool: `True` if it's an upsert.
463-
"""
464-
return self._upsert
465-
466-
def upsert(self, value=True):
467-
"""Sets the upset flag to the boolean of the value provided.
468-
Setting of this flag allows updating of the matched rows/documents
469-
with the provided value.
470-
471-
Args:
472-
value (optional[bool]): Set or unset the upsert flag.
473-
"""
474-
self._upsert = value
475-
return self
476-
477457
def execute(self):
478458
"""Execute the statement.
479459
@@ -491,8 +471,28 @@ class AddStatement(WriteStatement):
491471
"""
492472
def __init__(self, collection):
493473
super(AddStatement, self).__init__(collection, True)
474+
self._upsert = False
494475
self.ids = []
495476

477+
def is_upsert(self):
478+
"""Returns `True` if it's an upsert.
479+
480+
Returns:
481+
bool: `True` if it's an upsert.
482+
"""
483+
return self._upsert
484+
485+
def upsert(self, value=True):
486+
"""Sets the upset flag to the boolean of the value provided.
487+
Setting of this flag allows updating of the matched rows/documents
488+
with the provided value.
489+
490+
Args:
491+
value (optional[bool]): Set or unset the upsert flag.
492+
"""
493+
self._upsert = value
494+
return self
495+
496496
def add(self, *values):
497497
"""Adds a list of documents into a collection.
498498

0 commit comments

Comments
 (0)