if(!path.isCMRField())
{
throw new IllegalArgumentException("path must be a cmr field");
}
JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge) path.getCMRField();
String pathStr = path.getPath(path.size() - 2);
String parentAlias = aliasManager.getAlias(pathStr);
// if exists is not supported we use a left join and is null
if(!subquerySupported)
{
// add the path to the list of paths to left join
addLeftJoinPath(pathStr, path);
forceDistinct = true;
addJoinPath(path);
if(cmrField.getRelationMetaData().isForeignKeyMappingStyle())
{
JDBCEntityBridge childEntity = (JDBCEntityBridge) cmrField.getRelatedEntity();
String childAlias = aliasManager.getAlias(path.getPath());
SQLUtil.getIsNullClause(!not, childEntity.getPrimaryKeyFields(), childAlias, buf);
}
else
{
String relationTableAlias = aliasManager.getRelationTableAlias(path.getPath());
SQLUtil.getIsNullClause(!not, cmrField.getTableKeyFields(), relationTableAlias, buf);
}
return;
}
if(not)
{
buf.append(SQLUtil.NOT);
}
buf.append(SQLUtil.EXISTS).append('(');
if(cmrField.getRelationMetaData().isForeignKeyMappingStyle())
{
JDBCEntityBridge childEntity = (JDBCEntityBridge) cmrField.getRelatedEntity();
String childAlias = aliasManager.getAlias(path.getPath());
buf.append(SQLUtil.SELECT);
SQLUtil.getColumnNamesClause(childEntity.getPrimaryKeyFields(), childAlias, buf)
.append(SQLUtil.FROM)
.append(childEntity.getQualifiedTableName()).append(' ').append(childAlias)
.append(SQLUtil.WHERE);
SQLUtil.getJoinClause(cmrField, parentAlias, childAlias, buf);
}
else
{
String relationTableAlias = aliasManager.getRelationTableAlias(path.getPath());
buf.append(SQLUtil.SELECT);
SQLUtil.getColumnNamesClause(cmrField.getTableKeyFields(), relationTableAlias, buf)
.append(SQLUtil.FROM)
.append(cmrField.getQualifiedTableName())
.append(' ')
.append(relationTableAlias)
.append(SQLUtil.WHERE);
SQLUtil.getRelationTableJoinClause(cmrField, parentAlias, relationTableAlias, buf);
}