{
rightCmd = mmgr.getMetaDataForClass(leftMmd.getMap().getValueType(), clr);
}
break;
default :
throw new NucleusUserException(
"Subquery has been specified with a candidate-expression that" +
" includes \"" + tokens[i] + "\" that isnt a relation field!!");
}
leftMmds[i] = leftMmd;
rightMmds[i] = rightMmd;
leftCmd = rightCmd;
}
// Work from subquery candidate back to outer query table, adding joins and where clause as appropriate
SQLTable rSqlTbl = candSqlTbl;
SQLTable outerSqlTbl = outerSqlTblMapping.table;
for (int i=leftMmds.length-1;i>=0;i--)
{
AbstractMemberMetaData leftMmd = leftMmds[i];
AbstractMemberMetaData rightMmd = rightMmds[i];
DatastoreClass leftTbl = storeMgr.getDatastoreClass(leftMmd.getClassName(true), clr);
SQLTable lSqlTbl = null;
int relationType = leftMmd.getRelationType(clr);
switch (relationType)
{
case Relation.ONE_TO_ONE_UNI:
{
// 1-1 with FK in left table
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rSqlTbl.getTable().getIdMapping());
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
JavaTypeMapping leftMapping = leftTbl.getMemberMapping(leftMmd);
lSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
leftTbl, null, leftMapping, null, null);
}
break;
}
case Relation.ONE_TO_ONE_BI:
{
if (leftMmd.getMappedBy() != null)
{
// 1-1 with FK in right table
JavaTypeMapping rightMapping = rSqlTbl.getTable().getMemberMapping(rightMmd);
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rightMapping);
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(rSqlTbl, rightMapping,
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
else
{
// 1-1 with FK in left table
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rSqlTbl.getTable().getIdMapping());
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null);
}
}
break;
}
case Relation.ONE_TO_MANY_UNI:
{
if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)
{
// 1-N with join table to right table, so join from right to join table
ElementContainerTable joinTbl =
(ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
joinTbl, null, joinTbl.getElementMapping(), null, null);
if (i == 0)
{
// Add where clause join table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression joinExpr = exprFactory.newExpression(stmt,
joinSqlTbl, joinTbl.getOwnerMapping());
stmt.whereAnd(outerExpr.eq(joinExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
else
{
// 1-N with FK in right table
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd));
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd),
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
break;
}
case Relation.ONE_TO_MANY_BI:
{
if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)
{
// 1-N with join table to right table, so join from right to join table
ElementContainerTable joinTbl =
(ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
joinTbl, null, joinTbl.getElementMapping(), null, null);
if (i == 0)
{
// Add where clause join table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression joinExpr = exprFactory.newExpression(stmt,
joinSqlTbl, joinTbl.getOwnerMapping());
stmt.whereAnd(outerExpr.eq(joinExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getOwnerMapping(),
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
else
{
// 1-N with FK in right table
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getIdMapping());
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd));
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getMemberMapping(rightMmd),
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
break;
}
case Relation.MANY_TO_ONE_BI:
{
if (leftMmd.getJoinMetaData() != null || rightMmd.getJoinMetaData() != null)
{
// 1-N with join table to right table, so join from right to join table
ElementContainerTable joinTbl =
(ElementContainerTable)storeMgr.getDatastoreContainerObject(leftMmd);
SQLTable joinSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
joinTbl, null, joinTbl.getOwnerMapping(), null, null);
if (i == 0)
{
// Add where clause join table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression joinExpr = exprFactory.newExpression(stmt,
joinSqlTbl, joinTbl.getElementMapping());
stmt.whereAnd(outerExpr.eq(joinExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
leftTbl, null, leftTbl.getIdMapping(), null, null);
}
}
else
{
if (i == 0)
{
// Add where clause right table to outer table
SQLExpression outerExpr = exprFactory.newExpression(outerSqlTbl.getSQLStatement(),
outerSqlTbl, outerSqlTbl.getTable().getMemberMapping(leftMmd));
SQLExpression rightExpr = exprFactory.newExpression(stmt,
rSqlTbl, rSqlTbl.getTable().getIdMapping());
stmt.whereAnd(outerExpr.eq(rightExpr), false);
}
else
{
// Join to left table
lSqlTbl = stmt.innerJoin(rSqlTbl, rSqlTbl.getTable().getIdMapping(),
leftTbl, null, leftTbl.getMemberMapping(leftMmd), null, null);
}
}
}
default:
break;
}
rSqlTbl = lSqlTbl;
}
}
// Process all join expressions
Expression rightExpr = clsExpr.getRight();
SQLTable sqlTbl = candSqlTbl;
while (rightExpr != null)
{
if (rightExpr instanceof JoinExpression)
{
JoinExpression joinExpr = (JoinExpression)rightExpr;
JoinType joinType = joinExpr.getType();
String joinAlias = joinExpr.getAlias();
PrimaryExpression joinPrimExpr = joinExpr.getPrimaryExpression();
Iterator<String> iter = joinPrimExpr.getTuples().iterator();
String rootId = iter.next();
String joinTableGroupName = null;
if (rootId.equalsIgnoreCase(candidateAlias))
{
// Join relative to the candidate
// Name table group of joined-to as per the relation
// Note : this will only work for one level out from the candidate TODO Extend this
joinTableGroupName = joinPrimExpr.getId();
}
else
{
// Join relative to some other alias
SQLTableMapping sqlTblMapping = getSQLTableMappingForAlias(rootId);
if (sqlTblMapping != null)
{
cmd = sqlTblMapping.cmd;
joinTableGroupName = sqlTblMapping.table.getGroupName();
sqlTbl = sqlTblMapping.table;
}
else
{
throw new NucleusUserException("Query has " + joinPrimExpr.getId() + " yet the first component "+
rootId + " is unknown!");
}
}
while (iter.hasNext())