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