}
SQLStatement sqlStmt = null;
RDBMSStoreManager storeMgr = (RDBMSStoreManager)this.storeMgr;
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
final ClassLoaderResolver clr = ownerSM.getExecutionContext().getClassLoaderResolver();
iteratorMappingDef = new StatementClassMapping();
if (elementInfo[0].getDatastoreClass().getDiscriminatorMetaData() != null &&
elementInfo[0].getDatastoreClass().getDiscriminatorMetaData().getStrategy() != DiscriminatorStrategy.NONE)
{
// TODO Only caters for one elementInfo, but with subclass-table we can have multiple
String elementType = ownerMemberMetaData.getCollection().getElementType();
if (ClassUtils.isReferenceType(clr.classForName(elementType)))
{
String[] clsNames =
storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
Class[] cls = new Class[clsNames.length];
for (int i=0; i<clsNames.length; i++)
{
cls[i] = clr.classForName(clsNames[i]);
}
sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr, cls, true, null, null).getStatement();
}
else
{
sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr,
clr.classForName(elementInfo[0].getClassName()), true, null, null).getStatement();
}
iterateUsingDiscriminator = true;
// Select the required fields (of the element class)
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
}
else
{
for (int i=0;i<elementInfo.length;i++)
{
final Class elementCls = clr.classForName(this.elementInfo[i].getClassName());
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, elementCls, true, null, null);
stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
SQLStatement subStmt = stmtGen.getStatement();
// Select the required fields (of the element class)
if (sqlStmt == null)
{
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(subStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), subStmt.getPrimaryTable(), emd, 0);
}
else
{
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(subStmt, null,
ownerSM.getExecutionContext().getFetchPlan(), subStmt.getPrimaryTable(), emd, 0);
}
if (sqlStmt == null)
{
sqlStmt = subStmt;
}
else
{
sqlStmt.union(subStmt);
}
}
}
// Apply condition to filter by owner
SQLTable ownerSqlTbl =
SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
if (relationDiscriminatorMapping != null)
{
// Apply condition on distinguisher field to filter by distinguisher (when present)
SQLTable distSqlTbl =
SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), relationDiscriminatorMapping);
SQLExpression distExpr = exprFactory.newExpression(sqlStmt, distSqlTbl, relationDiscriminatorMapping);
SQLExpression distVal = exprFactory.newLiteral(sqlStmt, relationDiscriminatorMapping, relationDiscriminatorValue);
sqlStmt.whereAnd(distExpr.eq(distVal), true);
}
if (orderMapping != null)
{
// Order by the ordering column, when present
SQLTable orderSqlTbl =
SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), orderMapping);
SQLExpression[] orderExprs = new SQLExpression[orderMapping.getNumberOfDatastoreMappings()];
boolean descendingOrder[] = new boolean[orderMapping.getNumberOfDatastoreMappings()];
orderExprs[0] = exprFactory.newExpression(sqlStmt, orderSqlTbl, orderMapping);
sqlStmt.setOrdering(orderExprs, descendingOrder);
}
// Input parameter(s) - the owner
int inputParamNum = 1;