kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 437ad5b67816ce0a3e3505637a158ddeaa08f91b3a813d690aabe2b5b7b8c52b |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-11-12 18:58 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-11-12 19:19 +01:00 |
| Parents | 22c9a3c32b30 |
3 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +4 −0
| @@ -82,6 +82,10 @@ | ||
| 82 | 82 | } else { |
| 83 | 83 | List<ByteBuffer> l = new ArrayList<ByteBuffer>(); |
| 84 | 84 | sb = buildQueryString(l); |
| 85 | + | |
| 86 | + if (l.size() > 65535) | |
| 87 | + throw new IllegalArgumentException("Too many values for built statement, the maximum allowed is 65535"); | |
| 88 | + | |
| 85 | 89 | if (!l.isEmpty()) |
| 86 | 90 | values = l.toArray(new ByteBuffer[l.size()]); |
| 87 | 91 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +2 −0
| @@ -74,6 +74,8 @@ | ||
| 74 | 74 | |
| 75 | 75 | if (values == null) |
| 76 | 76 | throw new IllegalArgumentException("Missing values for IN clause"); |
| 77 | + if (values.size() > 65535) | |
| 78 | + throw new IllegalArgumentException("Too many values for IN clause, the maximum allowed is 65535"); | |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | @Override |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +21 −3
| @@ -18,9 +18,7 @@ | ||
| 18 | 18 | import java.math.BigDecimal; |
| 19 | 19 | import java.math.BigInteger; |
| 20 | 20 | import java.net.InetAddress; |
| 21 | -import java.util.Arrays; | |
| 22 | -import java.util.TreeMap; | |
| 23 | -import java.util.TreeSet; | |
| 21 | +import java.util.*; | |
| 24 | 22 | |
| 25 | 23 | import org.testng.annotations.Test; |
| 26 | 24 | import static org.testng.Assert.*; |
| @@ -680,4 +678,24 @@ | ||
| 680 | 678 | select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); |
| 681 | 679 | assertEquals(select.toString(), query); |
| 682 | 680 | } |
| 681 | + | |
| 682 | + @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) | |
| 683 | + public void should_fail_if_in_clause_has_too_many_values() { | |
| 684 | + List<Object> values = Collections.<Object>nCopies(65536, "a"); | |
| 685 | + select().all().from("foo").where(in("bar", values.toArray())); | |
| 686 | + } | |
| 687 | + | |
| 688 | + @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) | |
| 689 | + public void should_fail_if_built_statement_has_too_many_values() { | |
| 690 | + List<Object> values = Collections.<Object>nCopies(65535, "a"); | |
| 691 | + | |
| 692 | + // If the excessive count results from successive DSL calls, we don't check it on the fly so this statement works: | |
| 693 | + BuiltStatement statement = select().all().from("foo") | |
| 694 | + .where(eq("bar", "a")) | |
| 695 | + .and(in("baz", values.toArray())); | |
| 696 | + | |
| 697 | + // But we still want to check it client-side, to fail fast instead of sending a bad query to Cassandra. | |
| 698 | + // getValues() is called on any RegularStatement before we send it (see SessionManager.makeRequestMessage). | |
| 699 | + statement.getValues(); | |
| 700 | + } | |
| 683 | 701 | } |