Object-PL/SQL: Difference between revisions

Content deleted Content added
Rescuing 3 sources and tagging 0 as dead.) #IABot (v2.0
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 26:
 
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>
<sourcesyntaxhighlight lang="plpgsql">
create or replace type base_type as object (
a number,
Line 33:
member procedure proc (n number)
) instantiable not final;
/</sourcesyntaxhighlight>
Now, the type's implementation is created. The implementation defines how the type's functions, procedures and how explicit constructors behave:<sourcesyntaxhighlight lang="plpgsql">
create or replace type body base_type as
constructor function base_type return self as result is
Line 52:
end proc;
end;
/</sourcesyntaxhighlight>
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.<sourcesyntaxhighlight lang="plpgsql">
create or replace type deriv_type under base_type (
m number,
overriding member function func return number
);
/</sourcesyntaxhighlight>
As is the case with base types, the overridden methods in the derived type must be implemented:
<sourcesyntaxhighlight lang="plpgsql">
create or replace type body deriv_type as
overriding member function func return number is
Line 67:
end;
end;
/</sourcesyntaxhighlight>
The created types can be instantiated and methods can be called:
<sourcesyntaxhighlight lang="plpgsql">
declare
b1 base_type :=base_type();
Line 83:
dbms_output.put_line(d2.func);
end;
/</sourcesyntaxhighlight>
Results
0
Line 90:
30
The created types have become real types and can be used in tables:
<sourcesyntaxhighlight lang="plpgsql">
create table table_base (
b base_type
Line 103:
/
select t.b.func() from table_base t;
</syntaxhighlight>
</source>
Results:
0
72
<sourcesyntaxhighlight lang="plpgsql">
select avg(t.b.func()) from table_base t;
</syntaxhighlight>
</source>
Result:
36