示例#1
0
 private static void SetStatementModelIfFound(OraclePlSqlCursorVariable cursor, StatementGrammarNode selectStatementNode)
 {
     if (selectStatementNode != null)
     {
         cursor.SemanticModel = cursor.Owner.SqlModels.SingleOrDefault(m => m.Statement.RootNode == selectStatementNode);
     }
 }
示例#2
0
        public void VisitPlSqlCursorVariable(OraclePlSqlCursorVariable cursorVariable)
        {
            if (_plSqlReference.OwnerNode != null || _plSqlReference.IdentifierNode == null)
            {
                return;
            }

            var bindVariableExpressionOrPlSqlTargetNode = _plSqlReference.RootNode.GetPathFilterAncestor(n => !String.Equals(n.Id, NonTerminals.PlSqlStatementType), NonTerminals.BindVariableExpressionOrPlSqlTarget);

            if (bindVariableExpressionOrPlSqlTargetNode != null && bindVariableExpressionOrPlSqlTargetNode.ParentNode.Id.In(NonTerminals.PlSqlOpenStatement, NonTerminals.PlSqlOpenForStatement, NonTerminals.PlSqlFetchStatement, NonTerminals.PlSqlCloseStatement))
            {
                VisitPlSqlElement(cursorVariable);
                return;
            }

            var cursorName = _plSqlReference.ObjectNode?.Token.Value.ToQuotedIdentifier();

            IsMatch =
                IsWithinScope(cursorVariable) && String.Equals(cursorVariable.Name, cursorName) &&
                (cursorVariable.SemanticModel == null || cursorVariable.SemanticModel.MainQueryBlock?.NamedColumns.Contains(_plSqlReference.NormalizedName) == true);
        }
示例#3
0
        private void ResolveLocalVariableTypeAndLabelDeclarations(OraclePlSqlProgram program)
        {
            StatementGrammarNode programSourceNode;

            switch (program.RootNode.Id)
            {
            case NonTerminals.CreateFunction:
                programSourceNode = program.RootNode[NonTerminals.PlSqlFunctionSource, NonTerminals.FunctionTypeDefinition, NonTerminals.ProgramImplentationDeclaration];
                break;

            case NonTerminals.PlSqlBlock:
                programSourceNode = program.RootNode[NonTerminals.PlSqlBlockDeclareSection];
                break;

            case NonTerminals.CreatePackageBody:
                programSourceNode = program.RootNode[NonTerminals.PackageBodyDefinition];
                break;

            default:
                programSourceNode = program.RootNode[NonTerminals.ProgramImplentationDeclaration];
                break;
            }

            ResolveProgramDeclarationLabels(program);

            var firstItemNode = programSourceNode?[NonTerminals.ProgramDeclareSection, NonTerminals.ItemList1, NonTerminals.Item1OrPragmaDefinition];

            if (firstItemNode == null)
            {
                return;
            }

            var itemOrPragmaNodes = StatementGrammarNode.GetAllChainedClausesByPath(firstItemNode, n => n.ParentNode, NonTerminals.ItemList1Chained, NonTerminals.Item1OrPragmaDefinition);

            foreach (var itemOrPragmaSwitchNode in itemOrPragmaNodes)
            {
                var itemOrPragmaNode = itemOrPragmaSwitchNode[0];
                if (String.Equals(itemOrPragmaNode.Id, NonTerminals.Item1))
                {
                    var declarationRoot = itemOrPragmaNode[0];
                    switch (declarationRoot?.Id)
                    {
                    case NonTerminals.ItemDeclaration:
                        var specificNode = declarationRoot[0];
                        if (specificNode != null)
                        {
                            OraclePlSqlVariable variable;
                            switch (specificNode.Id)
                            {
                            case NonTerminals.ConstantDeclaration:
                                variable =
                                    new OraclePlSqlVariable
                                {
                                    Owner                 = program,
                                    DefinitionNode        = specificNode,
                                    IsConstant            = true,
                                    DefaultExpressionNode = specificNode[NonTerminals.PlSqlExpression]
                                };

                                SetDataTypeAndNullablePropertiesAndAddIfIdentifierFound(variable, specificNode, program);
                                break;

                            case NonTerminals.ExceptionDeclaration:
                                var exceptionName = specificNode[Terminals.ExceptionIdentifier]?.Token.Value.ToQuotedIdentifier();
                                if (exceptionName != null)
                                {
                                    var exception =
                                        new OraclePlSqlException
                                    {
                                        Owner          = program,
                                        Name           = exceptionName,
                                        DefinitionNode = specificNode
                                    };

                                    program.Exceptions.Add(exception);
                                }

                                break;

                            case NonTerminals.VariableDeclaration:
                                specificNode = specificNode[NonTerminals.FieldDefinition];
                                if (specificNode != null)
                                {
                                    variable =
                                        new OraclePlSqlVariable
                                    {
                                        Owner                 = program,
                                        DefinitionNode        = specificNode,
                                        DefaultExpressionNode = specificNode[NonTerminals.VariableDeclarationDefaultValue, NonTerminals.PlSqlExpression],
                                    };

                                    SetDataTypeAndNullablePropertiesAndAddIfIdentifierFound(variable, specificNode, program);
                                }

                                break;
                            }
                        }

                        break;

                    case NonTerminals.TypeDefinition:
                        var typeIdentifierNode = declarationRoot[Terminals.TypeIdentifier];
                        var associativeArrayTypeDefinitionNode = declarationRoot[NonTerminals.TypeDefinitionSpecification, NonTerminals.CollectionTypeDefinition, NonTerminals.AssociativeArrayTypeDefinition];
                        if (typeIdentifierNode != null)
                        {
                            var type =
                                new OraclePlSqlType
                            {
                                Owner              = program,
                                DefinitionNode     = declarationRoot,
                                Name               = typeIdentifierNode.Token.Value.ToQuotedIdentifier(),
                                IsAssociativeArray = associativeArrayTypeDefinitionNode?[Terminals.Index] != null
                            };

                            program.Types.Add(type);

                            var associativeArrayIndexTypeNode = associativeArrayTypeDefinitionNode?[NonTerminals.AssociativeArrayIndexType];
                            if (associativeArrayIndexTypeNode != null && OracleReferenceBuilder.TryCreatePlSqlDataTypeReference(program, associativeArrayIndexTypeNode, out var dataTypeReference))
                            {
                                type.AssociativeArrayIndexDataTypeReference = dataTypeReference;
                            }
                        }

                        break;

                    case NonTerminals.CursorDefinition:
                        var identifierNode = declarationRoot[Terminals.CursorIdentifier];
                        if (identifierNode != null)
                        {
                            var cursor =
                                new OraclePlSqlCursorVariable
                            {
                                Owner          = program,
                                DefinitionNode = declarationRoot,
                                Name           = identifierNode.Token.Value.ToQuotedIdentifier()
                            };

                            SetStatementModelIfFound(cursor, declarationRoot[NonTerminals.SelectStatement]);

                            program.Variables.Add(cursor);
                        }
                        break;
                    }
                }
                else
                {
                    var exceptionInit = itemOrPragmaNode[NonTerminals.PragmaType, Terminals.ExceptionInit];
                    if (exceptionInit != null)
                    {
                        var exceptionIdentifier = exceptionInit.ParentNode[Terminals.ExceptionIdentifier];
                        var errorCodeModifier   = exceptionInit.ParentNode[Terminals.MathMinus] == null ? 1 : -1;
                        var integerLiteral      = exceptionInit.ParentNode[Terminals.IntegerLiteral];

                        if (exceptionIdentifier != null && integerLiteral != null && Int32.TryParse(integerLiteral.Token.Value, out var errorCode))
                        {
                            var exceptionName = exceptionIdentifier.Token.Value.ToQuotedIdentifier();
                            foreach (var exception in program.Exceptions)
                            {
                                if (String.Equals(exception.Name, exceptionName))
                                {
                                    exception.ErrorCode = errorCodeModifier * errorCode;
                                }
                            }

                            CreatePlSqlExceptionReference(program, integerLiteral.ParentNode);
                        }
                    }
                }
            }
        }
示例#4
0
        private void FindPlSqlReferences(OraclePlSqlProgram program, StatementGrammarNode node)
        {
            if (node == null)
            {
                return;
            }

            var excludedIdentifiers = new HashSet <StatementGrammarNode>();

            switch (node.Id)
            {
            case NonTerminals.PlSqlRaiseStatement:
                var prefixedExceptionIdentifierNode = node[NonTerminals.PrefixedExceptionIdentifier];
                CreatePlSqlExceptionReference(program, prefixedExceptionIdentifierNode);
                break;

            case NonTerminals.PlSqlCursorForLoopStatement:
                var cursorIdentifier = node[Terminals.PlSqlIdentifier];
                var cursorScopeNode  = node[NonTerminals.PlSqlBasicLoopStatement];
                if (cursorIdentifier != null && cursorScopeNode != null)
                {
                    excludedIdentifiers.Add(cursorIdentifier);

                    var cursor =
                        new OraclePlSqlCursorVariable
                    {
                        Name           = cursorIdentifier.Token.Value.ToQuotedIdentifier(),
                        IsImplicit     = true,
                        Owner          = program,
                        DefinitionNode = cursorIdentifier,
                        ScopeNode      = cursorScopeNode
                    };

                    SetStatementModelIfFound(cursor, node[NonTerminals.CursorSource, NonTerminals.SelectStatement]);

                    program.Variables.Add(cursor);

                    var cursorReferenceIdentifier = node[NonTerminals.CursorSource, Terminals.CursorIdentifier];
                    if (cursorReferenceIdentifier != null)
                    {
                        var cursorReference =
                            new OraclePlSqlVariableReference
                        {
                            RootNode       = cursorReferenceIdentifier.ParentNode,
                            IdentifierNode = cursorReferenceIdentifier,
                            //ObjectNode = cursorIdentifierNode.ParentNode[NonTerminals.ObjectPrefix, Terminals.ObjectIdentifier],
                            //OwnerNode = cursorIdentifierNode.ParentNode[NonTerminals.SchemaPrefix, Terminals.SchemaIdentifier],
                            Container    = program,
                            PlSqlProgram = program
                        };

                        program.PlSqlVariableReferences.Add(cursorReference);
                    }
                }

                goto default;

            case NonTerminals.PlSqlForLoopStatement:
                var identifier = node[Terminals.PlSqlIdentifier];
                var scopeNode  = node[NonTerminals.PlSqlBasicLoopStatement];
                if (identifier != null && scopeNode != null)
                {
                    excludedIdentifiers.Add(identifier);

                    var variable =
                        new OraclePlSqlVariable
                    {
                        Name           = identifier.Token.Value.ToQuotedIdentifier(),
                        Owner          = program,
                        DefinitionNode = identifier,
                        DataType       = OracleDataType.BinaryIntegerType,
                        ScopeNode      = scopeNode
                    };

                    program.Variables.Add(variable);
                }

                goto default;

            default:
                var identifiers = node.GetPathFilterDescendants(NodeFilters.BreakAtPlSqlSubProgramOrSqlCommand, Terminals.Identifier, Terminals.PlSqlIdentifier, Terminals.RowIdPseudocolumn, Terminals.Level, Terminals.RowNumberPseudocolumn, Terminals.User)
                                  .Where(i => !excludedIdentifiers.Contains(i));

                ResolveColumnFunctionOrDataTypeReferencesFromIdentifiers(null, program, identifiers, StatementPlacement.None, null, null, GetFunctionCallNodes);

                var grammarSpecificFunctions = GetGrammarSpecificFunctionNodes(node);
                CreateGrammarSpecificFunctionReferences(grammarSpecificFunctions, program, null, StatementPlacement.None, null);
                break;
            }
        }