Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changeset688aaba7241c3ce999ec01320033fbcd26cd9879db1627c681917c89e481b1cc
Authorolim7t <omichallat+github@gmail.com> · 2015-02-20 18:49 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-02-20 18:49 +01:00
Parents6d6a6c256bb8, 8a7467807397

Browse the files at this changeset

4 files changed

modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedRow.java +10 −10
@@ -84,19 +84,19 @@
8484
8585 @Override
8686 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
9787 return getToken(metadata.getFirstIdx(name));
9888 }
9989
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+
100100 @Override
101101 public String toString() {
102102 StringBuilder sb = new StringBuilder();
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +49 −0
@@ -726,6 +726,9 @@
726726
727727 /**
728728 * 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.
729732 *
730733 * @param i the index of the variable to set.
731734 * @param v the value to set.
@@ -741,6 +744,18 @@
741744 /**
742745 * Sets the value for (all occurrences of) variable {@code name} to the
743746 * 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>
744759 *
745760 * @param name the name of the variable to set; if multiple variables
746761 * {@code name} are prepared, all of them are set.
@@ -756,6 +771,40 @@
756771 return wrapper.setToken(name, v);
757772 }
758773
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+
759808 /**
760809 * Sets the {@code i}th value to the provided list.
761810 * <p>
modified driver-core/src/main/java/com/datastax/driver/core/Row.java +28 −18
@@ -435,10 +435,12 @@
435435
436436 /**
437437 * 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).
438441 *
439442 * @param i the index ({@code 0 <= i < size()}) of the column to retrieve.
440443 * @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.
442444 *
443445 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
444446 * @throws InvalidTypeException if column {@code i} is not of the type of token values
@@ -449,26 +451,11 @@
449451 /**
450452 * Returns the value of column {@code name} as a {@link Token}.
451453 * <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).
468456 *
469457 * @param name the name of the column to retrieve.
470458 * @return the value of column {@code name} as a Token.
471- * If the value is NULL, {@code null} is returned.
472459 *
473460 * @throws IllegalArgumentException if {@code name} is not part of the
474461 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
@@ -477,6 +464,29 @@
477464 */
478465 public Token getToken(String name);
479466
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+
480490 /**
481491 * Returns the {@code i}th value of this row as a list.
482492 *
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +5 −7
@@ -166,21 +166,16 @@
166166 Token token = row.getToken(0);
167167 assertThat(token.getType()).isEqualTo(expectedTokenType);
168168
169- // get by "base" column name (will add token() around it):
170169 assertThat(
171- row.getToken("i")
170+ row.getPartitionKeyToken()
172171 ).isEqualTo(token);
173172
174- // get by name:
173+ // get by name when column is aliased:
175174 assertThat(
176175 session.execute("SELECT token(i) AS t FROM test.foo WHERE i = 1").one()
177176 .getToken("t")
178177 ).isEqualTo(token);
179178
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-
184179
185180 PreparedStatement pst = session.prepare("SELECT * FROM test.foo WHERE token(i) = ?");
186181 row = session.execute(pst.bind(token)).one();
@@ -189,6 +184,9 @@
189184 row = session.execute(pst.bind().setToken(0, token)).one();
190185 assertThat(row.getInt(0)).isEqualTo(1);
191186
187+ row = session.execute(pst.bind().setPartitionKeyToken(token)).one();
188+ assertThat(row.getInt(0)).isEqualTo(1);
189+
192190 PreparedStatement pst2 = session.prepare("SELECT * FROM test.foo WHERE token(i) = :myToken");
193191 row = session.execute(pst2.bind().setToken("myToken", token)).one();
194192 assertThat(row.getInt(0)).isEqualTo(1);