Search in sources :

Example 11 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQuery method applySelectWithFilter.

private void applySelectWithFilter(Object[] parameters, Builder builder) {
    Iterator it = Arrays.asList(parameters).iterator();
    Filter[] filters = this.filterParts.stream().map(part -> {
        // build properties chain for nested properties
        // if the property is not nested, the list would contain only one property
        List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part);
        String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName).collect(Collectors.joining("."));
        if (part.getType() == Part.Type.IS_NULL) {
            return PropertyFilter.isNull(fieldName);
        }
        BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType());
        if (filterFactory == null) {
            throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType());
        }
        if (!it.hasNext()) {
            throw new DatastoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
        }
        Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next());
        return filterFactory.apply(fieldName, convertedValue);
    }).toArray(Filter[]::new);
    builder.setFilter((filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]);
}
Also used : Query(com.google.cloud.datastore.Query) Arrays(java.util.Arrays) MapBuilder(com.google.cloud.spring.core.util.MapBuilder) BiFunction(java.util.function.BiFunction) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) Map(java.util.Map) DatastoreQueryOptions(com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Builder(com.google.cloud.datastore.StructuredQuery.Builder) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) List(java.util.List) PropertyDescriptor(java.beans.PropertyDescriptor) OrPart(org.springframework.data.repository.query.parser.PartTree.OrPart) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) PageImpl(org.springframework.data.domain.PageImpl) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Function(java.util.function.Function) Part(org.springframework.data.repository.query.parser.Part) Cursor(com.google.cloud.datastore.Cursor) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreOperations(com.google.cloud.spring.data.datastore.core.DatastoreOperations) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) EntityQuery(com.google.cloud.datastore.EntityQuery) Value(com.google.cloud.datastore.Value) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastoreTemplate(com.google.cloud.spring.data.datastore.core.DatastoreTemplate) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) BiFunction(java.util.function.BiFunction) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Iterator(java.util.Iterator) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) List(java.util.List) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)11 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)8 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)7 Entity (com.google.cloud.datastore.Entity)5 Value (com.google.cloud.datastore.Value)5 BaseEntity (com.google.cloud.datastore.BaseEntity)4 Cursor (com.google.cloud.datastore.Cursor)4 EntityQuery (com.google.cloud.datastore.EntityQuery)4 FullEntity (com.google.cloud.datastore.FullEntity)4 KeyQuery (com.google.cloud.datastore.KeyQuery)4 KeyValue (com.google.cloud.datastore.KeyValue)4 ListValue (com.google.cloud.datastore.ListValue)4 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)4 Query (com.google.cloud.datastore.Query)4 StructuredQuery (com.google.cloud.datastore.StructuredQuery)4 Filter (com.google.cloud.datastore.StructuredQuery.Filter)4 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)4 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 BaseKey (com.google.cloud.datastore.BaseKey)3