Construction of the package is straightforward; see the invasions.pkg file on this book’s web page for the details. Here are the highlights.
First, create a named constant for the pipe name so it is not repeated throughout:
CREATE OR REPLACE PACKAGE BODY invpkg IS pipename CONSTANT VARCHAR2(20) := 'invasions'; stat PLS_INTEGER;
The sendinfo procedure makes certain to reset the buffer; error handling is left out, but you should probably check for a nonzero status and report the error:
PROCEDURE sendinfo ( country IN VARCHAR2, date_of_invasion DATE, people_killed NUMBER ) IS BEGIN DBMS_PIPE.RESET_BUFFER; DBMS_PIPE.PACK_MESSAGE (country); DBMS_PIPE.PACK_MESSAGE (date_of_invasion); DBMS_PIPE.PACK_MESSAGE (people_killed); stat := DBMS_PIPE.SEND_MESSAGE (pipename, timeout => 10); END;
The record overloading simply calls the other implementation; try hard to avoid any redundant code:
PROCEDURE sendinfo (rec IN inv_rectype) IS BEGIN sendinfo ( rec.country, rec.date_of_invasion, rec.people_killed); END;
The nextinfo function receives and unpacks without any surprises (see the file for details). Finally, you have to deal with pipe creation. You want to make sure the pipe is created before anyone sends or receives. Since the pipe is private, the creation step must be done explicitly. The best place for this to happen is the initialization section at the bottom of the package body: ...