kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 67ef7cb4ad3f1b8ce1fac26d94bf2866152bea9ae3fb784bbfc1d69d25858291 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-10 10:55 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-16 21:48 +01:00 |
| Parents | 56ea1f21527e |
10 files changed
modified changelog/README.md +1 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ### 2.1.10 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes. |
| 6 | +- [bug] JAVA-983: QueryBuilder cannot handle collections containing function calls. | |
| 6 | 7 | |
| 7 | 8 | |
| 8 | 9 | ### 2.1.9 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +2 −2
| @@ -160,11 +160,11 @@ | ||
| 160 | 160 | |
| 161 | 161 | // TODO: Correctly document the InvalidTypeException |
| 162 | 162 | 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)) | |
| 164 | 164 | return; |
| 165 | 165 | |
| 166 | 166 | 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())) { | |
| 168 | 168 | DataType dt = partitionKey.get(i).getType(); |
| 169 | 169 | // We don't really care which protocol version we use, since the only place it matters if for |
| 170 | 170 | // 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 @@ | ||
| 52 | 52 | if (keyspace != null) |
| 53 | 53 | Utils.appendName(keyspace, builder).append('.'); |
| 54 | 54 | Utils.appendName(table, builder); |
| 55 | - builder.append('('); | |
| 55 | + builder.append(" ("); | |
| 56 | 56 | Utils.joinAndAppendNames(builder, ",", names); |
| 57 | 57 | builder.append(") VALUES ("); |
| 58 | 58 | Utils.joinAndAppendValues(builder, ",", values, variables); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +54 −42
| @@ -61,23 +61,6 @@ | ||
| 61 | 61 | return sb; |
| 62 | 62 | } |
| 63 | 63 | |
| 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 | - | |
| 81 | 64 | static ByteBuffer[] convert(List<Object> values, ProtocolVersion protocolVersion) { |
| 82 | 65 | ByteBuffer[] serializedValues = new ByteBuffer[values.size()]; |
| 83 | 66 | for (int i = 0; i < values.size(); i++) { |
| @@ -185,17 +168,6 @@ | ||
| 185 | 168 | } |
| 186 | 169 | } |
| 187 | 170 | |
| 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 | - | |
| 199 | 171 | static StringBuilder appendList(List<?> l, StringBuilder sb) { |
| 200 | 172 | sb.append('['); |
| 201 | 173 | for (int i = 0; i < l.size(); i++) { |
| @@ -255,17 +227,64 @@ | ||
| 255 | 227 | static boolean containsBindMarker(Object value) { |
| 256 | 228 | if (value instanceof BindMarker) |
| 257 | 229 | 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 | + } | |
| 258 | 244 | |
| 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; | |
| 266 | 256 | return false; |
| 267 | 257 | } |
| 268 | 258 | |
| 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 | + | |
| 269 | 288 | static boolean isIdempotent(Object value) { |
| 270 | 289 | if(value == null) { |
| 271 | 290 | return true; |
| @@ -295,13 +314,6 @@ | ||
| 295 | 314 | return sb.append(DataType.text().format(value)); |
| 296 | 315 | } |
| 297 | 316 | |
| 298 | - static boolean isRawValue(Object value) { | |
| 299 | - return value != null | |
| 300 | - && !(value instanceof FCall) | |
| 301 | - && !(value instanceof CName) | |
| 302 | - && !(value instanceof BindMarker); | |
| 303 | - } | |
| 304 | - | |
| 305 | 317 | static String toRawString(Object value) { |
| 306 | 318 | return appendValue(value, new StringBuilder()).toString(); |
| 307 | 319 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +3 −3
| @@ -82,11 +82,11 @@ | ||
| 82 | 82 | public void prepareTest() throws Exception { |
| 83 | 83 | |
| 84 | 84 | // 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); | |
| 87 | 87 | assertEquals(stmt.getQueryString(), query); |
| 88 | 88 | |
| 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);"; | |
| 90 | 90 | stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker("c")).value("d", 0); |
| 91 | 91 | assertEquals(stmt.getQueryString(), query); |
| 92 | 92 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +3 −3
| @@ -136,17 +136,17 @@ | ||
| 136 | 136 | String query; |
| 137 | 137 | Statement insert; |
| 138 | 138 | |
| 139 | - query = "INSERT INTO foo(a) VALUES ('123); --comment');"; | |
| 139 | + query = "INSERT INTO foo (a) VALUES ('123); --comment');"; | |
| 140 | 140 | insert = insertInto("foo") |
| 141 | 141 | .value("a", "123); --comment"); |
| 142 | 142 | assertEquals(insert.toString(), query); |
| 143 | 143 | |
| 144 | - query = "INSERT INTO foo(\"a,b\") VALUES (123);"; | |
| 144 | + query = "INSERT INTO foo (\"a,b\") VALUES (123);"; | |
| 145 | 145 | insert = insertInto("foo") |
| 146 | 146 | .value("a,b", 123); |
| 147 | 147 | assertEquals(insert.toString(), query); |
| 148 | 148 | |
| 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;"; | |
| 150 | 150 | 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)); |
| 151 | 151 | assertEquals(insert.toString(), query); |
| 152 | 152 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +2 −2
| @@ -106,7 +106,7 @@ | ||
| 106 | 106 | query = select().from(table).where(eq("k", 42)); |
| 107 | 107 | |
| 108 | 108 | 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); | |
| 110 | 110 | batch_query += String.format("UPDATE %s.test_int USING TTL 400;", keyspace); |
| 111 | 111 | batch_query += "APPLY BATCH;"; |
| 112 | 112 | batch = batch() |
| @@ -133,7 +133,7 @@ | ||
| 133 | 133 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 134 | 134 | |
| 135 | 135 | 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);"; | |
| 137 | 137 | batch_query += "APPLY BATCH;"; |
| 138 | 138 | batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123)); |
| 139 | 139 | assertEquals(batch.getRoutingKey(), null); |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +89 −25
| @@ -22,6 +22,8 @@ | ||
| 22 | 22 | |
| 23 | 23 | import com.google.common.collect.ImmutableList; |
| 24 | 24 | import com.google.common.collect.ImmutableMap; |
| 25 | +import com.google.common.collect.Lists; | |
| 26 | +import com.google.common.collect.Sets; | |
| 25 | 27 | import org.testng.annotations.Test; |
| 26 | 28 | |
| 27 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -183,7 +185,7 @@ | ||
| 183 | 185 | String query; |
| 184 | 186 | Statement insert; |
| 185 | 187 | |
| 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;"; | |
| 187 | 189 | insert = insertInto("foo") |
| 188 | 190 | .value("a", 123) |
| 189 | 191 | .value("b", InetAddress.getByName("127.0.0.1")) |
| @@ -195,13 +197,13 @@ | ||
| 195 | 197 | .using(timestamp(42)).and(ttl(24)); |
| 196 | 198 | assertEquals(insert.toString(), query); |
| 197 | 199 | |
| 198 | - query = "INSERT INTO foo(a,b) VALUES (2,null);"; | |
| 200 | + query = "INSERT INTO foo (a,b) VALUES (2,null);"; | |
| 199 | 201 | insert = insertInto("foo") |
| 200 | 202 | .value("a", 2) |
| 201 | 203 | .value("b", null); |
| 202 | 204 | assertEquals(insert.toString(), query); |
| 203 | 205 | |
| 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;"; | |
| 205 | 207 | insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 206 | 208 | add(2); |
| 207 | 209 | add(3); |
| @@ -209,7 +211,7 @@ | ||
| 209 | 211 | }}, 3.4 }).using(ttl(24)).and(timestamp(42)); |
| 210 | 212 | assertEquals(insert.toString(), query); |
| 211 | 213 | |
| 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 ?;"; | |
| 213 | 215 | insert = insertInto("foo", "bar") |
| 214 | 216 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 215 | 217 | add(2); |
| @@ -221,7 +223,7 @@ | ||
| 221 | 223 | assertEquals(insert.toString(), query); |
| 222 | 224 | |
| 223 | 225 | // 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;"; | |
| 225 | 227 | insert = insertInto("foo", "bar") |
| 226 | 228 | .using(timestamp(42)) |
| 227 | 229 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| @@ -233,7 +235,7 @@ | ||
| 233 | 235 | assertEquals(insert.toString(), query); |
| 234 | 236 | |
| 235 | 237 | // 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;"; | |
| 237 | 239 | insert = insertInto("foo") |
| 238 | 240 | .using(timestamp(42)) |
| 239 | 241 | .value("c", 123) |
| @@ -252,11 +254,11 @@ | ||
| 252 | 254 | } |
| 253 | 255 | |
| 254 | 256 | // 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;"; | |
| 256 | 258 | insert = insertInto("foo").value("k", 0).value("x", 1).ifNotExists(); |
| 257 | 259 | assertEquals(insert.toString(), query); |
| 258 | 260 | |
| 259 | - query = "INSERT INTO foo(k,x) VALUES (0,(1));"; | |
| 261 | + query = "INSERT INTO foo (k,x) VALUES (0,(1));"; | |
| 260 | 262 | insert = insertInto("foo").value("k", 0).value("x", TupleType.of(cint()).newValue(1)); |
| 261 | 263 | assertEquals(insert.toString(), query); |
| 262 | 264 | |
| @@ -415,7 +417,7 @@ | ||
| 415 | 417 | Statement batch; |
| 416 | 418 | |
| 417 | 419 | 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);"; | |
| 419 | 421 | query += "UPDATE foo SET a[2]='foo',b=[3,2,1]+b,c=c-{'a'} WHERE k=2;"; |
| 420 | 422 | query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 421 | 423 | query += "APPLY BATCH;"; |
| @@ -525,7 +527,7 @@ | ||
| 525 | 527 | String query; |
| 526 | 528 | Statement insert; |
| 527 | 529 | |
| 528 | - query = "INSERT INTO test(k,c) VALUES (0,?);"; | |
| 530 | + query = "INSERT INTO test (k,c) VALUES (0,?);"; | |
| 529 | 531 | insert = insertInto("test") |
| 530 | 532 | .value("k", 0) |
| 531 | 533 | .value("c", bindMarker()); |
| @@ -618,15 +620,15 @@ | ||
| 618 | 620 | String query; |
| 619 | 621 | Statement insert; |
| 620 | 622 | |
| 621 | - query = "INSERT INTO foo(a) VALUES ('123); --comment');"; | |
| 623 | + query = "INSERT INTO foo (a) VALUES ('123); --comment');"; | |
| 622 | 624 | insert = insertInto("foo").value("a", "123); --comment"); |
| 623 | 625 | assertEquals(insert.toString(), query); |
| 624 | 626 | |
| 625 | - query = "INSERT INTO foo(\"a,b\") VALUES (123);"; | |
| 627 | + query = "INSERT INTO foo (\"a,b\") VALUES (123);"; | |
| 626 | 628 | insert = insertInto("foo").value("a,b", 123); |
| 627 | 629 | assertEquals(insert.toString(), query); |
| 628 | 630 | |
| 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;"; | |
| 630 | 632 | insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<String>() {{ |
| 631 | 633 | add("2'} space"); |
| 632 | 634 | add("3"); |
| @@ -731,23 +733,23 @@ | ||
| 731 | 733 | |
| 732 | 734 | @Test(groups = "unit") |
| 733 | 735 | public void quotingTest() { |
| 734 | - assertEquals(QueryBuilder.select().from("Metrics", "epochs").getQueryString(), | |
| 736 | + assertEquals(select().from("Metrics", "epochs").getQueryString(), | |
| 735 | 737 | "SELECT * FROM Metrics.epochs;"); |
| 736 | - assertEquals(QueryBuilder.select().from("Metrics", quote("epochs")).getQueryString(), | |
| 738 | + assertEquals(select().from("Metrics", quote("epochs")).getQueryString(), | |
| 737 | 739 | "SELECT * FROM Metrics.\"epochs\";"); |
| 738 | - assertEquals(QueryBuilder.select().from(quote("Metrics"), "epochs").getQueryString(), | |
| 740 | + assertEquals(select().from(quote("Metrics"), "epochs").getQueryString(), | |
| 739 | 741 | "SELECT * FROM \"Metrics\".epochs;"); |
| 740 | - assertEquals(QueryBuilder.select().from(quote("Metrics"), quote("epochs")).getQueryString(), | |
| 742 | + assertEquals(select().from(quote("Metrics"), quote("epochs")).getQueryString(), | |
| 741 | 743 | "SELECT * FROM \"Metrics\".\"epochs\";"); |
| 742 | 744 | |
| 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 ();"); | |
| 751 | 753 | } |
| 752 | 754 | |
| 753 | 755 | @Test(groups = "unit") |
| @@ -896,4 +898,66 @@ | ||
| 896 | 898 | assertThat(actual).isSameAs(expected); |
| 897 | 899 | } |
| 898 | 900 | |
| 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 | + | |
| 899 | 963 | } |
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 @@ | ||
| 52 | 52 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 53 | 53 | |
| 54 | 54 | 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'});"); | |
| 56 | 56 | |
| 57 | 57 | session.execute(insert); |
| 58 | 58 | |
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); |
| 72 | 72 | |
| 73 | 73 | 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'}]);"); | |
| 75 | 75 | |
| 76 | 76 | session.execute(insert); |
| 77 | 77 |