Map and order methods, PL-SQL Programming

Map and Order Methods:

The values of the scalar datatype like CHAR or REAL have a predefined order that allows them to be compared. While, the instances of an object type has no predefined order. To put them in order, the PL/SQL calls a map method supplied by you.

In the illustration below, the keyword MAP indicates that the method converts orders rational objects by mapping them to the REAL values:

CREATE TYPE Rational AS OBJECT (

num INTEGER,

den INTEGER,

MAP MEMBER FUNCTION convert RETURN REAL,

...

);

CREATE TYPE BODY Rational AS

MAP MEMBER FUNCTION convert RETURN REAL IS

BEGIN

RETURN num / den;

END convert;

...

END;

The PL/SQL uses the ordering to compute the Boolean expressions like x > y, and to do comparisons implied by the GROUP BY, DISTINCT, and ORDER BY clauses. The Map method convert returns to the relative position of an object in the ordering of all the rational objects.

An object type can have only one map method that should be a parameter less function with one of the scalar return types shown below: DATE, VARCHAR2, NUMBER, or an ANSI SQL type like CHARACTER or REAL.

On the other hand, you can supply the PL/SQL with an order method. An object type can have only one order method that should be a function which returns a numeric result. In the illustration below, the keyword ORDER indicates that method match compares 2 objects:

CREATE TYPE Customer AS OBJECT (

id NUMBER,

name VARCHAR2(20),

addr VARCHAR2(30),

ORDER MEMBER FUNCTION match (c Customer) RETURN INTEGER

);

CREATE TYPE BODY Customer AS

ORDER MEMBER FUNCTION match (c Customer) RETURN INTEGER IS

BEGIN

IF id < c.id THEN

RETURN -1; -- any negative number will do

ELSIF id > c.id THEN

RETURN 1; -- any positive number will do

ELSE

RETURN 0;

END IF;

END;

END;

Every order method takes merely two parameters: the built-in parameter SELF & the other object of similar type. If c1 and c2 are Customer objects, a comparison like c1 > c2 calls method match automatically. The method returns a , zero, negative number or a positive number suggesting that the SELF is correspondingly less than, equal to, or greater than the other parameter. If whichever parameter passed to an order method is null, then the method returns a null.

Posted Date: 10/6/2012 8:09:45 AM | Location : United States







Related Discussions:- Map and order methods, Assignment Help, Ask Question on Map and order methods, Get Answer, Expert's Help, Map and order methods Discussions

Write discussion on Map and order methods
Your posts are moderated
Related Questions
Defining REF CURSOR Types To make cursor variables, you take 2 steps. At first, you define a REF CURSOR type, and then declare the cursor variables of that type. You can defin

Using DEFAULT You can use the keyword DEFAULT rather than that of the assignment operator to initialize the variables. For e.g. the declaration blood_type CHAR := ’O’; it can b

Using Savepoints The scope of the savepoint is a transaction in which it is defined. The Savepoints defined in the major transaction are not related to the savepoints defined

Package STANDARD package named STANDARD defines the PL/SQL atmosphere. The package specification globally declares the exceptions, types, and subprograms that are available a

Relational Algebra - SQL It describes some operators, that together constitute an algebra that is not only relationally complete but also irreducibly so (very nearly- apart f

Existential Quantification - SQL Existential quantification-stating that something is true of at least one object under consideration-can be expressed by OR(r,c), meaning tha

Using %TYPE The %TYPE attribute gives the datatype of a variable or the database column. In the example below, the %TYPE gives the datatype of a variable: credit REAL(7,2); debi

Using Operator REF: You can retrieve refs by using the operator REF that, like VALUE, takes as its argument a correlation variable. In the illustration below, you retrieve one

Avoiding Collection Exceptions   In many cases, if you reference a nonexistent collection element, then PL/SQL raises a predefined exception. Consider the illustration shown b

Iteration Schemes The bounds of a loop range can be variables, literals, variables, or expressions but must compute to integers. Below are some of the examples. As you can see t