Elffuss BraidHub

kikocis/java-driver public

JAVA-983: QueryBuilder cannot handle collections containing function calls.

This commit also incidentally formats INSERT statements with a whitestpace between table name and column list,
so that they do not look like a function call.
Changeset67ef7cb4ad3f1b8ce1fac26d94bf2866152bea9ae3fb784bbfc1d69d25858291
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-10 10:55 +01:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-16 21:48 +01:00
Parents56ea1f21527e

Browse the files at this changeset

10 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 2.1.10 (in progress)
44
55 - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes.
6+- [bug] JAVA-983: QueryBuilder cannot handle collections containing function calls.
67
78
89 ### 2.1.9
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +2 −2
@@ -160,11 +160,11 @@
160160
161161 // TODO: Correctly document the InvalidTypeException
162162 void maybeAddRoutingKey(String name, Object value) {
163- if (routingKey == null || name == null || value == null || value instanceof BindMarker)
163+ if (routingKey == null || name == null || value == null || Utils.containsSpecialValue(value))
164164 return;
165165
166166 for (int i = 0; i < partitionKey.size(); i++) {
167- if (name.equals(partitionKey.get(i).getName()) && Utils.isRawValue(value)) {
167+ if (name.equals(partitionKey.get(i).getName())) {
168168 DataType dt = partitionKey.get(i).getType();
169169 // We don't really care which protocol version we use, since the only place it matters if for
170170 // collections (not inside UDT), and those are not allowed in a partition key anyway, hence the hardcoding.
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +1 −1
@@ -52,7 +52,7 @@
5252 if (keyspace != null)
5353 Utils.appendName(keyspace, builder).append('.');
5454 Utils.appendName(table, builder);
55- builder.append('(');
55+ builder.append(" (");
5656 Utils.joinAndAppendNames(builder, ",", names);
5757 builder.append(") VALUES (");
5858 Utils.joinAndAppendValues(builder, ",", values, variables);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +54 −42
@@ -61,23 +61,6 @@
6161 return sb;
6262 }
6363
64- // Returns false if it's not really serializable (function call, bind markers, ...)
65- static boolean isSerializable(Object value) {
66- if (value instanceof BindMarker || value instanceof FCall || value instanceof CName)
67- return false;
68-
69- if (value instanceof RawString)
70- return false;
71-
72- // We also don't serialize fixed size number types. The reason is that if we do it, we will
73- // force a particular size (4 bytes for ints, ...) and for the query builder, we don't want
74- // users to have to bother with that.
75- if (value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal))
76- return false;
77-
78- return true;
79- }
80-
8164 static ByteBuffer[] convert(List<Object> values, ProtocolVersion protocolVersion) {
8265 ByteBuffer[] serializedValues = new ByteBuffer[values.size()];
8366 for (int i = 0; i < values.size(); i++) {
@@ -185,17 +168,6 @@
185168 }
186169 }
187170
188- static StringBuilder appendCollection(Object value, StringBuilder sb, List<Object> variables) {
189- if (variables == null || !isSerializable(value)) {
190- boolean wasCollection = appendValueIfCollection(value, sb);
191- assert wasCollection;
192- } else {
193- sb.append('?');
194- variables.add(value);
195- }
196- return sb;
197- }
198-
199171 static StringBuilder appendList(List<?> l, StringBuilder sb) {
200172 sb.append('[');
201173 for (int i = 0; i < l.size(); i++) {
@@ -255,17 +227,64 @@
255227 static boolean containsBindMarker(Object value) {
256228 if (value instanceof BindMarker)
257229 return true;
230+ if (value instanceof FCall)
231+ for (Object param : ((FCall)value).parameters)
232+ if (containsBindMarker(param))
233+ return true;
234+ if (value instanceof Collection)
235+ for (Object elt : (Collection)value)
236+ if (containsBindMarker(elt))
237+ return true;
238+ if (value instanceof Map)
239+ for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet())
240+ if (containsBindMarker(entry.getKey()) || containsBindMarker(entry.getValue()))
241+ return true;
242+ return false;
243+ }
258244
259- if (!(value instanceof FCall))
260- return false;
261-
262- FCall fcall = (FCall)value;
263- for (Object param : fcall.parameters)
264- if (containsBindMarker(param))
265- return true;
245+ static boolean containsSpecialValue(Object value) {
246+ if (value instanceof BindMarker || value instanceof FCall || value instanceof CName || value instanceof RawString)
247+ return true;
248+ if (value instanceof Collection)
249+ for (Object elt : (Collection)value)
250+ if (containsSpecialValue(elt))
251+ return true;
252+ if (value instanceof Map)
253+ for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet())
254+ if (containsSpecialValue(entry.getKey()) || containsSpecialValue(entry.getValue()))
255+ return true;
266256 return false;
267257 }
268258
259+ /**
260+ * Return true if the given value is likely to find a suitable codec
261+ * to be serialized as a query parameter.
262+ * If the value is not serializable, it must be included in the query string.
263+ * Non serializable values include special values such as function calls,
264+ * column names and bind markers, and collections thereof.
265+ * We also don't serialize fixed size number types. The reason is that if we do it, we will
266+ * force a particular size (4 bytes for ints, ...) and for the query builder, we don't want
267+ * users to have to bother with that.
268+ *
269+ * @param value the value to inspect.
270+ * @return true if the value is serializable, false otherwise.
271+ */
272+ static boolean isSerializable(Object value) {
273+ if (containsSpecialValue(value))
274+ return false;
275+ if (value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal))
276+ return false;
277+ if (value instanceof Collection)
278+ for (Object elt : (Collection)value)
279+ if (!isSerializable(elt))
280+ return false;
281+ if (value instanceof Map)
282+ for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet())
283+ if (!isSerializable(entry.getKey()) || !isSerializable(entry.getValue()))
284+ return false;
285+ return true;
286+ }
287+
269288 static boolean isIdempotent(Object value) {
270289 if(value == null) {
271290 return true;
@@ -295,13 +314,6 @@
295314 return sb.append(DataType.text().format(value));
296315 }
297316
298- static boolean isRawValue(Object value) {
299- return value != null
300- && !(value instanceof FCall)
301- && !(value instanceof CName)
302- && !(value instanceof BindMarker);
303- }
304-
305317 static String toRawString(Object value) {
306318 return appendValue(value, new StringBuilder()).toString();
307319 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +3 −3
@@ -82,11 +82,11 @@
8282 public void prepareTest() throws Exception {
8383
8484 // Just check we correctly avoid values when there is a bind marker
85- String query = "INSERT INTO foo(a,b,c,d) VALUES ('foo','bar',?,0);";
86- RegularStatement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0);
85+ String query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',?,0);";
86+ BuiltStatement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0);
8787 assertEquals(stmt.getQueryString(), query);
8888
89- query = "INSERT INTO foo(a,b,c,d) VALUES ('foo','bar',:c,0);";
89+ query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',:c,0);";
9090 stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker("c")).value("d", 0);
9191 assertEquals(stmt.getQueryString(), query);
9292 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +3 −3
@@ -136,17 +136,17 @@
136136 String query;
137137 Statement insert;
138138
139- query = "INSERT INTO foo(a) VALUES ('123); --comment');";
139+ query = "INSERT INTO foo (a) VALUES ('123); --comment');";
140140 insert = insertInto("foo")
141141 .value("a", "123); --comment");
142142 assertEquals(insert.toString(), query);
143143
144- query = "INSERT INTO foo(\"a,b\") VALUES (123);";
144+ query = "INSERT INTO foo (\"a,b\") VALUES (123);";
145145 insert = insertInto("foo")
146146 .value("a,b", 123);
147147 assertEquals(insert.toString(), query);
148148
149- query = "INSERT INTO foo(a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;";
149+ query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;";
150150 insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<String>(){{ add("2'} space"); add("3"); add("4"); }}, 3.4 }).using(ttl(24)).and(timestamp(42));
151151 assertEquals(insert.toString(), query);
152152 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +2 −2
@@ -106,7 +106,7 @@
106106 query = select().from(table).where(eq("k", 42));
107107
108108 batch_query = "BEGIN BATCH ";
109- batch_query += String.format("INSERT INTO %s.test_int(k,a) VALUES (42,1);", keyspace);
109+ batch_query += String.format("INSERT INTO %s.test_int (k,a) VALUES (42,1);", keyspace);
110110 batch_query += String.format("UPDATE %s.test_int USING TTL 400;", keyspace);
111111 batch_query += "APPLY BATCH;";
112112 batch = batch()
@@ -133,7 +133,7 @@
133133 // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL
134134
135135 batch_query = "BEGIN BATCH USING TIMESTAMP 42 ";
136- batch_query += "INSERT INTO foo.bar(a) VALUES (123);";
136+ batch_query += "INSERT INTO foo.bar (a) VALUES (123);";
137137 batch_query += "APPLY BATCH;";
138138 batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123));
139139 assertEquals(batch.getRoutingKey(), null);
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +89 −25
@@ -22,6 +22,8 @@
2222
2323 import com.google.common.collect.ImmutableList;
2424 import com.google.common.collect.ImmutableMap;
25+import com.google.common.collect.Lists;
26+import com.google.common.collect.Sets;
2527 import org.testng.annotations.Test;
2628
2729 import static org.assertj.core.api.Assertions.assertThat;
@@ -183,7 +185,7 @@
183185 String query;
184186 Statement insert;
185187
186- query = "INSERT INTO foo(a,b,\"C\",d) VALUES (123,'127.0.0.1','foo''bar',{'x':3,'y':2}) USING TIMESTAMP 42 AND TTL 24;";
188+ query = "INSERT INTO foo (a,b,\"C\",d) VALUES (123,'127.0.0.1','foo''bar',{'x':3,'y':2}) USING TIMESTAMP 42 AND TTL 24;";
187189 insert = insertInto("foo")
188190 .value("a", 123)
189191 .value("b", InetAddress.getByName("127.0.0.1"))
@@ -195,13 +197,13 @@
195197 .using(timestamp(42)).and(ttl(24));
196198 assertEquals(insert.toString(), query);
197199
198- query = "INSERT INTO foo(a,b) VALUES (2,null);";
200+ query = "INSERT INTO foo (a,b) VALUES (2,null);";
199201 insert = insertInto("foo")
200202 .value("a", 2)
201203 .value("b", null);
202204 assertEquals(insert.toString(), query);
203205
204- query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;";
206+ query = "INSERT INTO foo (a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;";
205207 insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
206208 add(2);
207209 add(3);
@@ -209,7 +211,7 @@
209211 }}, 3.4 }).using(ttl(24)).and(timestamp(42));
210212 assertEquals(insert.toString(), query);
211213
212- query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;";
214+ query = "INSERT INTO foo.bar (a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;";
213215 insert = insertInto("foo", "bar")
214216 .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
215217 add(2);
@@ -221,7 +223,7 @@
221223 assertEquals(insert.toString(), query);
222224
223225 // commutative result of TIMESTAMP
224- query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;";
226+ query = "INSERT INTO foo.bar (a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;";
225227 insert = insertInto("foo", "bar")
226228 .using(timestamp(42))
227229 .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
@@ -233,7 +235,7 @@
233235 assertEquals(insert.toString(), query);
234236
235237 // commutative result of value() and values()
236- query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;";
238+ query = "INSERT INTO foo (c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;";
237239 insert = insertInto("foo")
238240 .using(timestamp(42))
239241 .value("c", 123)
@@ -252,11 +254,11 @@
252254 }
253255
254256 // CAS test
255- query = "INSERT INTO foo(k,x) VALUES (0,1) IF NOT EXISTS;";
257+ query = "INSERT INTO foo (k,x) VALUES (0,1) IF NOT EXISTS;";
256258 insert = insertInto("foo").value("k", 0).value("x", 1).ifNotExists();
257259 assertEquals(insert.toString(), query);
258260
259- query = "INSERT INTO foo(k,x) VALUES (0,(1));";
261+ query = "INSERT INTO foo (k,x) VALUES (0,(1));";
260262 insert = insertInto("foo").value("k", 0).value("x", TupleType.of(cint()).newValue(1));
261263 assertEquals(insert.toString(), query);
262264
@@ -415,7 +417,7 @@
415417 Statement batch;
416418
417419 query = "BEGIN BATCH USING TIMESTAMP 42 ";
418- query += "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4);";
420+ query += "INSERT INTO foo (a,b) VALUES ({2,3,4},3.4);";
419421 query += "UPDATE foo SET a[2]='foo',b=[3,2,1]+b,c=c-{'a'} WHERE k=2;";
420422 query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;";
421423 query += "APPLY BATCH;";
@@ -525,7 +527,7 @@
525527 String query;
526528 Statement insert;
527529
528- query = "INSERT INTO test(k,c) VALUES (0,?);";
530+ query = "INSERT INTO test (k,c) VALUES (0,?);";
529531 insert = insertInto("test")
530532 .value("k", 0)
531533 .value("c", bindMarker());
@@ -618,15 +620,15 @@
618620 String query;
619621 Statement insert;
620622
621- query = "INSERT INTO foo(a) VALUES ('123); --comment');";
623+ query = "INSERT INTO foo (a) VALUES ('123); --comment');";
622624 insert = insertInto("foo").value("a", "123); --comment");
623625 assertEquals(insert.toString(), query);
624626
625- query = "INSERT INTO foo(\"a,b\") VALUES (123);";
627+ query = "INSERT INTO foo (\"a,b\") VALUES (123);";
626628 insert = insertInto("foo").value("a,b", 123);
627629 assertEquals(insert.toString(), query);
628630
629- query = "INSERT INTO foo(a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;";
631+ query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;";
630632 insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<String>() {{
631633 add("2'} space");
632634 add("3");
@@ -731,23 +733,23 @@
731733
732734 @Test(groups = "unit")
733735 public void quotingTest() {
734- assertEquals(QueryBuilder.select().from("Metrics", "epochs").getQueryString(),
736+ assertEquals(select().from("Metrics", "epochs").getQueryString(),
735737 "SELECT * FROM Metrics.epochs;");
736- assertEquals(QueryBuilder.select().from("Metrics", quote("epochs")).getQueryString(),
738+ assertEquals(select().from("Metrics", quote("epochs")).getQueryString(),
737739 "SELECT * FROM Metrics.\"epochs\";");
738- assertEquals(QueryBuilder.select().from(quote("Metrics"), "epochs").getQueryString(),
740+ assertEquals(select().from(quote("Metrics"), "epochs").getQueryString(),
739741 "SELECT * FROM \"Metrics\".epochs;");
740- assertEquals(QueryBuilder.select().from(quote("Metrics"), quote("epochs")).getQueryString(),
742+ assertEquals(select().from(quote("Metrics"), quote("epochs")).getQueryString(),
741743 "SELECT * FROM \"Metrics\".\"epochs\";");
742744
743- assertEquals(QueryBuilder.insertInto("Metrics", "epochs").getQueryString(),
744- "INSERT INTO Metrics.epochs() VALUES ();");
745- assertEquals(QueryBuilder.insertInto("Metrics", quote("epochs")).getQueryString(),
746- "INSERT INTO Metrics.\"epochs\"() VALUES ();");
747- assertEquals(QueryBuilder.insertInto(quote("Metrics"), "epochs").getQueryString(),
748- "INSERT INTO \"Metrics\".epochs() VALUES ();");
749- assertEquals(QueryBuilder.insertInto(quote("Metrics"), quote("epochs")).getQueryString(),
750- "INSERT INTO \"Metrics\".\"epochs\"() VALUES ();");
745+ assertEquals(insertInto("Metrics", "epochs").toString(),
746+ "INSERT INTO Metrics.epochs () VALUES ();");
747+ assertEquals(insertInto("Metrics", quote("epochs")).toString(),
748+ "INSERT INTO Metrics.\"epochs\" () VALUES ();");
749+ assertEquals(insertInto(quote("Metrics"), "epochs").toString(),
750+ "INSERT INTO \"Metrics\".epochs () VALUES ();");
751+ assertEquals(insertInto(quote("Metrics"), quote("epochs")).toString(),
752+ "INSERT INTO \"Metrics\".\"epochs\" () VALUES ();");
751753 }
752754
753755 @Test(groups = "unit")
@@ -896,4 +898,66 @@
896898 assertThat(actual).isSameAs(expected);
897899 }
898900
901+ @Test(groups = "unit")
902+ @CassandraVersion(major = 2.1)
903+ public void should_serialize_collections_of_serializable_elements() {
904+ Set<UUID> set = Sets.newHashSet(UUID.randomUUID());
905+ List<Date> list = Lists.newArrayList(new Date());
906+ Map<BigInteger, String> map = ImmutableMap.of(new BigInteger("1"), "foo");
907+ BuiltStatement query = insertInto("foo").value("v", set);
908+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
909+ assertThat(query.getObject(0)).isEqualTo(set);
910+ query = insertInto("foo").value("v", list);
911+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
912+ assertThat(query.getObject(0)).isEqualTo(list);
913+ query = insertInto("foo").value("v", map);
914+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);");
915+ assertThat(query.getObject(0)).isEqualTo(map);
916+ }
917+
918+ @Test(groups = "unit")
919+ @CassandraVersion(major = 2.1)
920+ public void should_not_attempt_to_serialize_function_calls_in_collections() {
921+ BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1)));
922+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});");
923+ assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
924+ }
925+
926+ @Test(groups = "unit")
927+ @CassandraVersion(major = 2.1)
928+ public void should_not_attempt_to_serialize_bind_markers_in_collections() {
929+ BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker()));
930+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);");
931+ assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
932+ }
933+
934+ @Test(groups = "unit")
935+ @CassandraVersion(major = 2.1)
936+ public void should_not_attempt_to_serialize_raw_values_in_collections() {
937+ BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x")));
938+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});");
939+ assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
940+ }
941+
942+ @Test(groups = "unit")
943+ @CassandraVersion(major = 2.1)
944+ public void should_not_attempt_to_serialize_collections_containing_numbers() {
945+ BuiltStatement query;
946+ // lists
947+ List<Integer> list = Lists.newArrayList(1, 2, 3);
948+ query = insertInto("foo").value("v", list);
949+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,3]);");
950+ assertThat(query.hasValues()).isFalse();
951+ // sets
952+ Set<Integer> set = Sets.newHashSet(1, 2, 3);
953+ query = insertInto("foo").value("v", set);
954+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1,2,3});");
955+ assertThat(query.hasValues()).isFalse();
956+ // maps
957+ Map<Integer, Float> map = ImmutableMap.of(1, 12.34f);
958+ query = insertInto("foo").value("v", map);
959+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:12.34});");
960+ assertThat(query.hasValues()).isFalse();
961+ }
962+
899963 }
added driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTupleExecutionTest.java +67 −0
@@ -0,0 +1,67 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.querybuilder;
17+
18+import java.util.Collection;
19+import java.util.Collections;
20+import java.util.List;
21+
22+import com.google.common.collect.ImmutableList;
23+import org.testng.annotations.Test;
24+
25+import static org.assertj.core.api.Assertions.assertThat;
26+import static org.testng.Assert.assertEquals;
27+
28+import com.datastax.driver.core.CCMBridge;
29+import com.datastax.driver.core.TupleType;
30+import com.datastax.driver.core.TupleValue;
31+import com.datastax.driver.core.utils.CassandraVersion;
32+
33+import static com.datastax.driver.core.DataType.cint;
34+import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
35+import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
36+import static com.datastax.driver.core.querybuilder.QueryBuilder.set;
37+import static com.datastax.driver.core.querybuilder.QueryBuilder.update;
38+
39+@CassandraVersion(major=2.1, minor=3)
40+public class QueryBuilderTupleExecutionTest extends CCMBridge.PerClassSingleNodeCluster {
41+
42+ @Override
43+ protected Collection<String> getTableDefinitions() {
44+ return Collections.emptyList();
45+ }
46+
47+ @Test(groups = "short")
48+ public void should_handle_tuple() throws Exception {
49+ String query = "INSERT INTO foo (k,x) VALUES (0,(1));";
50+ TupleType tupleType = TupleType.of(cint());
51+ BuiltStatement insert = insertInto("foo").value("k", 0).value("x", tupleType.newValue(1));
52+ assertEquals(insert.toString(), query);
53+ }
54+
55+ @SuppressWarnings("deprecation")
56+ @Test(groups = "short")
57+ public void should_handle_collections_of_tuples() {
58+ String query;
59+ BuiltStatement statement;
60+ query = "UPDATE foo SET l=[(1, 2)] WHERE k=1;";
61+ TupleType tupleType = TupleType.of(cint(), cint());
62+ List<TupleValue> list = ImmutableList.of(tupleType.newValue(1, 2));
63+ statement = update("foo").with(set("l", list)).where(eq("k", 1));
64+ assertThat(statement.toString()).isEqualTo(query);
65+ }
66+
67+}
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderUDTExecutionTest.java +2 −2
@@ -52,7 +52,7 @@
5252 UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost"));
5353
5454 Statement insert = insertInto("udtTest").value("k", 1).value("t", udtValue);
55- assertEquals(insert.toString(), "INSERT INTO udtTest(k,t) VALUES (1,{i:2, a:'127.0.0.1'});");
55+ assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) VALUES (1,{i:2, a:'127.0.0.1'});");
5656
5757 session.execute(insert);
5858
@@ -71,7 +71,7 @@
7171 UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost"));
7272
7373 Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue));
74- assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest(k,l) VALUES (1,[{i:2, a:'127.0.0.1'}]);");
74+ assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2, a:'127.0.0.1'}]);");
7575
7676 session.execute(insert);
7777