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

Components of an Object Type

Components of an Object Type

An object type encapsulates data and operations. You can declare attributes and methods in an object type spec, but not constants, exceptions, cursors, or types. You must declare at least one attribute (the maximum is 1000). Methods are optional.

Attributes

Like a variable, an attribute is declared with a name and datatype. The name must be unique within the object type (but can be reused in other object types). The datatype can be any Oracle type except:

LONG and LONG RAW

ROWID and UROWID

The PL/SQL-specific types BINARY_INTEGER (and its subtypes), BOOLEAN, PLS_ INTEGER, RECORD, REF CURSOR, %TYPE, and %ROWTYPE

Types defined inside a PL/SQL package

You cannot initialize an attribute in its declaration using the assignment operator or DEFAULT clause. Also, you cannot impose the NOT NULL constraint on an attribute. However, objects can be stored in database tables on which you can impose constraints.

The kind of data structure formed by a set of attributes depends on the real-world object being modeled. For example, to represent a rational number, which has a numerator and a denominator, you need only two INTEGER variables. On the other hand, to represent a college student, you need several VARCHAR2 variables to hold a name, address, phone number, status, and so on, plus a VARRAY variable to hold courses and grades.

The data structure can be very complex. For example, the datatype of an attribute can be another object type (called a nested object type). That lets you build a complex object type from simpler object types. Some object types such as queues, lists, and trees are dynamic, meaning that they can grow as they are used. Recursive object types, which contain direct or indirect references to themselves, allow for highly sophisticated data models.

Methods

In general, a method is a subprogram declared in an object type spec using the keyword MEMBER or STATIC. The method cannot have the same name as the object type or any of its attributes. MEMBER methods are invoked on instances, as in

instance_expression.method()

However, STATIC methods are invoked on the object type, not its instances, as in

object_type_name.method()

Like packaged subprograms, methods have two parts: a specification and a body. The specification (spec for short) consists of a method name, an optional parameter list, and, for functions, a return type. The body is the code that executes to perform a specific task.

For each method spec in an object type spec, there must either be a corresponding method body in the object type body, or the method must be declared NOT INSTANTIABLE to indicate that the body is only present in subtypes of this type. To match method specs and bodies, the PL/SQL compiler does a token-by-token comparison of their headers. The headers must match exactly.

Using PL/SQL Object Types 12-5

Components of an Object Type

Like an attribute, a formal parameter is declared with a name and datatype. However, the datatype of a parameter cannot be size-constrained. The datatype can be any Oracle type except those disallowed for attributes. (See "Attributes" on page 12-5.) The same restrictions apply to return types.

What Languages can I Use for Methods of Object Types?

Oracle lets you implement object methods in PL/SQL, Java or C. You can implement type methods in Java or C by providing a call specification in your type. A call spec publishes a Java method or external C function in the Oracle data dictionary. It publishes the routine by mapping its name, parameter types, and return type to their SQL counterparts. To learn how to write Java call specs, see Oracle Database Java Developer's Guide. To learn how to write C call specs, see Oracle Database Application Developer's Guide - Fundamentals.

How Object Types Handle the SELF Parameter

MEMBER methods accept a built-in parameter named SELF, which is an instance of the object type. It is always the first parameter passed to a MEMBER method. If you do not declare it, it declared automatically.

For example, the transform method declares SELF as an IN OUT parameter:

CREATE TYPE Complex AS OBJECT (

MEMBER FUNCTION transform (SELF IN OUT Complex) ...

You must specify the same datatype for SELF as the original object.

In MEMBER functions, if SELF is not declared, its parameter mode defaults to IN.

In MEMBER procedures, if SELF is not declared, its parameter mode defaults to IN OUT.

You cannot specify the OUT parameter mode for SELF.

STATIC methods cannot accept or reference SELF.

In the method body, SELF denotes the object whose method was invoked. You can refer to SELF.attribute_name or SELF.member_name, to make clear that you are referring to that object rather than something in a supertype. As the following example shows, methods can reference the attributes of SELF without a qualifier:

CREATE FUNCTION gcd (x INTEGER, y INTEGER) RETURN INTEGER AS -- find greatest common divisor of x and y

ans INTEGER; BEGIN

IF (y <= x) AND (x MOD y = 0) THEN ans := y; ELSIF x < y THEN ans := gcd(y, x);

ELSE ans := gcd(y, x MOD y); END IF;

RETURN ans; END;

CREATE TYPE Rational AS OBJECT ( num INTEGER,

den INTEGER,

MEMBER PROCEDURE normalize,

...

);

CREATE TYPE BODY Rational AS

MEMBER PROCEDURE normalize IS

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

Components of an Object Type

g INTEGER; BEGIN

g := gcd(SELF.num, SELF.den);

g := gcd(num, den); -- equivalent to previous statement num := num / g;

den := den / g; END normalize;

...

END;

From a SQL statement, if you call a MEMBER method on a null instance (that is, SELF is null), the method is not invoked and a null is returned. From a procedural statement, if you call a MEMBER method on a null instance, PL/SQL raises the predefined exception SELF_IS_NULL before the method is invoked.

Overloading

Like packaged subprograms, methods of the same kind (functions or procedures) can be overloaded. You can use the same name for different methods if their formal parameters are different enough to tell apart. When you call one of the methods, PL/SQL finds it by comparing the actual parameters with each list of formal parameters.

A subtype can also overload methods it inherits from its supertype. In this case, the methods can have exactly the same formal parameters.

You cannot overload two methods whose formal parameters differ only in their mode. You cannot overload two member functions that differ only in return type. For more information, see "Overloading Subprogram Names" on page 8-9.

MAP and ORDER Methods

Instances of an object type have no predefined order. To put them in order for comparisons or sorting, PL/SQL calls a MAP method supplied by you. In the following example, the keyword MAP indicates that method convert() orders Rational objects by mapping them to REAL values:

CREATE

TYPE Rational AS OBJECT (

num

INTEGER,

den

INTEGER,

MAP

MEMBER FUNCTION convert RETURN REAL,

);

 

CREATE

TYPE BODY Rational AS

MAP

MEMBER FUNCTION convert RETURN REAL IS

BEGIN

 

RETURN num / den;

END

convert;

END;

 

PL/SQL uses the map method to evaluate Boolean expressions such as x > y, and to do comparisons implied by the DISTINCT, GROUP BY, and ORDER BY clauses. MAP method convert() returns the relative position of an object in the ordering of all

Rational objects.

An object type can contain only one MAP method. It accepts the built-in parameter SELF and returns one of the following scalar types: DATE, NUMBER, VARCHAR2, or an ANSI SQL type such as CHARACTER or REAL.

Using PL/SQL Object Types 12-7

Components of an Object Type

Alternatively, you can supply PL/SQL with an ORDER method. An object type can contain only one ORDER method, which must be a function that returns a numeric result. In the following example, the keyword ORDER indicates that method match() compares two objects:

CREATE TYPE Customer AS OBJECT ( id NUMBER,

name VARCHAR2(20), addr VARCHAR2(30),

ORDER MEMBER FUNCTION match (c Customer) RETURN INTEGER

);

CREATE TYPE BODY Customer AS

ORDER MEMBER FUNCTION match (c Customer) RETURN INTEGER IS

BEGIN

IF id < c.id THEN

RETURN -1; -- any negative number will do

ELSIF id > c.id THEN

RETURN 1; -- any positive number will do

ELSE

RETURN 0;

END IF;

END;

END;

Every ORDER method takes two parameters: the built-in parameter SELF and another object of the same type. If c1 and c2 are Customer objects, a comparison such as c1 > c2 calls match() automatically. The method returns a negative number, zero, or a positive number signifying that SELF is less than, equal to, or greater than the other parameter. If either parameter passed to an ORDER method is null, the method returns a null.

Guidelines

A MAP method, acting like a hash function, maps object values into scalar values, which are then compared using operators such as <, =, and so on. An ORDER method simply compares one object value to another.

You can declare a MAP method or an ORDER method but not both. If you declare either method, you can compare objects in SQL and procedural statements. If you declare neither method, you can compare objects only in SQL statements and only for equality or inequality. (Two objects of the same type are equal only if the values of their corresponding attributes are equal.)

When sorting or merging a large number of objects, use a MAP method. One call maps all the objects into scalars, then sorts the scalars. An ORDER method is less efficient because it must be called repeatedly (it can compare only two objects at a time). You must use a MAP method for hash joins because PL/SQL hashes on the object value.

Constructor Methods

Every object type has a constructor method, a function with the same name as the object type that initializes and returns a new instance of that object type.

Oracle generates a default constructor for every object type, with formal parameters that match the order, names, and datatypes of the object attributes.

You can define your own constructor methods, either overriding a system-defined constructor, or defining a new function with a different signature.

PL/SQL never calls a constructor implicitly. You must call it explicitly.

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

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