StringBuffer buf = (StringBuffer) data;
// setup compare to vars first, so we can compre types in from vars
ASTPath toPath = (ASTPath) node.jjtGetChild(1);
JDBCCMRFieldBridge toCMRField = (JDBCCMRFieldBridge) toPath.getCMRField();
JDBCEntityBridge toChildEntity = (JDBCEntityBridge) toPath.getEntity();
String pathStr = toPath.getPath(toPath.size() - 2);
String toParentAlias = aliasManager.getAlias(pathStr);
String toChildAlias = aliasManager.getAlias(toPath.getPath());
String relationTableAlias = null;
if(toCMRField.getRelationMetaData().isTableMappingStyle())
{
relationTableAlias = aliasManager.getRelationTableAlias(toPath.getPath());
}
// setup from variables
String fromAlias = null;
int fromParamNumber = -1;
if(node.jjtGetChild(0) instanceof ASTParameter)
{
ASTParameter fromParam = (ASTParameter) node.jjtGetChild(0);
// can only compare like kind entities
verifyParameterEntityType(fromParam.number, toChildEntity);
fromParamNumber = fromParam.number;
}
else
{
ASTPath fromPath = (ASTPath) node.jjtGetChild(0);
addJoinPath(fromPath);
JDBCEntityBridge fromEntity = (JDBCEntityBridge) fromPath.getEntity();
fromAlias = aliasManager.getAlias(fromPath.getPath());
// can only compare like kind entities
if(!fromEntity.equals(toChildEntity))
{
throw new IllegalStateException("Only like types can be " +
"compared: from entity=" +
fromEntity.getEntityName() +
" to entity=" + toChildEntity.getEntityName());
}
}
// add the path to the list of paths to left join
addLeftJoinPath(pathStr, toPath);
// first part makes toChild not in toParent.child
if(!subquerySupported)
{
addJoinPath(toPath);
// subquery not supported; use a left join and is not null
if(node.not)
{
buf.append(SQLUtil.NOT);
}
buf.append('(');
if(relationTableAlias == null)
{
SQLUtil.getIsNullClause(true, toChildEntity.getPrimaryKeyFields(), toChildAlias, buf);
}
else
{
SQLUtil.getIsNullClause(true, toCMRField.getTableKeyFields(), relationTableAlias, buf);
}
}
else
{
// subquery supported; use exists subquery
if(node.not)
{
buf.append(SQLUtil.NOT);
}
buf.append(SQLUtil.EXISTS).append('(');
if(relationTableAlias == null)
{
buf.append(SQLUtil.SELECT);
SQLUtil.getColumnNamesClause(toChildEntity.getPrimaryKeyFields(), toChildAlias, buf)
.append(SQLUtil.FROM)
.append(toChildEntity.getQualifiedTableName())
.append(' ')
.append(toChildAlias)
.append(SQLUtil.WHERE);
SQLUtil.getJoinClause(toCMRField, toParentAlias, toChildAlias, buf);
}
else
{
buf.append(SQLUtil.SELECT);
SQLUtil.getColumnNamesClause(toCMRField.getRelatedCMRField().getTableKeyFields(), relationTableAlias, buf)
.append(SQLUtil.FROM)
.append(toCMRField.getQualifiedTableName())
.append(' ')
.append(relationTableAlias)
.append(SQLUtil.WHERE);
SQLUtil.getRelationTableJoinClause(toCMRField, toParentAlias, relationTableAlias, buf);
}
}
buf.append(SQLUtil.AND);
// second part makes fromNode equal toChild
if(fromAlias != null)
{
// compre pk to pk
if(relationTableAlias == null)
{
SQLUtil.getSelfCompareWhereClause(toChildEntity.getPrimaryKeyFields(),
toChildAlias,
fromAlias,
buf);
}
else
{
SQLUtil.getRelationTableJoinClause(toCMRField.getRelatedCMRField(),
fromAlias,
relationTableAlias,
buf);
}
}
else
{
// add the parameters
inputParameters.addAll(QueryParameter.createParameters(fromParamNumber - 1,
toChildEntity));
// compare pk to parameter
if(relationTableAlias == null)
{
SQLUtil.getWhereClause(toChildEntity.getPrimaryKeyFields(), toChildAlias, buf);
}
else
{
SQLUtil.getWhereClause(toCMRField.getRelatedCMRField().getTableKeyFields(),
relationTableAlias,
buf);
}
}