Elffuss BraidHub

kikocis/java-driver public

Fix handling of SimpleStatement with values in query build batch (JAVA-393)
Changeset457118a4e6784d6e485713421b0b0410a9f1b8006381f5c5c07b882ca9225cd5
AuthorSylvain Lebresne <sylvain@datastax.com> · 2014-08-07 17:42 +02:00
CommitterSylvain Lebresne <sylvain@datastax.com> · 2014-08-07 17:42 +02:00
Parents50f551d5ba37

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -5,6 +5,8 @@
55 ------
66
77 - [bug] Release connections on ResultSetFuture#cancel (JAVA-407)
8+- [bug] Fix handling of SimpleStatement with values in query builder
9+ batches (JAVA-393)
810
911
1012 2.0.4:
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +39 −8
@@ -32,6 +32,9 @@
3232 private final Options usings;
3333 private ByteBuffer routingKey;
3434
35+ // Only used when we add at last one statement that is not a BuiltStatement subclass
36+ private int nonBuiltStatementValues;
37+
3538 Batch(RegularStatement[] statements, boolean logged) {
3639 super((String)null);
3740 this.statements = statements.length == 0
@@ -70,12 +73,9 @@
7073 if (!str.trim().endsWith(";"))
7174 builder.append(';');
7275
73- if (variables != null) {
74- ByteBuffer[] vars = stmt.getValues();
75- if (vars != null) {
76- Collections.addAll(variables, vars);
77- }
78- }
76+ // Note that we force hasBindMarkers if there is any non-BuiltStatement, so we know
77+ // that we can only get there with variables == null
78+ assert variables == null;
7979 }
8080 }
8181 builder.append("APPLY BATCH;");
@@ -102,10 +102,17 @@
102102 this.statements.add(statement);
103103
104104 if (statement instanceof BuiltStatement)
105- this.hasBindMarkers = ((BuiltStatement)statement).hasBindMarkers;
105+ {
106+ this.hasBindMarkers |= ((BuiltStatement)statement).hasBindMarkers;
107+ }
106108 else
107- // For non-BuiltStatement, we cannot know if it includes a bind makers. So we assume it does.
109+ {
110+ // For non-BuiltStatement, we cannot know if it includes a bind makers and we assume it does. In practice,
111+ // this means we will always serialize values as strings when there is non-BuiltStatement
108112 this.hasBindMarkers = true;
113+ if (statement.getValues() != null)
114+ this.nonBuiltStatementValues += statement.getValues().length;
115+ }
109116
110117 checkForBindMarkers(null);
111118
@@ -115,6 +122,30 @@
115122 return this;
116123 }
117124
125+ @Override
126+ public ByteBuffer[] getValues() {
127+ // If there is some non-BuiltStatement inside the batch with values, we shouldn't
128+ // use super.getValues() since it will ignore the values of said non-BuiltStatement.
129+ // If that's the case, we just collects all those values (and we know
130+ // super.getValues() == null in that case since we've explicitely set this.hasBindMarker
131+ // to true). Otherwise, we simply call super.getValues().
132+ if (nonBuiltStatementValues == 0)
133+ return super.getValues();
134+
135+ ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues];
136+ int i = 0;
137+ for (RegularStatement statement : statements)
138+ {
139+ if (statement instanceof BuiltStatement)
140+ continue;
141+
142+ ByteBuffer[] statementValues = statement.getValues();
143+ System.arraycopy(statementValues, 0, values, i, statementValues.length);
144+ i += statementValues.length;
145+ }
146+ return values;
147+ }
148+
118149 /**
119150 * Adds a new options for this BATCH statement.
120151 *
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +20 −4
@@ -22,10 +22,7 @@
2222
2323 import org.testng.annotations.Test;
2424
25-import com.datastax.driver.core.CCMBridge;
26-import com.datastax.driver.core.RegularStatement;
27-import com.datastax.driver.core.Row;
28-import com.datastax.driver.core.TestUtils;
25+import com.datastax.driver.core.*;
2926
3027 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
3128 import static org.testng.Assert.*;
@@ -85,4 +82,23 @@
8582 RegularStatement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0);
8683 assertEquals(query, stmt.getQueryString());
8784 }
85+
86+ @Test(groups = "short")
87+ public void batchNonBuiltStatementTest() throws Exception {
88+
89+ SimpleStatement simple = new SimpleStatement("INSERT INTO " + TABLE1 + " (k, t) VALUES (?, ?)", "batchTest1", "val1");
90+ RegularStatement built = insertInto(TABLE1).value("k", "batchTest2").value("t", "val2");
91+ session.execute(batch().add(simple).add(built));
92+
93+ List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all();
94+ assertEquals(2, rows.size());
95+
96+ Row r1 = rows.get(0);
97+ assertEquals("batchTest1", r1.getString("k"));
98+ assertEquals("val1", r1.getString("t"));
99+
100+ Row r2 = rows.get(1);
101+ assertEquals("batchTest2", r2.getString("k"));
102+ assertEquals("val2", r2.getString("t"));
103+ }
88104 }