kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5ec20e8005812750eee672779a9415d4186987e424aeabdd57fe7f572d45e6c2 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 14:32 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 14:32 +02:00 |
| Parents | 7d0b6aef376e, d8a72f06d942 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +18 −12
| @@ -594,7 +594,7 @@ | ||
| 594 | 594 | * |
| 595 | 595 | * @param name the column name (must be of type list). |
| 596 | 596 | * @param value the value to prepend. Using a BindMarker here is not supported. |
| 597 | - * To use a BindMarker use {@code QueryBuilder#addAll} with a | |
| 597 | + * To use a BindMarker use {@code QueryBuilder#prependAll} with a | |
| 598 | 598 | * singleton list. |
| 599 | 599 | * @return the correspond assignment (to use in an update query) |
| 600 | 600 | */ |
| @@ -602,7 +602,7 @@ | ||
| 602 | 602 | if (value instanceof BindMarker) { |
| 603 | 603 | throw new InvalidQueryException("binding a value in prepend() is not supported, use prependAll() and bind a singleton list"); |
| 604 | 604 | } |
| 605 | - return new Assignment.ListPrependAssignment(name, Collections.singletonList(value)); | |
| 605 | + return prependAll(name, Collections.singletonList(value)); | |
| 606 | 606 | } |
| 607 | 607 | |
| 608 | 608 | /** |
| @@ -638,7 +638,7 @@ | ||
| 638 | 638 | * |
| 639 | 639 | * @param name the column name (must be of type list). |
| 640 | 640 | * @param value the value to append. Using a BindMarker here is not supported. |
| 641 | - * To use a BindMarker use {@code QueryBuilder#addAll} with a | |
| 641 | + * To use a BindMarker use {@code QueryBuilder#appendAll} with a | |
| 642 | 642 | * singleton list. |
| 643 | 643 | * @return the correspond assignment (to use in an update query) |
| 644 | 644 | */ |
| @@ -646,7 +646,7 @@ | ||
| 646 | 646 | if (value instanceof BindMarker) { |
| 647 | 647 | throw new InvalidQueryException("Binding a value in append() is not supported, use appendAll() and bind a singleton list"); |
| 648 | 648 | } |
| 649 | - return new Assignment.CollectionAssignment(name, Collections.singletonList(value), true, false); | |
| 649 | + return appendAll(name, Collections.singletonList(value)); | |
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | /** |
| @@ -681,12 +681,15 @@ | ||
| 681 | 681 | * This will generate: {@code name = name - [value]}. |
| 682 | 682 | * |
| 683 | 683 | * @param name the column name (must be of type list). |
| 684 | - * @param value the value to discard | |
| 684 | + * @param value the value to discard. Using a BindMarker here is not supported. | |
| 685 | + * To use a BindMarker use {@code QueryBuilder#discardAll} with a singleton list. | |
| 685 | 686 | * @return the correspond assignment (to use in an update query) |
| 686 | 687 | */ |
| 687 | 688 | public static Assignment discard(String name, Object value) { |
| 688 | - Object v = value instanceof BindMarker ? value : Collections.singletonList(value); | |
| 689 | - return new Assignment.CollectionAssignment(name, v, false); | |
| 689 | + if (value instanceof BindMarker) { | |
| 690 | + throw new InvalidQueryException("Binding a value in discard() is not supported, use discardAll() and bind a singleton list"); | |
| 691 | + } | |
| 692 | + return discardAll(name, Collections.singletonList(value)); | |
| 690 | 693 | } |
| 691 | 694 | |
| 692 | 695 | /** |
| @@ -737,14 +740,14 @@ | ||
| 737 | 740 | * @param name the column name (must be of type set). |
| 738 | 741 | * @param value the value to add. Using a BindMarker here is not supported. |
| 739 | 742 | * To use a BindMarker use {@code QueryBuilder#addAll} with a |
| 740 | - * singleton list. | |
| 743 | + * singleton set. | |
| 741 | 744 | * @return the correspond assignment (to use in an update query) |
| 742 | 745 | */ |
| 743 | 746 | public static Assignment add(String name, Object value) { |
| 744 | 747 | if (value instanceof BindMarker){ |
| 745 | 748 | throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list"); |
| 746 | 749 | } |
| 747 | - return new Assignment.CollectionAssignment(name, Collections.singleton(value), true); | |
| 750 | + return addAll(name, Collections.singleton(value)); | |
| 748 | 751 | } |
| 749 | 752 | |
| 750 | 753 | /** |
| @@ -779,12 +782,15 @@ | ||
| 779 | 782 | * This will generate: {@code name = name - {value}}. |
| 780 | 783 | * |
| 781 | 784 | * @param name the column name (must be of type set). |
| 782 | - * @param value the value to remove | |
| 785 | + * @param value the value to remove. Using a BindMarker here is not supported. | |
| 786 | + * To use a BindMarker use {@code QueryBuilder#removeAll} with a singleton set. | |
| 783 | 787 | * @return the correspond assignment (to use in an update query) |
| 784 | 788 | */ |
| 785 | 789 | public static Assignment remove(String name, Object value) { |
| 786 | - Object v = value instanceof BindMarker ? value : Collections.singleton(value); | |
| 787 | - return new Assignment.CollectionAssignment(name, v, false); | |
| 790 | + if (value instanceof BindMarker) { | |
| 791 | + throw new InvalidQueryException("Binding a value in remove() is not supported, use removeAll() and bind a singleton set"); | |
| 792 | + } | |
| 793 | + return removeAll(name, Collections.singleton(value)); | |
| 788 | 794 | } |
| 789 | 795 | |
| 790 | 796 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +23 −18
| @@ -815,25 +815,30 @@ | ||
| 815 | 815 | assertThat(statement.toString()).isEqualTo(query); |
| 816 | 816 | } |
| 817 | 817 | |
| 818 | - @Test(groups = "unit") | |
| 819 | - public void should_not_allow_bind_marker_as_added_collection_item() { | |
| 820 | - try { | |
| 821 | - // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra | |
| 822 | - // (individual values in collections can't be bound) | |
| 823 | - update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 824 | - fail("Expected an exception"); | |
| 825 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 826 | - | |
| 827 | - // Same for list append/prepend | |
| 828 | - try { | |
| 829 | - update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 830 | - fail("Expected an exception"); | |
| 831 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 832 | - try { | |
| 833 | - update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 834 | - fail("Expected an exception"); | |
| 835 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 818 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 819 | + public void should_not_allow_bind_marker_for_add() { | |
| 820 | + // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra | |
| 821 | + update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 822 | + } | |
| 836 | 823 | |
| 824 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 825 | + public void should_now_allow_bind_marker_for_prepend() { | |
| 826 | + update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 827 | + } | |
| 828 | + | |
| 829 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 830 | + public void should_not_allow_bind_marker_for_append() { | |
| 831 | + update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 832 | + } | |
| 833 | + | |
| 834 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 835 | + public void should_not_allow_bind_marker_for_remove() { | |
| 836 | + update("foo").with(remove("s", bindMarker())).where(eq("k", 1)); | |
| 837 | + } | |
| 838 | + | |
| 839 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 840 | + public void should_not_allow_bind_marker_for_discard() { | |
| 841 | + update("foo").with(discard("l", bindMarker())).where(eq("k", 1)); | |
| 837 | 842 | } |
| 838 | 843 | |
| 839 | 844 | @Test(groups = "unit") |