kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 457118a4e6784d6e485713421b0b0410a9f1b8006381f5c5c07b882ca9225cd5 |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2014-08-07 17:42 +02:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2014-08-07 17:42 +02:00 |
| Parents | 50f551d5ba37 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | 7 | - [bug] Release connections on ResultSetFuture#cancel (JAVA-407) |
| 8 | +- [bug] Fix handling of SimpleStatement with values in query builder | |
| 9 | + batches (JAVA-393) | |
| 8 | 10 | |
| 9 | 11 | |
| 10 | 12 | 2.0.4: |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +39 −8
| @@ -32,6 +32,9 @@ | ||
| 32 | 32 | private final Options usings; |
| 33 | 33 | private ByteBuffer routingKey; |
| 34 | 34 | |
| 35 | + // Only used when we add at last one statement that is not a BuiltStatement subclass | |
| 36 | + private int nonBuiltStatementValues; | |
| 37 | + | |
| 35 | 38 | Batch(RegularStatement[] statements, boolean logged) { |
| 36 | 39 | super((String)null); |
| 37 | 40 | this.statements = statements.length == 0 |
| @@ -70,12 +73,9 @@ | ||
| 70 | 73 | if (!str.trim().endsWith(";")) |
| 71 | 74 | builder.append(';'); |
| 72 | 75 | |
| 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; | |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | builder.append("APPLY BATCH;"); |
| @@ -102,10 +102,17 @@ | ||
| 102 | 102 | this.statements.add(statement); |
| 103 | 103 | |
| 104 | 104 | if (statement instanceof BuiltStatement) |
| 105 | - this.hasBindMarkers = ((BuiltStatement)statement).hasBindMarkers; | |
| 105 | + { | |
| 106 | + this.hasBindMarkers |= ((BuiltStatement)statement).hasBindMarkers; | |
| 107 | + } | |
| 106 | 108 | 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 | |
| 108 | 112 | this.hasBindMarkers = true; |
| 113 | + if (statement.getValues() != null) | |
| 114 | + this.nonBuiltStatementValues += statement.getValues().length; | |
| 115 | + } | |
| 109 | 116 | |
| 110 | 117 | checkForBindMarkers(null); |
| 111 | 118 | |
| @@ -115,6 +122,30 @@ | ||
| 115 | 122 | return this; |
| 116 | 123 | } |
| 117 | 124 | |
| 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 | + | |
| 118 | 149 | /** |
| 119 | 150 | * Adds a new options for this BATCH statement. |
| 120 | 151 | * |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +20 −4
| @@ -22,10 +22,7 @@ | ||
| 22 | 22 | |
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | 24 | |
| 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.*; | |
| 29 | 26 | |
| 30 | 27 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 31 | 28 | import static org.testng.Assert.*; |
| @@ -85,4 +82,23 @@ | ||
| 85 | 82 | RegularStatement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0); |
| 86 | 83 | assertEquals(query, stmt.getQueryString()); |
| 87 | 84 | } |
| 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 | + } | |
| 88 | 104 | } |