Elffuss BraidHub

kikocis/java-driver public

JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
Changeset174fb0a64d43861db099b5feb4a46d53a4812327781e2fe3494b975fe81af665
AuthorSmirnov Danil <d.smirnov@hh.ru> · 2016-05-04 20:33 +03:00
Committerolim7t <omichallat+github@gmail.com> · 2016-05-11 11:24 -07:00
Parents2529393d3013

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -4,6 +4,7 @@
44
55 - [bug] JAVA-1179: Request objects should be copied when executed.
66 - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread.
7+- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
78
89
910 ### 2.0.12.1
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +3 −0
@@ -59,6 +59,9 @@
5959
6060 ColumnDefinitions columnDefs;
6161 if (r.metadata.columns == null) {
62+ if (statement instanceof StatementWrapper) {
63+ statement = ((StatementWrapper) statement).getWrappedStatement();
64+ }
6265 assert statement instanceof BoundStatement;
6366 columnDefs = ((BoundStatement) statement).statement.getPreparedId().resultSetMetadata;
6467 assert columnDefs != null;
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +3 −0
@@ -118,6 +118,9 @@
118118 * of statements for a BatchStatement allowed by the underlying protocol).
119119 */
120120 public BatchStatement add(Statement statement) {
121+ if (statement instanceof StatementWrapper) {
122+ statement = ((StatementWrapper) statement).getWrappedStatement();
123+ }
121124
122125 // We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly
123126 // easier to avoid endless loop if the use mistakenly pass a batch that depends on this
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +1 −1
@@ -143,7 +143,7 @@
143143 setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address)));
144144 break;
145145 }
146- } catch (RuntimeException e) {
146+ } catch (Throwable e) {
147147 // If we get a bug here, the client will not get it, so better forwarding the error
148148 setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e));
149149 }
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +58 −1
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.policies.*;
19+import com.datastax.driver.core.utils.CassandraVersion;
1920 import com.google.common.collect.Lists;
2021 import org.testng.annotations.Test;
2122
@@ -26,9 +27,15 @@
2627 import static org.assertj.core.api.Assertions.assertThat;
2728
2829 public class StatementWrapperTest extends CCMBridge.PerClassSingleNodeCluster {
30+
31+ private static final String INSERT_QUERY = "insert into test (k, v) values (?, ?)";
32+ private static final String SELECT_QUERY = "select * from test where k = ?";
33+
2934 @Override
3035 protected Collection<String> getTableDefinitions() {
31- return Lists.newArrayList();
36+ return Lists.newArrayList(
37+ "create table test (k text primary key, v int)"
38+ );
3239 }
3340
3441 CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy();
@@ -83,6 +90,56 @@
8390 assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1);
8491 }
8592
93+ @CassandraVersion(major=2.0)
94+ @Test(groups = "short")
95+ public void should_execute_wrapped_simple_statement() {
96+ session.execute(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key_simple", 1)));
97+
98+ ResultSet rs = session.execute(new CustomStatement(new SimpleStatement(SELECT_QUERY, "key_simple")));
99+ assertThat(rs.one().getInt("v")).isEqualTo(1);
100+ }
101+
102+ @Test(groups = "short")
103+ public void should_execute_wrapped_bound_statement() {
104+ PreparedStatement preparedStatement = session.prepare(new SimpleStatement(INSERT_QUERY));
105+ session.execute(new CustomStatement(preparedStatement.bind("key_bound", 1)));
106+
107+ preparedStatement = session.prepare(new SimpleStatement(SELECT_QUERY));
108+ ResultSet rs = session.execute(new CustomStatement(preparedStatement.bind("key_bound")));
109+ assertThat(rs.one().getInt("v")).isEqualTo(1);
110+ }
111+
112+ @CassandraVersion(major=2.0)
113+ @Test(groups = "short")
114+ public void should_execute_wrapped_batch_statement() {
115+ BatchStatement batchStatement = new BatchStatement();
116+ batchStatement.add(new SimpleStatement(INSERT_QUERY, "key_batch", 1));
117+
118+ session.execute(new CustomStatement(batchStatement));
119+
120+ ResultSet rs = session.execute(SELECT_QUERY, "key_batch");
121+ assertThat(rs.one().getInt("v")).isEqualTo(1);
122+ }
123+
124+ @CassandraVersion(major=2.0)
125+ @Test(groups = "short")
126+ public void should_add_wrapped_batch_statement_to_batch_statement() {
127+ BatchStatement batchStatementForWrapping = new BatchStatement();
128+ batchStatementForWrapping.add(new SimpleStatement(INSERT_QUERY, "key1", 1));
129+
130+ BatchStatement batchStatement = new BatchStatement();
131+ batchStatement.add(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key2", 2)));
132+ batchStatement.add(new CustomStatement(batchStatementForWrapping));
133+
134+ session.execute(batchStatement);
135+
136+ ResultSet rs = session.execute(SELECT_QUERY, "key1");
137+ assertThat(rs.one().getInt("v")).isEqualTo(1);
138+
139+ rs = session.execute(SELECT_QUERY, "key2");
140+ assertThat(rs.one().getInt("v")).isEqualTo(2);
141+ }
142+
86143 /**
87144 * A custom wrapper that's just used to mark statements.
88145 */