MetaDataManager mmgr = storeMgr.getMetaDataManager();
AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr, mmgr);
AbstractClassMetaData valCmd = mmd.getMap().getValueClassMetaData(clr, mmgr);
MapTable joinTbl = (MapTable)storeMgr.getDatastoreContainerObject(mmd);
SQLStatement subStmt = null;
if (mmd.getMap().getMapType() == MapType.MAP_TYPE_JOIN)
{
// JoinTable Map
subStmt = new SQLStatement(stmt, storeMgr, joinTbl, null, null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);
// Restrict to collection owner
JavaTypeMapping ownerMapping = ((JoinTable)joinTbl).getOwnerMapping();
SQLExpression ownerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, mapExpr.getSQLTable(),
mapExpr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
joinTbl.getValueMapping());
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value TODO Add join to valueTbl if present
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
SQLExpression keyIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
joinTbl.getKeyMapping());
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key TODO Add join to keyTbl if present
subStmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
else if (mmd.getMap().getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE)
{
// Key stored in value table
DatastoreClass valTbl = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
AbstractMemberMetaData valKeyMmd =
valCmd.getMetaDataForMember(mmd.getKeyMetaData().getMappedBy());
subStmt = new SQLStatement(stmt, storeMgr, valTbl, null, null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);
// Restrict to map owner (on value table)
JavaTypeMapping ownerMapping = null;
if (mmd.getMappedBy() != null)
{
ownerMapping = valTbl.getMemberMapping(valCmd.getMetaDataForMember(mmd.getMappedBy()));
}
else
{
ownerMapping = valTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
}
SQLExpression ownerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, mapExpr.getSQLTable(),
mapExpr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
valTbl.getIdMapping());
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
SQLExpression keyIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
valTbl.getMemberMapping(valKeyMmd));
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key TODO Add join to keyTbl if present
subStmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
else if (mmd.getMap().getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY)
{
DatastoreClass keyTbl = storeMgr.getDatastoreClass(mmd.getMap().getKeyType(), clr);
JavaTypeMapping ownerMapping = null;
if (mmd.getMappedBy() != null)
{
ownerMapping = keyTbl.getMemberMapping(keyCmd.getMetaDataForMember(mmd.getMappedBy()));
}
else
{
ownerMapping = keyTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
}
AbstractMemberMetaData keyValMmd =
keyCmd.getMetaDataForMember(mmd.getValueMetaData().getMappedBy());
subStmt = new SQLStatement(stmt, storeMgr, keyTbl, null, null);
subStmt.setClassLoaderResolver(clr);
JavaTypeMapping oneMapping = storeMgr.getMappingManager().getMapping(Integer.class);
subStmt.select(exprFactory.newLiteral(subStmt, oneMapping, 1), null);
// Restrict to map owner (on key table)
SQLExpression ownerExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, mapExpr.getSQLTable(),
mapExpr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
keyTbl.getMemberMapping(keyValMmd));
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value TODO Add join to valTbl if present
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
JavaTypeMapping keyMapping = keyTbl.getIdMapping();
SQLExpression keyIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), keyMapping);
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key
subStmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
return new BooleanSubqueryExpression(stmt, "EXISTS", subStmt);
}