Elffuss BraidHub

kikocis/java-driver public

JAVA-736 : use bindMarker into a QueryBuilder.add() method
Changeset39b978b2db6d95e16e0e9a90cefdf977181993ffe9357daa02c02dd3b10f822c
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-29 17:47 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-09 14:09 +02:00
Parents69d85ad116db

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.
@@ -596,12 +597,16 @@
596597 * This will generate: {@code name = [ value ] + name}.
597598 *
598599 * @param name the column name (must be of type list).
599- * @param value the value to prepend
600+ * @param value the value to prepend. Using a BindMarker here is not supported.
601+ * To use a BindMarker use {@code QueryBuilder#addAll} with a
602+ * singleton list.
600603 * @return the correspond assignment (to use in an update query)
601604 */
602605 public static Assignment prepend(String name, Object value) {
603- Object v = value instanceof BindMarker ? value : Collections.singletonList(value);
604- return new Assignment.ListPrependAssignment(name, v);
606+ if (value instanceof BindMarker) {
607+ throw new InvalidQueryException("binding a value in prepend() is not supported, use prependAll() and bind a singleton list");
608+ }
609+ return new Assignment.ListPrependAssignment(name, Collections.singletonList(value));
605610 }
606611
607612 /**
@@ -636,12 +641,16 @@
636641 * This will generate: {@code name = name + [value]}.
637642 *
638643 * @param name the column name (must be of type list).
639- * @param value the value to append
644+ * @param value the value to append. Using a BindMarker here is not supported.
645+ * To use a BindMarker use {@code QueryBuilder#addAll} with a
646+ * singleton list.
640647 * @return the correspond assignment (to use in an update query)
641648 */
642649 public static Assignment append(String name, Object value) {
643- Object v = value instanceof BindMarker ? value : Collections.singletonList(value);
644- return new Assignment.CollectionAssignment(name, v, true, false);
650+ if (value instanceof BindMarker) {
651+ throw new InvalidQueryException("Binding a value in append() is not supported, use appendAll() and bind a singleton list");
652+ }
653+ return new Assignment.CollectionAssignment(name, Collections.singletonList(value), true, false);
645654 }
646655
647656 /**
@@ -730,12 +739,16 @@
730739 * This will generate: {@code name = name + {value}}.
731740 *
732741 * @param name the column name (must be of type set).
733- * @param value the value to add
742+ * @param value the value to add. Using a BindMarker here is not supported.
743+ * To use a BindMarker use {@code QueryBuilder#addAll} with a
744+ * singleton list.
734745 * @return the correspond assignment (to use in an update query)
735746 */
736747 public static Assignment add(String name, Object value) {
737- Object v = value instanceof BindMarker ? value : Collections.singleton(value);
738- return new Assignment.CollectionAssignment(name, v, true);
748+ if (value instanceof BindMarker){
749+ throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list");
750+ }
751+ return new Assignment.CollectionAssignment(name, Collections.singleton(value), true);
739752 }
740753
741754 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +24 −2
@@ -25,10 +25,12 @@
2525 import org.testng.annotations.Test;
2626
2727 import static org.assertj.core.api.Assertions.assertThat;
28-import static org.testng.Assert.*;
28+import static org.testng.Assert.assertEquals;
29+import static org.testng.Assert.assertTrue;
30+import static org.testng.Assert.fail;
2931
3032 import com.datastax.driver.core.*;
31-import com.datastax.driver.core.querybuilder.Delete.Where;
33+import com.datastax.driver.core.exceptions.InvalidQueryException;
3234 import com.datastax.driver.core.utils.Bytes;
3335 import com.datastax.driver.core.utils.CassandraVersion;
3436
@@ -769,4 +771,24 @@
769771 statement = update("foo").with(prependAll("l", list)).where(eq("k", 1));
770772 assertThat(statement.toString()).isEqualTo(query);
771773 }
774+
775+ @Test(groups = "unit")
776+ public void should_not_allow_bind_marker_as_added_collection_item() {
777+ try {
778+ // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra
779+ // (individual values in collections can't be bound)
780+ update("foo").with(add("s", bindMarker())).where(eq("k", 1));
781+ fail("Expected an exception");
782+ } catch (InvalidQueryException e) { /*expected*/ }
783+
784+ // Same for list append/prepend
785+ try {
786+ update("foo").with(prepend("l", bindMarker())).where(eq("k", 1));
787+ fail("Expected an exception");
788+ } catch (InvalidQueryException e) { /*expected*/ }
789+ try {
790+ update("foo").with(append("l", bindMarker())).where(eq("k", 1));
791+ fail("Expected an exception");
792+ } catch (InvalidQueryException e) { /*expected*/ }
793+ }
772794 }