kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 21fa34bb30efa44199b7dd63adb24199f52ede4b57e0967a352bd4075caaeeb2 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-15 13:14 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-22 15:55 +02:00 |
| Parents | 03e44ead8f45 |
5 files changed
modified changelog/README.md +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [bug] Forbid bind marker in QueryBuilder add/append/prepend (JAVA-736) |
| 11 | 11 | - [bug] Prevent QueryBuilder.quote() from applying duplicate double quotes (JAVA-712) |
| 12 | 12 | - [bug] Prevent QueryBuilder from trying to serialize raw string (JAVA-688) |
| 13 | +- [bug] Support bind marker in QueryBuilder DELETE's list index (JAVA-679) | |
| 13 | 14 | |
| 14 | 15 | Merged from 2.0.10_fixes branch: |
| 15 | 16 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +94 −27
| @@ -27,25 +27,25 @@ | ||
| 27 | 27 | public class Delete extends BuiltStatement { |
| 28 | 28 | |
| 29 | 29 | private final String table; |
| 30 | - private final List<?> columnNames; | |
| 30 | + private final List<Selector> columns; | |
| 31 | 31 | private final Where where; |
| 32 | 32 | private final Options usings; |
| 33 | 33 | private final Conditions conditions; |
| 34 | 34 | private boolean ifExists; |
| 35 | 35 | |
| 36 | - Delete(String keyspace, String table, List<?> columnNames) { | |
| 36 | + Delete(String keyspace, String table, List<Selector> columns) { | |
| 37 | 37 | super(keyspace); |
| 38 | 38 | this.table = table; |
| 39 | - this.columnNames = columnNames; | |
| 39 | + this.columns = columns; | |
| 40 | 40 | this.where = new Where(this); |
| 41 | 41 | this.usings = new Options(this); |
| 42 | 42 | this.conditions = new Conditions(this); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - Delete(TableMetadata table, List<Object> columnNames) { | |
| 45 | + Delete(TableMetadata table, List<Selector> columns) { | |
| 46 | 46 | super(table); |
| 47 | 47 | this.table = escapeId(table.getName()); |
| 48 | - this.columnNames = columnNames; | |
| 48 | + this.columns = columns; | |
| 49 | 49 | this.where = new Where(this); |
| 50 | 50 | this.usings = new Options(this); |
| 51 | 51 | this.conditions = new Conditions(this); |
| @@ -56,8 +56,8 @@ | ||
| 56 | 56 | StringBuilder builder = new StringBuilder(); |
| 57 | 57 | |
| 58 | 58 | builder.append("DELETE"); |
| 59 | - if (columnNames != null) | |
| 60 | - Utils.joinAndAppendNames(builder.append(" "), ",", columnNames); | |
| 59 | + if (!columns.isEmpty()) | |
| 60 | + Utils.joinAndAppend(builder.append(" "), ",", columns, variables); | |
| 61 | 61 | |
| 62 | 62 | builder.append(" FROM "); |
| 63 | 63 | if (keyspace != null) |
| @@ -264,12 +264,15 @@ | ||
| 264 | 264 | */ |
| 265 | 265 | public static class Builder { |
| 266 | 266 | |
| 267 | - List<Object> columnNames; | |
| 267 | + List<Selector> columns = new ArrayList<Selector>(); | |
| 268 | 268 | |
| 269 | - Builder() {} | |
| 269 | + Builder() { | |
| 270 | + } | |
| 270 | 271 | |
| 271 | - Builder(List<Object> columnNames) { | |
| 272 | - this.columnNames = columnNames; | |
| 272 | + Builder(String... columnNames) { | |
| 273 | + for (String columnName : columnNames) { | |
| 274 | + this.columns.add(new Selector(columnName)); | |
| 275 | + } | |
| 273 | 276 | } |
| 274 | 277 | |
| 275 | 278 | /** |
| @@ -290,7 +293,7 @@ | ||
| 290 | 293 | * @return a newly built DELETE statement that deletes from {@code keyspace.table}. |
| 291 | 294 | */ |
| 292 | 295 | public Delete from(String keyspace, String table) { |
| 293 | - return new Delete(keyspace, table, columnNames); | |
| 296 | + return new Delete(keyspace, table, columns); | |
| 294 | 297 | } |
| 295 | 298 | |
| 296 | 299 | /** |
| @@ -300,7 +303,7 @@ | ||
| 300 | 303 | * @return a newly built DELETE statement that deletes from {@code table}. |
| 301 | 304 | */ |
| 302 | 305 | public Delete from(TableMetadata table) { |
| 303 | - return new Delete(table, columnNames); | |
| 306 | + return new Delete(table, columns); | |
| 304 | 307 | } |
| 305 | 308 | } |
| 306 | 309 | |
| @@ -317,23 +320,20 @@ | ||
| 317 | 320 | * @throws IllegalStateException if some columns had already been selected for this builder. |
| 318 | 321 | */ |
| 319 | 322 | public Builder all() { |
| 320 | - if (columnNames != null) | |
| 321 | - throw new IllegalStateException(String.format("Some columns (%s) have already been selected.", columnNames)); | |
| 323 | + if (!columns.isEmpty()) | |
| 324 | + throw new IllegalStateException(String.format("Some columns (%s) have already been selected.", columns)); | |
| 322 | 325 | |
| 323 | - return (Builder)this; | |
| 326 | + return this; | |
| 324 | 327 | } |
| 325 | 328 | |
| 326 | 329 | /** |
| 327 | 330 | * Deletes the provided column. |
| 328 | 331 | * |
| 329 | - * @param name the column name to select for deletion. | |
| 332 | + * @param columnName the column to select for deletion. | |
| 330 | 333 | * @return this in-build DELETE Selection |
| 331 | 334 | */ |
| 332 | - public Selection column(String name) { | |
| 333 | - if (columnNames == null) | |
| 334 | - columnNames = new ArrayList<Object>(); | |
| 335 | - | |
| 336 | - columnNames.add(name); | |
| 335 | + public Selection column(String columnName) { | |
| 336 | + columns.add(new Selector(columnName)); | |
| 337 | 337 | return this; |
| 338 | 338 | } |
| 339 | 339 | |
| @@ -345,9 +345,21 @@ | ||
| 345 | 345 | * @return this in-build DELETE Selection |
| 346 | 346 | */ |
| 347 | 347 | public Selection listElt(String columnName, int idx) { |
| 348 | - StringBuilder sb = new StringBuilder(); | |
| 349 | - Utils.appendName(columnName, sb); | |
| 350 | - return column(sb.append('[').append(idx).append(']').toString()); | |
| 348 | + columns.add(new IndexedSelector(columnName, idx)); | |
| 349 | + return this; | |
| 350 | + } | |
| 351 | + | |
| 352 | + /** | |
| 353 | + * Deletes the provided list element, | |
| 354 | + * specified as a bind marker. | |
| 355 | + * | |
| 356 | + * @param columnName the name of the list column. | |
| 357 | + * @param idx the index of the element to delete. | |
| 358 | + * @return this in-build DELETE Selection | |
| 359 | + */ | |
| 360 | + public Selection listElt(String columnName, BindMarker idx) { | |
| 361 | + columns.add(new IndexedSelector(columnName, idx)); | |
| 362 | + return this; | |
| 351 | 363 | } |
| 352 | 364 | |
| 353 | 365 | /** |
| @@ -358,12 +370,67 @@ | ||
| 358 | 370 | * @return this in-build DELETE Selection |
| 359 | 371 | */ |
| 360 | 372 | public Selection mapElt(String columnName, Object key) { |
| 361 | - StringBuilder sb = new StringBuilder(); | |
| 373 | + columns.add(new IndexedSelector(columnName, key)); | |
| 374 | + return this; | |
| 375 | + } | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * A selector in a DELETE selection clause. | |
| 380 | + * A selector can be either a column name, | |
| 381 | + * a list element or a map entry. | |
| 382 | + */ | |
| 383 | + static class Selector extends Utils.Appendeable { | |
| 384 | + | |
| 385 | + private final String columnName; | |
| 386 | + | |
| 387 | + Selector(String columnName) { | |
| 388 | + this.columnName = columnName; | |
| 389 | + } | |
| 390 | + | |
| 391 | + @Override | |
| 392 | + void appendTo(StringBuilder sb, List<ByteBuffer> values) { | |
| 362 | 393 | Utils.appendName(columnName, sb); |
| 394 | + } | |
| 395 | + | |
| 396 | + @Override | |
| 397 | + boolean containsBindMarker() { | |
| 398 | + return false; | |
| 399 | + } | |
| 400 | + | |
| 401 | + @Override | |
| 402 | + public String toString() { | |
| 403 | + StringBuilder sb = new StringBuilder(); | |
| 404 | + appendTo(sb, new ArrayList<ByteBuffer>()); | |
| 405 | + return sb.toString(); | |
| 406 | + } | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * A list element or a map entry in a DELETE selection clause. | |
| 411 | + */ | |
| 412 | + static class IndexedSelector extends Selector { | |
| 413 | + | |
| 414 | + private final Object key; | |
| 415 | + | |
| 416 | + IndexedSelector(String columnName, Object key) { | |
| 417 | + super(columnName); | |
| 418 | + this.key = key; | |
| 419 | + } | |
| 420 | + | |
| 421 | + @Override | |
| 422 | + void appendTo(StringBuilder sb, List<ByteBuffer> values) { | |
| 423 | + super.appendTo(sb, values); | |
| 363 | 424 | sb.append('['); |
| 364 | 425 | Utils.appendFlatValue(key, sb); |
| 365 | - return column(sb.append(']').toString()); | |
| 426 | + sb.append(']'); | |
| 366 | 427 | } |
| 428 | + | |
| 429 | + @Override | |
| 430 | + boolean containsBindMarker() { | |
| 431 | + return Utils.containsBindMarker(key); | |
| 432 | + } | |
| 433 | + | |
| 367 | 434 | } |
| 368 | 435 | |
| 369 | 436 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +1 −1
| @@ -137,7 +137,7 @@ | ||
| 137 | 137 | * clause needs to be provided to complete the query). |
| 138 | 138 | */ |
| 139 | 139 | public static Delete.Builder delete(String... columns) { |
| 140 | - return new Delete.Builder(Arrays.asList((Object[])columns)); | |
| 140 | + return new Delete.Builder(columns); | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +62 −7
| @@ -15,17 +15,19 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import java.util.Arrays; | |
| 19 | -import java.util.Collection; | |
| 20 | -import java.util.Date; | |
| 21 | -import java.util.List; | |
| 18 | +import java.util.*; | |
| 22 | 19 | |
| 23 | 20 | import org.testng.annotations.Test; |
| 24 | 21 | |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 23 | +import static org.assertj.core.data.MapEntry.entry; | |
| 24 | +import static org.testng.Assert.assertEquals; | |
| 25 | +import static org.testng.Assert.assertFalse; | |
| 26 | +import static org.testng.Assert.assertTrue; | |
| 27 | + | |
| 25 | 28 | import com.datastax.driver.core.*; |
| 26 | 29 | |
| 27 | 30 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 28 | -import static org.testng.Assert.*; | |
| 29 | 31 | |
| 30 | 32 | public class QueryBuilderExecutionTest extends CCMBridge.PerClassSingleNodeCluster { |
| 31 | 33 | |
| @@ -33,8 +35,10 @@ | ||
| 33 | 35 | |
| 34 | 36 | @Override |
| 35 | 37 | protected Collection<String> getTableDefinitions() { |
| 36 | - return Arrays.asList(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), | |
| 37 | - "CREATE TABLE dateTest (t timestamp PRIMARY KEY)"); | |
| 38 | + return Arrays.asList( | |
| 39 | + String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), | |
| 40 | + "CREATE TABLE dateTest (t timestamp PRIMARY KEY)", | |
| 41 | + "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>)"); | |
| 38 | 42 | } |
| 39 | 43 | |
| 40 | 44 | @Test(groups = "short") |
| @@ -100,4 +104,55 @@ | ||
| 100 | 104 | assertEquals("batchTest2", r2.getString("k")); |
| 101 | 105 | assertEquals("val2", r2.getString("t")); |
| 102 | 106 | } |
| 107 | + | |
| 108 | + @Test(groups = "short") | |
| 109 | + public void should_delete_list_element() throws Exception { | |
| 110 | + //given | |
| 111 | + session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); | |
| 112 | + //when | |
| 113 | + BuiltStatement statement = delete().listElt("a", 1).from("test_coll").where(eq("k", 1)); | |
| 114 | + session.execute(statement); | |
| 115 | + //then | |
| 116 | + List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 117 | + assertThat(actual).containsExactly(1, 3); | |
| 118 | + } | |
| 119 | + | |
| 120 | + @Test(groups = "short") | |
| 121 | + public void should_delete_list_element_with_bind_marker() throws Exception { | |
| 122 | + //given | |
| 123 | + session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); | |
| 124 | + //when | |
| 125 | + BuiltStatement statement = delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 126 | + PreparedStatement ps = session.prepare(statement); | |
| 127 | + session.execute(ps.bind(1)); | |
| 128 | + //then | |
| 129 | + List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 130 | + assertThat(actual).containsExactly(1, 3); | |
| 131 | + } | |
| 132 | + | |
| 133 | + @Test(groups = "short") | |
| 134 | + public void should_delete_map_entry() throws Exception { | |
| 135 | + //given | |
| 136 | + session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); | |
| 137 | + //when | |
| 138 | + BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); | |
| 139 | + session.execute(statement); | |
| 140 | + //then | |
| 141 | + Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 142 | + assertThat(actual).containsExactly(entry(2, "bar")); | |
| 143 | + } | |
| 144 | + | |
| 145 | + @Test(groups = "short") | |
| 146 | + public void should_delete_map_entry_with_bind_marker() throws Exception { | |
| 147 | + //given | |
| 148 | + session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); | |
| 149 | + //when | |
| 150 | + BuiltStatement statement = delete().mapElt("b", bindMarker("mapEntry")).from("test_coll").where(eq("k", 1)); | |
| 151 | + PreparedStatement ps = session.prepare(statement); | |
| 152 | + session.execute(ps.bind().setInt("mapEntry", 1)); | |
| 153 | + //then | |
| 154 | + Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 155 | + assertThat(actual).containsExactly(entry(2, "bar")); | |
| 156 | + } | |
| 157 | + | |
| 103 | 158 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +4 −0
| @@ -305,6 +305,10 @@ | ||
| 305 | 305 | delete = delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1)); |
| 306 | 306 | assertEquals(delete.toString(), query); |
| 307 | 307 | |
| 308 | + query = "DELETE a[?],b[?],c FROM foo WHERE k=1;"; | |
| 309 | + delete = delete().listElt("a", bindMarker()).mapElt("b", bindMarker()).column("c").from("foo").where(eq("k", 1)); | |
| 310 | + assertEquals(delete.toString(), query); | |
| 311 | + | |
| 308 | 312 | // Invalid CQL, testing edge case |
| 309 | 313 | query = "DELETE a,b,c FROM foo;"; |
| 310 | 314 | delete = delete("a", "b", "c").from("foo"); |