Search in sources :

Example 26 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class JDOSQLQueryTest method scalarQueries.

@Test
public void scalarQueries() {
    BooleanExpression filter = product.name.startsWith("A");
    // fetchCount
    assertEquals(10L, sql().from(product).where(filter).fetchCount());
    // countDistinct
    assertEquals(10L, sql().from(product).where(filter).distinct().fetchCount());
    // fetch
    assertEquals(10, sql().from(product).where(filter).select(product.name).fetch().size());
    // fetch with limit
    assertEquals(3, sql().from(product).limit(3).select(product.name).fetch().size());
    // fetch with offset
    // assertEquals(7, sql().from(product).offset(3).fetch(product.name).size());
    // fetch with limit and offset
    assertEquals(3, sql().from(product).offset(3).limit(3).select(product.name).fetch().size());
    // fetch multiple
    for (Tuple row : sql().from(product).select(product.productId, product.name, product.amount).fetch()) {
        assertNotNull(row.get(0, Object.class));
        assertNotNull(row.get(1, Object.class));
        assertNotNull(row.get(2, Object.class));
    }
    // fetchResults
    QueryResults<String> results = sql().from(product).limit(3).select(product.name).fetchResults();
    assertEquals(3, results.getResults().size());
    assertEquals(30L, results.getTotal());
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Tuple(com.querydsl.core.Tuple) Test(org.junit.Test)

Example 27 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class ExpressionTest method test.

@Test
public void test() throws Throwable {
    List<Expression<?>> exprs = new ArrayList<Expression<?>>();
    exprs.add(QAnimalTest_Animal.animal);
    exprs.add(QAnimalTest_Cat.cat);
    exprs.add(QConstructorTest_Category.category);
    exprs.add(QConstructorTest_ClassWithConstructor.classWithConstructor);
    exprs.add(QEntityTest_Entity1.entity1);
    exprs.add(QEntityTest_Entity2.entity2);
    exprs.add(QEntityTest_Entity3.entity3);
    exprs.add(QEmbeddableTest_EntityWithEmbedded.entityWithEmbedded);
    exprs.add(QGenericTest_GenericType.genericType);
    exprs.add(QInterfaceTypeTest_InterfaceType.interfaceType);
    exprs.add(QInterfaceTypeTest_InterfaceType2.interfaceType2);
    exprs.add(QInterfaceTypeTest_InterfaceType3.interfaceType3);
    exprs.add(QInterfaceTypeTest_InterfaceType4.interfaceType4);
    exprs.add(QInterfaceTypeTest_InterfaceType5.interfaceType5);
    exprs.add(QGenericTest_ItemType.itemType);
    exprs.add(QJodaTimeSupportTest_JodaTimeSupport.jodaTimeSupport);
    exprs.add(QQueryInitTest_PEntity.pEntity);
    exprs.add(QQueryInitTest_PEntity2.pEntity2);
    exprs.add(QQueryInitTest_PEntity3.pEntity3);
    exprs.add(QQueryInitTest_PEntity4.pEntity4);
    exprs.add(QQueryTypeTest_QueryTypeEntity.queryTypeEntity);
    exprs.add(QRelationTest_Reference.reference);
    exprs.add(QRelationTest_RelationType.relationType);
    exprs.add(QReservedNamesTest_ReservedNames.reservedNames);
    exprs.add(QSimpleTypesTest_SimpleTypes.simpleTypes);
    exprs.add(ConstantImpl.create("Hello World!"));
    exprs.add(ConstantImpl.create(1000));
    exprs.add(ConstantImpl.create(10L));
    exprs.add(ConstantImpl.create(true));
    exprs.add(ConstantImpl.create(false));
    // all entities
    Set<Expression<?>> toVisit = new HashSet<Expression<?>>(exprs);
    // and all their direct properties
    for (Expression<?> expr : exprs) {
        for (Field field : expr.getClass().getFields()) {
            Object rv = field.get(expr);
            if (rv instanceof Expression) {
                if (rv instanceof StringExpression) {
                    StringExpression str = (StringExpression) rv;
                    toVisit.add(str.toLowerCase());
                    toVisit.add(str.charAt(0));
                    toVisit.add(str.isEmpty());
                } else if (rv instanceof BooleanExpression) {
                    BooleanExpression b = (BooleanExpression) rv;
                    toVisit.add(b.not());
                }
                toVisit.add((Expression<?>) rv);
            }
        }
    }
    Set<String> failures = new TreeSet<String>();
    for (Expression<?> expr : toVisit) {
        for (Method method : expr.getClass().getMethods()) {
            if (method.getName().equals("getParameter")) {
                continue;
            }
            if (method.getName().equals("getArg")) {
                continue;
            }
            if (method.getReturnType() != void.class && !method.getReturnType().isPrimitive()) {
                Class<?>[] types = method.getParameterTypes();
                Object[] args;
                if (types.length == 0) {
                    args = new Object[0];
                } else if (types.length == 1) {
                    if (types[0] == int.class) {
                        args = new Object[] { 1 };
                    } else if (types[0] == boolean.class) {
                        args = new Object[] { Boolean.TRUE };
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                Object rv = method.invoke(expr, args);
                if (method.invoke(expr, args) != rv) {
                    failures.add(expr.getClass().getSimpleName() + "." + method.getName() + " is unstable");
                }
            }
        }
    }
    if (failures.size() > 0) {
        System.err.println("Got " + failures.size() + " failures\n");
    }
    for (String failure : failures) {
        System.err.println(failure);
    }
// assertTrue("Got "+failures.size()+" failures",failures.isEmpty());
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Expression(com.querydsl.core.types.Expression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Test(org.junit.Test)

Example 28 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class SearchQueryTest method list.

@Test
public void list() {
    BooleanExpression filter = user.emailAddress.eq("bob@example.com");
    List<User> list = query().where(filter).fetch();
    assertEquals(1, list.size());
    User u = query().where(filter).fetchOne();
    assertEquals(u, list.get(0));
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Test(org.junit.Test)

Example 29 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class SearchQueryTest method uniqueResult.

@Test
public void uniqueResult() {
    BooleanExpression filter = user.emailAddress.eq("bob@example.com");
    User u = query().where(filter).fetchOne();
    assertNotNull(u);
    assertEquals("bob@example.com", u.getEmailAddress());
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Test(org.junit.Test)

Example 30 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project midpoint by Evolveum.

the class QLookupTableMapping method appendLookupTableRows.

private void appendLookupTableRows(UUID ownerOid, LookupTableType base, GetOperationOptions rowOptions, JdbcSession session) {
    try {
        RelationalValueSearchQuery queryDef = rowOptions == null ? null : rowOptions.getRelationalValueSearchQuery();
        QLookupTableRowMapping rowMapping = QLookupTableRowMapping.get();
        QLookupTableRow alias = rowMapping.defaultAlias();
        BooleanExpression whereQuery = appendConditions(alias, alias.ownerOid.eq(ownerOid), queryDef);
        SQLQuery<MLookupTableRow> query = session.newQuery().from(alias).select(alias).where(whereQuery);
        query = pagingAndOrdering(query, queryDef, rowMapping, alias);
        List<MLookupTableRow> result = query.fetch();
        for (MLookupTableRow r : result) {
            LookupTableRowType lookupRow = new LookupTableRowType().key(r.key);
            if (r.labelOrig != null || r.labelNorm != null) {
                lookupRow.label(PolyString.toPolyStringType(new PolyString(r.labelOrig, r.labelNorm)));
            }
            lookupRow.lastChangeTimestamp(MiscUtil.asXMLGregorianCalendar(r.lastChangeTimestamp));
            lookupRow.value(r.value);
            lookupRow.asPrismContainerValue().setId(r.cid);
            base.getRow().add(lookupRow);
        }
    } catch (QueryException e) {
        throw new SystemException("Unable to fetch nested table rows", e);
    }
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) SystemException(com.evolveum.midpoint.util.exception.SystemException) RelationalValueSearchQuery(com.evolveum.midpoint.schema.RelationalValueSearchQuery) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)

Aggregations

BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)30 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)4 StringPath (com.querydsl.core.types.dsl.StringPath)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 Predicate (com.querydsl.core.types.Predicate)2 Field (java.lang.reflect.Field)2 LinkedList (java.util.LinkedList)2 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)1 RelationalValueSearchQuery (com.evolveum.midpoint.schema.RelationalValueSearchQuery)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 LookupTableRowType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tuple (com.querydsl.core.Tuple)1 Expression (com.querydsl.core.types.Expression)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 ComparableExpressionBase (com.querydsl.core.types.dsl.ComparableExpressionBase)1 EntityPathBase (com.querydsl.core.types.dsl.EntityPathBase)1 StringExpression (com.querydsl.core.types.dsl.StringExpression)1 QAdDescription (de.spring.example.persistence.domain.QAdDescription)1