kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 42972cf86120a1cd794d3787a0251e562630ca6991cfe5f64a1ed51999cf2be8 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-03-27 11:11 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-27 11:11 +01:00 |
| Parents | 2e24721d7f5d, 54bf5eab8439 |
13 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | (JAVA-655) |
| 19 | 19 | - [bug] Empty TokenRange returned in a one token cluster (JAVA-684) |
| 20 | 20 | - [improvement] Expose TokenRange#contains (JAVA-687) |
| 21 | +- [new feature] Expose values of BoundStatement (JAVA-547) | |
| 22 | +- [new feature] Add getObject to BoundStatement and Row (JAVA-584) | |
| 21 | 23 | |
| 22 | 24 | Merged from 2.0.9_fixes branch: |
| 23 | 25 |
added driver-core/src/main/java/com/datastax/driver/core/AbstractGettableData.java +315 −0
| @@ -0,0 +1,315 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 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 java.math.BigDecimal; | |
| 19 | +import java.math.BigInteger; | |
| 20 | +import java.net.InetAddress; | |
| 21 | +import java.nio.ByteBuffer; | |
| 22 | +import java.util.*; | |
| 23 | + | |
| 24 | +import com.google.common.collect.Lists; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 27 | + | |
| 28 | +abstract class AbstractGettableData implements GettableData { | |
| 29 | + | |
| 30 | + protected final ColumnDefinitions metadata; | |
| 31 | + | |
| 32 | + protected AbstractGettableData(ColumnDefinitions metadata) { | |
| 33 | + this.metadata = metadata; | |
| 34 | + } | |
| 35 | + | |
| 36 | + protected abstract ByteBuffer getValue(int i); | |
| 37 | + | |
| 38 | + public boolean isNull(int i) { | |
| 39 | + metadata.checkBounds(i); | |
| 40 | + return getValue(i) == null; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public boolean isNull(String name) { | |
| 44 | + return isNull(metadata.getFirstIdx(name)); | |
| 45 | + } | |
| 46 | + | |
| 47 | + public boolean getBool(int i) { | |
| 48 | + metadata.checkType(i, DataType.Name.BOOLEAN); | |
| 49 | + | |
| 50 | + ByteBuffer value = getValue(i); | |
| 51 | + if (value == null || value.remaining() == 0) | |
| 52 | + return false; | |
| 53 | + | |
| 54 | + return TypeCodec.BooleanCodec.instance.deserializeNoBoxing(value); | |
| 55 | + } | |
| 56 | + | |
| 57 | + public boolean getBool(String name) { | |
| 58 | + return getBool(metadata.getFirstIdx(name)); | |
| 59 | + } | |
| 60 | + | |
| 61 | + public int getInt(int i) { | |
| 62 | + metadata.checkType(i, DataType.Name.INT); | |
| 63 | + | |
| 64 | + ByteBuffer value = getValue(i); | |
| 65 | + if (value == null || value.remaining() == 0) | |
| 66 | + return 0; | |
| 67 | + | |
| 68 | + return TypeCodec.IntCodec.instance.deserializeNoBoxing(value); | |
| 69 | + } | |
| 70 | + | |
| 71 | + public int getInt(String name) { | |
| 72 | + return getInt(metadata.getFirstIdx(name)); | |
| 73 | + } | |
| 74 | + | |
| 75 | + public long getLong(int i) { | |
| 76 | + metadata.checkType(i, DataType.Name.BIGINT, DataType.Name.COUNTER); | |
| 77 | + | |
| 78 | + ByteBuffer value = getValue(i); | |
| 79 | + if (value == null || value.remaining() == 0) | |
| 80 | + return 0L; | |
| 81 | + | |
| 82 | + return TypeCodec.LongCodec.instance.deserializeNoBoxing(value); | |
| 83 | + } | |
| 84 | + | |
| 85 | + public long getLong(String name) { | |
| 86 | + return getLong(metadata.getFirstIdx(name)); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public Date getDate(int i) { | |
| 90 | + metadata.checkType(i, DataType.Name.TIMESTAMP); | |
| 91 | + | |
| 92 | + ByteBuffer value = getValue(i); | |
| 93 | + if (value == null || value.remaining() == 0) | |
| 94 | + return null; | |
| 95 | + | |
| 96 | + return TypeCodec.DateCodec.instance.deserialize(value); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public Date getDate(String name) { | |
| 100 | + return getDate(metadata.getFirstIdx(name)); | |
| 101 | + } | |
| 102 | + | |
| 103 | + public float getFloat(int i) { | |
| 104 | + metadata.checkType(i, DataType.Name.FLOAT); | |
| 105 | + | |
| 106 | + ByteBuffer value = getValue(i); | |
| 107 | + if (value == null || value.remaining() == 0) | |
| 108 | + return 0.0f; | |
| 109 | + | |
| 110 | + return TypeCodec.FloatCodec.instance.deserializeNoBoxing(value); | |
| 111 | + } | |
| 112 | + | |
| 113 | + public float getFloat(String name) { | |
| 114 | + return getFloat(metadata.getFirstIdx(name)); | |
| 115 | + } | |
| 116 | + | |
| 117 | + public double getDouble(int i) { | |
| 118 | + metadata.checkType(i, DataType.Name.DOUBLE); | |
| 119 | + | |
| 120 | + ByteBuffer value = getValue(i); | |
| 121 | + if (value == null || value.remaining() == 0) | |
| 122 | + return 0.0; | |
| 123 | + | |
| 124 | + return TypeCodec.DoubleCodec.instance.deserializeNoBoxing(value); | |
| 125 | + } | |
| 126 | + | |
| 127 | + public double getDouble(String name) { | |
| 128 | + return getDouble(metadata.getFirstIdx(name)); | |
| 129 | + } | |
| 130 | + | |
| 131 | + public ByteBuffer getBytesUnsafe(int i) { | |
| 132 | + metadata.checkBounds(i); | |
| 133 | + | |
| 134 | + ByteBuffer value = getValue(i); | |
| 135 | + if (value == null) | |
| 136 | + return null; | |
| 137 | + | |
| 138 | + return value.duplicate(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + public ByteBuffer getBytesUnsafe(String name) { | |
| 142 | + return getBytesUnsafe(metadata.getFirstIdx(name)); | |
| 143 | + } | |
| 144 | + | |
| 145 | + public ByteBuffer getBytes(int i) { | |
| 146 | + metadata.checkType(i, DataType.Name.BLOB); | |
| 147 | + return getBytesUnsafe(i); | |
| 148 | + } | |
| 149 | + | |
| 150 | + public ByteBuffer getBytes(String name) { | |
| 151 | + return getBytes(metadata.getFirstIdx(name)); | |
| 152 | + } | |
| 153 | + | |
| 154 | + public String getString(int i) { | |
| 155 | + DataType.Name type = metadata.checkType(i, DataType.Name.VARCHAR, | |
| 156 | + DataType.Name.TEXT, | |
| 157 | + DataType.Name.ASCII); | |
| 158 | + | |
| 159 | + ByteBuffer value = getValue(i); | |
| 160 | + if (value == null) | |
| 161 | + return null; | |
| 162 | + | |
| 163 | + return type == DataType.Name.ASCII | |
| 164 | + ? TypeCodec.StringCodec.asciiInstance.deserialize(value) | |
| 165 | + : TypeCodec.StringCodec.utf8Instance.deserialize(value); | |
| 166 | + } | |
| 167 | + | |
| 168 | + public String getString(String name) { | |
| 169 | + return getString(metadata.getFirstIdx(name)); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public BigInteger getVarint(int i) { | |
| 173 | + metadata.checkType(i, DataType.Name.VARINT); | |
| 174 | + | |
| 175 | + ByteBuffer value = getValue(i); | |
| 176 | + if (value == null || value.remaining() == 0) | |
| 177 | + return null; | |
| 178 | + | |
| 179 | + return TypeCodec.BigIntegerCodec.instance.deserialize(value); | |
| 180 | + } | |
| 181 | + | |
| 182 | + public BigInteger getVarint(String name) { | |
| 183 | + return getVarint(metadata.getFirstIdx(name)); | |
| 184 | + } | |
| 185 | + | |
| 186 | + public BigDecimal getDecimal(int i) { | |
| 187 | + metadata.checkType(i, DataType.Name.DECIMAL); | |
| 188 | + | |
| 189 | + ByteBuffer value = getValue(i); | |
| 190 | + if (value == null || value.remaining() == 0) | |
| 191 | + return null; | |
| 192 | + | |
| 193 | + return TypeCodec.DecimalCodec.instance.deserialize(value); | |
| 194 | + } | |
| 195 | + | |
| 196 | + public BigDecimal getDecimal(String name) { | |
| 197 | + return getDecimal(metadata.getFirstIdx(name)); | |
| 198 | + } | |
| 199 | + | |
| 200 | + public UUID getUUID(int i) { | |
| 201 | + DataType.Name type = metadata.checkType(i, DataType.Name.UUID, DataType.Name.TIMEUUID); | |
| 202 | + | |
| 203 | + ByteBuffer value = getValue(i); | |
| 204 | + if (value == null || value.remaining() == 0) | |
| 205 | + return null; | |
| 206 | + | |
| 207 | + return type == DataType.Name.UUID | |
| 208 | + ? TypeCodec.UUIDCodec.instance.deserialize(value) | |
| 209 | + : TypeCodec.TimeUUIDCodec.instance.deserialize(value); | |
| 210 | + } | |
| 211 | + | |
| 212 | + public UUID getUUID(String name) { | |
| 213 | + return getUUID(metadata.getFirstIdx(name)); | |
| 214 | + } | |
| 215 | + | |
| 216 | + public InetAddress getInet(int i) { | |
| 217 | + metadata.checkType(i, DataType.Name.INET); | |
| 218 | + | |
| 219 | + ByteBuffer value = getValue(i); | |
| 220 | + if (value == null || value.remaining() == 0) | |
| 221 | + return null; | |
| 222 | + | |
| 223 | + return TypeCodec.InetCodec.instance.deserialize(value); | |
| 224 | + } | |
| 225 | + | |
| 226 | + public InetAddress getInet(String name) { | |
| 227 | + return getInet(metadata.getFirstIdx(name)); | |
| 228 | + } | |
| 229 | + | |
| 230 | + @SuppressWarnings("unchecked") | |
| 231 | + public <T> List<T> getList(int i, Class<T> elementsClass) { | |
| 232 | + DataType type = metadata.getType(i); | |
| 233 | + if (type.getName() != DataType.Name.LIST) | |
| 234 | + throw new InvalidTypeException(String.format("Column %s is not of list type", metadata.getName(i))); | |
| 235 | + | |
| 236 | + Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType; | |
| 237 | + if (!elementsClass.isAssignableFrom(expectedClass)) | |
| 238 | + throw new InvalidTypeException(String.format("Column %s is a list of %s (CQL type %s), cannot be retrieve as a list of %s", metadata.getName(i), expectedClass, type, elementsClass)); | |
| 239 | + | |
| 240 | + ByteBuffer value = getValue(i); | |
| 241 | + if (value == null) | |
| 242 | + return Collections.<T>emptyList(); | |
| 243 | + | |
| 244 | + return Collections.unmodifiableList((List<T>)type.codec().deserialize(value)); | |
| 245 | + } | |
| 246 | + | |
| 247 | + public <T> List<T> getList(String name, Class<T> elementsClass) { | |
| 248 | + return getList(metadata.getFirstIdx(name), elementsClass); | |
| 249 | + } | |
| 250 | + | |
| 251 | + @SuppressWarnings("unchecked") | |
| 252 | + public <T> Set<T> getSet(int i, Class<T> elementsClass) { | |
| 253 | + DataType type = metadata.getType(i); | |
| 254 | + if (type.getName() != DataType.Name.SET) | |
| 255 | + throw new InvalidTypeException(String.format("Column %s is not of set type", metadata.getName(i))); | |
| 256 | + | |
| 257 | + Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType; | |
| 258 | + if (!elementsClass.isAssignableFrom(expectedClass)) | |
| 259 | + throw new InvalidTypeException(String.format("Column %s is a set of %s (CQL type %s), cannot be retrieve as a set of %s", metadata.getName(i), expectedClass, type, elementsClass)); | |
| 260 | + | |
| 261 | + ByteBuffer value = getValue(i); | |
| 262 | + if (value == null) | |
| 263 | + return Collections.<T>emptySet(); | |
| 264 | + | |
| 265 | + return Collections.unmodifiableSet((Set<T>)type.codec().deserialize(value)); | |
| 266 | + } | |
| 267 | + | |
| 268 | + public <T> Set<T> getSet(String name, Class<T> elementsClass) { | |
| 269 | + return getSet(metadata.getFirstIdx(name), elementsClass); | |
| 270 | + } | |
| 271 | + | |
| 272 | + @SuppressWarnings("unchecked") | |
| 273 | + public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) { | |
| 274 | + DataType type = metadata.getType(i); | |
| 275 | + if (type.getName() != DataType.Name.MAP) | |
| 276 | + throw new InvalidTypeException(String.format("Column %s is not of map type", metadata.getName(i))); | |
| 277 | + | |
| 278 | + Class<?> expectedKeysClass = type.getTypeArguments().get(0).getName().javaType; | |
| 279 | + Class<?> expectedValuesClass = type.getTypeArguments().get(1).getName().javaType; | |
| 280 | + if (!keysClass.isAssignableFrom(expectedKeysClass) || !valuesClass.isAssignableFrom(expectedValuesClass)) | |
| 281 | + throw new InvalidTypeException(String.format("Column %s is a map of %s->%s (CQL type %s), cannot be retrieve as a map of %s->%s", metadata.getName(i), expectedKeysClass, expectedValuesClass, type, keysClass, valuesClass)); | |
| 282 | + | |
| 283 | + ByteBuffer value = getValue(i); | |
| 284 | + if (value == null) | |
| 285 | + return Collections.<K, V>emptyMap(); | |
| 286 | + | |
| 287 | + return Collections.unmodifiableMap((Map<K, V>)type.codec().deserialize(value)); | |
| 288 | + } | |
| 289 | + | |
| 290 | + public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass) { | |
| 291 | + return getMap(metadata.getFirstIdx(name), keysClass, valuesClass); | |
| 292 | + } | |
| 293 | + | |
| 294 | + public Object getObject(int i) { | |
| 295 | + ByteBuffer raw = getValue(i); | |
| 296 | + DataType type = metadata.getType(i); | |
| 297 | + if (raw == null) | |
| 298 | + switch (type.getName()) { | |
| 299 | + case LIST: | |
| 300 | + return Collections.emptyList(); | |
| 301 | + case SET: | |
| 302 | + return Collections.emptySet(); | |
| 303 | + case MAP: | |
| 304 | + return Collections.emptyMap(); | |
| 305 | + default: | |
| 306 | + return null; | |
| 307 | + } | |
| 308 | + else | |
| 309 | + return type.deserialize(raw); | |
| 310 | + } | |
| 311 | + | |
| 312 | + public Object getObject(String name) { | |
| 313 | + return getValue(metadata.getFirstIdx(name)); | |
| 314 | + } | |
| 315 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedRow.java +7 −263
| @@ -15,26 +15,21 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.math.BigDecimal; | |
| 19 | -import java.math.BigInteger; | |
| 20 | -import java.net.InetAddress; | |
| 21 | 18 | import java.nio.ByteBuffer; |
| 22 | -import java.util.*; | |
| 19 | +import java.util.List; | |
| 23 | 20 | |
| 24 | 21 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 25 | -import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 26 | 22 | |
| 27 | 23 | /** |
| 28 | 24 | * Implementation of a Row backed by an ArrayList. |
| 29 | 25 | */ |
| 30 | -class ArrayBackedRow implements Row { | |
| 26 | +class ArrayBackedRow extends AbstractGettableData implements Row { | |
| 31 | 27 | |
| 32 | - private final ColumnDefinitions metadata; | |
| 33 | 28 | private final Token.Factory tokenFactory; |
| 34 | 29 | private final List<ByteBuffer> data; |
| 35 | 30 | |
| 36 | 31 | private ArrayBackedRow(ColumnDefinitions metadata, Token.Factory tokenFactory, List<ByteBuffer> data) { |
| 37 | - this.metadata = metadata; | |
| 32 | + super(metadata); | |
| 38 | 33 | this.tokenFactory = tokenFactory; |
| 39 | 34 | this.data = data; |
| 40 | 35 | } |
| @@ -50,196 +45,9 @@ | ||
| 50 | 45 | return metadata; |
| 51 | 46 | } |
| 52 | 47 | |
| 53 | - public boolean isNull(int i) { | |
| 54 | - metadata.checkBounds(i); | |
| 55 | - return data.get(i) == null; | |
| 56 | - } | |
| 57 | - | |
| 58 | - public boolean isNull(String name) { | |
| 59 | - return isNull(metadata.getFirstIdx(name)); | |
| 60 | - } | |
| 61 | - | |
| 62 | - public boolean getBool(int i) { | |
| 63 | - metadata.checkType(i, DataType.Name.BOOLEAN); | |
| 64 | - | |
| 65 | - ByteBuffer value = data.get(i); | |
| 66 | - if (value == null || value.remaining() == 0) | |
| 67 | - return false; | |
| 68 | - | |
| 69 | - return TypeCodec.BooleanCodec.instance.deserializeNoBoxing(value); | |
| 70 | - } | |
| 71 | - | |
| 72 | - public boolean getBool(String name) { | |
| 73 | - return getBool(metadata.getFirstIdx(name)); | |
| 74 | - } | |
| 75 | - | |
| 76 | - public int getInt(int i) { | |
| 77 | - metadata.checkType(i, DataType.Name.INT); | |
| 78 | - | |
| 79 | - ByteBuffer value = data.get(i); | |
| 80 | - if (value == null || value.remaining() == 0) | |
| 81 | - return 0; | |
| 82 | - | |
| 83 | - return TypeCodec.IntCodec.instance.deserializeNoBoxing(value); | |
| 84 | - } | |
| 85 | - | |
| 86 | - public int getInt(String name) { | |
| 87 | - return getInt(metadata.getFirstIdx(name)); | |
| 88 | - } | |
| 89 | - | |
| 90 | - public long getLong(int i) { | |
| 91 | - metadata.checkType(i, DataType.Name.BIGINT, DataType.Name.COUNTER); | |
| 92 | - | |
| 93 | - ByteBuffer value = data.get(i); | |
| 94 | - if (value == null || value.remaining() == 0) | |
| 95 | - return 0L; | |
| 96 | - | |
| 97 | - return TypeCodec.LongCodec.instance.deserializeNoBoxing(value); | |
| 98 | - } | |
| 99 | - | |
| 100 | - public long getLong(String name) { | |
| 101 | - return getLong(metadata.getFirstIdx(name)); | |
| 102 | - } | |
| 103 | - | |
| 104 | - public Date getDate(int i) { | |
| 105 | - metadata.checkType(i, DataType.Name.TIMESTAMP); | |
| 106 | - | |
| 107 | - ByteBuffer value = data.get(i); | |
| 108 | - if (value == null || value.remaining() == 0) | |
| 109 | - return null; | |
| 110 | - | |
| 111 | - return TypeCodec.DateCodec.instance.deserialize(value); | |
| 112 | - } | |
| 113 | - | |
| 114 | - public Date getDate(String name) { | |
| 115 | - return getDate(metadata.getFirstIdx(name)); | |
| 116 | - } | |
| 117 | - | |
| 118 | - public float getFloat(int i) { | |
| 119 | - metadata.checkType(i, DataType.Name.FLOAT); | |
| 120 | - | |
| 121 | - ByteBuffer value = data.get(i); | |
| 122 | - if (value == null || value.remaining() == 0) | |
| 123 | - return 0.0f; | |
| 124 | - | |
| 125 | - return TypeCodec.FloatCodec.instance.deserializeNoBoxing(value); | |
| 126 | - } | |
| 127 | - | |
| 128 | - public float getFloat(String name) { | |
| 129 | - return getFloat(metadata.getFirstIdx(name)); | |
| 130 | - } | |
| 131 | - | |
| 132 | - public double getDouble(int i) { | |
| 133 | - metadata.checkType(i, DataType.Name.DOUBLE); | |
| 134 | - | |
| 135 | - ByteBuffer value = data.get(i); | |
| 136 | - if (value == null || value.remaining() == 0) | |
| 137 | - return 0.0; | |
| 138 | - | |
| 139 | - return TypeCodec.DoubleCodec.instance.deserializeNoBoxing(value); | |
| 140 | - } | |
| 141 | - | |
| 142 | - public double getDouble(String name) { | |
| 143 | - return getDouble(metadata.getFirstIdx(name)); | |
| 144 | - } | |
| 145 | - | |
| 146 | - public ByteBuffer getBytesUnsafe(int i) { | |
| 147 | - metadata.checkBounds(i); | |
| 148 | - | |
| 149 | - ByteBuffer value = data.get(i); | |
| 150 | - if (value == null) | |
| 151 | - return null; | |
| 152 | - | |
| 153 | - return value.duplicate(); | |
| 154 | - } | |
| 155 | - | |
| 156 | - public ByteBuffer getBytesUnsafe(String name) { | |
| 157 | - return getBytesUnsafe(metadata.getFirstIdx(name)); | |
| 158 | - } | |
| 159 | - | |
| 160 | - public ByteBuffer getBytes(int i) { | |
| 161 | - metadata.checkType(i, DataType.Name.BLOB); | |
| 162 | - return getBytesUnsafe(i); | |
| 163 | - } | |
| 164 | - | |
| 165 | - public ByteBuffer getBytes(String name) { | |
| 166 | - return getBytes(metadata.getFirstIdx(name)); | |
| 167 | - } | |
| 168 | - | |
| 169 | - public String getString(int i) { | |
| 170 | - DataType.Name type = metadata.checkType(i, DataType.Name.VARCHAR, | |
| 171 | - DataType.Name.TEXT, | |
| 172 | - DataType.Name.ASCII); | |
| 173 | - | |
| 174 | - ByteBuffer value = data.get(i); | |
| 175 | - if (value == null) | |
| 176 | - return null; | |
| 177 | - | |
| 178 | - return type == DataType.Name.ASCII | |
| 179 | - ? TypeCodec.StringCodec.asciiInstance.deserialize(value) | |
| 180 | - : TypeCodec.StringCodec.utf8Instance.deserialize(value); | |
| 181 | - } | |
| 182 | - | |
| 183 | - public String getString(String name) { | |
| 184 | - return getString(metadata.getFirstIdx(name)); | |
| 185 | - } | |
| 186 | - | |
| 187 | - public BigInteger getVarint(int i) { | |
| 188 | - metadata.checkType(i, DataType.Name.VARINT); | |
| 189 | - | |
| 190 | - ByteBuffer value = data.get(i); | |
| 191 | - if (value == null || value.remaining() == 0) | |
| 192 | - return null; | |
| 193 | - | |
| 194 | - return TypeCodec.BigIntegerCodec.instance.deserialize(value); | |
| 195 | - } | |
| 196 | - | |
| 197 | - public BigInteger getVarint(String name) { | |
| 198 | - return getVarint(metadata.getFirstIdx(name)); | |
| 199 | - } | |
| 200 | - | |
| 201 | - public BigDecimal getDecimal(int i) { | |
| 202 | - metadata.checkType(i, DataType.Name.DECIMAL); | |
| 203 | - | |
| 204 | - ByteBuffer value = data.get(i); | |
| 205 | - if (value == null || value.remaining() == 0) | |
| 206 | - return null; | |
| 207 | - | |
| 208 | - return TypeCodec.DecimalCodec.instance.deserialize(value); | |
| 209 | - } | |
| 210 | - | |
| 211 | - public BigDecimal getDecimal(String name) { | |
| 212 | - return getDecimal(metadata.getFirstIdx(name)); | |
| 213 | - } | |
| 214 | - | |
| 215 | - public UUID getUUID(int i) { | |
| 216 | - DataType.Name type = metadata.checkType(i, DataType.Name.UUID, DataType.Name.TIMEUUID); | |
| 217 | - | |
| 218 | - ByteBuffer value = data.get(i); | |
| 219 | - if (value == null || value.remaining() == 0) | |
| 220 | - return null; | |
| 221 | - | |
| 222 | - return type == DataType.Name.UUID | |
| 223 | - ? TypeCodec.UUIDCodec.instance.deserialize(value) | |
| 224 | - : TypeCodec.TimeUUIDCodec.instance.deserialize(value); | |
| 225 | - } | |
| 226 | - | |
| 227 | - public UUID getUUID(String name) { | |
| 228 | - return getUUID(metadata.getFirstIdx(name)); | |
| 229 | - } | |
| 230 | - | |
| 231 | - public InetAddress getInet(int i) { | |
| 232 | - metadata.checkType(i, DataType.Name.INET); | |
| 233 | - | |
| 234 | - ByteBuffer value = data.get(i); | |
| 235 | - if (value == null || value.remaining() == 0) | |
| 236 | - return null; | |
| 237 | - | |
| 238 | - return TypeCodec.InetCodec.instance.deserialize(value); | |
| 239 | - } | |
| 240 | - | |
| 241 | - public InetAddress getInet(String name) { | |
| 242 | - return getInet(metadata.getFirstIdx(name)); | |
| 48 | + @Override | |
| 49 | + protected ByteBuffer getValue(int i) { | |
| 50 | + return data.get(i); | |
| 243 | 51 | } |
| 244 | 52 | |
| 245 | 53 | public Token getToken(int i) { |
| @@ -269,70 +77,6 @@ | ||
| 269 | 77 | throw new IllegalStateException("Found no column named 'token(...)'. If the column is aliased, use getToken(String)."); |
| 270 | 78 | } |
| 271 | 79 | |
| 272 | - @SuppressWarnings("unchecked") | |
| 273 | - public <T> List<T> getList(int i, Class<T> elementsClass) { | |
| 274 | - DataType type = metadata.getType(i); | |
| 275 | - if (type.getName() != DataType.Name.LIST) | |
| 276 | - throw new InvalidTypeException(String.format("Column %s is not of list type", metadata.getName(i))); | |
| 277 | - | |
| 278 | - Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType; | |
| 279 | - if (!elementsClass.isAssignableFrom(expectedClass)) | |
| 280 | - throw new InvalidTypeException(String.format("Column %s is a list of %s (CQL type %s), cannot be retrieve as a list of %s", metadata.getName(i), expectedClass, type, elementsClass)); | |
| 281 | - | |
| 282 | - ByteBuffer value = data.get(i); | |
| 283 | - if (value == null) | |
| 284 | - return Collections.<T>emptyList(); | |
| 285 | - | |
| 286 | - return Collections.unmodifiableList((List<T>)type.codec().deserialize(value)); | |
| 287 | - } | |
| 288 | - | |
| 289 | - public <T> List<T> getList(String name, Class<T> elementsClass) { | |
| 290 | - return getList(metadata.getFirstIdx(name), elementsClass); | |
| 291 | - } | |
| 292 | - | |
| 293 | - @SuppressWarnings("unchecked") | |
| 294 | - public <T> Set<T> getSet(int i, Class<T> elementsClass) { | |
| 295 | - DataType type = metadata.getType(i); | |
| 296 | - if (type.getName() != DataType.Name.SET) | |
| 297 | - throw new InvalidTypeException(String.format("Column %s is not of set type", metadata.getName(i))); | |
| 298 | - | |
| 299 | - Class<?> expectedClass = type.getTypeArguments().get(0).getName().javaType; | |
| 300 | - if (!elementsClass.isAssignableFrom(expectedClass)) | |
| 301 | - throw new InvalidTypeException(String.format("Column %s is a set of %s (CQL type %s), cannot be retrieve as a set of %s", metadata.getName(i), expectedClass, type, elementsClass)); | |
| 302 | - | |
| 303 | - ByteBuffer value = data.get(i); | |
| 304 | - if (value == null) | |
| 305 | - return Collections.<T>emptySet(); | |
| 306 | - | |
| 307 | - return Collections.unmodifiableSet((Set<T>)type.codec().deserialize(value)); | |
| 308 | - } | |
| 309 | - | |
| 310 | - public <T> Set<T> getSet(String name, Class<T> elementsClass) { | |
| 311 | - return getSet(metadata.getFirstIdx(name), elementsClass); | |
| 312 | - } | |
| 313 | - | |
| 314 | - @SuppressWarnings("unchecked") | |
| 315 | - public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) { | |
| 316 | - DataType type = metadata.getType(i); | |
| 317 | - if (type.getName() != DataType.Name.MAP) | |
| 318 | - throw new InvalidTypeException(String.format("Column %s is not of map type", metadata.getName(i))); | |
| 319 | - | |
| 320 | - Class<?> expectedKeysClass = type.getTypeArguments().get(0).getName().javaType; | |
| 321 | - Class<?> expectedValuesClass = type.getTypeArguments().get(1).getName().javaType; | |
| 322 | - if (!keysClass.isAssignableFrom(expectedKeysClass) || !valuesClass.isAssignableFrom(expectedValuesClass)) | |
| 323 | - throw new InvalidTypeException(String.format("Column %s is a map of %s->%s (CQL type %s), cannot be retrieve as a map of %s->%s", metadata.getName(i), expectedKeysClass, expectedValuesClass, type, keysClass, valuesClass)); | |
| 324 | - | |
| 325 | - ByteBuffer value = data.get(i); | |
| 326 | - if (value == null) | |
| 327 | - return Collections.<K, V>emptyMap(); | |
| 328 | - | |
| 329 | - return Collections.unmodifiableMap((Map<K, V>)type.codec().deserialize(value)); | |
| 330 | - } | |
| 331 | - | |
| 332 | - public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass) { | |
| 333 | - return getMap(metadata.getFirstIdx(name), keysClass, valuesClass); | |
| 334 | - } | |
| 335 | - | |
| 336 | 80 | @Override |
| 337 | 81 | public String toString() { |
| 338 | 82 | StringBuilder sb = new StringBuilder(); |
| @@ -340,7 +84,7 @@ | ||
| 340 | 84 | for (int i = 0; i < metadata.size(); i++) { |
| 341 | 85 | if (i != 0) |
| 342 | 86 | sb.append(", "); |
| 343 | - ByteBuffer bb = data.get(i); | |
| 87 | + ByteBuffer bb = getValue(i); | |
| 344 | 88 | if (bb == null) |
| 345 | 89 | sb.append("NULL"); |
| 346 | 90 | else |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +208 −1
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | import java.nio.ByteBuffer; |
| 22 | 22 | import java.util.*; |
| 23 | 23 | |
| 24 | +import com.google.common.collect.Lists; | |
| 25 | + | |
| 24 | 26 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 25 | 27 | |
| 26 | 28 | /** |
| @@ -37,13 +39,20 @@ | ||
| 37 | 39 | * variables for that name. |
| 38 | 40 | * <p> |
| 39 | 41 | * Any variable that hasn't been specifically set will be considered {@code null}. |
| 42 | + * <p> | |
| 43 | + * Bound values may also be retrieved using {@code get*()} methods. Note that this | |
| 44 | + * may have a non-negligible impact on performance: internally, values are stored | |
| 45 | + * in serialized form, so they need to be deserialized again. These methods are | |
| 46 | + * provided for debugging purposes. | |
| 40 | 47 | */ |
| 41 | -public class BoundStatement extends Statement { | |
| 48 | +public class BoundStatement extends Statement implements GettableData { | |
| 42 | 49 | |
| 43 | 50 | final PreparedStatement statement; |
| 44 | 51 | final ByteBuffer[] values; |
| 45 | 52 | private ByteBuffer routingKey; |
| 46 | 53 | |
| 54 | + private DataWrapper wrapper; | |
| 55 | + | |
| 47 | 56 | /** |
| 48 | 57 | * Creates a new {@code BoundStatement} from the provided prepared |
| 49 | 58 | * statement. |
| @@ -53,6 +62,10 @@ | ||
| 53 | 62 | this.statement = statement; |
| 54 | 63 | this.values = new ByteBuffer[statement.getVariables().size()]; |
| 55 | 64 | |
| 65 | + // We want to reuse code from AbstractGettableData, but this class already extends Statement, | |
| 66 | + // so we emulate a mixin with a delegate. | |
| 67 | + this.wrapper = new DataWrapper(this); | |
| 68 | + | |
| 56 | 69 | if (statement.getConsistencyLevel() != null) |
| 57 | 70 | this.setConsistencyLevel(statement.getConsistencyLevel()); |
| 58 | 71 | if (statement.getSerialConsistencyLevel() != null) |
| @@ -1104,6 +1117,186 @@ | ||
| 1104 | 1117 | return this; |
| 1105 | 1118 | } |
| 1106 | 1119 | |
| 1120 | + @Override | |
| 1121 | + public boolean isNull(int i) { | |
| 1122 | + return wrapper.isNull(i); | |
| 1123 | + } | |
| 1124 | + | |
| 1125 | + @Override | |
| 1126 | + public boolean isNull(String name) { | |
| 1127 | + return wrapper.isNull(name); | |
| 1128 | + } | |
| 1129 | + | |
| 1130 | + @Override | |
| 1131 | + public boolean getBool(int i) { | |
| 1132 | + return wrapper.getBool(i); | |
| 1133 | + } | |
| 1134 | + | |
| 1135 | + @Override | |
| 1136 | + public boolean getBool(String name) { | |
| 1137 | + return wrapper.getBool(name); | |
| 1138 | + } | |
| 1139 | + | |
| 1140 | + @Override | |
| 1141 | + public int getInt(int i) { | |
| 1142 | + return wrapper.getInt(i); | |
| 1143 | + } | |
| 1144 | + | |
| 1145 | + @Override | |
| 1146 | + public int getInt(String name) { | |
| 1147 | + return wrapper.getInt(name); | |
| 1148 | + } | |
| 1149 | + | |
| 1150 | + @Override | |
| 1151 | + public long getLong(int i) { | |
| 1152 | + return wrapper.getLong(i); | |
| 1153 | + } | |
| 1154 | + | |
| 1155 | + @Override | |
| 1156 | + public long getLong(String name) { | |
| 1157 | + return wrapper.getLong(name); | |
| 1158 | + } | |
| 1159 | + | |
| 1160 | + @Override | |
| 1161 | + public Date getDate(int i) { | |
| 1162 | + return wrapper.getDate(i); | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + @Override | |
| 1166 | + public Date getDate(String name) { | |
| 1167 | + return wrapper.getDate(name); | |
| 1168 | + } | |
| 1169 | + | |
| 1170 | + @Override | |
| 1171 | + public float getFloat(int i) { | |
| 1172 | + return wrapper.getFloat(i); | |
| 1173 | + } | |
| 1174 | + | |
| 1175 | + @Override | |
| 1176 | + public float getFloat(String name) { | |
| 1177 | + return wrapper.getFloat(name); | |
| 1178 | + } | |
| 1179 | + | |
| 1180 | + @Override | |
| 1181 | + public double getDouble(int i) { | |
| 1182 | + return wrapper.getDouble(i); | |
| 1183 | + } | |
| 1184 | + | |
| 1185 | + @Override | |
| 1186 | + public double getDouble(String name) { | |
| 1187 | + return wrapper.getDouble(name); | |
| 1188 | + } | |
| 1189 | + | |
| 1190 | + @Override | |
| 1191 | + public ByteBuffer getBytesUnsafe(int i) { | |
| 1192 | + return wrapper.getBytesUnsafe(i); | |
| 1193 | + } | |
| 1194 | + | |
| 1195 | + @Override | |
| 1196 | + public ByteBuffer getBytesUnsafe(String name) { | |
| 1197 | + return wrapper.getBytesUnsafe(name); | |
| 1198 | + } | |
| 1199 | + | |
| 1200 | + @Override | |
| 1201 | + public ByteBuffer getBytes(int i) { | |
| 1202 | + return wrapper.getBytes(i); | |
| 1203 | + } | |
| 1204 | + | |
| 1205 | + @Override | |
| 1206 | + public ByteBuffer getBytes(String name) { | |
| 1207 | + return wrapper.getBytes(name); | |
| 1208 | + } | |
| 1209 | + | |
| 1210 | + @Override | |
| 1211 | + public String getString(int i) { | |
| 1212 | + return wrapper.getString(i); | |
| 1213 | + } | |
| 1214 | + | |
| 1215 | + @Override | |
| 1216 | + public String getString(String name) { | |
| 1217 | + return wrapper.getString(name); | |
| 1218 | + } | |
| 1219 | + | |
| 1220 | + @Override | |
| 1221 | + public BigInteger getVarint(int i) { | |
| 1222 | + return wrapper.getVarint(i); | |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + @Override | |
| 1226 | + public BigInteger getVarint(String name) { | |
| 1227 | + return wrapper.getVarint(name); | |
| 1228 | + } | |
| 1229 | + | |
| 1230 | + @Override | |
| 1231 | + public BigDecimal getDecimal(int i) { | |
| 1232 | + return wrapper.getDecimal(i); | |
| 1233 | + } | |
| 1234 | + | |
| 1235 | + @Override | |
| 1236 | + public BigDecimal getDecimal(String name) { | |
| 1237 | + return wrapper.getDecimal(name); | |
| 1238 | + } | |
| 1239 | + | |
| 1240 | + @Override | |
| 1241 | + public UUID getUUID(int i) { | |
| 1242 | + return wrapper.getUUID(i); | |
| 1243 | + } | |
| 1244 | + | |
| 1245 | + @Override | |
| 1246 | + public UUID getUUID(String name) { | |
| 1247 | + return wrapper.getUUID(name); | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + @Override | |
| 1251 | + public InetAddress getInet(int i) { | |
| 1252 | + return wrapper.getInet(i); | |
| 1253 | + } | |
| 1254 | + | |
| 1255 | + @Override | |
| 1256 | + public InetAddress getInet(String name) { | |
| 1257 | + return wrapper.getInet(name); | |
| 1258 | + } | |
| 1259 | + | |
| 1260 | + @Override | |
| 1261 | + public <T> List<T> getList(int i, Class<T> elementsClass) { | |
| 1262 | + return wrapper.getList(i, elementsClass); | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | + @Override | |
| 1266 | + public <T> List<T> getList(String name, Class<T> elementsClass) { | |
| 1267 | + return wrapper.getList(name, elementsClass); | |
| 1268 | + } | |
| 1269 | + | |
| 1270 | + @Override | |
| 1271 | + public <T> Set<T> getSet(int i, Class<T> elementsClass) { | |
| 1272 | + return wrapper.getSet(i, elementsClass); | |
| 1273 | + } | |
| 1274 | + | |
| 1275 | + @Override | |
| 1276 | + public <T> Set<T> getSet(String name, Class<T> elementsClass) { | |
| 1277 | + return wrapper.getSet(name, elementsClass); | |
| 1278 | + } | |
| 1279 | + | |
| 1280 | + @Override | |
| 1281 | + public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) { | |
| 1282 | + return wrapper.getMap(i, keysClass, valuesClass); | |
| 1283 | + } | |
| 1284 | + | |
| 1285 | + @Override | |
| 1286 | + public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass) { | |
| 1287 | + return wrapper.getMap(name, keysClass, valuesClass); | |
| 1288 | + } | |
| 1289 | + | |
| 1290 | + @Override | |
| 1291 | + public Object getObject(int i) { | |
| 1292 | + return wrapper.getObject(i); | |
| 1293 | + } | |
| 1294 | + | |
| 1295 | + @Override | |
| 1296 | + public Object getObject(String name) { | |
| 1297 | + return wrapper.getObject(name); | |
| 1298 | + } | |
| 1299 | + | |
| 1107 | 1300 | private ColumnDefinitions metadata() { |
| 1108 | 1301 | return statement.getVariables(); |
| 1109 | 1302 | } |
| @@ -1112,4 +1305,18 @@ | ||
| 1112 | 1305 | values[i] = value; |
| 1113 | 1306 | return this; |
| 1114 | 1307 | } |
| 1308 | + | |
| 1309 | + static class DataWrapper extends AbstractGettableData { | |
| 1310 | + final ByteBuffer[] values; | |
| 1311 | + | |
| 1312 | + DataWrapper(BoundStatement wrapped) { | |
| 1313 | + super(wrapped.metadata()); | |
| 1314 | + values = wrapped.values; | |
| 1315 | + } | |
| 1316 | + | |
| 1317 | + @Override | |
| 1318 | + protected ByteBuffer getValue(int i) { | |
| 1319 | + return values[i]; | |
| 1320 | + } | |
| 1321 | + } | |
| 1115 | 1322 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +14 −0
| @@ -457,6 +457,20 @@ | ||
| 457 | 457 | return codec().serialize(codec.parse(value)); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | + /** | |
| 461 | + * Format a Java object as an equivalent CQL value. | |
| 462 | + * | |
| 463 | + * @param value the value to format. | |
| 464 | + * @return a string corresponding to the CQL representation of {@code value}. | |
| 465 | + * | |
| 466 | + * @throws InvalidTypeException if {@code value} does not correspond to | |
| 467 | + * a CQL value (known by the driver). Please note that for custom types this | |
| 468 | + * method will always return this exception. | |
| 469 | + */ | |
| 470 | + public String format(Object value) { | |
| 471 | + return value == null ? null : codec().format(value); | |
| 472 | + } | |
| 473 | + | |
| 460 | 474 | /** |
| 461 | 475 | * Returns whether this type is a collection one, i.e. a list, set or map type. |
| 462 | 476 | * |
added driver-core/src/main/java/com/datastax/driver/core/GettableData.java +500 −0
| @@ -0,0 +1,500 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.math.BigDecimal; | |
| 4 | +import java.math.BigInteger; | |
| 5 | +import java.net.InetAddress; | |
| 6 | +import java.nio.ByteBuffer; | |
| 7 | +import java.util.*; | |
| 8 | + | |
| 9 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Collection of (typed) CQL values that can be retrieved by index (starting at 0), or by name. | |
| 13 | + */ | |
| 14 | +public interface GettableData { | |
| 15 | + /** | |
| 16 | + * Returns whether the {@code i}th value is NULL. | |
| 17 | + * | |
| 18 | + * @param i the index ({@code 0 <= i < size()}) of the value to check. | |
| 19 | + * @return whether the {@code i}th value is NULL. | |
| 20 | + * | |
| 21 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 22 | + */ | |
| 23 | + boolean isNull(int i); | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Returns whether the value for {@code name} is NULL. | |
| 27 | + * | |
| 28 | + * @param name the name of the value to check. | |
| 29 | + * @return whether the value for {@code name} is NULL. | |
| 30 | + * | |
| 31 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 32 | + */ | |
| 33 | + boolean isNull(String name); | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Returns the {@code i}th value as a boolean. | |
| 37 | + * | |
| 38 | + * @param i the index to retrieve. | |
| 39 | + * @return the boolean value of the {@code i}th value. If the | |
| 40 | + * value is NULL, {@code false} is returned. | |
| 41 | + * | |
| 42 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 43 | + * @throws InvalidTypeException if value {@code i} is not of type BOOLEAN. | |
| 44 | + */ | |
| 45 | + boolean getBool(int i); | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Returns the value for {@code name} as a boolean. | |
| 49 | + * | |
| 50 | + * @param name the name to retrieve. | |
| 51 | + * @return the boolean value for {@code name}. If the value is NULL, | |
| 52 | + * {@code false} is returned. | |
| 53 | + * | |
| 54 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 55 | + * @throws InvalidTypeException if value {@code name} is not of type BOOLEAN. | |
| 56 | + */ | |
| 57 | + boolean getBool(String name); | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Returns the {@code i}th value as an integer. | |
| 61 | + * | |
| 62 | + * @param i the index to retrieve. | |
| 63 | + * @return the value of the {@code i}th value as an integer. If the | |
| 64 | + * value is NULL, {@code 0} is returned. | |
| 65 | + * | |
| 66 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 67 | + * @throws InvalidTypeException if value {@code i} is not of type INT. | |
| 68 | + */ | |
| 69 | + int getInt(int i); | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Returns the value for {@code name} as an integer. | |
| 73 | + * | |
| 74 | + * @param name the name to retrieve. | |
| 75 | + * @return the value for {@code name} as an integer. If the value is NULL, | |
| 76 | + * {@code 0} is returned. | |
| 77 | + * | |
| 78 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 79 | + * @throws InvalidTypeException if value {@code name} is not of type INT. | |
| 80 | + */ | |
| 81 | + int getInt(String name); | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Returns the {@code i}th value as a long. | |
| 85 | + * | |
| 86 | + * @param i the index to retrieve. | |
| 87 | + * @return the value of the {@code i}th value as a long. If the | |
| 88 | + * value is NULL, {@code 0L} is returned. | |
| 89 | + * | |
| 90 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 91 | + * @throws InvalidTypeException if value {@code i} is not of type BIGINT or COUNTER. | |
| 92 | + */ | |
| 93 | + long getLong(int i); | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Returns the value for {@code name} as a long. | |
| 97 | + * | |
| 98 | + * @param name the name to retrieve. | |
| 99 | + * @return the value for {@code name} as a long. If the value is NULL, | |
| 100 | + * {@code 0L} is returned. | |
| 101 | + * | |
| 102 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 103 | + * @throws InvalidTypeException if value {@code i} is not of type BIGINT or COUNTER. | |
| 104 | + */ | |
| 105 | + long getLong(String name); | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Returns the {@code i}th value as a date. | |
| 109 | + * | |
| 110 | + * @param i the index to retrieve. | |
| 111 | + * @return the value of the {@code i}th value as a data. If the | |
| 112 | + * value is NULL, {@code null} is returned. | |
| 113 | + * | |
| 114 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 115 | + * @throws InvalidTypeException if value {@code i} is not of type TIMESTAMP. | |
| 116 | + */ | |
| 117 | + Date getDate(int i); | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Returns the value for {@code name} as a date. | |
| 121 | + * | |
| 122 | + * @param name the name to retrieve. | |
| 123 | + * @return the value for {@code name} as a date. If the value is NULL, | |
| 124 | + * {@code null} is returned. | |
| 125 | + * | |
| 126 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 127 | + * @throws InvalidTypeException if value {@code name} is not of type TIMESTAMP. | |
| 128 | + */ | |
| 129 | + Date getDate(String name); | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Returns the {@code i}th value as a float. | |
| 133 | + * | |
| 134 | + * @param i the index to retrieve. | |
| 135 | + * @return the value of the {@code i}th value as a float. If the | |
| 136 | + * value is NULL, {@code 0.0f} is returned. | |
| 137 | + * | |
| 138 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 139 | + * @throws InvalidTypeException if value {@code i} is not of type FLOAT. | |
| 140 | + */ | |
| 141 | + float getFloat(int i); | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Returns the value for {@code name} as a float. | |
| 145 | + * | |
| 146 | + * @param name the name to retrieve. | |
| 147 | + * @return the value for {@code name} as a float. If the value is NULL, | |
| 148 | + * {@code 0.0f} is returned. | |
| 149 | + * | |
| 150 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 151 | + * @throws InvalidTypeException if value {@code name} is not of type FLOAT. | |
| 152 | + */ | |
| 153 | + float getFloat(String name); | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Returns the {@code i}th value as a double. | |
| 157 | + * | |
| 158 | + * @param i the index to retrieve. | |
| 159 | + * @return the value of the {@code i}th value as a double. If the | |
| 160 | + * value is NULL, {@code 0.0} is returned. | |
| 161 | + * | |
| 162 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 163 | + * @throws InvalidTypeException if value {@code i} is not of type DOUBLE. | |
| 164 | + */ | |
| 165 | + double getDouble(int i); | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Returns the value for {@code name} as a double. | |
| 169 | + * | |
| 170 | + * @param name the name to retrieve. | |
| 171 | + * @return the value for {@code name} as a double. If the value is NULL, | |
| 172 | + * {@code 0.0} is returned. | |
| 173 | + * | |
| 174 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 175 | + * @throws InvalidTypeException if value {@code name} is not of type DOUBLE. | |
| 176 | + */ | |
| 177 | + double getDouble(String name); | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Returns the {@code i}th value as a ByteBuffer. | |
| 181 | + * | |
| 182 | + * Note: this method always return the bytes composing the value, even if | |
| 183 | + * it is not of type BLOB. That is, this method never throw an | |
| 184 | + * InvalidTypeException. However, if the type is not BLOB, it is up to the | |
| 185 | + * caller to handle the returned value correctly. | |
| 186 | + * | |
| 187 | + * @param i the index to retrieve. | |
| 188 | + * @return the value of the {@code i}th value as a ByteBuffer. If the | |
| 189 | + * value is NULL, {@code null} is returned. | |
| 190 | + * | |
| 191 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 192 | + */ | |
| 193 | + ByteBuffer getBytesUnsafe(int i); | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Returns the value for {@code name} as a ByteBuffer. | |
| 197 | + * | |
| 198 | + * Note: this method always return the bytes composing the value, even if | |
| 199 | + * it is not of type BLOB. That is, this method never throw an | |
| 200 | + * InvalidTypeException. However, if the type is not BLOB, it is up to the | |
| 201 | + * caller to handle the returned value correctly. | |
| 202 | + * | |
| 203 | + * @param name the name to retrieve. | |
| 204 | + * @return the value for {@code name} as a ByteBuffer. If the value is NULL, | |
| 205 | + * {@code null} is returned. | |
| 206 | + * | |
| 207 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 208 | + */ | |
| 209 | + ByteBuffer getBytesUnsafe(String name); | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Returns the {@code i}th value as a byte array. | |
| 213 | + * <p> | |
| 214 | + * Note that this method validate that the value is of type BLOB. If you want to retrieve | |
| 215 | + * the bytes for any type of values, use {@link #getBytesUnsafe(int)} instead. | |
| 216 | + * | |
| 217 | + * @param i the index to retrieve. | |
| 218 | + * @return the value of the {@code i}th value as a byte array. If the | |
| 219 | + * value is NULL, {@code null} is returned. | |
| 220 | + * | |
| 221 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 222 | + * @throws InvalidTypeException if value {@code i} type is not of type BLOB. | |
| 223 | + */ | |
| 224 | + ByteBuffer getBytes(int i); | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Returns the value for {@code name} as a byte array. | |
| 228 | + * <p> | |
| 229 | + * Note that this method validate that the value is of type BLOB. If you want to retrieve | |
| 230 | + * the bytes for any type of values, use {@link #getBytesUnsafe(String)} instead. | |
| 231 | + * | |
| 232 | + * @param name the name to retrieve. | |
| 233 | + * @return the value for {@code name} as a byte array. If the value is NULL, | |
| 234 | + * {@code null} is returned. | |
| 235 | + * | |
| 236 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 237 | + * @throws InvalidTypeException if value {@code i} type is not of type BLOB. | |
| 238 | + */ | |
| 239 | + ByteBuffer getBytes(String name); | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Returns the {@code i}th value as a string. | |
| 243 | + * | |
| 244 | + * @param i the index to retrieve. | |
| 245 | + * @return the value of the {@code i}th value as a string. If the | |
| 246 | + * value is NULL, {@code null} is returned. | |
| 247 | + * | |
| 248 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 249 | + * @throws InvalidTypeException if value {@code i} type is none of: | |
| 250 | + * VARCHAR, TEXT or ASCII. | |
| 251 | + */ | |
| 252 | + String getString(int i); | |
| 253 | + | |
| 254 | + /** | |
| 255 | + * Returns the value for {@code name} as a string. | |
| 256 | + * | |
| 257 | + * @param name the name to retrieve. | |
| 258 | + * @return the value for {@code name} as a string. If the value is NULL, | |
| 259 | + * {@code null} is returned. | |
| 260 | + * | |
| 261 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 262 | + * @throws InvalidTypeException if value {@code name} type is none of: | |
| 263 | + * VARCHAR, TEXT or ASCII. | |
| 264 | + */ | |
| 265 | + String getString(String name); | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Returns the {@code i}th value as a variable length integer. | |
| 269 | + * | |
| 270 | + * @param i the index to retrieve. | |
| 271 | + * @return the value of the {@code i}th value as a variable | |
| 272 | + * length integer. If the value is NULL, {@code null} is returned. | |
| 273 | + * | |
| 274 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 275 | + * @throws InvalidTypeException if value {@code i} is not of type VARINT. | |
| 276 | + */ | |
| 277 | + BigInteger getVarint(int i); | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * Returns the value for {@code name} as a variable length integer. | |
| 281 | + * | |
| 282 | + * @param name the name to retrieve. | |
| 283 | + * @return the value for {@code name} as a variable length integer. | |
| 284 | + * If the value is NULL, {@code null} is returned. | |
| 285 | + * | |
| 286 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 287 | + * @throws InvalidTypeException if value {@code name} is not of type VARINT. | |
| 288 | + */ | |
| 289 | + BigInteger getVarint(String name); | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * Returns the {@code i}th value as a variable length decimal. | |
| 293 | + * | |
| 294 | + * @param i the index to retrieve. | |
| 295 | + * @return the value of the {@code i}th value as a variable | |
| 296 | + * length decimal. If the value is NULL, {@code null} is returned. | |
| 297 | + * | |
| 298 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 299 | + * @throws InvalidTypeException if value {@code i} is not of type DECIMAL. | |
| 300 | + */ | |
| 301 | + BigDecimal getDecimal(int i); | |
| 302 | + | |
| 303 | + /** | |
| 304 | + * Returns the value for {@code name} as a variable length decimal. | |
| 305 | + * | |
| 306 | + * @param name the name to retrieve. | |
| 307 | + * @return the value for {@code name} as a variable length decimal. | |
| 308 | + * If the value is NULL, {@code null} is returned. | |
| 309 | + * | |
| 310 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 311 | + * @throws InvalidTypeException if value {@code name} is not of type DECIMAL. | |
| 312 | + */ | |
| 313 | + BigDecimal getDecimal(String name); | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Returns the {@code i}th value as a UUID. | |
| 317 | + * | |
| 318 | + * @param i the index to retrieve. | |
| 319 | + * @return the value of the {@code i}th value as a UUID. | |
| 320 | + * If the value is NULL, {@code null} is returned. | |
| 321 | + * | |
| 322 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 323 | + * @throws InvalidTypeException if value {@code i} is not of type UUID | |
| 324 | + * or TIMEUUID. | |
| 325 | + */ | |
| 326 | + UUID getUUID(int i); | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * Returns the value for {@code name} as a UUID. | |
| 330 | + * | |
| 331 | + * @param name the name to retrieve. | |
| 332 | + * @return the value for {@code name} as a UUID. | |
| 333 | + * If the value is NULL, {@code null} is returned. | |
| 334 | + * | |
| 335 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 336 | + * @throws InvalidTypeException if value {@code name} is not of type | |
| 337 | + * UUID or TIMEUUID. | |
| 338 | + */ | |
| 339 | + UUID getUUID(String name); | |
| 340 | + | |
| 341 | + /** | |
| 342 | + * Returns the {@code i}th value as an InetAddress. | |
| 343 | + * | |
| 344 | + * @param i the index to retrieve. | |
| 345 | + * @return the value of the {@code i}th value as an InetAddress. | |
| 346 | + * If the value is NULL, {@code null} is returned. | |
| 347 | + * | |
| 348 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 349 | + * @throws InvalidTypeException if value {@code i} is not of type INET. | |
| 350 | + */ | |
| 351 | + InetAddress getInet(int i); | |
| 352 | + | |
| 353 | + /** | |
| 354 | + * Returns the value for {@code name} as an InetAddress. | |
| 355 | + * | |
| 356 | + * @param name the name to retrieve. | |
| 357 | + * @return the value for {@code name} as an InetAddress. | |
| 358 | + * If the value is NULL, {@code null} is returned. | |
| 359 | + * | |
| 360 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 361 | + * @throws InvalidTypeException if value {@code name} is not of type | |
| 362 | + * INET. | |
| 363 | + */ | |
| 364 | + InetAddress getInet(String name); | |
| 365 | + | |
| 366 | + /** | |
| 367 | + * Returns the {@code i}th value as a list. | |
| 368 | + * | |
| 369 | + * @param <T> the type of the elements of the list to return. | |
| 370 | + * @param i the index to retrieve. | |
| 371 | + * @param elementsClass the class for the elements of the list to retrieve. | |
| 372 | + * @return the value of the {@code i}th value as a list of | |
| 373 | + * {@code elementsClass} objects. If the value is NULL, an empty list is | |
| 374 | + * returned (note that Cassandra makes no difference between an empty list | |
| 375 | + * and column of type list that is not set). The returned list is immutable. | |
| 376 | + * | |
| 377 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 378 | + * @throws InvalidTypeException if value {@code i} is not a list or if its | |
| 379 | + * elements are not of class {@code elementsClass}. | |
| 380 | + */ | |
| 381 | + <T> List<T> getList(int i, Class<T> elementsClass); | |
| 382 | + | |
| 383 | + /** | |
| 384 | + * Returns the value for {@code name} as a list. | |
| 385 | + * | |
| 386 | + * @param <T> the type of the elements of the list to return. | |
| 387 | + * @param name the name to retrieve. | |
| 388 | + * @param elementsClass the class for the elements of the list to retrieve. | |
| 389 | + * @return the value of the {@code i}th value as a list of | |
| 390 | + * {@code elementsClass} objects. If the value is NULL, an empty list is | |
| 391 | + * returned (note that Cassandra makes no difference between an empty list | |
| 392 | + * and column of type list that is not set). The returned list is immutable. | |
| 393 | + * | |
| 394 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 395 | + * @throws InvalidTypeException if value {@code name} is not a list or if its | |
| 396 | + * elements are not of class {@code elementsClass}. | |
| 397 | + */ | |
| 398 | + <T> List<T> getList(String name, Class<T> elementsClass); | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Returns the {@code i}th value as a set. | |
| 402 | + * | |
| 403 | + * @param <T> the type of the elements of the set to return. | |
| 404 | + * @param i the index to retrieve. | |
| 405 | + * @param elementsClass the class for the elements of the set to retrieve. | |
| 406 | + * @return the value of the {@code i}th value as a set of | |
| 407 | + * {@code elementsClass} objects. If the value is NULL, an empty set is | |
| 408 | + * returned (note that Cassandra makes no difference between an empty set | |
| 409 | + * and column of type set that is not set). The returned set is immutable. | |
| 410 | + * | |
| 411 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 412 | + * @throws InvalidTypeException if value {@code i} is not a set or if its | |
| 413 | + * elements are not of class {@code elementsClass}. | |
| 414 | + */ | |
| 415 | + <T> Set<T> getSet(int i, Class<T> elementsClass); | |
| 416 | + | |
| 417 | + /** | |
| 418 | + * Returns the value for {@code name} as a set. | |
| 419 | + * | |
| 420 | + * @param <T> the type of the elements of the set to return. | |
| 421 | + * @param name the name to retrieve. | |
| 422 | + * @param elementsClass the class for the elements of the set to retrieve. | |
| 423 | + * @return the value of the {@code i}th value as a set of | |
| 424 | + * {@code elementsClass} objects. If the value is NULL, an empty set is | |
| 425 | + * returned (note that Cassandra makes no difference between an empty set | |
| 426 | + * and column of type set that is not set). The returned set is immutable. | |
| 427 | + * | |
| 428 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 429 | + * @throws InvalidTypeException if value {@code name} is not a set or if its | |
| 430 | + * elements are not of class {@code elementsClass}. | |
| 431 | + */ | |
| 432 | + <T> Set<T> getSet(String name, Class<T> elementsClass); | |
| 433 | + | |
| 434 | + /** | |
| 435 | + * Returns the {@code i}th value as a map. | |
| 436 | + * | |
| 437 | + * @param <K> the type of the keys of the map to return. | |
| 438 | + * @param <V> the type of the values of the map to return. | |
| 439 | + * @param i the index to retrieve. | |
| 440 | + * @param keysClass the class for the keys of the map to retrieve. | |
| 441 | + * @param valuesClass the class for the values of the map to retrieve. | |
| 442 | + * @return the value of the {@code i}th value as a map of | |
| 443 | + * {@code keysClass} to {@code valuesClass} objects. If the value is NULL, | |
| 444 | + * an empty map is returned (note that Cassandra makes no difference | |
| 445 | + * between an empty map and column of type map that is not set). The | |
| 446 | + * returned map is immutable. | |
| 447 | + * | |
| 448 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 449 | + * @throws InvalidTypeException if value {@code i} is not a map, if its | |
| 450 | + * keys are not of class {@code keysClass} or if its values are not of | |
| 451 | + * class {@code valuesClass}. | |
| 452 | + */ | |
| 453 | + <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass); | |
| 454 | + | |
| 455 | + /** | |
| 456 | + * Returns the value for {@code name} as a map. | |
| 457 | + * | |
| 458 | + * @param <K> the type of the keys of the map to return. | |
| 459 | + * @param <V> the type of the values of the map to return. | |
| 460 | + * @param name the name to retrieve. | |
| 461 | + * @param keysClass the class for the keys of the map to retrieve. | |
| 462 | + * @param valuesClass the class for the values of the map to retrieve. | |
| 463 | + * @return the value of the {@code i}th value as a map of | |
| 464 | + * {@code keysClass} to {@code valuesClass} objects. If the value is NULL, | |
| 465 | + * an empty map is returned (note that Cassandra makes no difference | |
| 466 | + * between an empty map and column of type map that is not set). The | |
| 467 | + * returned map is immutable. | |
| 468 | + * | |
| 469 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 470 | + * @throws InvalidTypeException if value {@code name} is not a map, if its | |
| 471 | + * keys are not of class {@code keysClass} or if its values are not of | |
| 472 | + * class {@code valuesClass}. | |
| 473 | + */ | |
| 474 | + <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass); | |
| 475 | + | |
| 476 | + | |
| 477 | + /** | |
| 478 | + * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 479 | + * | |
| 480 | + * @param i the index to retrieve. | |
| 481 | + * @return the value of the {@code i}th value as the Java type matching its CQL type. | |
| 482 | + * If the value is NULL and is a simple type, NULL is returned. | |
| 483 | + * If it is NULL and is a collection type, an empty (immutable) collection is returned. | |
| 484 | + * | |
| 485 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 486 | + */ | |
| 487 | + Object getObject(int i); | |
| 488 | + | |
| 489 | + /** | |
| 490 | + * Returns the value for {@code name} as the Java type matching its CQL type. | |
| 491 | + * | |
| 492 | + * @param name the name to retrieve. | |
| 493 | + * @return the value of the {@code i}th value as the Java type matching its CQL type. | |
| 494 | + * If the value is NULL and is a simple type, NULL is returned. | |
| 495 | + * If it is NULL and is a collection type, an empty (immutable) collection is returned. | |
| 496 | + * | |
| 497 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 498 | + */ | |
| 499 | + Object getObject(String name); | |
| 500 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/Row.java +35 −1
| @@ -30,7 +30,7 @@ | ||
| 30 | 30 | * or name. When setting them by name, names follow the case insensitivity |
| 31 | 31 | * rules explained in {@link ColumnDefinitions}. |
| 32 | 32 | */ |
| 33 | -public interface Row { | |
| 33 | +public interface Row extends GettableData { | |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * Returns the columns contained in this Row. |
| @@ -47,6 +47,7 @@ | ||
| 47 | 47 | * |
| 48 | 48 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 49 | 49 | */ |
| 50 | + @Override | |
| 50 | 51 | public boolean isNull(int i); |
| 51 | 52 | |
| 52 | 53 | /** |
| @@ -58,6 +59,7 @@ | ||
| 58 | 59 | * @throws IllegalArgumentException if {@code name} is not part of the |
| 59 | 60 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 60 | 61 | */ |
| 62 | + @Override | |
| 61 | 63 | public boolean isNull(String name); |
| 62 | 64 | |
| 63 | 65 | /** |
| @@ -70,6 +72,7 @@ | ||
| 70 | 72 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 71 | 73 | * @throws InvalidTypeException if column {@code i} is not of type BOOLEAN. |
| 72 | 74 | */ |
| 75 | + @Override | |
| 73 | 76 | public boolean getBool(int i); |
| 74 | 77 | |
| 75 | 78 | /** |
| @@ -83,6 +86,7 @@ | ||
| 83 | 86 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 84 | 87 | * @throws InvalidTypeException if column {@code name} is not of type BOOLEAN. |
| 85 | 88 | */ |
| 89 | + @Override | |
| 86 | 90 | public boolean getBool(String name); |
| 87 | 91 | |
| 88 | 92 | /** |
| @@ -95,6 +99,7 @@ | ||
| 95 | 99 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 96 | 100 | * @throws InvalidTypeException if column {@code i} is not of type INT. |
| 97 | 101 | */ |
| 102 | + @Override | |
| 98 | 103 | public int getInt(int i); |
| 99 | 104 | |
| 100 | 105 | /** |
| @@ -108,6 +113,7 @@ | ||
| 108 | 113 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 109 | 114 | * @throws InvalidTypeException if column {@code name} is not of type INT. |
| 110 | 115 | */ |
| 116 | + @Override | |
| 111 | 117 | public int getInt(String name); |
| 112 | 118 | |
| 113 | 119 | /** |
| @@ -120,6 +126,7 @@ | ||
| 120 | 126 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 121 | 127 | * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER. |
| 122 | 128 | */ |
| 129 | + @Override | |
| 123 | 130 | public long getLong(int i); |
| 124 | 131 | |
| 125 | 132 | /** |
| @@ -133,6 +140,7 @@ | ||
| 133 | 140 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 134 | 141 | * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER. |
| 135 | 142 | */ |
| 143 | + @Override | |
| 136 | 144 | public long getLong(String name); |
| 137 | 145 | |
| 138 | 146 | /** |
| @@ -145,6 +153,7 @@ | ||
| 145 | 153 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 146 | 154 | * @throws InvalidTypeException if column {@code i} is not of type TIMESTAMP. |
| 147 | 155 | */ |
| 156 | + @Override | |
| 148 | 157 | public Date getDate(int i); |
| 149 | 158 | |
| 150 | 159 | /** |
| @@ -158,6 +167,7 @@ | ||
| 158 | 167 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 159 | 168 | * @throws InvalidTypeException if column {@code name} is not of type TIMESTAMP. |
| 160 | 169 | */ |
| 170 | + @Override | |
| 161 | 171 | public Date getDate(String name); |
| 162 | 172 | |
| 163 | 173 | /** |
| @@ -170,6 +180,7 @@ | ||
| 170 | 180 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 171 | 181 | * @throws InvalidTypeException if column {@code i} is not of type FLOAT. |
| 172 | 182 | */ |
| 183 | + @Override | |
| 173 | 184 | public float getFloat(int i); |
| 174 | 185 | |
| 175 | 186 | /** |
| @@ -183,6 +194,7 @@ | ||
| 183 | 194 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 184 | 195 | * @throws InvalidTypeException if column {@code name} is not of type FLOAT. |
| 185 | 196 | */ |
| 197 | + @Override | |
| 186 | 198 | public float getFloat(String name); |
| 187 | 199 | |
| 188 | 200 | /** |
| @@ -195,6 +207,7 @@ | ||
| 195 | 207 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 196 | 208 | * @throws InvalidTypeException if column {@code i} is not of type DOUBLE. |
| 197 | 209 | */ |
| 210 | + @Override | |
| 198 | 211 | public double getDouble(int i); |
| 199 | 212 | |
| 200 | 213 | /** |
| @@ -208,6 +221,7 @@ | ||
| 208 | 221 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 209 | 222 | * @throws InvalidTypeException if column {@code name} is not of type DOUBLE. |
| 210 | 223 | */ |
| 224 | + @Override | |
| 211 | 225 | public double getDouble(String name); |
| 212 | 226 | |
| 213 | 227 | /** |
| @@ -224,6 +238,7 @@ | ||
| 224 | 238 | * |
| 225 | 239 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 226 | 240 | */ |
| 241 | + @Override | |
| 227 | 242 | public ByteBuffer getBytesUnsafe(int i); |
| 228 | 243 | |
| 229 | 244 | /** |
| @@ -241,6 +256,7 @@ | ||
| 241 | 256 | * @throws IllegalArgumentException if {@code name} is not part of the |
| 242 | 257 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 243 | 258 | */ |
| 259 | + @Override | |
| 244 | 260 | public ByteBuffer getBytesUnsafe(String name); |
| 245 | 261 | |
| 246 | 262 | /** |
| @@ -256,6 +272,7 @@ | ||
| 256 | 272 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 257 | 273 | * @throws InvalidTypeException if column {@code i} type is not of type BLOB. |
| 258 | 274 | */ |
| 275 | + @Override | |
| 259 | 276 | public ByteBuffer getBytes(int i); |
| 260 | 277 | |
| 261 | 278 | /** |
| @@ -272,6 +289,7 @@ | ||
| 272 | 289 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 273 | 290 | * @throws InvalidTypeException if column {@code i} type is not of type BLOB. |
| 274 | 291 | */ |
| 292 | + @Override | |
| 275 | 293 | public ByteBuffer getBytes(String name); |
| 276 | 294 | |
| 277 | 295 | /** |
| @@ -285,6 +303,7 @@ | ||
| 285 | 303 | * @throws InvalidTypeException if column {@code i} type is none of: |
| 286 | 304 | * VARCHAR, TEXT or ASCII. |
| 287 | 305 | */ |
| 306 | + @Override | |
| 288 | 307 | public String getString(int i); |
| 289 | 308 | |
| 290 | 309 | /** |
| @@ -299,6 +318,7 @@ | ||
| 299 | 318 | * @throws InvalidTypeException if column {@code name} type is none of: |
| 300 | 319 | * VARCHAR, TEXT or ASCII. |
| 301 | 320 | */ |
| 321 | + @Override | |
| 302 | 322 | public String getString(String name); |
| 303 | 323 | |
| 304 | 324 | /** |
| @@ -311,6 +331,7 @@ | ||
| 311 | 331 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 312 | 332 | * @throws InvalidTypeException if column {@code i} is not of type VARINT. |
| 313 | 333 | */ |
| 334 | + @Override | |
| 314 | 335 | public BigInteger getVarint(int i); |
| 315 | 336 | |
| 316 | 337 | /** |
| @@ -324,6 +345,7 @@ | ||
| 324 | 345 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 325 | 346 | * @throws InvalidTypeException if column {@code name} is not of type VARINT. |
| 326 | 347 | */ |
| 348 | + @Override | |
| 327 | 349 | public BigInteger getVarint(String name); |
| 328 | 350 | |
| 329 | 351 | /** |
| @@ -336,6 +358,7 @@ | ||
| 336 | 358 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 337 | 359 | * @throws InvalidTypeException if column {@code i} is not of type DECIMAL. |
| 338 | 360 | */ |
| 361 | + @Override | |
| 339 | 362 | public BigDecimal getDecimal(int i); |
| 340 | 363 | |
| 341 | 364 | /** |
| @@ -349,6 +372,7 @@ | ||
| 349 | 372 | * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. |
| 350 | 373 | * @throws InvalidTypeException if column {@code name} is not of type DECIMAL. |
| 351 | 374 | */ |
| 375 | + @Override | |
| 352 | 376 | public BigDecimal getDecimal(String name); |
| 353 | 377 | |
| 354 | 378 | /** |
| @@ -362,6 +386,7 @@ | ||
| 362 | 386 | * @throws InvalidTypeException if column {@code i} is not of type UUID |
| 363 | 387 | * or TIMEUUID. |
| 364 | 388 | */ |
| 389 | + @Override | |
| 365 | 390 | public UUID getUUID(int i); |
| 366 | 391 | |
| 367 | 392 | /** |
| @@ -376,6 +401,7 @@ | ||
| 376 | 401 | * @throws InvalidTypeException if column {@code name} is not of type |
| 377 | 402 | * UUID or TIMEUUID. |
| 378 | 403 | */ |
| 404 | + @Override | |
| 379 | 405 | public UUID getUUID(String name); |
| 380 | 406 | |
| 381 | 407 | /** |
| @@ -388,6 +414,7 @@ | ||
| 388 | 414 | * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. |
| 389 | 415 | * @throws InvalidTypeException if column {@code i} is not of type INET. |
| 390 | 416 | */ |
| 417 | + @Override | |
| 391 | 418 | public InetAddress getInet(int i); |
| 392 | 419 | |
| 393 | 420 | /** |
| @@ -402,6 +429,7 @@ | ||
| 402 | 429 | * @throws InvalidTypeException if column {@code name} is not of type |
| 403 | 430 | * INET. |
| 404 | 431 | */ |
| 432 | + @Override | |
| 405 | 433 | public InetAddress getInet(String name); |
| 406 | 434 | |
| 407 | 435 | /** |
| @@ -473,6 +501,7 @@ | ||
| 473 | 501 | * @throws InvalidTypeException if column {@code i} is not a list or if its |
| 474 | 502 | * elements are not of class {@code elementsClass}. |
| 475 | 503 | */ |
| 504 | + @Override | |
| 476 | 505 | public <T> List<T> getList(int i, Class<T> elementsClass); |
| 477 | 506 | |
| 478 | 507 | /** |
| @@ -491,6 +520,7 @@ | ||
| 491 | 520 | * @throws InvalidTypeException if column {@code name} is not a list or if its |
| 492 | 521 | * elements are not of class {@code elementsClass}. |
| 493 | 522 | */ |
| 523 | + @Override | |
| 494 | 524 | public <T> List<T> getList(String name, Class<T> elementsClass); |
| 495 | 525 | |
| 496 | 526 | /** |
| @@ -508,6 +538,7 @@ | ||
| 508 | 538 | * @throws InvalidTypeException if column {@code i} is not a set or if its |
| 509 | 539 | * elements are not of class {@code elementsClass}. |
| 510 | 540 | */ |
| 541 | + @Override | |
| 511 | 542 | public <T> Set<T> getSet(int i, Class<T> elementsClass); |
| 512 | 543 | |
| 513 | 544 | /** |
| @@ -526,6 +557,7 @@ | ||
| 526 | 557 | * @throws InvalidTypeException if column {@code name} is not a set or if its |
| 527 | 558 | * elements are not of class {@code elementsClass}. |
| 528 | 559 | */ |
| 560 | + @Override | |
| 529 | 561 | public <T> Set<T> getSet(String name, Class<T> elementsClass); |
| 530 | 562 | |
| 531 | 563 | /** |
| @@ -547,6 +579,7 @@ | ||
| 547 | 579 | * keys are not of class {@code keysClass} or if its values are not of |
| 548 | 580 | * class {@code valuesClass}. |
| 549 | 581 | */ |
| 582 | + @Override | |
| 550 | 583 | public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass); |
| 551 | 584 | |
| 552 | 585 | /** |
| @@ -569,5 +602,6 @@ | ||
| 569 | 602 | * keys are not of class {@code keysClass} or if its values are not of |
| 570 | 603 | * class {@code valuesClass}. |
| 571 | 604 | */ |
| 605 | + @Override | |
| 572 | 606 | public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass); |
| 573 | 607 | } |
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +137 −0
| @@ -79,6 +79,8 @@ | ||
| 79 | 79 | private TypeCodec() {} |
| 80 | 80 | |
| 81 | 81 | public abstract T parse(String value); |
| 82 | + public abstract String format(T value); | |
| 83 | + | |
| 82 | 84 | public abstract ByteBuffer serialize(T value); |
| 83 | 85 | public abstract T deserialize(ByteBuffer bytes); |
| 84 | 86 | |
| @@ -229,6 +231,43 @@ | ||
| 229 | 231 | return value; |
| 230 | 232 | } |
| 231 | 233 | |
| 234 | + @Override | |
| 235 | + public String format(String value) { | |
| 236 | + return '\'' + replace(value, '\'', "''") + '\''; | |
| 237 | + } | |
| 238 | + | |
| 239 | + // Simple method to replace a single character. String.replace is a bit too | |
| 240 | + // inefficient (see JAVA-67) | |
| 241 | + static String replace(String text, char search, String replacement) { | |
| 242 | + if (text == null || text.isEmpty()) | |
| 243 | + return text; | |
| 244 | + | |
| 245 | + int nbMatch = 0; | |
| 246 | + int start = -1; | |
| 247 | + do { | |
| 248 | + start = text.indexOf(search, start+1); | |
| 249 | + if (start != -1) | |
| 250 | + ++nbMatch; | |
| 251 | + } while (start != -1); | |
| 252 | + | |
| 253 | + if (nbMatch == 0) | |
| 254 | + return text; | |
| 255 | + | |
| 256 | + int newLength = text.length() + nbMatch * (replacement.length() - 1); | |
| 257 | + char[] result = new char[newLength]; | |
| 258 | + int newIdx = 0; | |
| 259 | + for (int i = 0; i < text.length(); i++) { | |
| 260 | + char c = text.charAt(i); | |
| 261 | + if (c == search) { | |
| 262 | + for (int r = 0; r < replacement.length(); r++) | |
| 263 | + result[newIdx++] = replacement.charAt(r); | |
| 264 | + } else { | |
| 265 | + result[newIdx++] = c; | |
| 266 | + } | |
| 267 | + } | |
| 268 | + return new String(result); | |
| 269 | + } | |
| 270 | + | |
| 232 | 271 | @Override |
| 233 | 272 | public ByteBuffer serialize(String value) { |
| 234 | 273 | return ByteBuffer.wrap(value.getBytes(charset)); |
| @@ -255,6 +294,11 @@ | ||
| 255 | 294 | } |
| 256 | 295 | } |
| 257 | 296 | |
| 297 | + @Override | |
| 298 | + public String format(Long value) { | |
| 299 | + return Long.toString(value); | |
| 300 | + } | |
| 301 | + | |
| 258 | 302 | @Override |
| 259 | 303 | public ByteBuffer serialize(Long value) { |
| 260 | 304 | return serializeNoBoxing(value); |
| @@ -290,6 +334,11 @@ | ||
| 290 | 334 | return Bytes.fromHexString(value); |
| 291 | 335 | } |
| 292 | 336 | |
| 337 | + @Override | |
| 338 | + public String format(ByteBuffer value) { | |
| 339 | + return Bytes.toHexString(value); | |
| 340 | + } | |
| 341 | + | |
| 293 | 342 | @Override |
| 294 | 343 | public ByteBuffer serialize(ByteBuffer value) { |
| 295 | 344 | return value.duplicate(); |
| @@ -320,6 +369,11 @@ | ||
| 320 | 369 | throw new InvalidTypeException(String.format("Cannot parse boolean value from \"%s\"", value)); |
| 321 | 370 | } |
| 322 | 371 | |
| 372 | + @Override | |
| 373 | + public String format(Boolean value) { | |
| 374 | + return value ? "true" : "false"; | |
| 375 | + } | |
| 376 | + | |
| 323 | 377 | @Override |
| 324 | 378 | public ByteBuffer serialize(Boolean value) { |
| 325 | 379 | return serializeNoBoxing(value); |
| @@ -357,6 +411,11 @@ | ||
| 357 | 411 | } |
| 358 | 412 | } |
| 359 | 413 | |
| 414 | + @Override | |
| 415 | + public String format(BigDecimal value) { | |
| 416 | + return value.toString(); | |
| 417 | + } | |
| 418 | + | |
| 360 | 419 | @Override |
| 361 | 420 | public ByteBuffer serialize(BigDecimal value) { |
| 362 | 421 | BigInteger bi = value.unscaledValue(); |
| @@ -400,6 +459,11 @@ | ||
| 400 | 459 | } |
| 401 | 460 | } |
| 402 | 461 | |
| 462 | + @Override | |
| 463 | + public String format(Double value) { | |
| 464 | + return Double.toString(value); | |
| 465 | + } | |
| 466 | + | |
| 403 | 467 | @Override |
| 404 | 468 | public ByteBuffer serialize(Double value) { |
| 405 | 469 | return serializeNoBoxing(value); |
| @@ -439,6 +503,11 @@ | ||
| 439 | 503 | } |
| 440 | 504 | } |
| 441 | 505 | |
| 506 | + @Override | |
| 507 | + public String format(Float value) { | |
| 508 | + return Float.toString(value); | |
| 509 | + } | |
| 510 | + | |
| 442 | 511 | @Override |
| 443 | 512 | public ByteBuffer serialize(Float value) { |
| 444 | 513 | return serializeNoBoxing(value); |
| @@ -478,6 +547,11 @@ | ||
| 478 | 547 | } |
| 479 | 548 | } |
| 480 | 549 | |
| 550 | + @Override | |
| 551 | + public String format(InetAddress value) { | |
| 552 | + return "'" + value.getHostAddress() + "'"; | |
| 553 | + } | |
| 554 | + | |
| 481 | 555 | @Override |
| 482 | 556 | public ByteBuffer serialize(InetAddress value) { |
| 483 | 557 | return ByteBuffer.wrap(value.getAddress()); |
| @@ -508,6 +582,11 @@ | ||
| 508 | 582 | } |
| 509 | 583 | } |
| 510 | 584 | |
| 585 | + @Override | |
| 586 | + public String format(Integer value) { | |
| 587 | + return Integer.toString(value); | |
| 588 | + } | |
| 589 | + | |
| 511 | 590 | @Override |
| 512 | 591 | public ByteBuffer serialize(Integer value) { |
| 513 | 592 | return serializeNoBoxing(value); |
| @@ -598,6 +677,11 @@ | ||
| 598 | 677 | } |
| 599 | 678 | } |
| 600 | 679 | |
| 680 | + @Override | |
| 681 | + public String format(Date value) { | |
| 682 | + return Long.toString(value.getTime()); | |
| 683 | + } | |
| 684 | + | |
| 601 | 685 | @Override |
| 602 | 686 | public ByteBuffer serialize(Date value) { |
| 603 | 687 | return LongCodec.instance.serializeNoBoxing(value.getTime()); |
| @@ -624,6 +708,11 @@ | ||
| 624 | 708 | } |
| 625 | 709 | } |
| 626 | 710 | |
| 711 | + @Override | |
| 712 | + public String format(UUID value) { | |
| 713 | + return value.toString(); | |
| 714 | + } | |
| 715 | + | |
| 627 | 716 | @Override |
| 628 | 717 | public ByteBuffer serialize(UUID value) { |
| 629 | 718 | ByteBuffer bb = ByteBuffer.allocate(16); |
| @@ -676,6 +765,11 @@ | ||
| 676 | 765 | } |
| 677 | 766 | } |
| 678 | 767 | |
| 768 | + @Override | |
| 769 | + public String format(BigInteger value) { | |
| 770 | + return value.toString(); | |
| 771 | + } | |
| 772 | + | |
| 679 | 773 | @Override |
| 680 | 774 | public ByteBuffer serialize(BigInteger value) { |
| 681 | 775 | return ByteBuffer.wrap(value.toByteArray()); |
| @@ -700,6 +794,19 @@ | ||
| 700 | 794 | throw new UnsupportedOperationException(); |
| 701 | 795 | } |
| 702 | 796 | |
| 797 | + @Override | |
| 798 | + public String format(List<T> value) { | |
| 799 | + StringBuilder sb = new StringBuilder(); | |
| 800 | + sb.append("["); | |
| 801 | + for (int i = 0; i < value.size(); i++) { | |
| 802 | + if (i != 0) | |
| 803 | + sb.append(", "); | |
| 804 | + sb.append(eltCodec.format(value.get(i))); | |
| 805 | + } | |
| 806 | + sb.append("]"); | |
| 807 | + return sb.toString(); | |
| 808 | + } | |
| 809 | + | |
| 703 | 810 | @Override |
| 704 | 811 | public ByteBuffer serialize(List<T> value) { |
| 705 | 812 | List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(value.size()); |
| @@ -745,6 +852,20 @@ | ||
| 745 | 852 | throw new UnsupportedOperationException(); |
| 746 | 853 | } |
| 747 | 854 | |
| 855 | + @Override | |
| 856 | + public String format(Set<T> value) { | |
| 857 | + StringBuilder sb = new StringBuilder(); | |
| 858 | + sb.append("{"); | |
| 859 | + int i = 0; | |
| 860 | + for (T v : value) { | |
| 861 | + if (i++ != 0) | |
| 862 | + sb.append(", "); | |
| 863 | + sb.append(eltCodec.format(v)); | |
| 864 | + } | |
| 865 | + sb.append("}"); | |
| 866 | + return sb.toString(); | |
| 867 | + } | |
| 868 | + | |
| 748 | 869 | @Override |
| 749 | 870 | public ByteBuffer serialize(Set<T> value) { |
| 750 | 871 | List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(value.size()); |
| @@ -792,6 +913,22 @@ | ||
| 792 | 913 | throw new UnsupportedOperationException(); |
| 793 | 914 | } |
| 794 | 915 | |
| 916 | + @Override | |
| 917 | + public String format(Map<K, V> value) { | |
| 918 | + StringBuilder sb = new StringBuilder(); | |
| 919 | + sb.append("{"); | |
| 920 | + int i = 0; | |
| 921 | + for (Map.Entry<K, V> e : value.entrySet()) { | |
| 922 | + if (i++ != 0) | |
| 923 | + sb.append(", "); | |
| 924 | + sb.append(keyCodec.format(e.getKey())); | |
| 925 | + sb.append(":"); | |
| 926 | + sb.append(valueCodec.format(e.getValue())); | |
| 927 | + } | |
| 928 | + sb.append("}"); | |
| 929 | + return sb.toString(); | |
| 930 | + } | |
| 931 | + | |
| 795 | 932 | @Override |
| 796 | 933 | public ByteBuffer serialize(Map<K, V> value) { |
| 797 | 934 | List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(2 * value.size()); |
modified driver-core/src/main/java/com/datastax/driver/core/VersionNumber.java +13 −0
| @@ -159,6 +159,19 @@ | ||
| 159 | 159 | return build; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | + /** | |
| 163 | + * The next stable version, i.e. the version stripped of its pre-release labels and build metadata. | |
| 164 | + * <p> | |
| 165 | + * This is mostly used during our development stage, where we test the driver against pre-release | |
| 166 | + * versions of Cassandra like 2.1.0-rc7-SNAPSHOT, but need to compare to the stable version 2.1.0 | |
| 167 | + * when testing for native protocol compatibility, etc. | |
| 168 | + * | |
| 169 | + * @return the next stable version. | |
| 170 | + */ | |
| 171 | + public VersionNumber nextStable() { | |
| 172 | + return new VersionNumber(major, minor, patch, dsePatch, null, null); | |
| 173 | + } | |
| 174 | + | |
| 162 | 175 | public int compareTo(VersionNumber other) { |
| 163 | 176 | if (major < other.major) |
| 164 | 177 | return -1; |
added driver-core/src/test/java/com/datastax/driver/core/BoundStatementTest.java +56 −0
| @@ -0,0 +1,56 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.util.Collection; | |
| 4 | +import java.util.Collections; | |
| 5 | + | |
| 6 | +import com.google.common.collect.Lists; | |
| 7 | +import org.testng.annotations.BeforeClass; | |
| 8 | +import org.testng.annotations.Test; | |
| 9 | + | |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 11 | +import static org.assertj.core.api.Assertions.fail; | |
| 12 | + | |
| 13 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 14 | + | |
| 15 | +public class BoundStatementTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 16 | + | |
| 17 | + PreparedStatement prepared; | |
| 18 | + | |
| 19 | + @Override | |
| 20 | + protected Collection<String> getTableDefinitions() { | |
| 21 | + return Lists.newArrayList("CREATE TABLE foo (k int primary key, v1 text, v2 list<int>)"); | |
| 22 | + } | |
| 23 | + | |
| 24 | + @BeforeClass(groups = "short") | |
| 25 | + public void setup() { | |
| 26 | + prepared = session.prepare("INSERT INTO foo (k, v1, v2) VALUES (?, ?, ?)"); | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Test(groups = "short") | |
| 30 | + public void should_get_single_value() { | |
| 31 | + // This test is not exhaustive, note that the method is also covered in DataTypeIntegrationTest. | |
| 32 | + BoundStatement statement = prepared.bind(1, "test", Lists.newArrayList(1)); | |
| 33 | + | |
| 34 | + assertThat(statement.getInt(0)) | |
| 35 | + .isEqualTo(statement.getInt("k")) | |
| 36 | + .isEqualTo(1); | |
| 37 | + | |
| 38 | + assertThat(statement.getString(1)) | |
| 39 | + .isEqualTo(statement.getString("v1")) | |
| 40 | + .isEqualTo("test"); | |
| 41 | + | |
| 42 | + assertThat(statement.getList(2, Integer.class)) | |
| 43 | + .isEqualTo(statement.getList("v2", Integer.class)) | |
| 44 | + .isEqualTo(Lists.newArrayList(1)); | |
| 45 | + | |
| 46 | + try { | |
| 47 | + statement.getString(0); | |
| 48 | + fail("Expected type error"); | |
| 49 | + } catch (InvalidTypeException e) { /* expected */ } | |
| 50 | + | |
| 51 | + try { | |
| 52 | + statement.getString(3); | |
| 53 | + fail("Expected index error"); | |
| 54 | + } catch (IndexOutOfBoundsException e) { /* expected */ } | |
| 55 | + } | |
| 56 | +} | |
| No newline at the end of the file |
added driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +240 −0
| @@ -0,0 +1,240 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 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 java.util.*; | |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 20 | + | |
| 21 | +import com.google.common.collect.*; | |
| 22 | +import org.slf4j.Logger; | |
| 23 | +import org.slf4j.LoggerFactory; | |
| 24 | +import org.testng.annotations.Test; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.assertj.core.api.Assertions.fail; | |
| 28 | + | |
| 29 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * The goal of this test is to cover the serialization and deserialization of datatypes. | |
| 33 | + * | |
| 34 | + * It creates a table with a column of a given type, inserts a value and then tries to retrieve it. | |
| 35 | + * There are 3 variants for the insert query: a raw string, a simple statement with a parameter | |
| 36 | + * (protocol > v2 only) and a prepared statement. | |
| 37 | + * This is repeated with a large number of datatypes. | |
| 38 | + */ | |
| 39 | +public class DataTypeIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 40 | + private static final Logger logger = LoggerFactory.getLogger(DataTypeIntegrationTest.class); | |
| 41 | + | |
| 42 | + List<TestTable> tables = allTables(); | |
| 43 | + VersionNumber cassandraVersion; | |
| 44 | + | |
| 45 | + enum StatementType {RAW_STRING, SIMPLE_WITH_PARAM, PREPARED} | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + protected Collection<String> getTableDefinitions() { | |
| 49 | + Host host = cluster.getMetadata().getAllHosts().iterator().next(); | |
| 50 | + cassandraVersion = host.getCassandraVersion().nextStable(); | |
| 51 | + | |
| 52 | + List<String> statements = Lists.newArrayList(); | |
| 53 | + for (TestTable table : tables) { | |
| 54 | + if (cassandraVersion.compareTo(table.minCassandraVersion) < 0) | |
| 55 | + logger.debug("Skipping table because it uses a feature not supported by Cassandra {}: {}", | |
| 56 | + cassandraVersion, table.createStatement); | |
| 57 | + else | |
| 58 | + statements.add(table.createStatement); | |
| 59 | + } | |
| 60 | + | |
| 61 | + return statements; | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Test(groups = "long") | |
| 65 | + public void should_insert_and_retrieve_data_with_legacy_statements() { | |
| 66 | + should_insert_and_retrieve_data(StatementType.RAW_STRING); | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Test(groups = "long") | |
| 70 | + public void should_insert_and_retrieve_data_with_prepared_statements() { | |
| 71 | + should_insert_and_retrieve_data(StatementType.PREPARED); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Test(groups = "long") | |
| 75 | + @CassandraVersion(major = 2.0, description = "Uses parameterized simple statements, which are only available with protocol v2") | |
| 76 | + public void should_insert_and_retrieve_data_with_parameterized_simple_statements() { | |
| 77 | + should_insert_and_retrieve_data(StatementType.SIMPLE_WITH_PARAM); | |
| 78 | + } | |
| 79 | + | |
| 80 | + protected void should_insert_and_retrieve_data(StatementType statementType) { | |
| 81 | + for (TestTable table : tables) { | |
| 82 | + if (cassandraVersion.compareTo(table.minCassandraVersion) < 0) | |
| 83 | + continue; | |
| 84 | + | |
| 85 | + switch (statementType) { | |
| 86 | + case RAW_STRING: | |
| 87 | + session.execute(table.insertStatement.replace("?", table.testColumnType.format(table.sampleValue))); | |
| 88 | + break; | |
| 89 | + case SIMPLE_WITH_PARAM: | |
| 90 | + session.execute(table.insertStatement, table.sampleValue); | |
| 91 | + break; | |
| 92 | + case PREPARED: | |
| 93 | + PreparedStatement ps = session.prepare(table.insertStatement); | |
| 94 | + BoundStatement bs = ps.bind(table.sampleValue); | |
| 95 | + checkGetterReturnsBoundValue(bs, table); | |
| 96 | + session.execute(bs); | |
| 97 | + break; | |
| 98 | + } | |
| 99 | + | |
| 100 | + Row row = session.execute(table.selectStatement).one(); | |
| 101 | + Object queriedValue = table.testColumnType.deserialize(row.getBytesUnsafe("v")); | |
| 102 | + | |
| 103 | + assertThat(queriedValue) | |
| 104 | + .as("Test failure on %s statement with table:%n%s;%n" + | |
| 105 | + "insert statement:%n%s;%n", | |
| 106 | + statementType, | |
| 107 | + table.createStatement, | |
| 108 | + table.insertStatement) | |
| 109 | + .isEqualTo(table.sampleValue); | |
| 110 | + | |
| 111 | + session.execute(table.truncateStatement); | |
| 112 | + } | |
| 113 | + } | |
| 114 | + | |
| 115 | + private void checkGetterReturnsBoundValue(BoundStatement bs, TestTable table) { | |
| 116 | + Object getterResult = getBoundValue(bs, table.testColumnType); | |
| 117 | + assertThat(getterResult).isEqualTo(table.sampleValue); | |
| 118 | + | |
| 119 | + // Ensure that bs.getObject() also returns the expected value. | |
| 120 | + assertThat(bs.getObject(0)).isEqualTo(table.sampleValue); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Abstracts information about a table (corresponding to a given column type). | |
| 125 | + */ | |
| 126 | + static class TestTable { | |
| 127 | + private static final AtomicInteger counter = new AtomicInteger(); | |
| 128 | + private String tableName = "date_type_test" + counter.incrementAndGet(); | |
| 129 | + | |
| 130 | + final DataType testColumnType; | |
| 131 | + final Object sampleValue; | |
| 132 | + | |
| 133 | + final String createStatement; | |
| 134 | + final String insertStatement = String.format("INSERT INTO %s (k, v) VALUES (1, ?)", tableName); | |
| 135 | + final String selectStatement = String.format("SELECT v FROM %s WHERE k = 1", tableName); | |
| 136 | + final String truncateStatement = String.format("TRUNCATE %s", tableName); | |
| 137 | + | |
| 138 | + final VersionNumber minCassandraVersion; | |
| 139 | + | |
| 140 | + TestTable(DataType testColumnType, Object sampleValue, String minCassandraVersion) { | |
| 141 | + this.testColumnType = testColumnType; | |
| 142 | + this.sampleValue = sampleValue; | |
| 143 | + this.minCassandraVersion = VersionNumber.parse(minCassandraVersion); | |
| 144 | + | |
| 145 | + this.createStatement = String.format("CREATE TABLE %s (k int PRIMARY KEY, v %s)", tableName, testColumnType); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + private static List<TestTable> allTables() { | |
| 150 | + List<TestTable> tables = Lists.newArrayList(); | |
| 151 | + | |
| 152 | + tables.addAll(tablesWithPrimitives()); | |
| 153 | + tables.addAll(tablesWithCollectionsOfPrimitives()); | |
| 154 | + tables.addAll(tablesWithMapsOfPrimitives()); | |
| 155 | + | |
| 156 | + return ImmutableList.copyOf(tables); | |
| 157 | + } | |
| 158 | + | |
| 159 | + private static List<TestTable> tablesWithPrimitives() { | |
| 160 | + List<TestTable> tables = Lists.newArrayList(); | |
| 161 | + for (Map.Entry<DataType, Object> entry : PrimitiveTypeSamples.ALL.entrySet()) | |
| 162 | + tables.add(new TestTable(entry.getKey(), entry.getValue(), "1.2.0")); | |
| 163 | + return tables; | |
| 164 | + } | |
| 165 | + | |
| 166 | + private static List<TestTable> tablesWithCollectionsOfPrimitives() { | |
| 167 | + List<TestTable> tables = Lists.newArrayList(); | |
| 168 | + for (Map.Entry<DataType, Object> entry : PrimitiveTypeSamples.ALL.entrySet()) { | |
| 169 | + | |
| 170 | + DataType elementType = entry.getKey(); | |
| 171 | + Object elementSample = entry.getValue(); | |
| 172 | + | |
| 173 | + tables.add(new TestTable(DataType.list(elementType), Lists.newArrayList(elementSample, elementSample), "1.2.0")); | |
| 174 | + tables.add(new TestTable(DataType.set(elementType), Sets.newHashSet(elementSample), "1.2.0")); | |
| 175 | + } | |
| 176 | + return tables; | |
| 177 | + } | |
| 178 | + | |
| 179 | + private static List<TestTable> tablesWithMapsOfPrimitives() { | |
| 180 | + List<TestTable> tables = Lists.newArrayList(); | |
| 181 | + for (Map.Entry<DataType, Object> keyEntry : PrimitiveTypeSamples.ALL.entrySet()) { | |
| 182 | + DataType keyType = keyEntry.getKey(); | |
| 183 | + Object keySample = keyEntry.getValue(); | |
| 184 | + for (Map.Entry<DataType, Object> valueEntry : PrimitiveTypeSamples.ALL.entrySet()) { | |
| 185 | + DataType valueType = valueEntry.getKey(); | |
| 186 | + Object valueSample = valueEntry.getValue(); | |
| 187 | + | |
| 188 | + tables.add(new TestTable(DataType.map(keyType, valueType), | |
| 189 | + ImmutableMap.builder().put(keySample, valueSample).build(), | |
| 190 | + "1.2.0")); | |
| 191 | + } | |
| 192 | + } | |
| 193 | + return tables; | |
| 194 | + } | |
| 195 | + | |
| 196 | + private Object getBoundValue(BoundStatement bs, DataType dataType) { | |
| 197 | + // This is kind of lame, but better than testing all getters manually | |
| 198 | + switch (dataType.getName()) { | |
| 199 | + case ASCII: | |
| 200 | + return bs.getString(0); | |
| 201 | + case BIGINT: | |
| 202 | + return bs.getLong(0); | |
| 203 | + case BLOB: | |
| 204 | + return bs.getBytes(0); | |
| 205 | + case BOOLEAN: | |
| 206 | + return bs.getBool(0); | |
| 207 | + case DECIMAL: | |
| 208 | + return bs.getDecimal(0); | |
| 209 | + case DOUBLE: | |
| 210 | + return bs.getDouble(0); | |
| 211 | + case FLOAT: | |
| 212 | + return bs.getFloat(0); | |
| 213 | + case INET: | |
| 214 | + return bs.getInet(0); | |
| 215 | + case INT: | |
| 216 | + return bs.getInt(0); | |
| 217 | + case TEXT: | |
| 218 | + case VARCHAR: | |
| 219 | + return bs.getString(0); | |
| 220 | + case TIMESTAMP: | |
| 221 | + return bs.getDate(0); | |
| 222 | + case UUID: | |
| 223 | + case TIMEUUID: | |
| 224 | + return bs.getUUID(0); | |
| 225 | + case VARINT: | |
| 226 | + return bs.getVarint(0); | |
| 227 | + case LIST: | |
| 228 | + return bs.getList(0, dataType.getTypeArguments().get(0).asJavaClass()); | |
| 229 | + case SET: | |
| 230 | + return bs.getSet(0, dataType.getTypeArguments().get(0).asJavaClass()); | |
| 231 | + case MAP: | |
| 232 | + return bs.getMap(0, dataType.getTypeArguments().get(0).asJavaClass(), dataType.getTypeArguments().get(1).asJavaClass()); | |
| 233 | + case CUSTOM: | |
| 234 | + case COUNTER: | |
| 235 | + default: | |
| 236 | + fail("Unexpected type in bound statement test: " + dataType); | |
| 237 | + return null; | |
| 238 | + } | |
| 239 | + } | |
| 240 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +10 −607
| @@ -15,475 +15,24 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.math.BigDecimal; | |
| 19 | -import java.math.BigInteger; | |
| 20 | -import java.net.InetAddress; | |
| 21 | 18 | import java.nio.ByteBuffer; |
| 22 | -import java.text.SimpleDateFormat; | |
| 23 | -import java.util.*; | |
| 19 | +import java.util.Arrays; | |
| 20 | +import java.util.List; | |
| 24 | 21 | |
| 25 | 22 | import org.testng.annotations.Test; |
| 26 | -import static org.testng.Assert.*; | |
| 27 | 23 | |
| 28 | -import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 29 | -import com.datastax.driver.core.utils.CassandraVersion; | |
| 30 | - | |
| 31 | -/** | |
| 32 | - * Tests DataType class to ensure data sent in is the same as data received | |
| 33 | - * All tests are executed via a Simple Statements | |
| 34 | - * Counters are the only datatype not tested within the entirety of the suite. | |
| 35 | - * There is, however, an isolated test case that needs to be implemented. | |
| 36 | - * All statements and sample data is easily exportable via the print_*() methods. | |
| 37 | - */ | |
| 38 | -public class DataTypeTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 39 | - | |
| 40 | - private final static Set<DataType> DATA_TYPE_PRIMITIVES = DataType.allPrimitiveTypes(); | |
| 41 | - private final static Set<DataType.Name> DATA_TYPE_NON_PRIMITIVE_NAMES = EnumSet.of(DataType.Name.MAP, DataType.Name.SET, DataType.Name.LIST); | |
| 42 | - | |
| 43 | - private final static String PRIMITIVE_INSERT_FORMAT = "INSERT INTO %1$s (k, v) VALUES (%2$s, %2$s);"; | |
| 44 | - private final static String BASIC_SELECT_FORMAT = "SELECT k, v FROM %1$s;"; | |
| 45 | - | |
| 46 | - private final static String COLLECTION_INSERT_FORMAT = "INSERT INTO %1$s (k, v) VALUES (%2$s, %3$s);"; | |
| 47 | - private final static String MAP_INSERT_FORMAT = "INSERT INTO %1$s (k, v) VALUES (%3$s, {%2$s: %3$s});"; | |
| 48 | - | |
| 49 | - private final static HashMap<DataType, Object> SAMPLE_DATA = getSampleData(); | |
| 50 | - private final static HashMap<DataType, Object> SAMPLE_COLLECTIONS = getSampleCollections(); | |
| 51 | - | |
| 52 | - private final static Collection<String> PRIMITIVE_INSERT_STATEMENTS = getPrimitiveInsertStatements(); | |
| 53 | - private final static HashMap<DataType, String> PRIMITIVE_SELECT_STATEMENTS = getPrimitiveSelectStatements(); | |
| 54 | - | |
| 55 | - private final static Collection<String> COLLECTION_INSERT_STATEMENTS = getCollectionInsertStatements(); | |
| 56 | - private final static HashMap<DataType, String> COLLECTION_SELECT_STATEMENTS = getCollectionSelectStatements(); | |
| 57 | - | |
| 58 | - | |
| 59 | - private static boolean exclude(DataType t) { | |
| 60 | - return t.getName() == DataType.Name.COUNTER; | |
| 61 | - } | |
| 62 | - | |
| 63 | - /** | |
| 64 | - * Generates the table definitions that will be used in testing | |
| 65 | - */ | |
| 66 | - @Override | |
| 67 | - protected Collection<String> getTableDefinitions() { | |
| 68 | - ArrayList<String> tableDefinitions = new ArrayList<String>(); | |
| 69 | - | |
| 70 | - // Create primitive data type definitions | |
| 71 | - for (DataType dataType : DATA_TYPE_PRIMITIVES) { | |
| 72 | - if (exclude(dataType)) | |
| 73 | - continue; | |
| 74 | - | |
| 75 | - tableDefinitions.add(String.format("CREATE TABLE %1$s (k %2$s PRIMARY KEY, v %1$s)", dataType, dataType)); | |
| 76 | - } | |
| 77 | - | |
| 78 | - // Create collection data type definitions | |
| 79 | - for (DataType.Name dataTypeName : DATA_TYPE_NON_PRIMITIVE_NAMES) { | |
| 80 | - // Create MAP data type definitions | |
| 81 | - if (dataTypeName == DataType.Name.MAP) { | |
| 82 | - for (DataType typeArgument1 : DATA_TYPE_PRIMITIVES) { | |
| 83 | - if (exclude(typeArgument1)) | |
| 84 | - continue; | |
| 85 | - | |
| 86 | - for (DataType typeArgument2 : DATA_TYPE_PRIMITIVES) { | |
| 87 | - if (exclude(typeArgument2)) | |
| 88 | - continue; | |
| 89 | - | |
| 90 | - tableDefinitions.add(String.format("CREATE TABLE %1$s_%2$s_%3$s (k %3$s PRIMARY KEY, v %1$s<%2$s, %3$s>)", dataTypeName, typeArgument1, typeArgument2)); | |
| 91 | - } | |
| 92 | - } | |
| 93 | - // Create SET and LIST data type definitions | |
| 94 | - } else { | |
| 95 | - for (DataType typeArgument : DATA_TYPE_PRIMITIVES) { | |
| 96 | - if (exclude(typeArgument)) | |
| 97 | - continue; | |
| 98 | - | |
| 99 | - tableDefinitions.add(String.format("CREATE TABLE %1$s_%2$s (k %2$s PRIMARY KEY, v %1$s<%2$s>)", dataTypeName, typeArgument)); | |
| 100 | - } | |
| 101 | - } | |
| 102 | - } | |
| 103 | - | |
| 104 | - return tableDefinitions; | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Generates the sample data that will be used in testing | |
| 109 | - */ | |
| 110 | - private static HashMap<DataType, Object> getSampleData() { | |
| 111 | - HashMap<DataType, Object> sampleData = new HashMap<DataType, Object>(); | |
| 112 | - | |
| 113 | - for (DataType dataType : DATA_TYPE_PRIMITIVES) { | |
| 114 | - switch (dataType.getName()) { | |
| 115 | - case ASCII: | |
| 116 | - sampleData.put(dataType, new String("ascii")); | |
| 117 | - break; | |
| 118 | - case BIGINT: | |
| 119 | - sampleData.put(dataType, Long.MAX_VALUE); | |
| 120 | - break; | |
| 121 | - case BLOB: | |
| 122 | - ByteBuffer bb = ByteBuffer.allocate(58); | |
| 123 | - bb.putShort((short) 0xCAFE); | |
| 124 | - bb.flip(); | |
| 125 | - sampleData.put(dataType, bb); | |
| 126 | - break; | |
| 127 | - case BOOLEAN: | |
| 128 | - sampleData.put(dataType, Boolean.TRUE); | |
| 129 | - break; | |
| 130 | - case COUNTER: | |
| 131 | - // Not supported in an insert statement | |
| 132 | - break; | |
| 133 | - case DECIMAL: | |
| 134 | - sampleData.put(dataType, new BigDecimal("12.3E+7")); | |
| 135 | - break; | |
| 136 | - case DOUBLE: | |
| 137 | - sampleData.put(dataType, Double.MAX_VALUE); | |
| 138 | - break; | |
| 139 | - case FLOAT: | |
| 140 | - sampleData.put(dataType, Float.MAX_VALUE); | |
| 141 | - break; | |
| 142 | - case INET: | |
| 143 | - try { | |
| 144 | - sampleData.put(dataType, InetAddress.getByName("123.123.123.123")); | |
| 145 | - } catch (java.net.UnknownHostException e) {} | |
| 146 | - break; | |
| 147 | - case INT: | |
| 148 | - sampleData.put(dataType, Integer.MAX_VALUE); | |
| 149 | - break; | |
| 150 | - case TEXT: | |
| 151 | - sampleData.put(dataType, new String("text")); | |
| 152 | - break; | |
| 153 | - case TIMESTAMP: | |
| 154 | - sampleData.put(dataType, new Date(872835240000L)); | |
| 155 | - break; | |
| 156 | - case TIMEUUID: | |
| 157 | - sampleData.put(dataType, UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66")); | |
| 158 | - break; | |
| 159 | - case UUID: | |
| 160 | - sampleData.put(dataType, UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00")); | |
| 161 | - break; | |
| 162 | - case VARCHAR: | |
| 163 | - sampleData.put(dataType, new String("varchar")); | |
| 164 | - break; | |
| 165 | - case VARINT: | |
| 166 | - sampleData.put(dataType, new BigInteger(Integer.toString(Integer.MAX_VALUE) + "000")); | |
| 167 | - break; | |
| 168 | - default: | |
| 169 | - throw new RuntimeException("Missing handling of " + dataType); | |
| 170 | - } | |
| 171 | - } | |
| 172 | - | |
| 173 | - return sampleData; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Generates the sample collections that will be used in testing | |
| 178 | - */ private static HashMap<DataType, Object> getSampleCollections() { | |
| 179 | - HashMap<DataType, Object> sampleCollections = new HashMap<DataType, Object>(); | |
| 180 | - HashMap<DataType, Object> setAndListCollection; | |
| 181 | - HashMap<DataType, HashMap<DataType, Object>> mapCollection; | |
| 182 | - | |
| 183 | - for (DataType.Name dataTypeName : DATA_TYPE_NON_PRIMITIVE_NAMES) { | |
| 184 | - switch (dataTypeName) { | |
| 185 | - case LIST: | |
| 186 | - for (DataType typeArgument : DATA_TYPE_PRIMITIVES) { | |
| 187 | - if (exclude(typeArgument)) | |
| 188 | - continue; | |
| 189 | - | |
| 190 | - List<Object> list = new ArrayList<Object>(); | |
| 191 | - for (int i = 0; i < 5; i++) { | |
| 192 | - list.add(SAMPLE_DATA.get(typeArgument)); | |
| 193 | - } | |
| 194 | - | |
| 195 | - setAndListCollection = new HashMap<DataType, Object>(); | |
| 196 | - setAndListCollection.put(typeArgument, list); | |
| 197 | - sampleCollections.put(DataType.list(typeArgument), setAndListCollection); | |
| 198 | - } | |
| 199 | - break; | |
| 200 | - case SET: | |
| 201 | - for (DataType typeArgument : DATA_TYPE_PRIMITIVES) { | |
| 202 | - if (exclude(typeArgument)) | |
| 203 | - continue; | |
| 204 | - | |
| 205 | - Set<Object> set = new HashSet<Object>(); | |
| 206 | - for (int i = 0; i < 5; i++) { | |
| 207 | - set.add(SAMPLE_DATA.get(typeArgument)); | |
| 208 | - } | |
| 209 | - | |
| 210 | - setAndListCollection = new HashMap<DataType, Object>(); | |
| 211 | - setAndListCollection.put(typeArgument, set); | |
| 212 | - sampleCollections.put(DataType.set(typeArgument), setAndListCollection); | |
| 213 | - } | |
| 214 | - break; | |
| 215 | - case MAP: | |
| 216 | - for (DataType typeArgument1 : DATA_TYPE_PRIMITIVES) { | |
| 217 | - if (exclude(typeArgument1)) | |
| 218 | - continue; | |
| 219 | - | |
| 220 | - for (DataType typeArgument2 : DATA_TYPE_PRIMITIVES) { | |
| 221 | - if (exclude(typeArgument2)) | |
| 222 | - continue; | |
| 223 | - | |
| 224 | - HashMap<DataType, Object> map = new HashMap<DataType, Object>(); | |
| 225 | - map.put(typeArgument1, SAMPLE_DATA.get(typeArgument2)); | |
| 226 | - | |
| 227 | - mapCollection = new HashMap<DataType, HashMap<DataType, Object>>(); | |
| 228 | - mapCollection.put(typeArgument1, map); | |
| 229 | - sampleCollections.put(DataType.map(typeArgument1, typeArgument2), mapCollection); | |
| 230 | - } | |
| 231 | - } | |
| 232 | - break; | |
| 233 | - default: | |
| 234 | - throw new RuntimeException("Missing handling of " + dataTypeName); | |
| 235 | - } | |
| 236 | - } | |
| 237 | - | |
| 238 | - return sampleCollections; | |
| 239 | - } | |
| 240 | - | |
| 241 | - /** | |
| 242 | - * Helper method to stringify SAMPLE_DATA for simple insert statements | |
| 243 | - */ | |
| 244 | - private static String helperStringifiedData(DataType dataType) { | |
| 245 | - String value = SAMPLE_DATA.get(dataType).toString(); | |
| 246 | - | |
| 247 | - switch (dataType.getName()) { | |
| 248 | - case BLOB: | |
| 249 | - value = "0xCAFE"; | |
| 250 | - break; | |
| 251 | - | |
| 252 | - case INET: | |
| 253 | - InetAddress v1 = (InetAddress) SAMPLE_DATA.get(dataType); | |
| 254 | - value = String.format("'%s'", v1.getHostAddress()); | |
| 255 | - break; | |
| 256 | - | |
| 257 | - case TIMESTAMP: | |
| 258 | - Date v2 = (Date) SAMPLE_DATA.get(dataType); | |
| 259 | - value = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(v2); | |
| 260 | - case ASCII: | |
| 261 | - case TEXT: | |
| 262 | - case VARCHAR: | |
| 263 | - value = String.format("'%s'", value); | |
| 264 | - break; | |
| 265 | - | |
| 266 | - default: | |
| 267 | - break; | |
| 268 | - } | |
| 269 | - | |
| 270 | - return value; | |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Generates the insert statements that will be used in testing | |
| 275 | - */ | |
| 276 | - private static Collection<String> getPrimitiveInsertStatements() { | |
| 277 | - ArrayList<String> insertStatements = new ArrayList<String>(); | |
| 278 | - | |
| 279 | - for (DataType dataType : SAMPLE_DATA.keySet()) { | |
| 280 | - String value = helperStringifiedData(dataType); | |
| 281 | - insertStatements.add(String.format(PRIMITIVE_INSERT_FORMAT, dataType, value)); | |
| 282 | - } | |
| 283 | - | |
| 284 | - return insertStatements; | |
| 285 | - } | |
| 286 | - | |
| 287 | - /** | |
| 288 | - * Generates the select statements that will be used in testing | |
| 289 | - */ | |
| 290 | - private static HashMap<DataType, String> getPrimitiveSelectStatements() { | |
| 291 | - HashMap<DataType, String> selectStatements = new HashMap<DataType, String>(); | |
| 292 | - | |
| 293 | - for (DataType dataType : SAMPLE_DATA.keySet()) { | |
| 294 | - selectStatements.put(dataType, String.format(BASIC_SELECT_FORMAT, dataType)); | |
| 295 | - } | |
| 296 | - | |
| 297 | - return selectStatements; | |
| 298 | - } | |
| 299 | - | |
| 300 | - /** | |
| 301 | - * Helper method to generate table names in the form of: | |
| 302 | - * DataType_TypeArgument[_TypeArgument] | |
| 303 | - */ | |
| 304 | - private static String helperGenerateTableName(DataType dataType) { | |
| 305 | - String tableName = dataType.getName().toString(); | |
| 306 | - for (DataType typeArgument : dataType.getTypeArguments()) | |
| 307 | - tableName += "_" + typeArgument; | |
| 308 | - | |
| 309 | - return tableName; | |
| 310 | - } | |
| 311 | - | |
| 312 | - /** | |
| 313 | - * Generates the insert statements that will be used in testing | |
| 314 | - */ | |
| 315 | - @SuppressWarnings("unchecked") | |
| 316 | - private static Collection<String> getCollectionInsertStatements() { | |
| 317 | - ArrayList<String> insertStatements = new ArrayList<String>(); | |
| 318 | - | |
| 319 | - String tableName; | |
| 320 | - String key; | |
| 321 | - String value; | |
| 322 | - for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) { | |
| 323 | - HashMap<DataType, Object> sampleValueMap = (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType); | |
| 324 | - | |
| 325 | - // Create tableName in form of: DataType_TypeArgument[_TypeArgument] | |
| 326 | - tableName = helperGenerateTableName(dataType); | |
| 327 | - | |
| 328 | - if (dataType.getName() == DataType.Name.MAP) { | |
| 329 | - List<DataType> typeArgument = dataType.getTypeArguments(); | |
| 330 | - | |
| 331 | - key = helperStringifiedData(typeArgument.get(0)); | |
| 332 | - value = helperStringifiedData(typeArgument.get(1)); | |
| 333 | - | |
| 334 | - insertStatements.add(String.format(MAP_INSERT_FORMAT, tableName, key, value)); | |
| 335 | - } else if (dataType.getName() == DataType.Name.LIST) { | |
| 336 | - DataType typeArgument = sampleValueMap.keySet().iterator().next(); | |
| 337 | - key = helperStringifiedData(typeArgument); | |
| 338 | - | |
| 339 | - // Create the value to be a list of the same 5 elements | |
| 340 | - value = "["; | |
| 341 | - for (int i = 0; i < 5; i++) | |
| 342 | - value += key + ','; | |
| 343 | - value = value.substring(0, value.length() - 1) + ']'; | |
| 344 | - | |
| 345 | - insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value)); | |
| 346 | - } else { | |
| 347 | - DataType typeArgument = sampleValueMap.keySet().iterator().next(); | |
| 348 | - key = helperStringifiedData(typeArgument); | |
| 349 | - value = '{' + key + '}'; | |
| 350 | - | |
| 351 | - insertStatements.add(String.format(COLLECTION_INSERT_FORMAT, tableName, key, value)); | |
| 352 | - } | |
| 353 | - } | |
| 24 | +import static org.testng.Assert.assertEquals; | |
| 25 | +import static org.testng.Assert.fail; | |
| 354 | 26 | |
| 355 | - return insertStatements; | |
| 356 | - } | |
| 357 | - | |
| 358 | - /** | |
| 359 | - * Generates the select statements that will be used in testing | |
| 360 | - */ | |
| 361 | - private static HashMap<DataType, String> getCollectionSelectStatements() { | |
| 362 | - HashMap<DataType, String> selectStatements = new HashMap<DataType, String>(); | |
| 363 | - | |
| 364 | - String tableName; | |
| 365 | - for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) { | |
| 366 | - tableName = helperGenerateTableName(dataType); | |
| 367 | - selectStatements.put(dataType, String.format(BASIC_SELECT_FORMAT, tableName)); | |
| 368 | - } | |
| 369 | - | |
| 370 | - return selectStatements; | |
| 371 | - } | |
| 372 | - | |
| 373 | - /** | |
| 374 | - * Test simple statement inserts for all primitive data types | |
| 375 | - */ | |
| 376 | - public void primitiveInsertTest() throws Throwable { | |
| 377 | - ResultSet rs; | |
| 378 | - for (String execute_string : PRIMITIVE_INSERT_STATEMENTS) { | |
| 379 | - rs = session.execute(execute_string); | |
| 380 | - assertTrue(rs.isExhausted()); | |
| 381 | - } | |
| 382 | - assertEquals(SAMPLE_DATA.size(), 15); | |
| 383 | - assertEquals(PRIMITIVE_INSERT_STATEMENTS.size(), SAMPLE_DATA.size()); | |
| 384 | - } | |
| 385 | - | |
| 386 | - /** | |
| 387 | - * Validate simple statement selects for all primitive data types | |
| 388 | - */ | |
| 389 | - public void primitiveSelectTest() throws Throwable { | |
| 390 | - String execute_string; | |
| 391 | - Object value; | |
| 392 | - Row row; | |
| 393 | - for (DataType dataType : PRIMITIVE_SELECT_STATEMENTS.keySet()) { | |
| 394 | - execute_string = PRIMITIVE_SELECT_STATEMENTS.get(dataType); | |
| 395 | - row = session.execute(execute_string).one(); | |
| 396 | - | |
| 397 | - value = SAMPLE_DATA.get(dataType); | |
| 398 | - assertEquals(TestUtils.getValue(row, "k", dataType), value); | |
| 399 | - assertEquals(TestUtils.getValue(row, "v", dataType), value); | |
| 400 | - } | |
| 401 | - assertEquals(SAMPLE_DATA.size(), 15); | |
| 402 | - assertEquals(PRIMITIVE_SELECT_STATEMENTS.keySet().size(), SAMPLE_DATA.size()); | |
| 403 | - } | |
| 404 | - | |
| 405 | - /** | |
| 406 | - * Test simple statement inserts and selects for all primitive data types | |
| 407 | - */ | |
| 408 | - @Test(groups = "long") | |
| 409 | - public void primitiveTests() throws Throwable { | |
| 410 | - primitiveInsertTest(); | |
| 411 | - primitiveSelectTest(); | |
| 412 | - } | |
| 413 | - | |
| 414 | - /** | |
| 415 | - * Test simple statement inserts for all collection data types | |
| 416 | - */ | |
| 417 | - public void collectionInsertTest() throws Throwable { | |
| 418 | - ResultSet rs; | |
| 419 | - for (String execute_string : COLLECTION_INSERT_STATEMENTS) { | |
| 420 | - rs = session.execute(execute_string); | |
| 421 | - assertTrue(rs.isExhausted()); | |
| 422 | - } | |
| 423 | - assertEquals(SAMPLE_COLLECTIONS.size(), 255); | |
| 424 | - assertEquals(COLLECTION_INSERT_STATEMENTS.size(), SAMPLE_COLLECTIONS.size()); | |
| 425 | - } | |
| 426 | - | |
| 427 | - /** | |
| 428 | - * Test simple statement selects for all collection data types | |
| 429 | - */ | |
| 430 | - @SuppressWarnings("unchecked") | |
| 431 | - public void collectionSelectTest() throws Throwable { | |
| 432 | - HashMap<DataType, Object> sampleValueMap; | |
| 433 | - String execute_string; | |
| 434 | - DataType typeArgument1; | |
| 435 | - DataType typeArgument2; | |
| 436 | - Row row; | |
| 437 | - for (DataType dataType : COLLECTION_SELECT_STATEMENTS.keySet()) { | |
| 438 | - execute_string = COLLECTION_SELECT_STATEMENTS.get(dataType); | |
| 439 | - row = session.execute(execute_string).one(); | |
| 440 | - | |
| 441 | - sampleValueMap = (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType); | |
| 442 | - typeArgument1 = dataType.getTypeArguments().get(0); | |
| 443 | - if (dataType.getName() == DataType.Name.MAP) { | |
| 444 | - typeArgument2 = dataType.getTypeArguments().get(1); | |
| 445 | - | |
| 446 | - // Create a copy of the map that is being expected | |
| 447 | - HashMap<DataType, Object> sampleMap = (HashMap<DataType, Object>) sampleValueMap.get(typeArgument1); | |
| 448 | - Object mapKey = SAMPLE_DATA.get(sampleMap.keySet().iterator().next()); | |
| 449 | - Object mapValue = sampleMap.values().iterator().next(); | |
| 450 | - HashMap<Object, Object> expectedMap = new HashMap<Object, Object>(); | |
| 451 | - expectedMap.put(mapKey, mapValue); | |
| 452 | - | |
| 453 | - assertEquals(TestUtils.getValue(row, "k", typeArgument2), SAMPLE_DATA.get(typeArgument2)); | |
| 454 | - assertEquals(TestUtils.getValue(row, "v", dataType), expectedMap); | |
| 455 | - } else { | |
| 456 | - Object expectedValue = sampleValueMap.get(typeArgument1); | |
| 457 | - | |
| 458 | - assertEquals(TestUtils.getValue(row, "k", typeArgument1), SAMPLE_DATA.get(typeArgument1)); | |
| 459 | - assertEquals(TestUtils.getValue(row, "v", dataType), expectedValue); | |
| 460 | - } | |
| 461 | - } | |
| 462 | - assertEquals(SAMPLE_COLLECTIONS.size(), 255); | |
| 463 | - assertEquals(COLLECTION_SELECT_STATEMENTS.keySet().size(), SAMPLE_COLLECTIONS.size()); | |
| 464 | - } | |
| 27 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 465 | 28 | |
| 466 | - /** | |
| 467 | - * Test simple statement inserts and selects for all collection data types | |
| 468 | - */ | |
| 469 | - @Test(groups = "long") | |
| 470 | - public void collectionTest() throws Throwable { | |
| 471 | - collectionInsertTest(); | |
| 472 | - collectionSelectTest(); | |
| 473 | - } | |
| 29 | +public class DataTypeTest { | |
| 474 | 30 | |
| 475 | - // The two following tests a really unit tests, but since the whole uses | |
| 476 | - // CCMBridge.PerClassSingleNodeCluster, they'll still spawn a cluster even | |
| 477 | - // you execute only them, so we keep them in the "long" group. We could | |
| 478 | - // move them in another class but not sure where honestly (one could argue | |
| 479 | - // that it would make more sense to move all the *other* tests to some | |
| 480 | - // DataTypeIntegrationTest class). | |
| 481 | - @Test(groups = "long") | |
| 31 | + @Test(groups = "unit") | |
| 482 | 32 | public void serializeDeserializeTest() { |
| 483 | 33 | |
| 484 | - for (DataType dt : DataType.allPrimitiveTypes()) | |
| 485 | - { | |
| 486 | - if (exclude(dt)) | |
| 34 | + for (DataType dt : DataType.allPrimitiveTypes()) { | |
| 35 | + if (dt.getName() == DataType.Name.COUNTER) | |
| 487 | 36 | continue; |
| 488 | 37 | |
| 489 | 38 | Object value = TestUtils.getFixedValue(dt); |
| @@ -502,7 +51,7 @@ | ||
| 502 | 51 | } catch (InvalidTypeException e) { /* That's what we want */ } |
| 503 | 52 | } |
| 504 | 53 | |
| 505 | - @Test(groups = "long") | |
| 54 | + @Test(groups = "unit") | |
| 506 | 55 | public void serializeDeserializeCollectionsTest() { |
| 507 | 56 | |
| 508 | 57 | List<String> l = Arrays.asList("foo", "bar"); |
| @@ -515,150 +64,4 @@ | ||
| 515 | 64 | fail("This should not have worked"); |
| 516 | 65 | } catch (InvalidTypeException e) { /* That's what we want */ } |
| 517 | 66 | } |
| 518 | - | |
| 519 | - @Test(groups = "short") | |
| 520 | - @CassandraVersion(major=2.0, minor=0, description="This feature requires protocol v2") | |
| 521 | - public void primitiveInsertWithValueTest() throws Throwable { | |
| 522 | - for (DataType dt : DataType.allPrimitiveTypes()) { | |
| 523 | - if (exclude(dt)) | |
| 524 | - continue; | |
| 525 | - | |
| 526 | - session.execute(String.format(PRIMITIVE_INSERT_FORMAT, dt, "?"), SAMPLE_DATA.get(dt), SAMPLE_DATA.get(dt)); | |
| 527 | - } | |
| 528 | - // Kind of checking results (kind of because the schema used by this class make it ultra painful | |
| 529 | - // somehow to use a different partition for different tests, so that the insert done here actually | |
| 530 | - // conflict with the one in primitiveInsertTest. So all we check is that we don't write something | |
| 531 | - // horribly wrong, but if the inserts of this test where to do nothing, the following check might | |
| 532 | - // not work. We should fix the schema used by this class) | |
| 533 | - primitiveSelectTest(); | |
| 534 | - } | |
| 535 | - | |
| 536 | - /** | |
| 537 | - * Prints the table definitions that will be used in testing | |
| 538 | - * (for exporting purposes) | |
| 539 | - */ | |
| 540 | - @Test(groups = "doc") | |
| 541 | - public void printTableDefinitions() { | |
| 542 | - String objective = "Table Definitions"; | |
| 543 | - System.out.println(String.format("Printing %s...", objective)); | |
| 544 | - | |
| 545 | - // Prints the full list of table definitions | |
| 546 | - for (String definition : getTableDefinitions()) { | |
| 547 | - System.out.println(definition); | |
| 548 | - } | |
| 549 | - | |
| 550 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 551 | - } | |
| 552 | - | |
| 553 | - /** | |
| 554 | - * Prints the sample data that will be used in testing | |
| 555 | - * (for exporting purposes) | |
| 556 | - */ | |
| 557 | - @Test(groups = "doc") | |
| 558 | - public void printSampleData() { | |
| 559 | - String objective = "Sample Data"; | |
| 560 | - System.out.println(String.format("Printing %s...", objective)); | |
| 561 | - | |
| 562 | - for (DataType dataType : SAMPLE_DATA.keySet()) { | |
| 563 | - Object sampleValue = SAMPLE_DATA.get(dataType); | |
| 564 | - System.out.println(String.format("%1$-10s %2$s", dataType, sampleValue)); | |
| 565 | - } | |
| 566 | - | |
| 567 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 568 | - } | |
| 569 | - | |
| 570 | - /** | |
| 571 | - * Prints the sample collections that will be used in testing | |
| 572 | - * (for exporting purposes) | |
| 573 | - */ | |
| 574 | - @Test(groups = "doc") | |
| 575 | - @SuppressWarnings("unchecked") | |
| 576 | - public void printSampleCollections() { | |
| 577 | - String objective = "Sample Collections"; | |
| 578 | - System.out.println(String.format("Printing %s...", objective)); | |
| 579 | - | |
| 580 | - for (DataType dataType : SAMPLE_COLLECTIONS.keySet()) { | |
| 581 | - HashMap<DataType, Object> sampleValueMap = (HashMap<DataType, Object>) SAMPLE_COLLECTIONS.get(dataType); | |
| 582 | - | |
| 583 | - if (dataType.getName() == DataType.Name.MAP) { | |
| 584 | - DataType typeArgument = sampleValueMap.keySet().iterator().next(); | |
| 585 | - HashMap<DataType, Object> sampleMap = (HashMap<DataType, Object>) sampleValueMap.get(typeArgument); | |
| 586 | - | |
| 587 | - Object mapKey = SAMPLE_DATA.get(typeArgument); | |
| 588 | - Object mapValue = sampleMap.get(typeArgument); | |
| 589 | - System.out.println(String.format("%1$-30s {%2$s : %3$s}", dataType, mapKey, mapValue)); | |
| 590 | - } else { | |
| 591 | - DataType typeArgument = sampleValueMap.keySet().iterator().next(); | |
| 592 | - Object sampleValue = sampleValueMap.get(typeArgument); | |
| 593 | - | |
| 594 | - System.out.println(String.format("%1$-30s %2$s", dataType, sampleValue)); | |
| 595 | - } | |
| 596 | - } | |
| 597 | - | |
| 598 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 599 | - } | |
| 600 | - | |
| 601 | - /** | |
| 602 | - * Prints the simple insert statements that will be used in testing | |
| 603 | - * (for exporting purposes) | |
| 604 | - */ | |
| 605 | - @Test(groups = "doc") | |
| 606 | - public void printPrimitiveInsertStatements() { | |
| 607 | - String objective = "Primitive Insert Statements"; | |
| 608 | - System.out.println(String.format("Printing %s...", objective)); | |
| 609 | - | |
| 610 | - for (String execute_string : PRIMITIVE_INSERT_STATEMENTS) { | |
| 611 | - System.out.println(execute_string); | |
| 612 | - } | |
| 613 | - | |
| 614 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 615 | - } | |
| 616 | - | |
| 617 | - /** | |
| 618 | - * Prints the simple select statements that will be used in testing | |
| 619 | - * (for exporting purposes) | |
| 620 | - */ | |
| 621 | - @Test(groups = "doc") | |
| 622 | - public void printPrimitiveSelectStatements() { | |
| 623 | - String objective = "Primitive Select Statements"; | |
| 624 | - System.out.println(String.format("Printing %s...", objective)); | |
| 625 | - | |
| 626 | - for (String execute_string : PRIMITIVE_SELECT_STATEMENTS.values()) { | |
| 627 | - System.out.println(execute_string); | |
| 628 | - } | |
| 629 | - | |
| 630 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 631 | - } | |
| 632 | - | |
| 633 | - /** | |
| 634 | - * Prints the simple insert statements that will be used in testing | |
| 635 | - * (for exporting purposes) | |
| 636 | - */ | |
| 637 | - @Test(groups = "doc") | |
| 638 | - public void printCollectionInsertStatements() { | |
| 639 | - String objective = "Collection Insert Statements"; | |
| 640 | - System.out.println(String.format("Printing %s...", objective)); | |
| 641 | - | |
| 642 | - for (String execute_string : COLLECTION_INSERT_STATEMENTS) { | |
| 643 | - System.out.println(execute_string); | |
| 644 | - } | |
| 645 | - | |
| 646 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 647 | - } | |
| 648 | - | |
| 649 | - /** | |
| 650 | - * Prints the simple insert statements that will be used in testing | |
| 651 | - * (for exporting purposes) | |
| 652 | - */ | |
| 653 | - @Test(groups = "doc") | |
| 654 | - public void printCollectionSelectStatements() { | |
| 655 | - String objective = "Collection Select Statements"; | |
| 656 | - System.out.println(String.format("Printing %s...", objective)); | |
| 657 | - | |
| 658 | - for (String execute_string : COLLECTION_SELECT_STATEMENTS.values()) { | |
| 659 | - System.out.println(execute_string); | |
| 660 | - } | |
| 661 | - | |
| 662 | - System.out.println(String.format("\nEnd of %s\n\n", objective)); | |
| 663 | - } | |
| 664 | 67 | } |
added driver-core/src/test/java/com/datastax/driver/core/PrimitiveTypeSamples.java +75 −0
| @@ -0,0 +1,75 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 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 java.math.BigDecimal; | |
| 19 | +import java.math.BigInteger; | |
| 20 | +import java.net.InetAddress; | |
| 21 | +import java.net.UnknownHostException; | |
| 22 | +import java.util.Date; | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.Map; | |
| 25 | +import java.util.UUID; | |
| 26 | + | |
| 27 | +import com.google.common.collect.ImmutableMap; | |
| 28 | +import com.google.common.collect.Lists; | |
| 29 | + | |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 31 | + | |
| 32 | +import com.datastax.driver.core.utils.Bytes; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * This class provides sample values for each primitive data type. | |
| 36 | + * | |
| 37 | + * These values have no particular meaning, the goal is just to have an instance that can be used in automated tests. | |
| 38 | + */ | |
| 39 | +public class PrimitiveTypeSamples { | |
| 40 | + | |
| 41 | + public static final Map<DataType, Object> ALL = generateAll(); | |
| 42 | + | |
| 43 | + private static Map<DataType, Object> generateAll() { | |
| 44 | + try { | |
| 45 | + ImmutableMap<DataType, Object> result = ImmutableMap.<DataType, Object>builder() | |
| 46 | + .put(DataType.ascii(), "ascii") | |
| 47 | + .put(DataType.bigint(), Long.MAX_VALUE) | |
| 48 | + .put(DataType.blob(), Bytes.fromHexString("0xCAFE")) | |
| 49 | + .put(DataType.cboolean(), Boolean.TRUE) | |
| 50 | + .put(DataType.decimal(), new BigDecimal("12.3E+7")) | |
| 51 | + .put(DataType.cdouble(), Double.MAX_VALUE) | |
| 52 | + .put(DataType.cfloat(), Float.MAX_VALUE) | |
| 53 | + .put(DataType.inet(), InetAddress.getByName("123.123.123.123")) | |
| 54 | + .put(DataType.cint(), Integer.MAX_VALUE) | |
| 55 | + .put(DataType.text(), "text") | |
| 56 | + .put(DataType.varchar(), "text") | |
| 57 | + .put(DataType.timestamp(), new Date(872835240000L)) | |
| 58 | + .put(DataType.timeuuid(), UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66")) | |
| 59 | + .put(DataType.uuid(), UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00")) | |
| 60 | + .put(DataType.varint(), new BigInteger(Integer.toString(Integer.MAX_VALUE) + "000")) | |
| 61 | + .build(); | |
| 62 | + | |
| 63 | + // Check that we cover all types (except counter) | |
| 64 | + List<DataType> tmp = Lists.newArrayList(DataType.allPrimitiveTypes()); | |
| 65 | + tmp.removeAll(result.keySet()); | |
| 66 | + assertThat(tmp) | |
| 67 | + .as("new datatype not covered in test") | |
| 68 | + .containsOnly(DataType.counter()); | |
| 69 | + | |
| 70 | + return result; | |
| 71 | + } catch (UnknownHostException e) { | |
| 72 | + throw new AssertionError(e); | |
| 73 | + } | |
| 74 | + } | |
| 75 | +} | |
| No newline at the end of the file |