kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9e939f5989a5802e2610241297623b9ac9a0b3bced2f5c398c82858c13d486cd |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-05-29 11:40 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-09 14:15 +02:00 |
| Parents | 69d85ad116db |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +1 −5
| @@ -226,11 +226,7 @@ | ||
| 226 | 226 | * @return the quoted column name. |
| 227 | 227 | */ |
| 228 | 228 | 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 + '"'; | |
| 234 | 230 | } |
| 235 | 231 | |
| 236 | 232 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +79 −36
| @@ -25,10 +25,11 @@ | ||
| 25 | 25 | import org.testng.annotations.Test; |
| 26 | 26 | |
| 27 | 27 | 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; | |
| 29 | 31 | |
| 30 | 32 | import com.datastax.driver.core.*; |
| 31 | -import com.datastax.driver.core.querybuilder.Delete.Where; | |
| 32 | 33 | import com.datastax.driver.core.utils.Bytes; |
| 33 | 34 | import com.datastax.driver.core.utils.CassandraVersion; |
| 34 | 35 | |
| @@ -53,10 +54,10 @@ | ||
| 53 | 54 | |
| 54 | 55 | 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;"; |
| 55 | 56 | 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); | |
| 60 | 61 | assertEquals(select.toString(), query); |
| 61 | 62 | |
| 62 | 63 | query = "SELECT writetime(a),ttl(a) FROM foo ALLOW FILTERING;"; |
| @@ -166,48 +167,67 @@ | ||
| 166 | 167 | |
| 167 | 168 | 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;"; |
| 168 | 169 | 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)); | |
| 174 | 178 | assertEquals(insert.toString(), query); |
| 175 | 179 | |
| 176 | 180 | query = "INSERT INTO foo(a,b) VALUES (2,null);"; |
| 177 | 181 | insert = insertInto("foo") |
| 178 | - .value("a", 2) | |
| 179 | - .value("b", null); | |
| 182 | + .value("a", 2) | |
| 183 | + .value("b", null); | |
| 180 | 184 | assertEquals(insert.toString(), query); |
| 181 | 185 | |
| 182 | 186 | 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)); | |
| 184 | 192 | assertEquals(insert.toString(), query); |
| 185 | 193 | |
| 186 | 194 | query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; |
| 187 | 195 | 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())); | |
| 191 | 203 | assertEquals(insert.toString(), query); |
| 192 | 204 | |
| 193 | 205 | // commutative result of TIMESTAMP |
| 194 | 206 | query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; |
| 195 | 207 | 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); | |
| 199 | 215 | assertEquals(insert.toString(), query); |
| 200 | 216 | |
| 201 | 217 | // commutative result of value() and values() |
| 202 | 218 | query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;"; |
| 203 | 219 | 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 }); | |
| 207 | 227 | assertEquals(insert.toString(), query); |
| 208 | 228 | |
| 209 | 229 | 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 }); | |
| 211 | 231 | fail(); |
| 212 | 232 | } catch (IllegalArgumentException e) { |
| 213 | 233 | assertEquals(e.getMessage(), "Got 2 names but 3 values"); |
| @@ -249,20 +269,31 @@ | ||
| 249 | 269 | assertEquals(update.toString(), query); |
| 250 | 270 | |
| 251 | 271 | 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 | + }})); | |
| 253 | 277 | assertEquals(update.toString(), query); |
| 254 | 278 | |
| 255 | 279 | 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 | + }})); | |
| 259 | 290 | assertEquals(update.toString(), query); |
| 260 | 291 | |
| 261 | 292 | query = "UPDATE foo USING TTL 400;"; |
| 262 | 293 | update = update("foo").using(ttl(400)); |
| 263 | 294 | assertEquals(update.toString(), query); |
| 264 | 295 | |
| 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;"; | |
| 266 | 297 | update = update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2)); |
| 267 | 298 | assertEquals(update.toString(), query); |
| 268 | 299 | |
| @@ -363,7 +394,11 @@ | ||
| 363 | 394 | query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 364 | 395 | query += "APPLY BATCH;"; |
| 365 | 396 | 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 })) | |
| 367 | 402 | .add(update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2))) |
| 368 | 403 | .add(delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1))) |
| 369 | 404 | .using(timestamp(42)); |
| @@ -450,7 +485,7 @@ | ||
| 450 | 485 | assertEquals(batch.toString(), query); |
| 451 | 486 | } |
| 452 | 487 | |
| 453 | - @Test(groups = "unit", expectedExceptions={IllegalArgumentException.class}) | |
| 488 | + @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) | |
| 454 | 489 | public void batchMixedCounterTest() throws Exception { |
| 455 | 490 | String query; |
| 456 | 491 | Statement batch; |
| @@ -469,8 +504,8 @@ | ||
| 469 | 504 | |
| 470 | 505 | query = "INSERT INTO test(k,c) VALUES (0,?);"; |
| 471 | 506 | insert = insertInto("test") |
| 472 | - .value("k", 0) | |
| 473 | - .value("c", bindMarker()); | |
| 507 | + .value("k", 0) | |
| 508 | + .value("c", bindMarker()); | |
| 474 | 509 | assertEquals(insert.toString(), query); |
| 475 | 510 | } |
| 476 | 511 | |
| @@ -497,7 +532,6 @@ | ||
| 497 | 532 | assertEquals(select.toString(), query); |
| 498 | 533 | } |
| 499 | 534 | |
| 500 | - | |
| 501 | 535 | @Test(groups = "unit") |
| 502 | 536 | public void selectInjectionTests() throws Exception { |
| 503 | 537 | |
| @@ -629,7 +663,7 @@ | ||
| 629 | 663 | |
| 630 | 664 | query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 631 | 665 | delete = delete("a", "b").from("foo") |
| 632 | - .where(in("a", "b", "c'); --comment")); | |
| 666 | + .where(in("a", "b", "c'); --comment")); | |
| 633 | 667 | assertEquals(delete.toString(), query); |
| 634 | 668 | |
| 635 | 669 | query = "DELETE FROM foo WHERE \"k=1 OR k\">42;"; |
| @@ -703,7 +737,7 @@ | ||
| 703 | 737 | |
| 704 | 738 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>=('a',2) AND (c1,c2)<('b',0);"; |
| 705 | 739 | 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))); | |
| 707 | 741 | assertEquals(select.toString(), query); |
| 708 | 742 | |
| 709 | 743 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);"; |
| @@ -769,4 +803,13 @@ | ||
| 769 | 803 | statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); |
| 770 | 804 | assertThat(statement.toString()).isEqualTo(query); |
| 771 | 805 | } |
| 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 | + } | |
| 772 | 815 | } |