Object-PL/SQL: Difference between revisions

Content deleted Content added
Line 27:
=== Example 1 ===
 
A simple example of object-oriented PL/SQL<ref>{{cite web|url=http://www.adp-gmbh.ch/ora/plsql/oo/example_1.html|title=Object Oriented Oracle, example 1|publisher=René Nyffenegger's collection of things on the web|accessdate=19 April 2012}}</ref>
<source lang="plsqlplpgsql">
 
create or replace type base_type as object (
a number,
Line 36:
) instantiable not final;
/</source>
Now, the type's implementation is created. The implementation defines how the type's functions, procedures and how explicit constructors behave:<source lang="plsqlplpgsql">
create or replace type body base_type as
constructor function base_type return self as result is
Line 55:
end;
/</source>
We're ready to derive from base_type. The keyword for deriving is under. The derived type defines a new attribute (named: m) and overrides func.<source lang="plsqlplpgsql">
create or replace type deriv_type under base_type (
m number,
Line 61:
);
/</source>
As is the case with base types, the overridden methods in the derived type must be implemented:
<source lang="plsqlplpgsql">
create or replace type body deriv_type as
overriding member function func return number is
Line 69 ⟶ 70:
end;
/</source>
The created types can be instantiated and methods can be called:
<source lang="plsqlplpgsql">
declare
b1 base_type :=base_type();
Line 85 ⟶ 87:
/</source>
Results
* 0
* 4
* 24
* 30
The created types have become real types and can be used in tables:
<source lang="plsqlplpgsql">
create table table_base (
b base_type
Line 104 ⟶ 107:
</source>
Results:
* 0
* 72
<source lang="plsqlplpgsql">
select avg(t.b.func()) from table_base t;
</source>
Result:
* 36
 
=== Example 2 ===