Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Declaring and Initializing Objects

CREATE TYPE IntlAddress_typ UNDER Address_typ(...);

--Return REFs for those Person_typ objects that are instances of

--the Student_typ subtype, and NULL REFs otherwise.

SELECT TREAT(REF(p) AS REF Student_typ) FROM Person_v p;

--Example of using TREAT for assignment...

--Return REFs for those Person_type objects that are instances of

--Employee_type or Student_typ, or any of their subtypes.

SELECT REF(p) FROM Person_v P WHERE VALUE(p) IS OF (Employee_typ, Student_typ);

--Similar to above, but do not allow any subtypes of Student_typ. SELECT REF(p) FROM Person_v p WHERE VALUE(p) IS OF(ONLY Student_typ);

--The results of REF and DEREF can include objects of Person_typ

--and its subtypes such as Employee_typ and Student_typ.

SELECT REF(p) FROM Person_v p;

SELECT DEREF(REF(p)) FROM Person_v p;

Declaring and Initializing Objects

Once 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.

Declaring Objects

You can use object types wherever built-in types such as CHAR or NUMBER can be used. In the block below, you declare object r of type Rational. Then, you call the constructor for object type Rational to initialize the object. The call assigns the values 6 and 8 to attributes num and den, respectively.

DECLARE

r Rational; BEGIN

r := Rational(6, 8); dbms_output.put_line(r.num); -- prints 6

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 Account to specify the datatype of a formal parameter:

DECLARE

...

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

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

Using PL/SQL Object Types 12-11

Declaring and Initializing Objects

DECLARE

...

FUNCTION get_acct (acct_id IN INTEGER) RETURN Account IS ...

Initializing 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. Consider the following example:

DECLARE

r Rational; -- r becomes atomically null BEGIN

r := Rational(2,3); -- r becomes 2/3

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, as the following example shows:

DECLARE

r Rational; BEGIN

r Rational := Rational(1,2); -- r becomes 1/2 r := NULL; -- r becomes atomically null

IF r IS NULL THEN ... -- condition yields TRUE

A good programming practice is to initialize an object in its declaration, as shown in the following example:

DECLARE

r Rational := Rational(2,3); -- r becomes 2/3

How PL/SQL Treats Uninitialized Objects

In an expression, attributes of an uninitialized object evaluate to NULL. Trying to assign values to attributes of an uninitialized object raises the predefined exception ACCESS_INTO_NULL. When applied to an uninitialized object or its attributes, the IS NULL comparison operator yields TRUE.

The following example illustrates the difference between null objects and objects with null attributes:

DECLARE

 

 

r Rational;

-- r is atomically null

BEGIN

 

 

IF r IS NULL THEN ...

-- yields TRUE

IF r.num IS NULL THEN ...

-- yields TRUE

r := Rational(NULL, NULL);

-- initializes r

r.num := 4;

-- succeeds because r is no longer atomically null

 

-- even though all its attributes are null

r := NULL;

-- r becomes atomically null again

r.num := 4;

-- raises ACCESS_INTO_NULL

EXCEPTION

 

 

WHEN ACCESS_INTO_NULL THEN

...

END;

12-12 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g