Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2570

Weak "REF CURSOR" types should not be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Add a return type to this "REF CURSOR".
    • List of parameters:
      • Key: sysRefCursorAllowed
      • description: Whether or not the use of "SYS_REFCURSOR" is allowed.
      • defaut: false
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      WeakRefCursorCheck
    • Covered Languages:
      PL/SQL
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min

      Description

      Weak REF CURSOR types are harder to work with than ones with a return type. Indeed, the compiler's type-checker is unable to make some verifications, which are then delayed till runtime.

      When the use of weak REF CURSOR is required, it is best to use the SYS_REFCURSOR built-in type instead of defining a new one.
      This rule's sysRefCursorAllowed parameter can be used to control whether or not the usage of SYS_REFCURSOR is allowed.

      Noncompliant Code Example

      DECLARE
        TYPE dualCursorType IS REF CURSOR;                      -- Noncompliant
        dualCursor dualCursorType;
      
        otherCursor SYS_REFCURSOR;                              -- Compliant or non-compliant, depending on the "sysRefCursorAllowed" parameter
      BEGIN
        otherCursor := dualCursor;                              -- Works
      END;
      /
      

      Compliant Solution

      DECLARE
        TYPE dualCursorType IS REF CURSOR RETURN DUAL%ROWTYPE;
        dualCursor dualCursorType;
      
        TYPE otherCursorType IS REF CURSOR RETURN a%ROWTYPE;
        otherCursor otherCursorType;
      BEGIN
        otherCursor := dualCursor;                              -- raises PLS-00382: expression is of wrong type, which makes debugging easier
      END;
      /
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: