Search in sources :

Example 1 with FactoryExpression

use of com.querydsl.core.types.FactoryExpression 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, serializer.getConstantToLabel());
    Query query = session.createQuery(queryString);
    HibernateUtil.setConstants(query, serializer.getConstantToLabel(), 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FactoryExpression

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

the class AbstractHibernateSQLQuery method createQuery.

private Query createQuery(boolean forCount) {
    NativeSQLSerializer serializer = (NativeSQLSerializer) serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString, serializer.getConstantToLabel());
    org.hibernate.SQLQuery query = session.createSQLQuery(queryString);
    // set constants
    HibernateUtil.setConstants(query, serializer.getConstantToLabel(), queryMixin.getMetadata().getParams());
    if (!forCount) {
        ListMultimap<Expression<?>, String> aliases = serializer.getAliases();
        Set<String> used = Sets.newHashSet();
        // set entity paths
        Expression<?> projection = queryMixin.getMetadata().getProjection();
        if (projection instanceof FactoryExpression) {
            for (Expression<?> expr : ((FactoryExpression<?>) projection).getArgs()) {
                if (isEntityExpression(expr)) {
                    query.addEntity(extractEntityExpression(expr).toString(), expr.getType());
                } else if (aliases.containsKey(expr)) {
                    for (String scalar : aliases.get(expr)) {
                        if (!used.contains(scalar)) {
                            query.addScalar(scalar);
                            used.add(scalar);
                            break;
                        }
                    }
                }
            }
        } else if (isEntityExpression(projection)) {
            query.addEntity(extractEntityExpression(projection).toString(), projection.getType());
        } else if (aliases.containsKey(projection)) {
            for (String scalar : aliases.get(projection)) {
                if (!used.contains(scalar)) {
                    query.addScalar(scalar);
                    used.add(scalar);
                    break;
                }
            }
        }
        // set result transformer, if projection is a FactoryExpression instance
        if (projection instanceof FactoryExpression) {
            query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection));
        }
    }
    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 (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    return query;
}
Also used : FactoryExpression(com.querydsl.core.types.FactoryExpression) FactoryExpression(com.querydsl.core.types.FactoryExpression) Expression(com.querydsl.core.types.Expression) FactoryExpressionTransformer(com.querydsl.jpa.FactoryExpressionTransformer) NativeSQLSerializer(com.querydsl.jpa.NativeSQLSerializer) org.hibernate(org.hibernate)

Example 3 with FactoryExpression

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

the class AbstractJPASQLQuery method createQuery.

private Query createQuery(boolean forCount) {
    NativeSQLSerializer serializer = (NativeSQLSerializer) serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString, serializer.getConstantToLabel());
    Expression<?> projection = queryMixin.getMetadata().getProjection();
    Query query;
    if (!FactoryExpression.class.isAssignableFrom(projection.getClass()) && isEntityExpression(projection)) {
        if (queryHandler.createNativeQueryTyped()) {
            query = entityManager.createNativeQuery(queryString, projection.getType());
        } else {
            query = entityManager.createNativeQuery(queryString);
        }
    } else {
        query = entityManager.createNativeQuery(queryString);
    }
    if (!forCount) {
        ListMultimap<Expression<?>, String> aliases = serializer.getAliases();
        Set<String> used = Sets.newHashSet();
        if (projection instanceof FactoryExpression) {
            for (Expression<?> expr : ((FactoryExpression<?>) projection).getArgs()) {
                if (isEntityExpression(expr)) {
                    queryHandler.addEntity(query, extractEntityExpression(expr).toString(), expr.getType());
                } else if (aliases.containsKey(expr)) {
                    for (String scalar : aliases.get(expr)) {
                        if (!used.contains(scalar)) {
                            queryHandler.addScalar(query, scalar, expr.getType());
                            used.add(scalar);
                            break;
                        }
                    }
                }
            }
        } else if (isEntityExpression(projection)) {
            queryHandler.addEntity(query, extractEntityExpression(projection).toString(), projection.getType());
        } else if (aliases.containsKey(projection)) {
            for (String scalar : aliases.get(projection)) {
                if (!used.contains(scalar)) {
                    queryHandler.addScalar(query, scalar, projection.getType());
                    used.add(scalar);
                    break;
                }
            }
        }
    }
    if (lockMode != null) {
        query.setLockMode(lockMode);
    }
    if (flushMode != null) {
        query.setFlushMode(flushMode);
    }
    for (Map.Entry<String, Object> entry : hints.entries()) {
        query.setHint(entry.getKey(), entry.getValue());
    }
    // set constants
    JPAUtil.setConstants(query, serializer.getConstantToLabel(), queryMixin.getMetadata().getParams());
    // necessary when query is reused
    this.projection = null;
    if (projection instanceof FactoryExpression) {
        if (!queryHandler.transform(query, (FactoryExpression<?>) projection)) {
            this.projection = (FactoryExpression<?>) projection;
        }
    }
    return query;
}
Also used : FactoryExpression(com.querydsl.core.types.FactoryExpression) AbstractSQLQuery(com.querydsl.jpa.AbstractSQLQuery) Query(javax.persistence.Query) FactoryExpression(com.querydsl.core.types.FactoryExpression) Expression(com.querydsl.core.types.Expression) NativeSQLSerializer(com.querydsl.jpa.NativeSQLSerializer) Map(java.util.Map)

Example 4 with FactoryExpression

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

the class AbstractJPAQuery method createQuery.

private Query createQuery(@Nullable QueryModifiers modifiers, boolean forCount) {
    JPQLSerializer serializer = serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString, serializer.getConstantToLabel());
    Query query = entityManager.createQuery(queryString);
    JPAUtil.setConstants(query, serializer.getConstantToLabel(), getMetadata().getParams());
    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);
        }
    }
    if (lockMode != null) {
        query.setLockMode(lockMode);
    }
    if (flushMode != null) {
        query.setFlushMode(flushMode);
    }
    for (Map.Entry<String, Object> entry : hints.entries()) {
        query.setHint(entry.getKey(), entry.getValue());
    }
    // set transformer, if necessary and possible
    Expression<?> projection = getMetadata().getProjection();
    // necessary when query is reused
    this.projection = null;
    if (!forCount && projection instanceof FactoryExpression) {
        if (!queryHandler.transform(query, (FactoryExpression<?>) projection)) {
            this.projection = (FactoryExpression) projection;
        }
    }
    return query;
}
Also used : FactoryExpression(com.querydsl.core.types.FactoryExpression) Query(javax.persistence.Query) JPQLSerializer(com.querydsl.jpa.JPQLSerializer) Map(java.util.Map)

Example 5 with FactoryExpression

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

the class AbstractSQLQuery method createQuery.

private Query createQuery(boolean forCount) {
    SQLSerializer serializer = new SQLSerializer(configuration);
    if (union != null) {
        serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll);
    } else {
        serializer.serialize(queryMixin.getMetadata(), forCount);
    }
    // create Query
    if (logger.isDebugEnabled()) {
        logger.debug(serializer.toString());
    }
    Query query = persistenceManager.newQuery("javax.jdo.query.SQL", serializer.toString());
    orderedConstants = serializer.getConstants();
    queries.add(query);
    if (!forCount) {
        Expression<?> projection = queryMixin.getMetadata().getProjection();
        if (projection instanceof FactoryExpression) {
            this.projection = (FactoryExpression<?>) projection;
        }
    } else {
        query.setResultClass(Long.class);
    }
    return query;
}
Also used : FactoryExpression(com.querydsl.core.types.FactoryExpression) SQLSerializer(com.querydsl.sql.SQLSerializer) ProjectableSQLQuery(com.querydsl.sql.ProjectableSQLQuery) SQLQuery(com.querydsl.sql.SQLQuery) Query(javax.jdo.Query)

Aggregations

FactoryExpression (com.querydsl.core.types.FactoryExpression)5 Map (java.util.Map)3 Expression (com.querydsl.core.types.Expression)2 NativeSQLSerializer (com.querydsl.jpa.NativeSQLSerializer)2 Query (javax.persistence.Query)2 Path (com.querydsl.core.types.Path)1 AbstractSQLQuery (com.querydsl.jpa.AbstractSQLQuery)1 FactoryExpressionTransformer (com.querydsl.jpa.FactoryExpressionTransformer)1 JPQLSerializer (com.querydsl.jpa.JPQLSerializer)1 ProjectableSQLQuery (com.querydsl.sql.ProjectableSQLQuery)1 SQLQuery (com.querydsl.sql.SQLQuery)1 SQLSerializer (com.querydsl.sql.SQLSerializer)1 HashMap (java.util.HashMap)1 Query (javax.jdo.Query)1 org.hibernate (org.hibernate)1 Query (org.hibernate.Query)1