10.7 Data Abstraction


You can simplify complex application logic by building abstract structures on top of the existing language structures. The following package implements a STACK built using a PL/SQL index-by table. This is a last-in-first-out (LIFO) stack of student names . This demonstrates the ability to bind data and operations together and restrict the interface to the definitions in the package specification.

The user of this package must conform to the push/pop operations in the specification. Although the package body implements this structure with an index-by table, the implementation could be a database table, a set of tables, or tables in another database ”this uses a PL/SQL table.

The stack package has the following operations and exceptions:

Subprogram

Purpose

CLEAR

This procedure empties the stack.

PUSH

This pushes a name onto the stack.

POP

This is overloaded. You can use the function to pop a name. You can also use the procedure form.

STACK_NOT_EMPTY

This returns TRUE if the stack has any names.

Exceptions

 

UNDERFLOW

A pop of an empty stack is an exception, similar to subtracting 1 from a POSITIVE number that has a current value of 1. A divide by zero is an exception because it produces a result not in the set of numbers . We raise the exception UNDERFLOW following the pop of an empty stack.

First, look at how one might use the stack. We want an interface to be simple. The following PL/SQL code selects student names from the STUDENTS table and pushes the names onto the stack. Following that the names are poped within a call to DBMS_OUTPUT.

 
 BEGIN     FOR rec IN (SELECT student_name FROM students) LOOP         students_stack.push(rec.student_name);     END LOOP;     WHILE (students_stack.stack_not_empty) LOOP         dbms_output.put_line(students_stack.pop);     END LOOP; END; 

Running the PL/SQL block produces the following names:

 
 William Steven Kathryn Mary John 

The specification and body for the stack are shown next . You can easily modify this for a FIFO LIST structure. The component stored in the index-by table is a VARCHAR2. You can build a stack of STUDENT%ROWTYPE structures.

 
 CREATE OR REPLACE PACKAGE students_stack IS     --     -- Empties the stack.     --     PROCEDURE clear_stack;     --     -- Push a name onto the stack.     --     PROCEDURE push (name IN students.student_name%TYPE);     --     -- Pop and return a name from the stack.     --     PROCEDURE pop (name OUT students.student_name%TYPE);     FUNCTION pop RETURN students.student_name%TYPE;     --     -- Check status. Return true if stack has data     --     FUNCTION stack_not_empty RETURN BOOLEAN;     -- This exception is raised with a pop     -- on an empty stack.     --     UNDERFLOW exception; END students_stack; 

The package body for this stack encapsulates the stack that is implemented as a PL/SQL index-by table.

 
 CREATE OR REPLACE PACKAGE BODY students_stack IS     TYPE table_type IS TABLE OF students.student_name%TYPE     INDEX BY BINARY_INTEGER;     the_stack table_type;     stack_pointer BINARY_INTEGER := 0;     PROCEDURE clear_stack IS     BEGIN         stack_pointer := 0;     END clear_stack;     PROCEDURE push (name IN students.student_name%TYPE) IS     BEGIN         stack_pointer := stack_pointer +1;         the_stack(stack_pointer) := name;     END push;     PROCEDURE pop (name OUT students.student_name%TYPE) IS     BEGIN         name := the_stack(stack_pointer);         stack_pointer := stack_pointer1;     EXCEPTION         WHEN NO_DATA_FOUND THEN RAISE underflow;     END pop;     FUNCTION pop RETURN students.student_name%TYPE IS     BEGIN         stack_pointer := stack_pointer1;         RETURN the_stack(stack_pointer + 1);     EXCEPTION         WHEN NO_DATA_FOUND THEN             stack_pointer := stack_pointer + 1;             RAISE underflow;     END pop;     FUNCTION stack_not_empty RETURN BOOLEAN IS     BEGIN         RETURN (stack_pointer > 0);     END stack_not_empty; END students_stack; 


Programming Oracle Triggers and Stored Procedures
Programming Oracle Triggers and Stored Procedures (3rd Edition) (Prentice Hall PTR Oracle Series)
ISBN: 0130850330
EAN: 2147483647
Year: 2003
Pages: 111
Authors: Kevin Owens

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net