Elffuss BraidHub

kikocis/java-driver public

Add typed getters and getObject to BoundStatement (JAVA-547, JAVA-584).
Changeset25f66ba12d0b9788e5d7f095974127d30497d59bc48c7a77f7b598f272508587
Authorolim7t <omichallat+github@gmail.com> · 2015-02-13 19:06 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-27 10:56 +01:00
Parents2e24721d7f5d

Browse the files at this changeset

7 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -18,6 +18,8 @@
1818 (JAVA-655)
1919 - [bug] Empty TokenRange returned in a one token cluster (JAVA-684)
2020 - [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)
2123
2224 Merged from 2.0.9_fixes branch:
2325
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 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import java.math.BigDecimal;
19-import java.math.BigInteger;
20-import java.net.InetAddress;
2118 import java.nio.ByteBuffer;
22-import java.util.*;
19+import java.util.List;
2320
2421 import com.datastax.driver.core.exceptions.DriverInternalError;
25-import com.datastax.driver.core.exceptions.InvalidTypeException;
2622
2723 /**
2824 * Implementation of a Row backed by an ArrayList.
2925 */
30-class ArrayBackedRow implements Row {
26+class ArrayBackedRow extends AbstractGettableData implements Row {
3127
32- private final ColumnDefinitions metadata;
3328 private final Token.Factory tokenFactory;
3429 private final List<ByteBuffer> data;
3530
3631 private ArrayBackedRow(ColumnDefinitions metadata, Token.Factory tokenFactory, List<ByteBuffer> data) {
37- this.metadata = metadata;
32+ super(metadata);
3833 this.tokenFactory = tokenFactory;
3934 this.data = data;
4035 }
@@ -50,196 +45,9 @@
5045 return metadata;
5146 }
5247
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);
24351 }
24452
24553 public Token getToken(int i) {
@@ -269,70 +77,6 @@
26977 throw new IllegalStateException("Found no column named 'token(...)'. If the column is aliased, use getToken(String).");
27078 }
27179
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-
33680 @Override
33781 public String toString() {
33882 StringBuilder sb = new StringBuilder();
@@ -340,7 +84,7 @@
34084 for (int i = 0; i < metadata.size(); i++) {
34185 if (i != 0)
34286 sb.append(", ");
343- ByteBuffer bb = data.get(i);
87+ ByteBuffer bb = getValue(i);
34488 if (bb == null)
34589 sb.append("NULL");
34690 else
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +208 −1
@@ -21,6 +21,8 @@
2121 import java.nio.ByteBuffer;
2222 import java.util.*;
2323
24+import com.google.common.collect.Lists;
25+
2426 import com.datastax.driver.core.exceptions.InvalidTypeException;
2527
2628 /**
@@ -37,13 +39,20 @@
3739 * variables for that name.
3840 * <p>
3941 * 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.
4047 */
41-public class BoundStatement extends Statement {
48+public class BoundStatement extends Statement implements GettableData {
4249
4350 final PreparedStatement statement;
4451 final ByteBuffer[] values;
4552 private ByteBuffer routingKey;
4653
54+ private DataWrapper wrapper;
55+
4756 /**
4857 * Creates a new {@code BoundStatement} from the provided prepared
4958 * statement.
@@ -53,6 +62,10 @@
5362 this.statement = statement;
5463 this.values = new ByteBuffer[statement.getVariables().size()];
5564
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+
5669 if (statement.getConsistencyLevel() != null)
5770 this.setConsistencyLevel(statement.getConsistencyLevel());
5871 if (statement.getSerialConsistencyLevel() != null)
@@ -1104,6 +1117,186 @@
11041117 return this;
11051118 }
11061119
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+
11071300 private ColumnDefinitions metadata() {
11081301 return statement.getVariables();
11091302 }
@@ -1112,4 +1305,18 @@
11121305 values[i] = value;
11131306 return this;
11141307 }
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+ }
11151322 }
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 @@
3030 * or name. When setting them by name, names follow the case insensitivity
3131 * rules explained in {@link ColumnDefinitions}.
3232 */
33-public interface Row {
33+public interface Row extends GettableData {
3434
3535 /**
3636 * Returns the columns contained in this Row.
@@ -47,6 +47,7 @@
4747 *
4848 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
4949 */
50+ @Override
5051 public boolean isNull(int i);
5152
5253 /**
@@ -58,6 +59,7 @@
5859 * @throws IllegalArgumentException if {@code name} is not part of the
5960 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
6061 */
62+ @Override
6163 public boolean isNull(String name);
6264
6365 /**
@@ -70,6 +72,7 @@
7072 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
7173 * @throws InvalidTypeException if column {@code i} is not of type BOOLEAN.
7274 */
75+ @Override
7376 public boolean getBool(int i);
7477
7578 /**
@@ -83,6 +86,7 @@
8386 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
8487 * @throws InvalidTypeException if column {@code name} is not of type BOOLEAN.
8588 */
89+ @Override
8690 public boolean getBool(String name);
8791
8892 /**
@@ -95,6 +99,7 @@
9599 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
96100 * @throws InvalidTypeException if column {@code i} is not of type INT.
97101 */
102+ @Override
98103 public int getInt(int i);
99104
100105 /**
@@ -108,6 +113,7 @@
108113 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
109114 * @throws InvalidTypeException if column {@code name} is not of type INT.
110115 */
116+ @Override
111117 public int getInt(String name);
112118
113119 /**
@@ -120,6 +126,7 @@
120126 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
121127 * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER.
122128 */
129+ @Override
123130 public long getLong(int i);
124131
125132 /**
@@ -133,6 +140,7 @@
133140 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
134141 * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER.
135142 */
143+ @Override
136144 public long getLong(String name);
137145
138146 /**
@@ -145,6 +153,7 @@
145153 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
146154 * @throws InvalidTypeException if column {@code i} is not of type TIMESTAMP.
147155 */
156+ @Override
148157 public Date getDate(int i);
149158
150159 /**
@@ -158,6 +167,7 @@
158167 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
159168 * @throws InvalidTypeException if column {@code name} is not of type TIMESTAMP.
160169 */
170+ @Override
161171 public Date getDate(String name);
162172
163173 /**
@@ -170,6 +180,7 @@
170180 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
171181 * @throws InvalidTypeException if column {@code i} is not of type FLOAT.
172182 */
183+ @Override
173184 public float getFloat(int i);
174185
175186 /**
@@ -183,6 +194,7 @@
183194 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
184195 * @throws InvalidTypeException if column {@code name} is not of type FLOAT.
185196 */
197+ @Override
186198 public float getFloat(String name);
187199
188200 /**
@@ -195,6 +207,7 @@
195207 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
196208 * @throws InvalidTypeException if column {@code i} is not of type DOUBLE.
197209 */
210+ @Override
198211 public double getDouble(int i);
199212
200213 /**
@@ -208,6 +221,7 @@
208221 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
209222 * @throws InvalidTypeException if column {@code name} is not of type DOUBLE.
210223 */
224+ @Override
211225 public double getDouble(String name);
212226
213227 /**
@@ -224,6 +238,7 @@
224238 *
225239 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
226240 */
241+ @Override
227242 public ByteBuffer getBytesUnsafe(int i);
228243
229244 /**
@@ -241,6 +256,7 @@
241256 * @throws IllegalArgumentException if {@code name} is not part of the
242257 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
243258 */
259+ @Override
244260 public ByteBuffer getBytesUnsafe(String name);
245261
246262 /**
@@ -256,6 +272,7 @@
256272 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
257273 * @throws InvalidTypeException if column {@code i} type is not of type BLOB.
258274 */
275+ @Override
259276 public ByteBuffer getBytes(int i);
260277
261278 /**
@@ -272,6 +289,7 @@
272289 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
273290 * @throws InvalidTypeException if column {@code i} type is not of type BLOB.
274291 */
292+ @Override
275293 public ByteBuffer getBytes(String name);
276294
277295 /**
@@ -285,6 +303,7 @@
285303 * @throws InvalidTypeException if column {@code i} type is none of:
286304 * VARCHAR, TEXT or ASCII.
287305 */
306+ @Override
288307 public String getString(int i);
289308
290309 /**
@@ -299,6 +318,7 @@
299318 * @throws InvalidTypeException if column {@code name} type is none of:
300319 * VARCHAR, TEXT or ASCII.
301320 */
321+ @Override
302322 public String getString(String name);
303323
304324 /**
@@ -311,6 +331,7 @@
311331 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
312332 * @throws InvalidTypeException if column {@code i} is not of type VARINT.
313333 */
334+ @Override
314335 public BigInteger getVarint(int i);
315336
316337 /**
@@ -324,6 +345,7 @@
324345 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
325346 * @throws InvalidTypeException if column {@code name} is not of type VARINT.
326347 */
348+ @Override
327349 public BigInteger getVarint(String name);
328350
329351 /**
@@ -336,6 +358,7 @@
336358 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
337359 * @throws InvalidTypeException if column {@code i} is not of type DECIMAL.
338360 */
361+ @Override
339362 public BigDecimal getDecimal(int i);
340363
341364 /**
@@ -349,6 +372,7 @@
349372 * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
350373 * @throws InvalidTypeException if column {@code name} is not of type DECIMAL.
351374 */
375+ @Override
352376 public BigDecimal getDecimal(String name);
353377
354378 /**
@@ -362,6 +386,7 @@
362386 * @throws InvalidTypeException if column {@code i} is not of type UUID
363387 * or TIMEUUID.
364388 */
389+ @Override
365390 public UUID getUUID(int i);
366391
367392 /**
@@ -376,6 +401,7 @@
376401 * @throws InvalidTypeException if column {@code name} is not of type
377402 * UUID or TIMEUUID.
378403 */
404+ @Override
379405 public UUID getUUID(String name);
380406
381407 /**
@@ -388,6 +414,7 @@
388414 * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
389415 * @throws InvalidTypeException if column {@code i} is not of type INET.
390416 */
417+ @Override
391418 public InetAddress getInet(int i);
392419
393420 /**
@@ -402,6 +429,7 @@
402429 * @throws InvalidTypeException if column {@code name} is not of type
403430 * INET.
404431 */
432+ @Override
405433 public InetAddress getInet(String name);
406434
407435 /**
@@ -473,6 +501,7 @@
473501 * @throws InvalidTypeException if column {@code i} is not a list or if its
474502 * elements are not of class {@code elementsClass}.
475503 */
504+ @Override
476505 public <T> List<T> getList(int i, Class<T> elementsClass);
477506
478507 /**
@@ -491,6 +520,7 @@
491520 * @throws InvalidTypeException if column {@code name} is not a list or if its
492521 * elements are not of class {@code elementsClass}.
493522 */
523+ @Override
494524 public <T> List<T> getList(String name, Class<T> elementsClass);
495525
496526 /**
@@ -508,6 +538,7 @@
508538 * @throws InvalidTypeException if column {@code i} is not a set or if its
509539 * elements are not of class {@code elementsClass}.
510540 */
541+ @Override
511542 public <T> Set<T> getSet(int i, Class<T> elementsClass);
512543
513544 /**
@@ -526,6 +557,7 @@
526557 * @throws InvalidTypeException if column {@code name} is not a set or if its
527558 * elements are not of class {@code elementsClass}.
528559 */
560+ @Override
529561 public <T> Set<T> getSet(String name, Class<T> elementsClass);
530562
531563 /**
@@ -547,6 +579,7 @@
547579 * keys are not of class {@code keysClass} or if its values are not of
548580 * class {@code valuesClass}.
549581 */
582+ @Override
550583 public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass);
551584
552585 /**
@@ -569,5 +602,6 @@
569602 * keys are not of class {@code keysClass} or if its values are not of
570603 * class {@code valuesClass}.
571604 */
605+ @Override
572606 public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass);
573607 }
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