Search in sources :

Example 1 with Path

use of com.querydsl.core.types.Path in project spring-data-keyvalue by spring-projects.

the class KeyValueQuerydslUtils method buildOrderPropertyPathFrom.

/**
 * Creates an {@link Expression} for the given {@link Order} property.
 *
 * @param order must not be {@literal null}.
 * @param builder must not be {@literal null}.
 * @return
 */
private static Expression<?> buildOrderPropertyPathFrom(Order order, PathBuilder<?> builder) {
    Assert.notNull(order, "Order must not be null!");
    Assert.notNull(builder, "Builder must not be null!");
    PropertyPath path = PropertyPath.from(order.getProperty(), builder.getType());
    Expression<?> sortPropertyExpression = builder;
    while (path != null) {
        if (!path.hasNext() && order.isIgnoreCase()) {
            // if order is ignore-case we have to treat the last path segment as a String.
            sortPropertyExpression = Expressions.stringPath((Path<?>) sortPropertyExpression, path.getSegment()).lower();
        } else {
            sortPropertyExpression = 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) PropertyPath(org.springframework.data.mapping.PropertyPath)

Example 2 with Path

use of com.querydsl.core.types.Path in project spring-data-commons by spring-projects.

the class PropertyPathInformation method reifyPath.

private static Path<?> reifyPath(EntityPathResolver resolver, PropertyPath path, Optional<Path<?>> base) {
    Optional<Path<?>> map = // 
    base.filter(it -> it instanceof CollectionPathBase).map(CollectionPathBase.class::cast).map(// 
    CollectionPathBase::any).map(// 
    Path.class::cast).map(it -> reifyPath(resolver, path, Optional.of(it)));
    return map.orElseGet(() -> {
        Path<?> entityPath = base.orElseGet(() -> resolver.createPath(path.getOwningType().getType()));
        Field field = org.springframework.data.util.ReflectionUtils.findRequiredField(entityPath.getClass(), path.getSegment());
        Object value = ReflectionUtils.getField(field, entityPath);
        PropertyPath next = path.next();
        if (next != null) {
            return reifyPath(resolver, next, Optional.of((Path<?>) value));
        }
        return (Path<?>) value;
    });
}
Also used : Path(com.querydsl.core.types.Path) PropertyPath(org.springframework.data.mapping.PropertyPath) Field(java.lang.reflect.Field) PropertyPath(org.springframework.data.mapping.PropertyPath) CollectionPathBase(com.querydsl.core.types.dsl.CollectionPathBase)

Example 3 with Path

use of com.querydsl.core.types.Path in project querydsl by querydsl.

the class AbstractHibernateQuery method createQuery.

private Query createQuery(@Nullable QueryModifiers modifiers, boolean forCount) {
    JPQLSerializer serializer = serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString);
    Query query = session.createQuery(queryString);
    HibernateUtil.setConstants(query, serializer.getConstants(), getMetadata().getParams());
    if (fetchSize > 0) {
        query.setFetchSize(fetchSize);
    }
    if (timeout > 0) {
        query.setTimeout(timeout);
    }
    if (cacheable != null) {
        query.setCacheable(cacheable);
    }
    if (cacheRegion != null) {
        query.setCacheRegion(cacheRegion);
    }
    if (comment != null) {
        query.setComment(comment);
    }
    if (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    if (flushMode != null) {
        query.setFlushMode(flushMode);
    }
    if (modifiers != null && modifiers.isRestricting()) {
        Integer limit = modifiers.getLimitAsInteger();
        Integer offset = modifiers.getOffsetAsInteger();
        if (limit != null) {
            query.setMaxResults(limit);
        }
        if (offset != null) {
            query.setFirstResult(offset);
        }
    }
    // set transformer, if necessary
    Expression<?> projection = getMetadata().getProjection();
    if (!forCount && projection instanceof FactoryExpression) {
        query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection));
    }
    return query;
}
Also used : Path(com.querydsl.core.types.Path) FactoryExpression(com.querydsl.core.types.FactoryExpression) Query(org.hibernate.query.Query) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Path

use of com.querydsl.core.types.Path in project querydsl by querydsl.

the class TypesBase method create_tables.

@Test
public void create_tables() {
    Map<Class<?>, Object> instances = new LinkedHashMap<>();
    instances.put(BigInteger.class, BigInteger.valueOf(1));
    instances.put(Long.class, 1L);
    instances.put(Integer.class, 1);
    instances.put(Short.class, (short) 1);
    instances.put(Byte.class, (byte) 1);
    instances.put(BigDecimal.class, BigDecimal.valueOf(1.0));
    instances.put(Double.class, 1.0);
    instances.put(Float.class, 1.0f);
    instances.put(Boolean.class, Boolean.TRUE);
    instances.put(Character.class, 'a');
    instances.put(String.class, "ABC");
    for (Map.Entry<Class<?>, Object> entry : instances.entrySet()) {
        String tableName = "test_" + entry.getKey().getSimpleName();
        new DropTableClause(connection, configuration, tableName).execute();
        CreateTableClause c = new CreateTableClause(connection, configuration, tableName).column("col", entry.getKey());
        if (entry.getKey().equals(String.class)) {
            c.size(256);
        }
        c.execute();
        RelationalPath<Object> entityPath = new RelationalPathBase<Object>(Object.class, tableName, "PUBLIC", tableName);
        Path<?> columnPath = Expressions.path(entry.getKey(), entityPath, "col");
        insert(entityPath).set((Path) columnPath, entry.getValue()).execute();
        new DropTableClause(connection, configuration, tableName).execute();
    }
}
Also used : Path(com.querydsl.core.types.Path) DropTableClause(com.querydsl.sql.ddl.DropTableClause) LinkedHashMap(java.util.LinkedHashMap) CreateTableClause(com.querydsl.sql.ddl.CreateTableClause) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Path

use of com.querydsl.core.types.Path in project querydsl by querydsl.

the class CollUpdateClause method execute.

@Override
public long execute() {
    int rv = 0;
    for (T match : query.fetch()) {
        BeanMap beanMap = new BeanMap(match);
        for (Map.Entry<Path<?>, Object> entry : paths.entrySet()) {
            // TODO : support deep updates as well
            String propertyName = entry.getKey().getMetadata().getName();
            beanMap.put(propertyName, entry.getValue());
        }
        rv++;
    }
    return rv;
}
Also used : Path(com.querydsl.core.types.Path) BeanMap(com.querydsl.core.util.BeanMap) BeanMap(com.querydsl.core.util.BeanMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Path (com.querydsl.core.types.Path)20 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Test (org.junit.Test)5 EntityPath (com.querydsl.core.types.EntityPath)4 Expression (com.querydsl.core.types.Expression)4 LinkedHashMap (java.util.LinkedHashMap)4 Query (org.hibernate.query.Query)4 JPQLSerializer (com.querydsl.jpa.JPQLSerializer)3 Field (java.lang.reflect.Field)3 LockMode (org.hibernate.LockMode)3 JoinExpression (com.querydsl.core.JoinExpression)2 QueryException (com.querydsl.core.QueryException)2 QueryMetadata (com.querydsl.core.QueryMetadata)2 ExpressionUtils (com.querydsl.core.types.ExpressionUtils)2 FactoryExpression (com.querydsl.core.types.FactoryExpression)2 Predicate (com.querydsl.core.types.Predicate)2 NumberPath (com.querydsl.core.types.dsl.NumberPath)2 StringPath (com.querydsl.core.types.dsl.StringPath)2 RelationalPath (com.querydsl.sql.RelationalPath)2