kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 85d8166508e4baacd1e77f94d2a4daffd6a396d5b07e7d66b10636f839a64ff2 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-06-11 15:37 +02:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-11 15:38 +02:00 |
| Parents | 4c6dd1e29c05, 03c6ab42daf5 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +22 −9
| @@ -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. |
| @@ -592,12 +593,16 @@ | ||
| 592 | 593 | * This will generate: {@code name = [ value ] + name}. |
| 593 | 594 | * |
| 594 | 595 | * @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. | |
| 596 | 599 | * @return the correspond assignment (to use in an update query) |
| 597 | 600 | */ |
| 598 | 601 | 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)); | |
| 601 | 606 | } |
| 602 | 607 | |
| 603 | 608 | /** |
| @@ -632,12 +637,16 @@ | ||
| 632 | 637 | * This will generate: {@code name = name + [value]}. |
| 633 | 638 | * |
| 634 | 639 | * @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. | |
| 636 | 643 | * @return the correspond assignment (to use in an update query) |
| 637 | 644 | */ |
| 638 | 645 | 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); | |
| 641 | 650 | } |
| 642 | 651 | |
| 643 | 652 | /** |
| @@ -726,12 +735,16 @@ | ||
| 726 | 735 | * This will generate: {@code name = name + {value}}. |
| 727 | 736 | * |
| 728 | 737 | * @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. | |
| 730 | 741 | * @return the correspond assignment (to use in an update query) |
| 731 | 742 | */ |
| 732 | 743 | 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); | |
| 735 | 748 | } |
| 736 | 749 | |
| 737 | 750 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +22 −0
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | import static org.testng.Assert.fail; |
| 31 | 31 | |
| 32 | 32 | import com.datastax.driver.core.*; |
| 33 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 33 | 34 | import com.datastax.driver.core.utils.Bytes; |
| 34 | 35 | import com.datastax.driver.core.utils.CassandraVersion; |
| 35 | 36 | |
| @@ -802,6 +803,27 @@ | ||
| 802 | 803 | assertThat(statement.toString()).isEqualTo(query); |
| 803 | 804 | } |
| 804 | 805 | |
| 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 | + | |
| 805 | 827 | @Test(groups = "unit") |
| 806 | 828 | public void should_quote_complex_column_names() { |
| 807 | 829 | // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name |