public ScalarExpression newScalarExpression(QueryExpression qs, LogicSetExpression te)
{
if (getNumberOfDatastoreFields() > 0)
{
return new ObjectExpression(qs, this, te);
}
else
{
ClassLoaderResolver clr = qs.getClassLoaderResolver();
MappedStoreManager srm = qs.getStoreManager();
int relationType = fmd.getRelationType(clr);
if (relationType == Relation.ONE_TO_ONE_BI)
{
// Create an expression joining to the related field in the related table
DatastoreClass targetTable = srm.getDatastoreClass(fmd.getTypeName(), clr);
AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than one related field
JavaTypeMapping refMapping = targetTable.getFieldMapping(relatedMmds[0]);
JavaTypeMapping selectMapping = targetTable.getIDMapping();
DatastoreIdentifier targetTableIdentifier =
srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "RELATED" + fmd.getAbsoluteFieldNumber());
LogicSetExpression targetTe = qs.newTableExpression(targetTable, targetTableIdentifier);
return new ObjectExpression(qs, this, te, refMapping, targetTe, selectMapping);
}
else if (relationType == Relation.MANY_TO_ONE_BI)
{
AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than one related field
if (fmd.getJoinMetaData() != null || relatedMmds[0].getJoinMetaData() != null)
{
// Join table relation - only allows for Collection/Array
// Create an expression this table to the join table on the element id, selecting the owner id
DatastoreContainerObject targetTable = srm.getDatastoreContainerObject(relatedMmds[0]);
JavaTypeMapping refMapping = null;
JavaTypeMapping selectMapping = null;
DatastoreElementContainer elementTable = (DatastoreElementContainer)targetTable;
refMapping = elementTable.getElementMapping();
selectMapping = elementTable.getOwnerMapping();
DatastoreIdentifier targetTableIdentifier =
srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, "JOINTABLE" + fmd.getAbsoluteFieldNumber());
LogicSetExpression targetTe = qs.newTableExpression(targetTable, targetTableIdentifier);
return new ObjectExpression(qs, this, te, refMapping, targetTe, selectMapping);
}
}
}
// TODO Throw an exception since should be impossible