private void PrepareJoins(QueryTablePlanner tablePlanner, SqlQueryExpression queryExpression, QueryExpressionFrom queryFrom, ref SqlExpression searchExpression)
{
var fromClause = queryExpression.FromClause;
bool allInner = true;
for (int i = 0; i < fromClause.JoinPartCount; i++)
{
var joinPart = fromClause.GetJoinPart(i);
if (joinPart.JoinType != JoinType.Inner)
{
allInner = false;
}
}
for (int i = 0; i < fromClause.JoinPartCount; i++)
{
var joinPart = fromClause.GetJoinPart(i);
var joinType = joinPart.JoinType;
var onExpression = joinPart.OnExpression;
if (allInner)
{
// If the whole join set is inner joins then simply move the on
// expression (if there is one) to the WHERE clause.
if (searchExpression != null && onExpression != null)
{
searchExpression = SqlExpression.And(searchExpression, onExpression);
}
else if (searchExpression == null)
{
searchExpression = onExpression;
}
}
else
{
// Not all inner joins,
if (joinType == JoinType.Inner && onExpression == null)
{
// Regular join with no ON expression, so no preparation necessary
}
else
{
// Either an inner join with an ON expression, or an outer join with
// ON expression
if (onExpression == null)
{
throw new InvalidOperationException(String.Format("Join of type {0} requires ON expression.", joinType));
}
// Resolve the on_expression
onExpression = onExpression.Prepare(queryFrom.ExpressionPreparer);
// And set it in the planner
tablePlanner.JoinAt(i, joinType, onExpression);
}
}
}
}