kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8215ada6fc0cc4952dfe85b13472c7db8f1160f386d64b0d20a20e920d15c0c5 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-26 10:43 +02:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-26 18:08 +02:00 |
| Parents | fecf8f66e2dc |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +32 −7
| @@ -345,7 +345,7 @@ | ||
| 345 | 345 | * @return this in-build DELETE Selection |
| 346 | 346 | */ |
| 347 | 347 | public Selection listElt(String columnName, int idx) { |
| 348 | - columns.add(new IndexedSelector(columnName, idx)); | |
| 348 | + columns.add(new CollectionElementSelector(columnName, idx)); | |
| 349 | 349 | return this; |
| 350 | 350 | } |
| 351 | 351 | |
| @@ -358,7 +358,32 @@ | ||
| 358 | 358 | * @return this in-build DELETE Selection |
| 359 | 359 | */ |
| 360 | 360 | public Selection listElt(String columnName, BindMarker idx) { |
| 361 | - columns.add(new IndexedSelector(columnName, idx)); | |
| 361 | + columns.add(new CollectionElementSelector(columnName, idx)); | |
| 362 | + return this; | |
| 363 | + } | |
| 364 | + | |
| 365 | + /** | |
| 366 | + * Deletes the provided set element. | |
| 367 | + * | |
| 368 | + * @param columnName the name of the set column. | |
| 369 | + * @param element the element to delete. | |
| 370 | + * @return this in-build DELETE Selection | |
| 371 | + */ | |
| 372 | + public Selection setElt(String columnName, Object element) { | |
| 373 | + columns.add(new CollectionElementSelector(columnName, element)); | |
| 374 | + return this; | |
| 375 | + } | |
| 376 | + | |
| 377 | + /** | |
| 378 | + * Deletes the provided set element, | |
| 379 | + * specified as a bind marker. | |
| 380 | + * | |
| 381 | + * @param columnName the name of the set column. | |
| 382 | + * @param element the element to delete. | |
| 383 | + * @return this in-build DELETE Selection | |
| 384 | + */ | |
| 385 | + public Selection setElt(String columnName, BindMarker element) { | |
| 386 | + columns.add(new CollectionElementSelector(columnName, element)); | |
| 362 | 387 | return this; |
| 363 | 388 | } |
| 364 | 389 | |
| @@ -370,7 +395,7 @@ | ||
| 370 | 395 | * @return this in-build DELETE Selection |
| 371 | 396 | */ |
| 372 | 397 | public Selection mapElt(String columnName, Object key) { |
| 373 | - columns.add(new IndexedSelector(columnName, key)); | |
| 398 | + columns.add(new CollectionElementSelector(columnName, key)); | |
| 374 | 399 | return this; |
| 375 | 400 | } |
| 376 | 401 | } |
| @@ -378,7 +403,7 @@ | ||
| 378 | 403 | /** |
| 379 | 404 | * A selector in a DELETE selection clause. |
| 380 | 405 | * A selector can be either a column name, |
| 381 | - * a list element or a map entry. | |
| 406 | + * a list element, a set element or a map entry. | |
| 382 | 407 | */ |
| 383 | 408 | static class Selector extends Utils.Appendeable { |
| 384 | 409 | |
| @@ -407,13 +432,13 @@ | ||
| 407 | 432 | } |
| 408 | 433 | |
| 409 | 434 | /** |
| 410 | - * A list element or a map entry in a DELETE selection clause. | |
| 435 | + * A selector representing a list index, a set element or a map key in a DELETE selection clause. | |
| 411 | 436 | */ |
| 412 | - static class IndexedSelector extends Selector { | |
| 437 | + static class CollectionElementSelector extends Selector { | |
| 413 | 438 | |
| 414 | 439 | private final Object key; |
| 415 | 440 | |
| 416 | - IndexedSelector(String columnName, Object key) { | |
| 441 | + CollectionElementSelector(String columnName, Object key) { | |
| 417 | 442 | super(columnName); |
| 418 | 443 | this.key = key; |
| 419 | 444 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +28 −3
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | return Arrays.asList( |
| 39 | 39 | String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), |
| 40 | 40 | "CREATE TABLE dateTest (t timestamp PRIMARY KEY)", |
| 41 | - "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>)"); | |
| 41 | + "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>, c set<text>)"); | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | @Test(groups = "short") |
| @@ -120,7 +120,7 @@ | ||
| 120 | 120 | @Test(groups = "short") |
| 121 | 121 | public void should_delete_list_element_with_bind_marker() throws Exception { |
| 122 | 122 | //given |
| 123 | - session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); | |
| 123 | + session.execute("INSERT INTO test_coll (k, a) VALUES (1, [1,2,3])"); | |
| 124 | 124 | //when |
| 125 | 125 | BuiltStatement statement = delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1)); |
| 126 | 126 | PreparedStatement ps = session.prepare(statement); |
| @@ -130,10 +130,35 @@ | ||
| 130 | 130 | assertThat(actual).containsExactly(1, 3); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | + @Test(groups = "short") | |
| 134 | + public void should_delete_set_element() throws Exception { | |
| 135 | + //given | |
| 136 | + session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 137 | + //when | |
| 138 | + BuiltStatement statement = delete().setElt("c", "foo").from("test_coll").where(eq("k", 1)); | |
| 139 | + session.execute(statement); | |
| 140 | + //then | |
| 141 | + Set<String> actual = session.execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 142 | + assertThat(actual).containsOnly("bar", "qix"); | |
| 143 | + } | |
| 144 | + | |
| 145 | + @Test(groups = "short") | |
| 146 | + public void should_delete_set_element_with_bind_marker() throws Exception { | |
| 147 | + //given | |
| 148 | + session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 149 | + //when | |
| 150 | + BuiltStatement statement = delete().setElt("c", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 151 | + PreparedStatement ps = session.prepare(statement); | |
| 152 | + session.execute(ps.bind("foo")); | |
| 153 | + //then | |
| 154 | + Set<String> actual = session.execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 155 | + assertThat(actual).containsOnly("bar", "qix"); | |
| 156 | + } | |
| 157 | + | |
| 133 | 158 | @Test(groups = "short") |
| 134 | 159 | public void should_delete_map_entry() throws Exception { |
| 135 | 160 | //given |
| 136 | - session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); | |
| 161 | + session.execute("INSERT INTO test_coll (k, b) VALUES (1, {1:'foo', 2:'bar'})"); | |
| 137 | 162 | //when |
| 138 | 163 | BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); |
| 139 | 164 | session.execute(statement); |