Skip to content

Commit 6560bfc

Browse files
committed
cqle: make the distinction btw collections and containers
Makes the DML update query treat tuples as simple values, rather than special container types.
1 parent 4c9dc3e commit 6560bfc

2 files changed

Lines changed: 42 additions & 41 deletions

File tree

cassandra/cqlengine/columns.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ def to_database(self, value):
607607
return self.validate(value)
608608

609609

610-
class BaseContainerColumn(Column):
610+
class BaseCollectionColumn(Column):
611611
"""
612612
Base Container type for collection-like columns.
613613
@@ -630,10 +630,10 @@ def __init__(self, types, **kwargs):
630630
instances.append(inst)
631631

632632
self.types = instances
633-
super(BaseContainerColumn, self).__init__(**kwargs)
633+
super(BaseCollectionColumn, self).__init__(**kwargs)
634634

635635
def validate(self, value):
636-
value = super(BaseContainerColumn, self).validate(value)
636+
value = super(BaseCollectionColumn, self).validate(value)
637637
# It is dangerous to let collections have more than 65535.
638638
# See: https://issues.apache.org/jira/browse/CASSANDRA-5428
639639
if value is not None and len(value) > 65535:
@@ -652,6 +652,45 @@ def sub_types(self):
652652
return self.types
653653

654654

655+
class Tuple(BaseCollectionColumn):
656+
"""
657+
Stores a fixed-length set of positional values
658+
659+
http://docs.datastax.com/en/cql/3.1/cql/cql_reference/tupleType.html
660+
"""
661+
def __init__(self, *args, **kwargs):
662+
"""
663+
:param args: column types representing tuple composition
664+
"""
665+
if not args:
666+
raise ValueError("Tuple must specify at least one inner type")
667+
super(Tuple, self).__init__(args, **kwargs)
668+
self.db_type = 'tuple<{0}>'.format(', '.join(typ.db_type for typ in self.types))
669+
670+
def validate(self, value):
671+
val = super(Tuple, self).validate(value)
672+
if val is None:
673+
return
674+
if len(val) > len(self.types):
675+
raise ValidationError("Value %r has more fields than tuple definition (%s)" %
676+
(val, ', '.join(t for t in self.types)))
677+
return tuple(t.validate(v) for t, v in zip(self.types, val))
678+
679+
def to_python(self, value):
680+
if value is None:
681+
return tuple()
682+
return tuple(t.to_python(v) for t, v in zip(self.types, value))
683+
684+
def to_database(self, value):
685+
if value is None:
686+
return
687+
return tuple(t.to_database(v) for t, v in zip(self.types, value))
688+
689+
690+
class BaseContainerColumn(BaseCollectionColumn):
691+
pass
692+
693+
655694
class Set(BaseContainerColumn):
656695
"""
657696
Stores a set of unordered, unique values
@@ -787,41 +826,6 @@ def to_database(self, value):
787826
return dict((self.key_col.to_database(k), self.value_col.to_database(v)) for k, v in value.items())
788827

789828

790-
class Tuple(BaseContainerColumn):
791-
"""
792-
Stores a fixed-length set of positional values
793-
794-
http://docs.datastax.com/en/cql/3.1/cql/cql_reference/tupleType.html
795-
"""
796-
def __init__(self, *args, **kwargs):
797-
"""
798-
:param args: column types representing tuple composition
799-
"""
800-
if not args:
801-
raise ValueError("Tuple must specify at least one inner type")
802-
super(Tuple, self).__init__(args, **kwargs)
803-
self.db_type = 'tuple<{0}>'.format(', '.join(typ.db_type for typ in self.types))
804-
805-
def validate(self, value):
806-
val = super(Tuple, self).validate(value)
807-
if val is None:
808-
return
809-
if len(val) > len(self.types):
810-
raise ValidationError("Value %r has more fields than tuple definition (%s)" %
811-
(val, ', '.join(t for t in self.types)))
812-
return tuple(t.validate(v) for t, v in zip(self.types, val))
813-
814-
def to_python(self, value):
815-
if value is None:
816-
return tuple()
817-
return tuple(t.to_python(v) for t, v in zip(self.types, value))
818-
819-
def to_database(self, value):
820-
if value is None:
821-
return
822-
return tuple(t.to_database(v) for t, v in zip(self.types, value))
823-
824-
825829
class UDTValueManager(BaseValueManager):
826830
@property
827831
def changed(self):

cassandra/cqlengine/query.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,11 +1151,9 @@ def update(self):
11511151
val = getattr(self.instance, name, None)
11521152
val_mgr = self.instance._values[name]
11531153

1154-
# don't update something that is null
11551154
if val is None:
11561155
continue
11571156

1158-
# don't update something if it hasn't changed
11591157
if not val_mgr.changed and not isinstance(col, columns.Counter):
11601158
continue
11611159

@@ -1173,7 +1171,6 @@ def update(self):
11731171
else:
11741172
raise RuntimeError
11751173

1176-
# do the stuff
11771174
clause = klass(col.db_field_name, val,
11781175
previous=val_mgr.previous_value, column=col)
11791176
if clause.get_context_size() > 0:

0 commit comments

Comments
 (0)