Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object Relational Feature

49 views
Skip to first unread message

sb...@mooseandpebs.com

unread,
Jun 23, 2005, 5:13:37 PM6/23/05
to
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.
Thanks!

Billy

unread,
Jun 24, 2005, 2:03:10 AM6/24/05
to

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
-- call the parent class constructor
inherited create;

-- do the local construction stuff
..
end;
==

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

Billy

unread,
Jun 24, 2005, 2:12:16 AM6/24/05
to

PS. Forgot to mention that this is 9i. I have not yet had the time to
look at the 10G's improvments (if any) in this regard.. nor if some
nasty bugs have been fixed. Even so, I like the OR features in Oracle,
especially in the PL/SQL side. Pity that not many seem to be using it.
Wrapping DBMS_SQL into a TCursor class for dynamic SQL.. creating a
TDevice class (with subclassing) to handle various types of outputs
(HTML, UTL_FILE, XML, CSV, etc.) and then piping the output of one into
the others.. simply freakingly neat code. Easy to maintain. Easy to
use. Easy to extend.

--
Billy

Rene Nyffenegger

unread,
Jun 24, 2005, 4:51:27 AM6/24/05
to

If I understand your setup, there's no requirment to 'deref something into a
parent class'.

Maybe, this link helps:
http://www.adp-gmbh.ch/ora/plsql/oo/example_2.html

Rene


--
Rene Nyffenegger
http://www.adp-gmbh.ch/

0 new messages