kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8e976d7c1dbd389aad0fe08c1b7407386290153489012c2be6881f2a7ed13535 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-02 11:13 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 10:36 +01:00 |
| Parents | c2cdc3b9eac6 |
4 files changed
modified changelog/README.md +1 −0
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | - [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI |
| 26 | 26 | - [bug] JAVA-819: Expose more errors in RetryPolicy + provide idempotent-aware wrapper. |
| 27 | 27 | - [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger. |
| 28 | +- [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key. | |
| 28 | 29 | |
| 29 | 30 | Merged from 2.0 branch: |
| 30 | 31 |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +21 −5
| @@ -47,6 +47,15 @@ | ||
| 47 | 47 | private static final Pattern alphanumeric = Pattern.compile("\\w+"); // this includes _ |
| 48 | 48 | private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*"); |
| 49 | 49 | |
| 50 | + private static final Set<String> RESERVED_KEYWORDS = ImmutableSet.of( | |
| 51 | + "add", "allow", "alter", "and", "any", "apply", "asc", "authorize", "batch", "begin", "by", | |
| 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", | |
| 55 | + "primary", "quorum", "rename", "revoke", "schema", "select", "set", "table", "to", | |
| 56 | + "token", "three", "truncate", "two", "unlogged", "update", "use", "using", "where", "with" | |
| 57 | + ); | |
| 58 | + | |
| 50 | 59 | Metadata(Cluster.Manager cluster) { |
| 51 | 60 | this.cluster = cluster; |
| 52 | 61 | } |
| @@ -404,7 +413,7 @@ | ||
| 404 | 413 | return id.toLowerCase(); |
| 405 | 414 | |
| 406 | 415 | // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes |
| 407 | - if (id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 416 | + if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 408 | 417 | return id.substring(1, id.length() - 1).replaceAll("\"\"", "\""); |
| 409 | 418 | |
| 410 | 419 | // Otherwise, just return the id. |
| @@ -418,8 +427,11 @@ | ||
| 418 | 427 | // tables. Because it comes from Cassandra, we could just always quote it, |
| 419 | 428 | // but to get a nicer output we don't do it if it's not necessary. |
| 420 | 429 | static String escapeId(String ident) { |
| 421 | - // we don't need to escape if it's lowercase and match non-quoted CQL3 ids. | |
| 422 | - return lowercaseAlphanumeric.matcher(ident).matches() ? ident : quote(ident); | |
| 430 | + // we don't need to escape if it's lowercase and match non-quoted CQL3 ids, | |
| 431 | + // and if it's not a CQL reserved keyword | |
| 432 | + return lowercaseAlphanumeric.matcher(ident).matches() | |
| 433 | + && !RESERVED_KEYWORDS.contains(ident.toLowerCase()) ? | |
| 434 | + ident : quote(ident); | |
| 423 | 435 | } |
| 424 | 436 | |
| 425 | 437 | /** |
| @@ -430,8 +442,12 @@ | ||
| 430 | 442 | * the identifier in double quotes (see the |
| 431 | 443 | * <a href="http://cassandra.apache.org/doc/cql3/CQL.html#identifiers">CQL documentation</a> |
| 432 | 444 | * for details). If you are using case sensitive identifiers, this method |
| 433 | - * can be used to enclose such identifier in double quotes, making it case | |
| 445 | + * can be used to enclose such identifiers in double quotes, making them case | |
| 434 | 446 | * sensitive. |
| 447 | + * <p/> | |
| 448 | + * Note that | |
| 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. | |
| 435 | 451 | * |
| 436 | 452 | * @param id the keyspace or table identifier. |
| 437 | 453 | * @return {@code id} enclosed in double-quotes, for use in methods like |
| @@ -439,7 +455,7 @@ | ||
| 439 | 455 | * or even {@link Cluster#connect(String)}. |
| 440 | 456 | */ |
| 441 | 457 | public static String quote(String id) { |
| 442 | - return '"' + id + '"'; | |
| 458 | + return '"' + id.replace("\"", "\"\"") + '"'; | |
| 443 | 459 | } |
| 444 | 460 | |
| 445 | 461 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +2 −2
| @@ -435,12 +435,12 @@ | ||
| 435 | 435 | sb.append("CREATE TABLE ").append(Metadata.escapeId(keyspace.getName())).append('.').append(Metadata.escapeId(name)).append(" ("); |
| 436 | 436 | newLine(sb, formatted); |
| 437 | 437 | for (ColumnMetadata cm : columns.values()) |
| 438 | - newLine(sb.append(spaces(4, formatted)).append(cm).append(','), formatted); | |
| 438 | + newLine(sb.append(spaces(4, formatted)).append(cm).append(',').append(spaces(1, !formatted)), formatted); | |
| 439 | 439 | |
| 440 | 440 | // PK |
| 441 | 441 | sb.append(spaces(4, formatted)).append("PRIMARY KEY ("); |
| 442 | 442 | if (partitionKey.size() == 1) { |
| 443 | - sb.append(partitionKey.get(0).getName()); | |
| 443 | + sb.append(Metadata.escapeId(partitionKey.get(0).getName())); | |
| 444 | 444 | } else { |
| 445 | 445 | sb.append('('); |
| 446 | 446 | boolean first = true; |
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +73 −1
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import org.testng.annotations.Test; |
| 19 | 19 | |
| 20 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 20 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 21 | 21 | |
| 22 | 22 | @CCMConfig(clusterProvider = "createClusterBuilderNoDebouncing") |
| 23 | 23 | public class TableMetadataTest extends CCMTestsSupport { |
| @@ -35,4 +35,76 @@ | ||
| 35 | 35 | TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("single_quote"); |
| 36 | 36 | assertThat(table.asCQLQuery()).contains("comment with single quote '' should work"); |
| 37 | 37 | } |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Validates that a table with case-sensitive column names and column names | |
| 41 | + * consisting of (quoted) reserved keywords is correctly parsed | |
| 42 | + * and that the generated CQL is valid. | |
| 43 | + * | |
| 44 | + * @jira_ticket JAVA-1064 | |
| 45 | + * @test_category metadata | |
| 46 | + */ | |
| 47 | + @Test(groups = "short") | |
| 48 | + public void should_parse_table_with_case_sensitive_column_names_and_reserved_keywords() throws Exception { | |
| 49 | + // given | |
| 50 | + String c1 = Metadata.quote("quotes go \"\" here \"\" "); | |
| 51 | + String c2 = Metadata.quote("\\x00\\x25"); | |
| 52 | + String c3 = Metadata.quote("columnfamily"); | |
| 53 | + String c4 = Metadata.quote("select"); | |
| 54 | + String c5 = Metadata.quote("who''s there'? "); | |
| 55 | + String c6 = Metadata.quote("faux )"); | |
| 56 | + String c7 = Metadata.quote("COMPACT STORAGE"); | |
| 57 | + // single partition key | |
| 58 | + String cql1 = String.format("CREATE TABLE %s.\"MyTable1\" (" | |
| 59 | + + "%s text, " | |
| 60 | + + "%s text, " | |
| 61 | + + "%s text, " | |
| 62 | + + "%s text, " | |
| 63 | + + "%s text, " | |
| 64 | + + "%s text, " | |
| 65 | + + "%s text, " | |
| 66 | + + "PRIMARY KEY (%s, %s, %s, %s, %s, %s)" | |
| 67 | + + ")", keyspace, c1, c2, c3, c4, c5, c6, c7, c1, c2, c3, c4, c5, c6); | |
| 68 | + // composite partition key | |
| 69 | + String cql2 = String.format("CREATE TABLE %s.\"MyTable2\" (" | |
| 70 | + + "%s text, " | |
| 71 | + + "%s text, " | |
| 72 | + + "%s text, " | |
| 73 | + + "%s text, " | |
| 74 | + + "%s text, " | |
| 75 | + + "%s text, " | |
| 76 | + + "%s text, " | |
| 77 | + + "PRIMARY KEY ((%s, %s), %s, %s, %s, %s)" | |
| 78 | + + ")", keyspace, c1, c2, c3, c4, c5, c6, c7, c1, c2, c3, c4, c5, c6); | |
| 79 | + // when | |
| 80 | + execute(cql1, cql2); | |
| 81 | + TableMetadata table1 = cluster().getMetadata().getKeyspace(keyspace).getTable("\"MyTable1\""); | |
| 82 | + TableMetadata table2 = cluster().getMetadata().getKeyspace(keyspace).getTable("\"MyTable2\""); | |
| 83 | + // then | |
| 84 | + assertThat(table1) | |
| 85 | + .hasColumn(c1) | |
| 86 | + .hasColumn(c2) | |
| 87 | + .hasColumn(c3) | |
| 88 | + .hasColumn(c4) | |
| 89 | + .hasColumn(c5) | |
| 90 | + .hasColumn(c6) | |
| 91 | + .hasColumn(c7); | |
| 92 | + assertThat(table1.asCQLQuery()).startsWith(cql1); | |
| 93 | + assertThat(table2) | |
| 94 | + .hasColumn(c1) | |
| 95 | + .hasColumn(c2) | |
| 96 | + .hasColumn(c3) | |
| 97 | + .hasColumn(c4) | |
| 98 | + .hasColumn(c5) | |
| 99 | + .hasColumn(c6) | |
| 100 | + .hasColumn(c7); | |
| 101 | + assertThat(table2.asCQLQuery()).startsWith(cql2); | |
| 102 | + execute( | |
| 103 | + "DROP TABLE \"MyTable1\"", | |
| 104 | + "DROP TABLE \"MyTable2\"", | |
| 105 | + table1.asCQLQuery(), | |
| 106 | + table2.asCQLQuery() | |
| 107 | + ); | |
| 108 | + } | |
| 109 | + | |
| 38 | 110 | } |