kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 688aaba7241c3ce999ec01320033fbcd26cd9879db1627c681917c89e481b1cc |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-02-20 18:49 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-02-20 18:49 +01:00 |
| Parents | 6d6a6c256bb8, 8a7467807397 |
4 files changed
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedRow.java +10 −10
| @@ -84,19 +84,19 @@ | ||
| 84 | 84 | |
| 85 | 85 | @Override |
| 86 | 86 | public Token getToken(String name) { |
| 87 | - // first, try with `token(name)`, preserving case sensitivity | |
| 88 | - String tokenName = (name.length() >= 2 && name.charAt(0) == '"' && name.charAt(name.length() - 1) == '"') | |
| 89 | - ? "\"token(" + name.substring(1, name.length() - 1) + ")\"" | |
| 90 | - : "token(" + name + ")"; | |
| 91 | - int[] indexes = metadata.findAllIdx(tokenName); | |
| 92 | - if (indexes != null) { | |
| 93 | - return getToken(indexes[0]); | |
| 94 | - } | |
| 95 | - | |
| 96 | - // Otherwise, use normal name | |
| 97 | 87 | return getToken(metadata.getFirstIdx(name)); |
| 98 | 88 | } |
| 99 | 89 | |
| 90 | + public Token getPartitionKeyToken() { | |
| 91 | + int i = 0; | |
| 92 | + for (ColumnDefinitions.Definition column : metadata) { | |
| 93 | + if (column.getName().matches("token(.*)")) | |
| 94 | + return getToken(i); | |
| 95 | + i++; | |
| 96 | + } | |
| 97 | + throw new IllegalStateException("Found no column named 'token(...)'. If the column is aliased, use getToken(String)."); | |
| 98 | + } | |
| 99 | + | |
| 100 | 100 | @Override |
| 101 | 101 | public String toString() { |
| 102 | 102 | StringBuilder sb = new StringBuilder(); |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +49 −0
| @@ -726,6 +726,9 @@ | ||
| 726 | 726 | |
| 727 | 727 | /** |
| 728 | 728 | * Sets the {@code i}th value to the provided {@link Token}. |
| 729 | + * <p> | |
| 730 | + * {@link #setPartitionKeyToken(Token)} should generally be preferred if you | |
| 731 | + * have a single token variable. | |
| 729 | 732 | * |
| 730 | 733 | * @param i the index of the variable to set. |
| 731 | 734 | * @param v the value to set. |
| @@ -741,6 +744,18 @@ | ||
| 741 | 744 | /** |
| 742 | 745 | * Sets the value for (all occurrences of) variable {@code name} to the |
| 743 | 746 | * provided token. |
| 747 | + * <p> | |
| 748 | + * {@link #setPartitionKeyToken(Token)} should generally be preferred if you | |
| 749 | + * have a single token variable. | |
| 750 | + * <p> | |
| 751 | + * If you have multiple token variables, use positional binding ({@link #setToken(int, Token)}, | |
| 752 | + * or named bind markers: | |
| 753 | + * <pre> | |
| 754 | + * {@code | |
| 755 | + * PreparedStatement pst = session.prepare("SELECT * FROM my_table WHERE token(k) > :min AND token(k) <= :max"); | |
| 756 | + * BoundStatement b = pst.bind().setToken("min", minToken).setToken("max", maxToken); | |
| 757 | + * } | |
| 758 | + * </pre> | |
| 744 | 759 | * |
| 745 | 760 | * @param name the name of the variable to set; if multiple variables |
| 746 | 761 | * {@code name} are prepared, all of them are set. |
| @@ -756,6 +771,40 @@ | ||
| 756 | 771 | return wrapper.setToken(name, v); |
| 757 | 772 | } |
| 758 | 773 | |
| 774 | + /** | |
| 775 | + * Sets the value for (all occurrences of) variable "{@code partition key token}" | |
| 776 | + * to the provided token (this is the name generated by Cassandra for markers | |
| 777 | + * corresponding to a {@code token(...)} call). | |
| 778 | + * <p> | |
| 779 | + * This method is a shorthand for statements with a single token variable: | |
| 780 | + * <pre> | |
| 781 | + * {@code | |
| 782 | + * Token token = ... | |
| 783 | + * PreparedStatement pst = session.prepare("SELECT * FROM my_table WHERE token(k) = ?"); | |
| 784 | + * BoundStatement b = pst.bind().setPartitionKeyToken(token); | |
| 785 | + * } | |
| 786 | + * </pre> | |
| 787 | + * If you have multiple token variables, use positional binding ({@link #setToken(int, Token)}, | |
| 788 | + * or named bind markers: | |
| 789 | + * <pre> | |
| 790 | + * {@code | |
| 791 | + * PreparedStatement pst = session.prepare("SELECT * FROM my_table WHERE token(k) > :min AND token(k) <= :max"); | |
| 792 | + * BoundStatement b = pst.bind().setToken("min", minToken).setToken("max", maxToken); | |
| 793 | + * } | |
| 794 | + * </pre> | |
| 795 | + * | |
| 796 | + * @param v the value to set. | |
| 797 | + * @return this BoundStatement. | |
| 798 | + * | |
| 799 | + * @throws IllegalArgumentException if {@code name} is not a prepared | |
| 800 | + * variable, that is, if {@code !this.preparedStatement().variables().names().contains(name)}. | |
| 801 | + * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 802 | + * not of the type of the token's value. | |
| 803 | + */ | |
| 804 | + public BoundStatement setPartitionKeyToken(Token v) { | |
| 805 | + return setToken("partition key token", v); | |
| 806 | + } | |
| 807 | + | |
| 759 | 808 | /** |
| 760 | 809 | * Sets the {@code i}th value to the provided list. |
| 761 | 810 | * <p> |
modified driver-core/src/main/java/com/datastax/driver/core/Row.java +28 −18
| @@ -435,10 +435,12 @@ | ||
| 435 | 435 | |
| 436 | 436 | /** |
| 437 | 437 | * Returns the {@code i}th value of this row as a {@link Token}. |
| 438 | + * <p> | |
| 439 | + * {@link #getPartitionKeyToken()} should generally be preferred to this method (unless the | |
| 440 | + * token column is aliased). | |
| 438 | 441 | * |
| 439 | 442 | * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. |
| 440 | 443 | * @return the value of the {@code i}th column in this row as an Token. |
| 441 | - * If the value is NULL, {@code null} is returned. | |
| 442 | 444 | * |
| 443 | 445 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 444 | 446 | * @throws InvalidTypeException if column {@code i} is not of the type of token values |
| @@ -449,26 +451,11 @@ | ||
| 449 | 451 | /** |
| 450 | 452 | * Returns the value of column {@code name} as a {@link Token}. |
| 451 | 453 | * <p> |
| 452 | - * This method will first try with {@code token(name)}, because in most cases the column | |
| 453 | - * is named that way since it results from the application of a CQL function: | |
| 454 | - * <pre> | |
| 455 | - * {@code | |
| 456 | - * ResultSet rs = session.execute("SELECT token(k) FROM my_table WHERE k = 1"); | |
| 457 | - * Token token = rs.one().getToken("k"); // retrieves token(k) | |
| 458 | - * } | |
| 459 | - * </pre> | |
| 460 | - * If that fails, it will try will {@code name}. This handles the case where the column | |
| 461 | - * was aliased: | |
| 462 | - * <pre> | |
| 463 | - * {@code | |
| 464 | - * ResultSet rs = session.execute("SELECT token(k) AS t FROM my_table WHERE k = 1"); | |
| 465 | - * Token token = rs.one().getToken("t"); | |
| 466 | - * } | |
| 467 | - * </pre> | |
| 454 | + * {@link #getPartitionKeyToken()} should generally be preferred to this method (unless the | |
| 455 | + * token column is aliased). | |
| 468 | 456 | * |
| 469 | 457 | * @param name the name of the column to retrieve. |
| 470 | 458 | * @return the value of column {@code name} as a Token. |
| 471 | - * If the value is NULL, {@code null} is returned. | |
| 472 | 459 | * |
| 473 | 460 | * @throws IllegalArgumentException if {@code name} is not part of the |
| 474 | 461 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| @@ -477,6 +464,29 @@ | ||
| 477 | 464 | */ |
| 478 | 465 | public Token getToken(String name); |
| 479 | 466 | |
| 467 | + /** | |
| 468 | + * Returns the value of the first column containing a {@link Token}. | |
| 469 | + * <p> | |
| 470 | + * This method is a shorthand for queries returning a single token in an unaliased | |
| 471 | + * column. It will look for the first name matching {@code token(...)}: | |
| 472 | + * <pre> | |
| 473 | + * {@code | |
| 474 | + * ResultSet rs = session.execute("SELECT token(k) FROM my_table WHERE k = 1"); | |
| 475 | + * Token token = rs.one().getPartitionKeyToken(); // retrieves token(k) | |
| 476 | + * } | |
| 477 | + * </pre> | |
| 478 | + * If that doesn't work for you (for example, if you're using an alias), use | |
| 479 | + * {@link #getToken(int)} or {@link #getToken(String)}. | |
| 480 | + * | |
| 481 | + * @return the value of column {@code name} as a Token. | |
| 482 | + * | |
| 483 | + * @throws IllegalStateException if no column named {@code token(...)} exists in this | |
| 484 | + * ResultSet. | |
| 485 | + * @throws InvalidTypeException if the first column named {@code token(...)} is not of | |
| 486 | + * the type of token values for this cluster (this depends on the configured partitioner). | |
| 487 | + */ | |
| 488 | + public Token getPartitionKeyToken(); | |
| 489 | + | |
| 480 | 490 | /** |
| 481 | 491 | * Returns the {@code i}th value of this row as a list. |
| 482 | 492 | * |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +5 −7
| @@ -166,21 +166,16 @@ | ||
| 166 | 166 | Token token = row.getToken(0); |
| 167 | 167 | assertThat(token.getType()).isEqualTo(expectedTokenType); |
| 168 | 168 | |
| 169 | - // get by "base" column name (will add token() around it): | |
| 170 | 169 | assertThat( |
| 171 | - row.getToken("i") | |
| 170 | + row.getPartitionKeyToken() | |
| 172 | 171 | ).isEqualTo(token); |
| 173 | 172 | |
| 174 | - // get by name: | |
| 173 | + // get by name when column is aliased: | |
| 175 | 174 | assertThat( |
| 176 | 175 | session.execute("SELECT token(i) AS t FROM test.foo WHERE i = 1").one() |
| 177 | 176 | .getToken("t") |
| 178 | 177 | ).isEqualTo(token); |
| 179 | 178 | |
| 180 | - // get by "base" column name when it's case-sensitive | |
| 181 | - session.execute("SELECT token(\"caseSensitiveKey\") FROM test.foo2 WHERE \"caseSensitiveKey\" = 1").one() | |
| 182 | - .getToken("\"caseSensitiveKey\""); | |
| 183 | - | |
| 184 | 179 | |
| 185 | 180 | PreparedStatement pst = session.prepare("SELECT * FROM test.foo WHERE token(i) = ?"); |
| 186 | 181 | row = session.execute(pst.bind(token)).one(); |
| @@ -189,6 +184,9 @@ | ||
| 189 | 184 | row = session.execute(pst.bind().setToken(0, token)).one(); |
| 190 | 185 | assertThat(row.getInt(0)).isEqualTo(1); |
| 191 | 186 | |
| 187 | + row = session.execute(pst.bind().setPartitionKeyToken(token)).one(); | |
| 188 | + assertThat(row.getInt(0)).isEqualTo(1); | |
| 189 | + | |
| 192 | 190 | PreparedStatement pst2 = session.prepare("SELECT * FROM test.foo WHERE token(i) = :myToken"); |
| 193 | 191 | row = session.execute(pst2.bind().setToken("myToken", token)).one(); |
| 194 | 192 | assertThat(row.getInt(0)).isEqualTo(1); |