Search in sources :

Example 1 with Expression

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

the class MorphiaAnnotationProcessor method createConfiguration.

@Override
protected Configuration createConfiguration(RoundEnvironment roundEnv) {
    Class<? extends Annotation> entities = QueryEntities.class;
    Class<? extends Annotation> entity = Entity.class;
    Class<? extends Annotation> superType = QuerySupertype.class;
    Class<? extends Annotation> embedded = Embedded.class;
    Class<? extends Annotation> skip = Transient.class;
    DefaultConfiguration conf = new DefaultConfiguration(roundEnv, processingEnv.getOptions(), Collections.<String>emptySet(), entities, entity, superType, null, embedded, skip);
    try {
        // Point is an Expression<Double[]>
        @SuppressWarnings("unchecked") Class<? extends Expression<Double[]>> cl = (Class<? extends Expression<Double[]>>) Class.forName("com.querydsl.mongodb.Point");
        conf.addCustomType(Double[].class, cl);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    return conf;
}
Also used : Entity(org.mongodb.morphia.annotations.Entity) DefaultConfiguration(com.querydsl.apt.DefaultConfiguration) QuerySupertype(com.querydsl.core.annotations.QuerySupertype) QueryEntities(com.querydsl.core.annotations.QueryEntities) Expression(com.querydsl.core.types.Expression) Embedded(org.mongodb.morphia.annotations.Embedded) Transient(org.mongodb.morphia.annotations.Transient)

Example 2 with Expression

use of com.querydsl.core.types.Expression 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 Expression

use of com.querydsl.core.types.Expression 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 Expression

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

the class StringOperationsTest method indexOf.

@Test
@SuppressWarnings("rawtypes")
public void indexOf() {
    Path path = QCat.cat.name;
    Expression startIndex = Expressions.constant(0);
    Expression endIndex = Expressions.numberOperation(Integer.class, Ops.INDEX_OF, path, Expressions.constant("x"));
    Expression substr = Expressions.stringOperation(Ops.SUBSTR_2ARGS, path, startIndex, endIndex);
    assertToString("substring(cat.name,1,locate(?1,cat.name)-1 - ?2)", substr);
}
Also used : StringPath(com.querydsl.core.types.dsl.StringPath) Path(com.querydsl.core.types.Path) Expression(com.querydsl.core.types.Expression) Test(org.junit.Test)

Example 5 with Expression

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

the class CoverageTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws IllegalArgumentException, IllegalAccessException {
    // make sure all Operators are covered in expression factory methods
    Set<Operator> usedOperators = new HashSet<Operator>();
    List<Expression<?>> exprs = new ArrayList<Expression<?>>();
    Entity entity = Alias.alias(Entity.class, "entity");
    // numeric
    exprs.addAll(projections.numeric($(entity.getNum()), $(entity.getNum()), 1, false));
    exprs.addAll(matchers.numeric($(entity.getNum()), $(entity.getNum()), 1));
    exprs.addAll(filters.numeric($(entity.getNum()), $(entity.getNum()), 1));
    exprs.addAll(projections.numericCasts($(entity.getNum()), $(entity.getNum()), 1));
    // string
    exprs.addAll(projections.string($(entity.getStr()), $(entity.getStr()), "abc"));
    exprs.addAll(matchers.string($(entity.getStr()), $(entity.getStr()), "abc"));
    exprs.addAll(filters.string($(entity.getStr()), $(entity.getStr()), "abc"));
    // date
    exprs.addAll(projections.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    exprs.addAll(matchers.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    exprs.addAll(filters.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    // dateTime
    exprs.addAll(projections.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    exprs.addAll(matchers.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    exprs.addAll(filters.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    // time
    exprs.addAll(projections.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    exprs.addAll(matchers.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    exprs.addAll(filters.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    // boolean
    exprs.addAll(filters.booleanFilters($(entity.isBool()), $(entity.isBool())));
    // collection
    exprs.addAll(projections.list($(entity.getList()), $(entity.getList()), ""));
    exprs.addAll(filters.list($(entity.getList()), $(entity.getList()), ""));
    // array
    exprs.addAll(projections.array($(entity.getArray()), $(entity.getArray()), ""));
    exprs.addAll(filters.array($(entity.getArray()), $(entity.getArray()), ""));
    // map
    exprs.addAll(projections.map($(entity.getMap()), $(entity.getMap()), "", ""));
    exprs.addAll(filters.map($(entity.getMap()), $(entity.getMap()), "", ""));
    for (Expression<?> e : exprs) {
        if (e instanceof Operation) {
            Operation<?> op = (Operation<?>) e;
            if (op.getArg(0) instanceof Operation) {
                usedOperators.add(((Operation<?>) op.getArg(0)).getOperator());
            } else if (op.getArgs().size() > 1 && op.getArg(1) instanceof Operation) {
                usedOperators.add(((Operation<?>) op.getArg(1)).getOperator());
            }
            usedOperators.add(op.getOperator());
        }
    }
    // missing mappings
    usedOperators.addAll(Arrays.<Operator>asList(Ops.INSTANCE_OF, Ops.ALIAS, Ops.ARRAY_SIZE, Ops.MOD, Ops.STRING_CAST, //            Ops.DELEGATE,
    Ops.WRAPPED, Ops.ORDER, Ops.XOR, Ops.XNOR, Ops.CASE_WHEN, Ops.CASE_ELSE, Ops.CASE_EQ_WHEN, Ops.CASE_EQ_ELSE, Ops.LIST, Ops.SET, Ops.SINGLETON, Ops.COALESCE, // TODO: add support
    Ops.ORDINAL, Ops.MATCHES_IC, // aggregation
    Ops.AggOps.AVG_AGG, Ops.AggOps.MAX_AGG, Ops.AggOps.MIN_AGG, Ops.AggOps.SUM_AGG, Ops.AggOps.COUNT_AGG, Ops.AggOps.COUNT_ALL_AGG, Ops.EXISTS));
    List<Operator> notContained = new ArrayList<Operator>();
    for (Field field : Ops.class.getFields()) {
        if (Operator.class.isAssignableFrom(field.getType())) {
            Operator val = (Operator) field.get(null);
            if (!usedOperators.contains(val)) {
                System.err.println(field.getName() + " was not contained");
                notContained.add(val);
            }
        }
    }
    assertTrue(notContained.size() + " errors in processing, see log for details", notContained.isEmpty());
}
Also used : Operator(com.querydsl.core.types.Operator) Operation(com.querydsl.core.types.Operation) Field(java.lang.reflect.Field) Expression(com.querydsl.core.types.Expression) Test(org.junit.Test)

Aggregations

Expression (com.querydsl.core.types.Expression)6 Test (org.junit.Test)3 FactoryExpression (com.querydsl.core.types.FactoryExpression)2 NativeSQLSerializer (com.querydsl.jpa.NativeSQLSerializer)2 Field (java.lang.reflect.Field)2 DefaultConfiguration (com.querydsl.apt.DefaultConfiguration)1 QueryEntities (com.querydsl.core.annotations.QueryEntities)1 QuerySupertype (com.querydsl.core.annotations.QuerySupertype)1 Operation (com.querydsl.core.types.Operation)1 Operator (com.querydsl.core.types.Operator)1 Path (com.querydsl.core.types.Path)1 BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)1 StringExpression (com.querydsl.core.types.dsl.StringExpression)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 AbstractSQLQuery (com.querydsl.jpa.AbstractSQLQuery)1 FactoryExpressionTransformer (com.querydsl.jpa.FactoryExpressionTransformer)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 Query (javax.persistence.Query)1 org.hibernate (org.hibernate)1