Elffuss BraidHub

kikocis/java-driver public

JAVA-712 : QueryBuilder.quote() applied duplicate double quotes
Changeset9e939f5989a5802e2610241297623b9ac9a0b3bced2f5c398c82858c13d486cd
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-29 11:40 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-09 14:15 +02:00
Parents69d85ad116db

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +1 −5
@@ -226,11 +226,7 @@
226226 * @return the quoted column name.
227227 */
228228 public static String quote(String columnName) {
229- StringBuilder sb = new StringBuilder();
230- sb.append('"');
231- Utils.appendName(columnName, sb);
232- sb.append('"');
233- return sb.toString();
229+ return '"' + columnName + '"';
234230 }
235231
236232 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +79 −36
@@ -25,10 +25,11 @@
2525 import org.testng.annotations.Test;
2626
2727 import static org.assertj.core.api.Assertions.assertThat;
28-import static org.testng.Assert.*;
28+import static org.testng.Assert.assertEquals;
29+import static org.testng.Assert.assertTrue;
30+import static org.testng.Assert.fail;
2931
3032 import com.datastax.driver.core.*;
31-import com.datastax.driver.core.querybuilder.Delete.Where;
3233 import com.datastax.driver.core.utils.Bytes;
3334 import com.datastax.driver.core.utils.CassandraVersion;
3435
@@ -53,10 +54,10 @@
5354
5455 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;";
5556 select = select("a", "b", quote("C")).from("foo")
56- .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3")))
57- .and(eq(quote("C"), "foo"))
58- .orderBy(asc("a"), desc("b"))
59- .limit(42);
57+ .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3")))
58+ .and(eq(quote("C"), "foo"))
59+ .orderBy(asc("a"), desc("b"))
60+ .limit(42);
6061 assertEquals(select.toString(), query);
6162
6263 query = "SELECT writetime(a),ttl(a) FROM foo ALLOW FILTERING;";
@@ -166,48 +167,67 @@
166167
167168 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;";
168169 insert = insertInto("foo")
169- .value("a", 123)
170- .value("b", InetAddress.getByName("127.0.0.1"))
171- .value(quote("C"), "foo'bar")
172- .value("d", new TreeMap<String, Integer>(){{ put("x", 3); put("y", 2); }})
173- .using(timestamp(42)).and(ttl(24));
170+ .value("a", 123)
171+ .value("b", InetAddress.getByName("127.0.0.1"))
172+ .value(quote("C"), "foo'bar")
173+ .value("d", new TreeMap<String, Integer>() {{
174+ put("x", 3);
175+ put("y", 2);
176+ }})
177+ .using(timestamp(42)).and(ttl(24));
174178 assertEquals(insert.toString(), query);
175179
176180 query = "INSERT INTO foo(a,b) VALUES (2,null);";
177181 insert = insertInto("foo")
178- .value("a", 2)
179- .value("b", null);
182+ .value("a", 2)
183+ .value("b", null);
180184 assertEquals(insert.toString(), query);
181185
182186 query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;";
183- 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));
187+ insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
188+ add(2);
189+ add(3);
190+ add(4);
191+ }}, 3.4 }).using(ttl(24)).and(timestamp(42));
184192 assertEquals(insert.toString(), query);
185193
186194 query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;";
187195 insert = insertInto("foo", "bar")
188- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
189- .using(ttl(bindMarker()))
190- .and(timestamp(bindMarker()));
196+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
197+ add(2);
198+ add(3);
199+ add(4);
200+ }}, 3.4 })
201+ .using(ttl(bindMarker()))
202+ .and(timestamp(bindMarker()));
191203 assertEquals(insert.toString(), query);
192204
193205 // commutative result of TIMESTAMP
194206 query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;";
195207 insert = insertInto("foo", "bar")
196- .using(timestamp(42))
197- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 })
198- .value("c", 123);
208+ .using(timestamp(42))
209+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
210+ add(2);
211+ add(3);
212+ add(4);
213+ }}, 3.4 })
214+ .value("c", 123);
199215 assertEquals(insert.toString(), query);
200216
201217 // commutative result of value() and values()
202218 query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;";
203219 insert = insertInto("foo")
204- .using(timestamp(42))
205- .value("c", 123)
206- .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 });
220+ .using(timestamp(42))
221+ .value("c", 123)
222+ .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
223+ add(2);
224+ add(3);
225+ add(4);
226+ }}, 3.4 });
207227 assertEquals(insert.toString(), query);
208228
209229 try {
210- insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ 1, 2, 3 });
230+ insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ 1, 2, 3 });
211231 fail();
212232 } catch (IllegalArgumentException e) {
213233 assertEquals(e.getMessage(), "Got 2 names but 3 values");
@@ -249,20 +269,31 @@
249269 assertEquals(update.toString(), query);
250270
251271 query = "UPDATE foo SET b=b-[1,2,3],c=c+{1},d=d+{2,3,4};";
252- 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); }}));
272+ update = update("foo").with(discardAll("b", Arrays.asList(1, 2, 3))).and(add("c", 1)).and(addAll("d", new TreeSet<Integer>() {{
273+ add(2);
274+ add(3);
275+ add(4);
276+ }}));
253277 assertEquals(update.toString(), query);
254278
255279 query = "UPDATE foo SET b=b-{2,3,4},c['k']='v',d=d+{'x':3,'y':2};";
256- update = update("foo").with(removeAll("b", new TreeSet<Integer>(){{ add(2); add(3); add(4); }}))
257- .and(put("c", "k", "v"))
258- .and(putAll("d", new TreeMap<String, Integer>(){{ put("x", 3); put("y", 2); }}));
280+ update = update("foo").with(removeAll("b", new TreeSet<Integer>() {{
281+ add(2);
282+ add(3);
283+ add(4);
284+ }}))
285+ .and(put("c", "k", "v"))
286+ .and(putAll("d", new TreeMap<String, Integer>() {{
287+ put("x", 3);
288+ put("y", 2);
289+ }}));
259290 assertEquals(update.toString(), query);
260291
261292 query = "UPDATE foo USING TTL 400;";
262293 update = update("foo").using(ttl(400));
263294 assertEquals(update.toString(), query);
264295
265- query = "UPDATE foo SET a="+new BigDecimal(3.2)+",b=42 WHERE k=2;";
296+ query = "UPDATE foo SET a=" + new BigDecimal(3.2) + ",b=42 WHERE k=2;";
266297 update = update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2));
267298 assertEquals(update.toString(), query);
268299
@@ -363,7 +394,11 @@
363394 query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;";
364395 query += "APPLY BATCH;";
365396 batch = batch()
366- .add(insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }))
397+ .add(insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{
398+ add(2);
399+ add(3);
400+ add(4);
401+ }}, 3.4 }))
367402 .add(update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2)))
368403 .add(delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1)))
369404 .using(timestamp(42));
@@ -450,7 +485,7 @@
450485 assertEquals(batch.toString(), query);
451486 }
452487
453- @Test(groups = "unit", expectedExceptions={IllegalArgumentException.class})
488+ @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })
454489 public void batchMixedCounterTest() throws Exception {
455490 String query;
456491 Statement batch;
@@ -469,8 +504,8 @@
469504
470505 query = "INSERT INTO test(k,c) VALUES (0,?);";
471506 insert = insertInto("test")
472- .value("k", 0)
473- .value("c", bindMarker());
507+ .value("k", 0)
508+ .value("c", bindMarker());
474509 assertEquals(insert.toString(), query);
475510 }
476511
@@ -497,7 +532,6 @@
497532 assertEquals(select.toString(), query);
498533 }
499534
500-
501535 @Test(groups = "unit")
502536 public void selectInjectionTests() throws Exception {
503537
@@ -629,7 +663,7 @@
629663
630664 query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');";
631665 delete = delete("a", "b").from("foo")
632- .where(in("a", "b", "c'); --comment"));
666+ .where(in("a", "b", "c'); --comment"));
633667 assertEquals(delete.toString(), query);
634668
635669 query = "DELETE FROM foo WHERE \"k=1 OR k\">42;";
@@ -703,7 +737,7 @@
703737
704738 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>=('a',2) AND (c1,c2)<('b',0);";
705739 select = select().all().from("foo").where(eq("k", 4)).and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2)))
706- .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0)));
740+ .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0)));
707741 assertEquals(select.toString(), query);
708742
709743 query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);";
@@ -769,4 +803,13 @@
769803 statement = update("foo").with(prependAll("l", list)).where(eq("k", 1));
770804 assertThat(statement.toString()).isEqualTo(query);
771805 }
806+
807+ @Test(groups = "unit")
808+ public void should_quote_complex_column_names() {
809+ // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name
810+ String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;";
811+ Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1));
812+
813+ assertThat(statement.toString()).isEqualTo(query);
814+ }
772815 }