kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | bb579fbb35569d6df160b793180f96d4201960f7d44262eec495437ea0fbd3cb |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-03-01 09:46 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 11:49 +01:00 |
| Parents | 8e976d7c1dbd |
3 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +24 −4
| @@ -50,8 +50,8 @@ | ||
| 50 | 50 | private static final Set<String> RESERVED_KEYWORDS = ImmutableSet.of( |
| 51 | 51 | "add", "allow", "alter", "and", "any", "apply", "asc", "authorize", "batch", "begin", "by", |
| 52 | 52 | "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", | |
| 55 | 55 | "primary", "quorum", "rename", "revoke", "schema", "select", "set", "table", "to", |
| 56 | 56 | "token", "three", "truncate", "two", "unlogged", "update", "use", "using", "where", "with" |
| 57 | 57 | ); |
| @@ -430,7 +430,7 @@ | ||
| 430 | 430 | // we don't need to escape if it's lowercase and match non-quoted CQL3 ids, |
| 431 | 431 | // and if it's not a CQL reserved keyword |
| 432 | 432 | return lowercaseAlphanumeric.matcher(ident).matches() |
| 433 | - && !RESERVED_KEYWORDS.contains(ident.toLowerCase()) ? | |
| 433 | + && !isReservedCqlKeyword(ident) ? | |
| 434 | 434 | ident : quote(ident); |
| 435 | 435 | } |
| 436 | 436 | |
| @@ -447,7 +447,8 @@ | ||
| 447 | 447 | * <p/> |
| 448 | 448 | * Note that |
| 449 | 449 | * <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)}. | |
| 451 | 452 | * |
| 452 | 453 | * @param id the keyspace or table identifier. |
| 453 | 454 | * @return {@code id} enclosed in double-quotes, for use in methods like |
| @@ -458,6 +459,25 @@ | ||
| 458 | 459 | return '"' + id.replace("\"", "\"\"") + '"'; |
| 459 | 460 | } |
| 460 | 461 | |
| 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 | + | |
| 461 | 481 | /** |
| 462 | 482 | * Returns the token ranges that define data distribution in the ring. |
| 463 | 483 | * <p/> |
modified driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaStatement.java +2 −9
| @@ -15,13 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.schemabuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.Metadata; | |
| 18 | 19 | import com.datastax.driver.core.ProtocolVersion; |
| 19 | 20 | import com.datastax.driver.core.RegularStatement; |
| 20 | 21 | import com.google.common.base.Strings; |
| 21 | 22 | |
| 22 | 23 | import java.nio.ByteBuffer; |
| 23 | -import java.util.Arrays; | |
| 24 | -import java.util.List; | |
| 25 | 24 | |
| 26 | 25 | /** |
| 27 | 26 | * A DDL statement generated by {@link SchemaBuilder}. |
| @@ -31,12 +30,6 @@ | ||
| 31 | 30 | static final String STATEMENT_START = "\n\t"; |
| 32 | 31 | static final String COLUMN_FORMATTING = "\n\t\t"; |
| 33 | 32 | |
| 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 | - | |
| 40 | 33 | private volatile String cache; |
| 41 | 34 | |
| 42 | 35 | abstract String buildInternal(); |
| @@ -85,7 +78,7 @@ | ||
| 85 | 78 | } |
| 86 | 79 | |
| 87 | 80 | static void validateNotKeyWord(String label, String message) { |
| 88 | - if (RESERVED_KEYWORDS.contains(label)) { | |
| 81 | + if (Metadata.isReservedCqlKeyword(label)) { | |
| 89 | 82 | throw new IllegalArgumentException(message); |
| 90 | 83 | } |
| 91 | 84 | } |
modified driver-core/src/test/java/com/datastax/driver/core/schemabuilder/CreateTest.java +12 −12
| @@ -545,54 +545,54 @@ | ||
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | @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") | |
| 549 | 549 | public void should_fail_if_keyspace_name_is_a_reserved_keyword() throws Exception { |
| 550 | - createTable("add", "test") | |
| 550 | + createTable("ADD", "test") | |
| 551 | 551 | .addPartitionKey("pk", DataType.bigint()) |
| 552 | 552 | .addColumn("col", DataType.text()).getQueryString(); |
| 553 | 553 | } |
| 554 | 554 | |
| 555 | 555 | @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") | |
| 557 | 557 | public void should_fail_if_table_name_is_a_reserved_keyword() throws Exception { |
| 558 | - createTable("add") | |
| 558 | + createTable("ADD") | |
| 559 | 559 | .addPartitionKey("pk", DataType.bigint()) |
| 560 | 560 | .addColumn("col", DataType.text()).getQueryString(); |
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | @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") | |
| 565 | 565 | public void should_fail_if_partition_key_is_a_reserved_keyword() throws Exception { |
| 566 | 566 | createTable("test") |
| 567 | - .addPartitionKey("add", DataType.bigint()) | |
| 567 | + .addPartitionKey("ADD", DataType.bigint()) | |
| 568 | 568 | .addColumn("col", DataType.text()).getQueryString(); |
| 569 | 569 | } |
| 570 | 570 | |
| 571 | 571 | @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") | |
| 573 | 573 | public void should_fail_if_clustering_key_is_a_reserved_keyword() throws Exception { |
| 574 | 574 | createTable("test") |
| 575 | 575 | .addPartitionKey("pk", DataType.bigint()) |
| 576 | - .addClusteringColumn("add", DataType.uuid()) | |
| 576 | + .addClusteringColumn("ADD", DataType.uuid()) | |
| 577 | 577 | .addColumn("col", DataType.text()).getQueryString(); |
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | @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") | |
| 582 | 582 | public void should_fail_if_simple_column_is_a_reserved_keyword() throws Exception { |
| 583 | 583 | createTable("test") |
| 584 | 584 | .addPartitionKey("pk", DataType.bigint()) |
| 585 | 585 | .addClusteringColumn("cluster", DataType.uuid()) |
| 586 | - .addColumn("add", DataType.text()).getQueryString(); | |
| 586 | + .addColumn("ADD", DataType.text()).getQueryString(); | |
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | @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") | |
| 591 | 591 | public void should_fail_if_static_column_is_a_reserved_keyword() throws Exception { |
| 592 | 592 | createTable("test") |
| 593 | 593 | .addPartitionKey("pk", DataType.bigint()) |
| 594 | 594 | .addClusteringColumn("cluster", DataType.uuid()) |
| 595 | - .addStaticColumn("add", DataType.text()) | |
| 595 | + .addStaticColumn("ADD", DataType.text()) | |
| 596 | 596 | .addColumn("col", DataType.text()).getQueryString(); |
| 597 | 597 | } |
| 598 | 598 |