Elffuss BraidHub

kikocis/java-driver public

Unify reserved keywords list in Metadata and SchemaStatement. Fix SchemaStatement.validateNotKeyword().
Changesetbb579fbb35569d6df160b793180f96d4201960f7d44262eec495437ea0fbd3cb
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-03-01 09:46 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-03 11:49 +01:00
Parents8e976d7c1dbd

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +24 −4
@@ -50,8 +50,8 @@
5050 private static final Set<String> RESERVED_KEYWORDS = ImmutableSet.of(
5151 "add", "allow", "alter", "and", "any", "apply", "asc", "authorize", "batch", "begin", "by",
5252 "columnfamily", "create", "delete", "desc", "drop", "each_quorum", "from", "grant", "in",
53- "index", "inet", "insert", "into", "keyspace", "keyspaces", "limit", "local_one",
54- "local_quorum", "modify", "norecursive", "of", "on", "one", "order", "password",
53+ "index", "inet", "infinity", "insert", "into", "keyspace", "keyspaces", "limit", "local_one",
54+ "local_quorum", "modify", "nan", "norecursive", "of", "on", "one", "order", "password",
5555 "primary", "quorum", "rename", "revoke", "schema", "select", "set", "table", "to",
5656 "token", "three", "truncate", "two", "unlogged", "update", "use", "using", "where", "with"
5757 );
@@ -430,7 +430,7 @@
430430 // we don't need to escape if it's lowercase and match non-quoted CQL3 ids,
431431 // and if it's not a CQL reserved keyword
432432 return lowercaseAlphanumeric.matcher(ident).matches()
433- && !RESERVED_KEYWORDS.contains(ident.toLowerCase()) ?
433+ && !isReservedCqlKeyword(ident) ?
434434 ident : quote(ident);
435435 }
436436
@@ -447,7 +447,8 @@
447447 * <p/>
448448 * Note that
449449 * <a href="https://docs.datastax.com/en/cql/3.0/cql/cql_reference/keywords_r.html">reserved CQL keywords</a>
450- * should also be quoted.
450+ * should also be quoted. You can check if a given identifier is a reserved keyword
451+ * by calling {@link #isReservedCqlKeyword(String)}.
451452 *
452453 * @param id the keyspace or table identifier.
453454 * @return {@code id} enclosed in double-quotes, for use in methods like
@@ -458,6 +459,25 @@
458459 return '"' + id.replace("\"", "\"\"") + '"';
459460 }
460461
462+ /**
463+ * Checks whether an identifier is a known reserved CQL keyword or not.
464+ * <p/>
465+ * The check is case-insensitive, i.e., the word "{@code KeYsPaCe}"
466+ * would be considered as a reserved CQL keyword just as "{@code keyspace}".
467+ * <p/>
468+ * Note: The list of reserved CQL keywords is subject to change in future
469+ * versions of Cassandra. As a consequence, this method is provided solely as a
470+ * convenience utility and should not be considered as an authoritative
471+ * source of truth for checking reserved CQL keywords.
472+ *
473+ * @param id the identifier to check.
474+ * @return {@code true} if the given identifier is a known reserved
475+ * CQL keyword, {@code false} otherwise.
476+ */
477+ public static boolean isReservedCqlKeyword(String id) {
478+ return RESERVED_KEYWORDS.contains(id.toLowerCase());
479+ }
480+
461481 /**
462482 * Returns the token ranges that define data distribution in the ring.
463483 * <p/>
modified driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaStatement.java +2 −9
@@ -15,13 +15,12 @@
1515 */
1616 package com.datastax.driver.core.schemabuilder;
1717
18+import com.datastax.driver.core.Metadata;
1819 import com.datastax.driver.core.ProtocolVersion;
1920 import com.datastax.driver.core.RegularStatement;
2021 import com.google.common.base.Strings;
2122
2223 import java.nio.ByteBuffer;
23-import java.util.Arrays;
24-import java.util.List;
2524
2625 /**
2726 * A DDL statement generated by {@link SchemaBuilder}.
@@ -31,12 +30,6 @@
3130 static final String STATEMENT_START = "\n\t";
3231 static final String COLUMN_FORMATTING = "\n\t\t";
3332
34- static final List<String> RESERVED_KEYWORDS = Arrays.asList(
35- ("add,allow,alter,and,any,apply,asc,authorize,batch,begin,by,columnfamily,create,delete,desc,drop,each_quorum,from,grant,in,index,inet,infinity," +
36- "insert,into,keyspace,keyspaces,limit,local_one,local_quorum,modify,nan,norecursive,of,on,order,password,primary,quorum,rename,revoke,schema," +
37- "select,set,table,three,to,token,truncate,two,unlogged,update,use,using,where,with")
38- .split(","));
39-
4033 private volatile String cache;
4134
4235 abstract String buildInternal();
@@ -85,7 +78,7 @@
8578 }
8679
8780 static void validateNotKeyWord(String label, String message) {
88- if (RESERVED_KEYWORDS.contains(label)) {
81+ if (Metadata.isReservedCqlKeyword(label)) {
8982 throw new IllegalArgumentException(message);
9083 }
9184 }
modified driver-core/src/test/java/com/datastax/driver/core/schemabuilder/CreateTest.java +12 −12
@@ -545,54 +545,54 @@
545545 }
546546
547547 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
548- expectedExceptionsMessageRegExp = "The keyspace name 'add' is not allowed because it is a reserved keyword")
548+ expectedExceptionsMessageRegExp = "The keyspace name 'ADD' is not allowed because it is a reserved keyword")
549549 public void should_fail_if_keyspace_name_is_a_reserved_keyword() throws Exception {
550- createTable("add", "test")
550+ createTable("ADD", "test")
551551 .addPartitionKey("pk", DataType.bigint())
552552 .addColumn("col", DataType.text()).getQueryString();
553553 }
554554
555555 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
556- expectedExceptionsMessageRegExp = "The table name 'add' is not allowed because it is a reserved keyword")
556+ expectedExceptionsMessageRegExp = "The table name 'ADD' is not allowed because it is a reserved keyword")
557557 public void should_fail_if_table_name_is_a_reserved_keyword() throws Exception {
558- createTable("add")
558+ createTable("ADD")
559559 .addPartitionKey("pk", DataType.bigint())
560560 .addColumn("col", DataType.text()).getQueryString();
561561 }
562562
563563 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
564- expectedExceptionsMessageRegExp = "The partition key name 'add' is not allowed because it is a reserved keyword")
564+ expectedExceptionsMessageRegExp = "The partition key name 'ADD' is not allowed because it is a reserved keyword")
565565 public void should_fail_if_partition_key_is_a_reserved_keyword() throws Exception {
566566 createTable("test")
567- .addPartitionKey("add", DataType.bigint())
567+ .addPartitionKey("ADD", DataType.bigint())
568568 .addColumn("col", DataType.text()).getQueryString();
569569 }
570570
571571 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
572- expectedExceptionsMessageRegExp = "The clustering column name 'add' is not allowed because it is a reserved keyword")
572+ expectedExceptionsMessageRegExp = "The clustering column name 'ADD' is not allowed because it is a reserved keyword")
573573 public void should_fail_if_clustering_key_is_a_reserved_keyword() throws Exception {
574574 createTable("test")
575575 .addPartitionKey("pk", DataType.bigint())
576- .addClusteringColumn("add", DataType.uuid())
576+ .addClusteringColumn("ADD", DataType.uuid())
577577 .addColumn("col", DataType.text()).getQueryString();
578578 }
579579
580580 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
581- expectedExceptionsMessageRegExp = "The column name 'add' is not allowed because it is a reserved keyword")
581+ expectedExceptionsMessageRegExp = "The column name 'ADD' is not allowed because it is a reserved keyword")
582582 public void should_fail_if_simple_column_is_a_reserved_keyword() throws Exception {
583583 createTable("test")
584584 .addPartitionKey("pk", DataType.bigint())
585585 .addClusteringColumn("cluster", DataType.uuid())
586- .addColumn("add", DataType.text()).getQueryString();
586+ .addColumn("ADD", DataType.text()).getQueryString();
587587 }
588588
589589 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
590- expectedExceptionsMessageRegExp = "The static column name 'add' is not allowed because it is a reserved keyword")
590+ expectedExceptionsMessageRegExp = "The static column name 'ADD' is not allowed because it is a reserved keyword")
591591 public void should_fail_if_static_column_is_a_reserved_keyword() throws Exception {
592592 createTable("test")
593593 .addPartitionKey("pk", DataType.bigint())
594594 .addClusteringColumn("cluster", DataType.uuid())
595- .addStaticColumn("add", DataType.text())
595+ .addStaticColumn("ADD", DataType.text())
596596 .addColumn("col", DataType.text()).getQueryString();
597597 }
598598