Elffuss BraidHub

kikocis/java-driver public

JAVA-736 : use bindMarker into a QueryBuilder.add() method
Changeset85d8166508e4baacd1e77f94d2a4daffd6a396d5b07e7d66b10636f839a64ff2
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2015-06-11 15:37 +02:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-11 15:38 +02:00
Parents4c6dd1e29c05, 03c6ab42daf5

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +22 −9
@@ -19,6 +19,7 @@
1919
2020 import com.datastax.driver.core.RegularStatement;
2121 import com.datastax.driver.core.TableMetadata;
22+import com.datastax.driver.core.exceptions.InvalidQueryException;
2223
2324 /**
2425 * Static methods to build a CQL3 query.
@@ -592,12 +593,16 @@
592593 * This will generate: {@code name = [ value ] + name}.
593594 *
594595 * @param name the column name (must be of type list).
595- * @param value the value to prepend
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
598+ * singleton list.
596599 * @return the correspond assignment (to use in an update query)
597600 */
598601 public static Assignment prepend(String name, Object value) {
599- Object v = value instanceof BindMarker ? value : Collections.singletonList(value);
600- return new Assignment.ListPrependAssignment(name, v);
602+ if (value instanceof BindMarker) {
603+ throw new InvalidQueryException("binding a value in prepend() is not supported, use prependAll() and bind a singleton list");
604+ }
605+ return new Assignment.ListPrependAssignment(name, Collections.singletonList(value));
601606 }
602607
603608 /**
@@ -632,12 +637,16 @@
632637 * This will generate: {@code name = name + [value]}.
633638 *
634639 * @param name the column name (must be of type list).
635- * @param value the value to append
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
642+ * singleton list.
636643 * @return the correspond assignment (to use in an update query)
637644 */
638645 public static Assignment append(String name, Object value) {
639- Object v = value instanceof BindMarker ? value : Collections.singletonList(value);
640- return new Assignment.CollectionAssignment(name, v, true, false);
646+ if (value instanceof BindMarker) {
647+ throw new InvalidQueryException("Binding a value in append() is not supported, use appendAll() and bind a singleton list");
648+ }
649+ return new Assignment.CollectionAssignment(name, Collections.singletonList(value), true, false);
641650 }
642651
643652 /**
@@ -726,12 +735,16 @@
726735 * This will generate: {@code name = name + {value}}.
727736 *
728737 * @param name the column name (must be of type set).
729- * @param value the value to add
738+ * @param value the value to add. Using a BindMarker here is not supported.
739+ * To use a BindMarker use {@code QueryBuilder#addAll} with a
740+ * singleton list.
730741 * @return the correspond assignment (to use in an update query)
731742 */
732743 public static Assignment add(String name, Object value) {
733- Object v = value instanceof BindMarker ? value : Collections.singleton(value);
734- return new Assignment.CollectionAssignment(name, v, true);
744+ if (value instanceof BindMarker){
745+ throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list");
746+ }
747+ return new Assignment.CollectionAssignment(name, Collections.singleton(value), true);
735748 }
736749
737750 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +22 −0
@@ -30,6 +30,7 @@
3030 import static org.testng.Assert.fail;
3131
3232 import com.datastax.driver.core.*;
33+import com.datastax.driver.core.exceptions.InvalidQueryException;
3334 import com.datastax.driver.core.utils.Bytes;
3435 import com.datastax.driver.core.utils.CassandraVersion;
3536
@@ -802,6 +803,27 @@
802803 assertThat(statement.toString()).isEqualTo(query);
803804 }
804805
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*/ }
824+
825+ }
826+
805827 @Test(groups = "unit")
806828 public void should_quote_complex_column_names() {
807829 // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name