private void RematerilizeExtrenalNodeTableReference(WSelectQueryBlock query, Dictionary<string, MatchNode> nodes)
{
var tableRefs = query.FromClause.TableReferences;
var tableSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var newTableRefs = new List<WTableReference>();
for (int index = 0; index < tableRefs.Count; ++index)
{
var table = tableRefs[index] as WNamedTableReference;
if (table == null)
{
newTableRefs.Add(tableRefs[index]);
continue;
}
if (!nodes.ContainsKey(table.ExposedName.Value))
{
newTableRefs.Add(table);
}
else
{
tableSet.Add(table.ExposedName.Value);
}
}
query.FromClause = new WFromClause
{
TableReferences = newTableRefs,
};
WBooleanExpression whereCondiction = null;
foreach (var node in nodes.Where(node => !tableSet.Contains(node.Key)))
{
node.Value.External = true;
var newWhereCondition = new WBooleanComparisonExpression
{
ComparisonType = BooleanComparisonType.Equals,
FirstExpr = new WColumnReferenceExpression
{
MultiPartIdentifier = new WMultiPartIdentifier(
new Identifier { Value = node.Key },
new Identifier { Value = "GlobalNodeId" })
},
SecondExpr = new WColumnReferenceExpression
{
MultiPartIdentifier = new WMultiPartIdentifier(
new Identifier { Value = node.Value.RefAlias },
new Identifier { Value = "GlobalNodeId" })
},
};
whereCondiction = WBooleanBinaryExpression.Conjunction(whereCondiction, newWhereCondition);
}
if (whereCondiction != null)
{
if (query.WhereClause == null)
{
query.WhereClause = new WWhereClause { SearchCondition = whereCondiction };
}
else
{
if (query.WhereClause.SearchCondition == null)
{
query.WhereClause.SearchCondition = whereCondiction;
}
else
{
query.WhereClause.SearchCondition = new WBooleanBinaryExpression
{
BooleanExpressionType = BooleanBinaryExpressionType.And,
FirstExpr = new WBooleanParenthesisExpression
{
Expression = query.WhereClause.SearchCondition
},
SecondExpr = new WBooleanParenthesisExpression
{
Expression = whereCondiction
}
};
}
}
}
}