* @param ownerSM StateManager for the owner object
* @return The SQLStatement
*/
protected SQLStatement getSQLStatementForIterator(ObjectProvider ownerSM)
{
SQLStatement sqlStmt = null;
final ClassLoaderResolver clr = ownerSM.getExecutionContext().getClassLoaderResolver();
final Class keyCls = clr.classForName(elementType);
RDBMSStoreManager storeMgr = (RDBMSStoreManager)this.storeMgr;
SQLTable containerSqlTbl = null;
MapType mapType = getOwnerMemberMetaData().getMap().getMapType();
if (emd != null && emd.getDiscriminatorStrategyForTable() != null &&
emd.getDiscriminatorStrategyForTable() != DiscriminatorStrategy.NONE)
{
// Map<PC, ?> where key has discriminator
if (ClassUtils.isReferenceType(keyCls))
{
// Take the metadata for the first implementation of the reference type
String[] clsNames = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
Class[] cls = new Class[clsNames.length];
for (int j=0; j<clsNames.length; j++)
{
cls[j] = clr.classForName(clsNames[j]);
}
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();
}
containerSqlTbl = sqlStmt.getPrimaryTable();
iterateUsingDiscriminator = true;
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY)
{
// Select key fields
containerSqlTbl = sqlStmt.getPrimaryTable();
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
}
else
{
// MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
// Join to join table and select key fields
JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.innerJoin(sqlStmt.getPrimaryTable(), keyIdMapping,
containerTable, null, elementMapping, null, null);
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
}
}
else
{
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY)
{
// Select of key in key table (allow union of possible key types)
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement();
containerSqlTbl = sqlStmt.getPrimaryTable();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
}
else
{
// MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
if (emd != null)
{
// Select of key table, joining to join table
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement();
JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.innerJoin(sqlStmt.getPrimaryTable(), keyIdMapping,
containerTable, null, elementMapping, null, null);
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), sqlStmt.getPrimaryTable(), emd, 0);
}
else
{
// Select of key in join table
sqlStmt = new SQLStatement(storeMgr, containerTable, null, null);
sqlStmt.setClassLoaderResolver(clr);
containerSqlTbl = sqlStmt.getPrimaryTable();
sqlStmt.select(sqlStmt.getPrimaryTable(), elementMapping, null);
}
}
}
// Apply condition on owner field to filter by owner
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
SQLTable ownerSqlTbl =
SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, containerSqlTbl, ownerMapping);
SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0)
{
// Add parameter occurrence for each union of statement
for (int j=0;j<sqlStmt.getNumberOfUnions()+1;j++)
{
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k=0;k<ownerMapping.getNumberOfDatastoreMappings();k++)
{
paramPositions[k] = inputParamNum++;