kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | eec6c6de3e6433825dcfa0fd87fc629ca1cf3001e5f5d68f626c358f456d58ab |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-01 18:23 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-01 18:23 +02:00 |
| Parents | c9467c0de048, a3e6b3e0a1ce |
4 files changed
modified changelog/README.md +2 −0
| @@ -2,6 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | ### 3.0.1 (in progress) |
| 4 | 4 | |
| 5 | +- [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1. | |
| 6 | + | |
| 5 | 7 | Merged from 2.1 branch: |
| 6 | 8 | |
| 7 | 9 | - [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 @@ | ||
| 231 | 231 | |
| 232 | 232 | static class QueryProtocolOptions { |
| 233 | 233 | |
| 234 | - static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions(ConsistencyLevel.ONE, | |
| 234 | + static final QueryProtocolOptions DEFAULT = new QueryProtocolOptions( | |
| 235 | + Message.Request.Type.QUERY, | |
| 236 | + ConsistencyLevel.ONE, | |
| 235 | 237 | Collections.<ByteBuffer>emptyList(), |
| 236 | 238 | Collections.<String, ByteBuffer>emptyMap(), |
| 237 | 239 | false, |
| 238 | 240 | -1, |
| 239 | 241 | null, |
| 240 | - ConsistencyLevel.SERIAL, | |
| 241 | - Long.MIN_VALUE); | |
| 242 | + ConsistencyLevel.SERIAL, Long.MIN_VALUE); | |
| 242 | 243 | |
| 243 | 244 | private final EnumSet<QueryFlag> flags = EnumSet.noneOf(QueryFlag.class); |
| 245 | + private final Message.Request.Type requestType; | |
| 244 | 246 | final ConsistencyLevel consistency; |
| 245 | 247 | final List<ByteBuffer> positionalValues; |
| 246 | 248 | final Map<String, ByteBuffer> namedValues; |
| @@ -250,7 +252,8 @@ | ||
| 250 | 252 | final ConsistencyLevel serialConsistency; |
| 251 | 253 | final long defaultTimestamp; |
| 252 | 254 | |
| 253 | - QueryProtocolOptions(ConsistencyLevel consistency, | |
| 255 | + QueryProtocolOptions(Message.Request.Type requestType, | |
| 256 | + ConsistencyLevel consistency, | |
| 254 | 257 | List<ByteBuffer> positionalValues, |
| 255 | 258 | Map<String, ByteBuffer> namedValues, |
| 256 | 259 | boolean skipMetadata, |
| @@ -261,6 +264,7 @@ | ||
| 261 | 264 | |
| 262 | 265 | Preconditions.checkArgument(positionalValues.isEmpty() || namedValues.isEmpty()); |
| 263 | 266 | |
| 267 | + this.requestType = requestType; | |
| 264 | 268 | this.consistency = consistency; |
| 265 | 269 | this.positionalValues = positionalValues; |
| 266 | 270 | this.namedValues = namedValues; |
| @@ -290,14 +294,15 @@ | ||
| 290 | 294 | } |
| 291 | 295 | |
| 292 | 296 | 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); | |
| 294 | 298 | } |
| 295 | 299 | |
| 296 | 300 | void encode(ByteBuf dest, ProtocolVersion version) { |
| 297 | 301 | switch (version) { |
| 298 | 302 | 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) | |
| 301 | 306 | CBUtil.writeValueList(positionalValues, dest); |
| 302 | 307 | CBUtil.writeConsistencyLevel(consistency, dest); |
| 303 | 308 | break; |
| @@ -331,7 +336,9 @@ | ||
| 331 | 336 | int encodedSize(ProtocolVersion version) { |
| 332 | 337 | switch (version) { |
| 333 | 338 | 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) | |
| 335 | 342 | + CBUtil.sizeOfConsistencyLevel(consistency); |
| 336 | 343 | case V2: |
| 337 | 344 | case V3: |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +4 −4
| @@ -559,8 +559,8 @@ | ||
| 559 | 559 | |
| 560 | 560 | String qString = rs.getQueryString(); |
| 561 | 561 | |
| 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); | |
| 564 | 564 | request = new Requests.Query(qString, options, statement.isTracing()); |
| 565 | 565 | } else if (statement instanceof BoundStatement) { |
| 566 | 566 | BoundStatement bs = (BoundStatement) statement; |
| @@ -571,8 +571,8 @@ | ||
| 571 | 571 | if (protocolVersion.compareTo(ProtocolVersion.V4) < 0) |
| 572 | 572 | bs.ensureAllSet(); |
| 573 | 573 | 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); | |
| 576 | 576 | request = new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing()); |
| 577 | 577 | } else { |
| 578 | 578 | 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 | +} |