Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.1' into java736

Conflicts:
	driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java
Changeset03c6ab42daf56b2c91b4d31ec317981017fce88acc8fad1c3ccb46fd16348c25
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-11 15:29 +02:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-11 15:29 +02:00
Parents39b978b2db6d, 4c6dd1e29c05

Browse the files at this changeset

5 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +1 −5
@@ -227,11 +227,7 @@
227227 * @return the quoted column name.
228228 */
229229 public static String quote(String columnName) {
230- StringBuilder sb = new StringBuilder();
231- sb.append('"');
232- Utils.appendName(columnName, sb);
233- sb.append('"');
234- return sb.toString();
230+ return '"' + columnName + '"';
235231 }
236232
237233 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +4 −1
@@ -59,11 +59,14 @@
5959 return sb;
6060 }
6161
62- // Returns null if it's not really serializable (function call, bind markers, ...)
62+ // Returns false if it's not really serializable (function call, bind markers, ...)
6363 static boolean isSerializable(Object value) {
6464 if (value instanceof BindMarker || value instanceof FCall || value instanceof CName)
6565 return false;
6666
67+ if (value instanceof RawString)
68+ return false;
69+
6770 // We also don't serialize fixed size number types. The reason is that if we do it, we will
6871 // force a particular size (4 bytes for ints, ...) and for the query builder, we don't want
6972 // users to have to bother with that.
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +94 −46
@@ -55,10 +55,10 @@
5555
5656 query = "SELECT a,b,\"C\" FROM foo WHERE a IN ('127.0.0.1','127.0.0.3') AND \"C\"='foo' ORDER BY a ASC,b DESC LIMIT 42;";
5757 select = select("a", "b", quote("C")).from("foo")
58- .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3")))
59- .and(eq(quote("C"), "foo"))
60- .orderBy(asc("a"), desc("b"))
61- .limit(42);
58+ .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3")))
59+ .and(eq(quote("C"), "foo"))
60+ .orderBy(asc("a"), desc("b"))
61+ .limit(42);
6262 assertEquals(select.toString(), query);
6363
6464 query = "SELECT writetime(a),ttl(a) FROM foo ALLOW FILTERING;";
@@ -124,35 +124,35 @@
124124 assertEquals(select.toString(), query);
125125
126126 try {
127- select = select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b"));
127+ select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b"));
128128 fail();
129129 } catch (IllegalStateException e) {
130130 assertEquals(e.getMessage(), "An ORDER BY clause has already been provided");
131131 }
132132
133133 try {
134- select = select().column("a").all().from("foo");
134+ select().column("a").all().from("foo");
135135 fail();
136136 } catch (IllegalStateException e) {
137137 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
138138 }
139139
140140 try {
141- select = select().column("a").countAll().from("foo");
141+ select().column("a").countAll().from("foo");
142142 fail();
143143 } catch (IllegalStateException e) {
144144 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
145145 }
146146
147147 try {
148- select = select().all().from("foo").limit(-42);
148+ select().all().from("foo").limit(-42);
149149 fail();
150150 } catch (IllegalArgumentException e) {
151151 assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive");
152152 }
153153
154154 try {
155- select = select().all().from("foo").limit(42).limit(42);
155+ select().all().from("foo").limit(42).limit(42);
156156 fail();
157157 } catch (IllegalStateException e) {
158158 assertEquals(e.getMessage(), "A LIMIT value has already been provided");
@@ -168,48 +168,67 @@
168168
169169 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;";
170170 insert = insertInto("foo")
171- .value("a", 123)
172- .value("b", InetAddress.getByName("127.0.0.1"))
173- .value(quote("C"), "foo'bar")
174- .value("d", new TreeMap<String, Integer>(){{ put("x", 3); put("y", 2); }})
175- .using(timestamp(42)).and(ttl(24));
171+ .value("a", 123)
172+ .value("b", InetAddress.getByName("127.0.0.1"))
173+ .value(quote("C"), "foo'bar")
174+ .value("d", new TreeMap<String, Integer>() {{
175+ put("x", 3);
176+ put("y", 2);
177+ }})
178+ .using(timestamp(42)).and(ttl(24));
176179 assertEquals(insert.toString(), query);
177180
178181 query = "INSERT INTO foo(a,b) VALUES (2,null);";
179182 insert = insertInto("foo")
180- .value("a", 2)
181- .value("b", null);
183+ .value("a", 2)
184+ .value("b", null);
182185 assertEquals(insert.toString(), query);
183186
184187 query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;";
185- insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }).using(ttl(24)).and(timestamp(42));
188+ insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
189+ add(2);
190+ add(3);
191+ add(4);
192+ }}, 3.4 }).using(ttl(24)).and(timestamp(42));
186193 assertEquals(insert.toString(), query);
187194
188195 query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;";
189196 insert = insertInto("foo", "bar")
190- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
191- .using(ttl(bindMarker()))
192- .and(timestamp(bindMarker()));
197+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
198+ add(2);
199+ add(3);
200+ add(4);
201+ }}, 3.4 })
202+ .using(ttl(bindMarker()))
203+ .and(timestamp(bindMarker()));
193204 assertEquals(insert.toString(), query);
194205
195206 // commutative result of TIMESTAMP
196207 query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;";
197208 insert = insertInto("foo", "bar")
198- .using(timestamp(42))
199- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
200- .value("c", 123);
209+ .using(timestamp(42))
210+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
211+ add(2);
212+ add(3);
213+ add(4);
214+ }}, 3.4 })
215+ .value("c", 123);
201216 assertEquals(insert.toString(), query);
202217
203218 // commutative result of value() and values()
204219 query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;";
205220 insert = insertInto("foo")
206- .using(timestamp(42))
207- .value("c", 123)
208- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 });
221+ .using(timestamp(42))
222+ .value("c", 123)
223+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
224+ add(2);
225+ add(3);
226+ add(4);
227+ }}, 3.4 });
209228 assertEquals(insert.toString(), query);
210229
211230 try {
212- insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ 1, 2, 3 });
231+ insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ 1, 2, 3 });
213232 fail();
214233 } catch (IllegalArgumentException e) {
215234 assertEquals(e.getMessage(), "Got 2 names but 3 values");
@@ -251,20 +270,31 @@
251270 assertEquals(update.toString(), query);
252271
253272 query = "UPDATE foo SET b=b-[1,2,3],c=c+{1},d=d+{2,3,4};";
254- update = update("foo").with(discardAll("b", Arrays.asList(1, 2, 3))).and(add("c", 1)).and(addAll("d", new TreeSet<Integer>(){{ add(2); add(3); add(4); }}));
273+ update = update("foo").with(discardAll("b", Arrays.asList(1, 2, 3))).and(add("c", 1)).and(addAll("d", new TreeSet<Integer>() {{
274+ add(2);
275+ add(3);
276+ add(4);
277+ }}));
255278 assertEquals(update.toString(), query);
256279
257280 query = "UPDATE foo SET b=b-{2,3,4},c['k']='v',d=d+{'x':3,'y':2};";
258- update = update("foo").with(removeAll("b", new TreeSet<Integer>(){{ add(2); add(3); add(4); }}))
259- .and(put("c", "k", "v"))
260- .and(putAll("d", new TreeMap<String, Integer>(){{ put("x", 3); put("y", 2); }}));
281+ update = update("foo").with(removeAll("b", new TreeSet<Integer>() {{
282+ add(2);
283+ add(3);
284+ add(4);
285+ }}))
286+ .and(put("c", "k", "v"))
287+ .and(putAll("d", new TreeMap<String, Integer>() {{
288+ put("x", 3);
289+ put("y", 2);
290+ }}));
261291 assertEquals(update.toString(), query);
262292
263293 query = "UPDATE foo USING TTL 400;";
264294 update = update("foo").using(ttl(400));
265295 assertEquals(update.toString(), query);
266296
267- query = "UPDATE foo SET a="+new BigDecimal(3.2)+",b=42 WHERE k=2;";
297+ query = "UPDATE foo SET a=" + new BigDecimal(3.2) + ",b=42 WHERE k=2;";
268298 update = update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2));
269299 assertEquals(update.toString(), query);
270300
@@ -281,7 +311,7 @@
281311 assertEquals(update.toString(), query);
282312
283313 try {
284- update = update("foo").using(ttl(-400));
314+ update("foo").using(ttl(-400));
285315 fail();
286316 } catch (IllegalArgumentException e) {
287317 assertEquals(e.getMessage(), "Invalid ttl, must be positive");
@@ -327,14 +357,14 @@
327357 assertEquals(delete.toString(), query);
328358
329359 try {
330- delete = delete().column("a").all().from("foo");
360+ delete().column("a").all().from("foo");
331361 fail();
332362 } catch (IllegalStateException e) {
333363 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
334364 }
335365
336366 try {
337- delete = delete().from("foo").using(timestamp(-1240003134L));
367+ delete().from("foo").using(timestamp(-1240003134L));
338368 fail();
339369 } catch (IllegalArgumentException e) {
340370 assertEquals(e.getMessage(), "Invalid timestamp, must be positive");
@@ -365,7 +395,11 @@
365395 query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;";
366396 query += "APPLY BATCH;";
367397 batch = batch()
368- .add(insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }))
398+ .add(insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
399+ add(2);
400+ add(3);
401+ add(4);
402+ }}, 3.4 }))
369403 .add(update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2)))
370404 .add(delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1)))
371405 .using(timestamp(42));
@@ -452,12 +486,9 @@
452486 assertEquals(batch.toString(), query);
453487 }
454488
455- @Test(groups = "unit", expectedExceptions={IllegalArgumentException.class})
489+ @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })
456490 public void batchMixedCounterTest() throws Exception {
457- String query;
458- Statement batch;
459-
460- batch = batch()
491+ batch()
461492 .add(update("foo").with(incr("a", 1)))
462493 .add(update("foo").with(set("b", 2)))
463494 .add(update("foo").with(incr("c", 3)))
@@ -471,8 +502,8 @@
471502
472503 query = "INSERT INTO test(k,c) VALUES (0,?);";
473504 insert = insertInto("test")
474- .value("k", 0)
475- .value("c", bindMarker());
505+ .value("k", 0)
506+ .value("c", bindMarker());
476507 assertEquals(insert.toString(), query);
477508 }
478509
@@ -499,7 +530,6 @@
499530 assertEquals(select.toString(), query);
500531 }
501532
502-
503533 @Test(groups = "unit")
504534 public void selectInjectionTests() throws Exception {
505535
@@ -631,7 +661,7 @@
631661
632662 query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');";
633663 delete = delete("a", "b").from("foo")
634- .where(in("a", "b", "c'); --comment"));
664+ .where(in("a", "b", "c'); --comment"));
635665 assertEquals(delete.toString(), query);
636666
637667 query = "DELETE FROM foo WHERE \"k=1 OR k\">42;";
@@ -660,6 +690,7 @@
660690 public void rejectUnknownValueTest() throws Exception {
661691
662692 try {
693+ //noinspection ResultOfMethodCallIgnored
663694 update("foo").with(set("a", new byte[13])).where(eq("k", 2)).toString();
664695 fail("Byte arrays should not be valid, ByteBuffer should be used instead");
665696 } catch (IllegalArgumentException e) {
@@ -705,7 +736,7 @@
705736
706737 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>=('a',2) AND (c1,c2)<('b',0);";
707738 select = select().all().from("foo").where(eq("k", 4)).and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)))
708- .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0)));
739+ .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0)));
709740 assertEquals(select.toString(), query);
710741
711742 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);";
@@ -790,5 +821,22 @@
790821 update("foo").with(append("l", bindMarker())).where(eq("k", 1));
791822 fail("Expected an exception");
792823 } catch (InvalidQueryException e) { /*expected*/ }
824+
825+ }
826+
827+ @Test(groups = "unit")
828+ public void should_quote_complex_column_names() {
829+ // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name
830+ String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;";
831+ Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1));
832+
833+ assertThat(statement.toString()).isEqualTo(query);
834+ }
835+
836+ @Test(groups = "unit")
837+ public void should_not_serialize_raw_query_values() {
838+ RegularStatement select = select().from("test").where(gt("i", raw("1")));
839+ assertThat(select.getQueryString()).doesNotContain("?");
840+ assertThat(select.getValues(ProtocolVersion.V3)).isNull();
793841 }
794842 }
modified driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Enumerated.java +1 −1
@@ -29,7 +29,7 @@
2929 * The optional {@link EnumType} value defined how the enumeration must be
3030 * persisted.
3131 */
32-@Target(ElementType.FIELD)
32+@Target(value = {ElementType.FIELD, ElementType.PARAMETER})
3333 @Retention(RetentionPolicy.RUNTIME)
3434 public @interface Enumerated {
3535 /**
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +90 −0
@@ -0,0 +1,90 @@
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.mapping;
17+
18+import java.util.Collection;
19+
20+import com.google.common.collect.Lists;
21+import org.testng.annotations.Test;
22+
23+import static org.assertj.core.api.Assertions.assertThat;
24+
25+import com.datastax.driver.core.CCMBridge;
26+import com.datastax.driver.mapping.annotations.*;
27+
28+public class MapperAccessorParamsTest extends CCMBridge.PerClassSingleNodeCluster {
29+ @Override
30+ protected Collection<String> getTableDefinitions() {
31+ return Lists.newArrayList("CREATE TABLE user ( key int primary key, gender int)",
32+ "CREATE INDEX on user(gender)");
33+
34+ }
35+
36+ @Test(groups = "short")
37+ void should_include_enumtype_in_accessor() {
38+ Mapper<User> userMapper = new MappingManager(session).mapper(User.class);
39+ userMapper.save(new User(1, 0));
40+ userMapper.save(new User(2, 1));
41+
42+ UserAccessor accessor = new MappingManager(session)
43+ .createAccessor(UserAccessor.class);
44+ assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(0);
45+ assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1);
46+
47+ }
48+
49+ enum Enum {
50+ MALE, FEMALE
51+ }
52+
53+ @Accessor
54+ public interface UserAccessor {
55+ @Query("select * from user where gender=:val")
56+ Result<User> getUser(@Param("val") @Enumerated(EnumType.ORDINAL) Enum value);
57+ }
58+
59+ @Table(name = "user")
60+ public static class User {
61+ @PartitionKey
62+ private int key;
63+
64+ private int gender;
65+
66+ public User() {
67+ }
68+
69+ public User(int k, int val) {
70+ this.key = k;
71+ this.gender = val;
72+ }
73+
74+ public int getKey() {
75+ return this.key;
76+ }
77+
78+ public void setKey(int pk) {
79+ this.key = pk;
80+ }
81+
82+ public int getGender() {
83+ return this.gender;
84+ }
85+
86+ public void setGender(int val) {
87+ this.gender = val;
88+ }
89+ }
90+}
No newline at the end of the file