Search in sources :

Example 6 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityReader method read.

/**
 * Reads a single POJO from a Cloud Spanner row.
 * @param type the type of POJO
 * @param source the Cloud Spanner row
 * @param includeColumns the columns to read. If null then all columns will be read.
 * @param allowMissingColumns if true, then properties with no corresponding column are
 * not mapped. If false, then an exception is thrown.
 * @param <R> the type of the POJO.
 * @return the POJO
 */
@SuppressWarnings("unchecked")
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns, boolean allowMissingColumns) {
    boolean readAllColumns = includeColumns == null;
    SpannerPersistentEntity<R> persistentEntity = (SpannerPersistentEntity<R>) this.spannerMappingContext.getPersistentEntity(type);
    StructAccessor structAccessor = new StructAccessor(source);
    StructPropertyValueProvider propertyValueProvider = new StructPropertyValueProvider(structAccessor, this.converter, this, allowMissingColumns);
    PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor = persistentEntity.getPersistenceConstructor();
    // @formatter:off
    ParameterValueProvider<SpannerPersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
    // @formatter:on
    EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
    R instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
    persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> {
        if (spannerPersistentProperty.isEmbedded()) {
            accessor.setProperty(spannerPersistentProperty, read(spannerPersistentProperty.getType(), source, includeColumns, allowMissingColumns));
        } else {
            if (!shouldSkipProperty(structAccessor, spannerPersistentProperty, includeColumns, readAllColumns, allowMissingColumns, persistenceConstructor)) {
                Object value = propertyValueProvider.getPropertyValue(spannerPersistentProperty);
                accessor.setProperty(spannerPersistentProperty, value);
            }
        }
    });
    return instance;
}
Also used : SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) Set(java.util.Set) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Struct(com.google.cloud.spanner.Struct) EntityInstantiators(org.springframework.data.convert.EntityInstantiators) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)

Example 7 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class SpannerSchemaUtils method getCreateTableDdlString.

/**
 * Gets the DDL string to create the table for the given entity in Cloud Spanner. This
 * is just one of the possible schemas that can support the given entity type. The
 * specific schema
 * is determined by the configured property type converters used by the read and write
 * methods in this SpannerOperations and will be compatible with those methods.
 * @param entityClass the entity type.
 * @param <T> the type of the entity class
 * @return the DDL string.
 */
@SuppressWarnings("unchecked")
public <T> String getCreateTableDdlString(Class<T> entityClass) {
    SpannerPersistentEntity<T> spannerPersistentEntity = (SpannerPersistentEntity<T>) this.mappingContext.getPersistentEntity(entityClass);
    StringBuilder stringBuilder = new StringBuilder().append("CREATE TABLE ").append(spannerPersistentEntity.tableName()).append(" ( ");
    StringJoiner columnStrings = new StringJoiner(" , ");
    addColumnDdlStrings(spannerPersistentEntity, columnStrings);
    stringBuilder.append(columnStrings.toString()).append(" ) PRIMARY KEY ( ");
    StringJoiner keyStrings = new StringJoiner(" , ");
    addPrimaryKeyColumnNames(spannerPersistentEntity, keyStrings);
    stringBuilder.append(keyStrings.toString()).append(" )");
    return stringBuilder.toString();
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) StringJoiner(java.util.StringJoiner)

Example 8 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class MappingSpannerReadConverter method read.

/**
 * Reads a single POJO from a Spanner row.
 * @param type the type of POJO
 * @param source the Spanner row
 * @param includeColumns the columns to read. If null then all columns will be read.
 * @param <R> the type of the POJO.
 * @return the POJO
 */
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns) {
    boolean readAllColumns = includeColumns == null;
    R object = instantiate(type);
    SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(type);
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(object);
    persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) spannerPersistentProperty -> {
        String columnName = spannerPersistentProperty.getColumnName();
        try {
            if ((!readAllColumns && !includeColumns.contains(columnName)) || source.isNull(columnName)) {
                return;
            }
        } catch (IllegalArgumentException e) {
            throw new SpannerDataException("Unable to read column from Spanner results: " + columnName, e);
        }
        Class propType = spannerPersistentProperty.getType();
        boolean valueSet;
        if (ConversionUtils.isIterableNonByteArrayType(propType)) {
            valueSet = attemptReadIterableValue(spannerPersistentProperty, source, columnName, accessor);
        } else {
            Class sourceType = this.spannerColumnTypeToJavaTypeMapping.get(source.getColumnType(columnName));
            if (sourceType != null && canConvert(sourceType, propType)) {
                valueSet = attemptReadSingleItemValue(spannerPersistentProperty, source, sourceType, columnName, accessor);
            } else {
                valueSet = false;
            }
        }
        if (!valueSet) {
            throw new SpannerDataException(String.format("The value in column with name %s" + " could not be converted to the corresponding property in the entity." + " The property's type is %s.", columnName, propType));
        }
    });
    return object;
}
Also used : Date(com.google.cloud.Date) AbstractStructReader(com.google.cloud.spanner.AbstractStructReader) CustomConversions(org.springframework.data.convert.CustomConversions) ImmutableMap(com.google.common.collect.ImmutableMap) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) Type(com.google.cloud.spanner.Type) Timestamp(com.google.cloud.Timestamp) Constructor(java.lang.reflect.Constructor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) EntityReader(org.springframework.data.convert.EntityReader) List(java.util.List) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Struct(com.google.cloud.spanner.Struct) Map(java.util.Map) ByteArray(com.google.cloud.ByteArray) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Example 9 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class MappingSpannerWriteConverter method write.

/**
 * Writes an object's properties to the sink.
 * @param source the object to write
 * @param sink the sink to which to write
 * @param includeColumns the properties/columns to write. If null, then all columns
 * are written.
 */
public void write(Object source, WriteBuilder sink, Set<String> includeColumns) {
    boolean writeAllColumns = includeColumns == null;
    SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(source.getClass());
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
    persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) spannerPersistentProperty -> {
        if (!writeAllColumns && !includeColumns.contains(spannerPersistentProperty.getColumnName())) {
            return;
        }
        writeProperty(sink, accessor, spannerPersistentProperty);
    });
}
Also used : Date(com.google.cloud.Date) CustomConversions(org.springframework.data.convert.CustomConversions) ImmutableMap(com.google.common.collect.ImmutableMap) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Timestamp(com.google.cloud.Timestamp) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Map(java.util.Map) ValueBinder(com.google.cloud.spanner.ValueBinder) BiConsumer(java.util.function.BiConsumer) ByteArray(com.google.cloud.ByteArray) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) EntityWriter(org.springframework.data.convert.EntityWriter) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Example 10 with SpannerPersistentEntity

use of org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity in project spring-cloud-gcp by spring-cloud.

the class SpannerSchemaUtils method getDdlStringForInterleavedHierarchy.

private void getDdlStringForInterleavedHierarchy(String parentTable, Class entityClass, List<String> ddlStrings, Set<Class> seenClasses, BiFunction<Class, String, String> generateSingleDdlStringFunc, boolean prependDdlString) {
    if (seenClasses.contains(entityClass)) {
        return;
    }
    seenClasses.add(entityClass);
    ddlStrings.add(prependDdlString ? 0 : ddlStrings.size(), generateSingleDdlStringFunc.apply(entityClass, parentTable));
    SpannerPersistentEntity spannerPersistentEntity = this.mappingContext.getPersistentEntity(entityClass);
    spannerPersistentEntity.doWithInterleavedProperties((PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> getDdlStringForInterleavedHierarchy(spannerPersistentEntity.tableName(), spannerPersistentProperty.getColumnInnerType(), ddlStrings, seenClasses, generateSingleDdlStringFunc, prependDdlString));
}
Also used : SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerTypeMapper(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerTypeMapper) SpannerEntityProcessor(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerEntityProcessor) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) Type(com.google.cloud.spanner.Type) ConversionUtils(org.springframework.cloud.gcp.data.spanner.core.convert.ConversionUtils) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) HashSet(java.util.HashSet) OptionalLong(java.util.OptionalLong) List(java.util.List) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) StringJoiner(java.util.StringJoiner) Key(com.google.cloud.spanner.Key) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) Assert(org.springframework.util.Assert) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Aggregations

SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)10 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)6 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)6 Set (java.util.Set)5 SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)5 Struct (com.google.cloud.spanner.Struct)4 List (java.util.List)4 Map (java.util.Map)4 BiFunction (java.util.function.BiFunction)4 Statement (com.google.cloud.spanner.Statement)3 ArrayList (java.util.ArrayList)3 StringJoiner (java.util.StringJoiner)3 PropertyHandler (org.springframework.data.mapping.PropertyHandler)3 ByteArray (com.google.cloud.ByteArray)2 Date (com.google.cloud.Date)2 Timestamp (com.google.cloud.Timestamp)2 Key (com.google.cloud.spanner.Key)2 Type (com.google.cloud.spanner.Type)2 ValueBinder (com.google.cloud.spanner.ValueBinder)2 ImmutableMap (com.google.common.collect.ImmutableMap)2