1616package com .datastax .driver .core .querybuilder ;
1717
1818import com .datastax .driver .core .CodecRegistry ;
19+ import com .datastax .driver .core .ColumnMetadata ;
1920import com .datastax .driver .core .TableMetadata ;
2021
2122import java .util .ArrayList ;
23+ import java .util .Arrays ;
2224import java .util .List ;
2325
2426/**
@@ -34,21 +36,33 @@ public class Delete extends BuiltStatement {
3436 private boolean ifExists ;
3537
3638 Delete (String keyspace , String table , List <Selector > columns ) {
37- super (keyspace );
38- this .table = table ;
39- this .columns = columns ;
40- this .where = new Where (this );
41- this .usings = new Options (this );
42- this .conditions = new Conditions (this );
39+ this (keyspace , table , null , null , columns );
4340 }
4441
4542 Delete (TableMetadata table , List <Selector > columns ) {
46- super (table );
47- this .table = escapeId (table .getName ());
43+ this (escapeId (table .getKeyspace ().getName ()),
44+ escapeId (table .getName ()),
45+ Arrays .asList (new Object [table .getPartitionKey ().size ()]),
46+ table .getPartitionKey (),
47+ columns );
48+ }
49+
50+ Delete (String keyspace ,
51+ String table ,
52+ List <Object > routingKeyValues ,
53+ List <ColumnMetadata > partitionKey ,
54+ List <Selector > columns ) {
55+ super (keyspace , partitionKey , routingKeyValues );
56+ this .table = table ;
4857 this .columns = columns ;
4958 this .where = new Where (this );
5059 this .usings = new Options (this );
5160 this .conditions = new Conditions (this );
61+
62+ // This is for JAVA-1089, if the query deletes an element in a list, the statement should be non-idempotent.
63+ if (!areIdempotent (columns )) {
64+ setNonIdempotentOps ();
65+ }
5266 }
5367
5468 @ Override
@@ -110,6 +124,9 @@ public Where where() {
110124 * Adds a conditions clause (IF) to this statement.
111125 * <p/>
112126 * This is a shorter/more readable version for {@code onlyIf().and(condition)}.
127+ * <p/>
128+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
129+ * for more information.
113130 *
114131 * @param condition the condition to add.
115132 * @return the conditions of this query to which more conditions can be added.
@@ -120,6 +137,9 @@ public Conditions onlyIf(Clause condition) {
120137
121138 /**
122139 * Adds a conditions clause (IF) to this statement.
140+ * <p/>
141+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
142+ * for more information.
123143 *
124144 * @return the conditions of this query to which more conditions can be added.
125145 */
@@ -161,11 +181,14 @@ public Options using() {
161181 * Please keep in mind that using this option has a non negligible
162182 * performance impact and should be avoided when possible.
163183 * </p>
184+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
185+ * for more information.
164186 *
165187 * @return this DELETE statement.
166188 */
167189 public Delete ifExists () {
168190 this .ifExists = true ;
191+ setNonIdempotentOps ();
169192 return this ;
170193 }
171194
@@ -189,6 +212,9 @@ public static class Where extends BuiltStatement.ForwardingStatement<Delete> {
189212 public Where and (Clause clause ) {
190213 clauses .add (clause );
191214 statement .maybeAddRoutingKey (clause .name (), clause .firstValue ());
215+ if (!hasNonIdempotentOps () && !Utils .isIdempotent (clause )) {
216+ statement .setNonIdempotentOps ();
217+ }
192218 checkForBindMarkers (clause );
193219 return this ;
194220 }
@@ -354,7 +380,7 @@ public Selection column(String columnName) {
354380 * @return this in-build DELETE Selection
355381 */
356382 public Selection listElt (String columnName , int idx ) {
357- columns .add (new CollectionElementSelector (columnName , idx ));
383+ columns .add (new ListElementSelector (columnName , idx ));
358384 return this ;
359385 }
360386
@@ -367,7 +393,7 @@ public Selection listElt(String columnName, int idx) {
367393 * @return this in-build DELETE Selection
368394 */
369395 public Selection listElt (String columnName , BindMarker idx ) {
370- columns .add (new CollectionElementSelector (columnName , idx ));
396+ columns .add (new ListElementSelector (columnName , idx ));
371397 return this ;
372398 }
373399
@@ -379,7 +405,7 @@ public Selection listElt(String columnName, BindMarker idx) {
379405 * @return this in-build DELETE Selection
380406 */
381407 public Selection setElt (String columnName , Object element ) {
382- columns .add (new CollectionElementSelector (columnName , element ));
408+ columns .add (new SetElementSelector (columnName , element ));
383409 return this ;
384410 }
385411
@@ -392,7 +418,7 @@ public Selection setElt(String columnName, Object element) {
392418 * @return this in-build DELETE Selection
393419 */
394420 public Selection setElt (String columnName , BindMarker element ) {
395- columns .add (new CollectionElementSelector (columnName , element ));
421+ columns .add (new SetElementSelector (columnName , element ));
396422 return this ;
397423 }
398424
@@ -404,7 +430,7 @@ public Selection setElt(String columnName, BindMarker element) {
404430 * @return this in-build DELETE Selection
405431 */
406432 public Selection mapElt (String columnName , Object key ) {
407- columns .add (new CollectionElementSelector (columnName , key ));
433+ columns .add (new MapElementSelector (columnName , key ));
408434 return this ;
409435 }
410436 }
@@ -414,7 +440,7 @@ public Selection mapElt(String columnName, Object key) {
414440 * A selector can be either a column name,
415441 * a list element, a set element or a map entry.
416442 */
417- static class Selector extends Utils .Appendeable {
443+ private static class Selector extends Utils .Appendeable {
418444
419445 private final String columnName ;
420446
@@ -441,9 +467,9 @@ public String toString() {
441467 /**
442468 * A selector representing a list index, a set element or a map key in a DELETE selection clause.
443469 */
444- static class CollectionElementSelector extends Selector {
470+ private static class CollectionElementSelector extends Selector {
445471
446- private final Object key ;
472+ protected final Object key ;
447473
448474 CollectionElementSelector (String columnName , Object key ) {
449475 super (columnName );
@@ -465,6 +491,36 @@ boolean containsBindMarker() {
465491
466492 }
467493
494+ private static class ListElementSelector extends CollectionElementSelector {
495+
496+ ListElementSelector (String columnName , Object key ) {
497+ super (columnName , key );
498+ }
499+ }
500+
501+ private static class SetElementSelector extends CollectionElementSelector {
502+
503+ SetElementSelector (String columnName , Object key ) {
504+ super (columnName , key );
505+ }
506+ }
507+
508+ private static class MapElementSelector extends CollectionElementSelector {
509+
510+ MapElementSelector (String columnName , Object key ) {
511+ super (columnName , key );
512+ }
513+ }
514+
515+ private boolean areIdempotent (List <Selector > selectors ) {
516+ for (Selector sel : selectors ) {
517+ if (sel instanceof ListElementSelector ) {
518+ return false ;
519+ }
520+ }
521+ return true ;
522+ }
523+
468524 /**
469525 * Conditions for a DELETE statement.
470526 * <p>
@@ -494,6 +550,7 @@ public static class Conditions extends BuiltStatement.ForwardingStatement<Delete
494550 * @return this {@code Conditions} clause.
495551 */
496552 public Conditions and (Clause condition ) {
553+ statement .setNonIdempotentOps ();
497554 conditions .add (condition );
498555 checkForBindMarkers (condition );
499556 return this ;
0 commit comments