kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3c92ee75e72252e4b70d1f2f2d733f56331526cc3a2161e1719bc55a016581dc |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-16 23:22 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-16 23:22 +01:00 |
| Parents | 6dfe1c19ca9a, 1cbac626deaf |
10 files changed
modified changelog/README.md +1 −1
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | - [improvement] JAVA-652: Add DCAwareRoundRobinPolicy builder. |
| 32 | 32 | - [improvement] JAVA-808: Add generic filtering policy that can be used to exclude specific DCs. |
| 33 | 33 | - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes. |
| 34 | - | |
| 34 | +- [bug] JAVA-983: QueryBuilder cannot handle collections containing function calls. | |
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | ### 3.0.0-alpha4 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +2 −2
| @@ -169,11 +169,11 @@ | ||
| 169 | 169 | |
| 170 | 170 | // TODO: Correctly document the InvalidTypeException |
| 171 | 171 | void maybeAddRoutingKey(String name, Object value) { |
| 172 | - if (routingKeyValues == null || name == null || value == null || value instanceof BindMarker) | |
| 172 | + if (routingKeyValues == null || name == null || value == null || Utils.containsSpecialValue(value)) | |
| 173 | 173 | return; |
| 174 | 174 | |
| 175 | 175 | for (int i = 0; i < partitionKey.size(); i++) { |
| 176 | - if (name.equals(partitionKey.get(i).getName()) && Utils.isRawValue(value)) { | |
| 176 | + if (name.equals(partitionKey.get(i).getName())) { | |
| 177 | 177 | routingKeyValues.set(i, value); |
| 178 | 178 | return; |
| 179 | 179 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +1 −1
| @@ -56,7 +56,7 @@ | ||
| 56 | 56 | if (keyspace != null) |
| 57 | 57 | Utils.appendName(keyspace, builder).append('.'); |
| 58 | 58 | Utils.appendName(table, builder); |
| 59 | - builder.append('('); | |
| 59 | + builder.append(" ("); | |
| 60 | 60 | Utils.joinAndAppendNames(builder, codecRegistry, ",", names); |
| 61 | 61 | builder.append(") VALUES ("); |
| 62 | 62 | Utils.joinAndAppendValues(builder, codecRegistry, ",", values, variables); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +103 −26
| @@ -20,12 +20,12 @@ | ||
| 20 | 20 | import java.util.Collection; |
| 21 | 21 | import java.util.List; |
| 22 | 22 | import java.util.Map; |
| 23 | +import java.util.Set; | |
| 23 | 24 | import java.util.regex.Pattern; |
| 24 | 25 | |
| 25 | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | 27 | |
| 27 | -import com.datastax.driver.core.CodecRegistry; | |
| 28 | -import com.datastax.driver.core.TypeCodec; | |
| 28 | +import com.datastax.driver.core.*; | |
| 29 | 29 | |
| 30 | 30 | // Static utilities private to the query builder |
| 31 | 31 | abstract class Utils { |
| @@ -59,16 +59,6 @@ | ||
| 59 | 59 | return sb; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - private static boolean isForceAppendToQueryString(Object value) { | |
| 63 | - // Force append to query string for all fixed-size number types. | |
| 64 | - // The reason is that if we don't do it, we will | |
| 65 | - // force a particular size (4 bytes for ints, ...) | |
| 66 | - // and for the query builder, we don't want | |
| 67 | - // users to have to bother with that. | |
| 68 | - // TODO this probably does not work well with custom codecs for fixed-size numbers (int, bigint, float, double) | |
| 69 | - return value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal); | |
| 70 | - } | |
| 71 | - | |
| 72 | 62 | static StringBuilder appendValue(Object value, CodecRegistry codecRegistry, StringBuilder sb, List<Object> variables) { |
| 73 | 63 | if (value == null) { |
| 74 | 64 | sb.append("null"); |
| @@ -87,7 +77,16 @@ | ||
| 87 | 77 | appendName(((CName)value).name, codecRegistry, sb); |
| 88 | 78 | } else if (value instanceof RawString) { |
| 89 | 79 | sb.append(value.toString()); |
| 90 | - } else if (variables == null || isForceAppendToQueryString(value)) { | |
| 80 | + } else if(value instanceof List && !isSerializable(value)) { | |
| 81 | + // bind variables are not supported inside collection literals | |
| 82 | + appendList((List<?>)value, codecRegistry, sb, null); | |
| 83 | + } else if(value instanceof Set && !isSerializable(value)) { | |
| 84 | + // bind variables are not supported inside collection literals | |
| 85 | + appendSet((Set<?>)value, codecRegistry, sb, null); | |
| 86 | + } else if(value instanceof Map && !isSerializable(value)) { | |
| 87 | + // bind variables are not supported inside collection literals | |
| 88 | + appendMap((Map<?,?>)value, codecRegistry, sb, null); | |
| 89 | + } else if (variables == null || !isSerializable(value)) { | |
| 91 | 90 | // we are not collecting statement values (variables == null) |
| 92 | 91 | // or the value is meant to be forcefully appended to the query string: |
| 93 | 92 | // format it with the appropriate codec and append it now |
| @@ -104,20 +103,105 @@ | ||
| 104 | 103 | return sb; |
| 105 | 104 | } |
| 106 | 105 | |
| 106 | + private static StringBuilder appendList(List<?> l, CodecRegistry codecRegistry, StringBuilder sb, List<Object> variables) { | |
| 107 | + sb.append('['); | |
| 108 | + for (int i = 0; i < l.size(); i++) { | |
| 109 | + if (i > 0) | |
| 110 | + sb.append(','); | |
| 111 | + appendValue(l.get(i), codecRegistry, sb, variables); | |
| 112 | + } | |
| 113 | + sb.append(']'); | |
| 114 | + return sb; | |
| 115 | + } | |
| 116 | + | |
| 117 | + private static StringBuilder appendSet(Set<?> s, CodecRegistry codecRegistry, StringBuilder sb, List<Object> variables) { | |
| 118 | + sb.append('{'); | |
| 119 | + boolean first = true; | |
| 120 | + for (Object elt : s) { | |
| 121 | + if (first) first = false; else sb.append(','); | |
| 122 | + appendValue(elt, codecRegistry, sb, variables); | |
| 123 | + } | |
| 124 | + sb.append('}'); | |
| 125 | + return sb; | |
| 126 | + } | |
| 127 | + | |
| 128 | + private static StringBuilder appendMap(Map<?, ?> m, CodecRegistry codecRegistry, StringBuilder sb, List<Object> variables) { | |
| 129 | + sb.append('{'); | |
| 130 | + boolean first = true; | |
| 131 | + for (Map.Entry<?, ?> entry : m.entrySet()) { | |
| 132 | + if (first) | |
| 133 | + first = false; | |
| 134 | + else | |
| 135 | + sb.append(','); | |
| 136 | + appendValue(entry.getKey(), codecRegistry, sb, variables); | |
| 137 | + sb.append(':'); | |
| 138 | + appendValue(entry.getValue(), codecRegistry, sb, variables); | |
| 139 | + } | |
| 140 | + sb.append('}'); | |
| 141 | + return sb; | |
| 142 | + } | |
| 143 | + | |
| 107 | 144 | static boolean containsBindMarker(Object value) { |
| 108 | 145 | if (value instanceof BindMarker) |
| 109 | 146 | return true; |
| 110 | - | |
| 111 | - if (!(value instanceof FCall)) | |
| 112 | - return false; | |
| 113 | - | |
| 114 | - FCall fcall = (FCall)value; | |
| 115 | - for (Object param : fcall.parameters) | |
| 147 | + if (value instanceof FCall) | |
| 148 | + for (Object param : ((FCall)value).parameters) | |
| 116 | 149 | if (containsBindMarker(param)) |
| 117 | 150 | return true; |
| 151 | + if (value instanceof Collection) | |
| 152 | + for (Object elt : (Collection)value) | |
| 153 | + if (containsBindMarker(elt)) | |
| 154 | + return true; | |
| 155 | + if (value instanceof Map) | |
| 156 | + for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet()) | |
| 157 | + if (containsBindMarker(entry.getKey()) || containsBindMarker(entry.getValue())) | |
| 158 | + return true; | |
| 159 | + return false; | |
| 160 | + } | |
| 161 | + | |
| 162 | + static boolean containsSpecialValue(Object value) { | |
| 163 | + if (value instanceof BindMarker || value instanceof FCall || value instanceof CName || value instanceof RawString) | |
| 164 | + return true; | |
| 165 | + if (value instanceof Collection) | |
| 166 | + for (Object elt : (Collection)value) | |
| 167 | + if (containsSpecialValue(elt)) | |
| 168 | + return true; | |
| 169 | + if (value instanceof Map) | |
| 170 | + for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet()) | |
| 171 | + if (containsSpecialValue(entry.getKey()) || containsSpecialValue(entry.getValue())) | |
| 172 | + return true; | |
| 118 | 173 | return false; |
| 119 | 174 | } |
| 120 | 175 | |
| 176 | + /** | |
| 177 | + * Return true if the given value is likely to find a suitable codec | |
| 178 | + * to be serialized as a query parameter. | |
| 179 | + * If the value is not serializable, it must be included in the query string. | |
| 180 | + * Non serializable values include special values such as function calls, | |
| 181 | + * column names and bind markers, and collections thereof. | |
| 182 | + * We also don't serialize fixed size number types. The reason is that if we do it, we will | |
| 183 | + * force a particular size (4 bytes for ints, ...) and for the query builder, we don't want | |
| 184 | + * users to have to bother with that. | |
| 185 | + * | |
| 186 | + * @param value the value to inspect. | |
| 187 | + * @return true if the value is serializable, false otherwise. | |
| 188 | + */ | |
| 189 | + static boolean isSerializable(Object value) { | |
| 190 | + if (containsSpecialValue(value)) | |
| 191 | + return false; | |
| 192 | + if (value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal)) | |
| 193 | + return false; | |
| 194 | + if (value instanceof Collection) | |
| 195 | + for (Object elt : (Collection)value) | |
| 196 | + if (!isSerializable(elt)) | |
| 197 | + return false; | |
| 198 | + if (value instanceof Map) | |
| 199 | + for (Map.Entry<?,?> entry : ((Map<?,?>)value).entrySet()) | |
| 200 | + if (!isSerializable(entry.getKey()) || !isSerializable(entry.getValue())) | |
| 201 | + return false; | |
| 202 | + return true; | |
| 203 | + } | |
| 204 | + | |
| 121 | 205 | static boolean isIdempotent(Object value) { |
| 122 | 206 | if(value == null) { |
| 123 | 207 | return true; |
| @@ -143,13 +227,6 @@ | ||
| 143 | 227 | return true; |
| 144 | 228 | } |
| 145 | 229 | |
| 146 | - static boolean isRawValue(Object value) { | |
| 147 | - return value != null | |
| 148 | - && !(value instanceof FCall) | |
| 149 | - && !(value instanceof CName) | |
| 150 | - && !(value instanceof BindMarker); | |
| 151 | - } | |
| 152 | - | |
| 153 | 230 | static StringBuilder appendName(String name, StringBuilder sb) { |
| 154 | 231 | name = name.trim(); |
| 155 | 232 | // FIXME: checking for token( specifically is uber ugly, we'll need some better solution. |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +2 −2
| @@ -89,11 +89,11 @@ | ||
| 89 | 89 | @Test(groups = "short") |
| 90 | 90 | public void prepareTest() throws Exception { |
| 91 | 91 | // Just check we correctly avoid values when there is a bind marker |
| 92 | - String query = "INSERT INTO foo(a,b,c,d) VALUES ('foo','bar',?,0);"; | |
| 92 | + String query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',?,0);"; | |
| 93 | 93 | BuiltStatement stmt = builder.insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0); |
| 94 | 94 | assertEquals(stmt.getQueryString(), query); |
| 95 | 95 | |
| 96 | - query = "INSERT INTO foo(a,b,c,d) VALUES ('foo','bar',:c,0);"; | |
| 96 | + query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',:c,0);"; | |
| 97 | 97 | stmt = builder.insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker("c")).value("d", 0); |
| 98 | 98 | assertEquals(stmt.getQueryString(), query); |
| 99 | 99 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +3 −3
| @@ -143,17 +143,17 @@ | ||
| 143 | 143 | String query; |
| 144 | 144 | Statement insert; |
| 145 | 145 | |
| 146 | - query = "INSERT INTO foo(a) VALUES ('123); --comment');"; | |
| 146 | + query = "INSERT INTO foo (a) VALUES ('123); --comment');"; | |
| 147 | 147 | insert = builder.insertInto("foo") |
| 148 | 148 | .value("a", "123); --comment"); |
| 149 | 149 | assertEquals(insert.toString(), query); |
| 150 | 150 | |
| 151 | - query = "INSERT INTO foo(\"a,b\") VALUES (123);"; | |
| 151 | + query = "INSERT INTO foo (\"a,b\") VALUES (123);"; | |
| 152 | 152 | insert = builder.insertInto("foo") |
| 153 | 153 | .value("a,b", 123); |
| 154 | 154 | assertEquals(insert.toString(), query); |
| 155 | 155 | |
| 156 | - query = "INSERT INTO foo(a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 156 | + query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 157 | 157 | insert = builder.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)); |
| 158 | 158 | assertEquals(insert.toString(), query); |
| 159 | 159 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +2 −2
| @@ -107,7 +107,7 @@ | ||
| 107 | 107 | query = builder.select().from(table).where(eq("k", 42)); |
| 108 | 108 | |
| 109 | 109 | batch_query = "BEGIN BATCH "; |
| 110 | - batch_query += String.format("INSERT INTO %s.test_int(k,a) VALUES (42,1);", keyspace); | |
| 110 | + batch_query += String.format("INSERT INTO %s.test_int (k,a) VALUES (42,1);", keyspace); | |
| 111 | 111 | batch_query += String.format("UPDATE %s.test_int USING TTL 400;", keyspace); |
| 112 | 112 | batch_query += "APPLY BATCH;"; |
| 113 | 113 | batch = builder.batch() |
| @@ -134,7 +134,7 @@ | ||
| 134 | 134 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 135 | 135 | |
| 136 | 136 | batch_query = "BEGIN BATCH USING TIMESTAMP 42 "; |
| 137 | - batch_query += "INSERT INTO foo.bar(a) VALUES (123);"; | |
| 137 | + batch_query += "INSERT INTO foo.bar (a) VALUES (123);"; | |
| 138 | 138 | batch_query += "APPLY BATCH;"; |
| 139 | 139 | batch = builder.batch().using(timestamp(42)).add(builder.insertInto("foo", "bar").value("a", 123)); |
| 140 | 140 | assertEquals(batch.getRoutingKey(), null); |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +72 −19
| @@ -22,12 +22,11 @@ | ||
| 22 | 22 | |
| 23 | 23 | import com.google.common.collect.ImmutableList; |
| 24 | 24 | import com.google.common.collect.ImmutableMap; |
| 25 | -import org.testng.annotations.BeforeMethod; | |
| 25 | +import com.google.common.collect.Lists; | |
| 26 | +import com.google.common.collect.Sets; | |
| 26 | 27 | import org.testng.annotations.Test; |
| 27 | 28 | |
| 28 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | -import static org.mockito.Mockito.mock; | |
| 30 | -import static org.mockito.Mockito.when; | |
| 31 | 30 | import static org.testng.Assert.assertEquals; |
| 32 | 31 | import static org.testng.Assert.assertTrue; |
| 33 | 32 | import static org.testng.Assert.fail; |
| @@ -187,7 +186,7 @@ | ||
| 187 | 186 | String query; |
| 188 | 187 | Statement insert; |
| 189 | 188 | |
| 190 | - 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;"; | |
| 189 | + 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;"; | |
| 191 | 190 | insert = builder.insertInto("foo") |
| 192 | 191 | .value("a", 123) |
| 193 | 192 | .value("b", InetAddress.getByName("127.0.0.1")) |
| @@ -199,13 +198,13 @@ | ||
| 199 | 198 | .using(timestamp(42)).and(ttl(24)); |
| 200 | 199 | assertEquals(insert.toString(), query); |
| 201 | 200 | |
| 202 | - query = "INSERT INTO foo(a,b) VALUES (2,null);"; | |
| 201 | + query = "INSERT INTO foo (a,b) VALUES (2,null);"; | |
| 203 | 202 | insert = builder.insertInto("foo") |
| 204 | 203 | .value("a", 2) |
| 205 | 204 | .value("b", null); |
| 206 | 205 | assertEquals(insert.toString(), query); |
| 207 | 206 | |
| 208 | - query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 207 | + query = "INSERT INTO foo (a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 209 | 208 | insert = builder.insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 210 | 209 | add(2); |
| 211 | 210 | add(3); |
| @@ -213,7 +212,7 @@ | ||
| 213 | 212 | }}, 3.4 }).using(ttl(24)).and(timestamp(42)); |
| 214 | 213 | assertEquals(insert.toString(), query); |
| 215 | 214 | |
| 216 | - query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; | |
| 215 | + query = "INSERT INTO foo.bar (a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; | |
| 217 | 216 | insert = builder.insertInto("foo", "bar") |
| 218 | 217 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 219 | 218 | add(2); |
| @@ -225,7 +224,7 @@ | ||
| 225 | 224 | assertEquals(insert.toString(), query); |
| 226 | 225 | |
| 227 | 226 | // commutative result of TIMESTAMP |
| 228 | - query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; | |
| 227 | + query = "INSERT INTO foo.bar (a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; | |
| 229 | 228 | insert = builder.insertInto("foo", "bar") |
| 230 | 229 | .using(timestamp(42)) |
| 231 | 230 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| @@ -237,7 +236,7 @@ | ||
| 237 | 236 | assertEquals(insert.toString(), query); |
| 238 | 237 | |
| 239 | 238 | // commutative result of value() and values() |
| 240 | - query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;"; | |
| 239 | + query = "INSERT INTO foo (c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;"; | |
| 241 | 240 | insert = builder.insertInto("foo") |
| 242 | 241 | .using(timestamp(42)) |
| 243 | 242 | .value("c", 123) |
| @@ -256,7 +255,7 @@ | ||
| 256 | 255 | } |
| 257 | 256 | |
| 258 | 257 | // CAS test |
| 259 | - query = "INSERT INTO foo(k,x) VALUES (0,1) IF NOT EXISTS;"; | |
| 258 | + query = "INSERT INTO foo (k,x) VALUES (0,1) IF NOT EXISTS;"; | |
| 260 | 259 | insert = builder.insertInto("foo").value("k", 0).value("x", 1).ifNotExists(); |
| 261 | 260 | assertEquals(insert.toString(), query); |
| 262 | 261 | |
| @@ -416,7 +415,7 @@ | ||
| 416 | 415 | Statement batch; |
| 417 | 416 | |
| 418 | 417 | query = "BEGIN BATCH USING TIMESTAMP 42 "; |
| 419 | - query += "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4);"; | |
| 418 | + query += "INSERT INTO foo (a,b) VALUES ({2,3,4},3.4);"; | |
| 420 | 419 | query += "UPDATE foo SET a[2]='foo',b=[3,2,1]+b,c=c-{'a'} WHERE k=2;"; |
| 421 | 420 | query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 422 | 421 | query += "APPLY BATCH;"; |
| @@ -526,7 +525,7 @@ | ||
| 526 | 525 | String query; |
| 527 | 526 | Statement insert; |
| 528 | 527 | |
| 529 | - query = "INSERT INTO test(k,c) VALUES (0,?);"; | |
| 528 | + query = "INSERT INTO test (k,c) VALUES (0,?);"; | |
| 530 | 529 | insert = builder.insertInto("test") |
| 531 | 530 | .value("k", 0) |
| 532 | 531 | .value("c", bindMarker()); |
| @@ -619,15 +618,15 @@ | ||
| 619 | 618 | String query; |
| 620 | 619 | Statement insert; |
| 621 | 620 | |
| 622 | - query = "INSERT INTO foo(a) VALUES ('123); --comment');"; | |
| 621 | + query = "INSERT INTO foo (a) VALUES ('123); --comment');"; | |
| 623 | 622 | insert = builder.insertInto("foo").value("a", "123); --comment"); |
| 624 | 623 | assertEquals(insert.toString(), query); |
| 625 | 624 | |
| 626 | - query = "INSERT INTO foo(\"a,b\") VALUES (123);"; | |
| 625 | + query = "INSERT INTO foo (\"a,b\") VALUES (123);"; | |
| 627 | 626 | insert = builder.insertInto("foo").value("a,b", 123); |
| 628 | 627 | assertEquals(insert.toString(), query); |
| 629 | 628 | |
| 630 | - query = "INSERT INTO foo(a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 629 | + query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; | |
| 631 | 630 | insert = builder.insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<String>() {{ |
| 632 | 631 | add("2'} space"); |
| 633 | 632 | add("3"); |
| @@ -741,13 +740,13 @@ | ||
| 741 | 740 | "SELECT * FROM \"Metrics\".\"epochs\";"); |
| 742 | 741 | |
| 743 | 742 | assertEquals(builder.insertInto("Metrics", "epochs").toString(), |
| 744 | - "INSERT INTO Metrics.epochs() VALUES ();"); | |
| 743 | + "INSERT INTO Metrics.epochs () VALUES ();"); | |
| 745 | 744 | assertEquals(builder.insertInto("Metrics", quote("epochs")).toString(), |
| 746 | - "INSERT INTO Metrics.\"epochs\"() VALUES ();"); | |
| 745 | + "INSERT INTO Metrics.\"epochs\" () VALUES ();"); | |
| 747 | 746 | assertEquals(builder.insertInto(quote("Metrics"), "epochs").toString(), |
| 748 | - "INSERT INTO \"Metrics\".epochs() VALUES ();"); | |
| 747 | + "INSERT INTO \"Metrics\".epochs () VALUES ();"); | |
| 749 | 748 | assertEquals(builder.insertInto(quote("Metrics"), quote("epochs")).toString(), |
| 750 | - "INSERT INTO \"Metrics\".\"epochs\"() VALUES ();"); | |
| 749 | + "INSERT INTO \"Metrics\".\"epochs\" () VALUES ();"); | |
| 751 | 750 | } |
| 752 | 751 | |
| 753 | 752 | @Test(groups = "unit") |
| @@ -885,4 +884,58 @@ | ||
| 885 | 884 | assertThat(actual).isSameAs(expected); |
| 886 | 885 | } |
| 887 | 886 | |
| 887 | + @Test(groups = "unit") | |
| 888 | + @CassandraVersion(major = 2.1) | |
| 889 | + public void should_serialize_collections_of_serializable_elements() { | |
| 890 | + Set<UUID> set = Sets.newHashSet(UUID.randomUUID()); | |
| 891 | + List<Date> list = Lists.newArrayList(new Date()); | |
| 892 | + Map<BigInteger, String> map = ImmutableMap.of(new BigInteger("1"), "foo"); | |
| 893 | + BuiltStatement query = builder.insertInto("foo").value("v", set); | |
| 894 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); | |
| 895 | + assertThat(query.getObject(0)).isEqualTo(set); | |
| 896 | + query = builder.insertInto("foo").value("v", list); | |
| 897 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); | |
| 898 | + assertThat(query.getObject(0)).isEqualTo(list); | |
| 899 | + query = builder.insertInto("foo").value("v", map); | |
| 900 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); | |
| 901 | + assertThat(query.getObject(0)).isEqualTo(map); | |
| 902 | + } | |
| 903 | + | |
| 904 | + @Test(groups = "unit") | |
| 905 | + @CassandraVersion(major = 2.1) | |
| 906 | + public void should_not_attempt_to_serialize_function_calls_in_collections() { | |
| 907 | + BuiltStatement query = builder.insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); | |
| 908 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});"); | |
| 909 | + assertThat(query.getValues()).isNullOrEmpty(); | |
| 910 | + } | |
| 911 | + | |
| 912 | + @Test(groups = "unit") | |
| 913 | + @CassandraVersion(major = 2.1) | |
| 914 | + public void should_not_attempt_to_serialize_raw_values_in_collections() { | |
| 915 | + BuiltStatement query = builder.insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); | |
| 916 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});"); | |
| 917 | + assertThat(query.getValues()).isNullOrEmpty(); | |
| 918 | + } | |
| 919 | + | |
| 920 | + @Test(groups = "unit") | |
| 921 | + @CassandraVersion(major = 2.1) | |
| 922 | + public void should_not_attempt_to_serialize_collections_containing_numbers() { | |
| 923 | + BuiltStatement query; | |
| 924 | + // lists | |
| 925 | + List<Integer> list = Lists.newArrayList(1, 2, 3); | |
| 926 | + query = builder.insertInto("foo").value("v", list); | |
| 927 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,3]);"); | |
| 928 | + assertThat(query.hasValues()).isFalse(); | |
| 929 | + // sets | |
| 930 | + Set<Integer> set = Sets.newHashSet(1, 2, 3); | |
| 931 | + query = builder.insertInto("foo").value("v", set); | |
| 932 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1,2,3});"); | |
| 933 | + assertThat(query.hasValues()).isFalse(); | |
| 934 | + // maps | |
| 935 | + Map<Integer, Float> map = ImmutableMap.of(1, 12.34f); | |
| 936 | + query = builder.insertInto("foo").value("v", map); | |
| 937 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:12.34});"); | |
| 938 | + assertThat(query.hasValues()).isFalse(); | |
| 939 | + } | |
| 940 | + | |
| 888 | 941 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTupleExecutionTest.java +1 −1
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | |
| 45 | 45 | @Test(groups = "short") |
| 46 | 46 | public void should_handle_tuple() throws Exception { |
| 47 | - String query = "INSERT INTO foo(k,x) VALUES (0,(1));"; | |
| 47 | + String query = "INSERT INTO foo (k,x) VALUES (0,(1));"; | |
| 48 | 48 | TupleType tupleType = cluster.getMetadata().newTupleType(cint()); |
| 49 | 49 | BuiltStatement insert = new QueryBuilder(cluster).insertInto("foo").value("k", 0).value("x", tupleType.newValue(1)); |
| 50 | 50 | assertEquals(insert.toString(), query); |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderUDTExecutionTest.java +2 −2
| @@ -49,7 +49,7 @@ | ||
| 49 | 49 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 50 | 50 | |
| 51 | 51 | Statement insert = new QueryBuilder(cluster).insertInto("udtTest").value("k", 1).value("t", udtValue); |
| 52 | - assertEquals(insert.toString(), "INSERT INTO udtTest(k,t) VALUES (1,{i:2,a:'127.0.0.1'});"); | |
| 52 | + assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) VALUES (1,{i:2,a:'127.0.0.1'});"); | |
| 53 | 53 | |
| 54 | 54 | session.execute(insert); |
| 55 | 55 | |
| @@ -68,7 +68,7 @@ | ||
| 68 | 68 | UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); |
| 69 | 69 | |
| 70 | 70 | Statement insert = new QueryBuilder(cluster).insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); |
| 71 | - assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest(k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);"); | |
| 71 | + assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);"); | |
| 72 | 72 | |
| 73 | 73 | session.execute(insert); |
| 74 | 74 |