use of com.google.cloud.datastore.StructuredQuery.Filter in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method applyQueryOptions.
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) {
if (persistentEntity.getDiscriminationFieldName() != null && persistentEntity.getDiscriminatorValue() != null) {
StructuredQuery.Filter discriminationFilter = PropertyFilter.eq(persistentEntity.getDiscriminationFieldName(), persistentEntity.getDiscriminatorValue());
StructuredQuery.Filter filter = builder.build().getFilter();
if (filter != null) {
discriminationFilter = StructuredQuery.CompositeFilter.and(filter, discriminationFilter);
}
builder.setFilter(discriminationFilter);
}
if (queryOptions == null) {
return;
}
if (queryOptions.getLimit() != null) {
builder.setLimit(queryOptions.getLimit());
}
if (queryOptions.getCursor() == null && queryOptions.getOffset() != null) {
builder.setOffset(queryOptions.getOffset());
}
if (queryOptions.getCursor() != null) {
builder.setStartCursor(queryOptions.getCursor());
}
if (queryOptions.getSort() != null && persistentEntity != null) {
queryOptions.getSort().stream().map((order) -> createOrderBy(persistentEntity, order)).forEachOrdered((orderBy) -> builder.addOrderBy(orderBy));
}
}
use of com.google.cloud.datastore.StructuredQuery.Filter in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method validateExample.
private <T> void validateExample(Example<T> example) {
Assert.notNull(example, "A non-null example is expected");
ExampleMatcher matcher = example.getMatcher();
if (!matcher.isAllMatching()) {
throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported");
}
if (matcher.isIgnoreCaseEnabled()) {
throw new DatastoreDataException("Ignore case matching is not supported");
}
if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) {
throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");
}
Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter((s) -> s.contains(".")).findFirst();
if (path.isPresent()) {
throw new DatastoreDataException("Ignored paths deeper than 1 are not supported");
}
if (matcher.getPropertySpecifiers().hasValues()) {
throw new DatastoreDataException("Property matchers are not supported");
}
}
use of com.google.cloud.datastore.StructuredQuery.Filter 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