Elffuss BraidHub

kikocis/java-driver public

Merge pull request #178 from olim7t/JAVA-326

JAVA-326: add support for conditional DELETEs in QueryBuilder
Changeset60207be508132a9005cadc0d147382fe79f9e899fc23f668700ad4de311f855d
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 17:15 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 17:15 +02:00
Parents104fa6de5417, 5710601b2b51

Browse the files at this changeset

4 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -5,6 +5,7 @@
55 ------
66
77 - [bug] Check cluster name when connecting to a new node (JAVA-397)
8+- [bug] Add missing CAS delete support in QueryBuilder (JAVA-326)
89
910 2.0.5:
1011 ------
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +141 −0
@@ -30,6 +30,8 @@
3030 private final List<Object> columnNames;
3131 private final Where where;
3232 private final Options usings;
33+ private final Conditions conditions;
34+ private boolean ifExists;
3335
3436 Delete(String keyspace, String table, List<Object> columnNames) {
3537 super(keyspace);
@@ -37,6 +39,7 @@
3739 this.columnNames = columnNames;
3840 this.where = new Where(this);
3941 this.usings = new Options(this);
42+ this.conditions = new Conditions(this);
4043 }
4144
4245 Delete(TableMetadata table, List<Object> columnNames) {
@@ -45,6 +48,7 @@
4548 this.columnNames = columnNames;
4649 this.where = new Where(this);
4750 this.usings = new Options(this);
51+ this.conditions = new Conditions(this);
4852 }
4953
5054 @Override
@@ -69,6 +73,15 @@
6973 Utils.joinAndAppend(builder, " AND ", where.clauses, variables);
7074 }
7175
76+ if (ifExists) {
77+ builder.append(" IF EXISTS ");
78+ }
79+
80+ if (!conditions.conditions.isEmpty()) {
81+ builder.append(" IF ");
82+ Utils.joinAndAppend(builder, " AND ", conditions.conditions, variables);
83+ }
84+
7285 return builder;
7386 }
7487
@@ -93,6 +106,27 @@
93106 return where;
94107 }
95108
109+ /**
110+ * Adds a conditions clause (IF) to this statement.
111+ * <p>
112+ * This is a shorter/more readable version for {@code onlyIf().and(condition)}.
113+ *
114+ * @param condition the condition to add.
115+ * @return the conditions of this query to which more conditions can be added.
116+ */
117+ public Conditions onlyIf(Clause condition) {
118+ return conditions.and(condition);
119+ }
120+
121+ /**
122+ * Adds a conditions clause (IF) to this statement.
123+ *
124+ * @return the conditions of this query to which more conditions can be added.
125+ */
126+ public Conditions onlyIf() {
127+ return conditions;
128+ }
129+
96130 /**
97131 * Adds a new options for this DELETE statement.
98132 *
@@ -103,6 +137,27 @@
103137 return usings.and(using);
104138 }
105139
140+ /**
141+ * Sets the 'IF EXISTS' option for this DELETE statement.
142+ *
143+ * <p>
144+ * A delete with that option will report whether the statement actually
145+ * resulted in data being deleted. The existence check and deletion are
146+ * done transactionally in the sense that if multiple clients attempt to
147+ * delete a given row with this option, then at most one may succeed.
148+ * </p>
149+ * <p>
150+ * Please keep in mind that using this option has a non negligible
151+ * performance impact and should be avoided when possible.
152+ * </p>
153+ *
154+ * @return this DELETE statement.
155+ */
156+ public Delete ifExists() {
157+ this.ifExists = true;
158+ return this;
159+ }
160+
106161 /**
107162 * The WHERE clause of a DELETE statement.
108163 */
@@ -137,6 +192,37 @@
137192 public Options using(Using using) {
138193 return statement.using(using);
139194 }
195+
196+ /**
197+ * Sets the 'IF EXISTS' option for the DELETE statement this WHERE clause
198+ * is part of.
199+ *
200+ * <p>
201+ * A delete with that option will report whether the statement actually
202+ * resulted in data being deleted. The existence check and deletion are
203+ * done transactionally in the sense that if multiple clients attempt to
204+ * delete a given row with this option, then at most one may succeed.
205+ * </p>
206+ * <p>
207+ * Please keep in mind that using this option has a non negligible
208+ * performance impact and should be avoided when possible.
209+ * </p>
210+ *
211+ * @return the DELETE statement this WHERE clause is part of.
212+ */
213+ public Delete ifExists() {
214+ return statement.ifExists();
215+ }
216+
217+ /**
218+ * Adds a condition to the DELETE statement this WHERE clause is part of.
219+ *
220+ * @param condition the condition to add.
221+ * @return the conditions for the DELETE statement this WHERE clause is part of.
222+ */
223+ public Conditions onlyIf(Clause condition) {
224+ return statement.onlyIf(condition);
225+ }
140226 }
141227
142228 /**
@@ -279,4 +365,59 @@
279365 return column(sb.append(']').toString());
280366 }
281367 }
368+
369+ /**
370+ * Conditions for a DELETE statement.
371+ * <p>
372+ * When provided some conditions, a deletion will not apply unless the
373+ * provided conditions applies.
374+ * </p>
375+ * <p>
376+ * Please keep in mind that provided conditions have a non negligible
377+ * performance impact and should be avoided when possible.
378+ * </p>
379+ */
380+ public static class Conditions extends BuiltStatement.ForwardingStatement<Delete> {
381+
382+ private final List<Clause> conditions = new ArrayList<Clause>();
383+
384+ Conditions(Delete statement) {
385+ super(statement);
386+ }
387+
388+ /**
389+ * Adds the provided condition for the deletion.
390+ * <p>
391+ * Note that while the query builder accept any type of {@code Clause}
392+ * as conditions, Cassandra currently only allows equality ones.
393+ *
394+ * @param condition the condition to add.
395+ * @return this {@code Conditions} clause.
396+ */
397+ public Conditions and(Clause condition) {
398+ conditions.add(condition);
399+ checkForBindMarkers(condition);
400+ return this;
401+ }
402+
403+ /**
404+ * Adds a where clause to the DELETE statement these conditions are part of.
405+ *
406+ * @param clause clause to add.
407+ * @return the WHERE clause of the DELETE statement these conditions are part of.
408+ */
409+ public Where where(Clause clause) {
410+ return statement.where(clause);
411+ }
412+
413+ /**
414+ * Adds an option to the DELETE statement these conditions are part of.
415+ *
416+ * @param using the using clause to add.
417+ * @return the options of the DELETE statement these conditions are part of.
418+ */
419+ public Options using(Using using) {
420+ return statement.using(using);
421+ }
422+ }
282423 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +24 −0
@@ -205,4 +205,28 @@
205205 assertEquals(delete.toString(), query);
206206 }
207207
208+ @Test(groups = "short")
209+ public void conditionalDeletesTest() throws Exception {
210+ session.execute("INSERT INTO ks.test_int (k, a, b) VALUES (1, 1, 1)");
211+
212+ Statement delete;
213+ Row row;
214+ delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 2)).ifExists();
215+ row = session.execute(delete).one();
216+ assertFalse(row.getBool("[applied]"));
217+
218+ delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).ifExists();
219+ row = session.execute(delete).one();
220+ assertTrue(row.getBool("[applied]"));
221+
222+ session.execute("INSERT INTO ks.test_int (k, a, b) VALUES (1, 1, 1)");
223+
224+ delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2));
225+ row = session.execute(delete).one();
226+ assertFalse(row.getBool("[applied]"));
227+
228+ delete = delete().from(TestUtils.SIMPLE_KEYSPACE, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1));
229+ row = session.execute(delete).one();
230+ assertTrue(row.getBool("[applied]"));
231+ }
208232 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +8 −0
@@ -315,6 +315,14 @@
315315 } catch (IllegalArgumentException e) {
316316 assertEquals(e.getMessage(), "Invalid timestamp, must be positive");
317317 }
318+
319+ query = "DELETE FROM foo.bar WHERE k1='foo' IF EXISTS;";
320+ delete = delete().from("foo", "bar").where(eq("k1", "foo")).ifExists();
321+ assertEquals(delete.toString(), query);
322+
323+ query = "DELETE FROM foo.bar WHERE k1='foo' IF a=1 AND b=2;";
324+ delete = delete().from("foo", "bar").where(eq("k1", "foo")).onlyIf(eq("a", 1)).and(eq("b", 2));
325+ assertEquals(delete.toString(), query);
318326 }
319327
320328 @Test(groups = "unit")