kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a35fec42e322607a1acd70545fb2dbbcf944f30eb28b163fbe0d59d156edb968 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-03-22 10:02 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-23 11:32 +01:00 |
| Parents | 25b5ffc807a5 |
4 files changed
modified changelog/README.md +1 −0
| @@ -39,6 +39,7 @@ | ||
| 39 | 39 | - [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized. |
| 40 | 40 | - [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0. |
| 41 | 41 | - [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. | |
| 42 | 43 | |
| 43 | 44 | Merged from 2.0 branch: |
| 44 | 45 |
modified driver-core/src/main/java/com/datastax/driver/core/Requests.java +15 −7
| @@ -230,15 +230,17 @@ | ||
| 230 | 230 | |
| 231 | 231 | public static class QueryProtocolOptions { |
| 232 | 232 | |
| 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, | |
| 234 | 236 | Collections.<ByteBuffer>emptyList(), |
| 235 | 237 | false, |
| 236 | 238 | -1, |
| 237 | 239 | null, |
| 238 | - ConsistencyLevel.SERIAL, | |
| 239 | - Long.MIN_VALUE); | |
| 240 | + ConsistencyLevel.SERIAL, Long.MIN_VALUE); | |
| 240 | 241 | |
| 241 | 242 | private final EnumSet<QueryFlag> flags = EnumSet.noneOf(QueryFlag.class); |
| 243 | + private final Message.Request.Type requestType; | |
| 242 | 244 | public final ConsistencyLevel consistency; |
| 243 | 245 | public final List<ByteBuffer> values; |
| 244 | 246 | public final boolean skipMetadata; |
| @@ -247,7 +249,8 @@ | ||
| 247 | 249 | public final ConsistencyLevel serialConsistency; |
| 248 | 250 | public final long defaultTimestamp; |
| 249 | 251 | |
| 250 | - public QueryProtocolOptions(ConsistencyLevel consistency, | |
| 252 | + public QueryProtocolOptions(Message.Request.Type requestType, | |
| 253 | + ConsistencyLevel consistency, | |
| 251 | 254 | List<ByteBuffer> values, |
| 252 | 255 | boolean skipMetadata, |
| 253 | 256 | int pageSize, |
| @@ -255,6 +258,7 @@ | ||
| 255 | 258 | ConsistencyLevel serialConsistency, |
| 256 | 259 | long defaultTimestamp) { |
| 257 | 260 | |
| 261 | + this.requestType = requestType; | |
| 258 | 262 | this.consistency = consistency; |
| 259 | 263 | this.values = values; |
| 260 | 264 | this.skipMetadata = skipMetadata; |
| @@ -279,13 +283,15 @@ | ||
| 279 | 283 | } |
| 280 | 284 | |
| 281 | 285 | 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); | |
| 283 | 287 | } |
| 284 | 288 | |
| 285 | 289 | public void encode(ByteBuf dest, ProtocolVersion version) { |
| 286 | 290 | switch (version) { |
| 287 | 291 | 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) | |
| 289 | 295 | CBUtil.writeValueList(values, dest); |
| 290 | 296 | CBUtil.writeConsistencyLevel(consistency, dest); |
| 291 | 297 | break; |
| @@ -312,7 +318,9 @@ | ||
| 312 | 318 | public int encodedSize(ProtocolVersion version) { |
| 313 | 319 | switch (version) { |
| 314 | 320 | 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) | |
| 316 | 324 | + CBUtil.sizeOfConsistencyLevel(consistency); |
| 317 | 325 | case V2: |
| 318 | 326 | case V3: |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +2 −3
| @@ -89,7 +89,6 @@ | ||
| 89 | 89 | ListenableFuture<?> allPoolsUpdatedFuture = Futures.transform(allPoolsCreatedFuture, |
| 90 | 90 | new AsyncFunction<Object, Object>() { |
| 91 | 91 | @Override |
| 92 | - @SuppressWarnings("unchecked") | |
| 93 | 92 | public ListenableFuture<Object> apply(Object input) throws Exception { |
| 94 | 93 | isInit = true; |
| 95 | 94 | return (ListenableFuture<Object>) updateCreatedPools(); |
| @@ -546,7 +545,7 @@ | ||
| 546 | 545 | |
| 547 | 546 | List<ByteBuffer> values = rawValues == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(rawValues); |
| 548 | 547 | 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, | |
| 550 | 549 | fetchSize, usedPagingState, serialConsistency, defaultTimestamp); |
| 551 | 550 | return new Requests.Query(qString, options, statement.isTracing()); |
| 552 | 551 | } else if (statement instanceof BoundStatement) { |
| @@ -557,7 +556,7 @@ | ||
| 557 | 556 | } |
| 558 | 557 | bs.ensureAllSet(); |
| 559 | 558 | 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, | |
| 561 | 560 | fetchSize, usedPagingState, serialConsistency, defaultTimestamp); |
| 562 | 561 | return new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing()); |
| 563 | 562 | } 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 | +} |