if (!clr.isAssignableFrom(elementType, filteredElementType) &&
!clr.isAssignableFrom(filteredElementType, elementType))
{
throw new IncompatibleQueryElementTypeException(elementType, filteredElementType.getName());
}
DatastoreContainerObject filteredElementTable = storeMgr.getDatastoreClass(filteredElementType.getName(), clr);
if (stmt.getTableExpression(elementTableAlias) == null)
{
// Make sure we have the table containing the elements in the statement
stmt.newTableExpression(filteredElementTable, elementTableAlias);
}
DatastoreIdentifier containerRangeVar = setTableAlias;
if (existsQuery)
{
// Part of EXISTS query. Why do we treat this differently ?????????????
if (stmt.getTableExpression(containerRangeVar) == null)
{
// WHY????????????????????????
}
if (stmt.getTableExpression(containerRangeVar) == null)
{
// Create the container table if not yet present in the statement
containerRangeVar = elementTableAlias;
stmt.newTableExpression(containerTable, containerRangeVar);
}
}
else
{
if (parentStmt != stmt)
{
// Subquery. Why do we treat differently ??????
if (stmt.getTableExpression(containerRangeVar) == null)
{
// WHY????????????????????????
containerRangeVar = elementTableAlias;
}
if (stmt.getTableExpression(containerRangeVar) == null)
{
// Create the container table if not yet present in the statement
stmt.newTableExpression(containerTable, containerRangeVar);
}
// Reverse collection contains query so join back to the owner
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, ownerTe);
ScalarExpression ownerSetExpr = this.ownerMapping.newScalarExpression(stmt,
stmt.getTableExpression(containerRangeVar));
stmt.andCondition(ownerExpr.eq(ownerSetExpr), true);
}
else
{
if (elementExpr.getLogicSetExpression().getMainTable() == filteredElementTable)
{
// Element expression is of the container table
// Simple example is "SELECT FROM MyType WHERE field1.contains(this)"
// so candidate is MyType, element type is MyType and the element we are checking
// is MyType.
if (stmt.getTableExpression(containerRangeVar) == null)
{
// Why???????????????
containerRangeVar = elementTableAlias;
}
if (stmt.getTableExpression(containerRangeVar) == null)
{
// Create the container table if not yet present in the statement
stmt.newTableExpression(containerTable, containerRangeVar);
}
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, ownerTe);
ScalarExpression ownerSetExpr = this.ownerMapping.newScalarExpression(stmt,
stmt.getTableExpression(containerRangeVar));
stmt.andCondition(ownerExpr.eq(ownerSetExpr), true);
}
else
{
// Element is of a different table
// Simple example is "SELECT FROM MyType WHERE field1.contains(field2)"
// so candidate is MyType, element type is MyElement, and the element we are checking
// is MyType.field2. This creates an INNER JOIN MYTYPES.ID -> MYELEMENT.FK
if (stmt.getTableExpression(containerRangeVar) == null)
{
// Create the container table if not yet present in the statement
stmt.newTableExpression(containerTable, containerRangeVar);
}
// Add a join from the owner table to the container table
ScalarExpression ownerExpr = ownerMapping.newScalarExpression(stmt, ownerTe);
ScalarExpression ownerSetExpr = this.ownerMapping.newScalarExpression(stmt,
stmt.getTableExpression(containerRangeVar));
stmt.innerJoin(ownerExpr, ownerSetExpr, stmt.getTableExpression(containerRangeVar), true, true);
}
}
}
// Return the expression of the PK of the elements table for the element to be bound to
JavaTypeMapping elementTableID = filteredElementTable.getIDMapping();
return elementTableID.newScalarExpression(stmt, stmt.getTableExpression(containerRangeVar));
}