kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f3037e709a8a2f329f6207220cf7aed6cad25fe50ce3fada7eb49ef3eaae0145 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-03 20:23 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-01 11:24 +02:00 |
| Parents | ec028a844b59 |
10 files changed
modified changelog/README.md +1 −0
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | - [improvement] JAVA-879: Mapper.map() accepts mapper-generated and user queries. |
| 14 | 14 | - [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI |
| 15 | 15 | - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key. |
| 16 | +- [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent. | |
| 16 | 17 | |
| 17 | 18 | |
| 18 | 19 | ### 3.0.0 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +1 −1
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | private int nonBuiltStatementValues; |
| 38 | 38 | |
| 39 | 39 | Batch(RegularStatement[] statements, boolean logged) { |
| 40 | - super((String) null); | |
| 40 | + super(null, null, null); | |
| 41 | 41 | this.statements = statements.length == 0 |
| 42 | 42 | ? new ArrayList<RegularStatement>() |
| 43 | 43 | : new ArrayList<RegularStatement>(statements.length); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +6 −12
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | */ |
| 78 | 78 | public abstract class BuiltStatement extends RegularStatement { |
| 79 | 79 | |
| 80 | - private static final Pattern lowercaseId = Pattern.compile("[a-z][a-z0-9_]*"); | |
| 80 | + private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*"); | |
| 81 | 81 | |
| 82 | 82 | private final List<ColumnMetadata> partitionKey; |
| 83 | 83 | private final List<Object> routingKeyValues; |
| @@ -94,22 +94,16 @@ | ||
| 94 | 94 | boolean hasBindMarkers; |
| 95 | 95 | private boolean forceNoValues; |
| 96 | 96 | |
| 97 | - BuiltStatement(String keyspace) { | |
| 98 | - this.partitionKey = null; | |
| 99 | - this.routingKeyValues = null; | |
| 97 | + BuiltStatement(String keyspace, List<ColumnMetadata> partitionKey, List<Object> routingKeyValues) { | |
| 98 | + this.partitionKey = partitionKey; | |
| 99 | + this.routingKeyValues = routingKeyValues; | |
| 100 | 100 | this.keyspace = keyspace; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - BuiltStatement(TableMetadata tableMetadata) { | |
| 104 | - this.partitionKey = tableMetadata.getPartitionKey(); | |
| 105 | - this.routingKeyValues = Arrays.asList(new Object[tableMetadata.getPartitionKey().size()]); | |
| 106 | - this.keyspace = escapeId(tableMetadata.getKeyspace().getName()); | |
| 107 | - } | |
| 108 | - | |
| 109 | 103 | // Same as Metadata.escapeId, but we don't have access to it here. |
| 110 | 104 | protected static String escapeId(String ident) { |
| 111 | 105 | // we don't need to escape if it's lowercase and match non-quoted CQL3 ids. |
| 112 | - return lowercaseId.matcher(ident).matches() ? ident : Metadata.quote(ident); | |
| 106 | + return lowercaseAlphanumeric.matcher(ident).matches() ? ident : Metadata.quote(ident); | |
| 113 | 107 | } |
| 114 | 108 | |
| 115 | 109 | @Override |
| @@ -352,7 +346,7 @@ | ||
| 352 | 346 | T statement; |
| 353 | 347 | |
| 354 | 348 | ForwardingStatement(T statement) { |
| 355 | - super((String) null); | |
| 349 | + super(null, null, null); | |
| 356 | 350 | this.statement = statement; |
| 357 | 351 | } |
| 358 | 352 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +73 −16
| @@ -16,9 +16,11 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CodecRegistry; |
| 19 | +import com.datastax.driver.core.ColumnMetadata; | |
| 19 | 20 | import com.datastax.driver.core.TableMetadata; |
| 20 | 21 | |
| 21 | 22 | import java.util.ArrayList; |
| 23 | +import java.util.Arrays; | |
| 22 | 24 | import java.util.List; |
| 23 | 25 | |
| 24 | 26 | /** |
| @@ -34,21 +36,33 @@ | ||
| 34 | 36 | private boolean ifExists; |
| 35 | 37 | |
| 36 | 38 | 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); | |
| 43 | 40 | } |
| 44 | 41 | |
| 45 | 42 | 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; | |
| 48 | 57 | this.columns = columns; |
| 49 | 58 | this.where = new Where(this); |
| 50 | 59 | this.usings = new Options(this); |
| 51 | 60 | 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 | + } | |
| 52 | 66 | } |
| 53 | 67 | |
| 54 | 68 | @Override |
| @@ -110,6 +124,9 @@ | ||
| 110 | 124 | * Adds a conditions clause (IF) to this statement. |
| 111 | 125 | * <p/> |
| 112 | 126 | * 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. | |
| 113 | 130 | * |
| 114 | 131 | * @param condition the condition to add. |
| 115 | 132 | * @return the conditions of this query to which more conditions can be added. |
| @@ -120,6 +137,9 @@ | ||
| 120 | 137 | |
| 121 | 138 | /** |
| 122 | 139 | * 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. | |
| 123 | 143 | * |
| 124 | 144 | * @return the conditions of this query to which more conditions can be added. |
| 125 | 145 | */ |
| @@ -161,11 +181,14 @@ | ||
| 161 | 181 | * Please keep in mind that using this option has a non negligible |
| 162 | 182 | * performance impact and should be avoided when possible. |
| 163 | 183 | * </p> |
| 184 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 185 | + * for more information. | |
| 164 | 186 | * |
| 165 | 187 | * @return this DELETE statement. |
| 166 | 188 | */ |
| 167 | 189 | public Delete ifExists() { |
| 168 | 190 | this.ifExists = true; |
| 191 | + setNonIdempotentOps(); | |
| 169 | 192 | return this; |
| 170 | 193 | } |
| 171 | 194 | |
| @@ -189,6 +212,9 @@ | ||
| 189 | 212 | public Where and(Clause clause) { |
| 190 | 213 | clauses.add(clause); |
| 191 | 214 | statement.maybeAddRoutingKey(clause.name(), clause.firstValue()); |
| 215 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) { | |
| 216 | + statement.setNonIdempotentOps(); | |
| 217 | + } | |
| 192 | 218 | checkForBindMarkers(clause); |
| 193 | 219 | return this; |
| 194 | 220 | } |
| @@ -354,7 +380,7 @@ | ||
| 354 | 380 | * @return this in-build DELETE Selection |
| 355 | 381 | */ |
| 356 | 382 | public Selection listElt(String columnName, int idx) { |
| 357 | - columns.add(new CollectionElementSelector(columnName, idx)); | |
| 383 | + columns.add(new ListElementSelector(columnName, idx)); | |
| 358 | 384 | return this; |
| 359 | 385 | } |
| 360 | 386 | |
| @@ -367,7 +393,7 @@ | ||
| 367 | 393 | * @return this in-build DELETE Selection |
| 368 | 394 | */ |
| 369 | 395 | public Selection listElt(String columnName, BindMarker idx) { |
| 370 | - columns.add(new CollectionElementSelector(columnName, idx)); | |
| 396 | + columns.add(new ListElementSelector(columnName, idx)); | |
| 371 | 397 | return this; |
| 372 | 398 | } |
| 373 | 399 | |
| @@ -379,7 +405,7 @@ | ||
| 379 | 405 | * @return this in-build DELETE Selection |
| 380 | 406 | */ |
| 381 | 407 | public Selection setElt(String columnName, Object element) { |
| 382 | - columns.add(new CollectionElementSelector(columnName, element)); | |
| 408 | + columns.add(new SetElementSelector(columnName, element)); | |
| 383 | 409 | return this; |
| 384 | 410 | } |
| 385 | 411 | |
| @@ -392,7 +418,7 @@ | ||
| 392 | 418 | * @return this in-build DELETE Selection |
| 393 | 419 | */ |
| 394 | 420 | public Selection setElt(String columnName, BindMarker element) { |
| 395 | - columns.add(new CollectionElementSelector(columnName, element)); | |
| 421 | + columns.add(new SetElementSelector(columnName, element)); | |
| 396 | 422 | return this; |
| 397 | 423 | } |
| 398 | 424 | |
| @@ -404,7 +430,7 @@ | ||
| 404 | 430 | * @return this in-build DELETE Selection |
| 405 | 431 | */ |
| 406 | 432 | public Selection mapElt(String columnName, Object key) { |
| 407 | - columns.add(new CollectionElementSelector(columnName, key)); | |
| 433 | + columns.add(new MapElementSelector(columnName, key)); | |
| 408 | 434 | return this; |
| 409 | 435 | } |
| 410 | 436 | } |
| @@ -414,7 +440,7 @@ | ||
| 414 | 440 | * A selector can be either a column name, |
| 415 | 441 | * a list element, a set element or a map entry. |
| 416 | 442 | */ |
| 417 | - static class Selector extends Utils.Appendeable { | |
| 443 | + private static class Selector extends Utils.Appendeable { | |
| 418 | 444 | |
| 419 | 445 | private final String columnName; |
| 420 | 446 | |
| @@ -441,9 +467,9 @@ | ||
| 441 | 467 | /** |
| 442 | 468 | * A selector representing a list index, a set element or a map key in a DELETE selection clause. |
| 443 | 469 | */ |
| 444 | - static class CollectionElementSelector extends Selector { | |
| 470 | + private static class CollectionElementSelector extends Selector { | |
| 445 | 471 | |
| 446 | - private final Object key; | |
| 472 | + protected final Object key; | |
| 447 | 473 | |
| 448 | 474 | CollectionElementSelector(String columnName, Object key) { |
| 449 | 475 | super(columnName); |
| @@ -465,6 +491,36 @@ | ||
| 465 | 491 | |
| 466 | 492 | } |
| 467 | 493 | |
| 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 | + | |
| 468 | 524 | /** |
| 469 | 525 | * Conditions for a DELETE statement. |
| 470 | 526 | * <p> |
| @@ -494,6 +550,7 @@ | ||
| 494 | 550 | * @return this {@code Conditions} clause. |
| 495 | 551 | */ |
| 496 | 552 | public Conditions and(Clause condition) { |
| 553 | + statement.setNonIdempotentOps(); | |
| 497 | 554 | conditions.add(condition); |
| 498 | 555 | checkForBindMarkers(condition); |
| 499 | 556 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +18 −5
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CodecRegistry; |
| 19 | +import com.datastax.driver.core.ColumnMetadata; | |
| 19 | 20 | import com.datastax.driver.core.TableMetadata; |
| 20 | 21 | |
| 21 | 22 | import java.util.ArrayList; |
| @@ -34,14 +35,22 @@ | ||
| 34 | 35 | private boolean ifNotExists; |
| 35 | 36 | |
| 36 | 37 | Insert(String keyspace, String table) { |
| 37 | - super(keyspace); | |
| 38 | - this.table = table; | |
| 39 | - this.usings = new Options(this); | |
| 38 | + this(keyspace, table, null, null); | |
| 40 | 39 | } |
| 41 | 40 | |
| 42 | 41 | Insert(TableMetadata table) { |
| 43 | - super(table); | |
| 44 | - this.table = escapeId(table.getName()); | |
| 42 | + this(escapeId(table.getKeyspace().getName()), | |
| 43 | + escapeId(table.getName()), | |
| 44 | + Arrays.asList(new Object[table.getPartitionKey().size()]), | |
| 45 | + table.getPartitionKey()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + Insert(String keyspace, | |
| 49 | + String table, | |
| 50 | + List<Object> routingKeyValues, | |
| 51 | + List<ColumnMetadata> partitionKey) { | |
| 52 | + super(keyspace, partitionKey, routingKeyValues); | |
| 53 | + this.table = table; | |
| 45 | 54 | this.usings = new Options(this); |
| 46 | 55 | } |
| 47 | 56 | |
| @@ -157,10 +166,14 @@ | ||
| 157 | 166 | * <p/> |
| 158 | 167 | * Please keep in mind that using this option has a non negligible |
| 159 | 168 | * performance impact and should be avoided when possible. |
| 169 | + * <p/> | |
| 170 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 171 | + * for more information. | |
| 160 | 172 | * |
| 161 | 173 | * @return this INSERT statement. |
| 162 | 174 | */ |
| 163 | 175 | public Insert ifNotExists() { |
| 176 | + this.setNonIdempotentOps(); | |
| 164 | 177 | this.ifNotExists = true; |
| 165 | 178 | return this; |
| 166 | 179 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +18 −7
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CodecRegistry; |
| 19 | +import com.datastax.driver.core.ColumnMetadata; | |
| 19 | 20 | import com.datastax.driver.core.TableMetadata; |
| 20 | 21 | |
| 21 | 22 | import java.util.ArrayList; |
| @@ -39,16 +40,26 @@ | ||
| 39 | 40 | private boolean allowFiltering; |
| 40 | 41 | |
| 41 | 42 | Select(String keyspace, String table, List<Object> columnNames, boolean isDistinct) { |
| 42 | - super(keyspace); | |
| 43 | - this.table = table; | |
| 44 | - this.isDistinct = isDistinct; | |
| 45 | - this.columnNames = columnNames; | |
| 46 | - this.where = new Where(this); | |
| 43 | + this(keyspace, table, null, null, columnNames, isDistinct); | |
| 47 | 44 | } |
| 48 | 45 | |
| 49 | 46 | Select(TableMetadata table, List<Object> columnNames, boolean isDistinct) { |
| 50 | - super(table); | |
| 51 | - this.table = escapeId(table.getName()); | |
| 47 | + this(escapeId(table.getKeyspace().getName()), | |
| 48 | + escapeId(table.getName()), | |
| 49 | + Arrays.asList(new Object[table.getPartitionKey().size()]), | |
| 50 | + table.getPartitionKey(), | |
| 51 | + columnNames, | |
| 52 | + isDistinct); | |
| 53 | + } | |
| 54 | + | |
| 55 | + Select(String keyspace, | |
| 56 | + String table, | |
| 57 | + List<Object> routingKeyValues, | |
| 58 | + List<ColumnMetadata> partitionKey, | |
| 59 | + List<Object> columnNames, | |
| 60 | + boolean isDistinct) { | |
| 61 | + super(keyspace, partitionKey, routingKeyValues); | |
| 62 | + this.table = table; | |
| 52 | 63 | this.isDistinct = isDistinct; |
| 53 | 64 | this.columnNames = columnNames; |
| 54 | 65 | this.where = new Where(this); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +15 −4
| @@ -16,8 +16,10 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CodecRegistry; |
| 19 | +import com.datastax.driver.core.ColumnMetadata; | |
| 19 | 20 | import com.datastax.driver.core.TableMetadata; |
| 20 | 21 | |
| 22 | +import java.util.Arrays; | |
| 21 | 23 | import java.util.List; |
| 22 | 24 | |
| 23 | 25 | /** |
| @@ -28,13 +30,22 @@ | ||
| 28 | 30 | private final String table; |
| 29 | 31 | |
| 30 | 32 | Truncate(String keyspace, String table) { |
| 31 | - super(keyspace); | |
| 32 | - this.table = table; | |
| 33 | + this(keyspace, table, null, null); | |
| 33 | 34 | } |
| 34 | 35 | |
| 35 | 36 | Truncate(TableMetadata table) { |
| 36 | - super(table); | |
| 37 | - this.table = escapeId(table.getName()); | |
| 37 | + this(escapeId(table.getKeyspace().getName()), | |
| 38 | + escapeId(table.getName()), | |
| 39 | + Arrays.asList(new Object[table.getPartitionKey().size()]), | |
| 40 | + table.getPartitionKey()); | |
| 41 | + } | |
| 42 | + | |
| 43 | + Truncate(String keyspace, | |
| 44 | + String table, | |
| 45 | + List<Object> routingKeyValues, | |
| 46 | + List<ColumnMetadata> partitionKey) { | |
| 47 | + super(keyspace, partitionKey, routingKeyValues); | |
| 48 | + this.table = table; | |
| 38 | 49 | } |
| 39 | 50 | |
| 40 | 51 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +31 −11
| @@ -16,10 +16,12 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.CodecRegistry; |
| 19 | +import com.datastax.driver.core.ColumnMetadata; | |
| 19 | 20 | import com.datastax.driver.core.TableMetadata; |
| 20 | 21 | import com.datastax.driver.core.querybuilder.Assignment.CounterAssignment; |
| 21 | 22 | |
| 22 | 23 | import java.util.ArrayList; |
| 24 | +import java.util.Arrays; | |
| 23 | 25 | import java.util.List; |
| 24 | 26 | |
| 25 | 27 | /** |
| @@ -35,18 +37,22 @@ | ||
| 35 | 37 | private boolean ifExists; |
| 36 | 38 | |
| 37 | 39 | Update(String keyspace, String table) { |
| 38 | - super(keyspace); | |
| 39 | - this.table = table; | |
| 40 | - this.assignments = new Assignments(this); | |
| 41 | - this.where = new Where(this); | |
| 42 | - this.usings = new Options(this); | |
| 43 | - this.conditions = new Conditions(this); | |
| 44 | - this.ifExists = false; | |
| 40 | + this(keyspace, table, null, null); | |
| 45 | 41 | } |
| 46 | 42 | |
| 47 | 43 | Update(TableMetadata table) { |
| 48 | - super(table); | |
| 49 | - this.table = escapeId(table.getName()); | |
| 44 | + this(escapeId(table.getKeyspace().getName()), | |
| 45 | + escapeId(table.getName()), | |
| 46 | + Arrays.asList(new Object[table.getPartitionKey().size()]), | |
| 47 | + table.getPartitionKey()); | |
| 48 | + } | |
| 49 | + | |
| 50 | + Update(String keyspace, | |
| 51 | + String table, | |
| 52 | + List<Object> routingKeyValues, | |
| 53 | + List<ColumnMetadata> partitionKey) { | |
| 54 | + super(keyspace, partitionKey, routingKeyValues); | |
| 55 | + this.table = table; | |
| 50 | 56 | this.assignments = new Assignments(this); |
| 51 | 57 | this.where = new Where(this); |
| 52 | 58 | this.usings = new Options(this); |
| @@ -136,6 +142,9 @@ | ||
| 136 | 142 | * Adds a conditions clause (IF) to this statement. |
| 137 | 143 | * <p/> |
| 138 | 144 | * This is a shorter/more readable version for {@code onlyIf().and(condition)}. |
| 145 | + * <p/> | |
| 146 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 147 | + * for more information. | |
| 139 | 148 | * |
| 140 | 149 | * @param condition the condition to add. |
| 141 | 150 | * @return the conditions of this query to which more conditions can be added. |
| @@ -211,6 +220,9 @@ | ||
| 211 | 220 | |
| 212 | 221 | /** |
| 213 | 222 | * Adds a condition to the UPDATE statement those assignments are part of. |
| 223 | + * <p/> | |
| 224 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 225 | + * for more information. | |
| 214 | 226 | * |
| 215 | 227 | * @param condition the condition to add. |
| 216 | 228 | * @return the conditions for the UPDATE statement those assignments are part of. |
| @@ -241,6 +253,9 @@ | ||
| 241 | 253 | public Where and(Clause clause) { |
| 242 | 254 | clauses.add(clause); |
| 243 | 255 | statement.maybeAddRoutingKey(clause.name(), clause.firstValue()); |
| 256 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) { | |
| 257 | + statement.setNonIdempotentOps(); | |
| 258 | + } | |
| 244 | 259 | checkForBindMarkers(clause); |
| 245 | 260 | return this; |
| 246 | 261 | } |
| @@ -267,6 +282,9 @@ | ||
| 267 | 282 | |
| 268 | 283 | /** |
| 269 | 284 | * Adds a condition to the UPDATE statement this WHERE clause is part of. |
| 285 | + * <p/> | |
| 286 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 287 | + * for more information. | |
| 270 | 288 | * |
| 271 | 289 | * @param condition the condition to add. |
| 272 | 290 | * @return the conditions for the UPDATE statement this WHERE clause is part of. |
| @@ -279,21 +297,22 @@ | ||
| 279 | 297 | * Sets the 'IF EXISTS' option for the UPDATE statement this WHERE clause |
| 280 | 298 | * is part of. |
| 281 | 299 | * <p/> |
| 282 | - * <p> | |
| 283 | 300 | * An update with that option will report whether the statement actually |
| 284 | 301 | * resulted in data being updated. The existence check and update are |
| 285 | 302 | * done transactionally in the sense that if multiple clients attempt to |
| 286 | 303 | * update a given row with this option, then at most one may succeed. |
| 287 | 304 | * </p> |
| 288 | - * <p> | |
| 289 | 305 | * Please keep in mind that using this option has a non negligible |
| 290 | 306 | * performance impact and should be avoided when possible. |
| 291 | 307 | * </p> |
| 308 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 309 | + * for more information. | |
| 292 | 310 | * |
| 293 | 311 | * @return the UPDATE statement this WHERE clause is part of. |
| 294 | 312 | */ |
| 295 | 313 | public IfExists ifExists() { |
| 296 | 314 | statement.ifExists = true; |
| 315 | + statement.setNonIdempotentOps(); | |
| 297 | 316 | return new IfExists(statement); |
| 298 | 317 | } |
| 299 | 318 | } |
| @@ -379,6 +398,7 @@ | ||
| 379 | 398 | * @return this {@code Conditions} clause. |
| 380 | 399 | */ |
| 381 | 400 | public Conditions and(Clause condition) { |
| 401 | + statement.setNonIdempotentOps(); | |
| 382 | 402 | conditions.add(condition); |
| 383 | 403 | checkForBindMarkers(condition); |
| 384 | 404 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +3 −0
| @@ -229,6 +229,9 @@ | ||
| 229 | 229 | if (!isIdempotent(entry.getKey()) || !isIdempotent(entry.getValue())) |
| 230 | 230 | return false; |
| 231 | 231 | } |
| 232 | + } else if (value instanceof Clause) { | |
| 233 | + Object clauseValue = ((Clause) value).firstValue(); | |
| 234 | + return isIdempotent(clauseValue); | |
| 232 | 235 | } |
| 233 | 236 | return true; |
| 234 | 237 | } |
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +17 −0
| @@ -155,6 +155,23 @@ | ||
| 155 | 155 | update("foo").with(set("v", Lists.newArrayList(now()))).where(eq("k", 1)), |
| 156 | 156 | update("foo").with(set("v", Lists.newArrayList(uuid()))).where(eq("k", 1)), |
| 157 | 157 | |
| 158 | + delete().from("foo").where(lt("k", fcall("now"))), | |
| 159 | + delete().from("foo").where(lt("k", now())), | |
| 160 | + update("foo").where(eq("k", fcall("now"))), | |
| 161 | + delete().listElt("a", 1).from("test_coll"), | |
| 162 | + | |
| 163 | + // LWT | |
| 164 | + update("foo").where(eq("is", "charlie?")).ifExists(), | |
| 165 | + update("foo").where(eq("good", "drivers")).onlyIf(contains("developers", "datastax")), | |
| 166 | + update("foo").onlyIf().and(contains("developers", "datastax")).where(eq("good", "drivers")), | |
| 167 | + update("foo").onlyIf(contains("developers", "datastax")).with(set("v", 0)), | |
| 168 | + update("foo").with(set("v", 0)).onlyIf(contains("hello", "world")), | |
| 169 | + | |
| 170 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())).ifNotExists(), | |
| 171 | + | |
| 172 | + delete().from("foo").where(eq("k", 2)).ifExists(), | |
| 173 | + delete().from("foo").onlyIf(eq("k", 2)), | |
| 174 | + | |
| 158 | 175 | // raw() calls |
| 159 | 176 | |
| 160 | 177 | insertInto("foo").value("k", 1).value("v", raw("foo()")), |