kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | d8a72f06d9424f2e6e056885726062aa7b5d664778ad3c968f9e9864401d9249 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-18 18:21 -05:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-18 18:31 -05:00 |
| Parents | a752ad38d063 |
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
| @@ -803,25 +803,30 @@ | ||
| 803 | 803 | assertThat(statement.toString()).isEqualTo(query); |
| 804 | 804 | } |
| 805 | 805 | |
| 806 | - @Test(groups = "unit") | |
| 807 | - public void should_not_allow_bind_marker_as_added_collection_item() { | |
| 808 | - try { | |
| 809 | - // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra | |
| 810 | - // (individual values in collections can't be bound) | |
| 811 | - update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 812 | - fail("Expected an exception"); | |
| 813 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 814 | - | |
| 815 | - // Same for list append/prepend | |
| 816 | - try { | |
| 817 | - update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 818 | - fail("Expected an exception"); | |
| 819 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 820 | - try { | |
| 821 | - update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 822 | - fail("Expected an exception"); | |
| 823 | - } catch (InvalidQueryException e) { /*expected*/ } | |
| 806 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 807 | + public void should_not_allow_bind_marker_for_add() { | |
| 808 | + // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra | |
| 809 | + update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 810 | + } | |
| 824 | 811 | |
| 812 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 813 | + public void should_now_allow_bind_marker_for_prepend() { | |
| 814 | + update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 815 | + } | |
| 816 | + | |
| 817 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 818 | + public void should_not_allow_bind_marker_for_append() { | |
| 819 | + update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 820 | + } | |
| 821 | + | |
| 822 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 823 | + public void should_not_allow_bind_marker_for_remove() { | |
| 824 | + update("foo").with(remove("s", bindMarker())).where(eq("k", 1)); | |
| 825 | + } | |
| 826 | + | |
| 827 | + @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) | |
| 828 | + public void should_not_allow_bind_marker_for_discard() { | |
| 829 | + update("foo").with(discard("l", bindMarker())).where(eq("k", 1)); | |
| 825 | 830 | } |
| 826 | 831 | |
| 827 | 832 | @Test(groups = "unit") |