* @param ownerSM StateManager for the owning object
* @return The SQLStatement
*/
protected SQLStatement getSQLStatementForGet(ObjectProvider ownerSM)
{
SQLStatement sqlStmt = null;
final ClassLoaderResolver clr = ownerSM.getExecutionContext().getClassLoaderResolver();
final Class valueCls = clr.classForName(this.valueType);
RDBMSStoreManager storeMgr = (RDBMSStoreManager)this.storeMgr;
if (valuesAreEmbedded || valuesAreSerialised)
{
// Value is stored in join table
sqlStmt = new SQLStatement(storeMgr, mapTable, null, null);
sqlStmt.setClassLoaderResolver(clr);
sqlStmt.select(sqlStmt.getPrimaryTable(), valueMapping, null);
}
else
{
// Value is stored in own table
getMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen =
new UnionStatementGenerator(storeMgr, clr, valueCls, true, null, null, mapTable, null, valueMapping);
stmtGen.setOption(StatementGenerator.OPTION_SELECT_NUCLEUS_TYPE);
getMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.NUC_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement();
// Select the value field(s)
SQLTable valueSqlTbl = sqlStmt.getTable(valueTable, sqlStmt.getPrimaryTable().getGroupName());
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, getMappingDef,
ownerSM.getExecutionContext().getFetchPlan(), valueSqlTbl, vmd, 0);
}
// Apply condition on owner field to filter by owner
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
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);
// Apply condition on key
if (keyMapping instanceof SerialisedMapping)
{
// if the keyMapping contains a BLOB column (or any other column not supported by the database
// as primary key), uses like instead of the operator OP_EQ (=)
// in future do not check if the keyMapping is of ObjectMapping, but use the database
// adapter to check the data types not supported as primary key
// if object mapping (BLOB) use like
SQLExpression keyExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), keyMapping);
SQLExpression keyVal = exprFactory.newLiteralParameter(sqlStmt, keyMapping, null, "KEY");
sqlStmt.whereAnd(new org.datanucleus.store.rdbms.sql.expression.BooleanExpression(keyExpr,
Expression.OP_LIKE, keyVal), true);
}
else
{
SQLExpression keyExpr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(), keyMapping);
SQLExpression keyVal = exprFactory.newLiteralParameter(sqlStmt, keyMapping, null, "KEY");
sqlStmt.whereAnd(keyExpr.eq(keyVal), true);
}
// Input parameter(s) - owner, key
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
StatementMappingIndex keyIdx = new StatementMappingIndex(keyMapping);
if (sqlStmt.getNumberOfUnions() > 0)
{
// Add parameter occurrence for each union of statement
for (int j=0;j<sqlStmt.getNumberOfUnions()+1;j++)
{
int[] ownerPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k=0;k<ownerPositions.length;k++)
{
ownerPositions[k] = inputParamNum++;