kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 60207be508132a9005cadc0d147382fe79f9e899fc23f668700ad4de311f855d |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 17:15 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 17:15 +02:00 |
| Parents | 104fa6de5417, 5710601b2b51 |
4 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | 7 | - [bug] Check cluster name when connecting to a new node (JAVA-397) |
| 8 | +- [bug] Add missing CAS delete support in QueryBuilder (JAVA-326) | |
| 8 | 9 | |
| 9 | 10 | 2.0.5: |
| 10 | 11 | ------ |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +141 −0
| @@ -30,6 +30,8 @@ | ||
| 30 | 30 | private final List<Object> columnNames; |
| 31 | 31 | private final Where where; |
| 32 | 32 | private final Options usings; |
| 33 | + private final Conditions conditions; | |
| 34 | + private boolean ifExists; | |
| 33 | 35 | |
| 34 | 36 | Delete(String keyspace, String table, List<Object> columnNames) { |
| 35 | 37 | super(keyspace); |
| @@ -37,6 +39,7 @@ | ||
| 37 | 39 | this.columnNames = columnNames; |
| 38 | 40 | this.where = new Where(this); |
| 39 | 41 | this.usings = new Options(this); |
| 42 | + this.conditions = new Conditions(this); | |
| 40 | 43 | } |
| 41 | 44 | |
| 42 | 45 | Delete(TableMetadata table, List<Object> columnNames) { |
| @@ -45,6 +48,7 @@ | ||
| 45 | 48 | this.columnNames = columnNames; |
| 46 | 49 | this.where = new Where(this); |
| 47 | 50 | this.usings = new Options(this); |
| 51 | + this.conditions = new Conditions(this); | |
| 48 | 52 | } |
| 49 | 53 | |
| 50 | 54 | @Override |
| @@ -69,6 +73,15 @@ | ||
| 69 | 73 | Utils.joinAndAppend(builder, " AND ", where.clauses, variables); |
| 70 | 74 | } |
| 71 | 75 | |
| 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 | + | |
| 72 | 85 | return builder; |
| 73 | 86 | } |
| 74 | 87 | |
| @@ -93,6 +106,27 @@ | ||
| 93 | 106 | return where; |
| 94 | 107 | } |
| 95 | 108 | |
| 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 | + | |
| 96 | 130 | /** |
| 97 | 131 | * Adds a new options for this DELETE statement. |
| 98 | 132 | * |
| @@ -103,6 +137,27 @@ | ||
| 103 | 137 | return usings.and(using); |
| 104 | 138 | } |
| 105 | 139 | |
| 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 | + | |
| 106 | 161 | /** |
| 107 | 162 | * The WHERE clause of a DELETE statement. |
| 108 | 163 | */ |
| @@ -137,6 +192,37 @@ | ||
| 137 | 192 | public Options using(Using using) { |
| 138 | 193 | return statement.using(using); |
| 139 | 194 | } |
| 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 | + } | |
| 140 | 226 | } |
| 141 | 227 | |
| 142 | 228 | /** |
| @@ -279,4 +365,59 @@ | ||
| 279 | 365 | return column(sb.append(']').toString()); |
| 280 | 366 | } |
| 281 | 367 | } |
| 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 | + } | |
| 282 | 423 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +24 −0
| @@ -205,4 +205,28 @@ | ||
| 205 | 205 | assertEquals(delete.toString(), query); |
| 206 | 206 | } |
| 207 | 207 | |
| 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 | + } | |
| 208 | 232 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +8 −0
| @@ -315,6 +315,14 @@ | ||
| 315 | 315 | } catch (IllegalArgumentException e) { |
| 316 | 316 | assertEquals(e.getMessage(), "Invalid timestamp, must be positive"); |
| 317 | 317 | } |
| 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); | |
| 318 | 326 | } |
| 319 | 327 | |
| 320 | 328 | @Test(groups = "unit") |