kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 06afebc26b362d2442cc4477633bee3e444b5749815205ff7ff7129ac6ce6450 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2016-08-31 13:36 -05:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-08-31 11:36 -07:00 |
| Parents | 846b89ef8105 |
4 files changed
modified changelog/README.md +1 −0
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | - [improvement] JAVA-1261: Throw error when attempting to page in I/O thread. |
| 7 | 7 | - [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126. |
| 8 | 8 | - [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence |
| 9 | +- [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting. | |
| 9 | 10 | |
| 10 | 11 | |
| 11 | 12 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +2 −9
| @@ -135,14 +135,7 @@ | ||
| 135 | 135 | return id.toLowerCase(); |
| 136 | 136 | |
| 137 | 137 | // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes |
| 138 | - if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 139 | - return id.substring(1, id.length() - 1).replaceAll("\"\"", "\""); | |
| 140 | - | |
| 141 | - // Otherwise, just return the id. | |
| 142 | - // Note that this is a bit at odds with the rules explained above, because the client can pass an | |
| 143 | - // identifier that contains special characters, without the need to quote it. | |
| 144 | - // Still it's better to be lenient here rather than throwing an exception. | |
| 145 | - return id; | |
| 138 | + return ParseUtils.unDoubleQuote(id); | |
| 146 | 139 | } |
| 147 | 140 | |
| 148 | 141 | // Escape a CQL3 identifier based on its value as read from the schema |
| @@ -217,7 +210,7 @@ | ||
| 217 | 210 | * or even {@link Cluster#connect(String)}. |
| 218 | 211 | */ |
| 219 | 212 | public static String quote(String id) { |
| 220 | - return '"' + id.replace("\"", "\"\"") + '"'; | |
| 213 | + return ParseUtils.doubleQuote(id); | |
| 221 | 214 | } |
| 222 | 215 | |
| 223 | 216 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +141 −25
| @@ -232,7 +232,7 @@ | ||
| 232 | 232 | * by single quotes, and {@code false} otherwise. |
| 233 | 233 | */ |
| 234 | 234 | public static boolean isQuoted(String value) { |
| 235 | - return value != null && value.length() > 1 && value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\''; | |
| 235 | + return isQuoted(value, '\''); | |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| @@ -243,7 +243,7 @@ | ||
| 243 | 243 | * @return The quoted string. |
| 244 | 244 | */ |
| 245 | 245 | public static String quote(String value) { |
| 246 | - return '\'' + replaceChar(value, '\'', "''") + '\''; | |
| 246 | + return quote(value, '\''); | |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
| @@ -254,9 +254,41 @@ | ||
| 254 | 254 | * @return The unquoted string. |
| 255 | 255 | */ |
| 256 | 256 | public static String unquote(String value) { |
| 257 | - if (!isQuoted(value)) | |
| 258 | - return value; | |
| 259 | - return value.substring(1, value.length() - 1).replace("''", "'"); | |
| 257 | + return unquote(value, '\''); | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 261 | + * Return {@code true} if the given string is surrounded | |
| 262 | + * by double quotes, and {@code false} otherwise. | |
| 263 | + * | |
| 264 | + * @param value The string to inspect. | |
| 265 | + * @return {@code true} if the given string is surrounded | |
| 266 | + * by double quotes, and {@code false} otherwise. | |
| 267 | + */ | |
| 268 | + public static boolean isDoubleQuoted(String value) { | |
| 269 | + return isQuoted(value, '\"'); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Double quote the given string; double quotes are escaped. | |
| 274 | + * If the given string is null, this method returns a quoted empty string ({@code ""}). | |
| 275 | + * | |
| 276 | + * @param value The value to double quote. | |
| 277 | + * @return The double quoted string. | |
| 278 | + */ | |
| 279 | + public static String doubleQuote(String value) { | |
| 280 | + return quote(value, '"'); | |
| 281 | + } | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * Unquote the given string if it is double quoted; double quotes are unescaped. | |
| 285 | + * If the given string is not double quoted, it is returned without any modification. | |
| 286 | + * | |
| 287 | + * @param value The string to un-double quote. | |
| 288 | + * @return The un-double quoted string. | |
| 289 | + */ | |
| 290 | + public static String unDoubleQuote(String value) { | |
| 291 | + return unquote(value, '"'); | |
| 260 | 292 | } |
| 261 | 293 | |
| 262 | 294 | /** |
| @@ -428,45 +460,129 @@ | ||
| 428 | 460 | } |
| 429 | 461 | |
| 430 | 462 | /** |
| 431 | - * Simple method to replace a single character. | |
| 432 | - * {@code String.replace()} is a bit too inefficient (see JAVA-67). | |
| 433 | - * This methods is specially useful for escaping single quotes | |
| 434 | - * in CQL strings: | |
| 435 | - * <p/> | |
| 436 | - * <pre>{@code | |
| 437 | - * ParseUtils.replaceChar(value, '\'', "''"); | |
| 438 | - * }</pre> | |
| 463 | + * Return {@code true} if the given string is surrounded | |
| 464 | + * by the quote character given, and {@code false} otherwise. | |
| 465 | + * | |
| 466 | + * @param value The string to inspect. | |
| 467 | + * @return {@code true} if the given string is surrounded | |
| 468 | + * by the quote character, and {@code false} otherwise. | |
| 469 | + */ | |
| 470 | + private static boolean isQuoted(String value, char quoteChar) { | |
| 471 | + return value != null && value.length() > 1 | |
| 472 | + && value.charAt(0) == quoteChar && value.charAt(value.length() - 1) == quoteChar; | |
| 473 | + } | |
| 474 | + | |
| 475 | + /** | |
| 476 | + * @param quoteChar " or ' | |
| 477 | + * @return A quoted empty string. | |
| 478 | + */ | |
| 479 | + private static String emptyQuoted(char quoteChar) { | |
| 480 | + // don't handle non quote characters, this is done so that these are interned and don't create | |
| 481 | + // repeated empty quoted strings. | |
| 482 | + assert quoteChar == '"' || quoteChar == '\''; | |
| 483 | + if (quoteChar == '"') | |
| 484 | + return "\"\""; | |
| 485 | + else | |
| 486 | + return "''"; | |
| 487 | + } | |
| 488 | + | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * Quotes text and escapes any existing quotes in the text. | |
| 492 | + * {@code String.replace()} is a bit too inefficient (see JAVA-67, JAVA-1262). | |
| 439 | 493 | * |
| 440 | - * @param text The text. | |
| 441 | - * @param search The character to search for. | |
| 442 | - * @param replacement The replacement. | |
| 443 | - * @return The text with all occurrences of {@code search} replaced with {@code replacement}. | |
| 494 | + * @param text The text. | |
| 495 | + * @param quoteChar The character to use as a quote. | |
| 496 | + * @return The text with surrounded in quotes with all existing quotes escaped with (i.e. ' becomes '') | |
| 444 | 497 | */ |
| 445 | - private static String replaceChar(String text, char search, String replacement) { | |
| 498 | + private static String quote(String text, char quoteChar) { | |
| 446 | 499 | if (text == null || text.isEmpty()) |
| 447 | - return ""; | |
| 500 | + return emptyQuoted(quoteChar); | |
| 448 | 501 | |
| 449 | 502 | int nbMatch = 0; |
| 450 | 503 | int start = -1; |
| 451 | 504 | do { |
| 452 | - start = text.indexOf(search, start + 1); | |
| 505 | + start = text.indexOf(quoteChar, start + 1); | |
| 453 | 506 | if (start != -1) |
| 454 | 507 | ++nbMatch; |
| 455 | 508 | } while (start != -1); |
| 456 | 509 | |
| 510 | + // no quotes found that need to be escaped, simply surround in quotes and return. | |
| 457 | 511 | if (nbMatch == 0) |
| 512 | + return quoteChar + text + quoteChar; | |
| 513 | + | |
| 514 | + // 2 for beginning and end quotes. | |
| 515 | + // length for original text | |
| 516 | + // nbMatch for escape characters to add to quotes to be escaped. | |
| 517 | + int newLength = 2 + text.length() + nbMatch; | |
| 518 | + char[] result = new char[newLength]; | |
| 519 | + result[0] = quoteChar; | |
| 520 | + result[newLength - 1] = quoteChar; | |
| 521 | + int newIdx = 1; | |
| 522 | + for (int i = 0; i < text.length(); i++) { | |
| 523 | + char c = text.charAt(i); | |
| 524 | + if (c == quoteChar) { | |
| 525 | + // escape quote with another occurrence. | |
| 526 | + result[newIdx++] = c; | |
| 527 | + result[newIdx++] = c; | |
| 528 | + } else { | |
| 529 | + result[newIdx++] = c; | |
| 530 | + } | |
| 531 | + } | |
| 532 | + return new String(result); | |
| 533 | + } | |
| 534 | + | |
| 535 | + /** | |
| 536 | + * Unquotes text and unescapes non surrounding quotes. | |
| 537 | + * {@code String.replace()} is a bit too inefficient (see JAVA-67, JAVA-1262). | |
| 538 | + * | |
| 539 | + * @param text The text | |
| 540 | + * @param quoteChar The character to use as a quote. | |
| 541 | + * @return The text with surrounding quotes removed and non surrounding quotes unescaped (i.e. '' becomes ') | |
| 542 | + */ | |
| 543 | + private static String unquote(String text, char quoteChar) { | |
| 544 | + if (!isQuoted(text, quoteChar)) | |
| 458 | 545 | return text; |
| 459 | 546 | |
| 460 | - int newLength = text.length() + nbMatch * (replacement.length() - 1); | |
| 547 | + if (text.length() == 2) | |
| 548 | + return ""; | |
| 549 | + | |
| 550 | + String search = emptyQuoted(quoteChar); | |
| 551 | + int nbMatch = 0; | |
| 552 | + int start = -1; | |
| 553 | + do { | |
| 554 | + start = text.indexOf(search, start + 2); | |
| 555 | + // ignore the second to last character occurrence, as the last character is a quote. | |
| 556 | + if (start != -1 && start != text.length() - 2) | |
| 557 | + ++nbMatch; | |
| 558 | + } while (start != -1); | |
| 559 | + | |
| 560 | + // no escaped quotes found, simply remove surrounding quotes and return. | |
| 561 | + if (nbMatch == 0) | |
| 562 | + return text.substring(1, text.length() - 1); | |
| 563 | + | |
| 564 | + // length of the new string will be its current length - the number of occurrences. | |
| 565 | + int newLength = text.length() - nbMatch - 2; | |
| 461 | 566 | char[] result = new char[newLength]; |
| 462 | 567 | int newIdx = 0; |
| 463 | - for (int i = 0; i < text.length(); i++) { | |
| 568 | + // track whenever a quoteChar is encountered and the previous character is not a quoteChar. | |
| 569 | + boolean firstFound = false; | |
| 570 | + for (int i = 1; i < text.length() - 1; i++) { | |
| 464 | 571 | char c = text.charAt(i); |
| 465 | - if (c == search) { | |
| 466 | - for (int r = 0; r < replacement.length(); r++) | |
| 467 | - result[newIdx++] = replacement.charAt(r); | |
| 572 | + if (c == quoteChar) { | |
| 573 | + if (firstFound) { | |
| 574 | + // The previous character was a quoteChar, don't add this to result, this action in | |
| 575 | + // effect removes consecutive quotes. | |
| 576 | + firstFound = false; | |
| 577 | + } else { | |
| 578 | + // found a quoteChar and the previous character was not a quoteChar, include in result. | |
| 579 | + firstFound = true; | |
| 580 | + result[newIdx++] = c; | |
| 581 | + } | |
| 468 | 582 | } else { |
| 583 | + // non quoteChar encountered, include in result. | |
| 469 | 584 | result[newIdx++] = c; |
| 585 | + firstFound = false; | |
| 470 | 586 | } |
| 471 | 587 | } |
| 472 | 588 | return new String(result); |
modified driver-core/src/test/java/com/datastax/driver/core/ParseUtilsTest.java +45 −0
| @@ -43,6 +43,9 @@ | ||
| 43 | 43 | assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo"); |
| 44 | 44 | assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted |
| 45 | 45 | assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'"); |
| 46 | + assertThat(ParseUtils.unquote("'''")).isEqualTo("'"); | |
| 47 | + assertThat(ParseUtils.unquote("''foo'")).isEqualTo("'foo"); | |
| 48 | + assertThat(ParseUtils.unquote("'foo''")).isEqualTo("foo'"); | |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | @Test(groups = "unit") |
| @@ -60,4 +63,46 @@ | ||
| 60 | 63 | assertThat(ParseUtils.isQuoted("'''foo'''")).isTrue(); |
| 61 | 64 | } |
| 62 | 65 | |
| 66 | + @Test(groups = "unit") | |
| 67 | + public void testDoubleQuote() { | |
| 68 | + assertThat(ParseUtils.doubleQuote(null)).isEqualTo("\"\""); | |
| 69 | + assertThat(ParseUtils.doubleQuote("")).isEqualTo("\"\""); | |
| 70 | + assertThat(ParseUtils.doubleQuote(" ")).isEqualTo("\" \""); | |
| 71 | + assertThat(ParseUtils.doubleQuote("foo")).isEqualTo("\"foo\""); | |
| 72 | + assertThat(ParseUtils.doubleQuote(" \"foo\" ")).isEqualTo("\" \"\"foo\"\" \""); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Test(groups = "unit") | |
| 76 | + public void testDoubleUnquote() { | |
| 77 | + assertThat(ParseUtils.unDoubleQuote(null)).isNull(); | |
| 78 | + assertThat(ParseUtils.unDoubleQuote("")).isEqualTo(""); | |
| 79 | + assertThat(ParseUtils.unDoubleQuote(" ")).isEqualTo(" "); | |
| 80 | + assertThat(ParseUtils.unDoubleQuote("\"")).isEqualTo("\""); // malformed string left untouched | |
| 81 | + assertThat(ParseUtils.unDoubleQuote("foo")).isEqualTo("foo"); | |
| 82 | + assertThat(ParseUtils.unDoubleQuote("\"\"")).isEqualTo(""); | |
| 83 | + assertThat(ParseUtils.unDoubleQuote("\" \"")).isEqualTo(" "); | |
| 84 | + assertThat(ParseUtils.unDoubleQuote("\"foo")).isEqualTo("\"foo"); // malformed string left untouched | |
| 85 | + assertThat(ParseUtils.unDoubleQuote("\"foo\"")).isEqualTo("foo"); | |
| 86 | + assertThat(ParseUtils.unDoubleQuote(" \"foo\" ")).isEqualTo(" \"foo\" "); // considered unquoted | |
| 87 | + assertThat(ParseUtils.unDoubleQuote("\"\"\"foo\"\"\"")).isEqualTo("\"foo\""); | |
| 88 | + assertThat(ParseUtils.unDoubleQuote("\"\"\"")).isEqualTo("\""); | |
| 89 | + assertThat(ParseUtils.unDoubleQuote("\"\"foo\"")).isEqualTo("\"foo"); | |
| 90 | + assertThat(ParseUtils.unDoubleQuote("\"foo\"\"")).isEqualTo("foo\""); | |
| 91 | + } | |
| 92 | + | |
| 93 | + @Test(groups = "unit") | |
| 94 | + public void testIsDoubleQuoted() { | |
| 95 | + assertThat(ParseUtils.isDoubleQuoted(null)).isFalse(); | |
| 96 | + assertThat(ParseUtils.isDoubleQuoted("")).isFalse(); | |
| 97 | + assertThat(ParseUtils.isDoubleQuoted(" ")).isFalse(); | |
| 98 | + assertThat(ParseUtils.isDoubleQuoted("\"")).isFalse(); // malformed string considered unquoted | |
| 99 | + assertThat(ParseUtils.isDoubleQuoted("foo")).isFalse(); | |
| 100 | + assertThat(ParseUtils.isDoubleQuoted("\"\"")).isTrue(); | |
| 101 | + assertThat(ParseUtils.isDoubleQuoted("\" \"")).isTrue(); | |
| 102 | + assertThat(ParseUtils.isDoubleQuoted("\"foo")).isFalse(); // malformed string considered unquoted | |
| 103 | + assertThat(ParseUtils.isDoubleQuoted("\"foo\"")).isTrue(); | |
| 104 | + assertThat(ParseUtils.isDoubleQuoted(" \"foo\" ")).isFalse(); // considered unquoted | |
| 105 | + assertThat(ParseUtils.isDoubleQuoted("\"\"\"foo\"\"\"")).isTrue(); | |
| 106 | + } | |
| 107 | + | |
| 63 | 108 | } |