Search This Blog

Translate

Thursday, May 30, 2013

Oracle 11g : Object Oriented Programming In Oracle PLSQL

Get real time news update from your favorite websites.
Don't miss any news about your favorite topic.
Personalize your app.

Check out NTyles.


Get it on....

NTyles-App

Before I took the job as the Associate Software Engineer, I was a JAVA and .NET guy.
All the projects that I did before my current job, I did them JAVA or either .NET. In those time object was my play thing. Everything I do in my project I used to think in object way.I know how powerful  object oriented programming is.

Currently I am a oracle plsql developer. And as soon as I get this job I tried to model data in my own way as I used to do in JAVA and .NET. And I was very happy to find out that Oracle too has object oriented features.

Let me show you a quick code demo that uses oracle's object oriented features.
The gist of code is  :

"If I am two numbers then I can be added, subtracted, multiplied and divided."

Now here goes the code:

--this is object type with two number variables. 
--as in java one can define constructor, member functions
--and yes, setters and getters.
--i am not showing all of them coz i am lazy

CREATE OR REPLACE TYPE numbers AS object
(
    a NUMBER,
    b NUMBER
);

Upto this what I did is I created a number Object.
Think something like this :

"If I have a car, I can drive it, and of course I can sell it too." 

So, If I have numbers then I can add them, divided them , subtract them and yes multiply them. Here's the code that does all the above mentioned.

 (See think objectly , everything will look simple and programming would be so easy.)

--This is another object that does all the operation for numbers object.
--This is definition spec.
CREATE OR REPLACE TYPE numbersOp AS object
(
    n numbers, --my numbers object

    --this is how one defines member functions in oracle.
    member FUNCTION plus RETURN NUMBER ,    --add
    member FUNCTION sub RETURN NUMBER,      --subtract 
    member FUNCTION multiply RETURN NUMBER, --multiply
    member FUNCTION divide RETURN NUMBER    --divide
);
/

--This is body spec.
CREATE OR REPLACE TYPE BODY numbersOp as
  member FUNCTION plus RETURN NUMBER AS
   vblsum NUMBER ;
   BEGIN
     vblsum := n.a + n.b;
     RETURN vblsum;
   --EXCEPTION
   --  WHEN Others THEN
   END plus ;

   member FUNCTION sub RETURN NUMBER AS
    vblsub NUMBER;
     BEGIN
       vblsub := n.b - n.a;
       RETURN vblsub;
     --EXCEPTION
     --  WHEN Others THEN
     END sub;

   member FUNCTION multiply RETURN NUMBER AS
    vblmul NUMBER ;
     BEGIN
       vblmul := n.a * n.b;
       RETURN vblmul;
     --EXCEPTION
     --  WHEN Others THEN
     END;

   member FUNCTION divide RETURN NUMBER AS
    vbldiv NUMBER (10, 3);
     BEGIN
       vbldiv := n.a / n.b ;
       RETURN vbldiv;
     --EXCEPTION
     --  WHEN Others THEN
     END;

END ;
 / 

Now everything has been coded for numbers and it operations. Now let see some demo. Here's the demo code:


DECLARE

  a numbers := numbers(25, 60); --my numbers
  b numbersOp := numbersOp(a);  --I should able to operate them
BEGIN

   Dbms_Output.Put_Line(b.plus);
   Dbms_Output.Put_Line(b.sub);
   Dbms_Output.Put_Line(b.multiply);
   Dbms_Output.Put_Line(b.divide);
--EXCEPTION
--  WHEN Others THEN
END;


If you really find this stuff interesting, then my next blog will make u addicted to the object oriented concepts in oracle.
Till then
HAPPY OBJECTING IN ORACLE!!.

2 comments:

  1. Really helpful. Can you provide more oops concepts used in oracle with example?

    ReplyDelete
  2. Nice way to explain object concept in PLSQL.
    Waiting for new concept in OOPs

    ReplyDelete