12 Creating Packages

Aris Global Confidential

Objectives

After completing this lesson, you should be able to

do the following: • Describe packages and list their possible components • Create a package to group together related variables, cursors, constants, exceptions, procedures, and functions • Designate a package construct as either public or private • Invoke a package construct • Describe a use for a bodiless package

12-7 Aris Global Confidential

• The REPLACE option drops and recreates the

package specification. • Variables declared in the package specification are initialized to NULL by default. • All the constructs declared in a package specification are visible to users who are granted privileges on the package.

12-10 Aris Global Confidential

CREATE [OR REPLACE] PACKAGE BODY package_name

• The REPLACE option drops and recreates the

package body. • Identifiers defined only in the package body are private constructs. These are not visible outside the package body. • All private constructs must be declared before they are used in the public constructs.

12-18 Aris Global Confidential

To remove the package specification and the body,

To remove the package body, use the following syntax:

DROP PACKAGE BODY package_name;

12-19 Aris Global Confidential

Guidelines for Developing Packages

• Construct packages for general use.

• Define the package specification before the body. • The package specification should contain only those constructs that you want to be public. • Place items in the declaration part of the package body when you must maintain them throughout a session or across transactions. • Changes to the package specification require recompilation of each referencing subprogram. • The package specification should contain as few constructs as possible.

12-20 Aris Global Confidential

Advantages of Packages

• Modularity: Encapsulate related constructs.

• Easier application design: Code and compile specification and body separately. • Hiding information: – Only the declarations in the package specification are visible and accessible to applications. – Private constructs in the package body are hidden and inaccessible. – All coding is hidden in the package body.

12-21 Aris Global Confidential

Advantages of Packages

• Added functionality: Persistency of variables

and cursors • Better performance: – The entire package is loaded into memory when the package is first referenced. – There is only one copy in memory for all users. – The dependency hierarchy is simplified. • Overloading: Multiple subprograms of the same name

12-22 Aris Global Confidential

Summary

In this lesson, you should have learned how to:

• Improve organization, management, security, and performance by using packages • Group related procedures and functions together in a package • Change a package body without affecting a package specification • Grant security access to the entire package

12-23 Aris Global Confidential

Summary

In this lesson, you should have learned how to:

• Hide the source code from users • Load the entire package into memory on the first call • Reduce disk access for subsequent calls • Provide identifiers for the user session