private static IEnumerable <TerminalUsage> GetModelClauseAndPivotTableReferences(OracleReference columnReference, bool onlyAliasOrigin) { var usages = new List <TerminalUsage>(); if (columnReference.Owner == null) { return(usages); } var normalizedName = columnReference.NormalizedName; var modelClauseAndPivotTableColumnReferences = columnReference.Owner.AllColumnReferences .Where(c => c.ColumnNodeObjectReferences.Count == 1 && c != columnReference && c.Placement.In(StatementPlacement.Model, StatementPlacement.PivotClause) && String.Equals(c.SelectListColumn?.NormalizedName, normalizedName)); foreach (var innerReference in modelClauseAndPivotTableColumnReferences) { var definition = new TerminalUsage { Terminal = innerReference.SelectListColumn.AliasNode, Option = DisplayOptions.Definition }; usages.Add(definition); if (!innerReference.SelectListColumn.IsDirectReference) { continue; } if (innerReference.SelectListColumn.HasExplicitAlias) { var usage = new TerminalUsage { Terminal = innerReference.ColumnNode, Option = DisplayOptions.Usage }; usages.Add(usage); } var objectReference = (OracleDataObjectReference)innerReference.ColumnNodeObjectReferences.First(); foreach (var innerObjectReference in objectReference.IncludeInnerReferences) { usages.AddRange(GetChildQueryBlockColumnReferences(innerObjectReference, innerReference, onlyAliasOrigin)); } } if (columnReference.Owner.ModelReference != null) { foreach (var modelColumn in columnReference.Owner.ModelReference.ColumnDefinitions.Where(c => !c.IsDirectReference && c.AliasNode != null && String.Equals(c.NormalizedName, normalizedName))) { var definition = new TerminalUsage { Terminal = modelColumn.AliasNode, Option = DisplayOptions.Definition }; usages.Add(definition); } } return(usages); }
private static IEnumerable <TerminalUsage> GetColumnUsagesInternal(OracleStatementSemanticModel semanticModel, StatementGrammarNode columnNode, bool onlyAliasOrigin) { var usages = Enumerable.Empty <TerminalUsage>(); var columnReference = semanticModel.AllReferenceContainers .SelectMany(c => c.ColumnReferences) .FirstOrDefault(c => (c.ColumnNode == columnNode || c.SelectListColumn?.AliasNode == columnNode) && c.ColumnNodeObjectReferences.Count == 1 && c.ColumnNodeColumnReferences.Count == 1); OracleSelectListColumn selectListColumn; if (columnReference != null) { var objectReference = columnReference.ColumnNodeObjectReferences.First(); var sourceColumnReferences = objectReference.Owner == null ? objectReference.Container.ColumnReferences : objectReference.Owner.AllColumnReferences; var columnReferences = sourceColumnReferences.Where(c => IsValidReference(c, columnReference, objectReference)).ToArray(); usages = columnReferences.Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); if (objectReference is OraclePivotTableReference pivotTableReference) { var pivotColumnAliases = pivotTableReference.PivotColumns .Where(c => c.AliasNode != null && String.Equals(c.NormalizedName, columnReference.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.AliasNode, Option = DisplayOptions.Definition }); usages = usages.Concat(pivotColumnAliases); } bool searchChildren; if (String.Equals(columnNode.Id, Terminals.Identifier)) { searchChildren = true; selectListColumn = columnReference.SelectListColumn; if (selectListColumn == null) { selectListColumn = columnReferences.FirstOrDefault(c => c.SelectListColumn != null && c.SelectListColumn.IsDirectReference && c.SelectListColumn.HasExplicitDefinition)?.SelectListColumn; } else if (!selectListColumn.IsDirectReference) { selectListColumn = null; } if (selectListColumn != null && !onlyAliasOrigin && selectListColumn.HasExplicitAlias) { var definition = new TerminalUsage { Terminal = selectListColumn.AliasNode, Option = DisplayOptions.Definition }; usages = usages.Concat(new[] { definition }); } } else { selectListColumn = columnReference.Owner.Columns.Single(c => c.AliasNode == columnNode); var nodeList = Enumerable.Repeat(new TerminalUsage { Terminal = selectListColumn.AliasNode, Option = DisplayOptions.Definition }, 1); searchChildren = selectListColumn.IsDirectReference; usages = searchChildren ? usages.Concat(nodeList) : nodeList; } usages = usages.Concat(GetModelClauseAndPivotTableReferences(columnReference, onlyAliasOrigin)); if (searchChildren) { usages = usages.Concat(GetChildQueryBlockColumnReferences(objectReference, columnReference, onlyAliasOrigin)); } } else { var queryBlock = semanticModel.GetQueryBlock(columnNode); selectListColumn = queryBlock.Columns.SingleOrDefault(c => c.AliasNode == columnNode || c.ExplicitAliasNode == columnNode); var usageOption = DisplayOptions.Usage; if (selectListColumn?.ExplicitAliasNode == columnNode) { usageOption = DisplayOptions.Definition; if (queryBlock.IsRecursive && queryBlock.FollowingConcatenatedQueryBlock != null) { var oracleDataObjectReference = queryBlock.FollowingConcatenatedQueryBlock.ObjectReferences.FirstOrDefault(o => o.QueryBlocks.Count == 1 && o.QueryBlocks.First() == queryBlock.FollowingConcatenatedQueryBlock); usages = queryBlock.FollowingConcatenatedQueryBlock.AllColumnReferences .Where(c => String.Equals(c.NormalizedName, selectListColumn.NormalizedName) && c.ColumnNodeColumnReferences.Count == 1 && c.ColumnNodeObjectReferences.Count == 1 && c.ColumnNodeObjectReferences.First() == oracleDataObjectReference) .Select(c => CreateStandardTerminalUsage(c.ColumnNode)); } } var usage = new TerminalUsage { Terminal = columnNode, Option = usageOption }; usages = usages.Concat(Enumerable.Repeat(usage, 1)); if (selectListColumn == null) { if (queryBlock.ModelReference != null) { selectListColumn = queryBlock.ModelReference.ColumnDefinitions.SingleOrDefault(c => c.AliasNode == columnNode); if (selectListColumn != null) { columnReference = queryBlock.ModelReference.DimensionReferenceContainer.ColumnReferences .Concat(queryBlock.ModelReference.MeasuresReferenceContainer.ColumnReferences) .Concat(queryBlock.AllColumnReferences) .FirstOrDefault(c => String.Equals(c.NormalizedName, selectListColumn.NormalizedName)); if (columnReference != null) { return(GetColumnUsagesInternal(semanticModel, columnReference.ColumnNode, onlyAliasOrigin)); } } } else { var pivotTableColumn = queryBlock.ObjectReferences.OfType <OraclePivotTableReference>() .SelectMany(pt => pt.PivotColumns.Select(c => new { PivotTable = pt, Column = c })) .SingleOrDefault(ptc => ptc.Column.AliasNode == columnNode); if (pivotTableColumn != null) { selectListColumn = pivotTableColumn.Column; var pivotUsages = queryBlock.AllColumnReferences .Where(c => c.ValidObjectReference == pivotTableColumn.PivotTable && c.HasExplicitDefinition && String.Equals(c.NormalizedName, selectListColumn.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); usages = usages.Concat(pivotUsages); } } } else { var orderByReferenceUsages = queryBlock.ColumnReferences .Where(c => c.Placement == StatementPlacement.OrderBy && c.ValidObjectReference == queryBlock.SelfObjectReference && String.Equals(c.NormalizedName, selectListColumn.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); usages = usages.Concat(orderByReferenceUsages); } } var parentUsages = GetParentQueryBlockUsages(selectListColumn); if (onlyAliasOrigin) { var columnName = columnNode.Token.Value.ToQuotedIdentifier(); parentUsages = parentUsages.TakeWhile(u => String.Equals(u.Terminal.Token.Value.ToQuotedIdentifier(), columnName)); } usages = usages.Concat(parentUsages); return(usages); }
private static IEnumerable<TerminalUsage> GetColumnUsagesInternal(OracleStatementSemanticModel semanticModel, StatementGrammarNode columnNode, bool onlyAliasOrigin) { var usages = Enumerable.Empty<TerminalUsage>(); var columnReference = semanticModel.AllReferenceContainers .SelectMany(c => c.ColumnReferences) .FirstOrDefault(c => (c.ColumnNode == columnNode || c.SelectListColumn?.AliasNode == columnNode) && c.ColumnNodeObjectReferences.Count == 1 && c.ColumnNodeColumnReferences.Count == 1); OracleSelectListColumn selectListColumn; if (columnReference != null) { var objectReference = columnReference.ColumnNodeObjectReferences.First(); var sourceColumnReferences = objectReference.Owner == null ? objectReference.Container.ColumnReferences : objectReference.Owner.AllColumnReferences; var columnReferences = sourceColumnReferences.Where(c => IsValidReference(c, columnReference, objectReference)).ToArray(); usages = columnReferences.Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); var pivotTableReference = objectReference as OraclePivotTableReference; if (pivotTableReference != null) { var pivotColumnAliases = pivotTableReference.PivotColumns .Where(c => c.AliasNode != null && String.Equals(c.NormalizedName, columnReference.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.AliasNode, Option = DisplayOptions.Definition }); usages = usages.Concat(pivotColumnAliases); } bool searchChildren; if (String.Equals(columnNode.Id, Terminals.Identifier)) { searchChildren = true; selectListColumn = columnReference.SelectListColumn; if (selectListColumn == null) { selectListColumn = columnReferences.FirstOrDefault(c => c.SelectListColumn != null && c.SelectListColumn.IsDirectReference && c.SelectListColumn.HasExplicitDefinition)?.SelectListColumn; } else if (!selectListColumn.IsDirectReference) { selectListColumn = null; } if (selectListColumn != null && !onlyAliasOrigin && selectListColumn.HasExplicitAlias) { var definition = new TerminalUsage { Terminal = selectListColumn.AliasNode, Option = DisplayOptions.Definition }; usages = usages.Concat(new[] { definition }); } } else { selectListColumn = columnReference.Owner.Columns.Single(c => c.AliasNode == columnNode); var nodeList = Enumerable.Repeat(new TerminalUsage { Terminal = selectListColumn.AliasNode, Option = DisplayOptions.Definition }, 1); searchChildren = selectListColumn.IsDirectReference; usages = searchChildren ? usages.Concat(nodeList) : nodeList; } usages = usages.Concat(GetModelClauseAndPivotTableReferences(columnReference, onlyAliasOrigin)); if (searchChildren) { usages = usages.Concat(GetChildQueryBlockColumnReferences(objectReference, columnReference, onlyAliasOrigin)); } } else { var queryBlock = semanticModel.GetQueryBlock(columnNode); selectListColumn = queryBlock.Columns.SingleOrDefault(c => c.AliasNode == columnNode || c.ExplicitAliasNode == columnNode); var usageOption = DisplayOptions.Usage; if (selectListColumn?.ExplicitAliasNode == columnNode) { usageOption = DisplayOptions.Definition; if (queryBlock.IsRecursive && queryBlock.FollowingConcatenatedQueryBlock != null) { var oracleDataObjectReference = queryBlock.FollowingConcatenatedQueryBlock.ObjectReferences.FirstOrDefault(o => o.QueryBlocks.Count == 1 && o.QueryBlocks.First() == queryBlock.FollowingConcatenatedQueryBlock); usages = queryBlock.FollowingConcatenatedQueryBlock.AllColumnReferences .Where(c => String.Equals(c.NormalizedName, selectListColumn.NormalizedName) && c.ColumnNodeColumnReferences.Count == 1 && c.ColumnNodeObjectReferences.Count == 1 && c.ColumnNodeObjectReferences.First() == oracleDataObjectReference) .Select(c => CreateStandardTerminalUsage(c.ColumnNode)); } } var usage = new TerminalUsage { Terminal = columnNode, Option = usageOption }; usages = usages.Concat(Enumerable.Repeat(usage, 1)); if (selectListColumn == null) { if (queryBlock.ModelReference != null) { selectListColumn = queryBlock.ModelReference.ColumnDefinitions.SingleOrDefault(c => c.AliasNode == columnNode); if (selectListColumn != null) { columnReference = queryBlock.ModelReference.DimensionReferenceContainer.ColumnReferences .Concat(queryBlock.ModelReference.MeasuresReferenceContainer.ColumnReferences) .Concat(queryBlock.AllColumnReferences) .FirstOrDefault(c => String.Equals(c.NormalizedName, selectListColumn.NormalizedName)); if (columnReference != null) { return GetColumnUsagesInternal(semanticModel, columnReference.ColumnNode, onlyAliasOrigin); } } } else { var pivotTableColumn = queryBlock.ObjectReferences.OfType<OraclePivotTableReference>() .SelectMany(pt => pt.PivotColumns.Select(c => new { PivotTable = pt, Column = c })) .SingleOrDefault(ptc => ptc.Column.AliasNode == columnNode); if (pivotTableColumn != null) { selectListColumn = pivotTableColumn.Column; var pivotUsages = queryBlock.AllColumnReferences .Where(c => c.ValidObjectReference == pivotTableColumn.PivotTable && c.HasExplicitDefinition && String.Equals(c.NormalizedName, selectListColumn.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); usages = usages.Concat(pivotUsages); } } } else { var orderByReferenceUsages = queryBlock.ColumnReferences .Where(c => c.Placement == StatementPlacement.OrderBy && c.ValidObjectReference == queryBlock.SelfObjectReference && String.Equals(c.NormalizedName, selectListColumn.NormalizedName)) .Select(c => new TerminalUsage { Terminal = c.ColumnNode, Option = DisplayOptions.Usage }); usages = usages.Concat(orderByReferenceUsages); } } var parentUsages = GetParentQueryBlockUsages(selectListColumn); if (onlyAliasOrigin) { var columnName = columnNode.Token.Value.ToQuotedIdentifier(); parentUsages = parentUsages.TakeWhile(u => String.Equals(u.Terminal.Token.Value.ToQuotedIdentifier(), columnName)); } usages = usages.Concat(parentUsages); return usages; }
private static IEnumerable<TerminalUsage> GetModelClauseAndPivotTableReferences(OracleReference columnReference, bool onlyAliasOrigin) { var usages = new List<TerminalUsage>(); if (columnReference.Owner == null) { return usages; } var normalizedName = columnReference.NormalizedName; var modelClauseAndPivotTableColumnReferences = columnReference.Owner.AllColumnReferences .Where(c => c.ColumnNodeObjectReferences.Count == 1 && c != columnReference && c.Placement.In(StatementPlacement.Model, StatementPlacement.PivotClause) && String.Equals(c.SelectListColumn?.NormalizedName, normalizedName)); foreach (var innerReference in modelClauseAndPivotTableColumnReferences) { var definition = new TerminalUsage { Terminal = innerReference.SelectListColumn.AliasNode, Option = DisplayOptions.Definition }; usages.Add(definition); if (!innerReference.SelectListColumn.IsDirectReference) { continue; } if (innerReference.SelectListColumn.HasExplicitAlias) { var usage = new TerminalUsage { Terminal = innerReference.ColumnNode, Option = DisplayOptions.Usage }; usages.Add(usage); } var objectReference = (OracleDataObjectReference)innerReference.ColumnNodeObjectReferences.First(); foreach (var innerObjectReference in objectReference.IncludeInnerReferences) { usages.AddRange(GetChildQueryBlockColumnReferences(innerObjectReference, innerReference, onlyAliasOrigin)); } } if (columnReference.Owner.ModelReference != null) { foreach (var modelColumn in columnReference.Owner.ModelReference.ColumnDefinitions.Where(c => !c.IsDirectReference && c.AliasNode != null && String.Equals(c.NormalizedName, normalizedName))) { var definition = new TerminalUsage { Terminal = modelColumn.AliasNode, Option = DisplayOptions.Definition }; usages.Add(definition); } } return usages; }