Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

1.4.2.4 Inheritance for object types

Oracle first introduced the object type (the closest that Oracle comes to a "class" in the, ahem, classical sense of object orientation) in Oracle8, but it was plagued by a severe lack of standard object functionality, most notably inheritance.

In Oracle9i, you can now define a hierarchy of object types in which the attributes and methods of a supertype are inherited or overwritten by the subtype. The following three object type specification definitions offer a simple example of such a hierarchy (in which food is the root, desserts are types of food, and cakes are types of desserts).

CREATE TYPE food_t AS OBJECT (

name VARCHAR2(100),

food_group VARCHAR2 (100),

grown_in VARCHAR2 (100),

MEMBER FUNCTION price RETURN NUMBER

)

NOT FINAL;

/

CREATE TYPE dessert_t UNDER food_t (

contains_chocolate CHAR(1),

year_created NUMBER(4),

OVERRIDING MEMBER FUNCTION price RETURN NUMBER

)

NOT FINAL;

/

CREATE TYPE cake_t UNDER dessert_t (

diameter NUMBER,

inscription VARCHAR2(200)

-- The cake_t type has no distinct pricing calculator.

);

/

Notice that the dessert_t type overrides the root's price function, but the cake_t type inherits the price function from dessert_t.

With inheritance, there is now a much more compelling reason to give object types a serious evaluation for use in your applications. You can find out more about object types and inheritance in Chapter 21.

1.4.2.5 Enhancements to pl/sql collections

PL/SQL collections are array-like structures that allow you to maintain lists of information. These lists can be of simple data, such as strings, or more complex structures, such as records. In Oracle9i, Oracle extends the features of collections with two major enhancements:

  • Support for multiple-level collections (i.e., collections within collections).

  • The ability to index the contents of one type of collection (associative arrays, previously known as index-by tables) by strings as well as integers.

Here is an example of using strings for the index values or row numbers in a collection:

DECLARE

TYPE population_t IS TABLE OF NUMBER INDEX BY VARCHAR2(64);

country_population population_t;

continent_population population_t;

howmany NUMBER;

row_id VARCHAR2(64);

BEGIN

country_population('Colymphia') := 100000;

country_population('Ribalizia') := 750000;

howmany := country_population('Colymphia');

row_id := continent_population.FIRST;

DBMS_OUTPUT.PUT_LINE (continent_population(row_id));

END;

/

Multi-level collections allow us to emulate N-dimensional arrays and, more generally, model arbitrarily complex data. For more information about these new collection features, check out Chapter 11.

1.4.2.6 Native compilation of pl/sql code

Prior to Oracle9i, compilation of PL/SQL source code always results in a representation (usually referred to as bytecode) that is stored in the database and is interpreted at runtime by a virtual machine implemented within Oracle that, in turn, runs natively on the given platform. Oracle9i introduces a new approach. PL/SQL source code may optionally be compiled into native object code that is linked into Oracle. Native compilation can result in significant improvement in overall application performance. (Its impact is felt in compute-intensive programs, but will not affect SQL performance.)

For instructions on how to take advantage of native compilation, see Chapter 19.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]