Elffuss BraidHub

kikocis/java-driver public

JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
Changeseta3e6b3e0a1ce05b96bad65597373af9abdc9cd5d43c2622e411a7b0dacd27ca9
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-03-22 10:02 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-01 18:21 +02:00
Parentsc9467c0de048

Browse the files at this changeset

4 files changed

modified changelog/README.md +2 −0
@@ -2,6 +2,8 @@
22
33 ### 3.0.1 (in progress)
44
5+- [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
6+
57 Merged from 2.1 branch:
68
79 - [improvement] JAVA-1011: Expose PoolingOptions default values.
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +15 −8
@@ -231,16 +231,18 @@
231231
232232 static class QueryProtocolOptions {
233233
234- static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions(ConsistencyLevel.ONE,
234+ static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions(
235+ Message.Request.Type.QUERY,
236+ ConsistencyLevel.ONE,
235237 Collections.<ByteBuffer>emptyList(),
236238 Collections.<String, ByteBuffer>emptyMap(),
237239 false,
238240 -1,
239241 null,
240- ConsistencyLevel.SERIAL,
241- Long.MIN_VALUE);
242+ ConsistencyLevel.SERIAL, Long.MIN_VALUE);
242243
243244 private final EnumSet<QueryFlag> flags = EnumSet.noneOf(QueryFlag.class);
245+ private final Message.Request.Type requestType;
244246 final ConsistencyLevel consistency;
245247 final List<ByteBuffer> positionalValues;
246248 final Map<String, ByteBuffer> namedValues;
@@ -250,7 +252,8 @@
250252 final ConsistencyLevel serialConsistency;
251253 final long defaultTimestamp;
252254
253- QueryProtocolOptions(ConsistencyLevel consistency,
255+ QueryProtocolOptions(Message.Request.Type requestType,
256+ ConsistencyLevel consistency,
254257 List<ByteBuffer> positionalValues,
255258 Map<String, ByteBuffer> namedValues,
256259 boolean skipMetadata,
@@ -261,6 +264,7 @@
261264
262265 Preconditions.checkArgument(positionalValues.isEmpty() || namedValues.isEmpty());
263266
267+ this.requestType = requestType;
264268 this.consistency = consistency;
265269 this.positionalValues = positionalValues;
266270 this.namedValues = namedValues;
@@ -290,14 +294,15 @@
290294 }
291295
292296 QueryProtocolOptions copy(ConsistencyLevel newConsistencyLevel) {
293- return new QueryProtocolOptions(newConsistencyLevel, positionalValues, namedValues, skipMetadata, pageSize, pagingState, serialConsistency, defaultTimestamp);
297+ return new QueryProtocolOptions(requestType, newConsistencyLevel, positionalValues, namedValues, skipMetadata, pageSize, pagingState, serialConsistency, defaultTimestamp);
294298 }
295299
296300 void encode(ByteBuf dest, ProtocolVersion version) {
297301 switch (version) {
298302 case V1:
299- // Values in protocol v1 are only for bound statements, and these are never named
300- if (flags.contains(QueryFlag.VALUES))
303+ // only EXECUTE messages have variables in V1, and their list must be written
304+ // even if it is empty; and they are never named
305+ if (requestType == Message.Request.Type.EXECUTE)
301306 CBUtil.writeValueList(positionalValues, dest);
302307 CBUtil.writeConsistencyLevel(consistency, dest);
303308 break;
@@ -331,7 +336,9 @@
331336 int encodedSize(ProtocolVersion version) {
332337 switch (version) {
333338 case V1:
334- return CBUtil.sizeOfValueList(positionalValues)
339+ // only EXECUTE messages have variables in V1, and their list must be written
340+ // even if it is empty; and they are never named
341+ return (requestType == Message.Request.Type.EXECUTE ? CBUtil.sizeOfValueList(positionalValues) : 0)
335342 + CBUtil.sizeOfConsistencyLevel(consistency);
336343 case V2:
337344 case V3:
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +4 −4
@@ -559,8 +559,8 @@
559559
560560 String qString = rs.getQueryString();
561561
562- Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, positionalValues, namedValues, false,
563- fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
562+ Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(Message.Request.Type.QUERY, consistency, positionalValues, namedValues,
563+ false, fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
564564 request = new Requests.Query(qString, options, statement.isTracing());
565565 } else if (statement instanceof BoundStatement) {
566566 BoundStatement bs = (BoundStatement) statement;
@@ -571,8 +571,8 @@
571571 if (protocolVersion.compareTo(ProtocolVersion.V4) < 0)
572572 bs.ensureAllSet();
573573 boolean skipMetadata = protocolVersion != ProtocolVersion.V1 && bs.statement.getPreparedId().resultSetMetadata != null;
574- Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, Arrays.asList(bs.wrapper.values), Collections.<String, ByteBuffer>emptyMap(), skipMetadata,
575- fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
574+ Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(Message.Request.Type.EXECUTE, consistency, Arrays.asList(bs.wrapper.values), Collections.<String, ByteBuffer>emptyMap(),
575+ skipMetadata, fetchSize, usedPagingState, serialConsistency, defaultTimestamp);
576576 request = new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing());
577577 } else {
578578 assert statement instanceof BatchStatement : statement;
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.annotations.Test;
20+
21+import static com.datastax.driver.core.ProtocolVersion.V1;
22+import static org.assertj.core.api.Assertions.assertThat;
23+
24+/**
25+ * Tests targeting protocol v1 specifically.
26+ */
27+@CCMConfig(version = "1.2.19")
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+}