Tải bản đầy đủ
22 PLVprsps: PL/SQL Source Code Parsing

22 PLVprsps: PL/SQL Source Code Parsing

Tải bản đầy đủ

[Appendix A] Appendix: PL/SQL Exercises
PROCEDURE nokeep_bi;
Specifies that when a PL/SQL string is parsed, only builtin keywords are to be ignored.

5.22.3 Parsing PL/SQL source code
PROCEDURE init_table
(tokens_out IN OUT PLVtab.vc2000_table,
num_tokens_out IN OUT INTEGER);
Initializes (to empty) a PL/SQL table that will hold the parsed tokens.
PROCEDURE plsql_string
(line_in IN VARCHAR2,
tokens_out IN OUT PLVtab.vc2000_table,
num_tokens_out IN OUT INTEGER,
in_multiline_comment_out IN OUT BOOLEAN);
Parses a single line of code −− a PL/SQL string −− and deposits the separate tokens in the PL/SQL
table. It also passes back a Boolean flag to indicate whether, at the end of this string, the code is
within a multiline comment block.
PROCEDURE module
(module_in IN VARCHAR2 := NULL,
tokens_out IN OUT PLVtab.vc2000_table,
num_tokens_out IN OUT INTEGER);
Parses all the lines of code for the specified program.
PROCEDURE module
(tokens_out IN OUT PLVtab.vc2000_table,
num_tokens_out IN OUT INTEGER);
Parses all the lines of code for the current object as specified by the PLVobj package.

5.21 PLVprs: String
Parsing

5.23 PLVrb: Rollback
Processing

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

5.22.3 Parsing PL/SQL source code

233

Chapter 5
PL/Vision Package
Specifications

5.23 PLVrb: Rollback Processing
The PLVrb (PL/Vision RollBack) package provides a programmatic interface to rollback activity in PL/SQL.
See Chapter 20 for details.

5.23.1 Controlling rollback activity
PROCEDURE turn_on;
Enables rollback processing in PLVrbPLVcmt. This is not the default.
PROCEDURE turn_off;
Disables rollback processing in PLVrbPLVcmt. When this is called in the current session, the
ROLLBACK statement will not be executed (the default).
FUNCTION rolling_back RETURN BOOLEAN;
Returns TRUE if rollback processing is being performed by PLVrbPLVcmt.

5.23.2 Logging rollback activity
PROCEDURE log;
Requests that whenever a ROLLBACK is performed, a message is sent to the PL/Vision log.
PROCEDURE nolog;
Do not log a message with the ROLLBACK.
FUNCTION logging RETURN BOOLEAN;
Returns TRUE if currently logging the fact that a rollback was performed by PLVrbPLVcmt.

5.23.3 Performing rollbacks
PROCEDURE perform_rollback (context_in IN VARCHAR2 := NULL);
Issues a ROLLBACK command.
PROCEDURE rollback_to
(sp_in IN VARCHAR2, context_in IN VARCHAR2 := NULL);
Issues a ROLLBACK to the specified savepoint.
PROCEDURE rb_to_last (context_in IN VARCHAR2 := NULL);
Issues a ROLLBACK to the last savepoint specified in a call to set_savepoint.

5.23.4 Managing savepoints
PROCEDURE set_savepoint (sp_in IN VARCHAR2);

234

[Appendix A] Appendix: PL/SQL Exercises
Sets a savepoint by soft−coded string, rather than the usual hard−coded savepoint identifier. This
savepoint is set to the "last savepoint" recorded by PLVrbPLVcmt.
FUNCTION lastsp RETURN VARCHAR2;
Returns the name of the last savepoint.
PROCEDURE reset_savepoints;
Clears the stack of savepoints maintained by PLVrb. This is called by PLVrbPLVcmt after a commit
is performed.

5.22 PLVprsps: PL/SQL
Source Code Parsing

5.24 PLVstk: Stack
Manager

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

235

Chapter 5
PL/Vision Package
Specifications

5.24 PLVstk: Stack Manager
The PLVstk (PL/Vision STacK manager) package is a generic manager for both first−in−first−out (FIFO) and
last−in−last−out (LIFO) stacks; it is built on PLVlst. See the companion disk for details.

5.24.1 Package constants
defstk CONSTANT VARCHAR2(5) := 'stack';
The name of the default stack.
lifo CONSTANT VARCHAR2(4) := 'LIFO';
Indicates that you are working with a last−in−first−out stack. Used in calls to pop.
fifo CONSTANT VARCHAR2(4) := 'FIFO';
Indicates that you are working with a first−in−first−out stack. Used in calls to pop.

5.24.2 Creating and destroying stacks
PROCEDURE make
(stack_in IN VARCHAR2 := defstk,
overwrite_in IN BOOLEAN := TRUE);
Allocates storage for a stack of up to 1,000 items with the specified name. By default, if the stack
already exists it will be reinitialized to an empty stack.
PROCEDURE destroy (stack_in IN VARCHAR2 := defstk);
Releases all memory associated with this stack.

5.24.3 Modifying stack contents
PROCEDURE push
(item_in IN VARCHAR2, stack_in IN VARCHAR2 := defstk);
Pushes an item onto the specified stack.
PROCEDURE pop
(value_out IN OUT VARCHAR2,
stack_in IN VARCHAR2 := defstk,
stack_type_in IN VARCHAR2 := lifo);
Pops an item off the top (LIFO) or bottom (FIFO) of the stack.

5.24.4 Analyzing stack contents
FUNCTION nitems (stack_in IN VARCHAR2 := defstk)
RETURN INTEGER;
Returns the number of items currently in the stack.
236

[Appendix A] Appendix: PL/SQL Exercises
FUNCTION itemin (stack_in IN VARCHAR2, item_in IN VARCHAR2)
RETURN BOOLEAN;
Returns TRUE if the specified item is found in the stack.

5.24.5 Tracing Stack Activity
PROCEDURE show
(stack_in IN VARCHAR2 := defstk,
show_contents_in IN BOOLEAN := FALSE);
Requests that pre−action status of stack be displayed for the specified stack (or all).
PROCEDURE noshow;
Turns off display of pre−action status.
FUNCTION showing RETURN BOOLEAN;
Returns TRUE if showing pre−action status.
PROCEDURE verify
(stack_in IN VARCHAR2 := defstk,
show_contents_in IN BOOLEAN := FALSE);
Requests that post−action status of stack be displayed for the specified stack (or all).
PROCEDURE noverify;
Turns off display of post−action status.
FUNCTION verifying RETURN BOOLEAN;
Returns TRUE if showing post−action status.

5.23 PLVrb: Rollback
Processing

5.25 PLVtab: Table
Interface

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

5.24.5 Tracing Stack Activity

237

Chapter 5
PL/Vision Package
Specifications

5.25 PLVtab: Table Interface
The PLVtab (PL/Vision TABle) package makes it easier to declare, use, and display the contents of PL/SQL
tables by providing predefined PL/SQL table types and programs. See Chapter 8, PLVtab: Easy Access to
PL/SQL Tables for details.

5.25.1 Predefined table TYPEs
Since these table TYPES are already defined in the PLVtab package, you can use them to declare your own
PL/SQL tables −− and not deal with the cumbersome syntax.
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

boolean_table IS TABLE OF BOOLEAN INDEX BY BINARY_INTEGER;
date_table IS TABLE OF DATE INDEX BY BINARY_INTEGER;
integer_table IS TABLE OF INTEGER INDEX BY BINARY_INTEGER;
number_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
vc30_table IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
vc60_table IS TABLE OF VARCHAR2(60) INDEX BY BINARY_INTEGER;
vc80_table IS TABLE OF VARCHAR2(80) INDEX BY BINARY_INTEGER;
vc2000_table IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
vcmax_table IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER;

5.25.2 The empty PL/SQL tables
An empty PL/SQL table is a structure in which no rows have been defined. The only way to delete all the
rows from a PL/SQL table is to assign an empty table to that table. You can use these predefined PL/SQL
tables to accomplish this task easily.
empty_boolean boolean_table;
empty_date date_table;
empty_integer integer_table;
empty_number number_table;
empty_vc30 vc30_table;
empty_vc60 vc60_table;
empty_vc80 vc80_table;
empty_vc2000 vc2000_table;
empty_vcmax vcmax_table;

5.25.3 Toggle for showing header
PROCEDURE showhdr;
Requests that a header be displayed with the contents of the table (the header text is passed in the call
to the display procedure). This is the default.
PROCEDURE noshowhdr;
Turns off the display of the header text with the table contents.
FUNCTION showing_header RETURN BOOLEAN;
Returns TRUE if the header is being displayed.
238

[Appendix A] Appendix: PL/SQL Exercises

5.25.4 Toggle for showing row numbers
PROCEDURE showrow;
Requests that the row number be displayed with the row contents.
PROCEDURE noshowrow;
Turns off display of the row number (the default).
FUNCTION showing_row RETURN BOOLEAN;
Returns TRUE if the row number is being displayed.

5.25.5 Toggle for showing blanks in row
PROCEDURE showblk;
Requests that blank lines be displayed. A NULL row will display as the p.fornull NULL
substitution value. A line consisting only of blanks will be displayed as the word BLANKS.
PROCEDURE noshowblk;
Requests that blank lines be displayed as blank lines.
FUNCTION showing_blk RETURN BOOLEAN;
Returns TRUE if showing the contents of blank lines.

5.25.6 Setting the row prefix
PROCEDURE set_prefix (prefix_in IN VARCHAR2 := NULL);
Sets the character(s) used as a prefix to the text displayed before the value in the table's row (default is
NULL).
FUNCTION prefix RETURN VARCHAR2;
Returns the current prefix character(s).

5.25.7 Saving and restoring settings
PROCEDURE save;
Saves the current settings for the toggles to private variables in the package.
PROCEDURE restore;
Restores the settings for the toggles from the saved values.

5.25.8 Displaying a PLVtab table
PROCEDURE display
(tab_in IN boolean_table|date_table|number_table|integer_table,
end_in IN INTEGER,
hdr_in IN VARCHAR2 := NULL,
start_in IN INTEGER := 1,
failure_threshold_in IN INTEGER := 0,
increment_in IN INTEGER := +1);
PROCEDURE display
(tab_in IN
vc30_table|vc60_table|vc80_table|vc2000_table|vcmax_table,
end_in IN INTEGER,
5.25.4 Toggle for showing row numbers

239

[Appendix A] Appendix: PL/SQL Exercises
hdr_in IN VARCHAR2 := NULL,
start_in IN INTEGER := 1,
failure_threshold_in IN INTEGER := 0,
increment_in IN INTEGER := +1);
The display procedure is overloaded nine times, for a variety of datatypes. The first version above
shows the overloading for non−string datatypes. The second version shows all the different types of
string PL/SQL tables supported by the PLVtab.display procedure.

5.24 PLVstk: Stack
Manager

5.26 PLVtkn: Token Table
Interface

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

5.25.4 Toggle for showing row numbers

240

Chapter 5
PL/Vision Package
Specifications

5.26 PLVtkn: Token Table Interface
The PLVtkn (PL/Vision ToKeN) package provides an interface to the PLV_token table; the package
examines this table to determine whether an identifier is a keyword. See Chapter 10 for details.

5.26.1 Package constants
c_any CONSTANT CHAR(1) := '%';
c_syntax CONSTANT CHAR(1) := 'X';
c_builtin CONSTANT CHAR(1) := 'B';
c_symbol CONSTANT CHAR(1) := 'S';
c_exception CONSTANT CHAR(1) := 'E';
c_datatype CONSTANT CHAR(1) := 'D';
c_datadict CONSTANT CHAR(2) := 'DD';
c_sql CONSTANT CHAR(3) := 'SQL';
Each of these constants specify a different type of token or reserved word in the PL/SQL language
(except for c_any, of course). They match the values found in the PLV_token_type table. They
are used by various programs in the PLVtkn package.
c_plsql CONSTANT CHAR(1) := '%'; /* = c_builtin */
c_od2k CONSTANT CHAR(4) := 'OD2K';
c_of CONSTANT CHAR(2) := 'OF';
c_or CONSTANT CHAR(2) := 'OR';
c_og CONSTANT CHAR(2) := 'OG';
These constants identify different categories or sets of reserved words for the PL/SQL language. The
identifier SHOW_ALERT is, for example, a builtin in Oracle Forms, but has no significance in the
stored PL/SQL code. Currently PL/Vision is aware of stored PL/SQL and Oracle Forms reserved
words only.

5.26.2 The keyword record TYPE
TYPE kw_rectype IS RECORD
(token PLV_token%token,
token_type PLV_token%token_type,
is_keyword BOOLEAN);
A PL/SQL record TYPE defining a record holding information about a token.

5.26.3 Determining type of token
FUNCTION is_keyword
(token_in IN VARCHAR2, type_in IN VARCHAR2 := c_any)
RETURN BOOLEAN;
General function that returns TRUE if the specified token is a keyword or a reserved word in the
PL/SQL language. You can, with the type_in argument, ask if the token is a particular type of
241

[Appendix A] Appendix: PL/SQL Exercises
token. You can also call any of the following more narrowly defined functions:
FUNCTION is_syntax (token_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_builtin (token_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_symbol (token_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_datatype (token_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_exception (token_in IN VARCHAR2) RETURN BOOLEAN;
Each of these functions returns TRUE if the specified token is an identifier of the type indicated by
the name of the function.

5.26.4 Retrieving keyword information
PROCEDURE get_keyword (token_in IN VARCHAR2, kw OUT kw_rectype);
Retrieves all the information about the token provided in the parameter list.

5.25 PLVtab: Table
Interface

5.27 PLVtmr: Program
Performance Analyzer

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

5.26.4 Retrieving keyword information

242

Chapter 5
PL/Vision Package
Specifications

5.27 PLVtmr: Program Performance Analyzer
The PLVtmr (PL/Vision TiMeR) package allows you to measure the elapsed time of PL/SQL code; it
provides a programmatic layter around the GET_TIME function of Oracle's DBMS_UTILITY package. See
Chapter 14, PLVtmr: Analyzing Program Performance for details.

5.27.1 Control execution of timings
PROCEDURE turn_on;
Enables the timing package. All PLVtmr programs will execute as requested.
PROCEDURE turn_off;
Disables PLVtmr. Calls to timing programs will be ignored.

5.27.2 Setting the repetition value
PROCEDURE set_repeats (repeats_in IN NUMBER);
Sets the number of repetitions for all timing loops implemented inside the PLVtmr package
(calibrate, func, currsucc, currfail).
FUNCTION repeats RETURN NUMBER;
Returns the current repetition value.

5.27.3 Setting the factoring value
PROCEDURE set_factor (factor_in IN NUMBER);
Sets the number of iterations when calling PLVtmr in a loop. This value allows PLVtmr to show total
and individual elapsed times.
FUNCTION factor RETURN NUMBER;
Returns the current number of iterations.

5.27.4 Capturing the current timestamp
PROCEDURE capture (context_in IN VARCHAR2 := NULL);
Calls capture to capture the current timestamp (usually the start time of an elapsed time
calculation). You can provide an optional context for the timing.

5.27.5 Retrieving and displaying elapsed time
FUNCTION elapsed RETURN NUMBER;
Returns the number of hundredths of seconds elapsed since last call to capture.

243