From c1dd4b65a95dfdc88ae7b48baf9879493d1f825f Mon Sep 17 00:00:00 2001 From: Tyler Hobbs Date: Tue, 25 Feb 2014 15:32:31 -0600 Subject: [PATCH] Also quote column names when needed in CREATE stmts Done for PYTHON-51 --- CHANGELOG.rst | 2 +- cassandra/metadata.py | 6 +++--- tests/integration/standard/test_metadata.py | 13 +++++++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 35f0a383..55d62102 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,7 +8,7 @@ Bug Fixes Cassandra is a multiple of the read buffer size. Previously, if no more data became available to read on the socket, the message would never be processed, resulting in an OperationTimedOut error. -* Double quote keyspace and table names that require them (those using +* Double quote keyspace, table and column names that require them (those using uppercase characters or keywords) when generating CREATE statements through KeyspaceMetadata and TableMetadata. * Decode TimestampType as DateType. (Cassandra replaced DateType with diff --git a/cassandra/metadata.py b/cassandra/metadata.py index eb31d656..ae6d1b10 100644 --- a/cassandra/metadata.py +++ b/cassandra/metadata.py @@ -668,7 +668,7 @@ class TableMetadata(object): columns = [] for col in self.columns.values(): - columns.append("%s %s" % (col.name, col.typestring)) + columns.append("%s %s" % (protect_name(col.name), col.typestring)) if len(self.partition_key) == 1 and not self.clustering_key: columns[0] += " PRIMARY KEY" @@ -680,12 +680,12 @@ class TableMetadata(object): ret += "%s%sPRIMARY KEY (" % (column_join, padding) if len(self.partition_key) > 1: - ret += "(%s)" % ", ".join(col.name for col in self.partition_key) + ret += "(%s)" % ", ".join(protect_name(col.name) for col in self.partition_key) else: ret += self.partition_key[0].name if self.clustering_key: - ret += ", %s" % ", ".join(col.name for col in self.clustering_key) + ret += ", %s" % ", ".join(protect_name(col.name) for col in self.clustering_key) ret += ")" diff --git a/tests/integration/standard/test_metadata.py b/tests/integration/standard/test_metadata.py index e3c222ca..c850888e 100644 --- a/tests/integration/standard/test_metadata.py +++ b/tests/integration/standard/test_metadata.py @@ -327,8 +327,12 @@ class TestCodeCoverage(unittest.TestCase): """ % (ksname,)) session.execute(""" CREATE TABLE "%s"."%s" ( - k int PRIMARY KEY, - "MyColumn" int ) + k int, + "A" int, + "B" int, + "MyColumn" int, + PRIMARY KEY (k, "A")) + WITH CLUSTERING ORDER BY ("A" DESC) """ % (ksname, cfname)) session.execute(""" CREATE INDEX myindex ON "%s"."%s" ("MyColumn") @@ -338,6 +342,11 @@ class TestCodeCoverage(unittest.TestCase): schema = ksmeta.export_as_string() self.assertIn('CREATE KEYSPACE "AnInterestingKeyspace"', schema) self.assertIn('CREATE TABLE "AnInterestingKeyspace"."AnInterestingTable"', schema) + self.assertIn('"A" int', schema) + self.assertIn('"B" int', schema) + self.assertIn('"MyColumn" int', schema) + self.assertIn('PRIMARY KEY (k, "A")', schema) + self.assertIn('WITH CLUSTERING ORDER BY ("A" DESC)', schema) self.assertIn('CREATE INDEX myindex ON "AnInterestingKeyspace"."AnInterestingTable" ("MyColumn")', schema) def test_already_exists_exceptions(self):