kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2fc9216ae39db165f510501d96720a9e3cac62c54207d61c9f1de150a90cbe39 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-29 17:47 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-22 15:55 +02:00 |
| Parents | 6ad6548d975c |
3 files changed
modified changelog/README.md +1 −0
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | - [improvement] Allow DNS name with multiple A-records as contact point (#340) |
| 8 | 8 | - [bug] Allow tracing across multiple result pages (JAVA-794) |
| 9 | 9 | - [bug] DowngradingConsistencyRetryPolicy ignores write timeouts (JAVA-737) |
| 10 | +- [bug] Forbid bind marker in QueryBuilder add/append/prepend (JAVA-736) | |
| 10 | 11 | |
| 11 | 12 | Merged from 2.0.10_fixes branch: |
| 12 | 13 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +34 −15
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | import com.datastax.driver.core.RegularStatement; |
| 21 | 21 | import com.datastax.driver.core.TableMetadata; |
| 22 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * Static methods to build a CQL3 query. |
| @@ -596,12 +597,16 @@ | ||
| 596 | 597 | * This will generate: {@code name = [ value ] + name}. |
| 597 | 598 | * |
| 598 | 599 | * @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. | |
| 600 | 603 | * @return the correspond assignment (to use in an update query) |
| 601 | 604 | */ |
| 602 | 605 | 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)); | |
| 605 | 610 | } |
| 606 | 611 | |
| 607 | 612 | /** |
| @@ -636,12 +641,16 @@ | ||
| 636 | 641 | * This will generate: {@code name = name + [value]}. |
| 637 | 642 | * |
| 638 | 643 | * @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. | |
| 640 | 647 | * @return the correspond assignment (to use in an update query) |
| 641 | 648 | */ |
| 642 | 649 | 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)); | |
| 645 | 654 | } |
| 646 | 655 | |
| 647 | 656 | /** |
| @@ -676,12 +685,15 @@ | ||
| 676 | 685 | * This will generate: {@code name = name - [value]}. |
| 677 | 686 | * |
| 678 | 687 | * @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. | |
| 680 | 690 | * @return the correspond assignment (to use in an update query) |
| 681 | 691 | */ |
| 682 | 692 | 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)); | |
| 685 | 697 | } |
| 686 | 698 | |
| 687 | 699 | /** |
| @@ -730,12 +742,16 @@ | ||
| 730 | 742 | * This will generate: {@code name = name + {value}}. |
| 731 | 743 | * |
| 732 | 744 | * @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. | |
| 734 | 748 | * @return the correspond assignment (to use in an update query) |
| 735 | 749 | */ |
| 736 | 750 | 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)); | |
| 739 | 755 | } |
| 740 | 756 | |
| 741 | 757 | /** |
| @@ -770,12 +786,15 @@ | ||
| 770 | 786 | * This will generate: {@code name = name - {value}}. |
| 771 | 787 | * |
| 772 | 788 | * @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. | |
| 774 | 791 | * @return the correspond assignment (to use in an update query) |
| 775 | 792 | */ |
| 776 | 793 | 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)); | |
| 779 | 798 | } |
| 780 | 799 | |
| 781 | 800 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +72 −7
| @@ -21,10 +21,18 @@ | ||
| 21 | 21 | import java.util.*; |
| 22 | 22 | |
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | -import static org.testng.Assert.*; | |
| 25 | 24 | |
| 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; | |
| 26 | 31 | import com.datastax.driver.core.ConsistencyLevel; |
| 32 | +import com.datastax.driver.core.RegularStatement; | |
| 27 | 33 | import com.datastax.driver.core.Statement; |
| 34 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 35 | + | |
| 28 | 36 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 29 | 37 | |
| 30 | 38 | public class QueryBuilderTest { |
| @@ -151,7 +159,10 @@ | ||
| 151 | 159 | .value("a", 123) |
| 152 | 160 | .value("b", InetAddress.getByName("127.0.0.1")) |
| 153 | 161 | .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 | + }}) | |
| 155 | 166 | .using(timestamp(42)).and(ttl(24)); |
| 156 | 167 | assertEquals(insert.toString(), query); |
| 157 | 168 | |
| @@ -162,12 +173,20 @@ | ||
| 162 | 173 | assertEquals(insert.toString(), query); |
| 163 | 174 | |
| 164 | 175 | 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)); | |
| 166 | 181 | assertEquals(insert.toString(), query); |
| 167 | 182 | |
| 168 | 183 | query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; |
| 169 | 184 | 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 }) | |
| 171 | 190 | .using(ttl(bindMarker())) |
| 172 | 191 | .and(timestamp(bindMarker())); |
| 173 | 192 | assertEquals(insert.toString(), query); |
| @@ -176,7 +195,11 @@ | ||
| 176 | 195 | query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; |
| 177 | 196 | insert = insertInto("foo", "bar") |
| 178 | 197 | .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 }) | |
| 180 | 203 | .value("c", 123); |
| 181 | 204 | assertEquals(insert.toString(), query); |
| 182 | 205 | |
| @@ -313,11 +336,11 @@ | ||
| 313 | 336 | } catch (IllegalArgumentException e) { |
| 314 | 337 | assertEquals(e.getMessage(), "Invalid timestamp, must be positive"); |
| 315 | 338 | } |
| 316 | - | |
| 339 | + | |
| 317 | 340 | query = "DELETE FROM foo.bar WHERE k1='foo' IF EXISTS;"; |
| 318 | 341 | delete = delete().from("foo", "bar").where(eq("k1", "foo")).ifExists(); |
| 319 | 342 | assertEquals(delete.toString(), query); |
| 320 | - | |
| 343 | + | |
| 321 | 344 | query = "DELETE FROM foo.bar WHERE k1='foo' IF a=1 AND b=2;"; |
| 322 | 345 | delete = delete().from("foo", "bar").where(eq("k1", "foo")).onlyIf(eq("a", 1)).and(eq("b", 2)); |
| 323 | 346 | assertEquals(delete.toString(), query); |
| @@ -698,4 +721,46 @@ | ||
| 698 | 721 | // getValues() is called on any RegularStatement before we send it (see SessionManager.makeRequestMessage). |
| 699 | 722 | statement.getValues(); |
| 700 | 723 | } |
| 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 | + } | |
| 701 | 766 | } |