Search in sources :

Example 11 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class ExpressionEvaluationIT method compareSqlAndEval.

private void compareSqlAndEval(Expression exp, int expectedCount) {
    // apply exp in SQL
    Ordering ordering = new Ordering("db:ARTIST_ID");
    List<Artist> filteredInSQL = ObjectSelect.query(Artist.class, exp).orderBy(ordering).select(context);
    // apply exp to in-memory collection
    List<Artist> filteredInMemory = exp.filterObjects(ObjectSelect.query(Artist.class).prefetch(Artist.PAINTING_ARRAY.disjoint()).select(context));
    ordering.orderList(filteredInMemory);
    assertEquals(expectedCount, filteredInMemory.size());
    assertEquals(filteredInSQL, filteredInMemory);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Ordering(org.apache.cayenne.query.Ordering)

Example 12 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class QueryDescriptorLoader method addOrdering.

public void addOrdering(String path, String descending, String ignoreCase) {
    if (orderings == null) {
        orderings = new ArrayList<>();
    }
    if (path != null && isBlank(path)) {
        path = null;
    }
    boolean isDescending = "true".equalsIgnoreCase(descending);
    boolean isIgnoringCase = "true".equalsIgnoreCase(ignoreCase);
    SortOrder order;
    if (isDescending) {
        order = isIgnoringCase ? SortOrder.DESCENDING_INSENSITIVE : SortOrder.DESCENDING;
    } else {
        order = isIgnoringCase ? SortOrder.ASCENDING_INSENSITIVE : SortOrder.ASCENDING;
    }
    orderings.add(new Ordering(path, order));
}
Also used : Ordering(org.apache.cayenne.query.Ordering) SortOrder(org.apache.cayenne.query.SortOrder)

Example 13 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class EOModelProcessor method makeEOQueryDescriptor.

protected QueryDescriptor makeEOQueryDescriptor(ObjEntity root, Map plistMap) {
    SelectQueryDescriptor descriptor = QueryDescriptor.selectQueryDescriptor();
    descriptor.setRoot(root);
    descriptor.setDistinct("YES".equalsIgnoreCase((String) plistMap.get("usesDistinct")));
    Object fetchLimit = plistMap.get("fetchLimit");
    if (fetchLimit != null) {
        try {
            if (fetchLimit instanceof Number) {
                descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, String.valueOf(((Number) fetchLimit).intValue()));
            } else if (isNumeric(fetchLimit.toString())) {
                descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit.toString());
            }
        } catch (NumberFormatException nfex) {
        // ignoring...
        }
    }
    // sort orderings
    List<Map<String, String>> orderings = (List<Map<String, String>>) plistMap.get("sortOrderings");
    if (orderings != null && !orderings.isEmpty()) {
        for (Map<String, String> ordering : orderings) {
            boolean asc = !"compareDescending:".equals(ordering.get("selectorName"));
            String key = ordering.get("key");
            if (key != null) {
                descriptor.addOrdering(new Ordering(key, asc ? SortOrder.ASCENDING : SortOrder.DESCENDING));
            }
        }
    }
    // qualifiers
    Map<String, ?> qualifierMap = (Map<String, ?>) plistMap.get("qualifier");
    if (qualifierMap != null && !qualifierMap.isEmpty()) {
        descriptor.setQualifier(EOQuery.EOFetchSpecificationParser.makeQualifier((EOObjEntity) root, qualifierMap));
    }
    // prefetches
    List prefetches = (List) plistMap.get("prefetchingRelationshipKeyPaths");
    if (prefetches != null && !prefetches.isEmpty()) {
        Iterator it = prefetches.iterator();
        while (it.hasNext()) {
            descriptor.addPrefetch((String) it.next());
        }
    }
    // modeler...
    if (plistMap.containsKey("rawRowKeyPaths")) {
        descriptor.setProperty(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY, String.valueOf(true));
    }
    return descriptor;
}
Also used : SelectQueryDescriptor(org.apache.cayenne.map.SelectQueryDescriptor) Ordering(org.apache.cayenne.query.Ordering) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap)

Example 14 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class OptimisticLockingIT method testSuccessLockingOnMixed.

@Test
public void testSuccessLockingOnMixed() throws Exception {
    createLockingOnMixedDataSet();
    SelectQuery<SimpleLockingTestEntity> query = new SelectQuery<>(SimpleLockingTestEntity.class);
    query.addOrdering(new Ordering("db:LOCKING_TEST_ID", SortOrder.ASCENDING));
    List<?> allObjects = context.performQuery(query);
    assertEquals(3, allObjects.size());
    SimpleLockingTestEntity object1 = (SimpleLockingTestEntity) allObjects.get(0);
    SimpleLockingTestEntity object2 = (SimpleLockingTestEntity) allObjects.get(1);
    SimpleLockingTestEntity object3 = (SimpleLockingTestEntity) allObjects.get(2);
    // change description and save... no optimistic lock failure expected...
    object1.setDescription("first update for object1");
    object2.setDescription("first update for object2");
    object3.setName("object3 - new name");
    context.commitChanges();
// TODO: it would be nice to pick inside DataContext to see that 3 batches where
// generated...
// this requires refactoring of ContextCommit.
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) Ordering(org.apache.cayenne.query.Ordering) Test(org.junit.Test)

Example 15 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class OrderingTranslatorIT method testAppendFunctionExpression2.

@Test
public void testAppendFunctionExpression2() throws Exception {
    Ordering o1 = new Ordering(FunctionExpressionFactory.countExp(ExpressionFactory.pathExp("dateOfBirth")), SortOrder.ASCENDING_INSENSITIVE);
    Ordering o2 = new Ordering(FunctionExpressionFactory.sqrtExp("paintingArray.estimatedPrice"), SortOrder.DESCENDING);
    doTestAppendPart("UPPER(COUNT(ta.DATE_OF_BIRTH)), SQRT(ta.ESTIMATED_PRICE) DESC", o1, o2);
}
Also used : Ordering(org.apache.cayenne.query.Ordering) Test(org.junit.Test)

Aggregations

Ordering (org.apache.cayenne.query.Ordering)18 Test (org.junit.Test)10 QueryEvent (org.apache.cayenne.configuration.event.QueryEvent)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Expression (org.apache.cayenne.exp.Expression)1 ExpressionException (org.apache.cayenne.exp.ExpressionException)1 DataMap (org.apache.cayenne.map.DataMap)1 Entity (org.apache.cayenne.map.Entity)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 SelectQueryDescriptor (org.apache.cayenne.map.SelectQueryDescriptor)1 Query (org.apache.cayenne.query.Query)1 SortOrder (org.apache.cayenne.query.SortOrder)1 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1 CayenneMapEntry (org.apache.cayenne.util.CayenneMapEntry)1