Search in sources :

Example 6 with PropertyPath

use of org.springframework.data.mapping.PropertyPath in project spring-data-couchbase by spring-projects.

the class CouchbasePartTree method maybeInitDistinctFields.

private void maybeInitDistinctFields(String methodName, Class<?> domainType) {
    if (isDistinct()) {
        Matcher grp = DISTINCT_TEMPLATE.matcher(methodName);
        if (grp.matches()) {
            String grp3 = grp.group(3);
            String[] names = grp.group(3).split("And");
            int parameterCount = names.length;
            distinctFields = new String[names.length];
            for (int i = 0; i < parameterCount; ++i) {
                Part.Type type = Part.Type.fromProperty(names[i]);
                PropertyPath path = PropertyPath.from(type.extractProperty(names[i]), domainType);
                distinctFields[i] = path.toDotPath();
            }
        } else {
            distinctFields = new String[0];
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) Part(org.springframework.data.repository.query.parser.Part) PropertyPath(org.springframework.data.mapping.PropertyPath)

Example 7 with PropertyPath

use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.

the class QueryUtilsIntegrationTests method reusesExistingJoinForExpression.

// DATAJPA-403
@Test
void reusesExistingJoinForExpression() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<User> query = builder.createQuery(User.class);
    Root<User> from = query.from(User.class);
    PropertyPath managerFirstname = PropertyPath.from("manager.firstname", User.class);
    PropertyPath managerLastname = PropertyPath.from("manager.lastname", User.class);
    QueryUtils.toExpressionRecursively(from, managerLastname);
    QueryUtils.toExpressionRecursively(from, managerFirstname);
    assertThat(from.getJoins()).hasSize(1);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(org.springframework.data.jpa.domain.sample.User) PropertyPath(org.springframework.data.mapping.PropertyPath) Test(org.junit.jupiter.api.Test)

Example 8 with PropertyPath

use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.

the class Querydsl method buildOrderPropertyPathFrom.

/**
 * Creates an {@link Expression} for the given {@link Order} property.
 *
 * @param order must not be {@literal null}.
 * @return
 */
private Expression<?> buildOrderPropertyPathFrom(Order order) {
    Assert.notNull(order, "Order must not be null!");
    PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType());
    Expression<?> sortPropertyExpression = builder;
    while (path != null) {
        sortPropertyExpression = // 
        !path.hasNext() && order.isIgnoreCase() && String.class.equals(path.getType()) ? // 
        Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower() : Expressions.path(path.getType(), (Path<?>) sortPropertyExpression, path.getSegment());
        path = path.next();
    }
    return sortPropertyExpression;
}
Also used : Path(com.querydsl.core.types.Path) PropertyPath(org.springframework.data.mapping.PropertyPath) EntityPath(com.querydsl.core.types.EntityPath) PropertyPath(org.springframework.data.mapping.PropertyPath)

Example 9 with PropertyPath

use of org.springframework.data.mapping.PropertyPath in project spring-data-jpa by spring-projects.

the class QueryUtils method toExpressionRecursively.

/**
 * Creates an expression with proper inner and left joins by recursively navigating the path
 *
 * @param from the {@link From}
 * @param property the property path
 * @param isForSelection is the property navigated for the selection or ordering part of the query?
 * @param hasRequiredOuterJoin has a parent already required an outer join?
 * @param <T> the type of the expression
 * @return the expression
 */
@SuppressWarnings("unchecked")
static <T> Expression<T> toExpressionRecursively(From<?, ?> from, PropertyPath property, boolean isForSelection, boolean hasRequiredOuterJoin) {
    String segment = property.getSegment();
    boolean isLeafProperty = !property.hasNext();
    boolean requiresOuterJoin = requiresOuterJoin(from, property, isForSelection, hasRequiredOuterJoin);
    // if it does not require an outer join and is a leaf, simply get the segment
    if (!requiresOuterJoin && isLeafProperty) {
        return from.get(segment);
    }
    // get or create the join
    JoinType joinType = requiresOuterJoin ? JoinType.LEFT : JoinType.INNER;
    Join<?, ?> join = getOrCreateJoin(from, segment, joinType);
    // if it's a leaf, return the join
    if (isLeafProperty) {
        return (Expression<T>) join;
    }
    PropertyPath nextProperty = Objects.requireNonNull(property.next(), "An element of the property path is null!");
    // recurse with the next property
    return toExpressionRecursively(join, nextProperty, isForSelection, requiresOuterJoin);
}
Also used : Expression(javax.persistence.criteria.Expression) PropertyPath(org.springframework.data.mapping.PropertyPath) JoinType(javax.persistence.criteria.JoinType)

Example 10 with PropertyPath

use of org.springframework.data.mapping.PropertyPath in project redis-om-spring by redis.

the class RediSearchQuery method processPartTree.

private void processPartTree(PartTree pt) {
    pt.stream().forEach(orPart -> {
        List<Pair<String, QueryClause>> orPartParts = new ArrayList<Pair<String, QueryClause>>();
        orPart.iterator().forEachRemaining(part -> {
            PropertyPath propertyPath = part.getProperty();
            List<PropertyPath> path = StreamSupport.stream(propertyPath.spliterator(), false).collect(Collectors.toList());
            orPartParts.addAll(extractQueryFields(domainType, part, path));
        });
        queryOrParts.add(orPartParts);
    });
}
Also used : ArrayList(java.util.ArrayList) PropertyPath(org.springframework.data.mapping.PropertyPath) ToString(lombok.ToString) QueryClause(com.redis.om.spring.repository.query.clause.QueryClause) Pair(org.springframework.data.util.Pair)

Aggregations

PropertyPath (org.springframework.data.mapping.PropertyPath)24 ArrayList (java.util.ArrayList)6 Test (org.junit.jupiter.api.Test)4 Path (com.querydsl.core.types.Path)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 HashMap (java.util.HashMap)3 QueryClause (com.redis.om.spring.repository.query.clause.QueryClause)2 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)2 Neo4jPersistentProperty (org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)2 Person (org.springframework.data.neo4j.integration.shared.common.Person)2 Neo4jIntegrationTest (org.springframework.data.neo4j.test.Neo4jIntegrationTest)2 ProjectionInformation (org.springframework.data.projection.ProjectionInformation)2 Pair (org.springframework.data.util.Pair)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 DBRef (com.mongodb.DBRef)1 EntityPath (com.querydsl.core.types.EntityPath)1 CollectionPathBase (com.querydsl.core.types.dsl.CollectionPathBase)1 SelectedField (graphql.schema.SelectedField)1 Field (java.lang.reflect.Field)1