Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Object Relational Feature

Re: Object Relational Feature

From: Billy <vslabs_at_onwe.co.za>
Date: 23 Jun 2005 23:03:10 -0700
Message-ID: <1119592990.892040.13410@g49g2000cwa.googlegroups.com>


sburk_at_mooseandpebs.com wrote:
> I have a parent class (PL/SQL) and some child classes that inherit from the
> parent object.
> I have a table of the parent class type
> I have objects of the parent and children classes in the table.
> Each child has an overloaded method of the parent.
> The question is this:
> While inside a procedure in a separate package how do I deref the object
> into the a variable not knowing what type it is so I can execute the method
> of the class it is? If I deref it into the parent class it executes the
> parents method.

The code below illustrates - look specifically at what CheckAnimal() does. Also notice the constructor hack. PL/SQL does not allow you to call the parent constructor in your child class's constructor. In Delphi I would code a child class constructor (and destructor) something like this:
==
begin

This is not possible in PL/SQL, and thus you need to hack it to ensure that the parent classes constructors up the chain do their thing.

SQL> create or replace type TAnimal as Object   2 (
  3 id number(20),
  4

  5          member procedure AnimalConstructor( self IN OUT TAnimal ),
  6          constructor function TAnimal return self as result
  7 ) not final;
  8 /

Type created.

SQL> create or replace type body TAnimal as   2
  3 member procedure AnimalConstructor( self IN OUT TAnimal ) is   4 begin
  5 self.id := TO_NUMBER( TO_CHAR(sysdate,'yyyydddhh24miss') );
  6 end;
  7
  8 constructor function TAnimal return self as result is   9 begin

 10          self.AnimalConstructor;
 11          return;

 12 end;
 13
 14 end;
 15 /

Type body created.

SQL> create or replace type TMammel under TAnimal   2 (

  3          cat             varchar2(20),
  4          subcat          varchar2(20),
  5
  6          member procedure MammelConstructor( self IN OUT TMammel ),
  7          constructor function TMammel return self as result
  8 ) not final;
  9 /

Type created.

SQL> create or replace type body TMammel as   2
  3 member procedure MammelConstructor( self IN OUT TMammel ) is   4 begin

  5          self.AnimalConstructor;
  6          self.cat := 'WARMBLOODED';
  7          self.subcat := 'UNKNOWN';

  8 end;
  9
 10
 11 constructor function TMammel return self as result is  12 begin
 13          MammelConstructor;
 14          return;

 15 end;
 16
 17 end;
 18 /

Type body created.

SQL> create or replace type TCow under TMammel   2 (
  3 name varchar2(20),
  4

  5          member procedure CowConstructor( self IN OUT TCow ),
  6          constructor function TCow return self as result,
  7          member procedure Dump

  8 );
  9 /

Type created.

SQL> create or replace type body TCow as   2
  3 member procedure CowConstructor( self IN OUT TCow ) is   4 begin

  5          self.MammelConstructor;
  6          self.subcat := 'BOVINE';

  7 end;
  8
  9 constructor function TCow return self as result is  10 begin
 11          self.CowConstructor;
 12          return;

 13 end;
 14
 15 member procedure Dump is
 16 PS1 constant varchar2(20) := 'TCow> ';  17 begin
 18          W( PS1||'id='||self.id );
 19          W( PS1||'cat='||self.cat );
 20          W( PS1||'subcat='||self.subcat );
 21          W( PS1||'name='||self.name );
 22 end;
 23
 24
 25 end;
 26 /

Type body created.

SQL> create or replace procedure CheckAnimal( uanimal TAnimal ) is   2 cow$ TCow;
  3 begin

  4          if uanimal is of (TCow) then
  5                  cow$ := TREAT(uanimal as TCow);
  6                  cow$.Dump;
  7          end if;

  8 end;
  9 /

Procedure created.

SQL> declare
  2 cow$ TCow;
  3 begin

  4          cow$ := TCow();
  5          cow$.Name := 'Mina Moo';
  6          CheckAnimal( cow$ );

  7 end;
  8 /
TCow> id=2005175075520
TCow> cat=WARMBLOODED
TCow> subcat=BOVINE
TCow> name=Mina Moo

PL/SQL procedure successfully completed.

SQL>

--
Billy
Received on Fri Jun 24 2005 - 01:03:10 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US