Sunteți pe pagina 1din 16

Object Relational Interactions with Oracle

Outline

Why Object database management systems (ODBMSs)? ODBMS and RDBMS and ORDBMS Oracle Database Object Relational

Oracle Objects Collection Object Type Object Type Methods Inheritance

Why ODBMS?

The need for storing and manipulating complex data is increasing. We can not use relational databases to store this kind of data. The complex data types appear in several applications:

CAD/CAM Geometric modeling Geographical Information Systems Knowledge-based systems / through frames Web applications that need to store, index, search and manipulate diverse objects.

The impedance mismatch becomes apparent when trying to map these OO language properties to a RDBMS.

ODBMS ,RDBMS and ORDBMS

Oracle Database Object Relational

Object Types

Simple Object Type

A simple object type consists of scalar data types.


Example:
CREATE OR REPLACE TYPE Address_objtyp AS OBJECT ( Street VARCHAR2(200), City VARCHAR2(200), State CHAR(2), Zip VARCHAR2(20), MEMBER FUNCTION formatAsXML RETURN VARCHAR2 );

Object Types

Reference Object Type

A REF is a logical pointer to represent relationships between class instances. Example:


CREATE OR REPLACE TYPE LineItem_objtyp AS OBJECT ( LineItemNo NUMBER, Stock_ref REF StockItem_objtyp, Quantity NUMBER, Discount NUMBER );

Given a LineItem_objtyp object instance, the price of the item is easily accessed with the following:
<instance_variable>.DEREF(stock_ref).price

Collection Object Type

Varray

An ordered and bounded collection


CREATE OR REPLACE TYPE PhoneList_vartyp AS VARRAY(10) OF VARCHAR2(25);

Nested tables.

A nested table is an unbounded and unsorted collection.


CREATE OR REPLACE TYPE LineItemList_ntabtyp AS TABLE OF LineItem_objtyp;

Composite Object Type

CREATE OR REPLACE TYPE Customer_objtyp AS OBJECT ( CustNo NUMBER, CustName VARCHAR2(200), Address_obj Address_objtyp, PhoneList_var PhoneList_vartyp ) NOT FINAL;

Composite Object Type


CREATE OR REPLACE TYPE PurchaseOrder_objtyp
AS OBJECT( PONo NUMBER, OrderDate DATE, ShipDate DATE, Cust_ref REF Customer_objtyp, LineItemList_ntab LineItemList_ntabtyp, ShipToAddr_obj Address_objtyp, MAP MEMBER FUNCTION getPONo RETURN NUMBER, MEMBER FUNCTION sumLineItems RETURN NUMBER );

Object Type Methods

Member Methods
CREATE OR REPLACE FUNCTION formatAsXML (customer_id NUMBER)

Constructor Methods
CREATE OR REPLACE TYPE Customer_objtyp AS OBJECT ( ... some code snipped ... CONSTRUCTOR FUNCTION customer_objtyp(id IN NUMBER) RETURN SELF AS RESULT, ) NOT FINAL;

Object Type Methods

Using Constructor

DECLARE newCustomer customer_objtyp := NULL; BEGIN newCustomer := customer_objtyp (42, Adams, Douglas, Address_objtyp(123 Handy Road, Dandyville, CA, 94999), phonelist_vartyp(+1-650-555-1212, +61-8-9555-1212) ); END;

Comparison Methods
CREATE OR REPLACE TYPE Customer_objtyp AS OBJECT ( ORDER MEMBER FUNCTION compareCustOrders(x IN Customer_objtyp) RETURN INTEGER ... some code snipped ... ) NOT FINAL; CREATE OR REPLACE TYPE BODY customer_objtyp AS ORDER MEMBER FUNCTION compareCustOrders(x IN Customer_objtyp) RETURN INTEGER IS BEGIN RETURN SELF.CustNo - x.CustNo; END compareCustOrders; ... some code snipped ... END;

Inheritance

Using the under keyword.


CREATE OR REPLACE TYPE OnlineCustomer_objtyp UNDER Customer_objtyp ( EmailAddress VARCHAR2(50), MEMBER FUNCTION sendEmail(subject IN VARCHAR2, msg IN VARCHAR2) RETURN VARCHAR2 ) NOT FINAL; /

There can be multiple levels in this hierarchy

Mapping to UML Concepts

Conclusion

Further reading

[1] Object-Oriented Oracle , J.W.Rahayu, IRM Press, 2005 [2] Professional Oracle Programming, Rick Greenwald, Wrox, 2005 (Chapter 21) Using Oracle Object Types with Java ([2], page 501 - 520)

Exercise

S-ar putea să vă placă și