IST469-L10-Packages

advertisement

Packages: Containers for your program units

As more and more code is added to the database applications, the PL/SQL code will become larger and larger and managing the code will become challenging. It makes sense to create small program units that perform a specific task.

Locating all stored program units in a single place that all developers have access to allows developers to easily share and facilitates reuse possible. The development of new applications and maintenance of existing applications will also be faster and easier. Here we will discuss several methods that can be used to organize and share stored program units.

First, we will explore libraries. Then, we will take a look at packages.

Packages

A PL/SQL package is a database object that allows us to create a container for multiple program units. Using packages, program units can be organized into related groups. Private program units can be created. Additionally, variables may be shared among program units. Packages have a number of advantages that make them attractive for database designers:

Performance – When an object in a package is referenced for the first time the entire package is loaded into memory (SGA) making all other package objects immediately available for future calls to the package.

Top down design – The package specification can be written before the body allowing you to design the procedure and function interfaces (the parameter list) before they are actually constructed and implemented.

Object-oriented capability – Allows information hiding. Provides the capability to establish global variables that can be used by all procedures and functions within the package. It also allows you to make certain program units public or hide specific procedures and functions from the user allowing them to only be called by procedures and functions that are part of the package.

Learner Outcomes

MANAGEMENT OF SOLUTION DEVELOPMENT - You will achieve a deep level of knowledge and comprehension of the disciplines used in the development of information system solutions. You will develop the ability to apply these disciplines

to the solution of organizational and business problems. After completing this course, you will be able to:

1.

Design, construct and maintain a database and various database objects using procedural language constructs to solve problems

2.

Design and implement a complete problem solution using current database technology (Oracle 10g Database)

Specifically, after this lab you will be able to:

 Debate advantages of using Object-oriented technology

Explain database performance issues

 Implement database packages using Oracle 11g

Packages 101

Let’s take a look at how packages are built.

Packages are created in two parts: a package specification and a package body. The package specification (or package header) is where you identify the program units you want to be public (accessible by everyone). The format for creating the package header is:

CREATE OR REPLACE PACKAGE

CREATE OR REPLACE PACKAGE package_name

IS/AS declaration section

All Functions and/or Procedures (program units) headers

END;

Next, the package body is created. The package body contains the stored program unit code. The format for creating the package body is:

CREATE OR REPLACE PACKAGE BODY

CREATE OR REPLACE PACKAGE BODY package_name

IS/AS

All Functions and/or Procedures (program units)

END;

It is important to note that all variables declared in the declaration section of a package can be shared by all of the program units in the package. Therefore, if you declare a cursor, all program units in the package can use that cursor. All program units that are part of the package can reference all variables declared in a package. This is an efficient use of packages since these objects are created when the package is loaded and then reused over and over again.

Let’s try it!

The best way to make this apparent and obvious to you is with a demo. Let’s write 3 procedures around a package for vendors.

Do this: Open a new SQL file against the FUDGEMART database and save the script as vendors_package.sql

Package Name is here.

There are 3 program units inside this package.

Make sure you can get this portion to compile without errors before continuing.

In the code you just wrote you might notice there are no “guts” to any of our program units. This is what makes them a specification – they just define a contract for what’s required to execute the procedure or function.

In the next step we’ll continue in the same file and define the body for this specification, giving each of our program units some “guts” by including the details of what each program unit should do.

Do this: Add the following lines of code to your vendors_package.sql

Make sure you can get this code to compile before continuing. At this point you might be thinking WOW! That is a lot of

typing what did I just do??!?!?!?

Well you just created 3 program units a “scoped” under the package name fm_vendors specifically, these program units:

Getid returns the vendor_id given the vendor_name

Add inserts a new vendor into the vendors table.

Del deletes the vendor matching vendor_id

This will become more apparent in the next step where we write code to test our package:

Do this: Open a new SQL file against the FUDGEMART database and save the script as test_vendors_package.sql

First insert a new Vendor by executing the add procedure from our fm_vendors package:

Let’s see the vendor we just added to do that we’ll retrieve the vendor by name using the getid function.

You should see the new vendor in your output window:

Next, let’s test out del procedure by using it to remove our new vendor:

And then finally verify that the procedure did its job:

Lab Assignment

Use the FUDGEMART schema to complete each of the following problems. For each question you must provide a screenshot of your code and a screen shot which proves your code executed properly.

1.

Building on your work with the fm_vendors package add a new procedure called upd to the package, which given the vendor id, name, phone, and website as input will execute an SQL update statement on the vendor matching the vendor id. Be sure to include the procedure in the package specification along with the package body.

2.

Write SQL code which demonstrates your fm_vendors.upd package procedure is executing properly. Please add your own vendor to update. Do not update an existing vendor!

3.

Add another function to the fm_vendors package to count the number of vendors. Call this function

fm_vendors.count Be sure to include the procedure in the package specification along with the package body.

4.

Write SQL SELECT code which demonstrates your fm_vendors.count package function is executing properly.

5.

How does the package specification differ from the package body?

Download