10.3 Subprogram Encapsulation: Introduction to Packages


Packages provide a mechanism to logically group smaller program units together. The grouping of procedures into a package modularizes the code. Physically, the use of packages means fewer files and fewer modules to manage. Programmers are more inclined to reuse modular code.

Programmers who begin working on the system, built with packages, quickly grasp the overall architecture because they can learn the system in a top-down fashion ”looking at whole modules and how they interface.

Figure 10-3 illustrates two procedures. To grant the use of these procedures to other applications requires a GRANT EXECUTE on each procedure. Imagine 50 procedures. Additional functionality means more procedures, more grants, and more files.

Figure 10-3. Stand-Alone Procedures as Components in an Application.

graphics/10fig03.gif

Additionally, a stand-alone procedure can send and receive scalar parameters with a type of NUMBER, VARCHAR2, and DATE. The language allows you to define composite structures such as records. A stand-alone procedure cannot return a composite record structure unless another package specification defines that structure. Relying on individual procedures and functions is limiting in large-scale development.

Procedures P1 and P2 can be easily merged into a single package. This makes sense if these procedures perform related operations. Procedure P1 does an INSERT. Procedure P2 does a SELECT. Consider these SQL statements to be on the same table.

The migration process includes copying the procedure body of each into a single package body. The procedure interface definitions become the package specification. After this, you include new procedures and functions to enhance the overall functionality of the package. Figure 10-4 shows the encapsulation of procedures P1 and P2 into a single package.

Figure 10-4. Encapsulation of Procedures.

graphics/10fig04.gif

After the package is complete, the specification (shown in the top right of Figure 10-4) is compiled, then the body. Additional procedures and functions are added to the package.



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