kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a9e072694f53ecff8dba60779e86510b4cab0e51e769aceed5b4dc4803451ed9 |
|---|---|
| Author | Alexandre Dutra <alexdut@gmail.com> · 2015-08-03 18:04 +02:00 |
| Committer | Alexandre Dutra <alexdut@gmail.com> · 2015-08-03 18:04 +02:00 |
| Parents | 743d7237895c, 0e2c5b044515 |
16 files changed
modified changelog/README.md +1 −0
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | - [bug] Preserve tracing across retries (JAVA-815) |
| 24 | 24 | - [improvement] New RetryDecision.tryNextHost() (JAVA-709) |
| 25 | 25 | - [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733) |
| 26 | +- [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765) | |
| 26 | 27 | |
| 27 | 28 | Merged from 2.0.10_fixes branch: |
| 28 | 29 |
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +21 −7
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | public class SimpleStatement extends RegularStatement { |
| 25 | 25 | |
| 26 | 26 | private final String query; |
| 27 | - private final ByteBuffer[] values; | |
| 27 | + private final Object[] values; | |
| 28 | 28 | |
| 29 | 29 | private volatile ByteBuffer routingKey; |
| 30 | 30 | private volatile String keyspace; |
| @@ -77,16 +77,13 @@ | ||
| 77 | 77 | * |
| 78 | 78 | * @param query the query string. |
| 79 | 79 | * @param values values required for the execution of {@code query}. |
| 80 | - * | |
| 81 | - * @throws IllegalArgumentException if one of {@code values} is not of a type | |
| 82 | - * corresponding to a CQL3 type, i.e. is not a Class that could be returned | |
| 83 | - * by {@link DataType#asJavaClass}. | |
| 80 | + * @throws IllegalArgumentException if the number of values is greater than 65535. | |
| 84 | 81 | */ |
| 85 | 82 | public SimpleStatement(String query, Object... values) { |
| 86 | 83 | if (values.length > 65535) |
| 87 | 84 | throw new IllegalArgumentException("Too many values, the maximum allowed is 65535"); |
| 88 | 85 | this.query = query; |
| 89 | - this.values = convert(values); | |
| 86 | + this.values = values; | |
| 90 | 87 | } |
| 91 | 88 | |
| 92 | 89 | private static ByteBuffer[] convert(Object[] values) { |
| @@ -117,7 +114,24 @@ | ||
| 117 | 114 | |
| 118 | 115 | @Override |
| 119 | 116 | public ByteBuffer[] getValues() { |
| 120 | - return values; | |
| 117 | + return values == null ? null : convert(values); | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 122 | + * | |
| 123 | + * @param i the index to retrieve. | |
| 124 | + * @return the value of the {@code i}th value of this statement. | |
| 125 | + * | |
| 126 | + * @throws IllegalStateException if this statement does not have values. | |
| 127 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 128 | + */ | |
| 129 | + public Object getObject(int i) { | |
| 130 | + if (values == null) | |
| 131 | + throw new IllegalStateException("This statement does not have values"); | |
| 132 | + if (i < 0 || i >= values.length) | |
| 133 | + throw new ArrayIndexOutOfBoundsException(i); | |
| 134 | + return values[i]; | |
| 121 | 135 | } |
| 122 | 136 | |
| 123 | 137 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Assignment.java +6 −7
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import java.nio.ByteBuffer; | |
| 19 | 18 | import java.util.List; |
| 20 | 19 | |
| 21 | 20 | import static com.datastax.driver.core.querybuilder.Utils.appendName; |
| @@ -41,7 +40,7 @@ | ||
| 41 | 40 | } |
| 42 | 41 | |
| 43 | 42 | @Override |
| 44 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 43 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 45 | 44 | appendName(name, sb); |
| 46 | 45 | sb.append('='); |
| 47 | 46 | appendValue(value, sb, variables); |
| @@ -75,7 +74,7 @@ | ||
| 75 | 74 | } |
| 76 | 75 | |
| 77 | 76 | @Override |
| 78 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 77 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 79 | 78 | appendName(name, sb).append('='); |
| 80 | 79 | appendName(name, sb).append(isIncr ? "+" : "-"); |
| 81 | 80 | appendValue(value, sb, variables); |
| @@ -102,7 +101,7 @@ | ||
| 102 | 101 | } |
| 103 | 102 | |
| 104 | 103 | @Override |
| 105 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 104 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 106 | 105 | appendName(name, sb).append('='); |
| 107 | 106 | appendValue(value, sb, variables); |
| 108 | 107 | sb.append('+'); |
| @@ -132,7 +131,7 @@ | ||
| 132 | 131 | } |
| 133 | 132 | |
| 134 | 133 | @Override |
| 135 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 134 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 136 | 135 | appendName(name, sb).append('[').append(idx).append("]="); |
| 137 | 136 | appendValue(value, sb, variables); |
| 138 | 137 | } |
| @@ -166,7 +165,7 @@ | ||
| 166 | 165 | } |
| 167 | 166 | |
| 168 | 167 | @Override |
| 169 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 168 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 170 | 169 | appendName(name, sb).append('='); |
| 171 | 170 | appendName(name, sb).append(isAdd ? "+" : "-"); |
| 172 | 171 | appendValue(collection, sb, variables); |
| @@ -195,7 +194,7 @@ | ||
| 195 | 194 | } |
| 196 | 195 | |
| 197 | 196 | @Override |
| 198 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 197 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 199 | 198 | appendName(name, sb).append('['); |
| 200 | 199 | appendValue(key, sb, variables); |
| 201 | 200 | sb.append("]="); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +1 −1
| @@ -48,7 +48,7 @@ | ||
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | @Override |
| 51 | - StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 51 | + StringBuilder buildQueryString(List<Object> variables) { | |
| 52 | 52 | StringBuilder builder = new StringBuilder(); |
| 53 | 53 | |
| 54 | 54 | builder.append(isCounterOp() |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +29 −11
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | |
| 37 | 37 | private boolean dirty; |
| 38 | 38 | private String cache; |
| 39 | - private ByteBuffer[] values; | |
| 39 | + private List<Object> values; | |
| 40 | 40 | |
| 41 | 41 | Boolean isCounterOp; |
| 42 | 42 | boolean hasNonIdempotentOps; |
| @@ -64,13 +64,30 @@ | ||
| 64 | 64 | return lowercaseId.matcher(ident).matches() ? ident : Metadata.quote(ident); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - | |
| 68 | 67 | @Override |
| 69 | 68 | public String getQueryString() { |
| 70 | 69 | maybeRebuildCache(); |
| 71 | 70 | return cache; |
| 72 | 71 | } |
| 73 | 72 | |
| 73 | + /** | |
| 74 | + * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 75 | + * | |
| 76 | + * @param i the index to retrieve. | |
| 77 | + * @return the value of the {@code i}th value of this statement. | |
| 78 | + * | |
| 79 | + * @throws IllegalStateException if this statement does not have values. | |
| 80 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 81 | + */ | |
| 82 | + public Object getObject(int i) { | |
| 83 | + maybeRebuildCache(); | |
| 84 | + if (values == null || values.isEmpty()) | |
| 85 | + throw new IllegalStateException("This statement does not have values"); | |
| 86 | + if (i < 0 || i >= values.size()) | |
| 87 | + throw new ArrayIndexOutOfBoundsException(i); | |
| 88 | + return values.get(i); | |
| 89 | + } | |
| 90 | + | |
| 74 | 91 | private void maybeRebuildCache() { |
| 75 | 92 | if (!dirty && cache != null) |
| 76 | 93 | return; |
| @@ -81,14 +98,14 @@ | ||
| 81 | 98 | if (hasBindMarkers || forceNoValues) { |
| 82 | 99 | sb = buildQueryString(null); |
| 83 | 100 | } else { |
| 84 | - List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 85 | - sb = buildQueryString(l); | |
| 101 | + values = new ArrayList<Object>(); | |
| 102 | + sb = buildQueryString(values); | |
| 86 | 103 | |
| 87 | - if (l.size() > 65535) | |
| 104 | + if (values.size() > 65535) | |
| 88 | 105 | throw new IllegalArgumentException("Too many values for built statement, the maximum allowed is 65535"); |
| 89 | 106 | |
| 90 | - if (!l.isEmpty()) | |
| 91 | - values = l.toArray(new ByteBuffer[l.size()]); | |
| 107 | + if (values.isEmpty()) | |
| 108 | + values = null; | |
| 92 | 109 | } |
| 93 | 110 | |
| 94 | 111 | maybeAddSemicolon(sb); |
| @@ -111,7 +128,7 @@ | ||
| 111 | 128 | return sb; |
| 112 | 129 | } |
| 113 | 130 | |
| 114 | - abstract StringBuilder buildQueryString(List<ByteBuffer> variables); | |
| 131 | + abstract StringBuilder buildQueryString(List<Object> variables); | |
| 115 | 132 | |
| 116 | 133 | boolean isCounterOp() { |
| 117 | 134 | return isCounterOp == null ? false : isCounterOp; |
| @@ -148,7 +165,8 @@ | ||
| 148 | 165 | |
| 149 | 166 | for (int i = 0; i < partitionKey.size(); i++) { |
| 150 | 167 | if (name.equals(partitionKey.get(i).getName()) && Utils.isRawValue(value)) { |
| 151 | - routingKey[i] = partitionKey.get(i).getType().parse(Utils.toRawString(value)); | |
| 168 | + DataType dt = partitionKey.get(i).getType(); | |
| 169 | + routingKey[i] = dt.serialize(value); | |
| 152 | 170 | return; |
| 153 | 171 | } |
| 154 | 172 | } |
| @@ -176,7 +194,7 @@ | ||
| 176 | 194 | @Override |
| 177 | 195 | public ByteBuffer[] getValues() { |
| 178 | 196 | maybeRebuildCache(); |
| 179 | - return values; | |
| 197 | + return values == null ? null : Utils.convert(values); | |
| 180 | 198 | } |
| 181 | 199 | |
| 182 | 200 | @Override |
| @@ -272,7 +290,7 @@ | ||
| 272 | 290 | } |
| 273 | 291 | |
| 274 | 292 | @Override |
| 275 | - StringBuilder buildQueryString(List<ByteBuffer> values) { | |
| 293 | + StringBuilder buildQueryString(List<Object> values) { | |
| 276 | 294 | return statement.buildQueryString(values); |
| 277 | 295 | } |
| 278 | 296 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +3 −4
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import java.nio.ByteBuffer; | |
| 19 | 18 | import java.util.List; |
| 20 | 19 | |
| 21 | 20 | public abstract class Clause extends Utils.Appendeable { |
| @@ -48,7 +47,7 @@ | ||
| 48 | 47 | } |
| 49 | 48 | |
| 50 | 49 | @Override |
| 51 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 50 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 52 | 51 | Utils.appendName(name, sb).append(op); |
| 53 | 52 | Utils.appendValue(value, sb, variables); |
| 54 | 53 | } |
| @@ -79,7 +78,7 @@ | ||
| 79 | 78 | } |
| 80 | 79 | |
| 81 | 80 | @Override |
| 82 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 81 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 83 | 82 | |
| 84 | 83 | // We special case the case of just one bind marker because there is little |
| 85 | 84 | // reasons to do: |
| @@ -146,7 +145,7 @@ | ||
| 146 | 145 | } |
| 147 | 146 | |
| 148 | 147 | @Override |
| 149 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 148 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 150 | 149 | sb.append("("); |
| 151 | 150 | for (int i = 0; i < names.size(); i++) { |
| 152 | 151 | if (i > 0) |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +4 −5
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import java.nio.ByteBuffer; | |
| 19 | 18 | import java.util.ArrayList; |
| 20 | 19 | import java.util.List; |
| 21 | 20 | |
| @@ -52,7 +51,7 @@ | ||
| 52 | 51 | } |
| 53 | 52 | |
| 54 | 53 | @Override |
| 55 | - StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 54 | + StringBuilder buildQueryString(List<Object> variables) { | |
| 56 | 55 | StringBuilder builder = new StringBuilder(); |
| 57 | 56 | |
| 58 | 57 | builder.append("DELETE"); |
| @@ -414,7 +413,7 @@ | ||
| 414 | 413 | } |
| 415 | 414 | |
| 416 | 415 | @Override |
| 417 | - void appendTo(StringBuilder sb, List<ByteBuffer> values) { | |
| 416 | + void appendTo(StringBuilder sb, List<Object> values) { | |
| 418 | 417 | Utils.appendName(columnName, sb); |
| 419 | 418 | } |
| 420 | 419 | |
| @@ -426,7 +425,7 @@ | ||
| 426 | 425 | @Override |
| 427 | 426 | public String toString() { |
| 428 | 427 | StringBuilder sb = new StringBuilder(); |
| 429 | - appendTo(sb, new ArrayList<ByteBuffer>()); | |
| 428 | + appendTo(sb, new ArrayList<Object>()); | |
| 430 | 429 | return sb.toString(); |
| 431 | 430 | } |
| 432 | 431 | } |
| @@ -444,7 +443,7 @@ | ||
| 444 | 443 | } |
| 445 | 444 | |
| 446 | 445 | @Override |
| 447 | - void appendTo(StringBuilder sb, List<ByteBuffer> values) { | |
| 446 | + void appendTo(StringBuilder sb, List<Object> values) { | |
| 448 | 447 | super.appendTo(sb, values); |
| 449 | 448 | sb.append('['); |
| 450 | 449 | Utils.appendFlatValue(key, sb); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +1 −1
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | @Override |
| 49 | - StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 49 | + StringBuilder buildQueryString(List<Object> variables) { | |
| 50 | 50 | StringBuilder builder = new StringBuilder(); |
| 51 | 51 | |
| 52 | 52 | builder.append("INSERT INTO "); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Ordering.java +1 −1
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | @Override |
| 32 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 32 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 33 | 33 | Utils.appendName(name, sb); |
| 34 | 34 | sb.append(isDesc ? " DESC" : " ASC"); |
| 35 | 35 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +1 −2
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import java.nio.ByteBuffer; | |
| 19 | 18 | import java.util.ArrayList; |
| 20 | 19 | import java.util.Arrays; |
| 21 | 20 | import java.util.Collections; |
| @@ -55,7 +54,7 @@ | ||
| 55 | 54 | } |
| 56 | 55 | |
| 57 | 56 | @Override |
| 58 | - StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 57 | + StringBuilder buildQueryString(List<Object> variables) { | |
| 59 | 58 | StringBuilder builder = new StringBuilder(); |
| 60 | 59 | |
| 61 | 60 | builder.append("SELECT "); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +1 −1
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | @Override |
| 41 | - protected StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 41 | + protected StringBuilder buildQueryString(List<Object> variables) { | |
| 42 | 42 | StringBuilder builder = new StringBuilder(); |
| 43 | 43 | |
| 44 | 44 | builder.append("TRUNCATE "); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +1 −1
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | @Override |
| 55 | - StringBuilder buildQueryString(List<ByteBuffer> variables) { | |
| 55 | + StringBuilder buildQueryString(List<Object> variables) { | |
| 56 | 56 | StringBuilder builder = new StringBuilder(); |
| 57 | 57 | |
| 58 | 58 | builder.append("UPDATE "); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Using.java +2 −2
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | @Override |
| 38 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 38 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 39 | 39 | sb.append(optionName).append(' ').append(value); |
| 40 | 40 | } |
| 41 | 41 | |
| @@ -54,7 +54,7 @@ | ||
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | @Override |
| 57 | - void appendTo(StringBuilder sb, List<ByteBuffer> variables) { | |
| 57 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 58 | 58 | sb.append(optionName).append(' ').append(marker); |
| 59 | 59 | } |
| 60 | 60 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +49 −58
| @@ -25,14 +25,13 @@ | ||
| 25 | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | 26 | |
| 27 | 27 | import com.datastax.driver.core.DataType; |
| 28 | -import com.datastax.driver.core.utils.Bytes; | |
| 29 | 28 | |
| 30 | 29 | // Static utilities private to the query builder |
| 31 | 30 | abstract class Utils { |
| 32 | 31 | |
| 33 | 32 | private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?"); |
| 34 | 33 | |
| 35 | - static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<ByteBuffer> variables) { | |
| 34 | + static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<Object> variables) { | |
| 36 | 35 | for (int i = 0; i < values.size(); i++) { |
| 37 | 36 | if (i > 0) |
| 38 | 37 | sb.append(separator); |
| @@ -50,7 +49,7 @@ | ||
| 50 | 49 | return sb; |
| 51 | 50 | } |
| 52 | 51 | |
| 53 | - static StringBuilder joinAndAppendValues(StringBuilder sb, String separator, List<?> values, List<ByteBuffer> variables) { | |
| 52 | + static StringBuilder joinAndAppendValues(StringBuilder sb, String separator, List<?> values, List<Object> variables) { | |
| 54 | 53 | for (int i = 0; i < values.size(); i++) { |
| 55 | 54 | if (i > 0) |
| 56 | 55 | sb.append(separator); |
| @@ -59,37 +58,42 @@ | ||
| 59 | 58 | return sb; |
| 60 | 59 | } |
| 61 | 60 | |
| 62 | - // Returns null if it's not really serializable (function call, bind markers, ...) | |
| 63 | - static ByteBuffer serializeValue(Object value) { | |
| 64 | - if (value == QueryBuilder.bindMarker() || value instanceof FCall || value instanceof CName) | |
| 65 | - return null; | |
| 61 | + // Returns false if it's not really serializable (function call, bind markers, ...) | |
| 62 | + static boolean isSerializable(Object value) { | |
| 63 | + if (value instanceof BindMarker || value instanceof FCall || value instanceof CName) | |
| 64 | + return false; | |
| 66 | 65 | |
| 67 | 66 | if (value instanceof RawString) |
| 68 | - return null; | |
| 67 | + return false; | |
| 69 | 68 | |
| 70 | 69 | // We also don't serialize fixed size number types. The reason is that if we do it, we will |
| 71 | 70 | // force a particular size (4 bytes for ints, ...) and for the query builder, we don't want |
| 72 | 71 | // users to have to bother with that. |
| 73 | 72 | if (value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal)) |
| 74 | - return null; | |
| 75 | - | |
| 76 | - try { | |
| 77 | - return DataType.serializeValue(value); | |
| 78 | - } catch (IllegalArgumentException e) { | |
| 79 | - return null; | |
| 80 | - } | |
| 73 | + return false; | |
| 74 | + | |
| 75 | + return true; | |
| 81 | 76 | } |
| 82 | 77 | |
| 83 | - static StringBuilder appendValue(Object value, StringBuilder sb, List<ByteBuffer> variables) { | |
| 84 | - if (variables == null) | |
| 85 | - return appendValue(value, sb, false); | |
| 78 | + static ByteBuffer[] convert(List<Object> values) { | |
| 79 | + ByteBuffer[] serializedValues = new ByteBuffer[values.size()]; | |
| 80 | + for (int i = 0; i < values.size(); i++) { | |
| 81 | + try { | |
| 82 | + serializedValues[i] = DataType.serializeValue(values.get(i)); | |
| 83 | + } catch (IllegalArgumentException e) { | |
| 84 | + // Catch and rethrow to provide a more helpful error message (one that include which value is bad) | |
| 85 | + throw new IllegalArgumentException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, values.get(i).getClass())); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + return serializedValues; | |
| 89 | + } | |
| 86 | 90 | |
| 87 | - ByteBuffer bb = serializeValue(value); | |
| 88 | - if (bb == null) | |
| 91 | + static StringBuilder appendValue(Object value, StringBuilder sb, List<Object> variables) { | |
| 92 | + if (variables == null || !isSerializable(value)) | |
| 89 | 93 | return appendValue(value, sb, false); |
| 90 | 94 | |
| 91 | 95 | sb.append('?'); |
| 92 | - variables.add(bb); | |
| 96 | + variables.add(value); | |
| 93 | 97 | return sb; |
| 94 | 98 | } |
| 95 | 99 | |
| @@ -140,13 +144,13 @@ | ||
| 140 | 144 | sb.append(value); |
| 141 | 145 | return true; |
| 142 | 146 | } else if (value instanceof InetAddress) { |
| 143 | - sb.append('\'').append(((InetAddress)value).getHostAddress()).append('\''); | |
| 147 | + sb.append(DataType.inet().format(value)); | |
| 144 | 148 | return true; |
| 145 | 149 | } else if (value instanceof Date) { |
| 146 | - sb.append(((Date)value).getTime()); | |
| 150 | + sb.append(DataType.timestamp().format(value)); | |
| 147 | 151 | return true; |
| 148 | 152 | } else if (value instanceof ByteBuffer) { |
| 149 | - sb.append(Bytes.toHexString((ByteBuffer) value)); | |
| 153 | + sb.append(DataType.blob().format(value)); | |
| 150 | 154 | return true; |
| 151 | 155 | } else if (value instanceof BindMarker) { |
| 152 | 156 | sb.append(value); |
| @@ -188,23 +192,18 @@ | ||
| 188 | 192 | } |
| 189 | 193 | } |
| 190 | 194 | |
| 191 | - static StringBuilder appendCollection(Object value, StringBuilder sb, List<ByteBuffer> variables) { | |
| 192 | - ByteBuffer bb = variables == null ? null : serializeValue(value); | |
| 193 | - if (bb == null) { | |
| 195 | + static StringBuilder appendCollection(Object value, StringBuilder sb, List<Object> variables) { | |
| 196 | + if (variables == null || !isSerializable(value)) { | |
| 194 | 197 | boolean wasCollection = appendValueIfCollection(value, sb, false); |
| 195 | 198 | assert wasCollection; |
| 196 | 199 | } else { |
| 197 | 200 | sb.append('?'); |
| 198 | - variables.add(bb); | |
| 201 | + variables.add(value); | |
| 199 | 202 | } |
| 200 | 203 | return sb; |
| 201 | 204 | } |
| 202 | 205 | |
| 203 | - static StringBuilder appendList(List<?> l, StringBuilder sb) { | |
| 204 | - return appendList(l, sb, false); | |
| 205 | - } | |
| 206 | - | |
| 207 | - private static StringBuilder appendList(List<?> l, StringBuilder sb, boolean rawValue) { | |
| 206 | + static StringBuilder appendList(List<?> l, StringBuilder sb, boolean rawValue) { | |
| 208 | 207 | sb.append('['); |
| 209 | 208 | for (int i = 0; i < l.size(); i++) { |
| 210 | 209 | if (i > 0) |
| @@ -215,11 +214,7 @@ | ||
| 215 | 214 | return sb; |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | - static StringBuilder appendSet(Set<?> s, StringBuilder sb) { | |
| 219 | - return appendSet(s, sb, false); | |
| 220 | - } | |
| 221 | - | |
| 222 | - private static StringBuilder appendSet(Set<?> s, StringBuilder sb, boolean rawValue) { | |
| 217 | + static StringBuilder appendSet(Set<?> s, StringBuilder sb, boolean rawValue) { | |
| 223 | 218 | sb.append('{'); |
| 224 | 219 | boolean first = true; |
| 225 | 220 | for (Object elt : s) { |
| @@ -230,8 +225,20 @@ | ||
| 230 | 225 | return sb; |
| 231 | 226 | } |
| 232 | 227 | |
| 233 | - static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb) { | |
| 234 | - return appendMap(m, sb, false); | |
| 228 | + static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) { | |
| 229 | + sb.append('{'); | |
| 230 | + boolean first = true; | |
| 231 | + for (Map.Entry<?, ?> entry : m.entrySet()) { | |
| 232 | + if (first) | |
| 233 | + first = false; | |
| 234 | + else | |
| 235 | + sb.append(','); | |
| 236 | + appendFlatValue(entry.getKey(), sb, rawValue); | |
| 237 | + sb.append(':'); | |
| 238 | + appendFlatValue(entry.getValue(), sb, rawValue); | |
| 239 | + } | |
| 240 | + sb.append('}'); | |
| 241 | + return sb; | |
| 235 | 242 | } |
| 236 | 243 | |
| 237 | 244 | static boolean containsBindMarker(Object value) { |
| @@ -273,24 +280,8 @@ | ||
| 273 | 280 | return true; |
| 274 | 281 | } |
| 275 | 282 | |
| 276 | - private static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) { | |
| 277 | - sb.append('{'); | |
| 278 | - boolean first = true; | |
| 279 | - for (Map.Entry<?, ?> entry : m.entrySet()) { | |
| 280 | - if (first) | |
| 281 | - first = false; | |
| 282 | - else | |
| 283 | - sb.append(','); | |
| 284 | - appendFlatValue(entry.getKey(), sb, rawValue); | |
| 285 | - sb.append(':'); | |
| 286 | - appendFlatValue(entry.getValue(), sb, rawValue); | |
| 287 | - } | |
| 288 | - sb.append('}'); | |
| 289 | - return sb; | |
| 290 | - } | |
| 291 | - | |
| 292 | 283 | private static StringBuilder appendValueString(String value, StringBuilder sb) { |
| 293 | - return sb.append('\'').append(replace(value, '\'', "''")).append('\''); | |
| 284 | + return sb.append(DataType.text().format(value)); | |
| 294 | 285 | } |
| 295 | 286 | |
| 296 | 287 | static boolean isRawValue(Object value) { |
| @@ -339,7 +330,7 @@ | ||
| 339 | 330 | } |
| 340 | 331 | |
| 341 | 332 | static abstract class Appendeable { |
| 342 | - abstract void appendTo(StringBuilder sb, List<ByteBuffer> values); | |
| 333 | + abstract void appendTo(StringBuilder sb, List<Object> values); | |
| 343 | 334 | abstract boolean containsBindMarker(); |
| 344 | 335 | } |
| 345 | 336 |
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStatementTest.java +23 −1
| @@ -20,10 +20,32 @@ | ||
| 20 | 20 | |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 24 | + | |
| 23 | 25 | public class SimpleStatementTest { |
| 26 | + | |
| 24 | 27 | @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) |
| 25 | 28 | public void should_fail_if_too_many_variables() { |
| 26 | 29 | List<Object> args = Collections.nCopies(1 << 16, (Object)1); |
| 27 | 30 | new SimpleStatement("mock query", args.toArray()); |
| 28 | 31 | } |
| 29 | -} | |
| No newline at the end of the file | ||
| 32 | + | |
| 33 | + @Test(groups = "unit", expectedExceptions = { IllegalStateException.class }) | |
| 34 | + public void should_throw_ISE_if_getObject_called_on_statement_without_values() { | |
| 35 | + new SimpleStatement("doesn't matter").getObject(0); | |
| 36 | + } | |
| 37 | + | |
| 38 | + | |
| 39 | + @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class }) | |
| 40 | + public void should_throw_IOOBE_if_getObject_called_with_wrong_index() { | |
| 41 | + new SimpleStatement("doesn't matter", new Object()).getObject(1); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Test(groups = "unit") | |
| 45 | + public void should_return_object_at_ith_index() { | |
| 46 | + Object expected = new Object(); | |
| 47 | + Object actual = new SimpleStatement("doesn't matter", expected).getObject(0); | |
| 48 | + assertThat(actual).isSameAs(expected); | |
| 49 | + } | |
| 50 | + | |
| 51 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +18 −2
| @@ -18,7 +18,6 @@ | ||
| 18 | 18 | import java.math.BigDecimal; |
| 19 | 19 | import java.math.BigInteger; |
| 20 | 20 | import java.net.InetAddress; |
| 21 | -import java.nio.ByteBuffer; | |
| 22 | 21 | import java.util.*; |
| 23 | 22 | |
| 24 | 23 | import org.testng.annotations.Test; |
| @@ -28,7 +27,6 @@ | ||
| 28 | 27 | import static org.testng.Assert.assertTrue; |
| 29 | 28 | import static org.testng.Assert.fail; |
| 30 | 29 | |
| 31 | -import com.datastax.driver.core.Assertions; | |
| 32 | 30 | import com.datastax.driver.core.ConsistencyLevel; |
| 33 | 31 | import com.datastax.driver.core.RegularStatement; |
| 34 | 32 | import com.datastax.driver.core.Statement; |
| @@ -776,4 +774,22 @@ | ||
| 776 | 774 | assertThat(select.getQueryString()).doesNotContain("?"); |
| 777 | 775 | assertThat(select.getValues()).isNull(); |
| 778 | 776 | } |
| 777 | + | |
| 778 | + @Test(groups = "unit", expectedExceptions = { IllegalStateException.class }) | |
| 779 | + public void should_throw_ISE_if_getObject_called_on_statement_without_values() { | |
| 780 | + select().from("test").where(eq("foo", 42)).getObject(0); // integers are appended to the CQL string | |
| 781 | + } | |
| 782 | + | |
| 783 | + @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class }) | |
| 784 | + public void should_throw_IOOBE_if_getObject_called_with_wrong_index() { | |
| 785 | + select().from("test").where(eq("foo", new Object())).getObject(1); | |
| 786 | + } | |
| 787 | + | |
| 788 | + @Test(groups = "unit") | |
| 789 | + public void should_return_object_at_ith_index() { | |
| 790 | + Object expected = new Object(); | |
| 791 | + Object actual = select().from("test").where(eq("foo", expected)).getObject(0); | |
| 792 | + assertThat(actual).isSameAs(expected); | |
| 793 | + } | |
| 794 | + | |
| 779 | 795 | } |