Пример #1
0
        private void AnalyzeDataType(StatementGrammarNode effectiveTerminal)
        {
            var dataTypeNode = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.Expression), NonTerminals.DataType);

            if (dataTypeNode == null)
            {
                return;
            }

            ReferenceIdentifier = BuildReferenceIdentifier(dataTypeNode.GetDescendants(Terminals.SchemaIdentifier, Terminals.DataTypeIdentifier).ToArray());
        }
Пример #2
0
        private void AnalyzeQueryTableExpression(StatementGrammarNode effectiveTerminal)
        {
            var queryTableExpression = effectiveTerminal.GetPathFilterAncestor(n => !n.Id.In(NonTerminals.InnerTableReference, NonTerminals.QueryBlock), NonTerminals.QueryTableExpression);

            if (queryTableExpression == null)
            {
                return;
            }

            var identifiers = queryTableExpression.GetPathFilterDescendants(n => !n.Id.In(NonTerminals.Expression, NonTerminals.NestedQuery), Terminals.SchemaIdentifier, Terminals.ObjectIdentifier, Terminals.Identifier).ToArray();

            ReferenceIdentifier = BuildReferenceIdentifier(identifiers);
        }
Пример #3
0
        private void AnalyzePlSqlReference(StatementGrammarNode effectiveTerminal)
        {
            var plSqlIdentifiers          = new Stack <StatementGrammarNode>();
            var parentNode                = effectiveTerminal.ParentNode;
            var plSqlCompletion           = PlSqlCompletion.None;
            var plSqlAssignmentTargetNode = effectiveTerminal.GetAncestor(NonTerminals.PlSqlAssignmentTarget);

            if (plSqlAssignmentTargetNode != null)
            {
                plSqlIdentifiers.PushMany(GetPlSqlIdentifiers(plSqlAssignmentTargetNode[Terminals.PlSqlIdentifier]));
                plSqlCompletion = PlSqlCompletion.Function | PlSqlCompletion.Procedure;
            }
            else if (String.Equals(parentNode.ParentNode?.Id, NonTerminals.PlSqlProcedureCall))
            {
                var prefixNode = parentNode[NonTerminals.Prefix];
                if (prefixNode != null)
                {
                    var schemaIdentifierTerminal = prefixNode[NonTerminals.SchemaPrefix, Terminals.SchemaIdentifier];
                    plSqlIdentifiers.PushIfNotNull(schemaIdentifierTerminal);
                    var objectIdentifierTerminal = prefixNode[NonTerminals.ObjectPrefix, Terminals.ObjectIdentifier];
                    plSqlIdentifiers.PushIfNotNull(objectIdentifierTerminal);
                }

                var identifierTerminal = parentNode[Terminals.Identifier];
                plSqlIdentifiers.PushIfNotNull(identifierTerminal);
                plSqlCompletion = PlSqlCompletion.Procedure;
            }

            if (plSqlIdentifiers.Count == 0 || plSqlIdentifiers.Count > 3)
            {
                return;
            }

            PlSqlCompletion = plSqlCompletion;

            ReferenceIdentifier =
                new ReferenceIdentifier
            {
                Identifier       = plSqlIdentifiers.PopIfNotEmpty(),
                ObjectIdentifier = plSqlIdentifiers.PopIfNotEmpty(),
                SchemaIdentifier = plSqlIdentifiers.PopIfNotEmpty(),
                CursorPosition   = CursorPosition
            };
        }
Пример #4
0
        private void AnalyzePrefixedColumnReference(StatementGrammarNode effectiveTerminal)
        {
            IsNewExpressionWithInvalidGrammar = String.Equals(effectiveTerminal.Id, Terminals.SchemaIdentifier) && effectiveTerminal.FollowingTerminal != null && String.Equals(effectiveTerminal.FollowingTerminal.Id, Terminals.ObjectIdentifier);
            var isObjectAlias = String.Equals(effectiveTerminal.Id, Terminals.ObjectAlias);

            if (IsNewExpressionWithInvalidGrammar || isObjectAlias)
            {
                ReferenceIdentifier =
                    new ReferenceIdentifier
                {
                    Identifier     = effectiveTerminal,
                    CursorPosition = CursorPosition
                };

                ResolveCurrentTerminalValue(effectiveTerminal);

                return;
            }

            var prefixedColumnReference = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.Expression), NonTerminals.PrefixedColumnReference);
            var prefix     = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.Expression) && !String.Equals(n.Id, NonTerminals.AliasedExpressionOrAllTableColumns), NonTerminals.Prefix);
            var lookupNode = prefixedColumnReference ?? prefix;

            if (lookupNode == null && effectiveTerminal.Id.In(Terminals.Asterisk, Terminals.User, Terminals.SystemDate, Terminals.Level, Terminals.RowIdPseudocolumn, Terminals.Null))
            {
                lookupNode = effectiveTerminal.ParentNode;
            }

            if (lookupNode == null && effectiveTerminal.Id.IsIdentifier())
            {
                lookupNode = effectiveTerminal.ParentNode;
            }

            if (lookupNode == null)
            {
                return;
            }

            var identifiers = lookupNode.GetPathFilterDescendants(n => !String.Equals(n.Id, NonTerminals.Expression) && !String.Equals(n.Id, NonTerminals.AliasedExpressionOrAllTableColumns), Terminals.SchemaIdentifier, Terminals.ObjectIdentifier, Terminals.Identifier, Terminals.BindVariableIdentifier, Terminals.Asterisk, Terminals.User, Terminals.SystemDate, Terminals.Level, Terminals.RowIdPseudocolumn, Terminals.Null).ToList();

            ReferenceIdentifier = BuildReferenceIdentifier(identifiers);
        }
Пример #5
0
        public OracleCodeCompletionType(SqlDocumentRepository documentRepository, string statementText, int cursorPosition)
        {
            CursorPosition = cursorPosition;

            Statement = (OracleStatement)(documentRepository.Statements.GetStatementAtPosition(cursorPosition) ?? documentRepository.Statements.LastOrDefault());
            if (Statement == null)
            {
                return;
            }

            if (Statement.TerminatorNode != null && Statement.TerminatorNode.SourcePosition.IndexStart < cursorPosition)
            {
                return;
            }

            var nearestTerminal = Statement.GetNearestTerminalToPosition(cursorPosition);

            if (nearestTerminal == null)
            {
                return;
            }

            var precedingTerminal = nearestTerminal.PrecedingTerminal;

            InComment = Statement.Comments.Any(c => c.SourcePosition.ContainsIndex(cursorPosition));

            if (!documentRepository.ValidationModels.TryGetValue(Statement, out IValidationModel validationModel))
            {
                return;
            }

            SemanticModel = (OracleStatementSemanticModel)validationModel.SemanticModel;

            var requiredOffsetAfterToken = nearestTerminal.Id.IsZeroOffsetTerminalId() ? 0 : 1;
            var isCursorAfterToken       = nearestTerminal.SourcePosition.IndexEnd + requiredOffsetAfterToken < cursorPosition;
            var atAdHocTemporaryTerminal = false;

            if (isCursorAfterToken)
            {
                var unparsedTextBetweenTokenAndCursor           = statementText.Substring(nearestTerminal.SourcePosition.IndexEnd + 1, cursorPosition - nearestTerminal.SourcePosition.IndexEnd - 1);
                var unparsedEndTrimmedTextBetweenTokenAndCursor = unparsedTextBetweenTokenAndCursor.TrimEnd();

                OracleToken[] extraUnparsedTokens;
                using (var tokenReader = OracleTokenReader.Create(unparsedEndTrimmedTextBetweenTokenAndCursor))
                {
                    extraUnparsedTokens = tokenReader.GetTokens(true).ToArray();
                }

                if (extraUnparsedTokens.Length > 0)
                {
                    TerminalCandidates = OracleSqlParser.Instance.GetTerminalCandidates(nearestTerminal);
                    if (TerminalCandidates.Count == 0 || extraUnparsedTokens.Length > 1 || unparsedEndTrimmedTextBetweenTokenAndCursor.Length < unparsedTextBetweenTokenAndCursor.Length)
                    {
                        InUnparsedData = true;
                        return;
                    }
                }

                TerminalValueUnderCursor = extraUnparsedTokens.FirstOrDefault().Value;

                if (TerminalValueUnderCursor != null)
                {
                    TerminalValuePartUntilCaret = TerminalValueUnderCursor;
                    precedingTerminal           = nearestTerminal;
                    nearestTerminal             = CurrentTerminal = new StatementGrammarNode(NodeType.Terminal, Statement, new OracleToken(TerminalValueUnderCursor, cursorPosition - TerminalValuePartUntilCaret.Length));
                    precedingTerminal.ParentNode.Clone().AddChildNodes(nearestTerminal);
                    atAdHocTemporaryTerminal = true;

                    nearestTerminal.Id = nearestTerminal.Token.Value[0] == '"'
                                                ? Terminals.Identifier
                                                : GetIdentifierCandidate();

                    if (nearestTerminal.Id != null)
                    {
                        ReferenceIdentifier = BuildReferenceIdentifier(nearestTerminal.ParentNode.GetDescendants(Terminals.SchemaIdentifier, Terminals.ObjectIdentifier, Terminals.Identifier).ToArray());
                    }

                    if (!String.IsNullOrEmpty(TerminalValueUnderCursor) && nearestTerminal.SourcePosition.ContainsIndex(cursorPosition))
                    {
                        isCursorAfterToken = false;
                    }

                    TerminalValueUnderCursor = TerminalValueUnderCursor.Trim('"');
                }
            }
            else
            {
                CurrentTerminal = nearestTerminal;
                ResolveCurrentTerminalValue(nearestTerminal);
            }

            var effectiveTerminal = Statement.GetNearestTerminalToPosition(cursorPosition, n => !n.Id.In(Terminals.LeftParenthesis, Terminals.RightParenthesis, Terminals.Comma, Terminals.Semicolon)) ?? nearestTerminal;

            CurrentQueryBlock = SemanticModel.GetQueryBlock(effectiveTerminal);

            AnalyzeObjectReferencePrefixes(effectiveTerminal);
            var isCursorAfterEffectiveTerminal = cursorPosition > effectiveTerminal.SourcePosition.IndexEnd + 1;

            if (precedingTerminal == null && nearestTerminal != Statement.RootNode.FirstTerminalNode)
            {
                precedingTerminal = nearestTerminal;
            }

            var isCursorTouchingTwoTerminals = nearestTerminal.SourcePosition.IndexStart == cursorPosition && precedingTerminal != null && precedingTerminal.SourcePosition.IndexEnd + 1 == cursorPosition;

            if (isCursorTouchingTwoTerminals && !String.Equals(nearestTerminal.Id, Terminals.Identifier))
            {
                IsCursorTouchingIdentifier = String.Equals(precedingTerminal.Id, Terminals.Identifier);
                EffectiveTerminal          = precedingTerminal;
            }
            else
            {
                EffectiveTerminal = nearestTerminal;
            }

            var isColon = String.Equals(EffectiveTerminal.Id, Terminals.Colon);

            if (!String.Equals(Statement.RootNode.Id, NonTerminals.CreatePlSqlStatement))
            {
                BindVariable  = isColon && (String.Equals(EffectiveTerminal.ParentNode.Id, NonTerminals.AssignmentTriggerReferenceTarget) || String.Equals(EffectiveTerminal.ParentNode.Id, NonTerminals.BindVariableExpression));
                BindVariable |= String.Equals(EffectiveTerminal.Id, Terminals.BindVariableIdentifier);
            }

            var terminalCandidateSourceToken = isCursorAfterToken || isColon ? nearestTerminal : precedingTerminal;

            if (nearestTerminal.Id.In(Terminals.RightParenthesis, Terminals.Comma, Terminals.Dot, Terminals.Semicolon) && isCursorTouchingTwoTerminals && precedingTerminal.Id.IsIdentifier())
            {
                terminalCandidateSourceToken = precedingTerminal.PrecedingTerminal;
                ResolveCurrentTerminalValue(precedingTerminal);
            }

            if (TerminalCandidates == null)
            {
                TerminalCandidates = OracleSqlParser.Instance.GetTerminalCandidates(terminalCandidateSourceToken);
            }

            InSelectList = (atAdHocTemporaryTerminal ? precedingTerminal : EffectiveTerminal).GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.QueryBlock), NonTerminals.SelectList) != null;

            var invalidGrammarFilteredNearestTerminal = Statement.GetNearestTerminalToPosition(cursorPosition, n => !String.Equals(n.Id, Terminals.ObjectAlias));
            var isWithinExplicitPartitionClause       = invalidGrammarFilteredNearestTerminal != null && String.Equals(invalidGrammarFilteredNearestTerminal.ParentNode.Id, NonTerminals.PartitionNameOrKeySet) && (invalidGrammarFilteredNearestTerminal != nearestTerminal || TerminalCandidates.Contains(Terminals.ObjectIdentifier));

            ExplicitPartition    = isWithinExplicitPartitionClause && String.Equals(invalidGrammarFilteredNearestTerminal.ParentNode.ParentNode.FirstTerminalNode.Id, Terminals.Partition);
            ExplicitSubPartition = isWithinExplicitPartitionClause && String.Equals(invalidGrammarFilteredNearestTerminal.ParentNode.ParentNode.FirstTerminalNode.Id, Terminals.Subpartition);
            if (isWithinExplicitPartitionClause)
            {
                if (String.Equals(EffectiveTerminal.Id, Terminals.ObjectIdentifier))
                {
                    ReferenceIdentifier = BuildReferenceIdentifier(new[] { EffectiveTerminal });
                }

                if (invalidGrammarFilteredNearestTerminal != nearestTerminal)
                {
                    EffectiveTerminal           = invalidGrammarFilteredNearestTerminal;
                    TerminalValuePartUntilCaret = null;
                }
            }

            if (!isWithinExplicitPartitionClause)
            {
                ResolveSuggestedKeywords();
            }

            var isCursorBetweenTwoTerminalsWithPrecedingIdentifierWithoutPrefix = IsCursorTouchingIdentifier && !ReferenceIdentifier.HasObjectIdentifier;

            Schema =
                TerminalCandidates.Contains(Terminals.SchemaIdentifier) ||
                (String.Equals(EffectiveTerminal.Id, Terminals.SchemaIdentifier) && String.Equals(nearestTerminal.Id, Terminals.Dot));

            var isCurrentClauseSupported =
                EffectiveTerminal.IsWithinSelectClauseOrExpression() ||
                EffectiveTerminal.ParentNode.Id.In(NonTerminals.WhereClause, NonTerminals.GroupByClause, NonTerminals.HavingClause, NonTerminals.OrderByClause) ||
                EffectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.NestedQuery), NonTerminals.WhereClause) != null;

            var isCandidateIdentifier = TerminalCandidates.Contains(Terminals.Identifier);

            if (isCurrentClauseSupported)
            {
                SchemaProgram = Column = isCandidateIdentifier || isCursorBetweenTwoTerminalsWithPrecedingIdentifierWithoutPrefix;

                var functionParameterOptionalExpression = EffectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.OptionalParameterExpressionList), NonTerminals.OptionalParameterExpression);
                if (functionParameterOptionalExpression != null)
                {
                    var functionParameterExpression = functionParameterOptionalExpression[NonTerminals.Expression];
                    SpecialFunctionParameter = functionParameterExpression != null && functionParameterExpression.TerminalCount == 1 && (functionParameterExpression.FirstTerminalNode.Id.IsLiteral() || String.Equals(functionParameterExpression.FirstTerminalNode.Id, Terminals.Identifier));
                }
            }

            DatabaseLink = TerminalCandidates.Contains(Terminals.DatabaseLinkIdentifier);
            JoinType     = (!isCursorTouchingTwoTerminals || nearestTerminal.Id.In(Terminals.Comma, Terminals.RightParenthesis)) && !isWithinExplicitPartitionClause && TerminalCandidates.Contains(Terminals.Join);
            DataType     = TerminalCandidates.Contains(Terminals.DataTypeIdentifier);

            InQueryBlockFromClause = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.QueryBlock), NonTerminals.FromClause) != null || (effectiveTerminal.Id == Terminals.From && effectiveTerminal.ParentNode.Id == NonTerminals.QueryBlock);
            var isWithinJoinCondition         = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.JoinClause) && !String.Equals(n.Id, NonTerminals.NestedQuery), NonTerminals.JoinColumnsOrCondition) != null;
            var isAfterUpdateOrDeleteTerminal = (nearestTerminal.Id.In(Terminals.Update, Terminals.Delete) || (String.Equals(nearestTerminal.Id, Terminals.From) && nearestTerminal.PrecedingTerminal != null && String.Equals(nearestTerminal.PrecedingTerminal.Id, Terminals.Delete))) && isCursorAfterToken;
            var isWithinQueryBlock            = nearestTerminal.GetAncestor(NonTerminals.QueryBlock) != null;
            var isWithinMainObjectReference   = nearestTerminal.GetAncestor(NonTerminals.TableReference) != null && !isWithinQueryBlock;
            var isInInsertIntoTableReference  = nearestTerminal.GetPathFilterAncestor(NodeFilters.BreakAtNestedQueryBlock, NonTerminals.DmlTableExpressionClause) != null ||
                                                (nearestTerminal.GetPathFilterAncestor(NodeFilters.BreakAtNestedQueryBlock, NonTerminals.InsertIntoClause) != null && nearestTerminal.Id == Terminals.Into && isCursorAfterToken);

            SchemaDataObject = (InQueryBlockFromClause || isAfterUpdateOrDeleteTerminal || isWithinMainObjectReference || isInInsertIntoTableReference) && !isWithinJoinCondition && !isWithinExplicitPartitionClause && TerminalCandidates.Contains(Terminals.ObjectIdentifier);

            var isWithinJoinClause = effectiveTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.FromClause), NonTerminals.JoinClause) != null;

            JoinCondition = isWithinJoinClause && isCursorAfterEffectiveTerminal && (TerminalCandidates.Contains(Terminals.On) || String.Equals(nearestTerminal.Id, Terminals.On));

            var isWithinSelectList = (String.Equals(nearestTerminal.Id, Terminals.Select) && isCursorAfterToken) || nearestTerminal.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.QueryBlock), NonTerminals.SelectList) != null;

            AllColumns = isWithinSelectList && TerminalCandidates.Contains(Terminals.Asterisk);

            SchemaDataObjectReference = !InQueryBlockFromClause && (TerminalCandidates.Contains(Terminals.ObjectIdentifier) || isCursorBetweenTwoTerminalsWithPrecedingIdentifierWithoutPrefix);

            PackageFunction = !String.IsNullOrEmpty(ReferenceIdentifier.ObjectIdentifierOriginalValue) && (isCandidateIdentifier || isCursorTouchingTwoTerminals);

            var inMainQueryBlockOrMainObjectReference = CurrentQueryBlock == SemanticModel.MainQueryBlock || (CurrentQueryBlock == null && SemanticModel.MainObjectReferenceContainer.MainObjectReference != null);

            Sequence = inMainQueryBlockOrMainObjectReference && (nearestTerminal.IsWithinSelectClause() || !nearestTerminal.IsWithinExpression() || nearestTerminal.GetPathFilterAncestor(n => n.Id != NonTerminals.QueryBlock, NonTerminals.InsertValuesClause) != null);

            var isWithinUpdateSetNonTerminal         = String.Equals(nearestTerminal.ParentNode.Id, NonTerminals.PrefixedIdentifier) || nearestTerminal.GetPathFilterAncestor(NodeFilters.BreakAtNestedQueryBlock, NonTerminals.SetColumnListEqualsNestedQuery) != null;
            var isAfterSetTerminal                   = isCursorAfterToken && String.Equals(nearestTerminal.Id, Terminals.Set);
            var isAfterCommaInChainedUpdateSetClause = isCursorAfterToken && String.Equals(nearestTerminal.Id, Terminals.Comma) && String.Equals(nearestTerminal.ParentNode.Id, NonTerminals.UpdateSetColumnOrColumnListChainedList);

            UpdateSetColumn = isCandidateIdentifier && (isWithinUpdateSetNonTerminal || isAfterSetTerminal || isAfterCommaInChainedUpdateSetClause);
            var columnList = nearestTerminal.GetAncestor(NonTerminals.ParenthesisEnclosedPrefixedIdentifierList);

            InsertIntoColumn = isCandidateIdentifier && String.Equals(columnList?.ParentNode?.Id, NonTerminals.InsertIntoClause);

            ColumnAlias = Column && nearestTerminal.IsWithinOrderByClause();
        }