kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | d4861195664d9a812e2826524b51a67a49ca8011d9e2aabd8625c54c1d8338b9 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-04 09:59 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-04 09:59 +02:00 |
| Parents | a9e072694f53, 85fbb963e5dd |
3 files changed
modified changelog/README.md +1 −0
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | - [improvement] New RetryDecision.tryNextHost() (JAVA-709) |
| 25 | 25 | - [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733) |
| 26 | 26 | - [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765) |
| 27 | +- [improvement] implement UPDATE ... IF EXISTS in QueryBuilder (JAVA-827) | |
| 27 | 28 | |
| 28 | 29 | Merged from 2.0.10_fixes branch: |
| 29 | 30 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +37 −0
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | private final Where where; |
| 33 | 33 | private final Options usings; |
| 34 | 34 | private final Conditions conditions; |
| 35 | + private boolean ifExists; | |
| 35 | 36 | |
| 36 | 37 | Update(String keyspace, String table) { |
| 37 | 38 | super(keyspace); |
| @@ -40,6 +41,7 @@ | ||
| 40 | 41 | this.where = new Where(this); |
| 41 | 42 | this.usings = new Options(this); |
| 42 | 43 | this.conditions = new Conditions(this); |
| 44 | + this.ifExists = false; | |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | 47 | Update(TableMetadata table) { |
| @@ -49,6 +51,7 @@ | ||
| 49 | 51 | this.where = new Where(this); |
| 50 | 52 | this.usings = new Options(this); |
| 51 | 53 | this.conditions = new Conditions(this); |
| 54 | + this.ifExists = false; | |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | @Override |
| @@ -80,6 +83,10 @@ | ||
| 80 | 83 | Utils.joinAndAppend(builder, " AND ", conditions.conditions, variables); |
| 81 | 84 | } |
| 82 | 85 | |
| 86 | + if (ifExists) { | |
| 87 | + builder.append(" IF EXISTS"); | |
| 88 | + } | |
| 89 | + | |
| 83 | 90 | return builder; |
| 84 | 91 | } |
| 85 | 92 | |
| @@ -211,6 +218,7 @@ | ||
| 211 | 218 | public Conditions onlyIf(Clause condition) { |
| 212 | 219 | return statement.onlyIf(condition); |
| 213 | 220 | } |
| 221 | + | |
| 214 | 222 | } |
| 215 | 223 | |
| 216 | 224 | /** |
| @@ -266,6 +274,28 @@ | ||
| 266 | 274 | public Conditions onlyIf(Clause condition) { |
| 267 | 275 | return statement.onlyIf(condition); |
| 268 | 276 | } |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Sets the 'IF EXISTS' option for the UPDATE statement this WHERE clause | |
| 280 | + * is part of. | |
| 281 | + * | |
| 282 | + * <p> | |
| 283 | + * An update with that option will report whether the statement actually | |
| 284 | + * resulted in data being updated. The existence check and update are | |
| 285 | + * done transactionally in the sense that if multiple clients attempt to | |
| 286 | + * update a given row with this option, then at most one may succeed. | |
| 287 | + * </p> | |
| 288 | + * <p> | |
| 289 | + * Please keep in mind that using this option has a non negligible | |
| 290 | + * performance impact and should be avoided when possible. | |
| 291 | + * </p> | |
| 292 | + * | |
| 293 | + * @return the UPDATE statement this WHERE clause is part of. | |
| 294 | + */ | |
| 295 | + public IfExists ifExists() { | |
| 296 | + statement.ifExists = true; | |
| 297 | + return new IfExists(statement); | |
| 298 | + } | |
| 269 | 299 | } |
| 270 | 300 | |
| 271 | 301 | /** |
| @@ -383,5 +413,12 @@ | ||
| 383 | 413 | public Options using(Using using) { |
| 384 | 414 | return statement.using(using); |
| 385 | 415 | } |
| 416 | + | |
| 417 | + } | |
| 418 | + | |
| 419 | + public static class IfExists extends BuiltStatement.ForwardingStatement<Update> { | |
| 420 | + IfExists(Update statement) { | |
| 421 | + super(statement); | |
| 422 | + } | |
| 386 | 423 | } |
| 387 | 424 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +6 −1
| @@ -296,7 +296,12 @@ | ||
| 296 | 296 | update = update("foo").with(set("x", 4)).where(eq("k", 0)).onlyIf(eq("x", 1)); |
| 297 | 297 | assertEquals(update.toString(), query); |
| 298 | 298 | } |
| 299 | - | |
| 299 | + @Test(groups = "unit") | |
| 300 | + public void should_handle_update_with_conditions_and_if_exists() { | |
| 301 | + Statement update = update("foo").with(set("x", 3)).where(eq("k", 2)).ifExists(); | |
| 302 | + assertThat(update.toString()).isEqualTo("UPDATE foo SET x=3 WHERE k=2 IF EXISTS;"); | |
| 303 | + } | |
| 304 | + | |
| 300 | 305 | @Test(groups = "unit") |
| 301 | 306 | public void deleteTest() throws Exception { |
| 302 | 307 |