|Oracle 8i Data Cartridge Developer's Guide
Release 2 (8.1.6)
Part Number A76937-01
Defining Object Types , 2 of 5
In the Oracle8i ORDBMS, you use object types to model real-world entities. An object type has attributes, which reflect the entity's structure, and methods, which implement the operations on the entity. Attributes are defined using built-in types or other object types. Methods are functions or procedures written in PL/SQL or an external language like C and stored in the database.
A typical use for an object type is to impose structure on some part of the data kept in the database. For example, an object type named DataStream could used by a cartridge to store large amounts of data in a character
LOBs, or large objects, are discussed elsewhere in this document and in the Oracle8i Application Developer's Guide - Large Objects (LOBs).) This object type has attributes such as an identifier, a name, a date, and so on. The following statement defines the DataStream datatype:
create or replace type DataStream as object ( id integer, name varchar2(20), createdOn date, data clob, MEMBER FUNCTION DataStreamMin return pls_integer, MEMBER FUNCTION DataStreamMax return pls_integer, MAP MEMBER FUNCTION DataStreamToInt return integer, PRAGMA restrict_references(DataStreamMin, WNDS, WNPS), PRAGMA restrict_references(DataStreamMax, WNDS, WNPS));
A method is a procedure or function that is part of the object type definition and that can operate on the object type data attributes. Such methods are called member methods, and they take the keyword
MEMBER when you specify them as a component of the object type. The DataStream type definition declares three methods. The first two, DataStreamMin and DataStreamMax, calculate the minimum and maximum values, respectively, in the data stream stored inside the character
The third method (DataStreamToInt), a map method, governs comparisons between instances of data stream type. Map methods are described in elsewhere in this document.
The pragma (compiler directive)
RESTRICT_REFERENCES is necessary for security, and is discussed below.
After declaring the type, define the type body. The body contains the code for type methods. The following example shows the type body definition for the DataStream type. It defines the member function methods (DataStreamMin and DataStreamMax) and the map method (DataStreamToInt).
create or replace type body DataStream is
MEMBER FUNCTION DataStreamMin return pls_integer is a pls_integer := DS_Package.ds_findmin(data); begin return a; end; MEMBER FUNCTION DataStreamMax return pls_integer is b pls_integer := DS_Package.ds_findmax(data); begin return b; end; MAP MEMBER FUNCTION DataStreamToInt return integer is c integer := id; begin return c; end; end;
DataStreamMin and DataStreamMax involve calling routines in a PL/SQL package called DS_Package. Since these methods are likely to be compute-intensive (they process numbers stored in the CLOB to determine minimum and maximum values), they are defined as external procedures and implemented in C. The external dispatch is routed through a PL/SQL package named DS_Package. Such packages are discussed in Oracle8i Supplied Packages Reference.
The third method (DataStreamToInt), the map method, is implemented in PL/SQL. Because we have a identifier (id) attribute in DataStream, this method can return the value of the identifier attribute. (Most map methods, however, are more complex than DataStreamToInt.).