kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 4c6dd1e29c05b358e4b539214c31e189cb3c113c01294ea06b9642b02085fd25 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-06-11 15:14 +02:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-11 15:18 +02:00 |
| Parents | 0fc7265fe63e, 3cc9d8815f07 |
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 +89 −49
| @@ -18,7 +18,6 @@ | ||
| 18 | 18 | import java.math.BigDecimal; |
| 19 | 19 | import java.math.BigInteger; |
| 20 | 20 | import java.net.InetAddress; |
| 21 | -import java.nio.ByteBuffer; | |
| 22 | 21 | import java.util.*; |
| 23 | 22 | |
| 24 | 23 | import com.google.common.collect.ImmutableList; |
| @@ -26,10 +25,11 @@ | ||
| 26 | 25 | import org.testng.annotations.Test; |
| 27 | 26 | |
| 28 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | -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; | |
| 30 | 31 | |
| 31 | 32 | import com.datastax.driver.core.*; |
| 32 | -import com.datastax.driver.core.querybuilder.Delete.Where; | |
| 33 | 33 | import com.datastax.driver.core.utils.Bytes; |
| 34 | 34 | import com.datastax.driver.core.utils.CassandraVersion; |
| 35 | 35 | |
| @@ -54,10 +54,10 @@ | ||
| 54 | 54 | |
| 55 | 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;"; |
| 56 | 56 | select = select("a", "b", quote("C")).from("foo") |
| 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); | |
| 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); | |
| 61 | 61 | assertEquals(select.toString(), query); |
| 62 | 62 | |
| 63 | 63 | query = "SELECT writetime(a),ttl(a) FROM foo ALLOW FILTERING;"; |
| @@ -123,35 +123,35 @@ | ||
| 123 | 123 | assertEquals(select.toString(), query); |
| 124 | 124 | |
| 125 | 125 | try { |
| 126 | - select = select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); | |
| 126 | + select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); | |
| 127 | 127 | fail(); |
| 128 | 128 | } catch (IllegalStateException e) { |
| 129 | 129 | assertEquals(e.getMessage(), "An ORDER BY clause has already been provided"); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | try { |
| 133 | - select = select().column("a").all().from("foo"); | |
| 133 | + select().column("a").all().from("foo"); | |
| 134 | 134 | fail(); |
| 135 | 135 | } catch (IllegalStateException e) { |
| 136 | 136 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | try { |
| 140 | - select = select().column("a").countAll().from("foo"); | |
| 140 | + select().column("a").countAll().from("foo"); | |
| 141 | 141 | fail(); |
| 142 | 142 | } catch (IllegalStateException e) { |
| 143 | 143 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | try { |
| 147 | - select = select().all().from("foo").limit(-42); | |
| 147 | + select().all().from("foo").limit(-42); | |
| 148 | 148 | fail(); |
| 149 | 149 | } catch (IllegalArgumentException e) { |
| 150 | 150 | assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive"); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | try { |
| 154 | - select = select().all().from("foo").limit(42).limit(42); | |
| 154 | + select().all().from("foo").limit(42).limit(42); | |
| 155 | 155 | fail(); |
| 156 | 156 | } catch (IllegalStateException e) { |
| 157 | 157 | assertEquals(e.getMessage(), "A LIMIT value has already been provided"); |
| @@ -167,48 +167,67 @@ | ||
| 167 | 167 | |
| 168 | 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;"; |
| 169 | 169 | insert = insertInto("foo") |
| 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>(){{ put("x", 3); put("y", 2); }}) | |
| 174 | - .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)); | |
| 175 | 178 | assertEquals(insert.toString(), query); |
| 176 | 179 | |
| 177 | 180 | query = "INSERT INTO foo(a,b) VALUES (2,null);"; |
| 178 | 181 | insert = insertInto("foo") |
| 179 | - .value("a", 2) | |
| 180 | - .value("b", null); | |
| 182 | + .value("a", 2) | |
| 183 | + .value("b", null); | |
| 181 | 184 | assertEquals(insert.toString(), query); |
| 182 | 185 | |
| 183 | 186 | query = "INSERT INTO foo(a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;"; |
| 184 | - 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)); | |
| 185 | 192 | assertEquals(insert.toString(), query); |
| 186 | 193 | |
| 187 | 194 | query = "INSERT INTO foo.bar(a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; |
| 188 | 195 | insert = insertInto("foo", "bar") |
| 189 | - .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }) | |
| 190 | - .using(ttl(bindMarker())) | |
| 191 | - .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())); | |
| 192 | 203 | assertEquals(insert.toString(), query); |
| 193 | 204 | |
| 194 | 205 | // commutative result of TIMESTAMP |
| 195 | 206 | query = "INSERT INTO foo.bar(a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; |
| 196 | 207 | insert = insertInto("foo", "bar") |
| 197 | - .using(timestamp(42)) | |
| 198 | - .values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<Integer>(){{ add(2); add(3); add(4); }}, 3.4 }) | |
| 199 | - .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); | |
| 200 | 215 | assertEquals(insert.toString(), query); |
| 201 | 216 | |
| 202 | 217 | // commutative result of value() and values() |
| 203 | 218 | query = "INSERT INTO foo(c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;"; |
| 204 | 219 | insert = insertInto("foo") |
| 205 | - .using(timestamp(42)) | |
| 206 | - .value("c", 123) | |
| 207 | - .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 }); | |
| 208 | 227 | assertEquals(insert.toString(), query); |
| 209 | 228 | |
| 210 | 229 | try { |
| 211 | - insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ 1, 2, 3 }); | |
| 230 | + insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ 1, 2, 3 }); | |
| 212 | 231 | fail(); |
| 213 | 232 | } catch (IllegalArgumentException e) { |
| 214 | 233 | assertEquals(e.getMessage(), "Got 2 names but 3 values"); |
| @@ -250,20 +269,31 @@ | ||
| 250 | 269 | assertEquals(update.toString(), query); |
| 251 | 270 | |
| 252 | 271 | query = "UPDATE foo SET b=b-[1,2,3],c=c+{1},d=d+{2,3,4};"; |
| 253 | - 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 | + }})); | |
| 254 | 277 | assertEquals(update.toString(), query); |
| 255 | 278 | |
| 256 | 279 | query = "UPDATE foo SET b=b-{2,3,4},c['k']='v',d=d+{'x':3,'y':2};"; |
| 257 | - update = update("foo").with(removeAll("b", new TreeSet<Integer>(){{ add(2); add(3); add(4); }})) | |
| 258 | - .and(put("c", "k", "v")) | |
| 259 | - .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 | + }})); | |
| 260 | 290 | assertEquals(update.toString(), query); |
| 261 | 291 | |
| 262 | 292 | query = "UPDATE foo USING TTL 400;"; |
| 263 | 293 | update = update("foo").using(ttl(400)); |
| 264 | 294 | assertEquals(update.toString(), query); |
| 265 | 295 | |
| 266 | - 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;"; | |
| 267 | 297 | update = update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2)); |
| 268 | 298 | assertEquals(update.toString(), query); |
| 269 | 299 | |
| @@ -280,7 +310,7 @@ | ||
| 280 | 310 | assertEquals(update.toString(), query); |
| 281 | 311 | |
| 282 | 312 | try { |
| 283 | - update = update("foo").using(ttl(-400)); | |
| 313 | + update("foo").using(ttl(-400)); | |
| 284 | 314 | fail(); |
| 285 | 315 | } catch (IllegalArgumentException e) { |
| 286 | 316 | assertEquals(e.getMessage(), "Invalid ttl, must be positive"); |
| @@ -326,14 +356,14 @@ | ||
| 326 | 356 | assertEquals(delete.toString(), query); |
| 327 | 357 | |
| 328 | 358 | try { |
| 329 | - delete = delete().column("a").all().from("foo"); | |
| 359 | + delete().column("a").all().from("foo"); | |
| 330 | 360 | fail(); |
| 331 | 361 | } catch (IllegalStateException e) { |
| 332 | 362 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 333 | 363 | } |
| 334 | 364 | |
| 335 | 365 | try { |
| 336 | - delete = delete().from("foo").using(timestamp(-1240003134L)); | |
| 366 | + delete().from("foo").using(timestamp(-1240003134L)); | |
| 337 | 367 | fail(); |
| 338 | 368 | } catch (IllegalArgumentException e) { |
| 339 | 369 | assertEquals(e.getMessage(), "Invalid timestamp, must be positive"); |
| @@ -364,7 +394,11 @@ | ||
| 364 | 394 | query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 365 | 395 | query += "APPLY BATCH;"; |
| 366 | 396 | batch = batch() |
| 367 | - .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 })) | |
| 368 | 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))) |
| 369 | 403 | .add(delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1))) |
| 370 | 404 | .using(timestamp(42)); |
| @@ -451,12 +485,9 @@ | ||
| 451 | 485 | assertEquals(batch.toString(), query); |
| 452 | 486 | } |
| 453 | 487 | |
| 454 | - @Test(groups = "unit", expectedExceptions={IllegalArgumentException.class}) | |
| 488 | + @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) | |
| 455 | 489 | public void batchMixedCounterTest() throws Exception { |
| 456 | - String query; | |
| 457 | - Statement batch; | |
| 458 | - | |
| 459 | - batch = batch() | |
| 490 | + batch() | |
| 460 | 491 | .add(update("foo").with(incr("a", 1))) |
| 461 | 492 | .add(update("foo").with(set("b", 2))) |
| 462 | 493 | .add(update("foo").with(incr("c", 3))) |
| @@ -470,8 +501,8 @@ | ||
| 470 | 501 | |
| 471 | 502 | query = "INSERT INTO test(k,c) VALUES (0,?);"; |
| 472 | 503 | insert = insertInto("test") |
| 473 | - .value("k", 0) | |
| 474 | - .value("c", bindMarker()); | |
| 504 | + .value("k", 0) | |
| 505 | + .value("c", bindMarker()); | |
| 475 | 506 | assertEquals(insert.toString(), query); |
| 476 | 507 | } |
| 477 | 508 | |
| @@ -498,7 +529,6 @@ | ||
| 498 | 529 | assertEquals(select.toString(), query); |
| 499 | 530 | } |
| 500 | 531 | |
| 501 | - | |
| 502 | 532 | @Test(groups = "unit") |
| 503 | 533 | public void selectInjectionTests() throws Exception { |
| 504 | 534 | |
| @@ -630,7 +660,7 @@ | ||
| 630 | 660 | |
| 631 | 661 | query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 632 | 662 | delete = delete("a", "b").from("foo") |
| 633 | - .where(in("a", "b", "c'); --comment")); | |
| 663 | + .where(in("a", "b", "c'); --comment")); | |
| 634 | 664 | assertEquals(delete.toString(), query); |
| 635 | 665 | |
| 636 | 666 | query = "DELETE FROM foo WHERE \"k=1 OR k\">42;"; |
| @@ -659,6 +689,7 @@ | ||
| 659 | 689 | public void rejectUnknownValueTest() throws Exception { |
| 660 | 690 | |
| 661 | 691 | try { |
| 692 | + //noinspection ResultOfMethodCallIgnored | |
| 662 | 693 | update("foo").with(set("a", new byte[13])).where(eq("k", 2)).toString(); |
| 663 | 694 | fail("Byte arrays should not be valid, ByteBuffer should be used instead"); |
| 664 | 695 | } catch (IllegalArgumentException e) { |
| @@ -704,7 +735,7 @@ | ||
| 704 | 735 | |
| 705 | 736 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>=('a',2) AND (c1,c2)<('b',0);"; |
| 706 | 737 | select = select().all().from("foo").where(eq("k", 4)).and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))) |
| 707 | - .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0))); | |
| 738 | + .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0))); | |
| 708 | 739 | assertEquals(select.toString(), query); |
| 709 | 740 | |
| 710 | 741 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);"; |
| @@ -771,6 +802,15 @@ | ||
| 771 | 802 | assertThat(statement.toString()).isEqualTo(query); |
| 772 | 803 | } |
| 773 | 804 | |
| 805 | + @Test(groups = "unit") | |
| 806 | + public void should_quote_complex_column_names() { | |
| 807 | + // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name | |
| 808 | + String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;"; | |
| 809 | + Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1)); | |
| 810 | + | |
| 811 | + assertThat(statement.toString()).isEqualTo(query); | |
| 812 | + } | |
| 813 | + | |
| 774 | 814 | @Test(groups = "unit") |
| 775 | 815 | public void should_not_serialize_raw_query_values() { |
| 776 | 816 | RegularStatement select = select().from("test").where(gt("i", raw("1"))); |