Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataEntity / System / Data / Query / PlanCompiler / ColumnMapTranslator.cs / 2 / ColumnMapTranslator.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....], [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Data.Query.InternalTrees;
using System.Data.Query.PlanCompiler;
using System.Linq;
//using System.Diagnostics; // Please use PlanCompiler.Assert instead of Debug.Assert in this class...
namespace System.Data.Query.PlanCompiler
{
///
/// Delegate pattern that the ColumnMapTranslator uses to find its replacement
/// columnMaps. Given a columnMap, return it's replacement.
///
///
///
internal delegate ColumnMap ColumnMapTranslatorTranslationDelegate(ColumnMap columnMap);
///
/// ColumnMapTranslator visits the ColumnMap hiearchy and runs the translation delegate
/// you specify; There are some static methods to perform common translations, but you
/// can bring your own translation if you desire.
///
/// This visitor only creates new ColumnMap objects when necessary; it attempts to
/// replace-in-place, except when that is not possible because the field is not
/// writable.
///
/// NOTE: over time, we should be able to modify the ColumnMaps to have more writable
/// fields;
///
internal class ColumnMapTranslator : ColumnMapVisitorWithResults
{
#region Constructors
///
/// Singleton instance for the "public" methods to use;
///
static private ColumnMapTranslator Instance = new ColumnMapTranslator();
///
/// Constructor; no one should use this.
///
private ColumnMapTranslator()
{
}
#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, Dictionary 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 "Public" surface area
///
/// Bring-Your-Own-Replacement-Delegate method.
///
///
///
///
internal static ColumnMap Translate(ColumnMap columnMap, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
return columnMap.Accept(ColumnMapTranslator.Instance, translationDelegate);
}
///
/// Replace VarRefColumnMaps with the specified ColumnMap replacement
///
///
///
///
internal static ColumnMap Translate(ColumnMap columnMapToTranslate, Dictionary varToColumnMap)
{
ColumnMap result = Translate(columnMapToTranslate,
delegate(ColumnMap columnMap)
{
VarRefColumnMap varRefColumnMap = columnMap as VarRefColumnMap;
if (null != varRefColumnMap)
{
if (varToColumnMap.TryGetValue(varRefColumnMap.Var, out columnMap))
{
// perform fixups; only allow name changes when the replacement isn't
// already named (and the original is named...)
if (!columnMap.IsNamed && varRefColumnMap.IsNamed)
{
columnMap.Name = varRefColumnMap.Name;
}
}
else
{
columnMap = varRefColumnMap;
}
}
return columnMap;
}
);
return result;
}
///
/// Replace VarRefColumnMaps with new VarRefColumnMaps with the specified Var
///
///
///
///
internal static ColumnMap Translate(ColumnMap columnMapToTranslate, Dictionary varToVarMap)
{
ColumnMap result = Translate(columnMapToTranslate,
delegate(ColumnMap columnMap)
{
VarRefColumnMap varRefColumnMap = columnMap as VarRefColumnMap;
if (null != varRefColumnMap)
{
Var replacementVar = GetReplacementVar(varRefColumnMap.Var, varToVarMap);
if (varRefColumnMap.Var != replacementVar)
{
columnMap = new VarRefColumnMap(varRefColumnMap.Type, varRefColumnMap.Name, replacementVar);
}
}
return columnMap;
}
);
return result;
}
///
/// Replace VarRefColumnMaps with ScalarColumnMaps referring to the command and column
///
///
///
///
internal static ColumnMap Translate(ColumnMap columnMapToTranslate, Dictionary> varToCommandColumnMap)
{
ColumnMap result = Translate(columnMapToTranslate,
delegate(ColumnMap columnMap)
{
VarRefColumnMap varRefColumnMap = columnMap as VarRefColumnMap;
if (null != varRefColumnMap)
{
KeyValuePair commandAndColumn;
if (!varToCommandColumnMap.TryGetValue(varRefColumnMap.Var, out commandAndColumn))
{
throw EntityUtil.InternalError(EntityUtil.InternalErrorCode.UnknownVar, 1, varRefColumnMap.Var.Id); // shouldn't have gotten here without having a resolveable var
}
columnMap = new ScalarColumnMap(varRefColumnMap.Type, varRefColumnMap.Name, commandAndColumn.Key, commandAndColumn.Value);
}
// While we're at it, we ensure that all columnMaps are named; we wait
// until this point, because we don't want to assign names until after
// we've gone through the transformations;
if (!columnMap.IsNamed)
{
columnMap.Name = ColumnMap.DefaultColumnName;
}
return columnMap;
}
);
return result;
}
#endregion
#region Visitor methods
#region List handling
///
/// List(ColumnMap)
///
///
///
///
private void VisitList(TResultType[] tList, ColumnMapTranslatorTranslationDelegate translationDelegate)
where TResultType : ColumnMap
{
for (int i = 0; i < tList.Length; i++)
{
tList[i] = (TResultType)tList[i].Accept(this, translationDelegate);
}
}
#endregion
#region EntityIdentity handling
///
/// DiscriminatedEntityIdentity
///
///
///
///
protected override EntityIdentity VisitEntityIdentity(DiscriminatedEntityIdentity entityIdentity, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
ColumnMap newEntitySetColumnMap = entityIdentity.EntitySetColumnMap.Accept(this, translationDelegate);
VisitList(entityIdentity.Keys, translationDelegate);
if (newEntitySetColumnMap != entityIdentity.EntitySetColumnMap)
{
entityIdentity = new DiscriminatedEntityIdentity((SimpleColumnMap)newEntitySetColumnMap, entityIdentity.EntitySetMap, entityIdentity.Keys);
}
return entityIdentity;
}
///
/// SimpleEntityIdentity
///
///
///
///
protected override EntityIdentity VisitEntityIdentity(SimpleEntityIdentity entityIdentity, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
VisitList(entityIdentity.Keys, translationDelegate);
return entityIdentity;
}
#endregion
///
/// ComplexTypeColumnMap
///
///
///
///
internal override ColumnMap Visit(ComplexTypeColumnMap columnMap, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
SimpleColumnMap newNullSentinel = columnMap.NullSentinel;
if (null != newNullSentinel)
{
newNullSentinel = (SimpleColumnMap)translationDelegate(newNullSentinel);
}
VisitList(columnMap.Properties, translationDelegate);
if (columnMap.NullSentinel != newNullSentinel)
{
columnMap = new ComplexTypeColumnMap(columnMap.Type, columnMap.Name, columnMap.Properties, newNullSentinel);
}
return translationDelegate(columnMap);
}
///
/// DiscriminatedCollectionColumnMap
///
///
///
///
internal override ColumnMap Visit(DiscriminatedCollectionColumnMap columnMap, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
ColumnMap newDiscriminator = columnMap.Discriminator.Accept(this, translationDelegate);
VisitList(columnMap.ForeignKeys, translationDelegate);
VisitList(columnMap.Keys, translationDelegate);
ColumnMap newElement = columnMap.Element.Accept(this, translationDelegate);
if (newDiscriminator != columnMap.Discriminator || newElement != columnMap.Element)
{
columnMap = new DiscriminatedCollectionColumnMap(columnMap.Type, columnMap.Name, newElement, columnMap.Keys, columnMap.ForeignKeys, columnMap.SortKeys, (SimpleColumnMap)newDiscriminator, columnMap.DiscriminatorValue);
}
return translationDelegate(columnMap);
}
///
/// EntityColumnMap
///
///
///
///
internal override ColumnMap Visit(EntityColumnMap columnMap, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
EntityIdentity newEntityIdentity = VisitEntityIdentity(columnMap.EntityIdentity, translationDelegate);
VisitList(columnMap.Properties, translationDelegate);
if (newEntityIdentity != columnMap.EntityIdentity)
{
columnMap = new EntityColumnMap(columnMap.Type, columnMap.Name, columnMap.Properties, newEntityIdentity);
}
return translationDelegate(columnMap);
}
///
/// SimplePolymorphicColumnMap
///
///
///
///
internal override ColumnMap Visit(SimplePolymorphicColumnMap columnMap, ColumnMapTranslatorTranslationDelegate translationDelegate)
{
ColumnMap newTypeDiscriminator = columnMap.TypeDiscriminator.Accept(this, translationDelegate);
// NOTE: we're using Copy-On-Write logic to avoid allocation if we don't
// need to change things.
Dictionary
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Rotation3D.cs
- CompositeTypefaceMetrics.cs
- VirtualPathProvider.cs
- validationstate.cs
- StyleConverter.cs
- ColumnMapProcessor.cs
- DbProviderSpecificTypePropertyAttribute.cs
- FragmentNavigationEventArgs.cs
- SqlParameter.cs
- ActivationArguments.cs
- Window.cs
- ToolboxItemFilterAttribute.cs
- _FtpDataStream.cs
- BitConverter.cs
- Label.cs
- StrokeCollection.cs
- MappingItemCollection.cs
- ResourceManager.cs
- FieldAccessException.cs
- MimeTextImporter.cs
- Empty.cs
- XsltOutput.cs
- TextStore.cs
- XmlQueryStaticData.cs
- HttpListenerRequestUriBuilder.cs
- WebBrowserDocumentCompletedEventHandler.cs
- LocalFileSettingsProvider.cs
- BaseProcessProtocolHandler.cs
- ActiveXHelper.cs
- controlskin.cs
- HtmlGenericControl.cs
- Flowchart.cs
- Perspective.cs
- BufferModeSettings.cs
- NativeMethods.cs
- DocumentXmlWriter.cs
- RenderContext.cs
- documentsequencetextcontainer.cs
- DynamicMetaObject.cs
- QueryCacheManager.cs
- DrawingVisualDrawingContext.cs
- Pool.cs
- MeasurementDCInfo.cs
- PartialCachingControl.cs
- TraceListener.cs
- PipelineComponent.cs
- MaterialGroup.cs
- DrawingVisual.cs
- UIElementParagraph.cs
- Configuration.cs
- SqlException.cs
- ClientCultureInfo.cs
- DesignerDataSourceView.cs
- OciHandle.cs
- SQLInt64Storage.cs
- CodeMemberField.cs
- StringBuilder.cs
- ClientSponsor.cs
- EmptyEnumerator.cs
- CompressedStack.cs
- Compensate.cs
- UniformGrid.cs
- TextSearch.cs
- PropertyToken.cs
- OptionUsage.cs
- TextProviderWrapper.cs
- CheckBoxStandardAdapter.cs
- BindableAttribute.cs
- CodeDefaultValueExpression.cs
- CodeAttributeDeclarationCollection.cs
- IntegrationExceptionEventArgs.cs
- SqlDelegatedTransaction.cs
- ISSmlParser.cs
- _ProxyRegBlob.cs
- WebPartConnectVerb.cs
- UIElement3DAutomationPeer.cs
- StreamGeometry.cs
- ImageMetadata.cs
- assertwrapper.cs
- JsonUriDataContract.cs
- StrokeNode.cs
- DependencyObjectType.cs
- FixedFlowMap.cs
- TypeProvider.cs
- CodeIdentifiers.cs
- ResourcesBuildProvider.cs
- RegexMatch.cs
- __FastResourceComparer.cs
- HtmlSelect.cs
- CodeTypeParameterCollection.cs
- WhitespaceRuleLookup.cs
- dsa.cs
- xmlglyphRunInfo.cs
- SQLStringStorage.cs
- TrackBarDesigner.cs
- HandledMouseEvent.cs
- CryptoKeySecurity.cs
- SecurityValidationBehavior.cs
- ServicesUtilities.cs
- XXXInfos.cs