DATACASS-656 - Add CassandraValueProvider for new driver types (row, tuples, UDT).
Original pull request: #167.
This commit is contained in:
@@ -84,10 +84,6 @@ public class BasicCassandraRowValueProvider implements CassandraRowValueProvider
|
|||||||
: (T) this.reader.get(property.getRequiredColumnName());
|
: (T) this.reader.get(property.getRequiredColumnName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see org.springframework.data.cassandra.core.convert.CassandraRowValueProvider#getRow()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Row getRow() {
|
public Row getRow() {
|
||||||
return this.reader.getRow();
|
return this.reader.getRow();
|
||||||
}
|
}
|
||||||
@@ -100,6 +96,6 @@ public class BasicCassandraRowValueProvider implements CassandraRowValueProvider
|
|||||||
|
|
||||||
Assert.notNull(property, "CassandraPersistentProperty must not be null");
|
Assert.notNull(property, "CassandraPersistentProperty must not be null");
|
||||||
|
|
||||||
return getRow().getColumnDefinitions().contains(property.getRequiredColumnName().toCql());
|
return this.reader.contains(property.getRequiredColumnName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
package org.springframework.data.cassandra.core.convert;
|
package org.springframework.data.cassandra.core.convert;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -31,8 +33,7 @@ import org.springframework.data.convert.ReadingConverter;
|
|||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.NumberUtils;
|
import org.springframework.util.NumberUtils;
|
||||||
|
|
||||||
import com.datastax.driver.core.LocalDate;
|
import com.datastax.oss.driver.api.core.cql.Row;
|
||||||
import com.datastax.driver.core.Row;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper class to contain useful converters for the usage with Cassandra.
|
* Wrapper class to contain useful converters for the usage with Cassandra.
|
||||||
@@ -56,6 +57,7 @@ abstract class CassandraConverters {
|
|||||||
|
|
||||||
converters.add(RowToCassandraLocalDateConverter.INSTANCE);
|
converters.add(RowToCassandraLocalDateConverter.INSTANCE);
|
||||||
converters.add(RowToBooleanConverter.INSTANCE);
|
converters.add(RowToBooleanConverter.INSTANCE);
|
||||||
|
converters.add(RowToInstantConverter.INSTANCE);
|
||||||
converters.add(RowToDateConverter.INSTANCE);
|
converters.add(RowToDateConverter.INSTANCE);
|
||||||
converters.add(RowToInetAddressConverter.INSTANCE);
|
converters.add(RowToInetAddressConverter.INSTANCE);
|
||||||
converters.add(RowToListConverter.INSTANCE);
|
converters.add(RowToListConverter.INSTANCE);
|
||||||
@@ -74,7 +76,7 @@ abstract class CassandraConverters {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean convert(Row row) {
|
public Boolean convert(Row row) {
|
||||||
return row.getBool(0);
|
return row.getBoolean(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +92,23 @@ abstract class CassandraConverters {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date convert(Row row) {
|
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
|
@Override
|
||||||
public InetAddress convert(Row row) {
|
public InetAddress convert(Row row) {
|
||||||
return row.getInet(0);
|
return row.getInetAddress(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +200,7 @@ abstract class CassandraConverters {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UUID convert(Row row) {
|
public UUID convert(Row row) {
|
||||||
return row.getUUID(0);
|
return row.getUuid(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +216,7 @@ abstract class CassandraConverters {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalDate convert(Row row) {
|
public LocalDate convert(Row row) {
|
||||||
return row.getDate(0);
|
return row.getLocalDate(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,8 +25,4 @@ import com.datastax.driver.core.Row;
|
|||||||
*/
|
*/
|
||||||
public interface CassandraRowValueProvider extends CassandraValueProvider {
|
public interface CassandraRowValueProvider extends CassandraValueProvider {
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the underlying {@link Row}.
|
|
||||||
*/
|
|
||||||
Row getRow();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,4 +170,8 @@ public class ColumnReader {
|
|||||||
public Row getRow() {
|
public Row getRow() {
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean contains(CqlIdentifier columnName) {
|
||||||
|
return row.getColumnDefinitions().contains(columnName.toCql());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,12 +233,22 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
|||||||
return (R) row;
|
return (R) row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (com.datastax.oss.driver.api.core.cql.Row.class.isAssignableFrom(rawType)) {
|
||||||
|
return (R) row;
|
||||||
|
}
|
||||||
|
|
||||||
if (getCustomConversions().hasCustomReadTarget(Row.class, rawType)
|
if (getCustomConversions().hasCustomReadTarget(Row.class, rawType)
|
||||||
|| getConversionService().canConvert(Row.class, rawType)) {
|
|| getConversionService().canConvert(Row.class, rawType)) {
|
||||||
|
|
||||||
return getConversionService().convert(row, 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()) {
|
if (typeInfo.isCollectionLike() || typeInfo.isMap()) {
|
||||||
return getConversionService().convert(row, type);
|
return getConversionService().convert(row, type);
|
||||||
}
|
}
|
||||||
@@ -246,7 +256,15 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
|
|||||||
CassandraPersistentEntity<R> persistentEntity = (CassandraPersistentEntity<R>) getMappingContext()
|
CassandraPersistentEntity<R> persistentEntity = (CassandraPersistentEntity<R>) getMappingContext()
|
||||||
.getRequiredPersistentEntity(typeInfo);
|
.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) {
|
private <S> S readEntityFromRow(CassandraPersistentEntity<S> entity, Row row) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import org.springframework.expression.PropertyAccessor;
|
|||||||
import org.springframework.expression.TypedValue;
|
import org.springframework.expression.TypedValue;
|
||||||
import org.springframework.lang.Nullable;
|
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}.
|
* {@link PropertyAccessor} to read values from a {@link Row}.
|
||||||
|
|||||||
@@ -36,7 +36,9 @@ import com.datastax.driver.core.TableMetadata;
|
|||||||
* @author John Blum
|
* @author John Blum
|
||||||
* @see #toCql()
|
* @see #toCql()
|
||||||
* @see #toString()
|
* @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 {
|
public final class CqlIdentifier implements Comparable<CqlIdentifier>, Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -974441606330912437L;
|
private static final long serialVersionUID = -974441606330912437L;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package org.springframework.data.cassandra.core.cql.converter;
|
|||||||
|
|
||||||
import org.springframework.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
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}.
|
* 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> {
|
public abstract class AbstractResultSetToBasicFixedTypeConverter<T> extends AbstractResultSetConverter<T> {
|
||||||
|
|
||||||
protected static final ConversionService CONVERTER = new DefaultConversionService();
|
protected static final ConversionService CONVERTER = DefaultConversionService.getSharedInstance();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import org.springframework.data.cassandra.core.cql.CqlIdentifier;
|
|||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
import com.datastax.driver.core.UserType;
|
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}.
|
* Strategy interface to resolve {@link UserType} by {@link String name}.
|
||||||
@@ -42,4 +43,17 @@ public interface UserTypeResolver {
|
|||||||
@Nullable
|
@Nullable
|
||||||
UserType resolveType(CqlIdentifier typeName);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user