Tải bản đầy đủ
3 PLV: Top-Level Constants and Functions

3 PLV: Top-Level Constants and Functions

Tải bản đầy đủ

[Appendix A] Appendix: PL/SQL Exercises
FUNCTION nullval RETURN VARCHAR2;
Returns the current NULL substitution value.

5.3.5 Assertion routines
assertion_failure EXCEPTION;
Exception raised by the various assertion routines when the assertion fails.
PROCEDURE assert (bool_in IN BOOLEAN, stg_in IN VARCHAR2 := NULL);
Does nothing if the Boolean argument evaluates to TRUE. Otherwise (for FALSE or NULL), it raises
the assertion_failure exception and displays the message.
This same behavior holds for the other assertion routines shown below.
PROCEDURE assert_notnull
(val_in IN VARCHAR2, stg_in IN VARCHAR2 := NULL);
PROCEDURE assert_notnull
(val_in IN DATE, stg_in IN VARCHAR2 := NULL);
PROCEDURE assert_notnull
(val_in IN NUMBER, stg_in IN VARCHAR2 := NULL);
PROCEDURE assert_notnull
(val_in IN BOOLEAN, stg_in IN VARCHAR2 := NULL);
Overloadings for the NOT NULL assertion logic.
PROCEDURE assert_inrange
(val_in IN DATE,
start_in IN DATE := SYSDATE, end_in IN DATE := SYSDATE+1,
stg_in IN VARCHAR2 := NULL, truncate_in IN BOOLEAN := TRUE);
PROCEDURE assert_inrange
(val_in IN NUMBER, start_in IN NUMBER, end_in IN NUMBER,
stg_in IN VARCHAR2 := NULL);
Overloadings of "in range" assertions for both date and numeric information.

5.3.6 Miscellaneous programs
FUNCTION boolstg (bool_in IN BOOLEAN, stg_in IN VARCHAR2 := NULL)
RETURN VARCHAR2;
Returns a string representing the value of the Boolean argument: TRUE if the Boolean argument is
TRUE, FALSE if FALSE, and NULL if NULL.
FUNCTION errm (code_in IN INTEGER := SQLCODE) RETURN VARCHAR2;
Returns the error message provided by SQLERRM. Encapsulation inside this function allows
SQLERRM to be referenced inside a SQL statement.
FUNCTION now RETURN VARCHAR2;
Returns the current date and time using the current PL/Vision date format mask.
PROCEDURE pause (secs_in IN INTEGER);
Pauses your PL/SQL program for the specified number of seconds.

5.2 p: a DBMS_OUTPUT
Substitute

5.3.5 Assertion routines

5.4 PLVcase: PL/SQL
Code Conversion

178

[Appendix A] Appendix: PL/SQL Exercises

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

5.3.5 Assertion routines

179

Chapter 5
PL/Vision Package
Specifications

5.4 PLVcase: PL/SQL Code Conversion
The PLVcase (PL/Vision CASE) package converts the case of PL/SQL source code so that it conforms to the
UPPER−lower method (reserved words in upper−case, application−specific identifiers in lower−case). See
Chapter 18, PLVcase and PLVcat: Converting and Analyzing PL/SQL Code for details.

5.4.1 Package constants
c_usecor CONSTANT VARCHAR2(3) := 'COR';
The constant used in calls to module (see below) to indicate that a CREATE OR REPLACE should be
appended to the source code for the program unit.
c_nousecor CONSTANT VARCHAR2(4) := 'NCOR';
The constant used to tell module to not append the CREATE OR REPLACE to the program unit.

5.4.2 Case−converting programs
FUNCTION token (token_in IN VARCHAR2, pkg_in IN VARCHAR2 := NULL)
RETURN VARCHAR2;
Converts the case of a single token according to the UPPER−lower method.
PROCEDURE line
(line_in IN OUT PLVio.line_type,
line_out IN OUT PLVio.line_type,
found_out OUT BOOLEAN);
Converts the case of a single line of source code according to the UPPER−lower method (it calls
PLVcase.token for each token in the string).
FUNCTION string (string_in IN VARCHAR2) RETURN VARCHAR2;
Converts the case of a string according to the UPPER−lower method. It formats the string as
necessary for a call to the PLVcase.line procedure.
PROCEDURE string (string_inout IN OUT VARCHAR2);
Procedure version of the string function. It hands you back your own string variable with the
case of the tokens converted.
PROCEDURE module
(module_in IN VARCHAR2,
cor_in IN VARCHAR2 := c_usecor,
last_module_in IN BOOLEAN := TRUE);
Converts the case of a single program unit according to the UPPER−lower method.
PROCEDURE modules (module_spec_in IN VARCHAR2 := NULL);
Converts the case of multiple program units according to the UPPER−lower method.
180

[Appendix A] Appendix: PL/SQL Exercises

5.3 PLV: Top−Level
Constants and Functions

5.5 PLVcat: PL/SQL Code
Cataloguing

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

181

Chapter 5
PL/Vision Package
Specifications

5.5 PLVcat: PL/SQL Code Cataloguing
The PLVcat (PL/Vision CATalogue) package catalogues PL/SQL source code so that you can analyze the
contents of your program for cross−references, dependencies, and so on. See Chapter 18 for details.

5.5.1 Cataloguing package contents
PROCEDURE module (module_in IN VARCHAR2);
Scans the contents of the specified module (currently only package specifications are supported) and
writes the list of its contents to the PLVrfrnc table.
PROCEDURE modules (module_in IN VARCHAR2);
Performs same task as the module procedure, but for multiple program units. You can, in other
words, provide an argument for module_in that contains wildcards.

5.5.2 Identifying references in stored code
PROCEDURE refnonkw (module_in IN VARCHAR2);
Scans the contents of the specified program unit and writes to the PLVctlg table all references to
non−keyword identifiers.
PROCEDURE refbi (module_in IN VARCHAR2);
Generates the list of builtin functions and packages that are referenced within the specified program
unit. This list is then written to the PLVctlg table.

5.4 PLVcase: PL/SQL
Code Conversion

5.6 PLVchr: Operations on
Single Characters

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

182

Chapter 5
PL/Vision Package
Specifications

5.6 PLVchr: Operations on Single Characters
The PLVchr (PL/Vision CHaRacter) package provides information about single characters in a string. See the
companion disk for details.

5.6.1 PLVchr constants
blank_char CONSTANT CHAR(1) := ' ';
space_char CONSTANT CHAR(1) := ' ';
quote1_char CONSTANT CHAR(1) := '''';
quote2_char CONSTANT CHAR(2) := '''''';
tab_char CONSTANT CHAR(1) := CHR(9);
newline_char CONSTANT CHAR(1) := CHR(10);
Named constants to use in place of hard−coded literals. Sure, there is more typing involved. But at
least you don't have to mess with single quotes, and the code is a lot more readable.
c_nonprinting CONSTANT CHAR(1) := 'n';
c_digit CONSTANT CHAR(1) := '9';
c_letter CONSTANT CHAR(1) := 'a';
c_other CONSTANT CHAR(1) := '*';
c_all CONSTANT CHAR(1) := '%';
Action codes for use in PLVchr programs that allow you to specify the category of character that you
want to work with.

5.6.2 Character type functions
FUNCTION is_quote (char_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_blank (char_in IN VARCHAR2) RETURN BOOLEAN;
Functions to encapsulate hard−coded checks for contents of the strings.
FUNCTION is_nonprinting (code_in IN INTEGER) RETURN BOOLEAN;
FUNCTION is_nonprinting (letter_in IN VARCHAR2) RETURN BOOLEAN;
Returns TRUE if the character (or, overloaded as this function is, the ASCII code) is a non−printing
character.
FUNCTION is_digit (code_in IN INTEGER) RETURN BOOLEAN;
FUNCTION is_digit (letter_in IN VARCHAR2) RETURN BOOLEAN;
Returns TRUE if the character (or, overloaded as this function is, the ASCII code) is a digit (0
through 9).
FUNCTION is_letter (letter_in IN VARCHAR2) RETURN BOOLEAN;
FUNCTION is_letter (code_in IN INTEGER) RETURN BOOLEAN;
Returns TRUE if the character (or, overloaded as this function is, the ASCII code) is a letter (a−z or
A−Z).
183

[Appendix A] Appendix: PL/SQL Exercises
FUNCTION is_other (code_in IN INTEGER) RETURN BOOLEAN;
FUNCTION is_other (letter_in IN VARCHAR2) RETURN BOOLEAN;
Returns TRUE if the character (or ASCII code) is not a letter, digit, or nonprinting character.

5.6.3 Other functions and procedures
FUNCTION char_name (letter_in IN VARCHAR2) RETURN VARCHAR2;
FUNCTION char_name (code_in IN INTEGER) RETURN VARCHAR2;
Returns the name of the provided character. This name is actually a standard abbreviation for the
character, such as NL for new line. The name of a printable character is simply the character itself.
You can pass either a character or an integer code to see the name.
FUNCTION quoted1 (string_in IN VARCHAR2) RETURN VARCHAR2;
FUNCTION quoted2 (string_in IN VARCHAR2) RETURN VARCHAR2;
Each function returns a string wrapped inside the number of single quote marks needed to allow the
string to be evaluated to a string surrounded by one and two single quote marks, respectively.
FUNCTION stripped (string_in IN VARCHAR2, char_in IN VARCHAR2)
RETURN VARCHAR2;
Strips a string of all instances of the specified characters. This function is a frontend to TRANSLATE.
PROCEDURE show_string
(string_in IN VARCHAR2, flags_in IN VARCHAR2 := c_all);
Displays the ASCII code and its associated character for each character in the specified string. You
can request to view only certain kinds of characters.
PROCEDURE show_table
(start_code_in IN INTEGER := 1,
end_code_in IN INTEGER := NULL);
Displays the ASCII code and its associated character for all codes within the specified start−end
range.

5.5 PLVcat: PL/SQL Code
Cataloguing

5.7 PLVcmt: Commit
Processing

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

5.6.3 Other functions and procedures

184