kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 66f63295d45cccaa0f88d42e626293edb121af40d2a7184fd9dd0de38a452886 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-03-27 11:12 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-27 11:42 +01:00 |
| Parents | efe21da36364, 42972cf86120 |
8 files changed
modified driver-core/CHANGELOG.rst +3 −0
| @@ -4,6 +4,9 @@ | ||
| 4 | 4 | 2.1.6: |
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | +- [new feature] Add getObject to BoundStatement, Row, UDTValue and TupleValue | |
| 8 | + (JAVA-584) | |
| 9 | + | |
| 7 | 10 | |
| 8 | 11 | 2.1.5: |
| 9 | 12 | ------ |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractGettableByIndexData.java +22 −0
| @@ -443,4 +443,26 @@ | ||
| 443 | 443 | // tuples always use the protocol V3 to encode values |
| 444 | 444 | return (TupleValue)type.codec(ProtocolVersion.V3).deserialize(value); |
| 445 | 445 | } |
| 446 | + | |
| 447 | + /** | |
| 448 | + * {@inheritDoc} | |
| 449 | + */ | |
| 450 | + @Override | |
| 451 | + public Object getObject(int i) { | |
| 452 | + ByteBuffer raw = getValue(i); | |
| 453 | + DataType type = getType(i); | |
| 454 | + if (raw == null) | |
| 455 | + switch (type.getName()) { | |
| 456 | + case LIST: | |
| 457 | + return Collections.emptyList(); | |
| 458 | + case SET: | |
| 459 | + return Collections.emptySet(); | |
| 460 | + case MAP: | |
| 461 | + return Collections.emptyMap(); | |
| 462 | + default: | |
| 463 | + return null; | |
| 464 | + } | |
| 465 | + else | |
| 466 | + return type.deserialize(raw, protocolVersion); | |
| 467 | + } | |
| 446 | 468 | } |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractGettableData.java +8 −0
| @@ -234,4 +234,12 @@ | ||
| 234 | 234 | public TupleValue getTupleValue(String name) { |
| 235 | 235 | return getTupleValue(getIndexOf(name)); |
| 236 | 236 | } |
| 237 | + | |
| 238 | + /** | |
| 239 | + * {@inheritDoc} | |
| 240 | + */ | |
| 241 | + @Override | |
| 242 | + public Object getObject(String name) { | |
| 243 | + return getObject(getIndexOf(name)); | |
| 244 | + } | |
| 237 | 245 | } |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +14 −0
| @@ -1345,6 +1345,20 @@ | ||
| 1345 | 1345 | return wrapper.getTupleValue(name); |
| 1346 | 1346 | } |
| 1347 | 1347 | |
| 1348 | + /** | |
| 1349 | + * {@inheritDoc} | |
| 1350 | + */ | |
| 1351 | + public Object getObject(int i) { | |
| 1352 | + return wrapper.getObject(i); | |
| 1353 | + } | |
| 1354 | + | |
| 1355 | + /** | |
| 1356 | + * {@inheritDoc} | |
| 1357 | + */ | |
| 1358 | + public Object getObject(String name) { | |
| 1359 | + return wrapper.getObject(name); | |
| 1360 | + } | |
| 1361 | + | |
| 1348 | 1362 | static class DataWrapper extends AbstractData<BoundStatement> { |
| 1349 | 1363 | |
| 1350 | 1364 | DataWrapper(BoundStatement wrapped, int size) { |
modified driver-core/src/main/java/com/datastax/driver/core/GettableByIndexData.java +12 −0
| @@ -353,4 +353,16 @@ | ||
| 353 | 353 | * @throws InvalidTypeException if value {@code i} is not a tuple value. |
| 354 | 354 | */ |
| 355 | 355 | public TupleValue getTupleValue(int i); |
| 356 | + | |
| 357 | + /** | |
| 358 | + * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 359 | + * | |
| 360 | + * @param i the index to retrieve. | |
| 361 | + * @return the value of the {@code i}th value as the Java type matching its CQL type. | |
| 362 | + * If the value is NULL and is a simple type, UDT or tuple, {@code null} is returned. | |
| 363 | + * If it is NULL and is a collection type, an empty (immutable) collection is returned. | |
| 364 | + * | |
| 365 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 366 | + */ | |
| 367 | + public Object getObject(int i); | |
| 356 | 368 | } |
| No newline at the end of the file |
modified driver-core/src/main/java/com/datastax/driver/core/GettableByNameData.java +12 −0
| @@ -352,4 +352,16 @@ | ||
| 352 | 352 | * @throws InvalidTypeException if value {@code i} is not a tuple value. |
| 353 | 353 | */ |
| 354 | 354 | public TupleValue getTupleValue(String name); |
| 355 | + | |
| 356 | + /** | |
| 357 | + * Returns the value for {@code name} as the Java type matching its CQL type. | |
| 358 | + * | |
| 359 | + * @param name the name to retrieve. | |
| 360 | + * @return the value of the {@code i}th value as the Java type matching its CQL type. | |
| 361 | + * If the value is NULL and is a simple type, UDT or tuple, {@code null} is returned. | |
| 362 | + * If it is NULL and is a collection type, an empty (immutable) collection is returned. | |
| 363 | + * | |
| 364 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 365 | + */ | |
| 366 | + Object getObject(String name); | |
| 355 | 367 | } |
| No newline at the end of the file |
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 |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +57 −1
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | 25 | |
| 26 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.fail; | |
| 27 | 28 | |
| 28 | 29 | import com.datastax.driver.core.utils.CassandraVersion; |
| 29 | 30 | |
| @@ -92,7 +93,9 @@ | ||
| 92 | 93 | break; |
| 93 | 94 | case PREPARED: |
| 94 | 95 | PreparedStatement ps = session.prepare(table.insertStatement); |
| 95 | - session.execute(ps.bind(table.sampleValue)); | |
| 96 | + BoundStatement bs = ps.bind(table.sampleValue); | |
| 97 | + checkGetterReturnsBoundValue(bs, table); | |
| 98 | + session.execute(bs); | |
| 96 | 99 | break; |
| 97 | 100 | } |
| 98 | 101 | |
| @@ -111,6 +114,14 @@ | ||
| 111 | 114 | } |
| 112 | 115 | } |
| 113 | 116 | |
| 117 | + private void checkGetterReturnsBoundValue(BoundStatement bs, TestTable table) { | |
| 118 | + Object getterResult = getBoundValue(bs, table.testColumnType); | |
| 119 | + assertThat(getterResult).isEqualTo(table.sampleValue); | |
| 120 | + | |
| 121 | + // Ensure that bs.getObject() also returns the expected value. | |
| 122 | + assertThat(bs.getObject(0)).isEqualTo(table.sampleValue); | |
| 123 | + } | |
| 124 | + | |
| 114 | 125 | /** |
| 115 | 126 | * Abstracts information about a table (corresponding to a given column type). |
| 116 | 127 | */ |
| @@ -319,4 +330,49 @@ | ||
| 319 | 330 | } |
| 320 | 331 | return null; |
| 321 | 332 | } |
| 333 | + | |
| 334 | + private Object getBoundValue(BoundStatement bs, DataType dataType) { | |
| 335 | + // This is kind of lame, but better than testing all getters manually | |
| 336 | + switch (dataType.getName()) { | |
| 337 | + case ASCII: | |
| 338 | + return bs.getString(0); | |
| 339 | + case BIGINT: | |
| 340 | + return bs.getLong(0); | |
| 341 | + case BLOB: | |
| 342 | + return bs.getBytes(0); | |
| 343 | + case BOOLEAN: | |
| 344 | + return bs.getBool(0); | |
| 345 | + case DECIMAL: | |
| 346 | + return bs.getDecimal(0); | |
| 347 | + case DOUBLE: | |
| 348 | + return bs.getDouble(0); | |
| 349 | + case FLOAT: | |
| 350 | + return bs.getFloat(0); | |
| 351 | + case INET: | |
| 352 | + return bs.getInet(0); | |
| 353 | + case INT: | |
| 354 | + return bs.getInt(0); | |
| 355 | + case TEXT: | |
| 356 | + case VARCHAR: | |
| 357 | + return bs.getString(0); | |
| 358 | + case TIMESTAMP: | |
| 359 | + return bs.getDate(0); | |
| 360 | + case UUID: | |
| 361 | + case TIMEUUID: | |
| 362 | + return bs.getUUID(0); | |
| 363 | + case VARINT: | |
| 364 | + return bs.getVarint(0); | |
| 365 | + case LIST: | |
| 366 | + return bs.getList(0, dataType.getTypeArguments().get(0).asJavaClass()); | |
| 367 | + case SET: | |
| 368 | + return bs.getSet(0, dataType.getTypeArguments().get(0).asJavaClass()); | |
| 369 | + case MAP: | |
| 370 | + return bs.getMap(0, dataType.getTypeArguments().get(0).asJavaClass(), dataType.getTypeArguments().get(1).asJavaClass()); | |
| 371 | + case CUSTOM: | |
| 372 | + case COUNTER: | |
| 373 | + default: | |
| 374 | + fail("Unexpected type in bound statement test: " + dataType); | |
| 375 | + return null; | |
| 376 | + } | |
| 377 | + } | |
| 322 | 378 | } |