Elffuss BraidHub

kikocis/java-driver public

Merge pull request #413 from mbaechler/JAVA-827-over-2.0

JAVA-827 implement UPDATE ... IF EXISTS in QueryBuilder
Changesetd4861195664d9a812e2826524b51a67a49ca8011d9e2aabd8625c54c1d8338b9
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-04 09:59 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-04 09:59 +02:00
Parentsa9e072694f53, 85fbb963e5dd

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -24,6 +24,7 @@
2424 - [improvement] New RetryDecision.tryNextHost() (JAVA-709)
2525 - [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733)
2626 - [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765)
27+- [improvement] implement UPDATE ... IF EXISTS in QueryBuilder (JAVA-827)
2728
2829 Merged from 2.0.10_fixes branch:
2930
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +37 −0
@@ -32,6 +32,7 @@
3232 private final Where where;
3333 private final Options usings;
3434 private final Conditions conditions;
35+ private boolean ifExists;
3536
3637 Update(String keyspace, String table) {
3738 super(keyspace);
@@ -40,6 +41,7 @@
4041 this.where = new Where(this);
4142 this.usings = new Options(this);
4243 this.conditions = new Conditions(this);
44+ this.ifExists = false;
4345 }
4446
4547 Update(TableMetadata table) {
@@ -49,6 +51,7 @@
4951 this.where = new Where(this);
5052 this.usings = new Options(this);
5153 this.conditions = new Conditions(this);
54+ this.ifExists = false;
5255 }
5356
5457 @Override
@@ -80,6 +83,10 @@
8083 Utils.joinAndAppend(builder, " AND ", conditions.conditions, variables);
8184 }
8285
86+ if (ifExists) {
87+ builder.append(" IF EXISTS");
88+ }
89+
8390 return builder;
8491 }
8592
@@ -211,6 +218,7 @@
211218 public Conditions onlyIf(Clause condition) {
212219 return statement.onlyIf(condition);
213220 }
221+
214222 }
215223
216224 /**
@@ -266,6 +274,28 @@
266274 public Conditions onlyIf(Clause condition) {
267275 return statement.onlyIf(condition);
268276 }
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+ }
269299 }
270300
271301 /**
@@ -383,5 +413,12 @@
383413 public Options using(Using using) {
384414 return statement.using(using);
385415 }
416+
417+ }
418+
419+ public static class IfExists extends BuiltStatement.ForwardingStatement<Update> {
420+ IfExists(Update statement) {
421+ super(statement);
422+ }
386423 }
387424 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +6 −1
@@ -296,7 +296,12 @@
296296 update = update("foo").with(set("x", 4)).where(eq("k", 0)).onlyIf(eq("x", 1));
297297 assertEquals(update.toString(), query);
298298 }
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+
300305 @Test(groups = "unit")
301306 public void deleteTest() throws Exception {
302307