REM **************************************************************
REM David L. Hunt (file author) distributes this and other
REM files/scripts for educational purposes only, to illustrate the
REM use or application of various computing techniques. The author
REM makes no warranty regarding this script's fitness for any
REM specific industrial application nor is
REM there any claim that this or any similarly-distributed scripts
REM are error free or should be used for any purpose other than
REM illustration.
REM
REM Please contact the author via email (see address below) when
REM you have comments, suggestions, and/or difficulties with this
REM packages functions.
REM
REM [Please keep the above disclaimer and the embedded electronic
REM documentation with this script.]
REM **************************************************************
REM About this script/file:
REM
REM NAME: DH_FILE.SQL - PL/SQL code to create a package (DH_FILE)
REM providing the following 5 procedures and 1 function:
REM 1) DH_FILE.FILE_OPEN (<file#>,<path>,<file>,
REM <'W','R','A'>)
REM [Write, Read, Append] respectively.
REM 2) DH_FILE.FILE_CLOSE (<file#>)
REM ...uses handle from FILE array.
REM 3) DH_FILE.FILE_PRT (<file#>,<output string>)
REM ...uses handle from FILE array.
REM 4) DH_FILE.FILE_STAGE (<file#>,<output string>)
REM ...uses handle from FILE array.
REM 5) DH_FILE.FILE_FLUSH (<file#>)
REM ...uses handle from FILE array.
REM 6) DH_FILE.READ_NEXT (<file#>)
REM ...RETURNs next line from file_handle
REM ...uses handle from FILE array.
REM
REM This package facilitates usage of the Oracle UTL_FILE
REM package. The UTL_FILE file operations include:
REM FOPEN, IS_OPEN, FCLOSE, FCLOSE_ALL, GET_LINE, PUT,
REM NEW_LINE, PUT_LINE, PUTF, and FFLUSH
REM
REM VERY IMPORTANT NOTE!!!: For any write use of UTL_FILE,
REM the instance must be prepared to write to O/S directories
REM via the init.ora parameter,
REM "UTL_FILE_DIR = <path><or "*", meaning "all directories">.
REM
REM AUTHOR: Dave Hunt, Co-Principal, DaSages
REM dave@Dasages.com
REM 1-801-TRY-DAVE (1-801-879-3283)
REM
REM **************************************************************
REM Maintenance History:
REM
REM 19-MAR-97: Original Code
REM 22-DEC-98: Enhanced to allow for multiple file handling by number
REM **************************************************************
REM **************************************************************
REM DH_FILE Package Specification
REM **************************************************************
create or replace package dh_file is
procedure FILE_OPEN (which_file in binary_integer,
path in varchar2,
filename in varchar2,
read_write_append in varchar2);
procedure FILE_CLOSE (which_file in binary_integer);
procedure FILE_PRT (which_file in binary_integer,
str_to_prt in varchar2);
procedure FILE_STAGE (which_file in binary_integer,
str_to_prt in varchar2);
procedure FILE_FLUSH (which_file in binary_integer);
function READ_NEXT (which_file in binary_integer) return varchar2;
-- pragma restrict_references(read_next,WNDS);
end;
/
REM **************************************************************
REM DH_FILE Package Body
REM **************************************************************
create or replace package body dh_file is
-- **************************************************************
-- Packaged Global Procedure Definition: DH_FILE.FILE_OPEN
-- **************************************************************
type file_handle_stencil is table of utl_file.file_type
index by binary_integer;
file_handles file_handle_stencil;
file_handle utl_file.file_type;
procedure FILE_OPEN (which_file in binary_integer,
path in varchar2,
filename in varchar2,
read_write_append in varchar2) is
begin
file_handles(which_file)
:= utl_file.fopen(
location => path,
filename => filename,
open_mode => read_write_append,
max_linesize => 32767);
end;
-- **************************************************************
-- Packaged Global Procedure Definition: DH_FILE.FILE_CLOSE
-- **************************************************************
procedure FILE_CLOSE (which_file in binary_integer) is
begin
utl_file.fclose(file_handles(which_file));
end FILE_CLOSE;
-- **************************************************************
-- Packaged Global Procedure Definition: DH_FILE.FILE_PRT
-- **************************************************************
procedure FILE_PRT (which_file in binary_integer,
str_to_prt in varchar2) is
begin
utl_file.put_line(file_handles(which_file), str_to_prt);
end FILE_PRT;
-- **************************************************************
-- Packaged Global Procedure Definition: DH_FILE.FILE_STAGE
-- **************************************************************
procedure FILE_STAGE (which_file in binary_integer,
str_to_prt in varchar2) is
begin
utl_file.put (file_handles(which_file), str_to_prt);
end FILE_STAGE;
-- **************************************************************
-- Packaged Global Procedure Definition: DH_FILE.FILE_FLUSH
-- **************************************************************
procedure FILE_FLUSH (which_file in binary_integer) is
begin
utl_file.fflush (file_handles(which_file));
end FILE_FLUSH;
-- **************************************************************
-- Packaged Global Function Definition: DH_FILE.READ_NEXT
-- **************************************************************
function READ_NEXT (which_file in binary_integer)
return varchar2 is
hold_text varchar(2000);
begin
utl_file.get_line (file_handles(which_file), hold_text);
return hold_text;
exception
when no_data_found then
return '<EOF>';
end READ_NEXT;
end;
/
REM **************************************************************
REM End of Package: DH_FILE
REM **************************************************************