ColumnMapCopier.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Query / InternalTrees / ColumnMapCopier.cs / 1305376 / ColumnMapCopier.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Data.Query.InternalTrees; 
using System.Data.Query.PlanCompiler;
using System.Linq; 
using System.Data.Mapping; 
using System.Data.Metadata.Edm;
 
namespace System.Data.Query.InternalTrees
{
    /// 
    /// The ColumnMapCopier clones an entire ColumnMap hierarchy; this is different 
    /// than the ColumnMapTranslator, which only copies things that need to be copied.
    /// 
    /// Note that this is a stateless visitor; it uses the visitor's argument for its 
    /// state management.
    /// 
    /// The Visitor's argument is a VarMap; anytime a Var is found in the ColumnMap
    /// hierarchy, it is replaced with the replacement from the VarMap.
    ///
    /// Note also that previous implementations of this class attempted to avoid re- 
    /// processing ColumnMaps by caching the results for each input and returning it.
    /// I wasn't convinced that we were buying much with all that caching, since the 
    /// only ColumnMaps that should be repeated in the hierarchy are simple ones; there 
    /// is about as much object creation either way.  The only reason I see that we
    /// want to cache these is if we really cared to have only one VarRefColumnMap 
    /// instance for a given Var and be able to use reference equality instead of
    /// comparing the Vars themselves.  I don't believe we're making that guarantee
    /// anywhere else, so I've removed that for now because I don't want the added
    /// complexity that the caching adds.  If performance analysis indicates there is 
    /// a problem, we can considier addding the cache back in.
    ///  
    internal class ColumnMapCopier : ColumnMapVisitorWithResults 
    {
 
        #region Constructors

        /// 
        /// Singleton instance for the "public" methods to use; 
        /// 
        static private ColumnMapCopier Instance = new ColumnMapCopier(); 
 
        /// 
        /// Constructor; no one should use this. 
        /// 
        private ColumnMapCopier()
        {
        } 

        #endregion 
 
        #region "Public" surface area
 
        /// 
        /// Return a copy of the column map, replacing all vars with the replacements
        /// found in the replacementVarMap
        ///  
        /// 
        ///  
        ///  
        internal static ColumnMap Copy(ColumnMap columnMap, VarMap replacementVarMap)
        { 
            return columnMap.Accept(Instance, replacementVarMap);
        }

        #endregion 

        #region Visitor Helpers 
 
        /// 
        /// Returns the var to use in the copy, either the original or the 
        /// replacement.  Note that we will follow the chain of replacements, in
        /// case the replacement was also replaced.
        /// 
        ///  
        /// 
        ///  
        private static Var GetReplacementVar(Var originalVar, VarMap replacementVarMap) 
        {
            // SQLBUDT #478509: Follow the chain of mapped vars, don't 
            //                  just stop at the first one
            Var replacementVar = originalVar;

            while (replacementVarMap.TryGetValue(replacementVar, out originalVar)) 
            {
                if (originalVar == replacementVar) 
                { 
                    break;
                } 
                replacementVar = originalVar;
            }
            return replacementVar;
        } 

        #endregion 
 
        #region Visitor Methods
 
        #region List handling

        /// 
        /// Copies the List of ColumnMaps or SimpleColumnMaps 
        /// 
        ///  
        ///  
        /// 
        ///  
        internal TListType[] VisitList(TListType[] tList, VarMap replacementVarMap)
            where TListType : ColumnMap
        {
            TListType[] newTList = new TListType[tList.Length]; 
            for(int i = 0; i < tList.Length; ++i) {
                newTList[i] = (TListType)tList[i].Accept(this, replacementVarMap); 
            } 
            return newTList;
        } 

        #endregion

        #region EntityIdentity handling 

        ///  
        /// Copies the DiscriminatedEntityIdentity 
        /// 
        ///  
        /// 
        /// 
        protected override EntityIdentity VisitEntityIdentity(DiscriminatedEntityIdentity entityIdentity, VarMap replacementVarMap)
        { 
            SimpleColumnMap newEntitySetCol = (SimpleColumnMap)entityIdentity.EntitySetColumnMap.Accept(this, replacementVarMap);
            SimpleColumnMap[] newKeys = VisitList(entityIdentity.Keys, replacementVarMap); 
            return new DiscriminatedEntityIdentity(newEntitySetCol, entityIdentity.EntitySetMap, newKeys); 
        }
 
        /// 
        /// Copies the SimpleEntityIdentity
        /// 
        ///  
        /// 
        ///  
        protected override EntityIdentity VisitEntityIdentity(SimpleEntityIdentity entityIdentity, VarMap replacementVarMap) 
        {
            SimpleColumnMap[] newKeys = VisitList(entityIdentity.Keys, replacementVarMap); 
            return new SimpleEntityIdentity(entityIdentity.EntitySet, newKeys);
        }

        #endregion 

        ///  
        /// ComplexTypeColumnMap 
        /// 
        ///  
        /// 
        /// 
        internal override ColumnMap Visit(ComplexTypeColumnMap columnMap, VarMap replacementVarMap)
        { 
            SimpleColumnMap newNullability = columnMap.NullSentinel;
            if (null != newNullability) 
            { 
                newNullability = (SimpleColumnMap)newNullability.Accept(this, replacementVarMap);
            } 
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new ComplexTypeColumnMap(columnMap.Type, columnMap.Name, fieldList, newNullability);
        }
 
        /// 
        /// DiscriminatedCollectionColumnMap 
        ///  
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(DiscriminatedCollectionColumnMap columnMap, VarMap replacementVarMap)
        {
            ColumnMap newElementColumnMap = columnMap.Element.Accept(this, replacementVarMap); 
            SimpleColumnMap newDiscriminator = (SimpleColumnMap)columnMap.Discriminator.Accept(this, replacementVarMap);
            SimpleColumnMap[] newKeys = VisitList(columnMap.Keys, replacementVarMap); 
            SimpleColumnMap[] newForeignKeys = VisitList(columnMap.ForeignKeys, replacementVarMap); 
            return new DiscriminatedCollectionColumnMap(columnMap.Type, columnMap.Name, newElementColumnMap, newKeys, newForeignKeys, newDiscriminator, columnMap.DiscriminatorValue);
        } 

        /// 
        /// EntityColumnMap
        ///  
        /// 
        ///  
        ///  
        internal override ColumnMap Visit(EntityColumnMap columnMap, VarMap replacementVarMap)
        { 
            EntityIdentity newEntityIdentity = VisitEntityIdentity(columnMap.EntityIdentity, replacementVarMap);
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new EntityColumnMap(columnMap.Type, columnMap.Name, fieldList, newEntityIdentity);
        } 

        ///  
        /// SimplePolymorphicColumnMap 
        /// 
        ///  
        /// 
        /// 
        internal override ColumnMap Visit(SimplePolymorphicColumnMap columnMap, VarMap replacementVarMap)
        { 
            SimpleColumnMap newDiscriminator = (SimpleColumnMap)columnMap.TypeDiscriminator.Accept(this, replacementVarMap);
 
            Dictionary newTypeChoices = new Dictionary(columnMap.TypeChoices.Comparer); 
            foreach (KeyValuePair kv in columnMap.TypeChoices)
            { 
                TypedColumnMap newMap = (TypedColumnMap)kv.Value.Accept(this, replacementVarMap);
                newTypeChoices[kv.Key] = newMap;
            }
            ColumnMap[] newBaseFieldList = VisitList(columnMap.Properties, replacementVarMap); 
            return new SimplePolymorphicColumnMap(columnMap.Type, columnMap.Name, newBaseFieldList, newDiscriminator, newTypeChoices);
        } 
 
        /// 
        /// MultipleDiscriminatorPolymorphicColumnMap 
        /// 
        internal override ColumnMap Visit(MultipleDiscriminatorPolymorphicColumnMap columnMap, VarMap replacementVarMap)
        {
            // At this time, we shouldn't ever see this type here; it's for SPROCS which don't use 
            // the plan compiler.
            System.Data.Query.PlanCompiler.PlanCompiler.Assert(false, "unexpected MultipleDiscriminatorPolymorphicColumnMap in ColumnMapCopier"); 
            return null; 
        }
 
        /// 
        /// RecordColumnMap
        /// 
        ///  
        /// 
        ///  
        internal override ColumnMap Visit(RecordColumnMap columnMap, VarMap replacementVarMap) 
        {
            SimpleColumnMap newNullability = columnMap.NullSentinel; 
            if (null != newNullability)
            {
                newNullability = (SimpleColumnMap)newNullability.Accept(this, replacementVarMap);
            } 
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new RecordColumnMap(columnMap.Type, columnMap.Name, fieldList, newNullability); 
        } 

        ///  
        /// RefColumnMap
        /// 
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(RefColumnMap columnMap, VarMap replacementVarMap) 
        { 
            EntityIdentity newEntityIdentity = VisitEntityIdentity(columnMap.EntityIdentity, replacementVarMap);
            return new RefColumnMap(columnMap.Type, columnMap.Name, newEntityIdentity); 
        }

        /// 
        /// ScalarColumnMap 
        /// 
        ///  
        ///  
        /// 
        internal override ColumnMap Visit(ScalarColumnMap columnMap, VarMap replacementVarMap) 
        {
            return new ScalarColumnMap(columnMap.Type, columnMap.Name, columnMap.CommandId, columnMap.ColumnPos);
        }
 
        /// 
        /// SimpleCollectionColumnMap 
        ///  
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(SimpleCollectionColumnMap columnMap, VarMap replacementVarMap)
        {
            ColumnMap newElementColumnMap = columnMap.Element.Accept(this, replacementVarMap); 
            SimpleColumnMap[] newKeys = VisitList(columnMap.Keys, replacementVarMap);
            SimpleColumnMap[] newForeignKeys = VisitList(columnMap.ForeignKeys, replacementVarMap); 
            return new SimpleCollectionColumnMap(columnMap.Type, columnMap.Name, newElementColumnMap, newKeys, newForeignKeys); 
        }
 
        /// 
        /// VarRefColumnMap
        /// 
        ///  
        /// 
        ///  
        internal override ColumnMap Visit(VarRefColumnMap columnMap, VarMap replacementVarMap) 
        {
            Var replacementVar = GetReplacementVar(columnMap.Var, replacementVarMap); 
            return new VarRefColumnMap(columnMap.Type, columnMap.Name, replacementVar);
        }

        #endregion 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Data.Query.InternalTrees; 
using System.Data.Query.PlanCompiler;
using System.Linq; 
using System.Data.Mapping; 
using System.Data.Metadata.Edm;
 
namespace System.Data.Query.InternalTrees
{
    /// 
    /// The ColumnMapCopier clones an entire ColumnMap hierarchy; this is different 
    /// than the ColumnMapTranslator, which only copies things that need to be copied.
    /// 
    /// Note that this is a stateless visitor; it uses the visitor's argument for its 
    /// state management.
    /// 
    /// The Visitor's argument is a VarMap; anytime a Var is found in the ColumnMap
    /// hierarchy, it is replaced with the replacement from the VarMap.
    ///
    /// Note also that previous implementations of this class attempted to avoid re- 
    /// processing ColumnMaps by caching the results for each input and returning it.
    /// I wasn't convinced that we were buying much with all that caching, since the 
    /// only ColumnMaps that should be repeated in the hierarchy are simple ones; there 
    /// is about as much object creation either way.  The only reason I see that we
    /// want to cache these is if we really cared to have only one VarRefColumnMap 
    /// instance for a given Var and be able to use reference equality instead of
    /// comparing the Vars themselves.  I don't believe we're making that guarantee
    /// anywhere else, so I've removed that for now because I don't want the added
    /// complexity that the caching adds.  If performance analysis indicates there is 
    /// a problem, we can considier addding the cache back in.
    ///  
    internal class ColumnMapCopier : ColumnMapVisitorWithResults 
    {
 
        #region Constructors

        /// 
        /// Singleton instance for the "public" methods to use; 
        /// 
        static private ColumnMapCopier Instance = new ColumnMapCopier(); 
 
        /// 
        /// Constructor; no one should use this. 
        /// 
        private ColumnMapCopier()
        {
        } 

        #endregion 
 
        #region "Public" surface area
 
        /// 
        /// Return a copy of the column map, replacing all vars with the replacements
        /// found in the replacementVarMap
        ///  
        /// 
        ///  
        ///  
        internal static ColumnMap Copy(ColumnMap columnMap, VarMap replacementVarMap)
        { 
            return columnMap.Accept(Instance, replacementVarMap);
        }

        #endregion 

        #region Visitor Helpers 
 
        /// 
        /// Returns the var to use in the copy, either the original or the 
        /// replacement.  Note that we will follow the chain of replacements, in
        /// case the replacement was also replaced.
        /// 
        ///  
        /// 
        ///  
        private static Var GetReplacementVar(Var originalVar, VarMap replacementVarMap) 
        {
            // SQLBUDT #478509: Follow the chain of mapped vars, don't 
            //                  just stop at the first one
            Var replacementVar = originalVar;

            while (replacementVarMap.TryGetValue(replacementVar, out originalVar)) 
            {
                if (originalVar == replacementVar) 
                { 
                    break;
                } 
                replacementVar = originalVar;
            }
            return replacementVar;
        } 

        #endregion 
 
        #region Visitor Methods
 
        #region List handling

        /// 
        /// Copies the List of ColumnMaps or SimpleColumnMaps 
        /// 
        ///  
        ///  
        /// 
        ///  
        internal TListType[] VisitList(TListType[] tList, VarMap replacementVarMap)
            where TListType : ColumnMap
        {
            TListType[] newTList = new TListType[tList.Length]; 
            for(int i = 0; i < tList.Length; ++i) {
                newTList[i] = (TListType)tList[i].Accept(this, replacementVarMap); 
            } 
            return newTList;
        } 

        #endregion

        #region EntityIdentity handling 

        ///  
        /// Copies the DiscriminatedEntityIdentity 
        /// 
        ///  
        /// 
        /// 
        protected override EntityIdentity VisitEntityIdentity(DiscriminatedEntityIdentity entityIdentity, VarMap replacementVarMap)
        { 
            SimpleColumnMap newEntitySetCol = (SimpleColumnMap)entityIdentity.EntitySetColumnMap.Accept(this, replacementVarMap);
            SimpleColumnMap[] newKeys = VisitList(entityIdentity.Keys, replacementVarMap); 
            return new DiscriminatedEntityIdentity(newEntitySetCol, entityIdentity.EntitySetMap, newKeys); 
        }
 
        /// 
        /// Copies the SimpleEntityIdentity
        /// 
        ///  
        /// 
        ///  
        protected override EntityIdentity VisitEntityIdentity(SimpleEntityIdentity entityIdentity, VarMap replacementVarMap) 
        {
            SimpleColumnMap[] newKeys = VisitList(entityIdentity.Keys, replacementVarMap); 
            return new SimpleEntityIdentity(entityIdentity.EntitySet, newKeys);
        }

        #endregion 

        ///  
        /// ComplexTypeColumnMap 
        /// 
        ///  
        /// 
        /// 
        internal override ColumnMap Visit(ComplexTypeColumnMap columnMap, VarMap replacementVarMap)
        { 
            SimpleColumnMap newNullability = columnMap.NullSentinel;
            if (null != newNullability) 
            { 
                newNullability = (SimpleColumnMap)newNullability.Accept(this, replacementVarMap);
            } 
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new ComplexTypeColumnMap(columnMap.Type, columnMap.Name, fieldList, newNullability);
        }
 
        /// 
        /// DiscriminatedCollectionColumnMap 
        ///  
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(DiscriminatedCollectionColumnMap columnMap, VarMap replacementVarMap)
        {
            ColumnMap newElementColumnMap = columnMap.Element.Accept(this, replacementVarMap); 
            SimpleColumnMap newDiscriminator = (SimpleColumnMap)columnMap.Discriminator.Accept(this, replacementVarMap);
            SimpleColumnMap[] newKeys = VisitList(columnMap.Keys, replacementVarMap); 
            SimpleColumnMap[] newForeignKeys = VisitList(columnMap.ForeignKeys, replacementVarMap); 
            return new DiscriminatedCollectionColumnMap(columnMap.Type, columnMap.Name, newElementColumnMap, newKeys, newForeignKeys, newDiscriminator, columnMap.DiscriminatorValue);
        } 

        /// 
        /// EntityColumnMap
        ///  
        /// 
        ///  
        ///  
        internal override ColumnMap Visit(EntityColumnMap columnMap, VarMap replacementVarMap)
        { 
            EntityIdentity newEntityIdentity = VisitEntityIdentity(columnMap.EntityIdentity, replacementVarMap);
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new EntityColumnMap(columnMap.Type, columnMap.Name, fieldList, newEntityIdentity);
        } 

        ///  
        /// SimplePolymorphicColumnMap 
        /// 
        ///  
        /// 
        /// 
        internal override ColumnMap Visit(SimplePolymorphicColumnMap columnMap, VarMap replacementVarMap)
        { 
            SimpleColumnMap newDiscriminator = (SimpleColumnMap)columnMap.TypeDiscriminator.Accept(this, replacementVarMap);
 
            Dictionary newTypeChoices = new Dictionary(columnMap.TypeChoices.Comparer); 
            foreach (KeyValuePair kv in columnMap.TypeChoices)
            { 
                TypedColumnMap newMap = (TypedColumnMap)kv.Value.Accept(this, replacementVarMap);
                newTypeChoices[kv.Key] = newMap;
            }
            ColumnMap[] newBaseFieldList = VisitList(columnMap.Properties, replacementVarMap); 
            return new SimplePolymorphicColumnMap(columnMap.Type, columnMap.Name, newBaseFieldList, newDiscriminator, newTypeChoices);
        } 
 
        /// 
        /// MultipleDiscriminatorPolymorphicColumnMap 
        /// 
        internal override ColumnMap Visit(MultipleDiscriminatorPolymorphicColumnMap columnMap, VarMap replacementVarMap)
        {
            // At this time, we shouldn't ever see this type here; it's for SPROCS which don't use 
            // the plan compiler.
            System.Data.Query.PlanCompiler.PlanCompiler.Assert(false, "unexpected MultipleDiscriminatorPolymorphicColumnMap in ColumnMapCopier"); 
            return null; 
        }
 
        /// 
        /// RecordColumnMap
        /// 
        ///  
        /// 
        ///  
        internal override ColumnMap Visit(RecordColumnMap columnMap, VarMap replacementVarMap) 
        {
            SimpleColumnMap newNullability = columnMap.NullSentinel; 
            if (null != newNullability)
            {
                newNullability = (SimpleColumnMap)newNullability.Accept(this, replacementVarMap);
            } 
            ColumnMap[] fieldList = VisitList(columnMap.Properties, replacementVarMap);
            return new RecordColumnMap(columnMap.Type, columnMap.Name, fieldList, newNullability); 
        } 

        ///  
        /// RefColumnMap
        /// 
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(RefColumnMap columnMap, VarMap replacementVarMap) 
        { 
            EntityIdentity newEntityIdentity = VisitEntityIdentity(columnMap.EntityIdentity, replacementVarMap);
            return new RefColumnMap(columnMap.Type, columnMap.Name, newEntityIdentity); 
        }

        /// 
        /// ScalarColumnMap 
        /// 
        ///  
        ///  
        /// 
        internal override ColumnMap Visit(ScalarColumnMap columnMap, VarMap replacementVarMap) 
        {
            return new ScalarColumnMap(columnMap.Type, columnMap.Name, columnMap.CommandId, columnMap.ColumnPos);
        }
 
        /// 
        /// SimpleCollectionColumnMap 
        ///  
        /// 
        ///  
        /// 
        internal override ColumnMap Visit(SimpleCollectionColumnMap columnMap, VarMap replacementVarMap)
        {
            ColumnMap newElementColumnMap = columnMap.Element.Accept(this, replacementVarMap); 
            SimpleColumnMap[] newKeys = VisitList(columnMap.Keys, replacementVarMap);
            SimpleColumnMap[] newForeignKeys = VisitList(columnMap.ForeignKeys, replacementVarMap); 
            return new SimpleCollectionColumnMap(columnMap.Type, columnMap.Name, newElementColumnMap, newKeys, newForeignKeys); 
        }
 
        /// 
        /// VarRefColumnMap
        /// 
        ///  
        /// 
        ///  
        internal override ColumnMap Visit(VarRefColumnMap columnMap, VarMap replacementVarMap) 
        {
            Var replacementVar = GetReplacementVar(columnMap.Var, replacementVarMap); 
            return new VarRefColumnMap(columnMap.Type, columnMap.Name, replacementVar);
        }

        #endregion 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK