Elffuss BraidHub

kikocis/java-driver public

JAVA-988: Metadata.handleId should handle escaped double quotes.
Changeset801f5af815cb6106babe8f60196f51ac7066703140f824a0165d5cd502467eee
Authorolim7t <omichallat+github@gmail.com> · 2015-11-13 19:12 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-11-13 19:12 +01:00
Parentsee4e8586c278

Browse the files at this changeset

3 files changed

modified changelog/README.md +5 −0
@@ -1,5 +1,10 @@
11 ## Changelog
22
3+### 2.1.10 (in progress)
4+
5+- [bug] JAVA-988: Metadata.handleId should handle escaped double quotes.
6+
7+
38 ### 2.1.9
49
510 - [bug] Fix implementation of UserType.hashCode() (JAVA-942)
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +21 −9
@@ -46,8 +46,8 @@
4646 private final ConcurrentMap<String, KeyspaceMetadata> keyspaces = new ConcurrentHashMap<String, KeyspaceMetadata>();
4747 volatile TokenMap tokenMap;
4848
49- private static final Pattern cqlId = Pattern.compile("\\w+");
50- private static final Pattern lowercaseId = Pattern.compile("[a-z][a-z0-9_]*");
49+ private static final Pattern alphanumeric = Pattern.compile("\\w+"); // this includes _
50+ private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*");
5151
5252 Metadata(Cluster.Manager cluster) {
5353 this.cluster = cluster;
@@ -387,20 +387,32 @@
387387 return hosts.values();
388388 }
389389
390- // Deal with case sensitivity for a given keyspace or table id
390+ /*
391+ * Deal with case sensitivity for a given element id (keyspace, table, column, etc.)
392+ *
393+ * This method is used to convert identifiers provided by the client (through methods such as getKeyspace(String)),
394+ * to the format used internally by the driver.
395+ *
396+ * We expect client-facing APIs to behave like cqlsh, that is:
397+ * - identifiers that are mixed-case or contain special characters should be quoted.
398+ * - unquoted identifiers will be lowercased: getKeyspace("Foo") will look for a keyspace named "foo"
399+ */
391400 static String handleId(String id) {
392401 // Shouldn't really happen for this method, but no reason to fail here
393402 if (id == null)
394403 return null;
395404
396- if (cqlId.matcher(id).matches())
405+ if (alphanumeric.matcher(id).matches())
397406 return id.toLowerCase();
398407
399- // Check if it's enclosed in quotes. If it is, remove them
408+ // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes
400409 if (id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"')
401- return id.substring(1, id.length() - 1);
402-
403- // otherwise, just return the id.
410+ return id.substring(1, id.length() - 1).replaceAll("\"\"", "\"");
411+
412+ // Otherwise, just return the id.
413+ // Note that this is a bit at odds with the rules explained above, because the client can pass an
414+ // identifier that contains special characters, without the need to quote it.
415+ // Still it's better to be lenient here rather than throwing an exception.
404416 return id;
405417 }
406418
@@ -409,7 +421,7 @@
409421 // but to get a nicer output we don't do it if it's not necessary.
410422 static String escapeId(String ident) {
411423 // we don't need to escape if it's lowercase and match non-quoted CQL3 ids.
412- return lowercaseId.matcher(ident).matches() ? ident : quote(ident);
424+ return lowercaseAlphanumeric.matcher(ident).matches() ? ident : quote(ident);
413425 }
414426
415427 /**
modified driver-core/src/test/java/com/datastax/driver/core/MetadataTest.java +23 −0
@@ -127,4 +127,27 @@
127127 }
128128 return tokensByHost;
129129 }
130+
131+ @Test(groups = "unit")
132+ public void handleId_should_lowercase_unquoted_alphanumeric_identifiers() {
133+ assertThat(Metadata.handleId("FooBar1")).isEqualTo("foobar1");
134+ assertThat(Metadata.handleId("Foo_Bar_1")).isEqualTo("foo_bar_1");
135+ }
136+
137+ @Test(groups = "unit")
138+ public void handleId_should_unquote_and_preserve_case_of_quoted_identifiers() {
139+ assertThat(Metadata.handleId("\"FooBar1\"")).isEqualTo("FooBar1");
140+ assertThat(Metadata.handleId("\"Foo_Bar_1\"")).isEqualTo("Foo_Bar_1");
141+ assertThat(Metadata.handleId("\"Foo Bar 1\"")).isEqualTo("Foo Bar 1");
142+ }
143+
144+ @Test(groups = "unit")
145+ public void handleId_should_unescape_duplicate_double_quotes_in_quoted_identifiers() {
146+ assertThat(Metadata.handleId("\"Foo\"\"Bar\"")).isEqualTo("Foo\"Bar");
147+ }
148+
149+ @Test(groups = "unit")
150+ public void handleId_should_preserve_unquoted_non_alphanumeric_identifiers() {
151+ assertThat(Metadata.handleId("Foo Bar")).isEqualTo("Foo Bar");
152+ }
130153 }