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);
}
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));
}
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;
}
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.
}
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);
}
Aggregations