* @throws PersistenceException On error
*/
public Iterator runQuery(final PersistenceQuery query)
throws PersistenceException
{
TransactionGuard tg = new TransactionGuard(this);
try
{
Expression exp = null;
for (Iterator it = query.getCriterions(); it.hasNext();)
{
PersistenceCriterion criterion = (PersistenceCriterion) it.next();
String property = criterion.getProperty();
String operator = criterion.getOperator();
Object value = criterion.getOperand();
if (PersistenceCriterion.OPERATOR_EQ.equals(operator))
{
Expression newExp1 = ExpressionFactory.matchExp(property, value);
Expression newExp2 = ExpressionFactory.matchExp(property, null);
Expression newExp = newExp1.orExp(newExp2);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_EQ_OR_NULL.equals(operator))
{
Expression newExp = ExpressionFactory.matchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_NEQ.equals(operator))
{
Expression newExp = ExpressionFactory.noMatchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_GT.equals(operator))
{
Expression newExp = ExpressionFactory.noMatchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_GTE.equals(operator))
{
Expression newExp = ExpressionFactory.noMatchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_LT.equals(operator))
{
Expression newExp = ExpressionFactory.noMatchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_LTE.equals(operator))
{
Expression newExp = ExpressionFactory.noMatchExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_LIKE.equals(operator))
{
Expression newExp = ExpressionFactory.likeExp(property, value);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_NULL.equals(operator))
{
Expression newExp = ExpressionFactory.matchExp(property, null);
exp = conjugate(newExp, exp);
}
else if (PersistenceCriterion.OPERATOR_ALIAS.equals(operator))
throw new PersistenceException("'alias' operation not supproted by Cayenne persistence criterion.");
}
Class beanClass = determineEntityClass(query.getObjectClass ());
SelectQuery cq = new SelectQuery(beanClass, exp);
if (query.getMaxResults() > 0)
{
cq.setFetchLimit(query.getMaxResults());
}
for (Iterator it = query.getOrderings(); it.hasNext();)
{
PersistenceOrdering ordering = (PersistenceOrdering) it.next();
cq.addOrdering(ordering.getPropertyName(), ordering.isAscending());
}
// Run query and wrap result list into a collection that calls onLoad for each element that is being accessed.
List result = getDataContext().performQuery(cq);
return new CayenneIterator(result.iterator());
}
catch (Exception e)
{
tg.doCatch();
throw createLoggedException(e);
}
finally
{
tg.doFinally();
}
}