kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a46a54587186fede7fa0f1447f8f00f44f1e9fbd9aa1a60a8359ae71c4562986 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-10-07 18:35 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-10-14 22:09 +02:00 |
| Parents | e25fc31e4689 |
5 files changed
modified changelog/README.md +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [improvement] Parse 'extensions' column in table metadata (JAVA-938) |
| 9 | 9 | - [bug] Fix Configuration builder to allow disabled metrics (JAVA-900) |
| 10 | 10 | - [new feature] Prepare API for async query trace (JAVA-902) |
| 11 | +- [new feature] Add BoundStatement#unset (JAVA-930) | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | ### 3.0.0-alpha3 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractData.java +1 −1
| @@ -56,7 +56,7 @@ | ||
| 56 | 56 | |
| 57 | 57 | protected abstract int[] getAllIndexesOf(String name); |
| 58 | 58 | |
| 59 | - private T setValue(int i, ByteBuffer value) { | |
| 59 | + protected T setValue(int i, ByteBuffer value) { | |
| 60 | 60 | values[i] = value; |
| 61 | 61 | return wrapped; |
| 62 | 62 | } |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +41 −3
| @@ -38,10 +38,15 @@ | ||
| 38 | 38 | * variables have the same name, setting that name will set <b>all</b> the |
| 39 | 39 | * variables for that name. |
| 40 | 40 | * <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 | |
| 44 | 44 | * {@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. | |
| 45 | 50 | */ |
| 46 | 51 | public class BoundStatement extends Statement implements SettableData<BoundStatement>, GettableData { |
| 47 | 52 | static final ByteBuffer UNSET = ByteBuffer.allocate(0); |
| @@ -120,6 +125,39 @@ | ||
| 120 | 125 | return wrapper.getValue(wrapper.getIndexOf(name)) != UNSET; |
| 121 | 126 | } |
| 122 | 127 | |
| 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 | + | |
| 123 | 161 | /** |
| 124 | 162 | * Bound values to the variables of this statement. |
| 125 | 163 | * |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +83 −24
| @@ -28,12 +28,13 @@ | ||
| 28 | 28 | import static org.testng.Assert.fail; |
| 29 | 29 | |
| 30 | 30 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 31 | -import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 32 | 31 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 33 | 32 | import com.datastax.driver.core.utils.CassandraVersion; |
| 34 | 33 | |
| 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; | |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * Prepared statement tests. |
| @@ -169,7 +170,7 @@ | ||
| 169 | 170 | |
| 170 | 171 | String name = "c_list_" + rawType; |
| 171 | 172 | DataType type = DataType.list(rawType); |
| 172 | - List<Object> value = (List<Object>)getFixedValue(type);; | |
| 173 | + List<Object> value = (List<Object>)getFixedValue(type); | |
| 173 | 174 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); |
| 174 | 175 | BoundStatement bs = ps.bind(); |
| 175 | 176 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -193,7 +194,7 @@ | ||
| 193 | 194 | |
| 194 | 195 | String name = "c_list_" + rawType; |
| 195 | 196 | DataType type = DataType.list(rawType); |
| 196 | - List<Object> value = (List<Object>)getFixedValue2(type);; | |
| 197 | + List<Object> value = (List<Object>)getFixedValue2(type); | |
| 197 | 198 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); |
| 198 | 199 | BoundStatement bs = ps.bind(); |
| 199 | 200 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -214,7 +215,7 @@ | ||
| 214 | 215 | |
| 215 | 216 | String name = "c_set_" + rawType; |
| 216 | 217 | DataType type = DataType.set(rawType); |
| 217 | - Set<Object> value = (Set<Object>)getFixedValue(type);; | |
| 218 | + Set<Object> value = (Set<Object>)getFixedValue(type); | |
| 218 | 219 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); |
| 219 | 220 | BoundStatement bs = ps.bind(); |
| 220 | 221 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -238,7 +239,7 @@ | ||
| 238 | 239 | |
| 239 | 240 | String name = "c_set_" + rawType; |
| 240 | 241 | DataType type = DataType.set(rawType); |
| 241 | - Set<Object> value = (Set<Object>)getFixedValue2(type);; | |
| 242 | + Set<Object> value = (Set<Object>)getFixedValue2(type); | |
| 242 | 243 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); |
| 243 | 244 | BoundStatement bs = ps.bind(); |
| 244 | 245 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -264,7 +265,7 @@ | ||
| 264 | 265 | |
| 265 | 266 | String name = "c_map_" + rawKeyType + '_' + rawValueType; |
| 266 | 267 | 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); | |
| 268 | 269 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); |
| 269 | 270 | BoundStatement bs = ps.bind(); |
| 270 | 271 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -294,7 +295,7 @@ | ||
| 294 | 295 | |
| 295 | 296 | String name = "c_map_" + rawKeyType + '_' + rawValueType; |
| 296 | 297 | 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); | |
| 298 | 299 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); |
| 299 | 300 | BoundStatement bs = ps.bind(); |
| 300 | 301 | session.execute(setBoundValue(bs, name, type, value)); |
| @@ -385,16 +386,16 @@ | ||
| 385 | 386 | } |
| 386 | 387 | } |
| 387 | 388 | |
| 388 | - @Test(groups="short") | |
| 389 | + @Test(groups = "short") | |
| 389 | 390 | 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)); | |
| 391 | 392 | |
| 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)); | |
| 393 | 394 | BoundStatement bs = ps.bind(1); |
| 394 | 395 | assertThat(bs.getRoutingKey()).isNotNull(); |
| 395 | 396 | } |
| 396 | 397 | |
| 397 | - @Test(groups="short") | |
| 398 | + @Test(groups = "short") | |
| 398 | 399 | public void should_set_routing_key_on_case_sensitive_keyspace_and_table() { |
| 399 | 400 | session.execute("CREATE KEYSPACE \"Test\" WITH replication = { " |
| 400 | 401 | + " 'class': 'SimpleStrategy'," |
| @@ -407,7 +408,7 @@ | ||
| 407 | 408 | assertThat(bs.getRoutingKey()).isNotNull(); |
| 408 | 409 | } |
| 409 | 410 | |
| 410 | - @Test(groups="short", expectedExceptions = InvalidQueryException.class) | |
| 411 | + @Test(groups = "short", expectedExceptions = InvalidQueryException.class) | |
| 411 | 412 | public void should_fail_when_prepared_on_another_cluster() throws Exception { |
| 412 | 413 | Cluster otherCluster = Cluster.builder() |
| 413 | 414 | .addContactPointsWithPorts(ImmutableList.of(hostAddress)) |
| @@ -465,7 +466,7 @@ | ||
| 465 | 466 | * @jira_ticket JAVA-777 |
| 466 | 467 | */ |
| 467 | 468 | @Test(groups = "short") |
| 468 | - @CassandraVersion(major=2.0) | |
| 469 | + @CassandraVersion(major = 2.0) | |
| 469 | 470 | public void should_not_allow_unbound_value_on_batch_statement_when_protocol_lesser_than_v4() { |
| 470 | 471 | Cluster cluster = Cluster.builder() |
| 471 | 472 | .addContactPointsWithPorts(Collections.singleton(hostAddress)) |
| @@ -473,7 +474,7 @@ | ||
| 473 | 474 | .build(); |
| 474 | 475 | Session session = cluster.connect(); |
| 475 | 476 | 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 (?, ?)"); | |
| 477 | 478 | BatchStatement batch = new BatchStatement(); |
| 478 | 479 | batch.add(ps.bind("foo")); |
| 479 | 480 | // i is UNSET |
| @@ -513,7 +514,65 @@ | ||
| 513 | 514 | ResultSet rows = session.execute(st3); |
| 514 | 515 | assertThat(rows.one().getInt("i")).isEqualTo(1234); |
| 515 | 516 | 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"); | |
| 517 | 576 | } |
| 518 | 577 | |
| 519 | 578 | /** |
| @@ -542,7 +601,7 @@ | ||
| 542 | 601 | ResultSet rows = session.execute(st3); |
| 543 | 602 | assertThat(rows.one().getInt("i")).isEqualTo(1234); |
| 544 | 603 | QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace(); |
| 545 | - checkEventsContain(queryTrace, "0 tombstone"); | |
| 604 | + assertEventsContain(queryTrace, "0 tombstone"); | |
| 546 | 605 | } |
| 547 | 606 | |
| 548 | 607 | /** |
| @@ -564,7 +623,7 @@ | ||
| 564 | 623 | ResultSet rows = session.execute(st2); |
| 565 | 624 | assertThat(rows.one().isNull(0)).isTrue(); |
| 566 | 625 | QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace(); |
| 567 | - checkEventsContain(queryTrace, "1 tombstone"); | |
| 626 | + assertEventsContain(queryTrace, "1 tombstone"); | |
| 568 | 627 | } |
| 569 | 628 | |
| 570 | 629 | /** |
| @@ -575,7 +634,7 @@ | ||
| 575 | 634 | * @jira_ticket JAVA-777 |
| 576 | 635 | */ |
| 577 | 636 | @Test(groups = "short") |
| 578 | - @CassandraVersion(major=2.0) | |
| 637 | + @CassandraVersion(major = 2.0) | |
| 579 | 638 | public void should_create_tombstone_when_null_value_on_batch_statement() { |
| 580 | 639 | PreparedStatement prepared = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)"); |
| 581 | 640 | BoundStatement st1 = prepared.bind(); |
| @@ -587,14 +646,14 @@ | ||
| 587 | 646 | ResultSet rows = session.execute(st2); |
| 588 | 647 | assertThat(rows.one().isNull(0)).isTrue(); |
| 589 | 648 | QueryTrace queryTrace = rows.getExecutionInfo().getQueryTrace(); |
| 590 | - checkEventsContain(queryTrace, "1 tombstone"); | |
| 649 | + assertEventsContain(queryTrace, "1 tombstone"); | |
| 591 | 650 | } |
| 592 | 651 | |
| 593 | - private boolean checkEventsContain(QueryTrace queryTrace, String toFind) { | |
| 652 | + private void assertEventsContain(QueryTrace queryTrace, String toFind) { | |
| 594 | 653 | for (QueryTrace.Event event : queryTrace.getEvents()) { |
| 595 | 654 | if (event.getDescription().contains(toFind)) |
| 596 | - return true; | |
| 655 | + return; | |
| 597 | 656 | } |
| 598 | - return false; | |
| 657 | + fail("Did not find '" + toFind + "' in trace"); | |
| 599 | 658 | } |
| 600 | 659 | } |
modified features/statements/prepared/README.md +19 −3
| @@ -59,9 +59,6 @@ | ||
| 59 | 59 | .setString("d", "LCD screen"); |
| 60 | 60 | ``` |
| 61 | 61 | |
| 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 | - | |
| 65 | 62 | You can use named setters even if the query uses anonymous parameters; |
| 66 | 63 | Cassandra will name the parameters after the column they apply to: |
| 67 | 64 | |
| @@ -75,6 +72,25 @@ | ||
| 75 | 72 | for example: `select * from sales where sku = ? and date > ? and date < |
| 76 | 73 | ?`. In these situations, use positional setters or named parameters. |
| 77 | 74 | |
| 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 | + | |
| 78 | 94 | A bound statement also has getters to retrieve the values. Note that |
| 79 | 95 | this has a small performance overhead since values are stored in their |
| 80 | 96 | serialized form. |