IT3020-L03 Ordb
IT3020-L03 Ordb
1
Last Week
Nested Collections
VARRAYs and Nested tables
Storing
Querying
Manipulating
Any questions?
2
Encapsulation and UDTs:
Methods
Functions or procedures declared in an object type
definition to implement behaviour of objects.
Declared in a CREATE TYPE statement and Defined in a
CREATE TYPE BODY statement.
Methods written in PL/SQL or Java are stored in the
database.
preferable for data-intensive procedures and short
procedures that are called frequently.
Procedures in other languages, such as C, are
stored externally.
preferable for computationally intensive procedures that
are called less frequently.
3
Member Method
5
Some Points to Remember
6
Adding a new method
Use ALTER TYPE to add a CREATE OR REPLACE TYPE BODY
MenuType AS
method: MEMBER FUNCTION
ALTER TYPE MenuType priceInYen(rate FLOAT)
RETURN FLOAT IS
ADD MEMBER
BEGIN
FUNCTION RETURN rate * SELF.price;
priceInUSD(rate FLOAT) END priceInYen;
RETURN FLOAT MEMBER FUNCTION
priceInUSD(rate FLOAT)
CASCADE;
RETURN FLOAT IS
BEGIN
RETURN rate * SELF.price;
END priceInUSD;
END;
/
7
Example of Method Use
8
Object Comparison
10
Map Method
11
Example
CREATE TYPE Rectangle_type AS OBJECT
( length NUMBER,
width NUMBER,
MAP MEMBER FUNCTION area RETURN NUMBER
);
CREATE TYPE BODY Rectangle_type AS MAP MEMBER
FUNCTION area RETURN NUMBER IS
BEGIN
RETURN length * width;
END area;
END;
12
Example
14
Order Methods
Called automatically whenever two objects
need to be compared.
15
Example
An order method that compares customers by customer
ID:
17
On Comparison Methods
In defining an object type, you can specify either a
map method or an order method for it, but not both.
18
On Comparison Methods
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).
19
Methods on Nested Tables
20
Methods on Nested Tables
21
Methods on Nested Tables
22
Inheritance
23
Example: Person hierarchy
Person
District Manager
24
Inheritance in Oracle
25
Specializing Subtypes
26
Specializing Subtypes
27
FINAL and NOT FINAL Types
To permit subtypes, the object type must be
defined as not final.
By including the keyword NOT FINAL in the type
declaration.
By default, an object type is final.
Example
CREATE TYPE Person_type AS OBJECT
( pid NUMBER,
name VARCHAR2(30),
address VARCHAR2(100) ) NOT FINAL;
Subtypes of Person_type can be defined.
28
Altering object type
For example,
ALTER TYPE Person_type FINAL;
29
Creating Subtypes
31
Subtype under another
subtype
32
Table of supertype
Creating a supertype table
Create table person_tab of person_type
(pid primary key);
34
Selecting instances
From student type and its subtypes
SELECT VALUE(s)
FROM person_tab s
WHERE VALUE(s) IS OF (Student_type);
35
Selecting instances
From student type but not from subtypes
SELECT VALUE(s)
FROM person_tab s
WHERE VALUE(s) IS OF (ONLY student_type);
36
Selecting a Subtype Attribute
37
NOT INSTANTIABLE Types
Use this option with types intended solely as
supertypes of specialized subtypes.
CREATE TYPE Address_typ AS OBJECT(...)
NOT INSTANTIABLE NOT FINAL;*
CREATE TYPE AusAddress_typ UNDER
Address_typ(...);
CREATE TYPE IntlAddress_typ UNDER
Address_typ(...);
* The type body for T does not contain a definition for func1
39
NOT INSTANTIABLE Methods
Define a method as non-instantiable if every
subtype is to override the method in a different way.
40
FINAL and NOT FINAL
Methods
If a method is declared to be final, subtypes
cannot override it by providing their own
implementation.
Unlike types, methods are not final by default.
They must be explicitly declared to be final.
An overriding method is specified in a
CREATE TYPE BODY statement.
41
Example
CREATE TYPE MyType AS OBJECT
( ...,
MEMBER PROCEDURE Print,
FINAL MEMBER FUNCTION foo(x NUMBER) …, ...
) NOT FINAL;
/
CREATE TYPE MySubType UNDER MyType
( ...,
OVERRIDING MEMBER PROCEDURE Print,
...);
/
42
Overloading Methods
43
Example: Overloading Methods
44
Summary
Nested Collections
Varrays and nested tables
Storing
Querying
Manipulating
Inheritance in Oracle
FINAL/NOT FINAL
Subtypes (UNDER)
Getting at particular subtypes
INSTANTIABLE/NOT INSTANTIABLE (Types and
methods)
Overriding & Overloading
45