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