Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataEntity / System / Data / Map / ViewGeneration / GeneratedView.cs / 1 / GeneratedView.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Data.Common.Utils;
using System.Data.Common.CommandTrees;
using System.Data.Common.EntitySql;
using System.Text;
using System.Collections.Generic;
using System.Data.Mapping.ViewGeneration.Utils;
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees.Internal;
using System.Data.Query.InternalTrees;
using System.Data.Query.PlanCompiler;
namespace System.Data.Mapping.ViewGeneration
{
// Holds the view generated for a given OFTYPE(Extent, Type) combination
internal class GeneratedView : InternalBase
{
// effects: Creates an object that stores a view, cqlString, for the combination
internal GeneratedView(EntitySetBase extent, EdmType type, string cqlString,
MetadataWorkspace workSpace, ConfigViewGenerator config)
{
m_extent = extent;
m_type = type;
m_cqlString = cqlString;
m_config = config;
if (m_config.IsViewTracing)
{
if (EqualityComparer.Default.Equals(type, extent.ElementType))
{
Helpers.FormatTraceLine("CQL view for {0}", extent.Name);
}
else
{
Helpers.FormatTraceLine("CQL view for OFTYPE({0}, {1}.{2})", extent.Name, type.NamespaceName, type.Name);
}
Helpers.StringTraceLine(cqlString);
}
if (m_config.IsViewTracing)
{
ParseView(workSpace, false /*isUserSpecified*/);
}
}
#region Fields
private EntitySetBase m_extent;
private EdmType m_type;
private string m_cqlString;
private DbQueryCommandTree m_commandTree; //We cache CQTs for Update Views sicne that is the one update stack works of.
private Node m_internalTreeNode; //we cache IQTs for Query Views since that is the one query stack works of.
private ConfigViewGenerator m_config;
private DiscriminatorMap m_discriminatorMap;
#endregion
#region Properties
internal EntitySetBase Extent
{
get { return m_extent; }
}
internal EdmType EntityType
{
get { return m_type; }
}
internal string CqlString
{
get { return m_cqlString; }
}
internal DiscriminatorMap DiscriminatorMap
{
get { return m_discriminatorMap; }
}
#endregion
#region Methods
///
/// Parses and validates a user-defined query mapping view. Outputs errors if any.
///
internal IEnumerable ParseUserSpecifiedView(MetadataWorkspace workspace,
StoreItemCollection storeItemCollection, StorageSetMapping setMapping, EntityTypeBase elementType, bool includeSubtypes)
{
m_commandTree = ParseView(workspace, true);
// Verify that all expressions appearing in the view are supported.
foreach (var error in ViewValidator.ValidateQueryView(m_commandTree, storeItemCollection, setMapping, elementType, includeSubtypes))
{
yield return error;
}
// Verify that the result type of the query view is assignable to the element type of the entityset
CollectionType queryResultType = (m_commandTree.Query.ResultType.EdmType) as CollectionType;
if ((queryResultType == null) || (!setMapping.Set.ElementType.IsAssignableFrom(queryResultType.TypeUsage.EdmType)))
{
EdmSchemaError error = new EdmSchemaError(System.Data.Entity.Strings.Mapping_Invalid_QueryView_Type_1(setMapping.Set.Name),
(int)StorageMappingErrorCode.InvalidQueryViewResultType, EdmSchemaErrorSeverity.Error,
setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition);
yield return error;
}
}
// effects: Given an extent and its corresponding view, invokes the
// parser to check if the view definition is syntactically correct
// Returns true iff the parsing succeeds
private DbQueryCommandTree ParseView(MetadataWorkspace workspace, bool isUserSpecified)
{
// We do not catch any internal exceptions any more
m_config.StartSingleWatch(PerfType.ViewParsing);
//If it is a user specified view,
//allow all queries. Otherwise parse the view in a restricted mode.
ParserOptions.CompilationMode compilationMode = ParserOptions.CompilationMode.RestrictedViewGenerationMode;
if (isUserSpecified)
{
compilationMode = ParserOptions.CompilationMode.UserViewGenerationMode;
}
DbQueryCommandTree commandTree = (DbQueryCommandTree)ExternalCalls.CompileView(CqlString, workspace, compilationMode);
// For non user-specified views, perform simplification
if (!isUserSpecified)
{
commandTree = ViewSimplifier.SimplifyView(commandTree);
}
// See if the view matches the "discriminated" pattern (allows simplification of generated store commands)
if (m_extent.BuiltInTypeKind == BuiltInTypeKind.EntitySet)
{
DiscriminatorMap discriminatorMap;
if (DiscriminatorMap.TryCreateDiscriminatorMap((EntitySet)m_extent, commandTree.Query, out discriminatorMap))
{
m_discriminatorMap = discriminatorMap;
}
}
m_config.StopSingleWatch(PerfType.ViewParsing);
return commandTree;
}
internal DbQueryCommandTree GetCommandTree(MetadataWorkspace workspace)
{
//Ask command trees only for C space entity sets
Debug.Assert(m_extent.EntityContainer.DataSpace == DataSpace.SSpace, "CQT should be asked only for update view");
if (m_commandTree == null)
{
m_commandTree = ParseView(workspace, false);
}
return m_commandTree;
}
internal Node GetInternalTree(MetadataWorkspace workspace)
{
Debug.Assert(m_extent.EntityContainer.DataSpace == DataSpace.CSpace, "Internal Tree should be asked only for query view");
if (m_internalTreeNode == null)
{
DbQueryCommandTree tree = ParseView(workspace, false);
// Convert this into an ITree first
Command itree = ITreeGenerator.Generate(tree, this.DiscriminatorMap);
// Pull out the root physical project-op, and copy this itree into our own itree
PlanCompiler.Assert(itree.Root.Op.OpType == OpType.PhysicalProject,
"Expected a physical projectOp at the root of the tree - found " + itree.Root.Op.OpType);
// #554756: VarVec enumerators are not cached on the shared Command instance.
itree.DisableVarVecEnumCaching();
m_internalTreeNode = itree.Root.Child0;
}
return m_internalTreeNode;
}
#endregion
#region String Methods
internal override void ToCompactString(StringBuilder builder)
{
builder.Append("OFTYPE(")
.Append(Extent.Name)
.Append(", ")
.Append(EntityType.Name)
.Append(") = ")
.Append(CqlString);
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Data.Common.Utils;
using System.Data.Common.CommandTrees;
using System.Data.Common.EntitySql;
using System.Text;
using System.Collections.Generic;
using System.Data.Mapping.ViewGeneration.Utils;
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees.Internal;
using System.Data.Query.InternalTrees;
using System.Data.Query.PlanCompiler;
namespace System.Data.Mapping.ViewGeneration
{
// Holds the view generated for a given OFTYPE(Extent, Type) combination
internal class GeneratedView : InternalBase
{
// effects: Creates an object that stores a view, cqlString, for the combination
internal GeneratedView(EntitySetBase extent, EdmType type, string cqlString,
MetadataWorkspace workSpace, ConfigViewGenerator config)
{
m_extent = extent;
m_type = type;
m_cqlString = cqlString;
m_config = config;
if (m_config.IsViewTracing)
{
if (EqualityComparer.Default.Equals(type, extent.ElementType))
{
Helpers.FormatTraceLine("CQL view for {0}", extent.Name);
}
else
{
Helpers.FormatTraceLine("CQL view for OFTYPE({0}, {1}.{2})", extent.Name, type.NamespaceName, type.Name);
}
Helpers.StringTraceLine(cqlString);
}
if (m_config.IsViewTracing)
{
ParseView(workSpace, false /*isUserSpecified*/);
}
}
#region Fields
private EntitySetBase m_extent;
private EdmType m_type;
private string m_cqlString;
private DbQueryCommandTree m_commandTree; //We cache CQTs for Update Views sicne that is the one update stack works of.
private Node m_internalTreeNode; //we cache IQTs for Query Views since that is the one query stack works of.
private ConfigViewGenerator m_config;
private DiscriminatorMap m_discriminatorMap;
#endregion
#region Properties
internal EntitySetBase Extent
{
get { return m_extent; }
}
internal EdmType EntityType
{
get { return m_type; }
}
internal string CqlString
{
get { return m_cqlString; }
}
internal DiscriminatorMap DiscriminatorMap
{
get { return m_discriminatorMap; }
}
#endregion
#region Methods
///
/// Parses and validates a user-defined query mapping view. Outputs errors if any.
///
internal IEnumerable ParseUserSpecifiedView(MetadataWorkspace workspace,
StoreItemCollection storeItemCollection, StorageSetMapping setMapping, EntityTypeBase elementType, bool includeSubtypes)
{
m_commandTree = ParseView(workspace, true);
// Verify that all expressions appearing in the view are supported.
foreach (var error in ViewValidator.ValidateQueryView(m_commandTree, storeItemCollection, setMapping, elementType, includeSubtypes))
{
yield return error;
}
// Verify that the result type of the query view is assignable to the element type of the entityset
CollectionType queryResultType = (m_commandTree.Query.ResultType.EdmType) as CollectionType;
if ((queryResultType == null) || (!setMapping.Set.ElementType.IsAssignableFrom(queryResultType.TypeUsage.EdmType)))
{
EdmSchemaError error = new EdmSchemaError(System.Data.Entity.Strings.Mapping_Invalid_QueryView_Type_1(setMapping.Set.Name),
(int)StorageMappingErrorCode.InvalidQueryViewResultType, EdmSchemaErrorSeverity.Error,
setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition);
yield return error;
}
}
// effects: Given an extent and its corresponding view, invokes the
// parser to check if the view definition is syntactically correct
// Returns true iff the parsing succeeds
private DbQueryCommandTree ParseView(MetadataWorkspace workspace, bool isUserSpecified)
{
// We do not catch any internal exceptions any more
m_config.StartSingleWatch(PerfType.ViewParsing);
//If it is a user specified view,
//allow all queries. Otherwise parse the view in a restricted mode.
ParserOptions.CompilationMode compilationMode = ParserOptions.CompilationMode.RestrictedViewGenerationMode;
if (isUserSpecified)
{
compilationMode = ParserOptions.CompilationMode.UserViewGenerationMode;
}
DbQueryCommandTree commandTree = (DbQueryCommandTree)ExternalCalls.CompileView(CqlString, workspace, compilationMode);
// For non user-specified views, perform simplification
if (!isUserSpecified)
{
commandTree = ViewSimplifier.SimplifyView(commandTree);
}
// See if the view matches the "discriminated" pattern (allows simplification of generated store commands)
if (m_extent.BuiltInTypeKind == BuiltInTypeKind.EntitySet)
{
DiscriminatorMap discriminatorMap;
if (DiscriminatorMap.TryCreateDiscriminatorMap((EntitySet)m_extent, commandTree.Query, out discriminatorMap))
{
m_discriminatorMap = discriminatorMap;
}
}
m_config.StopSingleWatch(PerfType.ViewParsing);
return commandTree;
}
internal DbQueryCommandTree GetCommandTree(MetadataWorkspace workspace)
{
//Ask command trees only for C space entity sets
Debug.Assert(m_extent.EntityContainer.DataSpace == DataSpace.SSpace, "CQT should be asked only for update view");
if (m_commandTree == null)
{
m_commandTree = ParseView(workspace, false);
}
return m_commandTree;
}
internal Node GetInternalTree(MetadataWorkspace workspace)
{
Debug.Assert(m_extent.EntityContainer.DataSpace == DataSpace.CSpace, "Internal Tree should be asked only for query view");
if (m_internalTreeNode == null)
{
DbQueryCommandTree tree = ParseView(workspace, false);
// Convert this into an ITree first
Command itree = ITreeGenerator.Generate(tree, this.DiscriminatorMap);
// Pull out the root physical project-op, and copy this itree into our own itree
PlanCompiler.Assert(itree.Root.Op.OpType == OpType.PhysicalProject,
"Expected a physical projectOp at the root of the tree - found " + itree.Root.Op.OpType);
// #554756: VarVec enumerators are not cached on the shared Command instance.
itree.DisableVarVecEnumCaching();
m_internalTreeNode = itree.Root.Child0;
}
return m_internalTreeNode;
}
#endregion
#region String Methods
internal override void ToCompactString(StringBuilder builder)
{
builder.Append("OFTYPE(")
.Append(Extent.Name)
.Append(", ")
.Append(EntityType.Name)
.Append(") = ")
.Append(CqlString);
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SubstitutionList.cs
- ManagedWndProcTracker.cs
- Main.cs
- TypeGeneratedEventArgs.cs
- PanelDesigner.cs
- PermissionSetTriple.cs
- ResourceExpressionBuilder.cs
- NopReturnReader.cs
- RemotingSurrogateSelector.cs
- Int16KeyFrameCollection.cs
- Debug.cs
- TargetPerspective.cs
- HtmlControlAdapter.cs
- SafeCryptContextHandle.cs
- BitmapMetadataBlob.cs
- DocumentStatusResources.cs
- RelationshipManager.cs
- wgx_commands.cs
- UpdateTracker.cs
- Trustee.cs
- BulletDecorator.cs
- TextContainer.cs
- TreeNodeCollection.cs
- TypeSystemProvider.cs
- DescendentsWalkerBase.cs
- MouseActionValueSerializer.cs
- FlagsAttribute.cs
- PopOutPanel.cs
- QueryActivatableWorkflowsCommand.cs
- WebControlToolBoxItem.cs
- PrivilegedConfigurationManager.cs
- StateMachineDesignerPaint.cs
- XmlNotation.cs
- CatalogPartCollection.cs
- AtomServiceDocumentSerializer.cs
- SchemaTableOptionalColumn.cs
- DebugHandleTracker.cs
- RoleManagerSection.cs
- DocumentXmlWriter.cs
- ReadContentAsBinaryHelper.cs
- ManipulationStartingEventArgs.cs
- XNameConverter.cs
- DBCommandBuilder.cs
- ListItem.cs
- AppModelKnownContentFactory.cs
- SamlAssertionDirectKeyIdentifierClause.cs
- ExpressionBuilder.cs
- ClipboardProcessor.cs
- Parallel.cs
- DiagnosticsConfigurationHandler.cs
- UnaryOperationBinder.cs
- VisualStyleTypesAndProperties.cs
- VariantWrapper.cs
- DirectoryLocalQuery.cs
- Certificate.cs
- FixedSOMTextRun.cs
- MatrixTransform3D.cs
- ObjectParameter.cs
- QilUnary.cs
- TableLayoutPanelCellPosition.cs
- InkPresenter.cs
- Figure.cs
- Ray3DHitTestResult.cs
- HtmlString.cs
- SafeProcessHandle.cs
- BitmapCodecInfo.cs
- TemplateKeyConverter.cs
- CollectionsUtil.cs
- Interlocked.cs
- Point3DCollectionConverter.cs
- BamlTreeMap.cs
- PackageRelationshipSelector.cs
- PrivilegeNotHeldException.cs
- EdmTypeAttribute.cs
- ISO2022Encoding.cs
- TemplateInstanceAttribute.cs
- HyperLinkDataBindingHandler.cs
- UserUseLicenseDictionaryLoader.cs
- TypeBuilder.cs
- SortDescriptionCollection.cs
- MachineKeySection.cs
- SQLString.cs
- DataError.cs
- GcSettings.cs
- SharedPersonalizationStateInfo.cs
- VisualBrush.cs
- StrokeCollectionConverter.cs
- SqlXmlStorage.cs
- BigInt.cs
- EventlogProvider.cs
- Timeline.cs
- FixedSOMTextRun.cs
- ComponentConverter.cs
- PropagatorResult.cs
- ScriptingProfileServiceSection.cs
- ConstraintStruct.cs
- HtmlToClrEventProxy.cs
- WindowsRichEdit.cs
- IImplicitResourceProvider.cs
- XmlDataCollection.cs