RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
MetaDataManager mmgr = storeMgr.getMetaDataManager();
AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
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
if (valCmd == null)
{
// Map<?, Non-PC>
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 map 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);
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
valExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), joinTbl.getValueMapping());
stmt.getQueryGenerator().bindVariable(varName, valCmd, valExpr.getSQLTable(),
valExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
joinTbl.getValueMapping());
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
else
{
// Map<?, PC>
DatastoreClass valTbl = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
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);
// Join to join table
SQLTable joinSqlTbl = subStmt.innerJoin(subStmt.getPrimaryTable(), valTbl.getIdMapping(),
joinTbl, null, joinTbl.getValueMapping(), null, null);
// Restrict to map owner
JavaTypeMapping ownerMapping = joinTbl.getOwnerMapping();
SQLExpression ownerExpr = exprFactory.newExpression(subStmt, joinSqlTbl, ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, mapExpr.getSQLTable(),
mapExpr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
valExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), valTbl.getIdMapping());
stmt.getQueryGenerator().bindVariable(varName, valCmd, valExpr.getSQLTable(),
valExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
valTbl.getIdMapping());
subStmt.whereAnd(valIdExpr.eq(valExpr), 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);
JavaTypeMapping ownerMapping = null;
if (mmd.getMappedBy() != null)
{
ownerMapping = valTbl.getMemberMapping(valCmd.getMetaDataForMember(mmd.getMappedBy()));
}
else
{
ownerMapping = valTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
}
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)
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);
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
valExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), valTbl.getIdMapping());
stmt.getQueryGenerator().bindVariable(varName, valCmd, valExpr.getSQLTable(),
valExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
JavaTypeMapping valMapping = valTbl.getIdMapping();
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), valMapping);
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
else if (mmd.getMap().getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY)
{
AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr, mmgr);
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());
if (valCmd == null)
{
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);
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
valExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
keyTbl.getMemberMapping(keyValMmd));
stmt.getQueryGenerator().bindVariable(varName, valCmd, valExpr.getSQLTable(),
valExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
JavaTypeMapping valMapping = keyTbl.getMemberMapping(keyValMmd);
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), valMapping);
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
else
{
DatastoreClass valTbl = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
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);
// Join to key table
SQLTable keySqlTbl = subStmt.innerJoin(subStmt.getPrimaryTable(), valTbl.getIdMapping(),
keyTbl, null, keyTbl.getMemberMapping(keyValMmd), null, null);
// Restrict to map owner (on key table)
SQLExpression ownerExpr = exprFactory.newExpression(subStmt, keySqlTbl, ownerMapping);
SQLExpression ownerIdExpr = exprFactory.newExpression(stmt, mapExpr.getSQLTable(),
mapExpr.getSQLTable().getTable().getIdMapping());
subStmt.whereAnd(ownerExpr.eq(ownerIdExpr), true);
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
valExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(), valTbl.getIdMapping());
stmt.getQueryGenerator().bindVariable(varName, valCmd, valExpr.getSQLTable(),
valExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
SQLExpression valIdExpr = exprFactory.newExpression(subStmt, subStmt.getPrimaryTable(),
valTbl.getIdMapping());
subStmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
}
return new BooleanSubqueryExpression(stmt, "EXISTS", subStmt);