Tải bản đầy đủ
11 PLVfile: Operating System I/O Manager

11 PLVfile: Operating System I/O Manager

Tải bản đầy đủ

[Appendix A] Appendix: PL/SQL Exercises
Returns the current operating system delimiter.

5.11.4 Setting the default directory or location
PROCEDURE set_dir (dir_in IN VARCHAR2);
Sets the default directory for the file you are managing with PLVfile. If you specify the directory with
set_dir, you will not have to provide it in each call to PLVfile programs.
FUNCTION dir RETURN VARCHAR2;
Returns the current default directory.

5.11.5 Creating files
FUNCTION fcreate
(loc_in IN VARCHAR2, file_in IN VARCHAR2, line_in IN VARCHAR2)
RETURN UTL_FILE.FILE_TYPE;
Specify file location and name separately, as well as the single line of text to place in the file. The
fcreate procedure will create the file and return the handle to the file.
FUNCTION fcreate
(file_in IN VARCHAR2, line_in IN VARCHAR2 := NULL)
RETURN UTL_FILE.FILE_TYPE;
Create the file without explicitly providing the file location.
PROCEDURE fcreate
(loc_in IN VARCHAR2, file_in IN VARCHAR2, line_in IN VARCHAR2);
Create the file but do not return the handle to the file.
PROCEDURE fcreate
(file_in IN VARCHAR2, line_in IN VARCHAR2 := NULL);
Create the file without explicitly providing the file location and do not return the handle to the file.

5.11.6 Checking for file existence
FUNCTION fexists (loc_in IN VARCHAR2, file_in IN VARCHAR2)
RETURN BOOLEAN;
Provide location and name separately; function returns TRUE if PLVfile is able to open the file with
read−only access.
FUNCTION fexists (file_in IN VARCHAR2) RETURN BOOLEAN;
Returns TRUE if PLVfile is able to open the specified file with read−only access.

5.11.7 Opening a file
PROCEDURE fopen
(loc_in IN VARCHAR2, file_in IN VARCHAR2, mode_in IN VARCHAR2);
Opens a file for the specified mode (location and name provided separately) and does not return the
handle to the file.
PROCEDURE fopen
(file_in IN VARCHAR2, mode_in IN VARCHAR2 := c_append);
Opens a file for the specified mode and does not return the handle to the file.

5.11.4 Setting the default directory or location

196

[Appendix A] Appendix: PL/SQL Exercises
FUNCTION fopen
(file_in IN VARCHAR2, mode_in IN VARCHAR2 := c_append)
RETURN UTL_FILE.FILE_TYPE;
Opens a file for the specified mode and returns the handle to the file.
FUNCTION fopen
(loc_in IN VARCHAR2, file_in IN VARCHAR2, mode_in IN VARCHAR2)
RETURN UTL_FILE.FILE_TYPE;
Opens a file for the specified mode (location and name provided separately) and returns the handle to
the file.

5.11.8 Closing a file
PROCEDURE fclose (file_in IN UTL_FILE.FILE_TYPE);
Closes the specified file.
PROCEDURE fclose_all;
Closes all open files.

5.11.9 Reading from a file
PROCEDURE get_line
(file_in IN UTL_FILE.FILE_TYPE,
line_out IN OUT VARCHAR2,
eof_out OUT BOOLEAN);
Retrieves the next line from the specified file (by file handle). Returns a flag indicating whether the
end of the file has been reached.
FUNCTION line (file_in IN VARCHAR2, line_num_in IN INTEGER)
RETURN VARCHAR2;
Returns the nth line from the specified file. This program opens, reads from, and closes the file.
FUNCTION infile
(loc_in IN VARCHAR2,
file_in IN VARCHAR2,
text_in IN VARCHAR2,
nth_in IN INTEGER := 1,
start_line_in IN INTEGER := 1,
end_line_in IN INTEGER := 0,
ignore_case_in IN BOOLEAN := TRUE)
RETURN INTEGER;
UTL_FILE−version of INSTR. Finds the nth appearance of a string (text_in) within the specified
range of lines.
FUNCTION infile
(file_in IN VARCHAR2,
text_in IN VARCHAR2,
nth_in IN INTEGER := 1,
start_line_in IN INTEGER := 1,
end_line_in IN INTEGER := 0,
ignore_case_in IN BOOLEAN := TRUE)
RETURN INTEGER;
UTL_FILE−version of INSTR. Finds the nth appearance of a string (text_in) within the specified
range of lines. In this version you do not have to provide the location of the file separately from the
5.11.8 Closing a file

197

[Appendix A] Appendix: PL/SQL Exercises
name.

5.11.10 Writing to a file
PROCEDURE put_line
(file_in IN UTL_FILE.FILE_TYPE,line_in IN VARCHAR2);
Adds a line to the end of the specified file (by file handle). This file must already be opened for write
or append access.
PROCEDURE append_line (file_in IN VARCHAR2, line_in IN VARCHAR2);
Add a line to the end of the specified file. This program opens the file for append access, writes to the
file using put_line, and then closes the file.

5.11.11 Copying a file
PROCEDURE fcopy
(ofile_in IN VARCHAR2, nfile_in IN VARCHAR2,
start_in IN INTEGER := 1, end_in IN INTEGER := NULL);
Copies the contents of the old file (ofile_in) to the new file (nfile_in) for all lines within the
specified range.
PROCEDURE file2pstab
(file_in IN VARCHAR2,
table_inout IN OUT PLVtab.vc2000_table,
rows_out OUT INTEGER);
Copies the contents of the file to the PL/SQL table.
PROCEDURE file2list (file_in IN VARCHAR2, list_in IN VARCHAR2);
Copies the contents of the file to the PLVlst list specified by the list name.
PROCEDURE pstab2file
(table_in IN PLVtab.vc2000_table,
rows_in IN INTEGER,
file_in IN VARCHAR2,
mode_in IN VARCHAR2 := c_write);
Copies the contents of the PL/SQL table to a file. You can open the file in either write mode or
append mode (in which case the rows are added to the current contents of the file).

5.11.12 Displaying the contents of a file
PROCEDURE display
(file_in IN UTL_FILE.FILE_TYPE,
header_in IN VARCHAR2 := NULL,
start_in IN INTEGER := 1,
end_in IN INTEGER := NULL);
Displays the contents of the file (specified by file handle) using the p.l procedure. This version of
display assumes that the file has been opened.
PROCEDURE display
(file_in IN VARCHAR2,
header_in IN VARCHAR2 := NULL,
start_in IN INTEGER := 1,
end_in IN INTEGER := NULL);

5.11.10 Writing to a file

198

[Appendix A] Appendix: PL/SQL Exercises
Displays the contents of the file (specified by file name using the p.l procedure. This version of
display assumes that the file must be opened before reading the contents.

5.11.13 Miscellaneous operations
PROCEDURE parse_name
(file_in IN VARCHAR2, loc_out IN OUT VARCHAR2,
name_out IN OUT VARCHAR2);
Parses a file specification (directory, name, and extension) into two separate strings: the location or
directory and the file name itself.

5.10 PLVexc: Exception
Handling

5.12 PLVfk: Foreign Key
Interface

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

5.11.13 Miscellaneous operations

199

Chapter 5
PL/Vision Package
Specifications

5.12 PLVfk: Foreign Key Interface
The PLVfk (PL/Vision Foreign Key) package is a high−level, easy−to−use interface to look up foreign key
information in your tables. See Chapter 19 for details.

5.12.1 Package Constants
c_prefix CONSTANT VARCHAR2(1) := 'P';
Specifies that the column abbreviation is to be used as a prefix.
c_suffix CONSTANT VARCHAR2(1) := 'S';
Specifies that the column abbreviation is to be used as a suffix.
c_no_change CONSTANT VARCHAR2(10) := 'NO CHANGE';
Used to indicate that no change is to be made to the string value.
c_int_no_change CONSTANT INTEGER := 0;
Used to indicate that no change is to be made to the INTEGER value.

5.12.2 Setting the PLVfk configuration
PROCEDURE set_vclen (length_in IN INTEGER);
Sets the default VARCHAR2 length for the foreign key named retrieved by the PLVfk.name
function.
PROCEDURE set_id_default
(string_in IN VARCHAR2 := c_no_change,
type_in IN VARCHAR2 := c_no_change);
Sets the default value to be used as the suffix or prefix of the name for the ID column.
PROCEDURE set_nm_default
(string_in IN VARCHAR2 := c_no_change,
type_in IN VARCHAR2 := c_no_change);
Sets the default value to be used as the suffix or prefix of the name for the name column.

5.12.3 Looking up the name
FUNCTION name
(fk_id_in IN INTEGER,
fk_table_in IN VARCHAR2,
fk_id_col_in IN VARCHAR2 :=
fk_nm_col_in IN VARCHAR2 :=
max_length_in IN INTEGER :=
where_clause_in IN VARCHAR2
RETURN VARCHAR2;

c_no_change,
c_no_change,
c_int_no_change,
:= NULL)

200

[Appendix A] Appendix: PL/SQL Exercises
Retrieves the name for the specified table and ID.

5.12.4 Looking up the ID
FUNCTION id
(fk_nm_in IN VARCHAR2,
fk_table_in IN VARCHAR2,
fk_id_col_in IN VARCHAR2 :=
fk_nm_col_in IN VARCHAR2 :=
max_length_in IN INTEGER :=
where_clause_in IN VARCHAR2
RETURN INTEGER;

c_no_change,
c_no_change,
c_int_no_change,
:= NULL)

Retrieves the ID (primary key) for the specified table and name or descriptor.

5.11 PLVfile: Operating
System I/O Manager

5.13 PLVgen: PL/SQL
Code Generator

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

5.12.4 Looking up the ID

201

Chapter 5
PL/Vision Package
Specifications

5.13 PLVgen: PL/SQL Code Generator
The PLVgen (PL/Vision GENerator) package provides a set of procedure you can use to generate your own
PL/SQL code. See Chapter 16, PLVgen: Generating PL/SQL Programs for details.

5.13.1 Package constants
c_indent CONSTANT INTEGER := 0;
The default initial indentation of generated code.
c_incr_indent CONSTANT INTEGER := 3;
The default incremental indentation of generated code.
c_literal CONSTANT CHAR(1) := '=';
The character used to indicate that the default value for the string function is not to be evaluated
before placing in the function definition.
c_def_length CONSTANT INTEGER := 100;
The default length for a string function's local variable.
c_none CONSTANT VARCHAR2(1) := 'N';
Indicates that no blank lines are to be placed before or after the current line of code.
c_before CONSTANT VARCHAR2(1) := 'B';
Indicates that a blank line is to be placed before the current line of code.
c_after CONSTANT VARCHAR2(1) := 'A';
Indicates that a blank line is to be placed after the current line of code.
c_both CONSTANT VARCHAR2(2) := 'BA';
Indicates that a blank line is to be placed both before and after the current line of code.

5.13.2 Setting the indentation
PROCEDURE set_indent
(indent_in IN NUMBER,
incr_indent_in IN NUMBER := c_incr_indent);
Sets the initial and incremental indentation.
FUNCTION indent RETURN NUMBER;
Returns the current value for initial indentation.
FUNCTION incr_indent RETURN NUMBER;
Returns the current value for incremental indentation.
202

[Appendix A] Appendix: PL/SQL Exercises

5.13.3 Setting the author
PROCEDURE set_author (author_in IN VARCHAR2);
Assigns a value for the author string used in program headers.
FUNCTION author RETURN VARCHAR2;
Returns the current author string.

5.13.4 Toggles affecting generated code
PLVgen offers a large selection of toggles or on−off switches, which you can use to modify the content of
code generated by this package. Each toggle has a "turn on" procedure, a "turn off" procedure, and a function
returning the current state of the toggle (on or off).
PROCEDURE usetrc;
PROCEDURE nousetrc;
FUNCTION using_trc RETURN BOOLEAN;
Controls inclusion of the PLVtrc startup and terminate procedures.
PROCEDURE useexc;
PROCEDURE nouseexc;
FUNCTION using_exc RETURN BOOLEAN;
Controls inclusion of PLVexc exception handlers in exception sections of programs.
PROCEDURE usehdr;
PROCEDURE nousehdr;
FUNCTION using_hdr RETURN BOOLEAN;
Controls inclusion of program headers in packages, procedures, and functions.
PROCEDURE usecmnt;
PROCEDURE nousecmnt;
FUNCTION using_cmnt RETURN BOOLEAN;
Controls inclusion of comment lines in generated code.
PROCEDURE usehlp;
PROCEDURE nousehlp;
FUNCTION using_hlp RETURN BOOLEAN;
Controls inclusion of help text stubs and generation of the help procedure in packages.
PROCEDURE usecor;
PROCEDURE nousecor;
FUNCTION using_cor RETURN BOOLEAN;
Controls inclusion of code required to CREATE OR REPLACE program units.
PROCEDURE useln;
PROCEDURE nouseln;
FUNCTION usingln RETURN BOOLEAN;
Controls inclusion of line numbers in prefix of generated code.
PROCEDURE usemin;
Turns off all the above toggles.
PROCEDURE usemax;
Turns on all the above toggles.
5.13.3 Setting the author

203

[Appendix A] Appendix: PL/SQL Exercises

5.13.5 Help generators
PROCEDURE helpproc
(prog_in IN VARCHAR2 := NULL, indent_in IN INTEGER := 0);
Generates a procedure that gives main−topic help for the specified program unit.
PROCEDURE helptext (context_in IN VARCHAR2 := PLVhlp.c_main);
Generates a comment block in the correct format to be used as online help text with the PLVhlp
package.

5.13.6 Generating a package
PROCEDURE pkg (name_in IN VARCHAR2);
Generates the skeleton structure for a package's specification and body.

5.13.7 Generating a procedure
PROCEDURE proc
(name_in IN VARCHAR2,
params_in IN VARCHAR2 := NULL,
exec_in IN VARCHAR2 := NULL,
incl_exc_in IN BOOLEAN := TRUE,
indent_in IN INTEGER := 0,
blank_lines_in IN VARCHAR2 := c_before);
Generates a procedure of the specified name. You can also provide a parameter list and one or more
executable lines. Finally, you can decide to include an exception section, indent the code, and perform
blank−line processing.

5.13.8 Generating functions
A function has a RETURN datatype. PLVgen allows you to generate string, numeric, date, and Boolean
functions. You can also supply literal and symbol default values. As a result, the func procedure is
overloaded as shown:
PROCEDURE func
(name_in IN VARCHAR2,
datadesc_in VARCHAR2,
defval_in IN VARCHAR2 := NULL,
length_in IN INTEGER := c_def_length,
incl_exc_in IN BOOLEAN := TRUE);
Generates a string function (since the datatype for the datdesc_in parameter is VARCHAR2).
PROCEDURE func
(name_in IN VARCHAR2,
datadesc_in datatype,
defval_in IN datatype := NULL,
incl_exc_in IN BOOLEAN := TRUE);
Generates a function of the specified datatype, which is either NUMBER, DATE, or BOOLEAN.
Notice that the default has the same datatype as the datadesc_in parameter. This is a default value
that is evaluated as a literal.
PROCEDURE func
(name_in IN VARCHAR2,
5.13.5 Help generators

204

[Appendix A] Appendix: PL/SQL Exercises
datadesc_in datatype,
defval_in IN VARCHAR2,
incl_exc_in IN BOOLEAN := TRUE);
Generates a function of the specified datatype, which is either NUMBER, DATE, or BOOLEAN.
Notice that the default in this version is a string. When you use this format, the default value is treated
as an expression that is not evaluated.

5.13.9 Generating get−and−set routines
Get−and−set routines provide a programmatic layer of code around a private data structure. As a result, the
get−and−sets or "gas" generators have associated with them a datatype. PLVgen allows you to generate string,
numeric, date, and Boolean get−and−sets. You can also supply literal and symbol default values. As a result,
the gas procedure is overloaded with the following flavors:
PROCEDURE gas
(name_in IN VARCHAR2,
valtype_in VARCHAR2,
defval_in IN VARCHAR2 := NULL,
length_in IN INTEGER := c_def_length);
Generates a string function (since the datatype for the datdesc_in parameter is VARCHAR2).
PROCEDURE gas
(name_in IN VARCHAR2,
valtype_in datatype,
defval_in IN datatype := NULL);
Generates get−and−sets of the specified datatype, which is either NUMBER, DATE, or
BOOLEAN. Notice that the default has the same datatype as the datadesc_in parameter. This is a
default value that is evaluated as a literal.
PROCEDURE gas
(name_in IN VARCHAR2, valtype_in datatype,
defval_in IN VARCHAR2);
Generates get−and−sets of the specified datatype, which is either NUMBER, DATE, or
BOOLEAN. Notice that the default in this version is a string. When you use this format, the default
value is an expression that is not evaluated.
PROCEDURE toggle (name_in IN VARCHAR2 := NULL);
Generates a variation of get−and−set based on a Boolean toggle. If you do not give a name,
turn_on and turn_off are used as the on−off procedure names.

5.13.10 Miscellaneous code generators
PROCEDURE curdecl
(cur_in IN VARCHAR2,
ind_in IN INTEGER := 0,
table_in IN VARCHAR2 := NULL,
collist_in IN VARCHAR2 := NULL,
gen_rec_in IN BOOLEAN := TRUE);
Generates a cursor declaration with the SQL statement formatted for maximum readability.
PROCEDURE cfloop (table_in IN VARCHAR2);
Generates a cursor FOR loop and framework for a cursor declaration.
PROCEDURE recfnd (table_in IN VARCHAR2);
5.13.9 Generating get−and−set routines

205

[Appendix A] Appendix: PL/SQL Exercises
Generates a function that returns TRUE if a record is found, FALSE otherwise.
PROCEDURE timer (plsql_in IN VARCHAR2);
Generates a function that returns TRUE if a record is found, FALSE otherwise.

5.12 PLVfk: Foreign Key
Interface

5.14 PLVhlp: Online Help
Architechture

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

5.13.9 Generating get−and−set routines

206