/** * Extract the {@link Predicate} representing the {@link Example}. * * @param root must not be {@literal null}. * @param cb must not be {@literal null}. * @param example must not be {@literal null}. * @return never {@literal null}. */ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) { Assert.notNull(root, "Root must not be null!"); Assert.notNull(cb, "CriteriaBuilder must not be null!"); Assert.notNull(example, "Example must not be null!"); ExampleMatcher matcher = example.getMatcher(); List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(), example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe())); if (predicates.isEmpty()) { return cb.isTrue(cb.literal(true)); } if (predicates.size() == 1) { return predicates.iterator().next(); } Predicate[] array = predicates.toArray(new Predicate[0]); return matcher.isAllMatching() ? cb.and(array) : cb.or(array); }
/** * Get the actual type for the probe used. This is usually the given class, but the original class in case of a * CGLIB-generated subclass. * * @return * @see ProxyUtils#getUserClass(Class) */ @SuppressWarnings("unchecked") default Class<T> getProbeType() { return (Class<T>) ProxyUtils.getUserClass(getProbe().getClass()); } }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
/** * This method will create an invocation that had it occurred on a query method would provide sufficient * data for a parsed query method expression to be evaluated * @param descriptor the query descriptor * @param example the example that is used for evaluation * @return the fake method invocation corresponding to the example probe */ private Invocation createInvocation(QueryDescriptor descriptor, Example example) { final List<Object> values = new ArrayList<>(); //since according to http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example //the matcher only supports AND condition, so, we expect to see only one branch final List<List<Parameter>> branches = descriptor.getBranches(); final List<Parameter> parameters = branches.get(0); for (Parameter parameter : parameters) { final String propertyPath = parameter.getPath(); final Object propertyValue = PropertyUtils.getPropertyValue(example.getProbe(), propertyPath); final ExampleMatcher.PropertySpecifier specifier = example.getMatcher().getPropertySpecifiers().getForPath(propertyPath); values.add(specifier == null ? propertyValue : specifier.getPropertyValueTransformer().convert(propertyValue)); } return new ImmutableInvocation(null, values.toArray()); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
@Override public QueryDescriptor extract(RepositoryMetadata repositoryMetadata, RepositoryFactoryConfiguration configuration, Example example) { final OperatorContext operatorContext = configuration.getDescriptionExtractor().getOperatorContext(); final Map<String, Object> values = extractValues(example.getProbe()); final ExampleMatcher matcher = example.getMatcher(); final List<Parameter> parameters = new ArrayList<>();
public String convertExampleToPredicate(final Example<T> example, final Map<String, Object> bindVars) { final StringBuilder predicateBuilder = new StringBuilder(); final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(example.getProbeType()); Assert.isTrue(example.getProbe() != null, "Probe in Example cannot be null"); traversePropertyTree(example, predicateBuilder, bindVars, "", "", persistentEntity, example.getProbe()); return predicateBuilder.toString(); }
@Override public QueryDescriptor extract(RepositoryMetadata repositoryMetadata, RepositoryFactoryConfiguration configuration, Example example) { final OperatorContext operatorContext = configuration.getDescriptionExtractor().getOperatorContext(); final Map<String, Object> values = extractValues(example.getProbe()); final ExampleMatcher matcher = example.getMatcher(); final List<Parameter> parameters = new ArrayList<>();
public String convertExampleToPredicate(final Example<T> example, final Map<String, Object> bindVars) { final StringBuilder predicateBuilder = new StringBuilder(); final ArangoPersistentEntity<?> persistentEntity = context.getPersistentEntity(example.getProbeType()); Assert.isTrue(example.getProbe() != null, "Probe in Example cannot be null"); traversePropertyTree(example, predicateBuilder, bindVars, "", "", persistentEntity, example.getProbe()); return predicateBuilder.toString(); }
break; return ebeanServer.getExpressionFactory().exampleLike(example.getProbe(), example.getMatcher().isIgnoreCaseEnabled(), likeType);
/** * Extract the {@link Predicate} representing the {@link Example}. * * @param root must not be {@literal null}. * @param cb must not be {@literal null}. * @param example must not be {@literal null}. * @return never {@literal null}. */ public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) { Assert.notNull(root, "Root must not be null!"); Assert.notNull(cb, "CriteriaBuilder must not be null!"); Assert.notNull(example, "Example must not be null!"); ExampleMatcher matcher = example.getMatcher(); List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(), example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe())); if (predicates.isEmpty()) { return cb.isTrue(cb.literal(true)); } if (predicates.size() == 1) { return predicates.iterator().next(); } Predicate[] array = predicates.toArray(new Predicate[0]); return matcher.isAllMatching() ? cb.and(array) : cb.or(array); }
break; return ebeanServer.getExpressionFactory().exampleLike(example.getProbe(), example.getMatcher().isIgnoreCaseEnabled(), likeType);
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
/** * Retrieve a mapped {@link RedisOperationChain} to query secondary indexes given {@link Example}. * * @param example must not be {@literal null}. * @return the mapped {@link RedisOperationChain}. */ public RedisOperationChain getMappedExample(Example<?> example) { RedisOperationChain chain = new RedisOperationChain(); ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", example.getProbe(), mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, example.getMatcher().getMatchMode(), chain); return chain; }
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);