}
}
LogicSetExpression rightTblExpr;
String rightAlias;
DatastoreIdentifier rightTblAlias;
DatastoreClass rightTable;
if (i == tokens.length-1)
{
// Candidate
rightTblExpr = qs.getMainTableExpression();
rightTblAlias = qs.getMainTableAlias();
rightTable = (DatastoreClass)rightTblExpr.getMainTable();
rightAlias = candidateAlias;
}
else
{
// Not outer candidate, nor inner candidate so add table. Alias is "T{num}"
// TODO Parameterise this naming of intermediate tables
rightTable = storeMgr.getDatastoreClass(cls.getName(), clr);
rightAlias = "T" + i;
rightTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
IdentifierFactory.TABLE, rightAlias);
rightTblExpr = qs.newTableExpression(rightTable, rightTblAlias);
}
if (relationType == Relation.ONE_TO_ONE_UNI ||
(relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() == null) ||
(relationType == Relation.MANY_TO_ONE_BI &&
(leftMmd.getJoinMetaData() == null && rightMmd.getJoinMetaData() == null)))
{
// 1-1/N-1 with FK here
ScalarExpression leftExpr = leftTblExpr.newFieldExpression(tokens[i]);
ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
if (i == 1)
{
// And condition to outer candidate
qs.andCondition(leftExpr.eq(rightExpr), true);
}
else
{
// Inner join to table
qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
}
}
else if (relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() != null)
{
// 1-1 with FK on other side
ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
ScalarExpression rightExpr = rightTblExpr.newFieldExpression(rightMmd.getName());
if (i == 1)
{
// And condition to outer candidate
qs.andCondition(leftExpr.eq(rightExpr), true);
}
else
{
// Inner join to table
qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
}
}
else if ((relationType == Relation.ONE_TO_MANY_UNI && leftMmd.getJoinMetaData() == null) ||
(relationType == Relation.ONE_TO_MANY_BI &&
(leftMmd.getJoinMetaData() == null && rightMmd.getJoinMetaData() == null)))
{
// 1-N FK with FK on other side
ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
ScalarExpression rightExpr = rightTblExpr.newFieldExpression(rightMmd.getName());
if (i == 1)
{
// And condition to outer candidate
qs.andCondition(leftExpr.eq(rightExpr), true);
}
else
{
// Inner join to table
qs.innerJoin(rightExpr, leftExpr, leftTblExpr, true, true);
}
}
else if (relationType == Relation.ONE_TO_MANY_UNI && leftMmd.getJoinMetaData() != null)
{
// 1-N uni JoinTable
ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
ScalarExpression leftCentreExpr = null;
ScalarExpression rightCentreExpr = null;
LogicSetExpression joinTblExpr = null;
if (leftMmd.hasCollection())
{
CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(leftMmd);
DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
rightCentreExpr = joinTbl.getElementMapping().newScalarExpression(qs, joinTblExpr);
}
else if (leftMmd.hasMap())
{
MapTable joinTbl = (MapTable)storeMgr.getDatastoreContainerObject(leftMmd);
DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
rightCentreExpr = joinTbl.getValueMapping().newScalarExpression(qs, joinTblExpr);
}
if (i == 1)
{
// And condition to outer candidate
qs.andCondition(leftExpr.eq(leftCentreExpr), true);
}
else
{
// Inner join to table
qs.innerJoin(leftCentreExpr, leftExpr, leftTblExpr, true, true);
}
qs.innerJoin(rightExpr, rightCentreExpr, joinTblExpr, true, true);
}
else if ((relationType == Relation.ONE_TO_MANY_BI &&
(leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)) ||
(relationType == Relation.MANY_TO_ONE_BI &&
(leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)) ||
relationType == Relation.MANY_TO_MANY_BI)
{
// 1-N/N-1 bi JoinTable
ScalarExpression leftExpr = leftTable.getIDMapping().newScalarExpression(qs, leftTblExpr);
ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
ScalarExpression leftCentreExpr = null;
ScalarExpression rightCentreExpr = null;
LogicSetExpression joinTblExpr = null;
if (leftMmd.hasCollection() || rightMmd.hasCollection())
{
CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(leftMmd);
DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
rightCentreExpr = joinTbl.getElementMapping().newScalarExpression(qs, joinTblExpr);
}
else if (leftMmd.hasMap() || rightMmd.hasMap())
{
MapTable joinTbl = (MapTable)storeMgr.getDatastoreContainerObject(leftMmd);
DatastoreIdentifier joinTblAlias = storeMgr.getIdentifierFactory().newIdentifier(
IdentifierFactory.TABLE, leftAlias + "." + rightAlias);
joinTblExpr = qs.newTableExpression(joinTbl, joinTblAlias);
leftCentreExpr = joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
rightCentreExpr = joinTbl.getValueMapping().newScalarExpression(qs, joinTblExpr);
}