Skip to content

Commit 9b2bbbd

Browse files
committed
Optimize column_encryption_policy checks in BoundStatement.bind()
Apply the same split-path optimization to the bind() method, checking for column_encryption_policy once rather than per parameter.
1 parent 9c3b11f commit 9b2bbbd

1 file changed

Lines changed: 44 additions & 21 deletions

File tree

cassandra/query.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -636,28 +636,51 @@ def bind(self, values):
636636

637637
self.raw_values = values
638638
self.values = []
639-
for value, col_spec in zip(values, col_meta):
640-
if value is None:
641-
self.values.append(None)
642-
elif value is UNSET_VALUE:
643-
if proto_version >= 4:
644-
self._append_unset_value()
639+
if ce_policy:
640+
for value, col_spec in zip(values, col_meta):
641+
if value is None:
642+
self.values.append(None)
643+
elif value is UNSET_VALUE:
644+
if proto_version >= 4:
645+
self._append_unset_value()
646+
else:
647+
raise ValueError("Attempt to bind UNSET_VALUE while using unsuitable protocol version (%d < 4)" % proto_version)
645648
else:
646-
raise ValueError("Attempt to bind UNSET_VALUE while using unsuitable protocol version (%d < 4)" % proto_version)
647-
else:
648-
try:
649-
col_desc = ColDesc(col_spec.keyspace_name, col_spec.table_name, col_spec.name)
650-
uses_ce = ce_policy and ce_policy.contains_column(col_desc)
651-
col_type = ce_policy.column_type(col_desc) if uses_ce else col_spec.type
652-
col_bytes = col_type.serialize(value, proto_version)
653-
if uses_ce:
654-
col_bytes = ce_policy.encrypt(col_desc, col_bytes)
655-
self.values.append(col_bytes)
656-
except (TypeError, struct.error) as exc:
657-
actual_type = type(value)
658-
message = ('Received an argument of invalid type for column "%s". '
659-
'Expected: %s, Got: %s; (%s)' % (col_spec.name, col_spec.type, actual_type, exc))
660-
raise TypeError(message)
649+
try:
650+
col_desc = ColDesc(col_spec.keyspace_name, col_spec.table_name, col_spec.name)
651+
uses_ce = ce_policy.contains_column(col_desc)
652+
if uses_ce:
653+
col_type = ce_policy.column_type(col_desc)
654+
col_bytes = col_type.serialize(value, proto_version)
655+
col_bytes = ce_policy.encrypt(col_desc, col_bytes)
656+
else:
657+
col_type = col_spec.type
658+
col_bytes = col_type.serialize(value, proto_version)
659+
self.values.append(col_bytes)
660+
except (TypeError, struct.error) as exc:
661+
actual_type = type(value)
662+
message = ('Received an argument of invalid type for column "%s". '
663+
'Expected: %s, Got: %s; (%s)' % (col_spec.name, col_spec.type, actual_type, exc))
664+
raise TypeError(message)
665+
else:
666+
for value, col_spec in zip(values, col_meta):
667+
if value is None:
668+
self.values.append(None)
669+
elif value is UNSET_VALUE:
670+
if proto_version >= 4:
671+
self._append_unset_value()
672+
else:
673+
raise ValueError("Attempt to bind UNSET_VALUE while using unsuitable protocol version (%d < 4)" % proto_version)
674+
else:
675+
try:
676+
col_type = col_spec.type
677+
col_bytes = col_type.serialize(value, proto_version)
678+
self.values.append(col_bytes)
679+
except (TypeError, struct.error) as exc:
680+
actual_type = type(value)
681+
message = ('Received an argument of invalid type for column "%s". '
682+
'Expected: %s, Got: %s; (%s)' % (col_spec.name, col_spec.type, actual_type, exc))
683+
raise TypeError(message)
661684

662685
if proto_version >= 4:
663686
diff = col_meta_len - len(self.values)

0 commit comments

Comments
 (0)