Elffuss BraidHub

kikocis/java-driver public

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

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -7,6 +7,7 @@
77 - [improvement] Allow DNS name with multiple A-records as contact point (#340)
88 - [bug] Allow tracing across multiple result pages (JAVA-794)
99 - [bug] DowngradingConsistencyRetryPolicy ignores write timeouts (JAVA-737)
10+- [bug] Forbid bind marker in QueryBuilder add/append/prepend (JAVA-736)
1011
1112 Merged from 2.0.10_fixes branch:
1213
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +34 −15
@@ -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#prependAll} 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 prependAll(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#appendAll} 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 appendAll(name, Collections.singletonList(value));
645654 }
646655
647656 /**
@@ -676,12 +685,15 @@
676685 * This will generate: {@code name = name - [value]}.
677686 *
678687 * @param name the column name (must be of type list).
679- * @param value the value to discard
688+ * @param value the value to discard. Using a BindMarker here is not supported.
689+ * To use a BindMarker use {@code QueryBuilder#discardAll} with a singleton list.
680690 * @return the correspond assignment (to use in an update query)
681691 */
682692 public static Assignment discard(String name, Object value) {
683- Object v = value instanceof BindMarker ? value : Collections.singletonList(value);
684- return new Assignment.CollectionAssignment(name, v, false);
693+ if (value instanceof BindMarker) {
694+ throw new InvalidQueryException("Binding a value in discard() is not supported, use discardAll() and bind a singleton list");
695+ }
696+ return discardAll(name, Collections.singletonList(value));
685697 }
686698
687699 /**
@@ -730,12 +742,16 @@
730742 * This will generate: {@code name = name + {value}}.
731743 *
732744 * @param name the column name (must be of type set).
733- * @param value the value to add
745+ * @param value the value to add. Using a BindMarker here is not supported.
746+ * To use a BindMarker use {@code QueryBuilder#addAll} with a
747+ * singleton set.
734748 * @return the correspond assignment (to use in an update query)
735749 */
736750 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);
751+ if (value instanceof BindMarker){
752+ throw new InvalidQueryException("Binding a value in add() is not supported, use addAll() and bind a singleton list");
753+ }
754+ return addAll(name, Collections.singleton(value));
739755 }
740756
741757 /**
@@ -770,12 +786,15 @@
770786 * This will generate: {@code name = name - {value}}.
771787 *
772788 * @param name the column name (must be of type set).
773- * @param value the value to remove
789+ * @param value the value to remove. Using a BindMarker here is not supported.
790+ * To use a BindMarker use {@code QueryBuilder#removeAll} with a singleton set.
774791 * @return the correspond assignment (to use in an update query)
775792 */
776793 public static Assignment remove(String name, Object value) {
777- Object v = value instanceof BindMarker ? value : Collections.singleton(value);
778- return new Assignment.CollectionAssignment(name, v, false);
794+ if (value instanceof BindMarker) {
795+ throw new InvalidQueryException("Binding a value in remove() is not supported, use removeAll() and bind a singleton set");
796+ }
797+ return removeAll(name, Collections.singleton(value));
779798 }
780799
781800 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +72 −7
@@ -21,10 +21,18 @@
2121 import java.util.*;
2222
2323 import org.testng.annotations.Test;
24-import static org.testng.Assert.*;
2524
25+import static org.assertj.core.api.Assertions.assertThat;
26+import static org.testng.Assert.assertEquals;
27+import static org.testng.Assert.assertTrue;
28+import static org.testng.Assert.fail;
29+
30+import com.datastax.driver.core.Assertions;
2631 import com.datastax.driver.core.ConsistencyLevel;
32+import com.datastax.driver.core.RegularStatement;
2733 import com.datastax.driver.core.Statement;
34+import com.datastax.driver.core.exceptions.InvalidQueryException;
35+
2836 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
2937
3038 public class QueryBuilderTest {
@@ -151,7 +159,10 @@
151159 .value("a", 123)
152160 .value("b", InetAddress.getByName("127.0.0.1"))
153161 .value(quote("C"), "foo'bar")
154- .value("d", new TreeMap<String, Integer>(){{ put("x", 3); put("y", 2); }})
162+ .value("d", new TreeMap<String, Integer>() {{
163+ put("x", 3);
164+ put("y", 2);
165+ }})
155166 .using(timestamp(42)).and(ttl(24));
156167 assertEquals(insert.toString(), query);
157168
@@ -162,12 +173,20 @@
162173 assertEquals(insert.toString(), query);
163174
164175 query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;";
165- insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }).using(ttl(24)).and(timestamp(42));
176+ insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
177+ add(2);
178+ add(3);
179+ add(4);
180+ }}, 3.4 }).using(ttl(24)).and(timestamp(42));
166181 assertEquals(insert.toString(), query);
167182
168183 query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;";
169184 insert = insertInto("foo", "bar")
170- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
185+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
186+ add(2);
187+ add(3);
188+ add(4);
189+ }}, 3.4 })
171190 .using(ttl(bindMarker()))
172191 .and(timestamp(bindMarker()));
173192 assertEquals(insert.toString(), query);
@@ -176,7 +195,11 @@
176195 query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;";
177196 insert = insertInto("foo", "bar")
178197 .using(timestamp(42))
179- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
198+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
199+ add(2);
200+ add(3);
201+ add(4);
202+ }}, 3.4 })
180203 .value("c", 123);
181204 assertEquals(insert.toString(), query);
182205
@@ -313,11 +336,11 @@
313336 } catch (IllegalArgumentException e) {
314337 assertEquals(e.getMessage(), "Invalid timestamp, must be positive");
315338 }
316-
339+
317340 query = "DELETE FROM foo.bar WHERE k1='foo' IF EXISTS;";
318341 delete = delete().from("foo", "bar").where(eq("k1", "foo")).ifExists();
319342 assertEquals(delete.toString(), query);
320-
343+
321344 query = "DELETE FROM foo.bar WHERE k1='foo' IF a=1 AND b=2;";
322345 delete = delete().from("foo", "bar").where(eq("k1", "foo")).onlyIf(eq("a", 1)).and(eq("b", 2));
323346 assertEquals(delete.toString(), query);
@@ -698,4 +721,46 @@
698721 // getValues() is called on any RegularStatement before we send it (see SessionManager.makeRequestMessage).
699722 statement.getValues();
700723 }
724+
725+ @Test(groups = "unit", expectedExceptions = InvalidQueryException.class)
726+ public void should_not_allow_bind_marker_for_add() {
727+ // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra
728+ update("foo").with(add("s", bindMarker())).where(eq("k", 1));
729+ }
730+
731+ @Test(groups = "unit", expectedExceptions = InvalidQueryException.class)
732+ public void should_now_allow_bind_marker_for_prepend() {
733+ update("foo").with(prepend("l", bindMarker())).where(eq("k", 1));
734+ }
735+
736+ @Test(groups = "unit", expectedExceptions = InvalidQueryException.class)
737+ public void should_not_allow_bind_marker_for_append() {
738+ update("foo").with(append("l", bindMarker())).where(eq("k", 1));
739+ }
740+
741+ @Test(groups = "unit", expectedExceptions = InvalidQueryException.class)
742+ public void should_not_allow_bind_marker_for_remove() {
743+ update("foo").with(remove("s", bindMarker())).where(eq("k", 1));
744+ }
745+
746+ @Test(groups = "unit", expectedExceptions = InvalidQueryException.class)
747+ public void should_not_allow_bind_marker_for_discard() {
748+ update("foo").with(discard("l", bindMarker())).where(eq("k", 1));
749+ }
750+
751+ @Test(groups = "unit")
752+ public void should_quote_complex_column_names() {
753+ // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name
754+ String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;";
755+ Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1));
756+
757+ assertThat(statement.toString()).isEqualTo(query);
758+ }
759+
760+ @Test(groups = "unit")
761+ public void should_not_serialize_raw_query_values() {
762+ RegularStatement select = select().from("test").where(gt("i", raw("1")));
763+ assertThat(select.getQueryString()).doesNotContain("?");
764+ assertThat(select.getValues()).isNull();
765+ }
701766 }