Elffuss BraidHub

kikocis/java-driver public

JAVA-736 Forbid bind marker in QueryBuilder remove/discard.
Changesetd8a72f06d9424f2e6e056885726062aa7b5d664778ad3c968f9e9864401d9249
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-18 18:21 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-18 18:31 -05:00
Parentsa752ad38d063

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
@@ -803,25 +803,30 @@
803803 assertThat(statement.toString()).isEqualTo(query);
804804 }
805805
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+ }
824811
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));
825830 }
826831
827832 @Test(groups = "unit")