From b77367dabe29b094ad4ccd64a45faec82813da28 Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Thu, 26 Feb 2015 19:56:12 +0100 Subject: [PATCH] cql3: Simplify primary key membership checks --- cql3/statements/modification_statement.cc | 27 ++++++++----------- cql3/statements/update_statement.cc | 32 ++++++++--------------- schema.hh | 1 + 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/cql3/statements/modification_statement.cc b/cql3/statements/modification_statement.cc index f6ee89be04..77d5462547 100644 --- a/cql3/statements/modification_statement.cc +++ b/cql3/statements/modification_statement.cc @@ -340,7 +340,7 @@ modification_statement::execute_with_condition(service::storage_proxy& proxy, se void modification_statement::add_key_values(column_definition& def, ::shared_ptr values) { - if (def.kind == column_definition::CLUSTERING) { + if (def.is_clustering_key()) { _has_no_clustering_columns = false; } @@ -373,16 +373,14 @@ modification_statement::process_where_clause(std::vector where_cla throw exceptions::invalid_request_exception(sprint("Unknown key identifier %s", *id)); } - switch (def->kind) { - case column_definition::column_kind::PARTITION: - case column_definition::column_kind::CLUSTERING: - if (rel->is_EQ() || (def->is_partition_key() && rel->is_IN())) { - add_key_values(*def, rel->to_restriction(s, std::move(names))); - break; - } + if (def->is_primary_key()) { + if (rel->is_EQ() || (def->is_partition_key() && rel->is_IN())) { + add_key_values(*def, rel->to_restriction(s, std::move(names))); + } else { throw exceptions::invalid_request_exception(sprint("Invalid operator %s for PRIMARY KEY part %s", rel->get_operator(), def->name_as_text())); - default: - throw exceptions::invalid_request_exception(sprint("Non PRIMARY KEY %s found in where clause", def->name_as_text())); + } + } else { + throw exceptions::invalid_request_exception(sprint("Non PRIMARY KEY %s found in where clause", def->name_as_text())); } } } @@ -432,13 +430,10 @@ modification_statement::parsed::prepare(database& db, ::shared_ptrprepare(keyspace(), *def); condition->collect_marker_specificaton(bound_names); - switch (def->kind) { - case column_definition::PARTITION: - case column_definition::CLUSTERING: - throw exceptions::invalid_request_exception(sprint("PRIMARY KEY column '%s' cannot have IF conditions", *id)); - default: - stmt->add_condition(condition); + if (def->is_primary_key()) { + throw exceptions::invalid_request_exception(sprint("PRIMARY KEY column '%s' cannot have IF conditions", *id)); } + stmt->add_condition(condition); } } stmt->validate_where_clause_for_conditions(); diff --git a/cql3/statements/update_statement.cc b/cql3/statements/update_statement.cc index f1dc756deb..3fd85d1be2 100644 --- a/cql3/statements/update_statement.cc +++ b/cql3/statements/update_statement.cc @@ -117,20 +117,14 @@ update_statement::parsed_insert::prepare_internal(schema_ptr schema, auto&& value = _column_values[i]; - switch(def->kind) { - case column_definition::PARTITION: - case column_definition::CLUSTERING: { - auto t = value->prepare(keyspace(), def->column_specification); - t->collect_marker_specification(bound_names); - stmt->add_key_value(*def, std::move(t)); - break; - } - default: { - auto operation = operation::set_value(value).prepare(keyspace(), *def); - operation->collect_marker_specification(bound_names); - stmt->add_operation(std::move(operation)); - break; - } + if (def->is_primary_key()) { + auto t = value->prepare(keyspace(), def->column_specification); + t->collect_marker_specification(bound_names); + stmt->add_key_value(*def, std::move(t)); + } else { + auto operation = operation::set_value(value).prepare(keyspace(), *def); + operation->collect_marker_specification(bound_names); + stmt->add_operation(std::move(operation)); }; } return stmt; @@ -152,14 +146,10 @@ update_statement::parsed_update::prepare_internal(schema_ptr schema, auto operation = entry.second->prepare(keyspace(), *def); operation->collect_marker_specification(bound_names); - switch (def->kind) { - case column_definition::column_kind::PARTITION: - case column_definition::column_kind::CLUSTERING: - throw exceptions::invalid_request_exception(sprint("PRIMARY KEY part %s found in SET part", *entry.first)); - default: - stmt->add_operation(std::move(operation)); - break; + if (def->is_primary_key()) { + throw exceptions::invalid_request_exception(sprint("PRIMARY KEY part %s found in SET part", *entry.first)); } + stmt->add_operation(std::move(operation)); } stmt->process_where_clause(_where_clause, bound_names); diff --git a/schema.hh b/schema.hh index 1597af6013..5435f8032d 100644 --- a/schema.hh +++ b/schema.hh @@ -26,6 +26,7 @@ public: ::shared_ptr column_specification; bool is_static() const { return kind == column_kind::STATIC; } bool is_partition_key() const { return kind == column_kind::PARTITION; } + bool is_clustering_key() const { return kind == column_kind::CLUSTERING; } bool is_primary_key() const { return kind == column_kind::PARTITION || kind == column_kind::CLUSTERING; } bool is_atomic() const { return !type->is_multi_cell(); } bool is_compact_value() const;