Skip to content

Commit 7edb4d7

Browse files
committed
Merge pull request apache#665 from datastax/java1184
JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
2 parents 53d16c4 + 08e6fbb commit 7edb4d7

File tree

7 files changed

+97
-5
lines changed

7 files changed

+97
-5
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ static ArrayBackedResultSet fromMessage(Responses.Result msg, SessionManager ses
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;

driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ IdAndValues getIdAndValues() {
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

driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void run() {
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
}

driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ public void setMaxLoggedParameters(int maxLoggedParameters) {
614614
*/
615615
@Override
616616
public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) {
617+
if (statement instanceof StatementWrapper)
618+
statement = ((StatementWrapper) statement).getWrappedStatement();
619+
617620
long latencyMs = NANOSECONDS.toMillis(newLatencyNanos);
618621
if (exception == null) {
619622
maybeLogNormalOrSlowQuery(host, statement, latencyMs);
@@ -745,9 +748,6 @@ protected String parameterValueAsString(ColumnDefinitions.Definition definition,
745748
}
746749

747750
protected int append(Statement statement, StringBuilder buffer, int remaining) {
748-
if (statement instanceof StatementWrapper)
749-
statement = ((StatementWrapper) statement).getWrappedStatement();
750-
751751
if (statement instanceof RegularStatement) {
752752
remaining = append(((RegularStatement) statement).getQueryString().trim(), buffer, remaining);
753753
} else if (statement instanceof BoundStatement) {

driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import com.datastax.driver.core.StatementWrapperTest.CustomStatement;
1819
import com.datastax.driver.core.exceptions.DriverException;
1920
import com.datastax.driver.core.utils.CassandraVersion;
2021
import com.google.common.base.Function;
@@ -713,6 +714,33 @@ public void should_log_all_parameters_when_max_unlimited() throws Exception {
713714
.contains("pk:42");
714715
}
715716

717+
@CassandraVersion(major=2.0)
718+
@Test(groups = "short")
719+
public void should_log_wrapped_bound_statement() throws Exception {
720+
// given
721+
normal.setLevel(TRACE);
722+
queryLogger = QueryLogger.builder(cluster)
723+
.withConstantThreshold(Long.MAX_VALUE)
724+
.withMaxQueryStringLength(Integer.MAX_VALUE)
725+
.build();
726+
cluster.register(queryLogger);
727+
// when
728+
String query = "UPDATE test SET c_text = :param1 WHERE pk = :param2";
729+
PreparedStatement ps = session.prepare(query);
730+
BoundStatement bs = ps.bind();
731+
bs.setString("param1", "foo");
732+
bs.setInt("param2", 42);
733+
session.execute(new CustomStatement(bs));
734+
// then
735+
String line = normalAppender.waitAndGet(10000);
736+
assertThat(line)
737+
.contains("Query completed normally")
738+
.contains(ipOfNode(1))
739+
.contains(query)
740+
.contains("param2:42")
741+
.contains("param1:'foo'");
742+
}
743+
716744
@Override
717745
protected List<String> getTableDefinitions() {
718746
return Lists.newArrayList("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")");

driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 @@ public void should_pass_wrapped_statement_to_retry_policy() {
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
*/

0 commit comments

Comments
 (0)