Tải bản đầy đủ
6 PLVchr: Operations on Single Characters

6 PLVchr: Operations on Single Characters

Tải bản đầy đủ

[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

Chapter 5
PL/Vision Package
Specifications

5.7 PLVcmt: Commit Processing
The PLVcmt (PL/Vision CoMmiT) package provides a programmatic interface to the execution of commits,
rollbacks, and the setting of savepoints. See Chapter 20, PLVcmt and PLVrb: Commit and Rollback
Processing for details.

5.7.1 Controlling commit activity
PROCEDURE turn_on;
Enables commit processing in PLVcmt. This is the default.
PROCEDURE turn_off;
Disables commit processing in PLVcmt. When this program is called in the current session, the
COMMIT statement will not be executed.
FUNCTION committing RETURN BOOLEAN;
Returns TRUE if commit processing is being performed by PLVcmt.

5.7.2 Logging commit activity
PROCEDURE log;
Requests that, whenever a COMMIT is performed, a message be sent to the PL/Vision log.
PROCEDURE nolog;
Do not log a message with the COMMIT.
FUNCTION logging RETURN BOOLEAN;
Returns TRUE if currently logging the fact that a commit was performed by PLVcmt.

5.7.3 Performing commits
PROCEDURE increment_and_commit (context_in IN VARCHAR2 := NULL);
Increments the counter and commits if a commit point has been reached.
PROCEDURE perform_commit(context_in IN VARCHAR := NULL);
The PLVcmt package's version of COMMIT. I could probably get away with calling this program
commit, but I avoid using keywords even when the compiler doesn't seem to get confused.

5.7.4 Managing the commit counter
PROCEDURE commit_after (count_in IN INTEGER);
Sets the break point at which a commit is performed. In other words, when the package−based counter
reaches the specified number, issue a COMMIT. The default is to commit after the counter reaches 1.

185

[Appendix A] Appendix: PL/SQL Exercises
PROCEDURE init_counter;
Initializes the PLVcmt counter referenced by the increment_and_commit program to perform
incremental commits.

5.6 PLVchr: Operations on
Single Characters

5.8 PLVddd: DDL Syntax
Dump

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

186

Chapter 5
PL/Vision Package
Specifications

5.8 PLVddd: DDL Syntax Dump
The PLVddd (PL/Vision Dump Data Definition language) package dumps DDL syntax from a particular
schema to allow you to recreate database objects easily in other schemas. See the companion disk for details.

5.8.1 Including the schema
PROCEDURE inclschema;
Turns on the inclusion of schema names before each created object. This is the DEFAULT position.
PROCEDURE noinclschema;
Turns off the showing of the schema names before each created object.
FUNCTION including_schema RETURN BOOLEAN;
Returns TRUE if including the schema.

5.8.2 Including the storage parameter
PROCEDURE inclsp;
Turns on the inclusion of the storage parameters after appropriate objects.
PROCEDURE noinclsp;
Turns OFF the inclusion of storage parameters after appropriate objects. This is the DEFAULT
position.
FUNCTION including_sp RETURN BOOLEAN;
Function returning TRUE if storage parameters are included and FALSE if they are not.

5.8.3 Dumping the DDL
PROCEDURE tbl (owner_in IN VARCHAR2, table_in IN VARCHAR2 := '%');
Dumps DDL for tables including named column, check constraints, and storage information.
PROCEDURE idx
(owner_in IN VARCHAR2,
name_in IN VARCHAR2 := '%',
table_in IN VARCHAR2 := '%');
Dumps DDL for single indexes or all indexes on tables.
PROCEDURE pky
(owner_in IN VARCHAR2,
name_in IN VARCHAR2 := '%',
table_in IN VARCHAR2 := '%');
Dumps DDL for single primary keys or all primary keys on tables.
187

[Appendix A] Appendix: PL/SQL Exercises
PROCEDURE fky
(owner_in IN VARCHAR2,
name_in IN VARCHAR2 := '%',
table_in IN VARCHAR2 := '%');
Dumps DDL for single foreign keys or all foreign keys on tables.
PROCEDURE syn
(synonym_owner_in IN VARCHAR2,
name_in IN VARCHAR2 := '%',
object_owner_in IN VARCHAR2 := '%',
object_in IN VARCHAR2 := '%');
Dumps DDL for single synonyms or all synonyms for a table.
PROCEDURE vw (owner_in IN VARCHAR2, name_in IN VARCHAR2 := '%');
Dumps DDL for views.
PROCEDURE trig
(owner_in IN VARCHAR2,
name_in IN VARCHAR2 := '%',
table_in IN VARCHAR2 := '%');
Dumps DDL for single triggers or all triggers on tables.
PROCEDURE plsql (owner_in IN VARCHAR2, name_in IN
VARCHAR2 := '%');
Dumps DDL for PL/SQL code objects including functions, packages, package bodies, and
procedures.
PROCEDURE seq (owner_in IN VARCHAR2, name_in IN VARCHAR2 := '%');
Dumps DDL for sequences including starting points, max/min values, etc.
PROCEDURE schema (owner_in IN VARCHAR2, object_in IN
VARCHAR2 := '%');
Dumps all of the DDL related to a specified object. If the object is a table, for example, it can
generate all indexes, keys, triggers, synonyms, and views for that table as well.

5.7 PLVcmt: Commit
Processing

5.9 PLVdyn: Dynamic
SQL Operations

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

188

Chapter 5
PL/Vision Package
Specifications

5.9 PLVdyn: Dynamic SQL Operations
The PLVdyn (PL/Vision DYNamic SQL) package provides a high−level interface to Oracle's builtin
DBMS_SQL package. See Chapter 19, PLVdyn and PLVfk: Dynamic SQL and PL/SQL for details.

5.9.1 Tracing PLVdyn activity
PROCEDURE showsql (start_with_in IN VARCHAR2 := NULL);
Requests that the string being parsed dynamically be displayed. You can specify the string that should
start the displayed text.
PROCEDURE noshowsql;
Turns off the display of the dynamic SQL string.
FUNCTION showing RETURN BOOLEAN;
Returns TRUE if currently showing the dynamic SQL.

5.9.2 Controlling execution of dynamic SQL
PROCEDURE execsql;
Requests that calls to PLVdyn.execute call the underlying DBMS_SQL.EXECUTE builtin.
PROCEDURE noexecsql;
Requests that PLVdyn.execute not actually execute the specified cursor.
FUNCTION executing RETURN BOOLEAN;
Returns TRUE if currently executing the dynamic SQL.

5.9.3 Bundled, low−level operations
FUNCTION open_and_parse
(string_in IN VARCHAR2,
mode_in IN INTEGER := DBMS_SQL.NATIVE) RETURN INTEGER;
Combines the open and parse operations into a single function call.
PROCEDURE execute (cur_inout IN INTEGER);
A passthrough to the DBMS_SQL.EXECUTE function. By using PLVdyn.execute, you give
yourself the flexibility to turn off execution without modifying your code.
PROCEDURE execute_and_fetch
(cur_inout IN INTEGER, match_in IN BOOLEAN := FALSE);
A passthrough to the DBMS_SQL.EXECUTE_AND_FETCH function. By using this procedure, you
give yourself the flexibility to turn off execution without modifying your code.

189

[Appendix A] Appendix: PL/SQL Exercises
PROCEDURE execute_and_close (cur_inout IN OUT INTEGER);
Combines the execute and close operations into a single call.
PROCEDURE parse_delete
(table_in IN VARCHAR2, where_in IN VARCHAR2,
cur_out OUT INTEGER);
Performs the parse step of DBMS_SQL for a DELETE string constructed from the arguments in the
parameter list.

5.9.4 Data Definition Language operations
PROCEDURE ddl (string_in IN VARCHAR2);
Executes any DDL statement by performing an OPEN, then a PARSE. This program forces a commit
in your session, as when any DDL command is given.
PROCEDURE drop_object
(type_in IN VARCHAR2, name_in IN VARCHAR2,
schema_in IN VARCHAR2 := USER);
Provides a generic, powerful interface to the DDL DROP command. You can drop individual or
multiple objects.
PROCEDURE truncate
(type_in IN VARCHAR2, name_in IN VARCHAR2,
schema_in IN VARCHAR2 := USER);
Truncates either a table or a cluster as specified.
PROCEDURE compile
(stg_in IN VARCHAR2, show_err_in IN VARCHAR2 := PLV.noshow);
Executes a CREATE OR REPLACE of the program contained in the first argument, stg_in. You
can also request that errors from this compile be immediately displayed with a call to the
PLVvu.err procedure.
PROCEDURE compile
(table_in IN PLVtab.vc2000_table,
lines_in IN INTEGER,
show_err_in IN VARCHAR2 := PLV.noshow);
Another version of dynamic CREATE OR REPLACE that reads the source code for the program
from the PL/SQL table.
FUNCTION nextseq (seq_in IN VARCHAR2, increment_in IN INTEGER := 1)
RETURN INTEGER;
Returns the next value from the specified sequence. Can retrieve the immediate next value or the nth
next value. Use of this function avoids direct reference to the DUAL table.

5.9.5 Data Manipulation Language operations
PROCEDURE dml_insert_select
(table_in IN VARCHAR2, select_in IN VARCHAR2);
Issues an INSERT−SELECT statement based on the arguments provided.
PROCEDURE dml_delete
(table_in IN VARCHAR2, where_in IN VARCHAR2 := NULL);
Deletes all rows specified by the WHERE clause from the table argument.

5.9.4 Data Definition Language operations

190

[Appendix A] Appendix: PL/SQL Exercises
PROCEDURE dml_update
(table_in IN VARCHAR2,
column_in IN VARCHAR2,
value_in IN VARCHAR2|NUMBER|DATE,
where_in IN VARCHAR2 := NULL);
Overloaded to support string, numeric, and date values, dml_update performs a single−column
UPDATE as specified by the arguments.

5.9.6 Executing dynamic PL/SQL
PROCEDURE plsql (string_in IN VARCHAR2);
Executes any PL/SQL code. This procedure automatically packages your string inside a
BEGIN−END block and terminates it with a semicolon.

5.9.7 Miscellaneous programs
PROCEDURE disptab
(table_in IN VARCHAR2,
where_in IN VARCHAR2 := NULL,
string_length_in IN INTEGER := 20,
date_format_in IN VARCHAR2 := PLV.datemask,
num_length_in IN INTEGER := 10);
Displays the requested contents of any database table. Good example of the kind of code required to
perform Method 4 dynamic SQL.
FUNCTION plsql_block (string_in IN VARCHAR2) RETURN VARCHAR2;
Returns a string that is a valid PL/SQL block for dynamic PL/SQL execution.
FUNCTION placeholder
(string_in IN VARCHAR2, start_in IN INTEGER := 1)
RETURN VARCHAR2;
Locates and returns the nth placeholder for bind variables in strings.
FUNCTION tabexists (table_in IN VARCHAR2) RETURN BOOLEAN;
Returns TRUE if the specified table exists.
PROCEDURE time_plsql
(stg_in IN VARCHAR2, repetitions_in IN INTEGER := 1);
Calculates the overhead required to execute a dynamically constructed anonymous PL/SQL block.

5.8 PLVddd: DDL Syntax
Dump

5.10 PLVexc: Exception
Handling

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

5.9.6 Executing dynamic PL/SQL

191

Chapter 5
PL/Vision Package
Specifications

5.10 PLVexc: Exception Handling
The PLVexc (PL/Vision EXCeption handling) package provides generic and powerful exception−handling
capabilities. See Chapter 22, Exception Handling for details.

5.10.1 Package constants
c_go CONSTANT CHAR(1) := 'C';
Requests that your program continue (ignore the error). Explained in more detail below.
c_recNgo CONSTANT CHAR(2) := 'RC';
Requests that your program record the error and then continue. Explained in more detail below.
c_stop CONSTANT CHAR(1) := 'H';
Requests that your program be halted if this exception occurs. Explained in more detail below.
c_recNstop CONSTANT CHAR(2) := 'RH';
Requests that your program record the error and then halt. Explained in more detail below.

5.10.2 Package−based exceptions
process_halted EXCEPTION;
Package−specific exception raised when you request a "halt" action in the handler programs.
no_such_table EXCEPTION;
PRAGMA EXCEPTION_INIT (no_such_table, −942);
Predefined system exception for error ORA−942. Saves other developers from dealing with the
EXCEPTION_INIT pragma.
snapshot_too_old EXCEPTION;
PRAGMA EXCEPTION_INIT (snapshot_too_old, −1555);
Predefined system exception for error ORA−1555. Saves other developers from dealing with the
EXCEPTION_INIT pragma.

5.10.3 Logging exception−handling activity
PROCEDURE log;
Requests that whenever a PLVexc handler is called, a message is sent to the PL/Vision log.
PROCEDURE nolog;
Do not log the handling action when the exception is recorded and handled. with the COMMIT.
FUNCTION logging RETURN BOOLEAN;
Returns TRUE if currently logging PLVexc−based exception handling.
192

[Appendix A] Appendix: PL/SQL Exercises

5.10.4 Displaying exceptions
PROCEDURE show;
Requests that error information be displayed to your screen using the p.l procedure.
PROCEDURE noshow;
Turns off display of the error information.
FUNCTION showing RETURN BOOLEAN;
Returns TRUE if PLVexc is currently showing errors.

5.10.5 Rolling back on exception
PROCEDURE rblast;
Requests that a rollback be issued to the most recent savepoint before writing error information to the
log (the default).
PROCEDURE rbdef;
Requests that a rollback be issued to the default PLVlog savepoint before writing error information to
the log (the default).
PROCEDURE norb;
Turns off issuing of rollback before logging of the error information.
FUNCTION rb RETURN VARCHAR2;
Returns TRUE if PLVexc is currently issuing a rollback.

5.10.6 Exception handlers
PROCEDURE handle
(context_in IN VARCHAR2,
err_code_in IN INTEGER,
handle_action_in IN VARCHAR2,
msg_in IN VARCHAR2 := SQLERRM);
Low−level, generic exception−handling program. This program is called by all other PLVexc
handlers, which are overloaded for error number and message.
PROCEDURE recNgo (msg_in IN VARCHAR2 := NULL);
PROCEDURE recNgo (err_code_in IN INTEGER);
High−level exception handler that records and then ignores the error.
PROCEDURE go (msg_in IN VARCHAR2 := NULL);
PROCEDURE go (err_code_in IN INTEGER);
High−level exception handler that ignores the error, but gives you the opportunity to log or display
the exception.
PROCEDURE recNstop (msg_in IN VARCHAR2 := NULL);
PROCEDURE recNstop (err_code_in IN INTEGER);
High−level exception handler that records the error and then causes the current program to halt.
PROCEDURE stop (msg_in IN VARCHAR2 := NULL);
PROCEDURE stop (err_code_in IN INTEGER);
High−level exception handler that causes the current program to halt.

5.10.4 Displaying exceptions

193

[Appendix A] Appendix: PL/SQL Exercises

5.10.7 Bailing out program execution
PROCEDURE bailout;
Starts the bailout process; the current exception will be propagated out of all exception sections that
use PLVexc, regardless of the action handled.
PROCEDURE nobailout;
Turns off the bailout process. PLVexc will not propagate the exception past all PLVexc exception
handlers.
FUNCTION bailing_out RETURN BOOLEAN;
Returns TRUE if PLVexc is currently set to bail out when it encounters a bailout error.
PROCEDURE clear_bailouts;
Registers a specific error number as a bailout error.

5.10.8 Managing the list of bailout errors
PROCEDURE clear_bailouts;
Clears the PLVexc list of bailout errors.
PROCEDURE bailout_on (err_code_in IN INTEGER);
Adds an error code to the list that PLVexc treats as bailout errors.
PROCEDURE nobailout_on (err_code_in IN INTEGER);
Removes an error code from the list that PLVexc treats as bailout errors.
FUNCTION bailout_error (err_code_in IN INTEGER) RETURN BOOLEAN;
Returns TRUE if the specified error is a bailout error.

5.9 PLVdyn: Dynamic
SQL Operations

5.11 PLVfile: Operating
System I/O Manager

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

5.10.7 Bailing out program execution

194