kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 39b978b2db6d95e16e0e9a90cefdf977181993ffe9357daa02c02dd3b10f822c |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-29 17:47 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-09 14:09 +02:00 |
| Parents | 69d85ad116db |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +22 −9
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | import com.datastax.driver.core.RegularStatement; |
| 21 | 21 | import com.datastax.driver.core.TableMetadata; |
| 22 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * Static methods to build a CQL3 query. |
| @@ -596,12 +597,16 @@ | ||
| 596 | 597 | * This will generate: {@code name = [ value ] + name}. |
| 597 | 598 | * |
| 598 | 599 | * @param name the column name (must be of type list). |
| 599 | - * @param value the value to prepend | |
| 600 | + * @param value the value to prepend. Using a BindMarker here is not supported. | |
| 601 | + * To use a BindMarker use {@code QueryBuilder#addAll} with a | |
| 602 | + * singleton list. | |
| 600 | 603 | * @return the correspond assignment (to use in an update query) |
| 601 | 604 | */ |
| 602 | 605 | public static Assignment prepend(String name, Object value) { |
| 603 | - Object v = value instanceof BindMarker ? value : Collections.singletonList(value); | |
| 604 | - return new Assignment.ListPrependAssignment(name, v); | |
| 606 | + if (value instanceof BindMarker) { | |
| 607 | + throw new InvalidQueryException("binding a value in prepend() is not supported, use prependAll() and bind a singleton list"); | |
| 608 | + } | |
| 609 | + return new Assignment.ListPrependAssignment(name, Collections.singletonList(value)); | |
| 605 | 610 | } |
| 606 | 611 | |
| 607 | 612 | /** |
| @@ -636,12 +641,16 @@ | ||
| 636 | 641 | * This will generate: {@code name = name + [value]}. |
| 637 | 642 | * |
| 638 | 643 | * @param name the column name (must be of type list). |
| 639 | - * @param value the value to append | |
| 644 | + * @param value the value to append. Using a BindMarker here is not supported. | |
| 645 | + * To use a BindMarker use {@code QueryBuilder#addAll} with a | |
| 646 | + * singleton list. | |
| 640 | 647 | * @return the correspond assignment (to use in an update query) |
| 641 | 648 | */ |
| 642 | 649 | public static Assignment append(String name, Object value) { |
| 643 | - Object v = value instanceof BindMarker ? value : Collections.singletonList(value); | |
| 644 | - return new Assignment.CollectionAssignment(name, v, true, false); | |
| 650 | + if (value instanceof BindMarker) { | |
| 651 | + throw new InvalidQueryException("Binding a value in append() is not supported, use appendAll() and bind a singleton list"); | |
| 652 | + } | |
| 653 | + return new Assignment.CollectionAssignment(name, Collections.singletonList(value), true, false); | |
| 645 | 654 | } |
| 646 | 655 | |
| 647 | 656 | /** |
| @@ -730,12 +739,16 @@ | ||
| 730 | 739 | * This will generate: {@code name = name + {value}}. |
| 731 | 740 | * |
| 732 | 741 | * @param name the column name (must be of type set). |
| 733 | - * @param value the value to add | |
| 742 | + * @param value the value to add. Using a BindMarker here is not supported. | |
| 743 | + * To use a BindMarker use {@code QueryBuilder#addAll} with a | |
| 744 | + * singleton list. | |
| 734 | 745 | * @return the correspond assignment (to use in an update query) |
| 735 | 746 | */ |
| 736 | 747 | public static Assignment add(String name, Object value) { |
| 737 | - Object v = value instanceof BindMarker ? value : Collections.singleton(value); | |
| 738 | - return new Assignment.CollectionAssignment(name, v, true); | |
| 748 | + if (value instanceof BindMarker){ | |
| 749 | + throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list"); | |
| 750 | + } | |
| 751 | + return new Assignment.CollectionAssignment(name, Collections.singleton(value), true); | |
| 739 | 752 | } |
| 740 | 753 | |
| 741 | 754 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +24 −2
| @@ -25,10 +25,12 @@ | ||
| 25 | 25 | import org.testng.annotations.Test; |
| 26 | 26 | |
| 27 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 28 | -import static org.testng.Assert.*; | |
| 28 | +import static org.testng.Assert.assertEquals; | |
| 29 | +import static org.testng.Assert.assertTrue; | |
| 30 | +import static org.testng.Assert.fail; | |
| 29 | 31 | |
| 30 | 32 | import com.datastax.driver.core.*; |
| 31 | -import com.datastax.driver.core.querybuilder.Delete.Where; | |
| 33 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 32 | 34 | import com.datastax.driver.core.utils.Bytes; |
| 33 | 35 | import com.datastax.driver.core.utils.CassandraVersion; |
| 34 | 36 | |
| @@ -769,4 +771,24 @@ | ||
| 769 | 771 | statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); |
| 770 | 772 | assertThat(statement.toString()).isEqualTo(query); |
| 771 | 773 | } |
| 774 | + | |
| 775 | + @Test(groups = "unit") | |
| 776 | + public void should_not_allow_bind_marker_as_added_collection_item() { | |
| 777 | + try { | |
| 778 | + // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra | |
| 779 | + // (individual values in collections can't be bound) | |
| 780 | + update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 781 | + fail("Expected an exception"); | |
| 782 | + } catch (InvalidQueryException e) { /*expected*/ } | |
| 783 | + | |
| 784 | + // Same for list append/prepend | |
| 785 | + try { | |
| 786 | + update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 787 | + fail("Expected an exception"); | |
| 788 | + } catch (InvalidQueryException e) { /*expected*/ } | |
| 789 | + try { | |
| 790 | + update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 791 | + fail("Expected an exception"); | |
| 792 | + } catch (InvalidQueryException e) { /*expected*/ } | |
| 793 | + } | |
| 772 | 794 | } |