private ErrorLog GenerateDirectionalViews( ViewTarget viewTarget, CqlIdentifiers identifiers, KeyToListMap <EntitySetBase, GeneratedView> views) { bool flag = viewTarget == ViewTarget.QueryView; KeyToListMap <EntitySetBase, Cell> keyToListMap = ViewGenerator.GroupCellsByExtent((IEnumerable <Cell>) this.m_cellGroup, viewTarget); ErrorLog errorLog = new ErrorLog(); foreach (EntitySetBase key in keyToListMap.Keys) { if (this.m_config.IsViewTracing) { Helpers.StringTraceLine(string.Empty); Helpers.StringTraceLine(string.Empty); Helpers.FormatTraceLine("================= Generating {0} View for: {1} ===========================", flag ? (object)"Query" : (object)"Update", (object)key.Name); Helpers.StringTraceLine(string.Empty); Helpers.StringTraceLine(string.Empty); } try { QueryRewriter directionalViewsForExtent = this.GenerateDirectionalViewsForExtent(viewTarget, key, identifiers, views); if (viewTarget == ViewTarget.UpdateView) { if (this.m_config.IsValidationEnabled) { if (this.m_config.IsViewTracing) { Helpers.StringTraceLine(string.Empty); Helpers.StringTraceLine(string.Empty); Helpers.FormatTraceLine("----------------- Validation for generated update view for: {0} -----------------", (object)key.Name); Helpers.StringTraceLine(string.Empty); Helpers.StringTraceLine(string.Empty); } new RewritingValidator(directionalViewsForExtent.ViewgenContext, directionalViewsForExtent.BasicView).Validate(); } } } catch (InternalMappingException ex) { errorLog.Merge(ex.ErrorLog); } } return(errorLog); }
private QueryRewriter GenerateViewsForExtentAndType( EdmType generatedType, ViewgenContext context, CqlIdentifiers identifiers, KeyToListMap <EntitySetBase, GeneratedView> views, ViewGenMode mode) { QueryRewriter queryRewriter = new QueryRewriter(generatedType, context, mode); queryRewriter.GenerateViewComponents(); CellTreeNode basicView = queryRewriter.BasicView; if (this.m_config.IsNormalTracing) { Helpers.StringTrace("Basic View: "); Helpers.StringTraceLine(basicView.ToString()); } CellTreeNode simplifiedView = ViewGenerator.GenerateSimplifiedView(basicView, queryRewriter.UsedCells); if (this.m_config.IsNormalTracing) { Helpers.StringTraceLine(string.Empty); Helpers.StringTrace("Simplified View: "); Helpers.StringTraceLine(simplifiedView.ToString()); } CqlGenerator cqlGenerator = new CqlGenerator(simplifiedView, queryRewriter.CaseStatements, identifiers, context.MemberMaps.ProjectedSlotMap, queryRewriter.UsedCells.Count, queryRewriter.TopLevelWhereClause, this.m_entityContainerMapping.StorageMappingItemCollection); string eSQL; DbQueryCommandTree commandTree; if (this.m_config.GenerateEsql) { eSQL = cqlGenerator.GenerateEsql(); commandTree = (DbQueryCommandTree)null; } else { eSQL = (string)null; commandTree = cqlGenerator.GenerateCqt(); } GeneratedView generatedView = GeneratedView.CreateGeneratedView(context.Extent, generatedType, commandTree, eSQL, this.m_entityContainerMapping.StorageMappingItemCollection, this.m_config); views.Add(context.Extent, generatedView); return(queryRewriter); }
private static ViewGenResults GenerateViewsFromCells( List <Cell> cells, ConfigViewGenerator config, CqlIdentifiers identifiers, EntityContainerMapping containerMapping) { EntityContainer storageEntityContainer = containerMapping.StorageEntityContainer; ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog errorLog1 = ViewgenGatekeeper.EnsureAllCSpaceContainerSetsAreMapped((IEnumerable <Cell>)cells, containerMapping); if (errorLog1.Count > 0) { viewGenResults.AddErrors(errorLog1); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); return(viewGenResults); } List <ForeignConstraint> foreignConstraints = ForeignConstraint.GetForeignConstraints(storageEntityContainer); foreach (Set <Cell> groupRelatedCell in new CellPartitioner((IEnumerable <Cell>)cells, (IEnumerable <ForeignConstraint>)foreignConstraints).GroupRelatedCells()) { ViewGenerator viewGenerator = (ViewGenerator)null; ErrorLog errorLog2 = new ErrorLog(); try { viewGenerator = new ViewGenerator(groupRelatedCell, config, foreignConstraints, containerMapping); } catch (InternalMappingException ex) { errorLog2 = ex.ErrorLog; } if (errorLog2.Count == 0) { errorLog2 = viewGenerator.GenerateAllBidirectionalViews(viewGenResults.Views, identifiers); } if (errorLog2.Count != 0) { viewGenResults.AddErrors(errorLog2); } } return(viewGenResults); }
// <summary> // Entry point for Type specific generation of Query Views // </summary> internal static ViewGenResults GenerateTypeSpecificQueryView( EntityContainerMapping containerMapping, ConfigViewGenerator config, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out bool success) { DebugCheck.NotNull(containerMapping); DebugCheck.NotNull(config); DebugCheck.NotNull(entity); DebugCheck.NotNull(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 var args = new InputForComputingCellGroups(containerMapping, config); var result = containerMapping.GetCellgroups(args); success = result.Success; if (!success) { return null; } var foreignKeyConstraints = 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 var cellGroups = result.CellGroups.Select(setOfcells => new CellGroup(setOfcells.Select(cell => new Cell(cell)))).ToList(); var cells = result.Cells; var identifiers = result.Identifiers; var viewGenResults = new ViewGenResults(); var tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); success = true; //atleast we tried successfully return viewGenResults; } foreach (var cellGroup in cellGroups) { if (!DoesCellGroupContainEntitySet(cellGroup, entity)) { continue; } ViewGenerator viewGenerator = null; var 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 var mode = includeSubtypes ? ViewGenMode.OfTypeViews : ViewGenMode.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<Cell> cells, ConfigViewGenerator config, CqlIdentifiers identifiers, EntityContainerMapping containerMapping) { DebugCheck.NotNull(cells); DebugCheck.NotNull(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 var container = containerMapping.StorageEntityContainer; Debug.Assert(container != null); var viewGenResults = new ViewGenResults(); var tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); return viewGenResults; } var foreignKeyConstraints = ForeignConstraint.GetForeignConstraints(container); var partitioner = new CellPartitioner(cells, foreignKeyConstraints); var cellGroups = partitioner.GroupRelatedCells(); foreach (var cellGroup in cellGroups) { ViewGenerator viewGenerator = null; var 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; }
/// <summary> /// Entry point for Type specific generation of Query Views /// </summary> internal static ViewGenResults GenerateTypeSpecificQueryView( StorageEntityContainerMapping containerMapping, ConfigViewGenerator config, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out bool success) { DebugCheck.NotNull(containerMapping); DebugCheck.NotNull(config); DebugCheck.NotNull(entity); DebugCheck.NotNull(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 var args = new InputForComputingCellGroups(containerMapping, config); var result = containerMapping.GetCellgroups(args); success = result.Success; if (!success) { return(null); } var foreignKeyConstraints = 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 var cellGroups = result.CellGroups.Select(setOfcells => new CellGroup(setOfcells.Select(cell => new Cell(cell)))).ToList(); var cells = result.Cells; var identifiers = result.Identifiers; var viewGenResults = new ViewGenResults(); var tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); success = true; //atleast we tried successfully return(viewGenResults); } foreach (var cellGroup in cellGroups) { if (!DoesCellGroupContainEntitySet(cellGroup, entity)) { continue; } ViewGenerator viewGenerator = null; var 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 var mode = includeSubtypes ? ViewGenMode.OfTypeViews : ViewGenMode.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 <Cell> cells, ConfigViewGenerator config, CqlIdentifiers identifiers, StorageEntityContainerMapping containerMapping) { DebugCheck.NotNull(cells); DebugCheck.NotNull(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 var container = containerMapping.StorageEntityContainer; Debug.Assert(container != null); var viewGenResults = new ViewGenResults(); var tmpLog = EnsureAllCSpaceContainerSetsAreMapped(cells, containerMapping); if (tmpLog.Count > 0) { viewGenResults.AddErrors(tmpLog); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); return(viewGenResults); } var foreignKeyConstraints = ForeignConstraint.GetForeignConstraints(container); var partitioner = new CellPartitioner(cells, foreignKeyConstraints); var cellGroups = partitioner.GroupRelatedCells(); foreach (var cellGroup in cellGroups) { ViewGenerator viewGenerator = null; var 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); }
internal static ViewGenResults GenerateTypeSpecificQueryView( EntityContainerMapping containerMapping, ConfigViewGenerator config, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out bool success) { 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) { success = false; return((ViewGenResults)null); } InputForComputingCellGroups args = new InputForComputingCellGroups(containerMapping, config); OutputFromComputeCellGroups cellgroups = containerMapping.GetCellgroups(args); success = cellgroups.Success; if (!success) { return((ViewGenResults)null); } List <ForeignConstraint> foreignKeyConstraints = cellgroups.ForeignKeyConstraints; List <Set <Cell> > list = cellgroups.CellGroups.Select <Set <Cell>, Set <Cell> >((Func <Set <Cell>, Set <Cell> >)(setOfcells => new Set <Cell>(setOfcells.Select <Cell, Cell>((Func <Cell, Cell>)(cell => new Cell(cell)))))).ToList <Set <Cell> >(); List <Cell> cells = cellgroups.Cells; CqlIdentifiers identifiers = cellgroups.Identifiers; ViewGenResults viewGenResults = new ViewGenResults(); ErrorLog errorLog1 = ViewgenGatekeeper.EnsureAllCSpaceContainerSetsAreMapped((IEnumerable <Cell>)cells, containerMapping); if (errorLog1.Count > 0) { viewGenResults.AddErrors(errorLog1); Helpers.StringTraceLine(viewGenResults.ErrorsToString()); success = true; return(viewGenResults); } foreach (Set <Cell> set in list) { if (ViewgenGatekeeper.DoesCellGroupContainEntitySet(set, entity)) { ViewGenerator viewGenerator = (ViewGenerator)null; ErrorLog errorLog2 = new ErrorLog(); try { viewGenerator = new ViewGenerator(set, config, foreignKeyConstraints, containerMapping); } catch (InternalMappingException ex) { errorLog2 = ex.ErrorLog; } if (errorLog2.Count <= 0) { ViewGenMode mode = includeSubtypes ? ViewGenMode.OfTypeViews : ViewGenMode.OfTypeOnlyViews; ErrorLog viewForSingleExtent = viewGenerator.GenerateQueryViewForSingleExtent(viewGenResults.Views, identifiers, entity, type, mode); if (viewForSingleExtent.Count != 0) { viewGenResults.AddErrors(viewForSingleExtent); } } else { break; } } } success = true; return(viewGenResults); }