Object-PL/SQL

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Object-PL/SQL (Object-Procedural Language/Structured Query Language or simply O-PL/SQL) is a methodology of using the Oracle Corporation's procedural extension language for SQL and the Oracle relational database.[1] The additional features from version 7 and other improvements, lead to one of the large-scale environment implementations of the object-oriented database paradigm.[2]

Although PL/SQL's general syntax formerly used to resemble that of Ada or Pascal, there were many improvements that mainly include the Java embedding code[3] and the object-oriented syntax[4] inside the SQL.

The mixing and embedding of triggers and stored procedures was one of the breakthrough points up to support the use of PL/SQL in a OO paradigm.[5] The inclusion in the SQL syntax of statements such as [class].[object], and the implementation of the object type[6] (like any OO language), completed the minimum requisites to a mapping approach in an extended SQL language without use of specific mapping software.[7]

Autonomy, notoriety and importance of O-PL/SQL[edit]

The O-PSL/SQL isn't simply the use a version of a programming language but it's identified as how to use it, and it defines the autonomy of the theme.[8] Each version of PL/SQL, starting from 7, brings so many innovations that it's impossible to treat such usages as sub-themes of PL/SQL. So big is that revolution that it establishes a real borderline between the language, that can be used as formerly, and the OO approach inside itself. It's just this approach that makes the theme important and the large-scale using has brought its notoriety.[9]

A confusing of objects[edit]

There can be confusion of the notions of object of DBMS and of class object. This is very important as we live with both significances in one language. It's necessary to identify when the documentation refers to an object as one of the two definitions.

Database objects are concepts that refer to relational or sequential databases and persist being valid in new models. Tables, triggers, columns, indexes are examples of database objects,[6] which are present in O-PL/SQL, but with the same meaning of the notion of Java objects, specifically an element of a set that has its existence beginning from an instantiation of a class.

The PL/SQL[edit]

Main article: PL/SQL

PL/SQL is the extended SQL language used by Oracle Database.

PL/SQL is available in Oracle Database (since version 7), TimesTen in-memory database (since version 11.2.1), and IBM DB2 (since version 9.7).[10]

O-PL/SQL allows the definition of classes and instantiating these as objects, thus creating user-defined datatypes as writing constructors, beyond using Java in stored procedures and triggers.

Examples of uses of syntax of O-PL/SQL[edit]

Here is a small set of examples of O-PL/SQL syntax, extracted from the official documentation[11] and other sources:

Example 1[edit]

A simple example of object-oriented PL/SQL[12]

CREATE OR REPLACE TYPE base_type AS object (
  a NUMBER,
  constructor FUNCTION base_type RETURN self AS result,
  member FUNCTION  func RETURN NUMBER,
  member PROCEDURE proc (n NUMBER)
) instantiable NOT final;
/

Now, the type's implementation is created. The implementation defines how the type's functions, procedures and how explicit constructors behave:

CREATE OR REPLACE TYPE BODY base_type AS 
  constructor FUNCTION base_type RETURN self AS result IS
  BEGIN
    a:=0;
    RETURN;
  END base_type;
 
  member FUNCTION func RETURN NUMBER IS
  BEGIN
    RETURN a;
  END func;
 
  member PROCEDURE proc (n NUMBER) AS
  BEGIN
    a:=n;
  END proc;
END;
/

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.

CREATE OR REPLACE TYPE deriv_type under base_type (
  m NUMBER,
  overriding member FUNCTION func RETURN NUMBER
);
/

As is the case with base types, the overridden methods in the derived type must be implemented:

CREATE OR REPLACE TYPE BODY deriv_type AS
  overriding member FUNCTION func RETURN NUMBER IS
  BEGIN
    RETURN m*a;
  END;
END;
/

The created types can be instantiaded and methods can be called:

DECLARE
  b1 base_type :=base_type();
  b2 base_type :=base_type(4);
  d1 deriv_type:=deriv_type(5,6);
  d2 deriv_type:=deriv_type(5,6);
BEGIN
  DBMS_OUTPUT.put_line(b1.func);
  DBMS_OUTPUT.put_line(b2.func);
 
  d1.proc(4);
  DBMS_OUTPUT.put_line(d1.func);
  DBMS_OUTPUT.put_line(d2.func);
END;
/

Results

  • 0
  • 4
  • 24
  • 30

The created types have become real types and can be used in tables:

CREATE TABLE table_base (
  b base_type
);
DECLARE
  base  base_type := base_type();
  deriv deriv_type:= deriv_type(8,9);
BEGIN
  INSERT INTO table_base VALUES(base);
  INSERT INTO table_base VALUES(deriv);
END;
/
SELECT t.b.func() FROM table_base t;
T.B.FUNC()

Results:

  • 0
  • 72
SELECT AVG(t.b.func()) FROM table_base t;
AVG(T.B.FUNC())

Result:

  • 36

Example 2[edit]

Another example of stored procedure in Java embedded in Oracle Documentation[13]

Bibliography[edit]

See also[edit]

References[edit]

  1. ^ Lassan, Alan R., Due, Jacob Steen (13 June 2000). "Experiences with Object Oriented Development in PL/SQL". The danish National Center for IT Research. Retrieved 15 April 2012. 
  2. ^ Cunningham, Lewis. "PL/SQL Features by Release". Burleson Consulting. Retrieved 15 April 2012. 
  3. ^ "When Should you use Java Stored Procedures with an Oracle Database, what are the Drawbacks?". Stack Overflow. Retrieved 15 April 2012. 
  4. ^ "Oracle's Object-Oriented Features". etutorial.org. Retrieved 16 April 2012. 
  5. ^ Benett, 2002:144
  6. ^ a b Shubho, Al-Farooque (8 November 2009). "Optimize Database Files and Apply Partitioning". The Code Project. Retrieved 19 April 2012. 
  7. ^ Bales, 2007:107-209
  8. ^ "Use Object PL/SQL". java2s.com. Retrieved 19 April 2012. 
  9. ^ Feuerstein, 2009
  10. ^ "DB2 10: Run Oracle applications on DB2 10 for Linux, UNIX, and Windows". IBM. Retrieved 20 April 2012. 
  11. ^ "Oracle Documentatio". Oracle. Retrieved 19 April 2012. 
  12. ^ "Object Oriented Oracle, example 1". René Nyffenegger's collection of things on the web. Retrieved 19 April 2012. 
  13. ^ "Calling Java Methods in Oracle Database". Oracle. Retrieved 20 April 2012. 

External sources[edit]

Examples of O-Pl/SQL