DATACASS-656 - Add CassandraValueProvider for new driver types (row, tuples, UDT).

Original pull request: #167.
This commit is contained in:
Mark Paluch
2019-11-27 14:40:45 +01:00
parent 7f743e3c4e
commit 3eb2bb10f0
9 changed files with 68 additions and 19 deletions

View File

@@ -84,10 +84,6 @@ public class BasicCassandraRowValueProvider implements CassandraRowValueProvider
: (T) this.reader.get(property.getRequiredColumnName());
}
/* (non-Javadoc)
* @see org.springframework.data.cassandra.core.convert.CassandraRowValueProvider#getRow()
*/
@Override
public Row getRow() {
return this.reader.getRow();
}
@@ -100,6 +96,6 @@ public class BasicCassandraRowValueProvider implements CassandraRowValueProvider
Assert.notNull(property, "CassandraPersistentProperty must not be null");
return getRow().getColumnDefinitions().contains(property.getRequiredColumnName().toCql());
return this.reader.contains(property.getRequiredColumnName());
}
}

View File

@@ -17,6 +17,8 @@
package org.springframework.data.cassandra.core.convert;
import java.net.InetAddress;
import java.time.Instant;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@@ -31,8 +33,7 @@ import org.springframework.data.convert.ReadingConverter;
import org.springframework.util.Assert;
import org.springframework.util.NumberUtils;
import com.datastax.driver.core.LocalDate;
import com.datastax.driver.core.Row;
import com.datastax.oss.driver.api.core.cql.Row;
/**
* Wrapper class to contain useful converters for the usage with Cassandra.
@@ -56,6 +57,7 @@ abstract class CassandraConverters {
converters.add(RowToCassandraLocalDateConverter.INSTANCE);
converters.add(RowToBooleanConverter.INSTANCE);
converters.add(RowToInstantConverter.INSTANCE);
converters.add(RowToDateConverter.INSTANCE);
converters.add(RowToInetAddressConverter.INSTANCE);
converters.add(RowToListConverter.INSTANCE);
@@ -74,7 +76,7 @@ abstract class CassandraConverters {
@Override
public Boolean convert(Row row) {
return row.getBool(0);
return row.getBoolean(0);
}
}
@@ -90,7 +92,23 @@ abstract class CassandraConverters {
@Override
public Date convert(Row row) {
return row.getTimestamp(0);
return Date.from(row.getInstant(0));
}
}
/**
* Simple singleton to convert {@link Row}s to their {@link Instant} representation.
*
* @author Mark Paluch
*/
@ReadingConverter
public enum RowToInstantConverter implements Converter<Row, Instant> {
INSTANCE;
@Override
public Instant convert(Row row) {
return row.getInstant(0);
}
}
@@ -106,7 +124,7 @@ abstract class CassandraConverters {
@Override
public InetAddress convert(Row row) {
return row.getInet(0);
return row.getInetAddress(0);
}
}
@@ -182,7 +200,7 @@ abstract class CassandraConverters {
@Override
public UUID convert(Row row) {
return row.getUUID(0);
return row.getUuid(0);
}
}
@@ -198,7 +216,7 @@ abstract class CassandraConverters {
@Override
public LocalDate convert(Row row) {
return row.getDate(0);
return row.getLocalDate(0);
}
}
}

View File

@@ -25,8 +25,4 @@ import com.datastax.driver.core.Row;
*/
public interface CassandraRowValueProvider extends CassandraValueProvider {
/**
* @return the underlying {@link Row}.
*/
Row getRow();
}

View File

@@ -170,4 +170,8 @@ public class ColumnReader {
public Row getRow() {
return row;
}
public boolean contains(CqlIdentifier columnName) {
return row.getColumnDefinitions().contains(columnName.toCql());
}
}

View File

@@ -233,12 +233,22 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
return (R) row;
}
if (com.datastax.oss.driver.api.core.cql.Row.class.isAssignableFrom(rawType)) {
return (R) row;
}
if (getCustomConversions().hasCustomReadTarget(Row.class, rawType)
|| getConversionService().canConvert(Row.class, rawType)) {
return getConversionService().convert(row, rawType);
}
if (getCustomConversions().hasCustomReadTarget(com.datastax.oss.driver.api.core.cql.Row.class, rawType)
|| getConversionService().canConvert(com.datastax.oss.driver.api.core.cql.Row.class, rawType)) {
return getConversionService().convert(row, rawType);
}
if (typeInfo.isCollectionLike() || typeInfo.isMap()) {
return getConversionService().convert(row, type);
}
@@ -246,7 +256,15 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
CassandraPersistentEntity<R> persistentEntity = (CassandraPersistentEntity<R>) getMappingContext()
.getRequiredPersistentEntity(typeInfo);
return readEntityFromRow(persistentEntity, row);
if (row instanceof Row) {
return readEntityFromRow(persistentEntity, row);
}
return readEntityFromRow(persistentEntity, (com.datastax.oss.driver.api.core.cql.Row) row);
}
private <S> S readEntityFromRow(CassandraPersistentEntity<S> entity, com.datastax.oss.driver.api.core.cql.Row row) {
return doReadEntity(entity, row, expressionEvaluator -> new RowValueProvider(row, expressionEvaluator));
}
private <S> S readEntityFromRow(CassandraPersistentEntity<S> entity, Row row) {

View File

@@ -20,7 +20,7 @@ import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.lang.Nullable;
import com.datastax.driver.core.Row;
import com.datastax.oss.driver.api.core.cql.Row;
/**
* {@link PropertyAccessor} to read values from a {@link Row}.

View File

@@ -36,7 +36,9 @@ import com.datastax.driver.core.TableMetadata;
* @author John Blum
* @see #toCql()
* @see #toString()
* @deprecated since 3.0, use {@link com.datastax.oss.driver.api.core.CqlIdentifier} instead.
*/
@Deprecated
public final class CqlIdentifier implements Comparable<CqlIdentifier>, Serializable {
private static final long serialVersionUID = -974441606330912437L;

View File

@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core.cql.converter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
/**
* Thin wrapper that allows subclasses to delegate conversion of the given value to a {@link DefaultConversionService}.
@@ -26,5 +27,5 @@ import org.springframework.core.convert.support.DefaultConversionService;
*/
public abstract class AbstractResultSetToBasicFixedTypeConverter<T> extends AbstractResultSetConverter<T> {
protected static final ConversionService CONVERTER = new DefaultConversionService();
protected static final ConversionService CONVERTER = DefaultConversionService.getSharedInstance();
}

View File

@@ -19,6 +19,7 @@ import org.springframework.data.cassandra.core.cql.CqlIdentifier;
import org.springframework.lang.Nullable;
import com.datastax.driver.core.UserType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
/**
* Strategy interface to resolve {@link UserType} by {@link String name}.
@@ -42,4 +43,17 @@ public interface UserTypeResolver {
@Nullable
UserType resolveType(CqlIdentifier typeName);
/**
* Resolve a {@link UserType} by {@link String name}.
*
* @param typeName {@link String name} of the {@link UserType} to resolve; must not be {@literal null}.
* @return the resolved {@link UserType} or {@literal null} if not found.
* @see UserDefinedType
*/
@Nullable
default UserDefinedType resolveType(com.datastax.oss.driver.api.core.CqlIdentifier typeName) {
// TODO
return null;
}
}