use of org.springframework.data.mapping.PersistentPropertyAccessor 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.data.mapping.PersistentPropertyAccessor 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.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class SpannerRepositoryIntegrationTests method existsTest.
@Test
public void existsTest() {
Trade trade = Trade.aTrade();
this.tradeRepository.save(trade);
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(Trade.class);
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(trade);
PersistentProperty idProperty = persistentEntity.getIdProperty();
Key key = (Key) accessor.getProperty(idProperty);
assertThat(this.tradeRepository.existsById(key)).isTrue();
this.tradeRepository.delete(trade);
assertThat(this.tradeRepository.existsById(key)).isFalse();
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class SpannerCompositeKeyProperty method getId.
Key getId(Object entity) {
PersistentPropertyAccessor accessor = getOwner().getPropertyAccessor(entity);
List keyParts = new ArrayList();
for (SpannerPersistentProperty spannerPersistentProperty : this.primaryKeyColumns) {
Object value = accessor.getProperty(spannerPersistentProperty);
if (spannerPersistentProperty.isEmbedded()) {
Key embeddedKeyParts = this.spannerPersistentEntity.getSpannerMappingContext().getPersistentEntity(spannerPersistentProperty.getType()).getIdProperty().getId(value);
for (Object keyPart : embeddedKeyParts.getParts()) {
keyParts.add(keyPart);
}
} else if (spannerPersistentProperty.getAnnotatedColumnItemType() == null || value == null) {
keyParts.add(value);
} else {
keyParts.add(this.spannerPersistentEntity.getSpannerEntityProcessor().getSpannerWriteConverter().convert(value, SpannerTypeMapper.getSimpleJavaClassFor(spannerPersistentProperty.getAnnotatedColumnItemType())));
}
}
return this.spannerPersistentEntity.getSpannerEntityProcessor().convertToKey(keyParts);
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method exampleToQuery.
private <T> StructuredQuery exampleToQuery(Example<T> example, DatastoreQueryOptions queryOptions, boolean keyQuery) {
validateExample(example);
T probe = example.getProbe();
FullEntity.Builder<IncompleteKey> probeEntityBuilder = Entity.newBuilder();
this.datastoreEntityConverter.write(probe, probeEntityBuilder);
FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(example.getProbeType());
LinkedList<StructuredQuery.Filter> filters = new LinkedList<>();
NullHandler nullHandler = example.getMatcher().getNullHandler();
persistentEntity.doWithColumnBackedProperties((persistentProperty) -> {
if (!ignoredProperty(example, persistentProperty)) {
Value<?> value = getValue(example, probeEntity, persistentEntity, persistentProperty);
addFilter(nullHandler, filters, persistentProperty.getFieldName(), value);
}
});
persistentEntity.doWithAssociations((AssociationHandler<DatastorePersistentProperty>) association -> {
PersistentPropertyAccessor<?> accessor = persistentEntity.getPropertyAccessor(example.getProbe());
DatastorePersistentProperty property = association.getInverse();
Object value = accessor.getProperty(property);
Value<?> key = value == null ? NullValue.of() : KeyValue.of(objectToKeyFactory.getKeyFromObject(value, this.datastoreMappingContext.getPersistentEntity(value.getClass())));
addFilter(nullHandler, filters, property.getFieldName(), key);
});
StructuredQuery.Builder<?> builder = keyQuery ? Query.newKeyQueryBuilder() : Query.newEntityQueryBuilder();
builder.setKind(persistentEntity.kindName());
if (!filters.isEmpty()) {
builder.setFilter(StructuredQuery.CompositeFilter.and(filters.pop(), filters.toArray(new StructuredQuery.Filter[0])));
}
applyQueryOptions(builder, queryOptions, persistentEntity);
return builder.build();
}
Aggregations