Elffuss BraidHub

kikocis/java-driver public

Merge pull request #651 from datastax/java866

JAVA-866: Support tuple notation in QueryBuilder.eq/in.
Changesetb3b954dfe91cff4d77184317e73ffd34fc9d0bd67157619e3da256f6f022a88a
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 14:09 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 14:09 +02:00
Parents49e1ebe6c225, 6f181df35a26

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -10,6 +10,7 @@
1010 - [improvement] JAVA-1109: Document SSLOptions changes in upgrade guide.
1111 - [improvement] JAVA-1065: Add method to create token from partition key values.
1212 - [improvement] JAVA-1136: Enable JDK signature check in module driver-extras.
13+- [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in.
1314
1415 Merged from 2.1 branch:
1516
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +59 −0
@@ -224,4 +224,63 @@
224224 sb.append(")");
225225 }
226226 }
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+ }
227286 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +40 −0
@@ -276,6 +276,26 @@
276276 return new Clause.SimpleClause(name, "=", value);
277277 }
278278
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+
279299 /**
280300 * Create an "in" where clause stating the provided column must be equal
281301 * to one of the provided values.
@@ -300,6 +320,26 @@
300320 return new Clause.InClause(name, values);
301321 }
302322
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+
303343 /**
304344 * Creates a "contains" where clause stating the provided column must contain
305345 * the value provided.
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +20 −0
@@ -755,6 +755,10 @@
755755 String query;
756756 Statement select;
757757
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+
758762 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>('a',2);";
759763 select = select().all().from("foo").where(eq("k", 4)).and(gt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)));
760764 assertEquals(select.toString(), query);
@@ -767,6 +771,22 @@
767771 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);";
768772 select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)));
769773 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);
770790 }
771791
772792 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)