}
}
}
// Apply condition on join-table owner field to filter by owner
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression ownerVal = ownerMapping.newLiteral(stmt, ownerSM.getObject());
stmt.andCondition(ownerExpr.eq(ownerVal), true);
// Apply condition on distinguisher field to filter by distinguisher (when present)
if (relationDiscriminatorMapping != null)
{
ScalarExpression distinguisherExpr = relationDiscriminatorMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression distinguisherVal = relationDiscriminatorMapping.newLiteral(stmt, relationDiscriminatorValue);
stmt.andCondition(distinguisherExpr.eq(distinguisherVal), true);
}
// Add restriction(s) on index values (where required)
if (indexedList)
{
// "Indexed List" so allow restriction on returned indexes
boolean returning_range = false;
if (start_index == -1 && end_index == -1)
{
// Add no more constraints. We want all elements.
returning_range = true;
}
else if (start_index == end_index && start_index >= 0)
{
// Apply condition on join-table owner field to filter by index
ScalarExpression indexExpr = orderMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression indexVal = orderMapping.newLiteral(stmt, new Integer(start_index));
stmt.andCondition(indexExpr.eq(indexVal), true);
}
else
{
returning_range = true;
// Apply filter to retrieve indexes in the range
// a). start_index (inclusive) constraint
if (start_index >= 0)
{
// Apply condition on join-table index field to filter by index
ScalarExpression indexExpr = orderMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression indexVal = orderMapping.newLiteral(stmt, new Integer(start_index));
stmt.andCondition(indexExpr.gteq(indexVal), true);
}
// b). end_index (exclusive) constraint
if (end_index >= 0)
{
// Apply condition on join-table index field to filter by index
ScalarExpression indexExpr = orderMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression indexVal = orderMapping.newLiteral(stmt, new Integer(end_index));
stmt.andCondition(indexExpr.lt(indexVal), true);
}
}
if (returning_range)
{
// If returning a range, add ordering by index
ScalarExpression exprIndex[] = new ScalarExpression[orderMapping.getNumberOfDatastoreFields()];
boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreFields()];
exprIndex = orderMapping.newScalarExpression(stmt,stmt.getMainTableExpression()).getExpressionList().toArray();
stmt.setOrdering(exprIndex, descendingOrder);
}
}
else
{
// Apply ordering defined by <order-by>
DatastoreClass elementTbl = elementInfo[0].getDatastoreClass();
FieldOrder[] orderComponents = ownerMemberMetaData.getOrderMetaData().getFieldOrders();
for (int i=0;i<orderComponents.length;i++)
{
String fieldName = orderComponents[i].getFieldName();
JavaTypeMapping fieldMapping = elementTbl.getFieldMapping(elementInfo[0].getAbstractClassMetaData().getMetaDataForMember(fieldName));
ScalarExpression exprIndex[] = new ScalarExpression[fieldMapping.getNumberOfDatastoreFields()];
boolean descendingOrder[] = new boolean[fieldMapping.getNumberOfDatastoreFields()];
for (int j=0;j<descendingOrder.length;j++)
{
descendingOrder[j] = !orderComponents[i].isForward();
}