#

| Chapter 15 PLVvu: Viewing Source Code and Compile Errors |

## 15.3 Displaying Source Code

PLVvu provides two procedures to display source code: the `code` and `code_after` programs. The `code` program displays all the lines of code for the specified program unit found between the start and end lines specified. The `code_after` program displays the specified number of lines found after the n th occurrence of a particular string. They are both explained below.

### 15.3.1 Displaying Code by Line Number

The header for the `code` program is:

>     PROCEDURE code
>        (name_in IN VARCHAR2 := NULL,
>         start_in IN INTEGER := 1,
>         end_in IN INTEGER := NULL,
>         header_in IN VARCHAR2 := 'Code for');

The first argument is the name of the program unit. If you do not supply a program, `PLVvu.code` will use the object last compiled into the database.

The format for specifying a program unit is explained in Chapter 11. Briefly, you can supply only the name, the type:name (as in "b:PLVio" for the body of the PLVio package), or even the type:schema.name (as in "s:scott.showemps" to see the specification of the `showemps` package owned by SCOTT).

The second and third arguments provide the range of line numbers of the code to be displayed. The default is all lines, with the start value of 1 and the end value NULL. The final argument provides a prefix for the output's header.

If you want to see all the lines of source code for a program unit, simply pass the program name and leave all the other arguments as the default. This approach is shown below:

>     SQL> exec PLVvu.code('s:testcase');
>      --------------------------------------------------------------------
>      Code for PACKAGE TESTCASE
>      --------------------------------------------------------------------
>      Line#  Source
>      --------------------------------------------------------------------
>          1 package testcase
>          2 is
>          3    procedure save (string_in in varchar2);
>          4 end testcase;

The next call to `PLVvu.code` requests that it display lines 85 through 95 of the body of the PLVvu package.

>     SQL> exec PLVvu.code ('b:PLVvu', 85, 95, 'Contents of');
>      ----------------------------------------------------------------------
>      Contents of PACKAGE BODY PLVVU
>      ----------------------------------------------------------------------
>      Line#  Source
>      ----------------------------------------------------------------------
>         85      THEN
>         86         p.l ('ERR' || LPAD ('*', err_rec.position+4));
>         87         PLVprs.display_wrap
>         88            (PLVchr.stripped (err_rec.text, PLVchr.newline_char),
>         89             60, '    ');
>         90      END IF;
>         91      CLOSE err_cur;
>         92   END;
>         93   /*--------------- Public Modules -----------------*/
>         94   PROCEDURE set_overlap
>         95      (size_in IN INTEGER := c_overlap)

The `code.sql` SQL*Plus script allows you to skip some of the typing (and all of those irritating single quotes) when you use `PLVvu.code`. The last execution of `PLVvu.code`, for example, could be shortened to:

>     SQL> @code b:PLVvu 85 95

### 15.3.2 Displaying Code by Keyword

The code procedure is very useful and saves you the effort of putting together a quick SQL*Plus script to view lines of source code. However, scanning source code by line number ranges is not the only way you might want to locate and view your code. Another common method is to search for a keyword and then display the lines of code before, after, or around that keyword.

The `code_after` procedure displays the specified lines of code appearing after the nth occurrence of a keyword you provide. The header for `code_after` is:

>     PROCEDURE code_after
>        (name_in IN VARCHAR2 := NULL,
>         start_with_in IN VARCHAR2,
>         num_lines_in IN INTEGER := overlap,
>         nth_in IN INTEGER := 1)

The first argument is the name of the program unit. If you do not supply a program, `PLVvu.code` will use the object last compiled into the database.

The format for specifying a program unit is explained in Chapter 11. Briefly, you can supply only the name, the type:name (as in "b:PLVio" for the body of the PLVio package), or even the type:schema.name (as in "s:scott.showemps" to see the specification of the `showemps` package owned by SCOTT).

The second argument supplies the string for which `code_after` will search. The `num_lines_in` argument is the number of lines after the keyword is found that will be displayed (default provided by the current value of the overlap count). The last argument, `nth_in`, specifies the number of occurrences to be located before displaying the subsequent lines of code.

The following calls to `code_after` demonstrate the use of these different arguments. In the first example, I ask to see the default number of lines (5) following the first occurrence of SUBSTR. In the second call to `code_after`, I request to see only three lines following the fifth occurrence of SUBSTR.

>     SQL> exec PLVvu.code_after('b:PLVio','SUBSTR');
>      ----------------------------------------------------------------------
>      Code Starting with "SUBSTR" in PACKAGE BODY PLVIO
>      ----------------------------------------------------------------------
>      Line#  Source
>      ----------------------------------------------------------------------
>        330           (SUBSTR (srcrep.select_sql, 1, loc-1) ||
>        331           srcrep.where_clause || ' ' ||
>        332           SUBSTR (srcrep.select_sql, loc));
>        333      ELSE
>        334         RETURN srcrep.select_sql;
>        335      END IF;

>     SQL> exec PLVvu.code_after('b:PLVio','SUBSTR', 3, 5);
>      ----------------------------------------------------------------------
>      Code Starting with "SUBSTR" in PACKAGE BODY PLVIO
>      ----------------------------------------------------------------------
>      Line#  Source
>      ----------------------------------------------------------------------
>        704                   SUBSTR
>        705                      (string_repos.text_in,
>        706                       string_repos.start_pos);
>        707                string_repos.start_pos :=

Now you know how to use the `code` and `code_after` procedures to display your source code. Section 15.4, "Implementing PLVvu" shows you the techniques used to obtain this information.

15.2 Displaying Compile Errors |  | 15.4 Implementing PLVvu

Copyright (c) 2000 O'Reilly & Associates. All rights reserved.