Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / PL_SQL / b14261.pdf
Скачиваний:
27
Добавлен:
12.05.2015
Размер:
4.36 Mб
Скачать

12

Using PL/SQL With Object Types

Object-oriented programming is especially suited for building reusable components and complex applications. In PL/SQL, object-oriented programming is based on object types. They let you model real-world objects, separate interfaces and implementation details, and store object-oriented data persistently in the database.

This chapter contains these topics:

Declaring and Initializing Objects in PL/SQL

Manipulating Objects in PL/SQL

Defining SQL Types Equivalent to PL/SQL Collection Types

Using PL/SQL Collections with SQL Object Types

Using Dynamic SQL With Objects

For information about object types, see Oracle Database Application Developer's Guide - Object-Relational Features.

Declaring and Initializing Objects in PL/SQL

An object type can represent any real-world entity. For example, an object type can represent a student, bank account, computer screen, rational number, or data structure such as a queue, stack, or list.

Currently, you cannot define object types in a PL/SQL block, subprogram, or package. You can define them interactively in SQL*Plus using the SQL statement CREATE TYPE. See Example 1–17, "Defining an Object Type" on page 1-17.

For information on the CREATE TYPE SQL statement, see Oracle Database SQL Reference. For information on the CREATE TYPE BODY SQL statement, see Oracle Database SQL Reference.

After an object type is defined and installed in the schema, you can use it to declare objects in any PL/SQL block, subprogram, or package. For example, you can use the object type to specify the datatype of an attribute, column, variable, bind variable, record field, table element, formal parameter, or function result. At run time, instances of the object type are created; that is, objects of that type are instantiated. Each object can hold different values.

Such objects follow the usual scope and instantiation rules. In a block or subprogram, local objects are instantiated when you enter the block or subprogram and cease to exist when you exit. In a package, objects are instantiated when you first reference the package and cease to exist when you end the database session.

Using PL/SQL With Object Types 12-1

Declaring and Initializing Objects in PL/SQL

Example 12–1 shows how to create an object type, object body type, and a table of object types.

Example 12–1 Working With Object Types

CREATE TYPE address_typ AS OBJECT (

street

VARCHAR2(30),

city

VARCHAR2(20),

state

CHAR(2),

postal_code

VARCHAR2(6) );

/

 

CREATE TYPE employee_typ AS OBJECT (

employee_id

NUMBER(6),

first_name

VARCHAR2(20),

last_name

VARCHAR2(25),

email

VARCHAR2(25),

phone_number

VARCHAR2(20),

hire_date

DATE,

job_id

VARCHAR2(10),

salary

NUMBER(8,2),

commission_pct

NUMBER(2,2),

manager_id

NUMBER(6),

department_id

NUMBER(4),

address

address_typ,

MAP MEMBER FUNCTION get_idno RETURN NUMBER,

MEMBER PROCEDURE display_address ( SELF IN OUT NOCOPY employee_typ ) );

/

CREATE TYPE BODY employee_typ AS

MAP MEMBER FUNCTION get_idno RETURN NUMBER IS BEGIN

RETURN employee_id; END;

MEMBER PROCEDURE display_address ( SELF IN OUT NOCOPY employee_typ ) IS BEGIN

DBMS_OUTPUT.PUT_LINE(first_name || ' ' || last_name); DBMS_OUTPUT.PUT_LINE(address.street); DBMS_OUTPUT.PUT_LINE(address.city || ', ' || address.state || ' ' ||

address.postal_code);

END;

END;

/

CREATE TABLE employee_tab OF employee_typ;

Declaring Objects in a PL/SQL Block

You can use object types wherever built-in types such as CHAR or NUMBER can be used. In Example 12–2, you declare object emp of type employee_typ. Then, you call the constructor for object type employee_typ to initialize the object.

Example 12–2 Declaring Object Types in a PL/SQL Block

DECLARE

emp employee_typ; -- emp is atomically null BEGIN

-- call the constructor for employee_typ

emp := employee_typ(315, 'Francis', 'Logan', 'FLOGAN', '555.777.2222', '01-MAY-04', 'SA_MAN', 11000, .15, 101, 110, address_typ('376 Mission', 'San Francisco', 'CA', '94222'));

DBMS_OUTPUT.PUT_LINE(emp.first_name || ' ' || emp.last_name); -- display details emp.display_address(); -- call object method to display details

12-2 Oracle Database PL/SQL User’s Guide and Reference

Declaring and Initializing Objects in PL/SQL

END;

/

You can declare objects as the formal parameters of functions and procedures. That way, you can pass objects to stored subprograms and from one subprogram to another. In the next example, you use object type employee_typ to specify the datatype of a formal parameter:

PROCEDURE open_acct (new_acct IN OUT employee_typ) IS ...

In the following example, you use object type employee_typ to specify the return type of a function:

FUNCTION get_acct (acct_id IN NUMBER) RETURN employee_typ IS ...

How PL/SQL Treats Uninitialized Objects

Until you initialize an object by calling the constructor for its object type, the object is atomically null. That is, the object itself is null, not just its attributes.

A null object is never equal to another object. In fact, comparing a null object with any other object always yields NULL. Also, if you assign an atomically null object to another object, the other object becomes atomically null (and must be reinitialized). Likewise, if you assign the non-value NULL to an object, the object becomes atomically null.

In an expression, attributes of an uninitialized object evaluate to NULL. When applied to an uninitialized object or its attributes, the IS NULL comparison operator yields

TRUE.

Example 12–3 illustrates null objects and objects with null attributes.

Example 12–3 Null Objects in a PL/SQL Block

DECLARE

emp employee_typ; -- emp is atomically null BEGIN

IF emp IS NULL THEN DBMS_OUTPUT.PUT_LINE('emp is NULL #1'); END IF; IF emp.employee_id IS NULL THEN

DBMS_OUTPUT.PUT_LINE('emp.employee_id is NULL #1'); END IF;

emp.employee_id := 330;

IF emp IS NULL THEN DBMS_OUTPUT.PUT_LINE('emp is NULL #2'); END IF; IF emp.employee_id IS NULL THEN

DBMS_OUTPUT.PUT_LINE('emp.employee_id is NULL #2'); END IF;

emp := employee_typ(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, address_typ(NULL, NULL, NULL, NULL));

-- emp := NULL; -- this would have made the following IF statement TRUE IF emp IS NULL THEN DBMS_OUTPUT.PUT_LINE('emp is NULL #3'); END IF;

IF emp.employee_id IS NULL THEN DBMS_OUTPUT.PUT_LINE('emp.employee_id is NULL #3');

END IF;

EXCEPTION

WHEN ACCESS_INTO_NULL THEN

DBMS_OUTPUT.PUT_LINE('Cannot assign value to NULL object');

END;

/

The output is:

Using PL/SQL With Object Types 12-3

Соседние файлы в папке PL_SQL