Search in sources :

Example 1 with Builder

use of com.google.cloud.datastore.StructuredQuery.Builder in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method applyQueryBody.

private StructuredQuery applyQueryBody(Object[] parameters, Builder builder, boolean total, boolean singularResult, Cursor cursor) {
    ParameterAccessor paramAccessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
    if (this.tree.hasPredicate()) {
        applySelectWithFilter(parameters, builder);
    }
    Pageable pageable = paramAccessor.getPageable();
    Integer limit = null;
    Integer offset = null;
    if (singularResult || this.tree.isExistsProjection()) {
        limit = 1;
    } else if (this.tree.isLimiting()) {
        limit = this.tree.getMaxResults();
    }
    if (!singularResult && !total && pageable.isPaged()) {
        limit = pageable.getPageSize();
    }
    Sort sort = this.tree.getSort();
    if (getQueryMethod().getParameters().hasPageableParameter()) {
        sort = sort.and(pageable.getSort());
    }
    if (getQueryMethod().getParameters().hasSortParameter()) {
        sort = sort.and(paramAccessor.getSort());
    }
    if (pageable.isPaged() && !total) {
        offset = (int) pageable.getOffset();
    }
    Cursor cursorToApply = null;
    if (cursor != null) {
        cursorToApply = cursor;
    } else if (pageable instanceof DatastorePageable) {
        cursorToApply = ((DatastorePageable) pageable).toCursor();
    }
    DatastoreTemplate.applyQueryOptions(builder, new DatastoreQueryOptions.Builder().setLimit(limit).setOffset(offset).setSort(sort).setCursor(cursorToApply).build(), this.datastorePersistentEntity);
    return builder.build();
}
Also used : Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Builder(com.google.cloud.datastore.StructuredQuery.Builder) Sort(org.springframework.data.domain.Sort) Cursor(com.google.cloud.datastore.Cursor)

Example 2 with Builder

use of com.google.cloud.datastore.StructuredQuery.Builder in project spring-cloud-gcp by spring-cloud.

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) 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) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) 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) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) 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) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) 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) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) DatastoreOperations(org.springframework.cloud.gcp.data.datastore.core.DatastoreOperations) Value(com.google.cloud.datastore.Value) DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) 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(org.springframework.cloud.gcp.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(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

Cursor (com.google.cloud.datastore.Cursor)2 Builder (com.google.cloud.datastore.StructuredQuery.Builder)2 MapBuilder (org.springframework.cloud.gcp.core.util.MapBuilder)2 Pageable (org.springframework.data.domain.Pageable)2 Sort (org.springframework.data.domain.Sort)2 ParameterAccessor (org.springframework.data.repository.query.ParameterAccessor)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 EntityQuery (com.google.cloud.datastore.EntityQuery)1 KeyQuery (com.google.cloud.datastore.KeyQuery)1 KeyValue (com.google.cloud.datastore.KeyValue)1 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)1 Query (com.google.cloud.datastore.Query)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 CompositeFilter (com.google.cloud.datastore.StructuredQuery.CompositeFilter)1 Filter (com.google.cloud.datastore.StructuredQuery.Filter)1 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)1 Value (com.google.cloud.datastore.Value)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1