GraphView.TranslateMatchClauseVisitor.RematerilizeExtrenalNodeTableReference C# (CSharp) Method

RematerilizeExtrenalNodeTableReference() private method

If a table alias in the MATCH clause is defined in an upper-level context, to be able to translate this MATCH clause, this table alias must be re-materialized in the FROM clause of the current context and joined with the corresponding table in the upper-level context.
private RematerilizeExtrenalNodeTableReference ( GraphView.WSelectQueryBlock query, MatchNode>.Dictionary nodes ) : void
query GraphView.WSelectQueryBlock Select query
nodes MatchNode>.Dictionary A collection of node alias and match node instance
return void
        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
                            }
                        };
                    }
                }
            }
        }