Elffuss BraidHub

kikocis/java-driver public

JAVA-679: Support bind marker in QueryBuilder DELETE's list index.
Changeset21fa34bb30efa44199b7dd63adb24199f52ede4b57e0967a352bd4075caaeeb2
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-15 13:14 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-22 15:55 +02:00
Parents03e44ead8f45

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -10,6 +10,7 @@
1010 - [bug] Forbid bind marker in QueryBuilder add/append/prepend (JAVA-736)
1111 - [bug] Prevent QueryBuilder.quote() from applying duplicate double quotes (JAVA-712)
1212 - [bug] Prevent QueryBuilder from trying to serialize raw string (JAVA-688)
13+- [bug] Support bind marker in QueryBuilder DELETE's list index (JAVA-679)
1314
1415 Merged from 2.0.10_fixes branch:
1516
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +94 −27
@@ -27,25 +27,25 @@
2727 public class Delete extends BuiltStatement {
2828
2929 private final String table;
30- private final List<?> columnNames;
30+ private final List<Selector> columns;
3131 private final Where where;
3232 private final Options usings;
3333 private final Conditions conditions;
3434 private boolean ifExists;
3535
36- Delete(String keyspace, String table, List<?> columnNames) {
36+ Delete(String keyspace, String table, List<Selector> columns) {
3737 super(keyspace);
3838 this.table = table;
39- this.columnNames = columnNames;
39+ this.columns = columns;
4040 this.where = new Where(this);
4141 this.usings = new Options(this);
4242 this.conditions = new Conditions(this);
4343 }
4444
45- Delete(TableMetadata table, List<Object> columnNames) {
45+ Delete(TableMetadata table, List<Selector> columns) {
4646 super(table);
4747 this.table = escapeId(table.getName());
48- this.columnNames = columnNames;
48+ this.columns = columns;
4949 this.where = new Where(this);
5050 this.usings = new Options(this);
5151 this.conditions = new Conditions(this);
@@ -56,8 +56,8 @@
5656 StringBuilder builder = new StringBuilder();
5757
5858 builder.append("DELETE");
59- if (columnNames != null)
60- Utils.joinAndAppendNames(builder.append(" "), ",", columnNames);
59+ if (!columns.isEmpty())
60+ Utils.joinAndAppend(builder.append(" "), ",", columns, variables);
6161
6262 builder.append(" FROM ");
6363 if (keyspace != null)
@@ -264,12 +264,15 @@
264264 */
265265 public static class Builder {
266266
267- List<Object> columnNames;
267+ List<Selector> columns = new ArrayList<Selector>();
268268
269- Builder() {}
269+ Builder() {
270+ }
270271
271- Builder(List<Object> columnNames) {
272- this.columnNames = columnNames;
272+ Builder(String... columnNames) {
273+ for (String columnName : columnNames) {
274+ this.columns.add(new Selector(columnName));
275+ }
273276 }
274277
275278 /**
@@ -290,7 +293,7 @@
290293 * @return a newly built DELETE statement that deletes from {@code keyspace.table}.
291294 */
292295 public Delete from(String keyspace, String table) {
293- return new Delete(keyspace, table, columnNames);
296+ return new Delete(keyspace, table, columns);
294297 }
295298
296299 /**
@@ -300,7 +303,7 @@
300303 * @return a newly built DELETE statement that deletes from {@code table}.
301304 */
302305 public Delete from(TableMetadata table) {
303- return new Delete(table, columnNames);
306+ return new Delete(table, columns);
304307 }
305308 }
306309
@@ -317,23 +320,20 @@
317320 * @throws IllegalStateException if some columns had already been selected for this builder.
318321 */
319322 public Builder all() {
320- if (columnNames != null)
321- throw new IllegalStateException(String.format("Some columns (%s) have already been selected.", columnNames));
323+ if (!columns.isEmpty())
324+ throw new IllegalStateException(String.format("Some columns (%s) have already been selected.", columns));
322325
323- return (Builder)this;
326+ return this;
324327 }
325328
326329 /**
327330 * Deletes the provided column.
328331 *
329- * @param name the column name to select for deletion.
332+ * @param columnName the column to select for deletion.
330333 * @return this in-build DELETE Selection
331334 */
332- public Selection column(String name) {
333- if (columnNames == null)
334- columnNames = new ArrayList<Object>();
335-
336- columnNames.add(name);
335+ public Selection column(String columnName) {
336+ columns.add(new Selector(columnName));
337337 return this;
338338 }
339339
@@ -345,9 +345,21 @@
345345 * @return this in-build DELETE Selection
346346 */
347347 public Selection listElt(String columnName, int idx) {
348- StringBuilder sb = new StringBuilder();
349- Utils.appendName(columnName, sb);
350- return column(sb.append('[').append(idx).append(']').toString());
348+ columns.add(new IndexedSelector(columnName, idx));
349+ return this;
350+ }
351+
352+ /**
353+ * Deletes the provided list element,
354+ * specified as a bind marker.
355+ *
356+ * @param columnName the name of the list column.
357+ * @param idx the index of the element to delete.
358+ * @return this in-build DELETE Selection
359+ */
360+ public Selection listElt(String columnName, BindMarker idx) {
361+ columns.add(new IndexedSelector(columnName, idx));
362+ return this;
351363 }
352364
353365 /**
@@ -358,12 +370,67 @@
358370 * @return this in-build DELETE Selection
359371 */
360372 public Selection mapElt(String columnName, Object key) {
361- StringBuilder sb = new StringBuilder();
373+ columns.add(new IndexedSelector(columnName, key));
374+ return this;
375+ }
376+ }
377+
378+ /**
379+ * A selector in a DELETE selection clause.
380+ * A selector can be either a column name,
381+ * a list element or a map entry.
382+ */
383+ static class Selector extends Utils.Appendeable {
384+
385+ private final String columnName;
386+
387+ Selector(String columnName) {
388+ this.columnName = columnName;
389+ }
390+
391+ @Override
392+ void appendTo(StringBuilder sb, List<ByteBuffer> values) {
362393 Utils.appendName(columnName, sb);
394+ }
395+
396+ @Override
397+ boolean containsBindMarker() {
398+ return false;
399+ }
400+
401+ @Override
402+ public String toString() {
403+ StringBuilder sb = new StringBuilder();
404+ appendTo(sb, new ArrayList<ByteBuffer>());
405+ return sb.toString();
406+ }
407+ }
408+
409+ /**
410+ * A list element or a map entry in a DELETE selection clause.
411+ */
412+ static class IndexedSelector extends Selector {
413+
414+ private final Object key;
415+
416+ IndexedSelector(String columnName, Object key) {
417+ super(columnName);
418+ this.key = key;
419+ }
420+
421+ @Override
422+ void appendTo(StringBuilder sb, List<ByteBuffer> values) {
423+ super.appendTo(sb, values);
363424 sb.append('[');
364425 Utils.appendFlatValue(key, sb);
365- return column(sb.append(']').toString());
426+ sb.append(']');
366427 }
428+
429+ @Override
430+ boolean containsBindMarker() {
431+ return Utils.containsBindMarker(key);
432+ }
433+
367434 }
368435
369436 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +1 −1
@@ -137,7 +137,7 @@
137137 * clause needs to be provided to complete the query).
138138 */
139139 public static Delete.Builder delete(String... columns) {
140- return new Delete.Builder(Arrays.asList((Object[])columns));
140+ return new Delete.Builder(columns);
141141 }
142142
143143 /**
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +62 −7
@@ -15,17 +15,19 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-import java.util.Arrays;
19-import java.util.Collection;
20-import java.util.Date;
21-import java.util.List;
18+import java.util.*;
2219
2320 import org.testng.annotations.Test;
2421
22+import static org.assertj.core.api.Assertions.assertThat;
23+import static org.assertj.core.data.MapEntry.entry;
24+import static org.testng.Assert.assertEquals;
25+import static org.testng.Assert.assertFalse;
26+import static org.testng.Assert.assertTrue;
27+
2528 import com.datastax.driver.core.*;
2629
2730 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
28-import static org.testng.Assert.*;
2931
3032 public class QueryBuilderExecutionTest extends CCMBridge.PerClassSingleNodeCluster {
3133
@@ -33,8 +35,10 @@
3335
3436 @Override
3537 protected Collection<String> getTableDefinitions() {
36- return Arrays.asList(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1),
37- "CREATE TABLE dateTest (t timestamp PRIMARY KEY)");
38+ return Arrays.asList(
39+ String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1),
40+ "CREATE TABLE dateTest (t timestamp PRIMARY KEY)",
41+ "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>)");
3842 }
3943
4044 @Test(groups = "short")
@@ -100,4 +104,55 @@
100104 assertEquals("batchTest2", r2.getString("k"));
101105 assertEquals("val2", r2.getString("t"));
102106 }
107+
108+ @Test(groups = "short")
109+ public void should_delete_list_element() throws Exception {
110+ //given
111+ session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)");
112+ //when
113+ BuiltStatement statement = delete().listElt("a", 1).from("test_coll").where(eq("k", 1));
114+ session.execute(statement);
115+ //then
116+ List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class);
117+ assertThat(actual).containsExactly(1, 3);
118+ }
119+
120+ @Test(groups = "short")
121+ public void should_delete_list_element_with_bind_marker() throws Exception {
122+ //given
123+ session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)");
124+ //when
125+ BuiltStatement statement = delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1));
126+ PreparedStatement ps = session.prepare(statement);
127+ session.execute(ps.bind(1));
128+ //then
129+ List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class);
130+ assertThat(actual).containsExactly(1, 3);
131+ }
132+
133+ @Test(groups = "short")
134+ public void should_delete_map_entry() throws Exception {
135+ //given
136+ session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})");
137+ //when
138+ BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1));
139+ session.execute(statement);
140+ //then
141+ Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class);
142+ assertThat(actual).containsExactly(entry(2, "bar"));
143+ }
144+
145+ @Test(groups = "short")
146+ public void should_delete_map_entry_with_bind_marker() throws Exception {
147+ //given
148+ session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})");
149+ //when
150+ BuiltStatement statement = delete().mapElt("b", bindMarker("mapEntry")).from("test_coll").where(eq("k", 1));
151+ PreparedStatement ps = session.prepare(statement);
152+ session.execute(ps.bind().setInt("mapEntry", 1));
153+ //then
154+ Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class);
155+ assertThat(actual).containsExactly(entry(2, "bar"));
156+ }
157+
103158 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +4 −0
@@ -305,6 +305,10 @@
305305 delete = delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1));
306306 assertEquals(delete.toString(), query);
307307
308+ query = "DELETE a[?],b[?],c FROM foo WHERE k=1;";
309+ delete = delete().listElt("a", bindMarker()).mapElt("b", bindMarker()).column("c").from("foo").where(eq("k", 1));
310+ assertEquals(delete.toString(), query);
311+
308312 // Invalid CQL, testing edge case
309313 query = "DELETE a,b,c FROM foo;";
310314 delete = delete("a", "b", "c").from("foo");