Elffuss BraidHub

kikocis/java-driver public

JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
Changeset8e976d7c1dbd389aad0fe08c1b7407386290153489012c2be6881f2a7ed13535
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-02 11:13 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-03 10:36 +01:00
Parentsc2cdc3b9eac6

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -25,6 +25,7 @@
2525 - [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI
2626 - [bug] JAVA-819: Expose more errors in RetryPolicy + provide idempotent-aware wrapper.
2727 - [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
28+- [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
2829
2930 Merged from 2.0 branch:
3031
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +21 −5
@@ -47,6 +47,15 @@
4747 private static final Pattern alphanumeric = Pattern.compile("\\w+"); // this includes _
4848 private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*");
4949
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+
5059 Metadata(Cluster.Manager cluster) {
5160 this.cluster = cluster;
5261 }
@@ -404,7 +413,7 @@
404413 return id.toLowerCase();
405414
406415 // 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) == '"')
408417 return id.substring(1, id.length() - 1).replaceAll("\"\"", "\"");
409418
410419 // Otherwise, just return the id.
@@ -418,8 +427,11 @@
418427 // tables. Because it comes from Cassandra, we could just always quote it,
419428 // but to get a nicer output we don't do it if it's not necessary.
420429 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);
423435 }
424436
425437 /**
@@ -430,8 +442,12 @@
430442 * the identifier in double quotes (see the
431443 * <a href="http://cassandra.apache.org/doc/cql3/CQL.html#identifiers">CQL documentation</a>
432444 * 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
434446 * 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.
435451 *
436452 * @param id the keyspace or table identifier.
437453 * @return {@code id} enclosed in double-quotes, for use in methods like
@@ -439,7 +455,7 @@
439455 * or even {@link Cluster#connect(String)}.
440456 */
441457 public static String quote(String id) {
442- return '"' + id + '"';
458+ return '"' + id.replace("\"", "\"\"") + '"';
443459 }
444460
445461 /**
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +2 −2
@@ -435,12 +435,12 @@
435435 sb.append("CREATE TABLE ").append(Metadata.escapeId(keyspace.getName())).append('.').append(Metadata.escapeId(name)).append(" (");
436436 newLine(sb, formatted);
437437 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);
439439
440440 // PK
441441 sb.append(spaces(4, formatted)).append("PRIMARY KEY (");
442442 if (partitionKey.size() == 1) {
443- sb.append(partitionKey.get(0).getName());
443+ sb.append(Metadata.escapeId(partitionKey.get(0).getName()));
444444 } else {
445445 sb.append('(');
446446 boolean first = true;
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +73 −1
@@ -17,7 +17,7 @@
1717
1818 import org.testng.annotations.Test;
1919
20-import static org.assertj.core.api.Assertions.assertThat;
20+import static com.datastax.driver.core.Assertions.assertThat;
2121
2222 @CCMConfig(clusterProvider = "createClusterBuilderNoDebouncing")
2323 public class TableMetadataTest extends CCMTestsSupport {
@@ -35,4 +35,76 @@
3535 TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("single_quote");
3636 assertThat(table.asCQLQuery()).contains("comment with single quote '' should work");
3737 }
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+
38110 }