kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f70a42a4e0dd502e3d95d6a6ae727a3a5377dd372605acab6f136fc8adc6fd66 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2016-02-26 17:00 UTC |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 20:21 +01:00 |
| Parents | 449ad3d4341b |
10 files changed
modified changelog/README.md +1 −0
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key. |
| 29 | 29 | - [improvement] JAVA-888: Add cluster-wide percentile tracker. |
| 30 | 30 | - [improvement] JAVA-963: Automatically register PercentileTracker from components that use it. |
| 31 | +- [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent. | |
| 31 | 32 | |
| 32 | 33 | Merged from 2.0 branch: |
| 33 | 34 |
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 +7 −13
| @@ -28,7 +28,7 @@ | ||
| 28 | 28 | */ |
| 29 | 29 | public abstract class BuiltStatement extends RegularStatement { |
| 30 | 30 | |
| 31 | - private static final Pattern lowercaseId = Pattern.compile("[a-z][a-z0-9_]*"); | |
| 31 | + private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*"); | |
| 32 | 32 | |
| 33 | 33 | private final List<ColumnMetadata> partitionKey; |
| 34 | 34 | private final ByteBuffer[] routingKey; |
| @@ -46,22 +46,16 @@ | ||
| 46 | 46 | boolean hasBindMarkers; |
| 47 | 47 | private boolean forceNoValues; |
| 48 | 48 | |
| 49 | - BuiltStatement(String keyspace) { | |
| 50 | - this.partitionKey = null; | |
| 51 | - this.routingKey = null; | |
| 49 | + BuiltStatement(String keyspace, List<ColumnMetadata> partitionKey, ByteBuffer[] routingKey) { | |
| 50 | + this.partitionKey = partitionKey; | |
| 51 | + this.routingKey = routingKey; | |
| 52 | 52 | this.keyspace = keyspace; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - BuiltStatement(TableMetadata tableMetadata) { | |
| 56 | - this.partitionKey = tableMetadata.getPartitionKey(); | |
| 57 | - this.routingKey = new ByteBuffer[tableMetadata.getPartitionKey().size()]; | |
| 58 | - this.keyspace = escapeId(tableMetadata.getKeyspace().getName()); | |
| 59 | - } | |
| 60 | - | |
| 61 | 55 | // Same as Metadata.escapeId, but we don't have access to it here. |
| 62 | - protected String escapeId(String ident) { | |
| 56 | + protected static String escapeId(String ident) { | |
| 63 | 57 | // we don't need to escape if it's lowercase and match non-quoted CQL3 ids. |
| 64 | - return lowercaseId.matcher(ident).matches() ? ident : Metadata.quote(ident); | |
| 58 | + return lowercaseAlphanumeric.matcher(ident).matches() ? ident : Metadata.quote(ident); | |
| 65 | 59 | } |
| 66 | 60 | |
| 67 | 61 | @Override |
| @@ -292,7 +286,7 @@ | ||
| 292 | 286 | T statement; |
| 293 | 287 | |
| 294 | 288 | ForwardingStatement(T statement) { |
| 295 | - super((String) null); | |
| 289 | + super(null, null, null); | |
| 296 | 290 | this.statement = statement; |
| 297 | 291 | } |
| 298 | 292 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +73 −16
| @@ -15,8 +15,10 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.ColumnMetadata; | |
| 18 | 19 | import com.datastax.driver.core.TableMetadata; |
| 19 | 20 | |
| 21 | +import java.nio.ByteBuffer; | |
| 20 | 22 | import java.util.ArrayList; |
| 21 | 23 | import java.util.List; |
| 22 | 24 | |
| @@ -33,21 +35,33 @@ | ||
| 33 | 35 | private boolean ifExists; |
| 34 | 36 | |
| 35 | 37 | Delete(String keyspace, String table, List<Selector> columns) { |
| 36 | - super(keyspace); | |
| 37 | - this.table = table; | |
| 38 | - this.columns = columns; | |
| 39 | - this.where = new Where(this); | |
| 40 | - this.usings = new Options(this); | |
| 41 | - this.conditions = new Conditions(this); | |
| 38 | + this(keyspace, table, null, null, columns); | |
| 42 | 39 | } |
| 43 | 40 | |
| 44 | 41 | Delete(TableMetadata table, List<Selector> columns) { |
| 45 | - super(table); | |
| 46 | - this.table = escapeId(table.getName()); | |
| 42 | + this(escapeId(table.getKeyspace().getName()), | |
| 43 | + escapeId(table.getName()), | |
| 44 | + new ByteBuffer[table.getPartitionKey().size()], | |
| 45 | + table.getPartitionKey(), | |
| 46 | + columns); | |
| 47 | + } | |
| 48 | + | |
| 49 | + Delete(String keyspace, | |
| 50 | + String table, | |
| 51 | + ByteBuffer[] routingKey, | |
| 52 | + List<ColumnMetadata> partitionKey, | |
| 53 | + List<Selector> columns) { | |
| 54 | + super(keyspace, partitionKey, routingKey); | |
| 55 | + this.table = table; | |
| 47 | 56 | this.columns = columns; |
| 48 | 57 | this.where = new Where(this); |
| 49 | 58 | this.usings = new Options(this); |
| 50 | 59 | this.conditions = new Conditions(this); |
| 60 | + | |
| 61 | + // This is for JAVA-1089, if the query deletes an element in a list, the statement should be non-idempotent. | |
| 62 | + if (!areIdempotent(columns)) { | |
| 63 | + setNonIdempotentOps(); | |
| 64 | + } | |
| 51 | 65 | } |
| 52 | 66 | |
| 53 | 67 | @Override |
| @@ -109,6 +123,9 @@ | ||
| 109 | 123 | * Adds a conditions clause (IF) to this statement. |
| 110 | 124 | * <p/> |
| 111 | 125 | * This is a shorter/more readable version for {@code onlyIf().and(condition)}. |
| 126 | + * <p/> | |
| 127 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 128 | + * for more information. | |
| 112 | 129 | * |
| 113 | 130 | * @param condition the condition to add. |
| 114 | 131 | * @return the conditions of this query to which more conditions can be added. |
| @@ -119,6 +136,9 @@ | ||
| 119 | 136 | |
| 120 | 137 | /** |
| 121 | 138 | * Adds a conditions clause (IF) to this statement. |
| 139 | + * <p/> | |
| 140 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 141 | + * for more information. | |
| 122 | 142 | * |
| 123 | 143 | * @return the conditions of this query to which more conditions can be added. |
| 124 | 144 | */ |
| @@ -160,11 +180,14 @@ | ||
| 160 | 180 | * Please keep in mind that using this option has a non negligible |
| 161 | 181 | * performance impact and should be avoided when possible. |
| 162 | 182 | * </p> |
| 183 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 184 | + * for more information. | |
| 163 | 185 | * |
| 164 | 186 | * @return this DELETE statement. |
| 165 | 187 | */ |
| 166 | 188 | public Delete ifExists() { |
| 167 | 189 | this.ifExists = true; |
| 190 | + setNonIdempotentOps(); | |
| 168 | 191 | return this; |
| 169 | 192 | } |
| 170 | 193 | |
| @@ -188,6 +211,9 @@ | ||
| 188 | 211 | public Where and(Clause clause) { |
| 189 | 212 | clauses.add(clause); |
| 190 | 213 | statement.maybeAddRoutingKey(clause.name(), clause.firstValue()); |
| 214 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) { | |
| 215 | + statement.setNonIdempotentOps(); | |
| 216 | + } | |
| 191 | 217 | checkForBindMarkers(clause); |
| 192 | 218 | return this; |
| 193 | 219 | } |
| @@ -353,7 +379,7 @@ | ||
| 353 | 379 | * @return this in-build DELETE Selection |
| 354 | 380 | */ |
| 355 | 381 | public Selection listElt(String columnName, int idx) { |
| 356 | - columns.add(new CollectionElementSelector(columnName, idx)); | |
| 382 | + columns.add(new ListElementSelector(columnName, idx)); | |
| 357 | 383 | return this; |
| 358 | 384 | } |
| 359 | 385 | |
| @@ -366,7 +392,7 @@ | ||
| 366 | 392 | * @return this in-build DELETE Selection |
| 367 | 393 | */ |
| 368 | 394 | public Selection listElt(String columnName, BindMarker idx) { |
| 369 | - columns.add(new CollectionElementSelector(columnName, idx)); | |
| 395 | + columns.add(new ListElementSelector(columnName, idx)); | |
| 370 | 396 | return this; |
| 371 | 397 | } |
| 372 | 398 | |
| @@ -378,7 +404,7 @@ | ||
| 378 | 404 | * @return this in-build DELETE Selection |
| 379 | 405 | */ |
| 380 | 406 | public Selection setElt(String columnName, Object element) { |
| 381 | - columns.add(new CollectionElementSelector(columnName, element)); | |
| 407 | + columns.add(new SetElementSelector(columnName, element)); | |
| 382 | 408 | return this; |
| 383 | 409 | } |
| 384 | 410 | |
| @@ -391,7 +417,7 @@ | ||
| 391 | 417 | * @return this in-build DELETE Selection |
| 392 | 418 | */ |
| 393 | 419 | public Selection setElt(String columnName, BindMarker element) { |
| 394 | - columns.add(new CollectionElementSelector(columnName, element)); | |
| 420 | + columns.add(new SetElementSelector(columnName, element)); | |
| 395 | 421 | return this; |
| 396 | 422 | } |
| 397 | 423 | |
| @@ -403,7 +429,7 @@ | ||
| 403 | 429 | * @return this in-build DELETE Selection |
| 404 | 430 | */ |
| 405 | 431 | public Selection mapElt(String columnName, Object key) { |
| 406 | - columns.add(new CollectionElementSelector(columnName, key)); | |
| 432 | + columns.add(new MapElementSelector(columnName, key)); | |
| 407 | 433 | return this; |
| 408 | 434 | } |
| 409 | 435 | } |
| @@ -413,7 +439,7 @@ | ||
| 413 | 439 | * A selector can be either a column name, |
| 414 | 440 | * a list element, a set element or a map entry. |
| 415 | 441 | */ |
| 416 | - static class Selector extends Utils.Appendeable { | |
| 442 | + private static class Selector extends Utils.Appendeable { | |
| 417 | 443 | |
| 418 | 444 | private final String columnName; |
| 419 | 445 | |
| @@ -442,9 +468,9 @@ | ||
| 442 | 468 | /** |
| 443 | 469 | * A selector representing a list index, a set element or a map key in a DELETE selection clause. |
| 444 | 470 | */ |
| 445 | - static class CollectionElementSelector extends Selector { | |
| 471 | + private static class CollectionElementSelector extends Selector { | |
| 446 | 472 | |
| 447 | - private final Object key; | |
| 473 | + protected final Object key; | |
| 448 | 474 | |
| 449 | 475 | CollectionElementSelector(String columnName, Object key) { |
| 450 | 476 | super(columnName); |
| @@ -466,6 +492,36 @@ | ||
| 466 | 492 | |
| 467 | 493 | } |
| 468 | 494 | |
| 495 | + private static class ListElementSelector extends CollectionElementSelector { | |
| 496 | + | |
| 497 | + ListElementSelector(String columnName, Object key) { | |
| 498 | + super(columnName, key); | |
| 499 | + } | |
| 500 | + } | |
| 501 | + | |
| 502 | + private static class SetElementSelector extends CollectionElementSelector { | |
| 503 | + | |
| 504 | + SetElementSelector(String columnName, Object key) { | |
| 505 | + super(columnName, key); | |
| 506 | + } | |
| 507 | + } | |
| 508 | + | |
| 509 | + private static class MapElementSelector extends CollectionElementSelector { | |
| 510 | + | |
| 511 | + MapElementSelector(String columnName, Object key) { | |
| 512 | + super(columnName, key); | |
| 513 | + } | |
| 514 | + } | |
| 515 | + | |
| 516 | + private boolean areIdempotent(List<Selector> selectors) { | |
| 517 | + for (Selector sel : selectors) { | |
| 518 | + if (sel instanceof ListElementSelector) { | |
| 519 | + return false; | |
| 520 | + } | |
| 521 | + } | |
| 522 | + return true; | |
| 523 | + } | |
| 524 | + | |
| 469 | 525 | /** |
| 470 | 526 | * Conditions for a DELETE statement. |
| 471 | 527 | * <p> |
| @@ -495,6 +551,7 @@ | ||
| 495 | 551 | * @return this {@code Conditions} clause. |
| 496 | 552 | */ |
| 497 | 553 | public Conditions and(Clause condition) { |
| 554 | + statement.setNonIdempotentOps(); | |
| 498 | 555 | conditions.add(condition); |
| 499 | 556 | checkForBindMarkers(condition); |
| 500 | 557 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +19 −5
| @@ -15,8 +15,10 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.ColumnMetadata; | |
| 18 | 19 | import com.datastax.driver.core.TableMetadata; |
| 19 | 20 | |
| 21 | +import java.nio.ByteBuffer; | |
| 20 | 22 | import java.util.ArrayList; |
| 21 | 23 | import java.util.Arrays; |
| 22 | 24 | import java.util.List; |
| @@ -33,14 +35,22 @@ | ||
| 33 | 35 | private boolean ifNotExists; |
| 34 | 36 | |
| 35 | 37 | Insert(String keyspace, String table) { |
| 36 | - super(keyspace); | |
| 37 | - this.table = table; | |
| 38 | - this.usings = new Options(this); | |
| 38 | + this(keyspace, table, null, null); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | Insert(TableMetadata table) { |
| 42 | - super(table); | |
| 43 | - this.table = escapeId(table.getName()); | |
| 42 | + this(escapeId(table.getKeyspace().getName()), | |
| 43 | + escapeId(table.getName()), | |
| 44 | + new ByteBuffer[table.getPartitionKey().size()], | |
| 45 | + table.getPartitionKey()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + Insert(String keyspace, | |
| 49 | + String table, | |
| 50 | + ByteBuffer[] routingKey, | |
| 51 | + List<ColumnMetadata> partitionKey) { | |
| 52 | + super(keyspace, partitionKey, routingKey); | |
| 53 | + this.table = table; | |
| 44 | 54 | this.usings = new Options(this); |
| 45 | 55 | } |
| 46 | 56 | |
| @@ -156,10 +166,14 @@ | ||
| 156 | 166 | * <p/> |
| 157 | 167 | * Please keep in mind that using this option has a non negligible |
| 158 | 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. | |
| 159 | 172 | * |
| 160 | 173 | * @return this INSERT statement. |
| 161 | 174 | */ |
| 162 | 175 | public Insert ifNotExists() { |
| 176 | + this.setNonIdempotentOps(); | |
| 163 | 177 | this.ifNotExists = true; |
| 164 | 178 | return this; |
| 165 | 179 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +19 −7
| @@ -15,8 +15,10 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.ColumnMetadata; | |
| 18 | 19 | import com.datastax.driver.core.TableMetadata; |
| 19 | 20 | |
| 21 | +import java.nio.ByteBuffer; | |
| 20 | 22 | import java.util.ArrayList; |
| 21 | 23 | import java.util.Arrays; |
| 22 | 24 | import java.util.Collections; |
| @@ -38,16 +40,26 @@ | ||
| 38 | 40 | private boolean allowFiltering; |
| 39 | 41 | |
| 40 | 42 | Select(String keyspace, String table, List<Object> columnNames, boolean isDistinct) { |
| 41 | - super(keyspace); | |
| 42 | - this.table = table; | |
| 43 | - this.isDistinct = isDistinct; | |
| 44 | - this.columnNames = columnNames; | |
| 45 | - this.where = new Where(this); | |
| 43 | + this(keyspace, table, null, null, columnNames, isDistinct); | |
| 46 | 44 | } |
| 47 | 45 | |
| 48 | 46 | Select(TableMetadata table, List<Object> columnNames, boolean isDistinct) { |
| 49 | - super(table); | |
| 50 | - this.table = escapeId(table.getName()); | |
| 47 | + this(escapeId(table.getKeyspace().getName()), | |
| 48 | + escapeId(table.getName()), | |
| 49 | + new ByteBuffer[table.getPartitionKey().size()], | |
| 50 | + table.getPartitionKey(), | |
| 51 | + columnNames, | |
| 52 | + isDistinct); | |
| 53 | + } | |
| 54 | + | |
| 55 | + Select(String keyspace, | |
| 56 | + String table, | |
| 57 | + ByteBuffer[] routingKey, | |
| 58 | + List<ColumnMetadata> partitionKey, | |
| 59 | + List<Object> columnNames, | |
| 60 | + boolean isDistinct) { | |
| 61 | + super(keyspace, partitionKey, routingKey); | |
| 62 | + this.table = table; | |
| 51 | 63 | this.isDistinct = isDistinct; |
| 52 | 64 | this.columnNames = columnNames; |
| 53 | 65 | this.where = new Where(this); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +15 −4
| @@ -15,8 +15,10 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.ColumnMetadata; | |
| 18 | 19 | import com.datastax.driver.core.TableMetadata; |
| 19 | 20 | |
| 21 | +import java.nio.ByteBuffer; | |
| 20 | 22 | import java.util.List; |
| 21 | 23 | |
| 22 | 24 | /** |
| @@ -27,13 +29,22 @@ | ||
| 27 | 29 | private final String table; |
| 28 | 30 | |
| 29 | 31 | Truncate(String keyspace, String table) { |
| 30 | - super(keyspace); | |
| 31 | - this.table = table; | |
| 32 | + this(keyspace, table, null, null); | |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | Truncate(TableMetadata table) { |
| 35 | - super(table); | |
| 36 | - this.table = escapeId(table.getName()); | |
| 36 | + this(escapeId(table.getKeyspace().getName()), | |
| 37 | + escapeId(table.getName()), | |
| 38 | + new ByteBuffer[table.getPartitionKey().size()], | |
| 39 | + table.getPartitionKey()); | |
| 40 | + } | |
| 41 | + | |
| 42 | + Truncate(String keyspace, | |
| 43 | + String table, | |
| 44 | + ByteBuffer[] routingKey, | |
| 45 | + List<ColumnMetadata> partitionKey) { | |
| 46 | + super(keyspace, partitionKey, routingKey); | |
| 47 | + this.table = table; | |
| 37 | 48 | } |
| 38 | 49 | |
| 39 | 50 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +31 −11
| @@ -15,9 +15,11 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.ColumnMetadata; | |
| 18 | 19 | import com.datastax.driver.core.TableMetadata; |
| 19 | 20 | import com.datastax.driver.core.querybuilder.Assignment.CounterAssignment; |
| 20 | 21 | |
| 22 | +import java.nio.ByteBuffer; | |
| 21 | 23 | import java.util.ArrayList; |
| 22 | 24 | import java.util.List; |
| 23 | 25 | |
| @@ -34,18 +36,22 @@ | ||
| 34 | 36 | private boolean ifExists; |
| 35 | 37 | |
| 36 | 38 | Update(String keyspace, String table) { |
| 37 | - super(keyspace); | |
| 38 | - this.table = table; | |
| 39 | - this.assignments = new Assignments(this); | |
| 40 | - this.where = new Where(this); | |
| 41 | - this.usings = new Options(this); | |
| 42 | - this.conditions = new Conditions(this); | |
| 43 | - this.ifExists = false; | |
| 39 | + this(keyspace, table, null, null); | |
| 44 | 40 | } |
| 45 | 41 | |
| 46 | 42 | Update(TableMetadata table) { |
| 47 | - super(table); | |
| 48 | - this.table = escapeId(table.getName()); | |
| 43 | + this(escapeId(table.getKeyspace().getName()), | |
| 44 | + escapeId(table.getName()), | |
| 45 | + new ByteBuffer[table.getPartitionKey().size()], | |
| 46 | + table.getPartitionKey()); | |
| 47 | + } | |
| 48 | + | |
| 49 | + Update(String keyspace, | |
| 50 | + String table, | |
| 51 | + ByteBuffer[] routingKey, | |
| 52 | + List<ColumnMetadata> partitionKey) { | |
| 53 | + super(keyspace, partitionKey, routingKey); | |
| 54 | + this.table = table; | |
| 49 | 55 | this.assignments = new Assignments(this); |
| 50 | 56 | this.where = new Where(this); |
| 51 | 57 | this.usings = new Options(this); |
| @@ -135,6 +141,9 @@ | ||
| 135 | 141 | * Adds a conditions clause (IF) to this statement. |
| 136 | 142 | * <p/> |
| 137 | 143 | * This is a shorter/more readable version for {@code onlyIf().and(condition)}. |
| 144 | + * <p/> | |
| 145 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 146 | + * for more information. | |
| 138 | 147 | * |
| 139 | 148 | * @param condition the condition to add. |
| 140 | 149 | * @return the conditions of this query to which more conditions can be added. |
| @@ -210,6 +219,9 @@ | ||
| 210 | 219 | |
| 211 | 220 | /** |
| 212 | 221 | * Adds a condition to the UPDATE statement those assignments are part of. |
| 222 | + * <p/> | |
| 223 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 224 | + * for more information. | |
| 213 | 225 | * |
| 214 | 226 | * @param condition the condition to add. |
| 215 | 227 | * @return the conditions for the UPDATE statement those assignments are part of. |
| @@ -240,6 +252,9 @@ | ||
| 240 | 252 | public Where and(Clause clause) { |
| 241 | 253 | clauses.add(clause); |
| 242 | 254 | statement.maybeAddRoutingKey(clause.name(), clause.firstValue()); |
| 255 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) { | |
| 256 | + statement.setNonIdempotentOps(); | |
| 257 | + } | |
| 243 | 258 | checkForBindMarkers(clause); |
| 244 | 259 | return this; |
| 245 | 260 | } |
| @@ -266,6 +281,9 @@ | ||
| 266 | 281 | |
| 267 | 282 | /** |
| 268 | 283 | * Adds a condition to the UPDATE statement this WHERE clause is part of. |
| 284 | + * <p/> | |
| 285 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 286 | + * for more information. | |
| 269 | 287 | * |
| 270 | 288 | * @param condition the condition to add. |
| 271 | 289 | * @return the conditions for the UPDATE statement this WHERE clause is part of. |
| @@ -278,21 +296,22 @@ | ||
| 278 | 296 | * Sets the 'IF EXISTS' option for the UPDATE statement this WHERE clause |
| 279 | 297 | * is part of. |
| 280 | 298 | * <p/> |
| 281 | - * <p> | |
| 282 | 299 | * An update with that option will report whether the statement actually |
| 283 | 300 | * resulted in data being updated. The existence check and update are |
| 284 | 301 | * done transactionally in the sense that if multiple clients attempt to |
| 285 | 302 | * update a given row with this option, then at most one may succeed. |
| 286 | 303 | * </p> |
| 287 | - * <p> | |
| 288 | 304 | * Please keep in mind that using this option has a non negligible |
| 289 | 305 | * performance impact and should be avoided when possible. |
| 290 | 306 | * </p> |
| 307 | + * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()} | |
| 308 | + * for more information. | |
| 291 | 309 | * |
| 292 | 310 | * @return the UPDATE statement this WHERE clause is part of. |
| 293 | 311 | */ |
| 294 | 312 | public IfExists ifExists() { |
| 295 | 313 | statement.ifExists = true; |
| 314 | + statement.setNonIdempotentOps(); | |
| 296 | 315 | return new IfExists(statement); |
| 297 | 316 | } |
| 298 | 317 | } |
| @@ -378,6 +397,7 @@ | ||
| 378 | 397 | * @return this {@code Conditions} clause. |
| 379 | 398 | */ |
| 380 | 399 | public Conditions and(Clause condition) { |
| 400 | + statement.setNonIdempotentOps(); | |
| 381 | 401 | conditions.add(condition); |
| 382 | 402 | checkForBindMarkers(condition); |
| 383 | 403 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +3 −0
| @@ -307,6 +307,9 @@ | ||
| 307 | 307 | if (!isIdempotent(entry.getKey()) || !isIdempotent(entry.getValue())) |
| 308 | 308 | return false; |
| 309 | 309 | } |
| 310 | + } else if (value instanceof Clause) { | |
| 311 | + Object clauseValue = ((Clause) value).firstValue(); | |
| 312 | + return isIdempotent(clauseValue); | |
| 310 | 313 | } |
| 311 | 314 | return true; |
| 312 | 315 | } |
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +17 −0
| @@ -137,6 +137,23 @@ | ||
| 137 | 137 | update("foo").with(set("v", Lists.newArrayList(now()))).where(eq("k", 1)), |
| 138 | 138 | update("foo").with(set("v", Lists.newArrayList(uuid()))).where(eq("k", 1)), |
| 139 | 139 | |
| 140 | + delete().from("foo").where(lt("k", fcall("now"))), | |
| 141 | + delete().from("foo").where(lt("k", now())), | |
| 142 | + update("foo").where(eq("k", fcall("now"))), | |
| 143 | + delete().listElt("a", 1).from("test_coll"), | |
| 144 | + | |
| 145 | + // LWT | |
| 146 | + update("foo").where(eq("is", "charlie?")).ifExists(), | |
| 147 | + update("foo").where(eq("good", "drivers")).onlyIf(contains("developers", "datastax")), | |
| 148 | + update("foo").onlyIf().and(contains("developers", "datastax")).where(eq("good", "drivers")), | |
| 149 | + update("foo").onlyIf(contains("developers", "datastax")).with(set("v", 0)), | |
| 150 | + update("foo").with(set("v", 0)).onlyIf(contains("hello", "world")), | |
| 151 | + | |
| 152 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())).ifNotExists(), | |
| 153 | + | |
| 154 | + delete().from("foo").where(eq("k", 2)).ifExists(), | |
| 155 | + delete().from("foo").onlyIf(eq("k", 2)), | |
| 156 | + | |
| 140 | 157 | // raw() calls |
| 141 | 158 | |
| 142 | 159 | insertInto("foo").value("k", 1).value("v", raw("foo()")), |