kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 6f181df35a26b895f6e897e82005ba4a19a0888e9c4f9d6f841fe2c5a8ad70bf |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-04-14 18:23 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-26 14:09 +02:00 |
| Parents | 49e1ebe6c225 |
4 files changed
modified changelog/README.md +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [improvement] JAVA-1109: Document SSLOptions changes in upgrade guide. |
| 11 | 11 | - [improvement] JAVA-1065: Add method to create token from partition key values. |
| 12 | 12 | - [improvement] JAVA-1136: Enable JDK signature check in module driver-extras. |
| 13 | +- [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in. | |
| 13 | 14 | |
| 14 | 15 | Merged from 2.1 branch: |
| 15 | 16 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +59 −0
| @@ -224,4 +224,63 @@ | ||
| 224 | 224 | sb.append(")"); |
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | + | |
| 228 | + static class CompoundInClause extends Clause { | |
| 229 | + private final List<String> names; | |
| 230 | + private final List<List<?>> valueLists; | |
| 231 | + | |
| 232 | + public CompoundInClause(List<String> names, List<List<?>> valueLists) { | |
| 233 | + this.names = names; | |
| 234 | + this.valueLists = valueLists; | |
| 235 | + } | |
| 236 | + | |
| 237 | + @Override | |
| 238 | + String name() { | |
| 239 | + // This is only used for routing key purpose, and so far CompoundClause | |
| 240 | + // are not allowed for the partitionKey anyway | |
| 241 | + return null; | |
| 242 | + } | |
| 243 | + | |
| 244 | + @Override | |
| 245 | + Object firstValue() { | |
| 246 | + // This is only used for routing key purpose, and so far CompoundClause | |
| 247 | + // are not allowed for the partitionKey anyway | |
| 248 | + return null; | |
| 249 | + } | |
| 250 | + | |
| 251 | + @Override | |
| 252 | + boolean containsBindMarker() { | |
| 253 | + for (List<?> values : valueLists) | |
| 254 | + for (Object value : values) | |
| 255 | + if (Utils.containsBindMarker(value)) | |
| 256 | + return true; | |
| 257 | + return false; | |
| 258 | + } | |
| 259 | + | |
| 260 | + @Override | |
| 261 | + void appendTo(StringBuilder sb, List<Object> variables, CodecRegistry codecRegistry) { | |
| 262 | + sb.append("("); | |
| 263 | + for (int i = 0; i < names.size(); i++) { | |
| 264 | + if (i > 0) | |
| 265 | + sb.append(","); | |
| 266 | + Utils.appendName(names.get(i), sb); | |
| 267 | + } | |
| 268 | + sb.append(")").append(" IN ").append("("); | |
| 269 | + for (int i = 0; i < valueLists.size(); i++) { | |
| 270 | + if (i > 0) | |
| 271 | + sb.append(","); | |
| 272 | + | |
| 273 | + List<?> values = valueLists.get(i); | |
| 274 | + | |
| 275 | + // Special case when there is only one bind marker: "IN ?" instead of "IN (?)" | |
| 276 | + if (values.size() == 1 && values.get(0) instanceof BindMarker) { | |
| 277 | + sb.append(values.get(0)); | |
| 278 | + } else { | |
| 279 | + sb.append("("); | |
| 280 | + Utils.joinAndAppendValues(sb, codecRegistry, ",", values, variables).append(')'); | |
| 281 | + } | |
| 282 | + } | |
| 283 | + sb.append(")"); | |
| 284 | + } | |
| 285 | + } | |
| 227 | 286 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +40 −0
| @@ -276,6 +276,26 @@ | ||
| 276 | 276 | return new Clause.SimpleClause(name, "=", value); |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | + /** | |
| 280 | + * Creates an "equal" where clause for a group of clustering columns. | |
| 281 | + * <p/> | |
| 282 | + * For instance, {@code eq(Arrays.asList("a", "b"), Arrays.asList(2, "test"))} | |
| 283 | + * will generate the CQL WHERE clause {@code (a, b) = (2, 'test') }. | |
| 284 | + * <p/> | |
| 285 | + * Please note that this variant is only supported starting with Cassandra 2.0.6. | |
| 286 | + * | |
| 287 | + * @param names the column names | |
| 288 | + * @param values the values | |
| 289 | + * @return the corresponding where clause. | |
| 290 | + * @throws IllegalArgumentException if {@code names.size() != values.size()}. | |
| 291 | + */ | |
| 292 | + public static Clause eq(List<String> names, List<?> values) { | |
| 293 | + if (names.size() != values.size()) | |
| 294 | + throw new IllegalArgumentException(String.format("The number of names (%d) and values (%d) don't match", names.size(), values.size())); | |
| 295 | + | |
| 296 | + return new Clause.CompoundClause(names, "=", values); | |
| 297 | + } | |
| 298 | + | |
| 279 | 299 | /** |
| 280 | 300 | * Create an "in" where clause stating the provided column must be equal |
| 281 | 301 | * to one of the provided values. |
| @@ -300,6 +320,26 @@ | ||
| 300 | 320 | return new Clause.InClause(name, values); |
| 301 | 321 | } |
| 302 | 322 | |
| 323 | + /** | |
| 324 | + * Creates an "in" where clause for a group of clustering columns. | |
| 325 | + * <p/> | |
| 326 | + * For instance, {@code in(Arrays.asList("a", "b"), Arrays.asList(Arrays.asList(1, 2), Arrays.asList("foo", "bar")))} | |
| 327 | + * will generate the CQL WHERE clause {@code (a, b) IN ((1, 2), ('foo', 'bar')) }. | |
| 328 | + * <p/> | |
| 329 | + * Please note that this variant is only supported starting with Cassandra 2.0.9. | |
| 330 | + * | |
| 331 | + * @param names the column names | |
| 332 | + * @param values the values | |
| 333 | + * @return the corresponding where clause. | |
| 334 | + * @throws IllegalArgumentException if {@code names.size() != values.size()}. | |
| 335 | + */ | |
| 336 | + public static Clause in(List<String> names, List<List<?>> values) { | |
| 337 | + if (names.size() != values.size()) | |
| 338 | + throw new IllegalArgumentException(String.format("The number of names (%d) and values (%d) don't match", names.size(), values.size())); | |
| 339 | + | |
| 340 | + return new Clause.CompoundInClause(names, values); | |
| 341 | + } | |
| 342 | + | |
| 303 | 343 | /** |
| 304 | 344 | * Creates a "contains" where clause stating the provided column must contain |
| 305 | 345 | * the value provided. |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +20 −0
| @@ -755,6 +755,10 @@ | ||
| 755 | 755 | String query; |
| 756 | 756 | Statement select; |
| 757 | 757 | |
| 758 | + query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)=('a',2);"; | |
| 759 | + select = select().all().from("foo").where(eq("k", 4)).and(eq(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); | |
| 760 | + assertEquals(select.toString(), query); | |
| 761 | + | |
| 758 | 762 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>('a',2);"; |
| 759 | 763 | select = select().all().from("foo").where(eq("k", 4)).and(gt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); |
| 760 | 764 | assertEquals(select.toString(), query); |
| @@ -767,6 +771,22 @@ | ||
| 767 | 771 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);"; |
| 768 | 772 | select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); |
| 769 | 773 | assertEquals(select.toString(), query); |
| 774 | + | |
| 775 | + query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,2),('foo','bar'));"; | |
| 776 | + List<String> names = ImmutableList.of("c1", "c2"); | |
| 777 | + List<List<?>> values = ImmutableList.<List<?>>of( | |
| 778 | + ImmutableList.of(1, 2), | |
| 779 | + ImmutableList.of("foo", "bar")); | |
| 780 | + select = select().all().from("foo").where(eq("k", 4)).and(in(names, values)); | |
| 781 | + assertEquals(select.toString(), query); | |
| 782 | + | |
| 783 | + query = "SELECT * FROM foo WHERE k=4 AND (c1,c2) IN ((1,2),?);"; | |
| 784 | + names = ImmutableList.of("c1", "c2"); | |
| 785 | + values = ImmutableList.<List<?>>of( | |
| 786 | + ImmutableList.of(1, 2), | |
| 787 | + ImmutableList.of(bindMarker())); | |
| 788 | + select = select().all().from("foo").where(eq("k", 4)).and(in(names, values)); | |
| 789 | + assertEquals(select.toString(), query); | |
| 770 | 790 | } |
| 771 | 791 | |
| 772 | 792 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |