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;
}
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();
}
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;
}
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);
});
}
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));
}
Aggregations