kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 56ea1f21527ec94b9827c73df8531af59e204ed60b53e13be1e6288b0cd8d786 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-13 20:05 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-11-13 20:05 +01:00 |
| Parents | ee4e8586c278, 801f5af815cb |
3 files changed
modified changelog/README.md +5 −0
| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | ## Changelog |
| 2 | 2 | |
| 3 | +### 2.1.10 (in progress) | |
| 4 | + | |
| 5 | +- [bug] JAVA-988: Metadata.handleId should handle escaped double quotes. | |
| 6 | + | |
| 7 | + | |
| 3 | 8 | ### 2.1.9 |
| 4 | 9 | |
| 5 | 10 | - [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 @@ | ||
| 46 | 46 | private final ConcurrentMap<String, KeyspaceMetadata> keyspaces = new ConcurrentHashMap<String, KeyspaceMetadata>(); |
| 47 | 47 | volatile TokenMap tokenMap; |
| 48 | 48 | |
| 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_]*"); | |
| 51 | 51 | |
| 52 | 52 | Metadata(Cluster.Manager cluster) { |
| 53 | 53 | this.cluster = cluster; |
| @@ -387,20 +387,32 @@ | ||
| 387 | 387 | return hosts.values(); |
| 388 | 388 | } |
| 389 | 389 | |
| 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 | + */ | |
| 391 | 400 | static String handleId(String id) { |
| 392 | 401 | // Shouldn't really happen for this method, but no reason to fail here |
| 393 | 402 | if (id == null) |
| 394 | 403 | return null; |
| 395 | 404 | |
| 396 | - if (cqlId.matcher(id).matches()) | |
| 405 | + if (alphanumeric.matcher(id).matches()) | |
| 397 | 406 | return id.toLowerCase(); |
| 398 | 407 | |
| 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 | |
| 400 | 409 | 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. | |
| 404 | 416 | return id; |
| 405 | 417 | } |
| 406 | 418 | |
| @@ -409,7 +421,7 @@ | ||
| 409 | 421 | // but to get a nicer output we don't do it if it's not necessary. |
| 410 | 422 | static String escapeId(String ident) { |
| 411 | 423 | // 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); | |
| 413 | 425 | } |
| 414 | 426 | |
| 415 | 427 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/MetadataTest.java +23 −0
| @@ -127,4 +127,27 @@ | ||
| 127 | 127 | } |
| 128 | 128 | return tokensByHost; |
| 129 | 129 | } |
| 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 | + } | |
| 130 | 153 | } |