Elffuss BraidHub

kikocis/java-driver public

JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
Changeseta35fec42e322607a1acd70545fb2dbbcf944f30eb28b163fbe0d59d156edb968
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-03-22 10:02 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-23 11:32 +01:00
Parents25b5ffc807a5

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -39,6 +39,7 @@
3939 - [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
4040 - [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
4141 - [improvement] JAVA-852: Ignore peers with null entries during discovery.
42+- [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
4243
4344 Merged from 2.0 branch:
4445
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +15 −7
@@ -230,15 +230,17 @@
230230
231231 public static class QueryProtocolOptions {
232232
233- public static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions(ConsistencyLevel.ONE,
233+ public static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions(
234+ Message.Request.Type.QUERY,
235+ ConsistencyLevel.ONE,
234236 Collections.<ByteBuffer>emptyList(),
235237 false,
236238 -1,
237239 null,
238- ConsistencyLevel.SERIAL,
239- Long.MIN_VALUE);
240+ ConsistencyLevel.SERIAL, Long.MIN_VALUE);
240241
241242 private final EnumSet<QueryFlag> flags = EnumSet.noneOf(QueryFlag.class);
243+ private final Message.Request.Type requestType;
242244 public final ConsistencyLevel consistency;
243245 public final List<ByteBuffer> values;
244246 public final boolean skipMetadata;
@@ -247,7 +249,8 @@
247249 public final ConsistencyLevel serialConsistency;
248250 public final long defaultTimestamp;
249251
250- public QueryProtocolOptions(ConsistencyLevel consistency,
252+ public QueryProtocolOptions(Message.Request.Type requestType,
253+ ConsistencyLevel consistency,
251254 List<ByteBuffer> values,
252255 boolean skipMetadata,
253256 int pageSize,
@@ -255,6 +258,7 @@
255258 ConsistencyLevel serialConsistency,
256259 long defaultTimestamp) {
257260
261+ this.requestType = requestType;
258262 this.consistency = consistency;
259263 this.values = values;
260264 this.skipMetadata = skipMetadata;
@@ -279,13 +283,15 @@
279283 }
280284
281285 public QueryProtocolOptions copy(ConsistencyLevel newConsistencyLevel) {
282- return new QueryProtocolOptions(newConsistencyLevel, values, skipMetadata, pageSize, pagingState, serialConsistency, defaultTimestamp);
286+ return new QueryProtocolOptions(requestType, newConsistencyLevel, values, skipMetadata, pageSize, pagingState, serialConsistency, defaultTimestamp);
283287 }
284288
285289 public void encode(ByteBuf dest, ProtocolVersion version) {
286290 switch (version) {
287291 case V1:
288- if (flags.contains(QueryFlag.VALUES))
292+ // only EXECUTE messages have variables in V1, and their list must be written
293+ // even if it is empty
294+ if (requestType == Message.Request.Type.EXECUTE)
289295 CBUtil.writeValueList(values, dest);
290296 CBUtil.writeConsistencyLevel(consistency, dest);
291297 break;
@@ -312,7 +318,9 @@
312318 public int encodedSize(ProtocolVersion version) {
313319 switch (version) {
314320 case V1:
315- return CBUtil.sizeOfValueList(values)
321+ // only EXECUTE messages have variables in V1, and their list must be written
322+ // even if it is empty
323+ return (requestType == Message.Request.Type.EXECUTE ? CBUtil.sizeOfValueList(values) : 0)
316324 + CBUtil.sizeOfConsistencyLevel(consistency);
317325 case V2:
318326 case V3:
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +2 −3
@@ -89,7 +89,6 @@
8989 ListenableFuture<?> allPoolsUpdatedFuture = Futures.transform(allPoolsCreatedFuture,
9090 new AsyncFunction<Object, Object>() {
9191 @Override
92- @SuppressWarnings("unchecked")
9392 public ListenableFuture<Object> apply(Object input) throws Exception {
9493 isInit = true;
9594 return (ListenableFuture<Object>) updateCreatedPools();
@@ -546,7 +545,7 @@
546545
547546 List<ByteBuffer> values = rawValues == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(rawValues);
548547 String qString = rs.getQueryString();
549- Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, values, false,
548+ Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(Message.Request.Type.QUERY, consistency, values, false,
550549 fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
551550 return new Requests.Query(qString, options, statement.isTracing());
552551 } else if (statement instanceof BoundStatement) {
@@ -557,7 +556,7 @@
557556 }
558557 bs.ensureAllSet();
559558 boolean skipMetadata = version != ProtocolVersion.V1 && bs.statement.getPreparedId().resultSetMetadata != null;
560- Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, Arrays.asList(bs.wrapper.values), skipMetadata,
559+ Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(Message.Request.Type.EXECUTE, consistency, Arrays.asList(bs.wrapper.values), skipMetadata,
561560 fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
562561 return new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing());
563562 } else {
added driver-core/src/test/java/com/datastax/driver/core/ProtocolV1Test.java +79 −0
@@ -0,0 +1,79 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
19+import org.testng.SkipException;
20+import org.testng.annotations.Test;
21+
22+import static com.datastax.driver.core.ProtocolVersion.V1;
23+import static org.assertj.core.api.Assertions.assertThat;
24+
25+/**
26+ * Tests targeting protocol v1 specifically.
27+ */
28+public class ProtocolV1Test extends CCMTestsSupport {
29+
30+ @Override
31+ public Cluster.Builder createClusterBuilder() {
32+ return super.createClusterBuilder()
33+ .withProtocolVersion(V1);
34+ }
35+
36+ /**
37+ * Validates that a simple query with no variables is correctly executed.
38+ * @jira_ticket JAVA-1132
39+ */
40+ @Test(groups = "short")
41+ public void should_execute_query_with_no_variables() throws Exception {
42+ session().execute("select * from system.local");
43+ }
44+
45+ /**
46+ * Validates that a simple query with variables is not allowed with protocol V1.
47+ * (Values in protocol V1 are only allowed in prepared statements).
48+ * @jira_ticket JAVA-1132
49+ */
50+ @Test(groups = "short")
51+ public void should_not_execute_query_with_variables() throws Exception {
52+ try {
53+ session().execute(new SimpleStatement("select * from system.local where key=?", "local"));
54+ } catch (UnsupportedFeatureException e) {
55+ assertThat(e).hasMessageContaining("Unsupported feature with the native protocol V1 (which is currently in use): Binary values are not supported");
56+ }
57+ }
58+
59+ /**
60+ * Validates that a prepared statement with no variables is correctly prepared and executed.
61+ * @jira_ticket JAVA-1132
62+ */
63+ @Test(groups = "short")
64+ public void should_execute_prepared_statement_with_no_variables() throws Exception {
65+ PreparedStatement ps = session().prepare("select * from system.local");
66+ session().execute(ps.bind());
67+ }
68+
69+ /**
70+ * Validates that a prepared statement with variables is correctly prepared and executed.
71+ * @jira_ticket JAVA-1132
72+ */
73+ @Test(groups = "short")
74+ public void should_execute_prepared_statement_with_variables() throws Exception {
75+ PreparedStatement ps = session().prepare("select * from system.local where key=?");
76+ session().execute(ps.bind("local"));
77+ }
78+
79+}