Professional Documents
Culture Documents
9ib OBJ
9ib OBJ
•Type inheritance
•Substitutability
•Method overloading and overriding
•Dynamic method dispatch
•View hierarchies
Type inheritance
• Allows sharing similarities between types as
well as extending their characteristics
• Consists of supertypes and subtypes
• Subtypes inherit the characteristics of
supertypes
• Subtypes may also have their own attributes
and methods
• Subtypes can be substituted for supertypes in
code
Base
ROOT
A Parent
Supertype
Derived
D Child
Subtype
Inheritance Terminology
Different people use different words to mean the same thing with inheritance. Some people talk about base
and derived classes, some talk about superclasses and subclasses, and others keep it nice and simple and
speak of parent and child classes.
In this course the terms supertype and subtype are used. Therefore, before going any further, the diagram
in the slide shows how these terms compare. Class C is derived from type A, and type D is derived from
type C. This means that D inherits all the features of C, which in turn inherits all the members of A.
Super_type
attribute
Add behavior
Sub_type1 Sub_type2
attribute attribute
attribute
Modify behavior
Category_typ
Sub Types
subcategory_ref_list_type
Composite_categories_typ
Leaf Type
Catalog_typ
Leaf_category_typ
Type Hierarchy
An object type can be created as a subtype of an existing object type. This is based on a single
inheritance model which means that the subtype can be derived from only one parent type. A type
inherits all the attributes and methods of its direct supertype. It can add new attributes and methods and
further, override any of the inherited methods. The above figure illustrates two subtypes
LEAF_CATEGORY_TYP and SUBCATEGORY_REF_LIST_TYP created under CATEGORY_TYP.
Further a subtype can itself be refined by defining another subtype under it thus forming type hierarchies.
COMPOSITE_CATEGORIES_TYP is a subtype of CATEGORY_TYP with attributes of
type SUBCATEGORY_REF_LIST_TYP. CATALOG_TYP is a subtype of
COMPOSITE_CATEGORIES_TYP.
The signatures are as follows:
CREATE TYPE category_typ AS OBJECT
( category_name VARCHAR2(50)
, category_description VARCHAR2(1000)
, category_id NUMBER(2)
) NOT INSTANTIABLE NOT FINAL;
CREATE TYPE subcategory_ref_list_typ AS TABLE OF REF category_ty p;
CREATE TYPE product_ref_list_typ AS TABLE OF NUMBER(6);
CREATE TYPE leaf_category_typ UNDER category_typ (
product_ref_list product_ref_list_typ );
CREATE TYPE composite_category_typ UNDER category_typ(
subcategory_ref_list subcategory_ref_list_typ
)NOT FINAL;
CREATE TYPE catalog_typ UNDER composite_category_typ (
MEMBER FUNCTION getCatalogName RETURN varchar2 );
CREATE
CREATE TYPE
TYPE category_typ
category_typ AS
AS OBJECT
OBJECT
(category_name
(category_name varchar2(50),
varchar2(50),
category_description
category_description varchar2(1000)
varchar2(1000) ,,
category_id
category_id number(2)
number(2)
))
NOT
NOT FINAL;
FINAL;
Example
CREATE TYPE category_typ AS OBJECT
( category_name VARCHAR2(50)
, category_description VARCHAR2(1000)
, category_id number(2)
NOT INSTANTIABLE MEMBER FUNCTION func1() RETURN NUMBER)
NOT INSTANTIABLE;
A subtype of a not instantiable type can override any of the non instantiable methods of the supertype
and provide concrete implementations. If there are any non instantiable methods remaining, the subtype
must also necessarily be declared as not instantiable. A non instantiable subtype can be defined under
an instantiable supertype. Declaring a non instantiable type to be final is not useful and will be
disallowed.
CREATE
CREATE TYPE
TYPE category_typ
category_typ AS
AS OBJECT
OBJECT
(( category_name
category_name varchar2(50)
varchar2(50)
,, category_description
category_description varchar2(1000)
varchar2(1000)
,, category_id
category_id number(2)
number(2)
MEMBER
MEMBER PROCEDURE
PROCEDURE cat_proc
cat_proc (( id
id number),…
number),… ))
NOT
NOT FINAL;
FINAL;
CREATE
CREATE TYPE
TYPE leaf_category_typ
leaf_category_typ under
under category_typ
category_typ
((
product_ref_list
product_ref_list product_ref_list_typ
product_ref_list_typ
MEMBER
MEMBER PROCEDURE
PROCEDURE cat_proc
cat_proc (( id
id number)
number)
STATIC
STATIC FUNCTION
FUNCTION cat_func
cat_func (…)
(…)
);
);
CREATE
CREATE TYPE
TYPE leaf_category_typ
leaf_category_typ under
under category_typ
category_typ
((
product_ref_list
product_ref_list product_ref_list_typ
product_ref_list_typ
OVERRIDING
OVERRIDING MEMBER
MEMBER PROCEDURE
PROCEDURE cat_proc()
cat_proc()
);
);
CREATE
CREATE TYPE
TYPE category_typ
category_typ AS
AS OBJECT
OBJECT (…);
(…);
--definers
--definers rights
rights type
type
CREATE
CREATE TYPE
TYPE leaf_category_typ
leaf_category_typ UNDER
UNDER category_typ
category_typ
(…)
(…) ;; --
-- subtype
subtype in
in same
same schema
schema as
as supertype
supertype
CREATE
CREATE TYPE
TYPE schema1.composite_category_typ
schema1.composite_category_typ UNDER
UNDER
category_typ(…)
category_typ(…) ;; --
-- causes
causes error
error
CREATE
CREATE TYPE
TYPE category_typ
category_typ AUTHID
AUTHID CURRENT_USER
CURRENT_USER AS
AS
OBJECT
OBJECT (…);
(…); --
-- Invoker
Invoker rights
rights type
type
CREATE
CREATE TYPE
TYPE schema2.composite_category_typ
schema2.composite_category_typ UNDER
UNDER
category_typ(…);
category_typ(…); --
-- LEGAL
LEGAL
UPDATE
UPDATE categories_tab
categories_tab
SET
SET catmain
catmain == catleaf;
catleaf; --
-- assigning
assigning sub
sub type
type to
to
--supertype
--supertype
DECLARE
DECLARE
var1
var1 category_typ;
category_typ;
var2
var2 leaf_category_typ;
leaf_category_typ;
BEGIN
BEGIN --
-- assigning
assigning sub
sub type
type
var1
var1 :=
:= var2;
var2; --
-- to
to supertype
supertype
END;
END;
--
-- updating
updating supertype
supertype with
with subtype
subtype
UPDATE
UPDATE categories_tab
categories_tab set
set catcomp
catcomp == TREAT
TREAT
(catmain
(catmain as
as composite_category_typ);
composite_category_typ);
--
-- assigning
assigning supertype
supertype to
to subtype
subtype
DECLARE
DECLARE
var1
var1 category_typ;
category_typ;
var2
var2 leaf_category_typ;
leaf_category_typ;
BEGIN
BEGIN
var2
var2 :=
:= TREAT(var1
TREAT(var1 as
as leaf_category_typ);
leaf_category_typ);
END;
END;
CREATE
CREATE TYPE
TYPE cat_tab
cat_tab AS
AS TABLE
TABLE OF
OF category_typ;
category_typ;
--supertype
--supertype
CREATE
CREATE TYPE
TYPE leaf_cat_tab
leaf_cat_tab AS
AS TABLE
TABLE OF
OF leaf_category_typ;
leaf_category_typ;
--subtype
--subtype
DECLARE
DECLARE
v_cat_tab
v_cat_tab cat_tab
cat_tab ;;
v_leaf_cat_tab
v_leaf_cat_tab leaf_cat_tab;
leaf_cat_tab;
v_cat_elem
v_cat_elem category_typ;
category_typ;
v_leaf_cat_elem
v_leaf_cat_elem leaf_category_typ;
leaf_category_typ;
BEGIN
BEGIN
v_cat_tab
v_cat_tab :: == v_leaf_cat_tab
v_leaf_cat_tab ;;--ILLEGAL
--ILLEGAL not
not same
same type
type
v_cat_tab
v_cat_tab :=
:= cat_tab
cat_tab (v_cat_elem,
(v_cat_elem, v_leaf_cat_elem);
v_leaf_cat_elem);
--LEGAL
--LEGAL elements
elements of
of subtype
subtype
END;
END;
REFERENCE
Note : For more information on SQLJ Object types please refer to the lesson on SQLJ object
types in the Objects module
Objectives
In prior releases an object type referenced by a table or user-defined type could only be
changed by adding methods to it. In Oracle9i objects can evolve more freely and any changes
made to a type can propagate to schema objects referencing it. Oracle9i provides the ability to
add, drop and modify attributes of an object type.
Type Dependencies
Any object that references a type is considered a dependent object. When a type is modified all
dependent program units, views, operators and indextypes are marked invalid. Some objects are
automatically revalidated the next time that they are accessed. It the recompilation is successful then the
objects become valid again. However when a type has either type or table dependent, altering the type
definition becomes more complicated due to existing persisting data which relies on the current type
definition. To support this functionality the ALTER TYPE statement has additional options to allow
changes to an object type to be propagated to its dependent types and tables.
ALTER
ALTER TYPE
TYPE type_name
type_name
[ADD
[ADD || DROP
DROP || MODIFY|RENAME]
MODIFY|RENAME]
{{ method_spec
method_spec || attribute_spec
attribute_spec }}
[INVALIDATE
[INVALIDATE ||
CASCADE
CASCADE [[NOT]
[[NOT] INCLUDING
INCLUDING TABLE
TABLE DATA]
DATA]
[FORCE]
[FORCE] EXCEPTIONS
EXCEPTIONS INTO
INTO <table_name>
<table_name>
];
];
ALTER
ALTER TABLE
TABLE table_name
table_name
UPGRADE
UPGRADE [[NOT]
[[NOT] INCLUDING
INCLUDING DATA]
DATA]
[column_storage_clause];
[column_storage_clause];
INCLUDING DATA: Converts data stored in all user-defined columns to the most recent version of
the column's type.
COLUMN_STORAGE_CLAUSE : specifies the storage for the new VARRAY , nested table or LOB
attributes to be added to the table.
Target Type
INVALIDATE
CASCADE Dependent
NOT Types
INCLUDING
TABLE DATA
Metadata Metadata
2
CASCADE
INCLUDING Dependent
TABLE Tables
DATA
3
Other
Dependent
Objects
Leaf_category_typ
ALTER
ALTER TYPE
TYPE leaf_category_typ
leaf_category_typ
ADD
ADD ATTRIBUTE
ATTRIBUTE (( skew
skew number);
number);
ALTER
ALTER TYPE
TYPE category_type
category_type ADD
ADD attribute
attribute
(skew
(skew number)
number) CASCADE;
CASCADE;
Category_typ
Categories_tab
Leaf_category_typ
Schema Evolution
Category_tab is created as a table of category_typ as follows:
CREATE TABLE categories_tab OF category_typ NOT SUBSTITUTABLE
AT ALL LEVELS
( category_id primary key
);
ALTER
ALTER TYPE
TYPE category_typ
category_typ
ADD
ADD ATTRIBUTE
ATTRIBUTE (( hiredate
hiredate DATE)
DATE)
CASCADE
CASCADE NOT
NOT INCLUDING
INCLUDING TABLE
TABLE DATA;
DATA;
ALTER
ALTER TABLE
TABLE category_tab
category_tab UPGRADE
UPGRADE
INCLUDING
INCLUDING DATA;
DATA;
• {USER|ALL|DBA} _PENDING_CONV_TABLES
These views lists all tables which are in an
invalid state due to altering a parent type
• {USER|ALL|DBA}_TYPE_VERSIONS
These views lists all the versions of the
types
Overview
Collections were introduced in Oracle8.0 and are complex structures such as VARRAYS and Nested
tables which can be created as columns in database tables. Oracle9i allows collections to be nested
within other collections which can be used as datatypes for variables and column of tables
CREATE
CREATE TYPE
TYPE order_item_typ
order_item_typ AS
AS OBJECT
OBJECT
(( line_item_id
line_item_id NUMBER(3)
NUMBER(3)
,, unit_price
unit_price NUMBER(8,2)
NUMBER(8,2)
,, quantity
quantity NUMBER(8)
NUMBER(8)
);
);
CREATE
CREATE TYPE
TYPE order_item_list_typ
order_item_list_typ AS
AS TABLE
TABLE OF
OF
order_item_typ;
order_item_typ;
CREATE
CREATE TYPE
TYPE order_typ
order_typ AS
AS OBJECT
OBJECT
(( order_id
order_id NUMBER(12)
NUMBER(12)
,, order_item_list
order_item_list order_item_list_typ
order_item_list_typ
)) ;;
CREATE
CREATE TYPE
TYPE order_list_typ
order_list_typ AS
AS TABLE
TABLE OF
OF order_typ;
order_typ;
CREATE
CREATE TABLE
TABLE customers_drilldown
customers_drilldown
(( customer_id
customer_id NUMBER(6)
NUMBER(6)
,, cust_first_name
cust_first_name VARCHAR2(20)
VARCHAR2(20)
,, cust_last_name
cust_last_name VARCHAR2(20)
VARCHAR2(20)
,, cust_address
cust_address VARCHAR2(45)
VARCHAR2(45)
,, phone_numbers
phone_numbers VARCHAR2(15)
VARCHAR2(15)
,, credit_limit
credit_limit NUMBER(9,2)
NUMBER(9,2)
,, cust_orders
cust_orders order_list_typ)
order_list_typ)
NESTED
NESTED TABLE
TABLE cust_orders
cust_orders STORE
STORE AS
AS orders_tab
orders_tab
(PRIMARY
(PRIMARY KEY
KEY (( NESTED_TABLE_ID,
NESTED_TABLE_ID, order_id)
order_id)
NESTED
NESTED TABLE
TABLE order_items_list
order_items_list STORE
STORE AS
AS
order_items_tab
order_items_tab );
);
CREATE
CREATE TYPE
TYPE numtab_t
numtab_t AS
AS TABLE
TABLE OF
OF NUMBER;
NUMBER;
CREATE
CREATE TYPE
TYPE skutab_t
skutab_t AS
AS TABLE
TABLE OF
OF numtab_t;
numtab_t;
CREATE
CREATE TABLE
TABLE product_id_table
product_id_table as
as ((
line_number
line_number NUMBER(4),
NUMBER(4),
sku_number
sku_number skutab_t)
skutab_t)
NESTED
NESTED TABLE
TABLE sku_number
sku_number STORE
STORE AS
AS sku_table
sku_table
(NESTED
(NESTED TABLE
TABLE COLUMN_VALUE
COLUMN_VALUE STORE
STORE AS
AS
num_table);
num_table);
• Collection constructors
• Assignment and comparisons
• DML operations
• Collection unnesting
Note: The following slides give examples of DML assignment operations on multi level collections.
DECLARE
DECLARE
v_ords
v_ords order_list_typ;
order_list_typ;
BEGIN
BEGIN
……
--initialize
--initialize v_ords
v_ords
UPDATE
UPDATE customers_drilldown
customers_drilldown cc
SET
SET c.cust_orders
c.cust_orders == v_ords
v_ords
WHERE
WHERE customer_id
customer_id == 102;
102;
……
END
END
--
-- Insert
Insert new
new order
order for
for customer
customer 101
101
INSERT
INSERT INTO
INTO TABLE
TABLE (SELECT
(SELECT cust_orders
cust_orders
FROM
FROM customers_drilldown
customers_drilldown WHERE
WHERE customer_id
customer_id ==
101)
101)
VALUES
VALUES (( 1010,
1010, order_list_typ(
order_list_typ(
order_item_typ(
order_item_typ( 4901,
4901, 10,
10, 2),
2),
order_item_typ(
order_item_typ( 2077,
2077, 15,
15, 22 );
);
--Insert
--Insert new
new project
project for
for employee
employee number
number 100
100
INSERT
INSERT INTO
INTO TABLE
TABLE (SELECT
(SELECT o.order_item_list
o.order_item_list
FROM
FROM TABLE(
TABLE( SELECT
SELECT cust_orders
cust_orders from
from
customers_drilldown
customers_drilldown cc where
where
customer_id=
customer_id= 101)
101) oo
VALUES
VALUES (( 4444,
4444, 10,
10, 2)));
2)));
SELECT
SELECT c.customer_name
c.customer_name
FROM
FROM customers_drilldown
customers_drilldown c,
c,
TABLE(c.cust_orders)o,
TABLE(c.cust_orders)o,
TABLE(o.order_item_list)l;
TABLE(o.order_item_list)l;
Objects Enhancements 1
Objectives
Objects Enhancements 2
Overview
• The SQLJ Part2 standard API allows access of
object relational features.
• This lesson addresses the support for using Java
classes as SQL Types and discusses the creation
of objects or columns of these types.
• It also describes the mechanism for persisting
Java classes as SQL types, which enables the user
to define columns or rows of this Java type and to
query and manipulate the objects of this type.
Objects Enhancements 3
SQLJ Objects
Objects Enhancements 4
Creating SQLJ Object Types
Objects Enhancements 5
Creating the SQLJ Class
Objects Enhancements 6
Creating the SQLJ Class
public String getSQLTypeName() throws SQLException {
return sql_type; }
public void readSQL(SQLInput stream, String typeName) th rows
SQLException
{
sql_type = typeName;
ssn = stream.readInt();
name = stream.readString();
address = stream.readString();
}
public void writeSQL(SQLOutput stream) throws SQLExcepti on
{
stream.writeInt (ssn);
stream.writeString (name);
stream.writeString (address);
}
// other methods
public int id () { ... }
}
Objects Enhancements 7
Loading the SQLJ Class into the Database
Objects Enhancements 8
Creating SQLJ Object Types in the Database
Objects Enhancements 9
Receiving SQLJ Objects
Example of creating SQL accessible Object types and dependent object tables
CREATE TYPE person_t as object external name 'Person' language
java
using SQLData
( ss_no number(9) external name 'ssn',
name varchar2(100) external name 'name',
address varchar2(255) external name 'address'
);
Objects Enhancements 10
Accessing SQLJ Object Types in the
Database
• The map between the SQLJ name and the
corresponding Java class is an internal map
populated from the database.
• SQLJ object types can be used as columns of a
base table and attributes of a object type.
• The database SQL engine can access the SQLJ
object's attributes invoke it's methods. For
example:
SQL>
SQL> SELECT
SELECT emp_det.ss_no
emp_det.ss_no FROM
FROM employees;
employees;
Objects Enhancements 11
Creating SQLJ Objects
Objects Enhancements 12
Rules for Inheritance
• The SQLJ type cannot be created under a non
SQLJ object type.
• SQL types cannot be created under SQLJ types
• The USING clause must have the same value for
the SQLJ object type and all its subtypes.
• Two subtypes of a SQLJ object cannot be mapped
to the same external class
• Intermediate classes cannot be skipped in
mapping a SQLJ type hierarchy to a Java class
hierarchy.
REFERENCE
Note: For more information on Inheritance please refer to the lesson on Object Inheritance in
the Objects module
Objects Enhancements 13
Sending SQLJ Objects
• In a typical JDBC application, a Java object that
represents a database object is sent to the
database as one of the following
– a DML bind variable
– a PL/SQL IN parameter
– an object type attribute value
• The Java object can be a instance of SQLData or
OraData
• The Oracle JDBC driver converts the Java object
into the linearized format accepted by the
database SQL engine
REFERENCE
Note: For more examples and APIs usage, please refer to Oracle9i Working with Oracle Object
Types, Oracle9i JDBC Developer's Guide and Reference
Objects Enhancements 14
Metadata APIs
Metadata APIs
To obtain the type descriptor, the JDBC applications should use the
oracle.sql.StructDescriptor::createDescriptor factory method as follows --
/**
* Descriptor factory.
*
* Lookup the name in the database, and determine the characteristics
* of this array.
* @param name a String naming the type. (Not necessarily fully qualified)
* @param connection a Connection to a database
*/
public static StructDescriptor createDescriptor(String name, Connection conn)
throws SQLException
StructMetaData provides metadata APIs for SQLJ Object's data fields. StructDescriptor::getMetaData()
returns a instance of
StructMetaData of the type. It contains the following metadata API -
Objects Enhancements 16
Summary
Objects Enhancements 17
Appendix A
Terminology
Objects Enhancements 18
Type Inheritance Terminology
Common Supertype – A Type T is said to be the the common supertype
of type s if T is the supertype of every type in S.
Dynamic Type/ Most Specific Type (MST) – the type of value actually in
a slot at a particular moment of runtime execution.
Instance Substitutability – the ability of a slot declared as holding a
value of type T to in fact hold a value of type T or any subtype of type T.
Method overloading – the ability to define multiple methods with the
same method name. At compile time, the compiler chooses the correct
one based on the number, types and order of arguments passed to the
method.
Method Overriding (polymorphism) – the ability to define a new
implementation for a method inherited from a supertype. The only
difference in signature between the inherited and the overriding version of
a method is in the type of self parameter. The overriding method has self
as the subtype, whereas the inherited method has is as the Supertype.
Maximal Supertype (root type) – A type T is said to be a maximal
supertype if there exists no other type that is a supertype of T.
Minimal Common Supertype (MCS) - A type T is said to be the minimal
common supertype of a set of types S if T is a common supertype of S
and further , is a subtype of every common supertype of S
Narrowing – an assignment in which the source is a supertype of the
destination type
REF Substitutability – the ability of a slot declared a holding a value of
REF to type T to in fact hold a value of REF to type T or REF to any
subtype of type T.
Signature – the name of a method and the number, types and order of
the formal parameters of the method. Depending on the context, signature
may or may not include further information, such as the names of the
parameters, return type of the function, purity information and rights
information.
Slot - informally used in this lesson to mean a parameter, variable,
attribute of an object, element of a collection, or expression value – place
where a value can be stored.
Static type/Declared type – the type of value that a type is declared as
able to hold.
Objects Enhancements 19
Type hierarchy – the set of types consisting of a type T and all of
T's subtypes. A type S is more specific than a type T if type s is a
direct or indirect subtype of type T.
Virtual Dispatch – the runtime mechanism used to dynamically
dispatch to the correct routine , given the presence of method
overriding.
Widening – an assignment in which the static type of the source
is more specific than the static type of the target
E.g.. Assigning a leaf_category variable to a category slot. The
validity of this assignment can only be checked at runtime.
Objects Enhancements 20