Elffuss BraidHub

kikocis/java-driver public

Merge pull request #382 from datastax/java736-2

JAVA-736 Forbid bind marker in QueryBuilder remove/discard.
Changeset5ec20e8005812750eee672779a9415d4186987e424aeabdd57fe7f572d45e6c2
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 14:32 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 14:32 +02:00
Parents7d0b6aef376e, d8a72f06d942

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +18 −12
@@ -594,7 +594,7 @@
594594 *
595595 * @param name the column name (must be of type list).
596596 * @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
598598 * singleton list.
599599 * @return the correspond assignment (to use in an update query)
600600 */
@@ -602,7 +602,7 @@
602602 if (value instanceof BindMarker) {
603603 throw new InvalidQueryException("binding a value in prepend() is not supported, use prependAll() and bind a singleton list");
604604 }
605- return new Assignment.ListPrependAssignment(name, Collections.singletonList(value));
605+ return prependAll(name, Collections.singletonList(value));
606606 }
607607
608608 /**
@@ -638,7 +638,7 @@
638638 *
639639 * @param name the column name (must be of type list).
640640 * @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
642642 * singleton list.
643643 * @return the correspond assignment (to use in an update query)
644644 */
@@ -646,7 +646,7 @@
646646 if (value instanceof BindMarker) {
647647 throw new InvalidQueryException("Binding a value in append() is not supported, use appendAll() and bind a singleton list");
648648 }
649- return new Assignment.CollectionAssignment(name, Collections.singletonList(value), true, false);
649+ return appendAll(name, Collections.singletonList(value));
650650 }
651651
652652 /**
@@ -681,12 +681,15 @@
681681 * This will generate: {@code name = name - [value]}.
682682 *
683683 * @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.
685686 * @return the correspond assignment (to use in an update query)
686687 */
687688 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));
690693 }
691694
692695 /**
@@ -737,14 +740,14 @@
737740 * @param name the column name (must be of type set).
738741 * @param value the value to add. Using a BindMarker here is not supported.
739742 * To use a BindMarker use {@code QueryBuilder#addAll} with a
740- * singleton list.
743+ * singleton set.
741744 * @return the correspond assignment (to use in an update query)
742745 */
743746 public static Assignment add(String name, Object value) {
744747 if (value instanceof BindMarker){
745748 throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list");
746749 }
747- return new Assignment.CollectionAssignment(name, Collections.singleton(value), true);
750+ return addAll(name, Collections.singleton(value));
748751 }
749752
750753 /**
@@ -779,12 +782,15 @@
779782 * This will generate: {@code name = name - {value}}.
780783 *
781784 * @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.
783787 * @return the correspond assignment (to use in an update query)
784788 */
785789 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));
788794 }
789795
790796 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +23 −18
@@ -815,25 +815,30 @@
815815 assertThat(statement.toString()).isEqualTo(query);
816816 }
817817
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+ }
836823
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));
837842 }
838843
839844 @Test(groups = "unit")