Elffuss BraidHub

kikocis/java-driver public

Add BoundStatement#unset (JAVA-930).
Changeseta46a54587186fede7fa0f1447f8f00f44f1e9fbd9aa1a60a8359ae71c4562986
Authorolim7t <omichallat+github@gmail.com> · 2015-10-07 18:35 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-14 22:09 +02:00
Parentse25fc31e4689

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -8,6 +8,7 @@
88 - [improvement] Parse 'extensions' column in table metadata (JAVA-938)
99 - [bug] Fix Configuration builder to allow disabled metrics (JAVA-900)
1010 - [new feature] Prepare API for async query trace (JAVA-902)
11+- [new feature] Add BoundStatement#unset (JAVA-930)
1112
1213
1314 ### 3.0.0-alpha3
modified driver-core/src/main/java/com/datastax/driver/core/AbstractData.java +1 −1
@@ -56,7 +56,7 @@
5656
5757 protected abstract int[] getAllIndexesOf(String name);
5858
59- private T setValue(int i, ByteBuffer value) {
59+ protected T setValue(int i, ByteBuffer value) {
6060 values[i] = value;
6161 return wrapped;
6262 }
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +41 −3
@@ -38,10 +38,15 @@
3838 * variables have the same name, setting that name will set <b>all</b> the
3939 * variables for that name.
4040 * <p>
41- * All the variables of the statement must be bound. If you don't explicitly
42- * set a value for a variable, an {@code IllegalStateException} will be
43- * thrown when submitting the statement. If you want to set a variable to
41+ * With native protocol V3 or below, all variables of the statement must be bound.
42+ * If you don't explicitly set a value for a variable, an {@code IllegalStateException}
43+ * will be thrown when submitting the statement. If you want to set a variable to
4444 * {@code null}, use {@link #setToNull(int) setToNull}.
45+ * <p>
46+ * With native protocol V4 or above, variables can be left unset, in which case they
47+ * will be ignored server side (no tombstones will be generated). If you're reusing
48+ * a bound statement, you can {@link #unset(int) unset} variables that were previously
49+ * set.
4550 */
4651 public class BoundStatement extends Statement implements SettableData<BoundStatement>, GettableData {
4752 static final ByteBuffer UNSET = ByteBuffer.allocate(0);
@@ -120,6 +125,39 @@
120125 return wrapper.getValue(wrapper.getIndexOf(name)) != UNSET;
121126 }
122127
128+ /**
129+ * Unsets the {@code i}th variable. This will leave the statement in the same state as if no setter was
130+ * ever called for this variable.
131+ * <p>
132+ * The treatment of unset variables depends on the native protocol version, see {@link BoundStatement}
133+ * for explanations.
134+ *
135+ * @param i the index of the variable.
136+ *
137+ * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.preparedStatement().variables().size()}.
138+ */
139+ public void unset(int i) {
140+ wrapper.setValue(i, UNSET);
141+ }
142+
143+ /**
144+ * Unsets all occurrences of variable {@code name}. This will leave the statement in the same state
145+ * as if no setter was ever called for this variable.
146+ * <p>
147+ * The treatment of unset variables depends on the native protocol version, see {@link BoundStatement}
148+ * for explanations.
149+ *
150+ * @param name the name of the variable.
151+ *
152+ * @throws IllegalArgumentException if {@code name} is not a prepared
153+ * variable, that is if {@code !this.preparedStatement().variables().names().contains(name)}.
154+ */
155+ public void unset(String name) {
156+ for (int i : wrapper.getAllIndexesOf(name)) {
157+ wrapper.setValue(i, UNSET);
158+ }
159+ }
160+
123161 /**
124162 * Bound values to the variables of this statement.
125163 *
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +83 −24
@@ -28,12 +28,13 @@
2828 import static org.testng.Assert.fail;
2929
3030 import com.datastax.driver.core.exceptions.InvalidQueryException;
31-import com.datastax.driver.core.exceptions.NoHostAvailableException;
3231 import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
3332 import com.datastax.driver.core.utils.CassandraVersion;
3433
35-import static com.datastax.driver.core.ProtocolVersion.V3;
36-import static com.datastax.driver.core.TestUtils.*;
34+import static com.datastax.driver.core.TestUtils.getFixedValue;
35+import static com.datastax.driver.core.TestUtils.getFixedValue2;
36+import static com.datastax.driver.core.TestUtils.getValue;
37+import static com.datastax.driver.core.TestUtils.setBoundValue;
3738
3839 /**
3940 * Prepared statement tests.
@@ -169,7 +170,7 @@
169170
170171 String name = "c_list_" + rawType;
171172 DataType type = DataType.list(rawType);
172- List<Object> value = (List<Object>)getFixedValue(type);;
173+ List<Object> value = (List<Object>)getFixedValue(type);
173174 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name));
174175 BoundStatement bs = ps.bind();
175176 session.execute(setBoundValue(bs, name, type, value));
@@ -193,7 +194,7 @@
193194
194195 String name = "c_list_" + rawType;
195196 DataType type = DataType.list(rawType);
196- List<Object> value = (List<Object>)getFixedValue2(type);;
197+ List<Object> value = (List<Object>)getFixedValue2(type);
197198 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name));
198199 BoundStatement bs = ps.bind();
199200 session.execute(setBoundValue(bs, name, type, value));
@@ -214,7 +215,7 @@
214215
215216 String name = "c_set_" + rawType;
216217 DataType type = DataType.set(rawType);
217- Set<Object> value = (Set<Object>)getFixedValue(type);;
218+ Set<Object> value = (Set<Object>)getFixedValue(type);
218219 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name));
219220 BoundStatement bs = ps.bind();
220221 session.execute(setBoundValue(bs, name, type, value));
@@ -238,7 +239,7 @@
238239
239240 String name = "c_set_" + rawType;
240241 DataType type = DataType.set(rawType);
241- Set<Object> value = (Set<Object>)getFixedValue2(type);;
242+ Set<Object> value = (Set<Object>)getFixedValue2(type);
242243 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name));
243244 BoundStatement bs = ps.bind();
244245 session.execute(setBoundValue(bs, name, type, value));
@@ -264,7 +265,7 @@
264265
265266 String name = "c_map_" + rawKeyType + '_' + rawValueType;
266267 DataType type = DataType.map(rawKeyType, rawValueType);
267- Map<Object, Object> value = (Map<Object, Object>)getFixedValue(type);;
268+ Map<Object, Object> value = (Map<Object, Object>)getFixedValue(type);
268269 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name));
269270 BoundStatement bs = ps.bind();
270271 session.execute(setBoundValue(bs, name, type, value));
@@ -294,7 +295,7 @@
294295
295296 String name = "c_map_" + rawKeyType + '_' + rawValueType;
296297 DataType type = DataType.map(rawKeyType, rawValueType);
297- Map<Object, Object> value = (Map<Object, Object>)getFixedValue2(type);;
298+ Map<Object, Object> value = (Map<Object, Object>)getFixedValue2(type);
298299 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name));
299300 BoundStatement bs = ps.bind();
300301 session.execute(setBoundValue(bs, name, type, value));
@@ -385,16 +386,16 @@
385386 }
386387 }
387388
388- @Test(groups="short")
389+ @Test(groups = "short")
389390 public void should_set_routing_key_on_case_insensitive_keyspace_and_table() {
390- session.execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)",keyspace));
391+ session.execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)", keyspace));
391392
392- PreparedStatement ps = session.prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)",keyspace));
393+ PreparedStatement ps = session.prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace));
393394 BoundStatement bs = ps.bind(1);
394395 assertThat(bs.getRoutingKey()).isNotNull();
395396 }
396397
397- @Test(groups="short")
398+ @Test(groups = "short")
398399 public void should_set_routing_key_on_case_sensitive_keyspace_and_table() {
399400 session.execute("CREATE KEYSPACE \"Test\" WITH replication = { "
400401 + " 'class': 'SimpleStrategy',"
@@ -407,7 +408,7 @@
407408 assertThat(bs.getRoutingKey()).isNotNull();
408409 }
409410
410- @Test(groups="short", expectedExceptions = InvalidQueryException.class)
411+ @Test(groups = "short", expectedExceptions = InvalidQueryException.class)
411412 public void should_fail_when_prepared_on_another_cluster() throws Exception {
412413 Cluster otherCluster = Cluster.builder()
413414 .addContactPointsWithPorts(ImmutableList.of(hostAddress))
@@ -465,7 +466,7 @@
465466 * @jira_ticket JAVA-777
466467 */
467468 @Test(groups = "short")
468- @CassandraVersion(major=2.0)
469+ @CassandraVersion(major = 2.0)
469470 public void should_not_allow_unbound_value_on_batch_statement_when_protocol_lesser_than_v4() {
470471 Cluster cluster = Cluster.builder()
471472 .addContactPointsWithPorts(Collections.singleton(hostAddress))
@@ -473,7 +474,7 @@
473474 .build();
474475 Session session = cluster.connect();
475476 try {
476- PreparedStatement ps = session.prepare("INSERT INTO " + keyspace + "." + SIMPLE_TABLE + " (k, i) VALUES (?, ?)");
477+ PreparedStatement ps = session.prepare("INSERT INTO " + keyspace + "." + SIMPLE_TABLE + " (k, i) VALUES (?, ?)");
477478 BatchStatement batch = new BatchStatement();
478479 batch.add(ps.bind("foo"));
479480 // i is UNSET
@@ -513,7 +514,65 @@
513514 ResultSet rows = session.execute(st3);
514515 assertThat(rows.one().getInt("i")).isEqualTo(1234);
515516 QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
516- checkEventsContain(queryTrace, "0 tombstone");
517+ assertEventsContain(queryTrace, "0 tombstone");
518+ }
519+
520+ /**
521+ * Tests that a value that was previously set on a bound statement can be unset by index.
522+ * This only works from protocol V4 onwards.
523+ *
524+ * @since 2.2.0
525+ * @test_category prepared_statements:binding
526+ * @jira_ticket JAVA-930
527+ */
528+ @Test(groups = "short")
529+ @CassandraVersion(major = 2.2)
530+ public void should_unset_value_by_index() {
531+ PreparedStatement prepared = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)");
532+ BoundStatement bound = prepared.bind();
533+ bound.setString(0, "foo");
534+ bound.setInt(1, 1234);
535+
536+ bound.unset(1);
537+ assertThat(bound.isSet(1)).isFalse();
538+ session.execute(bound);
539+
540+ ResultSet rows = session.execute(
541+ session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'")
542+ .enableTracing());
543+
544+ assertThat(rows.one().isNull("i"));
545+ QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
546+ assertEventsContain(queryTrace, "0 tombstone");
547+ }
548+
549+ /**
550+ * Tests that a value that was previously set on a bound statement can be unset by name.
551+ * This only works from protocol V4 onwards.
552+ *
553+ * @since 2.2.0
554+ * @test_category prepared_statements:binding
555+ * @jira_ticket JAVA-930
556+ */
557+ @Test(groups = "short")
558+ @CassandraVersion(major = 2.2)
559+ public void should_unset_value_by_name() {
560+ PreparedStatement prepared = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (:k, :i)");
561+ BoundStatement bound = prepared.bind();
562+ bound.setString("k", "foo");
563+ bound.setInt("i", 1234);
564+
565+ bound.unset("i");
566+ assertThat(bound.isSet("i")).isFalse();
567+ session.execute(bound);
568+
569+ ResultSet rows = session.execute(
570+ session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'")
571+ .enableTracing());
572+
573+ assertThat(rows.one().isNull("i"));
574+ QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
575+ assertEventsContain(queryTrace, "0 tombstone");
517576 }
518577
519578 /**
@@ -542,7 +601,7 @@
542601 ResultSet rows = session.execute(st3);
543602 assertThat(rows.one().getInt("i")).isEqualTo(1234);
544603 QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
545- checkEventsContain(queryTrace, "0 tombstone");
604+ assertEventsContain(queryTrace, "0 tombstone");
546605 }
547606
548607 /**
@@ -564,7 +623,7 @@
564623 ResultSet rows = session.execute(st2);
565624 assertThat(rows.one().isNull(0)).isTrue();
566625 QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
567- checkEventsContain(queryTrace, "1 tombstone");
626+ assertEventsContain(queryTrace, "1 tombstone");
568627 }
569628
570629 /**
@@ -575,7 +634,7 @@
575634 * @jira_ticket JAVA-777
576635 */
577636 @Test(groups = "short")
578- @CassandraVersion(major=2.0)
637+ @CassandraVersion(major = 2.0)
579638 public void should_create_tombstone_when_null_value_on_batch_statement() {
580639 PreparedStatement prepared = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)");
581640 BoundStatement st1 = prepared.bind();
@@ -587,14 +646,14 @@
587646 ResultSet rows = session.execute(st2);
588647 assertThat(rows.one().isNull(0)).isTrue();
589648 QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace();
590- checkEventsContain(queryTrace, "1 tombstone");
649+ assertEventsContain(queryTrace, "1 tombstone");
591650 }
592651
593- private boolean checkEventsContain(QueryTrace queryTrace, String toFind) {
652+ private void assertEventsContain(QueryTrace queryTrace, String toFind) {
594653 for (QueryTrace.Event event : queryTrace.getEvents()) {
595654 if (event.getDescription().contains(toFind))
596- return true;
655+ return;
597656 }
598- return false;
657+ fail("Did not find '" + toFind + "' in trace");
599658 }
600659 }
modified features/statements/prepared/README.md +19 −3
@@ -59,9 +59,6 @@
5959 .setString("d", "LCD screen");
6060 ```
6161
62-If you don't set a parameter, it is sent as `null` (note that this
63-behavior changes in the 2.1 branch of the driver).
64-
6562 You can use named setters even if the query uses anonymous parameters;
6663 Cassandra will name the parameters after the column they apply to:
6764
@@ -75,6 +72,25 @@
7572 for example: `select * from sales where sku = ? and date > ? and date <
7673 ?`. In these situations, use positional setters or named parameters.
7774
75+For native protocol V3 or below, all variables must be bound. With native
76+protocol V4 or above, variables can be left unset, in which case they
77+will be ignored server side (no tombstones will be generated). If you're
78+reusing a bound statement you can use the `unset` method to unset variables
79+that were previously set:
80+
81+```java
82+BoundStatement bound = ps1.bind()
83+ .setString("sku", "324378")
84+ .setString("description", "LCD screen")
85+
86+// Using the unset method to unset previously set value.
87+// Positional setter:
88+bound.unset("description");
89+
90+// Named setter:
91+bound.unset(1);
92+```
93+
7894 A bound statement also has getters to retrieve the values. Note that
7995 this has a small performance overhead since values are stored in their
8096 serialized form.