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

12

Using PL/SQL Object Types

... It next will be right

To describe each particular batch: Distinguishing those that have feathers, and bite,

From those that have whiskers, and scratch. —Lewis Carroll

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. You might find object types helpful when writing programs that interoperate with Java or other object-oriented languages.

This chapter contains these topics:

Overview of PL/SQL Object Types on page 12-1

What Is an Object Type? on page 12-2

Why Use Object Types? on page 12-3

Structure of an Object Type on page 12-3

Components of an Object Type on page 12-5

Defining Object Types on page 12-9

Declaring and Initializing Objects on page 12-11

Accessing Object Attributes on page 12-13

Defining Object Constructors on page 12-13

Calling Object Constructors on page 12-14

Calling Object Methods on page 12-15

Sharing Objects through the REF Modifier on page 12-16

Manipulating Objects through SQL on page 12-17

Overview of PL/SQL Object Types

Before reading this chapter, you should be familiar with some backgroup topics:

Object-oriented programming, including the idea of abstraction.

The ideas of attributes and methods. In other languages, these are part of classes. In SQL and PL/SQL, they are part of object types.

Using PL/SQL Object Types 12-1

What Is an Object Type?

The SQL statement CREATE TYPE.

The best place to read about all of Oracle's object-oriented features is... The remainder of this chapter focuses on issues that are specific to PL/SQL.

What Is an Object Type?

An object type is a user-defined composite datatype representing a data structure and functions and procedures to manipulate the data. With scalar datatypes, each variable holds a single value. With collections, all the elements have the same type. Only object types let you associate code with the data.

The variables within the data structure are called attributes. The functions and procedures of the object type are called methods.

We usually think of an object as having attributes and actions. For example, a baby has the attributes gender, age, and weight, and the actions eat, drink, and sleep. Object types let you represent such real-world behavior in an application.

When you define an object type using the CREATE TYPE statement, you create an abstract template for some real-world object. The template specifies the attributes and behaviors the object needs in the application environment.

Figure 12–1 Each Application Uses a Subset of Object Attributes

Employee Attributes

 

Payroll App

name

date_hired

 

name

 

address

status

 

id_number

 

phone_number

department

 

ss_number

 

date_born

job_title

 

salary

 

sex

salary

 

commission

 

marital_status

commission

 

benefits_choices

 

education_level

rank

 

dependents

 

military_service

work_history

 

 

 

 

 

 

hobbies

office_location

Space Planning App

id_number

office_size

 

 

 

 

id_number

 

ss_number

benefits_choices

 

 

user_id

dependents

 

job_title

 

phone_extension

beneficiaries

 

department

 

 

 

 

office_location

 

 

 

 

 

 

office_size

 

 

 

 

 

 

Although the attributes are public (visible to client programs), well-behaved programs manipulate the data only through methodsthat you provide, not by assigning or reading values directly. Because the methods can do extra checking, the data is kept in a proper state.

At run time, you create instances of an abstract type, real objects with filled-in attributes.

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

Structure of an Object Type

Figure 12–2 Object Type and Objects (Instances) of that Type

Object Type Employee

 

Attributes

Methods

 

name

calculate_bonus

 

id_number

change_dept

 

department

change_job_title

 

job_title

change_salary

 

salary

change_rank

 

rank

 

 

 

 

 

 

Object

Object

name:

Hart Bell

name:

Ann Polk

id_number:

8022

id_number:

8835

department:

Accounting

department:

Marketing

job_title:

Clerk

job_title:

Analyst

salary:

1750

salary:

3200

rank:

4

rank:

3

Why Use Object Types?

Object types let you break down a large system into logical entities. This lets you create software components that are modular, maintainable, and reusable across projects and teams.

By associating code with data, object types move maintenance code out of SQL scripts and PL/SQL blocks into methods. Instead of writing a long procedure that does different things based on some parameter value, you can define different object types and make each operate slightly differently. By declaring an object of the correct type, you ensure that it can only perform the operations for that type.

Object types allow for realistic data modeling. Complex real-world entities and relationships map directly into object types. Object types map directly into classes defined in object-oriented languages such as Java and C++.

Structure of an Object Type

Like a package, an object type has a specification and a body (refer to Figure 12–3). The specification (or spec for short) defines the programming interface; it declares a set of attributes along with the operations (methods) to manipulate the data. The body defines the code for the methods.

Using PL/SQL Object Types 12-3

Structure of an Object Type

Figure 12–3 Object Type Structure

 

spec

 

 

 

 

 

 

attribute declarations

 

public interface

 

 

 

 

 

 

 

method specs

 

 

 

 

 

 

 

 

 

 

body

 

 

 

 

private implementation

 

method bodies

 

 

 

 

 

 

 

 

 

 

 

 

All the information a program needs to use the methods is in the spec. You can change the body without changing the spec, and without affecting client programs.

In an object type spec, all attributes must be declared before any methods. If an object type spec declares only attributes, the object type body is unnecessary. You cannot declare attributes in the body. All declarations in the object type spec are public (visible outside the object type).

The following example defines an object type for complex numbers, with a real part, an imaginary part, and arithmetic operations.

CREATE TYPE Complex AS OBJECT ( rpart REAL, -- "real" attribute

ipart REAL, -- "imaginary" attribute

MEMBER FUNCTION plus (x Complex) RETURN Complex, -- method MEMBER FUNCTION less (x Complex) RETURN Complex,

MEMBER FUNCTION times (x Complex) RETURN Complex, MEMBER FUNCTION divby (x Complex) RETURN Complex

);

CREATE TYPE BODY Complex AS

MEMBER FUNCTION plus (x Complex) RETURN Complex IS

BEGIN

RETURN Complex(rpart + x.rpart, ipart + x.ipart);

END plus;

MEMBER FUNCTION less (x Complex) RETURN Complex IS

BEGIN

RETURN Complex(rpart - x.rpart, ipart - x.ipart);

END less;

MEMBER FUNCTION times (x Complex) RETURN Complex IS BEGIN

RETURN Complex(rpart * x.rpart - ipart * x.ipart, rpart * x.ipart + ipart * x.rpart);

END times;

MEMBER FUNCTION divby (x Complex) RETURN Complex IS z REAL := x.rpart**2 + x.ipart**2;

BEGIN

RETURN Complex((rpart * x.rpart + ipart * x.ipart) / z, (ipart * x.rpart - rpart * x.ipart) / z);

END divby; END;

/

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

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

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