private void AddColumnToQueryBlock(OracleQueryBlock queryBlock, string columnName) { foreach (var parentDataObjectReference in AliasCommandHelper.GetParentObjectReferences(queryBlock)) { var parentQueryBlock = parentDataObjectReference.Owner; if (parentQueryBlock.ModelReference?.MeasureExpressionList?.LastTerminalNode != null) { ExecutionContext.SegmentsToReplace.Add( new TextSegment { IndextStart = parentQueryBlock.ModelReference.MeasureExpressionList.LastTerminalNode.Id == Terminals.RightParenthesis ? parentQueryBlock.ModelReference.MeasureExpressionList.LastTerminalNode.SourcePosition.IndexStart : parentQueryBlock.ModelReference.MeasureExpressionList.LastTerminalNode.SourcePosition.IndexEnd + 1, Text = ", " + columnName }); } var isNotReferencedByAsterisk = parentQueryBlock.AsteriskColumns.Count == 0; if (isNotReferencedByAsterisk) { var lastColumn = parentQueryBlock.Columns.LastOrDefault(c => c.HasExplicitDefinition); if (lastColumn != null) { ExecutionContext.SegmentsToReplace.Add( new TextSegment { IndextStart = lastColumn.RootNode.SourcePosition.IndexEnd + 1, Text = ", " + columnName }); } } AddColumnToQueryBlock(parentQueryBlock, columnName); } }
private static int?NavigateThroughQueryBlock(OracleQueryBlock queryBlock, string normalizedColumnName) { var selectListColumn = queryBlock.Columns.SingleOrDefault(c => c.NormalizedName == normalizedColumnName); if (selectListColumn == null) { return(null); } if (!selectListColumn.IsDirectReference) { return(selectListColumn.AliasNode?.SourcePosition.IndexStart ?? selectListColumn.RootNode.SourcePosition.IndexStart); } var columnReference = selectListColumn.ColumnReferences.Single(); var objectReference = columnReference.ValidObjectReference; var isAliasedDirectReference = selectListColumn.AliasNode != columnReference.ColumnNode && !selectListColumn.IsAsterisk; if (isAliasedDirectReference || objectReference == null || objectReference.QueryBlocks.Count != 1) { var destinationNode = selectListColumn.HasExplicitDefinition ? selectListColumn.AliasNode : columnReference.ColumnNode; return(destinationNode.SourcePosition.IndexStart); } return(NavigateThroughQueryBlock(objectReference.QueryBlocks.Single(), normalizedColumnName)); }
private UnnestInlineViewCommand(ActionExecutionContext executionContext) : base(executionContext) { _parentQueryBlock = SemanticModel?.QueryBlocks .Select(qb => qb.ObjectReferences.FirstOrDefault(o => o.Type == ReferenceType.InlineView && o.QueryBlocks.Count == 1 && o.QueryBlocks.First() == CurrentQueryBlock)) .Where(o => o != null) .Select(o => o.Owner) .FirstOrDefault(); }
private static int?NavigateToColumnDefinition(OracleQueryBlock queryBlock, StatementGrammarNode terminal) { var column = queryBlock.AllColumnReferences.SingleOrDefault(c => c.ColumnNode == terminal); if (column?.ValidObjectReference == null || column.ValidObjectReference.QueryBlocks.Count != 1) { return(null); } var childQueryBlock = column.ValidObjectReference.QueryBlocks.Single(); return(NavigateThroughQueryBlock(childQueryBlock, column.NormalizedName)); }
protected OracleCommandBase(ActionExecutionContext executionContext) { ExecutionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext)); CurrentNode = executionContext.DocumentRepository.Statements.GetNodeAtPosition(executionContext.CaretOffset, CurrentNodeFilterFunction); if (CurrentNode == null) { return; } SemanticModel = (OracleStatementSemanticModel)executionContext.DocumentRepository.ValidationModels[CurrentNode.Statement].SemanticModel; CurrentQueryBlock = SemanticModel.GetQueryBlock(CurrentNode); }
private static int?FindSelectColumnIndex(OracleQueryBlock queryBlock, StatementNode terminal) { while (queryBlock != null) { var selectColumn = FindItemAndIndexIndex(queryBlock.Columns.Where(ExplicitColumnFilter), c => c.RootNode.SourcePosition.Contains(terminal.SourcePosition), out var columnIndex); if (selectColumn != null) { return(columnIndex); } queryBlock = queryBlock.FollowingConcatenatedQueryBlock; } return(null); }
private static int?NavigateToObjectDefinition(OracleQueryBlock queryBlock, StatementGrammarNode terminal) { var column = queryBlock.AllColumnReferences.SingleOrDefault(c => c.ObjectNode == terminal); var dataObjectReference = column?.ValidObjectReference as OracleDataObjectReference; if (dataObjectReference == null) { return(null); } var destinationNode = dataObjectReference.AliasNode ?? GetObjectNode(dataObjectReference); return(destinationNode.SourcePosition.IndexStart); }
public void AddColumnAlias(StatementGrammarNode columnNode, OracleQueryBlock queryBlock, string columnNormalizedName, string alias) { _executionContext.SegmentsToReplace.Add( new TextSegment { IndextStart = columnNode.SourcePosition.IndexEnd + 1, Text = " " + alias }); var parentObjectReferences = GetParentObjectReferences(queryBlock); foreach (var objectReference in parentObjectReferences) { AddColumnAliasToQueryBlock(columnNormalizedName, alias, objectReference); } }
private FindUsagesCommand(ActionExecutionContext executionContext) { if (executionContext.DocumentRepository == null || executionContext.DocumentRepository.StatementText != executionContext.StatementText) { return; } _currentNode = GetFindUsagesCompatibleTerminal(executionContext.DocumentRepository.Statements, executionContext.CaretOffset); if (_currentNode == null) { return; } _semanticModel = (OracleStatementSemanticModel)executionContext.DocumentRepository.ValidationModels[_currentNode.Statement].SemanticModel; _queryBlock = _semanticModel.GetQueryBlock(_currentNode); _executionContext = executionContext; }
private IEnumerable <OracleDataObjectReference> GetDataObjectReferences(OracleQueryBlock cteQueryBlock) { var dataObjectReferences = cteQueryBlock.SemanticModel.AllReferenceContainers .SelectMany(c => c.ObjectReferences) .Where(o => o.QueryBlocks.Count == 1 && o.QueryBlocks.First() == cteQueryBlock); if (cteQueryBlock.IsRecursive) { var recursiveDataObjectReferences = cteQueryBlock.AllFollowingConcatenatedQueryBlocks .SelectMany(c => c.ObjectReferences) .Where(o => o.OwnerNode == null && o.ObjectNode != null && String.Equals(o.ObjectNode.Token.Value.ToQuotedIdentifier(), cteQueryBlock.NormalizedAlias)); dataObjectReferences = dataObjectReferences.Concat(recursiveDataObjectReferences); } return(dataObjectReferences); }
public static OracleDataTypeReference CreateDataTypeReference(OracleQueryBlock queryBlock, OracleSelectListColumn selectListColumn, StatementPlacement placement, StatementGrammarNode typeIdentifier) { var dataTypeNode = typeIdentifier.ParentNode.ParentNode; var dataTypeReference = new OracleDataTypeReference { RootNode = dataTypeNode, Owner = queryBlock, Container = queryBlock, OwnerNode = dataTypeNode[NonTerminals.SchemaDatatype, NonTerminals.SchemaPrefix, Terminals.SchemaIdentifier], ObjectNode = typeIdentifier, DatabaseLinkNode = String.Equals(typeIdentifier.Id, Terminals.DataTypeIdentifier) ? GetDatabaseLinkFromIdentifier(typeIdentifier) : null, Placement = placement, SelectListColumn = selectListColumn }; ResolveTypeMetadata(dataTypeReference); ResolveSchemaType(dataTypeReference); return(dataTypeReference); }
private static IToolTip BuildAsteriskToolTip(OracleQueryBlock queryBlock, StatementGrammarNode asteriskTerminal) { var asteriskColumn = queryBlock.AsteriskColumns.SingleOrDefault(c => c.RootNode.LastTerminalNode == asteriskTerminal); if (asteriskColumn == null) { return(null); } var columns = queryBlock.Columns.Where(c => c.AsteriskColumn == asteriskColumn) .Select((c, i) => { var validObjectReference = c.ColumnReferences[0].ValidObjectReference; var nullable = validObjectReference.SchemaObject.GetTargetSchemaObject() is OracleView ? (bool?)null : c.ColumnDescription.Nullable; return (new OracleColumnModel { Name = String.IsNullOrEmpty(c.ColumnDescription.Name) ? OracleSelectListColumn.BuildNonAliasedOutputColumnName(c.RootNode.Terminals) : c.ColumnDescription.Name, FullTypeName = c.ColumnDescription.FullTypeName, Nullable = nullable, ColumnIndex = i + 1, RowSourceName = validObjectReference?.FullyQualifiedObjectName.ToLabel() }); }).ToArray(); return(columns.Length == 0 ? null : new ToolTipAsterisk { Columns = columns }); }
private static void AddColumnAliasNodesToReplace(ICollection <TextSegment> segmentsToReplace, OracleQueryBlock queryBlock, StatementGrammarNode currentNode) { var column = queryBlock.Columns.SingleOrDefault(c => c.HasExplicitDefinition && c.IsDirectReference && c.RootNode.Terminals.Any(t => t == currentNode)); if (column == null) { return; } foreach (var terminal in FindUsagesCommand.GetParentQueryBlockColumnUsages(column)) { segmentsToReplace.Add (new TextSegment { IndextStart = terminal.SourcePosition.IndexStart, Length = terminal.SourcePosition.Length, Text = currentNode.PrecedingTerminal.Token.Value }); } segmentsToReplace.Add( new TextSegment { IndextStart = currentNode.SourcePosition.IndexStart, Length = currentNode.SourcePosition.Length, Text = String.Empty }); }
private static bool IsCommonTableExpressionColumnReference(OracleQueryBlock cteQueryBlock, OracleColumnReference columnReference) { var objectReference = columnReference.ValidObjectReference; return(objectReference.QueryBlocks.Count == 1 && (objectReference as OracleDataObjectReference)?.AliasNode == null && objectReference.QueryBlocks.First() == cteQueryBlock); }
private static bool MatchesOwnerQueryBlock(OracleObjectWithColumnsReference objectReference, OracleQueryBlock ownerQueryBlock) { Func <OracleObjectWithColumnsReference, bool> ownerQueryBlockPredicate = r => r.QueryBlocks.Count == 1 && r.QueryBlocks.First() == ownerQueryBlock; var dataObjectReference = objectReference as OracleDataObjectReference; var matchesOwnerQueryBlock = dataObjectReference?.IncludeInnerReferences.Any(ownerQueryBlockPredicate) ?? ownerQueryBlockPredicate(objectReference); var matchesOwnSelectColumn = objectReference.Owner != null && objectReference.QueryBlocks.FirstOrDefault() == objectReference.Owner; return(matchesOwnerQueryBlock || matchesOwnSelectColumn); }
private IEnumerable <OracleReference> GetCommonTableExpressionObjectPrefixedColumnReferences(OracleQueryBlock cteQueryBlock) { if (cteQueryBlock == null) { return(Enumerable.Empty <OracleReference>()); } var sourceColumnReferences = cteQueryBlock.AllColumnReferences; if (cteQueryBlock.IsRecursive) { sourceColumnReferences = sourceColumnReferences.Concat(cteQueryBlock.AllFollowingConcatenatedQueryBlocks.SelectMany(qb => qb.AllColumnReferences)); } return(sourceColumnReferences .Where(c => c.ObjectNode != null && c.ValidObjectReference != null && c.OwnerNode == null && String.Equals(c.ValidObjectReference.FullyQualifiedObjectName.NormalizedName, cteQueryBlock.NormalizedAlias))); }
private static IEnumerable <OracleDataObjectReference> GetInlineViewObjectReferences(OracleQueryBlock queryBlock) { return(queryBlock.ObjectReferences .SelectMany(o => o.IncludeInnerReferences) .Where(o => o.QueryBlocks.Count == 1)); }
internal static IEnumerable <OracleDataObjectReference> GetParentObjectReferences(OracleQueryBlock referredQueryBlock) { return(referredQueryBlock.SemanticModel.QueryBlocks .SelectMany(GetInlineViewObjectReferences) .Where(o => o.QueryBlocks.First() == referredQueryBlock)); }
private static IEnumerable <SourcePosition> SelectQueryBlockColumns(StatementNode columnNode, OracleQueryBlock queryBlock, int?columnIndex) { if (columnIndex == null) { yield break; } var selectColumnFound = false; while (queryBlock != null) { var selectColumn = queryBlock.Columns.Where(ExplicitColumnFilter).Skip(columnIndex.Value).FirstOrDefault(); if (selectColumn != null) { selectColumnFound = true; yield return(selectColumn.RootNode.SourcePosition); } queryBlock = queryBlock.FollowingConcatenatedQueryBlock; } if (selectColumnFound && columnNode != null) { yield return(columnNode.SourcePosition); } }
private static void AddObjectAliasNodesToReplace(ICollection <TextSegment> segmentsToReplace, OracleQueryBlock queryBlock, StatementGrammarNode currentNode) { var objectReference = queryBlock.ObjectReferences.Single(o => o.AliasNode == currentNode); if (objectReference.Type == ReferenceType.InlineView) { return; } foreach (var columnReference in queryBlock.AllColumnReferences .Where(c => c.ColumnNodeObjectReferences.Count == 1 && c.ColumnNodeObjectReferences.Single() == objectReference && c.ObjectNode != null)) { segmentsToReplace.Add( new TextSegment { IndextStart = columnReference.ObjectNode.SourcePosition.IndexStart, Length = columnReference.ObjectNode.SourcePosition.Length, Text = objectReference.ObjectNode.Token.Value }); } segmentsToReplace.Add(new TextSegment { IndextStart = currentNode.SourcePosition.IndexStart, Length = currentNode.SourcePosition.Length, Text = String.Empty }); }