public void Remove(InputFragment fragment)
        {
            if (fragment == null)
            {
                return;
            }
            if (fragment.Name != null)
            {
                scopeTable.Remove(fragment.Name);
            }

            if (fragment is SelectStatement)
            {
                Remove((fragment as SelectStatement).From);
            }
            else if (fragment is JoinFragment)
            {
                JoinFragment j = fragment as JoinFragment;
                Remove(j.Left);
                Remove(j.Right);
            }
            else if (fragment is UnionFragment)
            {
                UnionFragment u = fragment as UnionFragment;
                Remove(u.Left);
                Remove(u.Right);
            }
        }
        public override SqlFragment Visit(DbPropertyExpression expression)
        {
            propertyLevel++;
            PropertyFragment fragment = expression.Instance.Accept(this) as PropertyFragment;

            fragment.Properties.Add(expression.Property.Name);
            propertyLevel--;

            // if we are not at the top level property then just return
            if (propertyLevel > 0)
            {
                return(fragment);
            }

            ColumnFragment column = new ColumnFragment(null, fragment.LastProperty);

            column.PropertyFragment = fragment;
            InputFragment input = scope.FindInputFromProperties(fragment);

            if (input != null)
            {
                column.TableName = input.Name;
            }

            // now we need to check if our column name was possibly renamed
            if (input is TableFragment)
            {
                return(column);
            }

            SelectStatement select = input as SelectStatement;
            UnionFragment   union  = input as UnionFragment;

            if (select != null)
            {
                select.HasDifferentNameForColumn(column);
            }
            else if (union != null)
            {
                union.HasDifferentNameForColumn(column);
            }

            // input is a table, selectstatement, or unionstatement
            return(column);
        }
        public override SqlFragment Visit(DbUnionAllExpression expression)
        {
            UnionFragment f = new UnionFragment();

            Debug.Assert(expression.Left is DbProjectExpression);
            Debug.Assert(expression.Right is DbProjectExpression);

            SelectStatement left = VisitInputExpressionEnsureSelect(expression.Left, null, null);

            Debug.Assert(left.Name == null);
            //            left.Wrap(null);

            SelectStatement right = VisitInputExpressionEnsureSelect(expression.Right, null, null);

            Debug.Assert(right.Name == null);
            //          right.Wrap(null);

            f.Left  = left;
            f.Right = right;
            return(f);
        }
        public override SqlFragment Visit(DbUnionAllExpression expression)
        {
            UnionFragment f = new UnionFragment();
            Debug.Assert(expression.Left is DbProjectExpression);
            Debug.Assert(expression.Right is DbProjectExpression);

            SelectStatement left = VisitInputExpressionEnsureSelect(expression.Left, null, null);
            Debug.Assert(left.Name == null);
            //            left.Wrap(null);

            SelectStatement right = VisitInputExpressionEnsureSelect(expression.Right, null, null);
            Debug.Assert(right.Name == null);
            //          right.Wrap(null);

            f.Left = left;
            f.Right = right;
            return f;
        }