Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Map / ViewGeneration / ViewgenGatekeeper.cs / 1305376 / ViewgenGatekeeper.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Data.Common.Utils; using System.Data.Common.Utils.Boolean; using System.Data.Mapping.ViewGeneration.Structures; using System.Data.Mapping.ViewGeneration.Validation; using System.Data.Mapping.ViewGeneration.QueryRewriting; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Data.Mapping.ViewGeneration.Utils; using System.Data.Metadata.Edm; using System.Linq; namespace System.Data.Mapping.ViewGeneration { using ViewSet = KeyToListMap; using CellGroup = Set ; using WrapperBoolExpr = BoolExpr | ; using WrapperTrueExpr = TrueExpr ; using WrapperFalseExpr = FalseExpr ; using WrapperNotExpr = NotExpr ; using WrapperOrExpr = OrExpr ; abstract internal class ViewgenGatekeeper : InternalBase { /// /// Entry point for View Generation /// /// /// /// ///Generated Views for EntitySets internal static ViewGenResults GenerateViewsFromMapping(StorageEntityContainerMapping containerMapping, ConfigViewGenerator config) { EntityUtil.CheckArgumentNull(containerMapping, "containerMapping"); EntityUtil.CheckArgumentNull(config, "config"); Debug.Assert(!containerMapping.IsEmpty, "Precondition Violated: No mapping exists to generate viws for!"); //Create Cells from StorageEntityContainerMapping CellCreator cellCreator = new CellCreator(containerMapping); Listcells = cellCreator.GenerateCells(config); CqlIdentifiers identifiers = cellCreator.Identifiers; return GenerateViewsFromCells(cells, config, identifiers, containerMapping); } /// | /// Entry point for Type specific generation of Query Views /// internal static ViewGenResults GenerateTypeSpecificQueryView(StorageEntityContainerMapping containerMapping, ConfigViewGenerator config, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out bool success) { EntityUtil.CheckArgumentNull(containerMapping, "containerMapping"); EntityUtil.CheckArgumentNull(config, "config"); EntityUtil.CheckArgumentNull(entity, "entity"); EntityUtil.CheckArgumentNull(type, "type"); Debug.Assert(!type.Abstract, "Can not generate OfType/OfTypeOnly query view for and abstract type"); if (config.IsNormalTracing) { Helpers.StringTraceLine(""); Helpers.StringTraceLine("<<<<<<<< Generating Query View for Entity [" + entity.Name + "] OfType" + (includeSubtypes ? "" : "Only") + "(" + type.Name + ") >>>>>>>"); } if (containerMapping.GetEntitySetMapping(entity.Name).QueryView != null) { //Type-specific QV does not exist in the cache, but // there is a EntitySet QV. So we can't generate the view (no mapping exists for this EntitySet) // and we rely on Query to call us again to get the EntitySet View. success = false; return null; } //Compute Cell Groups or get it from Memoizer InputForComputingCellGroups args = new InputForComputingCellGroups(containerMapping, config); OutputFromComputeCellGroups result = containerMapping.GetCellgroups(args); success = result.Success; if (!success) { return null; } ListforeignKeyConstraints = result.ForeignKeyConstraints; // Get a Clone of cell groups from cache since cells are modified during viewgen, and we dont want the cached copy to change List cellGroups = cellGroups = result.CellGroups.Select(setOfcells => new CellGroup(setOfcells.Select(cell => new Cell(cell)))).ToList(); List cells = result.Cells; CqlIdentifiers identifiers = result.Identifiers; ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, config, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); success = true; //atleast we tried successfully return viewGenResults; } foreach (CellGroup cellGroup in cellGroups) { if (!DoesCellGroupContainEntitySet(cellGroup, entity)) { continue; } ViewGenerator viewGenerator = null; ErrorLog groupErrorLog = new ErrorLog(); try { viewGenerator = new ViewGenerator(cellGroup, config, foreignKeyConstraints, containerMapping); } catch (InternalMappingException exception) { // All exceptions have mapping errors in them Debug.Assert(exception.ErrorLog.Count > 0, "Incorrectly created mapping exception"); groupErrorLog = exception.ErrorLog; } if (groupErrorLog.Count > 0) { break; } Debug.Assert(viewGenerator != null); //make sure there is no exception thrown that does not add error to log ViewGenerationMode mode = includeSubtypes ? ViewGenerationMode.OfTypeViews : ViewGenerationMode.OfTypeOnlyViews; groupErrorLog = viewGenerator.GenerateQueryViewForSingleExtent(viewGenResults.Views, identifiers, entity, type, mode); if (groupErrorLog.Count != 0) { viewGenResults.AddErrors(groupErrorLog); } } success = true; return viewGenResults; } // effects: Given a list of cells in the schema, generates the query and // update mapping views for OFTYPE(Extent, Type) combinations in this schema // container. Returns a list of generated query and update views. // If it is false and some columns in a table are unmapped, an // exception is raised private static ViewGenResults GenerateViewsFromCells(List | cells, ConfigViewGenerator config, CqlIdentifiers identifiers, StorageEntityContainerMapping containerMapping) { EntityUtil.CheckArgumentNull(cells, "cells"); EntityUtil.CheckArgumentNull(config, "config"); Debug.Assert(cells.Count > 0, "There must be at least one cell in the container mapping"); // Go through each table and determine their foreign key constraints EntityContainer container = containerMapping.StorageEntityContainer; Debug.Assert(container != null); ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, config, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); return viewGenResults; } List | foreignKeyConstraints = ForeignConstraint.GetForeignConstraints(container); CellPartitioner partitioner = new CellPartitioner(cells, foreignKeyConstraints); List cellGroups = partitioner.GroupRelatedCells(); foreach (CellGroup cellGroup in cellGroups) { ViewGenerator viewGenerator = null; ErrorLog groupErrorLog = new ErrorLog(); try { viewGenerator = new ViewGenerator(cellGroup, config, foreignKeyConstraints, containerMapping); } catch (InternalMappingException exception) { // All exceptions have mapping errors in them Debug.Assert(exception.ErrorLog.Count > 0, "Incorrectly created mapping exception"); groupErrorLog = exception.ErrorLog; } if (groupErrorLog.Count == 0) { Debug.Assert(viewGenerator != null); groupErrorLog = viewGenerator.GenerateAllBidirectionalViews(viewGenResults.Views, identifiers); } if (groupErrorLog.Count != 0) { viewGenResults.AddErrors(groupErrorLog); } } // We used to print the errors here. Now we trace them as they are being thrown //if (viewGenResults.HasErrors && config.IsViewTracing) { // Helpers.StringTraceLine(viewGenResults.ErrorsToString()); //} return viewGenResults; } // effects: Given a container, ensures that all entity/association // sets in container on the C-side have been mapped private static ErrorLog EnsureAllCSpaceContainerSetsAreMapped(IEnumerable cells, ConfigViewGenerator config, StorageEntityContainerMapping containerMapping) { Set | mappedExtents = new Set (); string mslFileLocation = null; EntityContainer container = null; // Determine the container and name of the file while determining // the set of mapped extents in the cells foreach (Cell cell in cells) { mappedExtents.Add(cell.CQuery.Extent); mslFileLocation = cell.CellLabel.SourceLocation; // All cells are from the same container container = cell.CQuery.Extent.EntityContainer; } Debug.Assert(container != null); List missingExtents = new List (); // Go through all the extents in the container and determine // extents that are missing foreach (EntitySetBase extent in container.BaseEntitySets) { if (mappedExtents.Contains(extent) == false && !(containerMapping.HasQueryViewForSetMap(extent.Name))) { AssociationSet associationSet = extent as AssociationSet; if (associationSet==null || !associationSet.ElementType.IsForeignKey) { missingExtents.Add(extent); } } } ErrorLog errorLog = new ErrorLog(); // If any extent is not mapped, add an error if (missingExtents.Count > 0) { StringBuilder extentBuilder = new StringBuilder(); bool isFirst = true; foreach (EntitySetBase extent in missingExtents) { if (isFirst == false) { extentBuilder.Append(", "); } isFirst = false; extentBuilder.Append(extent.Name); } string message = System.Data.Entity.Strings.ViewGen_Missing_Set_Mapping_0(extentBuilder); // Find the cell with smallest line number - so that we can // point to the beginning of the file int lowestLineNum = -1; Cell smallestCell = null; foreach (Cell cell in cells) { if (lowestLineNum == -1 || cell.CellLabel.StartLineNumber < lowestLineNum) { smallestCell = cell; lowestLineNum = cell.CellLabel.StartLineNumber; } } Debug.Assert(smallestCell != null && lowestLineNum >= 0); EdmSchemaError edmSchemaError = new EdmSchemaError(message, (int)ViewGenErrorCode.MissingExtentMapping, EdmSchemaErrorSeverity.Error, containerMapping.SourceLocation, containerMapping.StartLineNumber, containerMapping.StartLinePosition, null); ErrorLog.Record record = new ErrorLog.Record(edmSchemaError); errorLog.AddEntry(record); } return errorLog; } #region Static Helpers private static bool DoesCellGroupContainEntitySet(CellGroup group, EntitySetBase entity) { foreach (Cell cell in group) { if (cell.GetLeftQuery(ViewTarget.QueryView).Extent.Equals(entity)) { return true; } } return false; } #endregion internal override void ToCompactString(StringBuilder builder) { } } } // 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.Utils.Boolean; using System.Data.Mapping.ViewGeneration.Structures; using System.Data.Mapping.ViewGeneration.Validation; using System.Data.Mapping.ViewGeneration.QueryRewriting; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Data.Mapping.ViewGeneration.Utils; using System.Data.Metadata.Edm; using System.Linq; namespace System.Data.Mapping.ViewGeneration { using ViewSet = KeyToListMap; using CellGroup = Set ; using WrapperBoolExpr = BoolExpr | ; using WrapperTrueExpr = TrueExpr ; using WrapperFalseExpr = FalseExpr ; using WrapperNotExpr = NotExpr ; using WrapperOrExpr = OrExpr ; abstract internal class ViewgenGatekeeper : InternalBase { /// /// Entry point for View Generation /// /// /// /// ///Generated Views for EntitySets internal static ViewGenResults GenerateViewsFromMapping(StorageEntityContainerMapping containerMapping, ConfigViewGenerator config) { EntityUtil.CheckArgumentNull(containerMapping, "containerMapping"); EntityUtil.CheckArgumentNull(config, "config"); Debug.Assert(!containerMapping.IsEmpty, "Precondition Violated: No mapping exists to generate viws for!"); //Create Cells from StorageEntityContainerMapping CellCreator cellCreator = new CellCreator(containerMapping); Listcells = cellCreator.GenerateCells(config); CqlIdentifiers identifiers = cellCreator.Identifiers; return GenerateViewsFromCells(cells, config, identifiers, containerMapping); } /// | /// Entry point for Type specific generation of Query Views /// internal static ViewGenResults GenerateTypeSpecificQueryView(StorageEntityContainerMapping containerMapping, ConfigViewGenerator config, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out bool success) { EntityUtil.CheckArgumentNull(containerMapping, "containerMapping"); EntityUtil.CheckArgumentNull(config, "config"); EntityUtil.CheckArgumentNull(entity, "entity"); EntityUtil.CheckArgumentNull(type, "type"); Debug.Assert(!type.Abstract, "Can not generate OfType/OfTypeOnly query view for and abstract type"); if (config.IsNormalTracing) { Helpers.StringTraceLine(""); Helpers.StringTraceLine("<<<<<<<< Generating Query View for Entity [" + entity.Name + "] OfType" + (includeSubtypes ? "" : "Only") + "(" + type.Name + ") >>>>>>>"); } if (containerMapping.GetEntitySetMapping(entity.Name).QueryView != null) { //Type-specific QV does not exist in the cache, but // there is a EntitySet QV. So we can't generate the view (no mapping exists for this EntitySet) // and we rely on Query to call us again to get the EntitySet View. success = false; return null; } //Compute Cell Groups or get it from Memoizer InputForComputingCellGroups args = new InputForComputingCellGroups(containerMapping, config); OutputFromComputeCellGroups result = containerMapping.GetCellgroups(args); success = result.Success; if (!success) { return null; } ListforeignKeyConstraints = result.ForeignKeyConstraints; // Get a Clone of cell groups from cache since cells are modified during viewgen, and we dont want the cached copy to change List cellGroups = cellGroups = result.CellGroups.Select(setOfcells => new CellGroup(setOfcells.Select(cell => new Cell(cell)))).ToList(); List cells = result.Cells; CqlIdentifiers identifiers = result.Identifiers; ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, config, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); success = true; //atleast we tried successfully return viewGenResults; } foreach (CellGroup cellGroup in cellGroups) { if (!DoesCellGroupContainEntitySet(cellGroup, entity)) { continue; } ViewGenerator viewGenerator = null; ErrorLog groupErrorLog = new ErrorLog(); try { viewGenerator = new ViewGenerator(cellGroup, config, foreignKeyConstraints, containerMapping); } catch (InternalMappingException exception) { // All exceptions have mapping errors in them Debug.Assert(exception.ErrorLog.Count > 0, "Incorrectly created mapping exception"); groupErrorLog = exception.ErrorLog; } if (groupErrorLog.Count > 0) { break; } Debug.Assert(viewGenerator != null); //make sure there is no exception thrown that does not add error to log ViewGenerationMode mode = includeSubtypes ? ViewGenerationMode.OfTypeViews : ViewGenerationMode.OfTypeOnlyViews; groupErrorLog = viewGenerator.GenerateQueryViewForSingleExtent(viewGenResults.Views, identifiers, entity, type, mode); if (groupErrorLog.Count != 0) { viewGenResults.AddErrors(groupErrorLog); } } success = true; return viewGenResults; } // effects: Given a list of cells in the schema, generates the query and // update mapping views for OFTYPE(Extent, Type) combinations in this schema // container. Returns a list of generated query and update views. // If it is false and some columns in a table are unmapped, an // exception is raised private static ViewGenResults GenerateViewsFromCells(List | cells, ConfigViewGenerator config, CqlIdentifiers identifiers, StorageEntityContainerMapping containerMapping) { EntityUtil.CheckArgumentNull(cells, "cells"); EntityUtil.CheckArgumentNull(config, "config"); Debug.Assert(cells.Count > 0, "There must be at least one cell in the container mapping"); // Go through each table and determine their foreign key constraints EntityContainer container = containerMapping.StorageEntityContainer; Debug.Assert(container != null); ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, config, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); return viewGenResults; } List | foreignKeyConstraints = ForeignConstraint.GetForeignConstraints(container); CellPartitioner partitioner = new CellPartitioner(cells, foreignKeyConstraints); List cellGroups = partitioner.GroupRelatedCells(); foreach (CellGroup cellGroup in cellGroups) { ViewGenerator viewGenerator = null; ErrorLog groupErrorLog = new ErrorLog(); try { viewGenerator = new ViewGenerator(cellGroup, config, foreignKeyConstraints, containerMapping); } catch (InternalMappingException exception) { // All exceptions have mapping errors in them Debug.Assert(exception.ErrorLog.Count > 0, "Incorrectly created mapping exception"); groupErrorLog = exception.ErrorLog; } if (groupErrorLog.Count == 0) { Debug.Assert(viewGenerator != null); groupErrorLog = viewGenerator.GenerateAllBidirectionalViews(viewGenResults.Views, identifiers); } if (groupErrorLog.Count != 0) { viewGenResults.AddErrors(groupErrorLog); } } // We used to print the errors here. Now we trace them as they are being thrown //if (viewGenResults.HasErrors && config.IsViewTracing) { // Helpers.StringTraceLine(viewGenResults.ErrorsToString()); //} return viewGenResults; } // effects: Given a container, ensures that all entity/association // sets in container on the C-side have been mapped private static ErrorLog EnsureAllCSpaceContainerSetsAreMapped(IEnumerable cells, ConfigViewGenerator config, StorageEntityContainerMapping containerMapping) { Set | mappedExtents = new Set (); string mslFileLocation = null; EntityContainer container = null; // Determine the container and name of the file while determining // the set of mapped extents in the cells foreach (Cell cell in cells) { mappedExtents.Add(cell.CQuery.Extent); mslFileLocation = cell.CellLabel.SourceLocation; // All cells are from the same container container = cell.CQuery.Extent.EntityContainer; } Debug.Assert(container != null); List missingExtents = new List (); // Go through all the extents in the container and determine // extents that are missing foreach (EntitySetBase extent in container.BaseEntitySets) { if (mappedExtents.Contains(extent) == false && !(containerMapping.HasQueryViewForSetMap(extent.Name))) { AssociationSet associationSet = extent as AssociationSet; if (associationSet==null || !associationSet.ElementType.IsForeignKey) { missingExtents.Add(extent); } } } ErrorLog errorLog = new ErrorLog(); // If any extent is not mapped, add an error if (missingExtents.Count > 0) { StringBuilder extentBuilder = new StringBuilder(); bool isFirst = true; foreach (EntitySetBase extent in missingExtents) { if (isFirst == false) { extentBuilder.Append(", "); } isFirst = false; extentBuilder.Append(extent.Name); } string message = System.Data.Entity.Strings.ViewGen_Missing_Set_Mapping_0(extentBuilder); // Find the cell with smallest line number - so that we can // point to the beginning of the file int lowestLineNum = -1; Cell smallestCell = null; foreach (Cell cell in cells) { if (lowestLineNum == -1 || cell.CellLabel.StartLineNumber < lowestLineNum) { smallestCell = cell; lowestLineNum = cell.CellLabel.StartLineNumber; } } Debug.Assert(smallestCell != null && lowestLineNum >= 0); EdmSchemaError edmSchemaError = new EdmSchemaError(message, (int)ViewGenErrorCode.MissingExtentMapping, EdmSchemaErrorSeverity.Error, containerMapping.SourceLocation, containerMapping.StartLineNumber, containerMapping.StartLinePosition, null); ErrorLog.Record record = new ErrorLog.Record(edmSchemaError); errorLog.AddEntry(record); } return errorLog; } #region Static Helpers private static bool DoesCellGroupContainEntitySet(CellGroup group, EntitySetBase entity) { foreach (Cell cell in group) { if (cell.GetLeftQuery(ViewTarget.QueryView).Extent.Equals(entity)) { return true; } } return false; } #endregion internal override void ToCompactString(StringBuilder builder) { } } } // 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
- StorageSetMapping.cs
- TextEditorThreadLocalStore.cs
- TextDecorationUnitValidation.cs
- PagePropertiesChangingEventArgs.cs
- CompilerCollection.cs
- Selector.cs
- DataPagerField.cs
- TextDecorationUnitValidation.cs
- WebServicesDescriptionAttribute.cs
- StylusDevice.cs
- ListDictionaryInternal.cs
- AppDomain.cs
- RotateTransform3D.cs
- DataKeyCollection.cs
- EntityParameterCollection.cs
- XmlSerializerSection.cs
- DispatcherEventArgs.cs
- ViewStateModeByIdAttribute.cs
- DateBoldEvent.cs
- OracleCommand.cs
- CompositeFontFamily.cs
- ScaleTransform.cs
- BamlReader.cs
- PropertyDescriptor.cs
- WebPartConnectionCollection.cs
- PersianCalendar.cs
- DataGridViewColumnTypeEditor.cs
- SerializableTypeCodeDomSerializer.cs
- MessageQueueEnumerator.cs
- HttpInputStream.cs
- URIFormatException.cs
- ConsoleCancelEventArgs.cs
- SortFieldComparer.cs
- XpsFilter.cs
- BaseDataListActionList.cs
- dtdvalidator.cs
- LogicalMethodInfo.cs
- StorageMappingItemCollection.cs
- PageParserFilter.cs
- ObjectDataSourceDesigner.cs
- FilterException.cs
- CompositeCollection.cs
- TcpActivation.cs
- DataGridCaption.cs
- CacheRequest.cs
- StringArrayEditor.cs
- Stylesheet.cs
- ScrollContentPresenter.cs
- DesignTimeParseData.cs
- Vector3D.cs
- ObjectQueryState.cs
- DPAPIProtectedConfigurationProvider.cs
- WebMessageEncodingElement.cs
- ToolboxSnapDragDropEventArgs.cs
- CustomPeerResolverService.cs
- OutputCacheSection.cs
- BitmapEffectInput.cs
- BindingExpressionBase.cs
- ControllableStoryboardAction.cs
- RuntimeEnvironment.cs
- XmlSchemaDatatype.cs
- BooleanFacetDescriptionElement.cs
- WindowsIPAddress.cs
- Win32Native.cs
- DateTimePickerDesigner.cs
- PopOutPanel.cs
- AvTraceFormat.cs
- AppDomainFactory.cs
- ScrollViewer.cs
- LocalizationComments.cs
- EventListener.cs
- DrawingAttributeSerializer.cs
- WebSysDisplayNameAttribute.cs
- SchemaTableOptionalColumn.cs
- RelationalExpressions.cs
- SystemIPInterfaceProperties.cs
- RelationshipConstraintValidator.cs
- LinqDataSourceEditData.cs
- PackageRelationshipCollection.cs
- WorkflowMarkupSerializationException.cs
- EventlogProvider.cs
- HtmlElementCollection.cs
- LifetimeServices.cs
- FieldDescriptor.cs
- HotSpot.cs
- InstanceKeyCollisionException.cs
- XmlSchemaSimpleTypeRestriction.cs
- TemplateKey.cs
- TitleStyle.cs
- PropertyGridCommands.cs
- TableCellCollection.cs
- ReturnEventArgs.cs
- ProgressBarBrushConverter.cs
- GeneralTransformGroup.cs
- ServiceOperationParameter.cs
- DataKeyArray.cs
- IdnElement.cs
- MarkupCompiler.cs
- PeerNameRecord.cs
- HttpConfigurationSystem.cs