Point type : Object « Object Oriented Database « Oracle PL / SQL






Point type

    

SQL>
SQL> set serveroutput on
SQL> CREATE OR REPLACE TYPE Point AS OBJECT (
  2    x NUMBER,
  3    y NUMBER,
  4
  5    MEMBER FUNCTION ToString RETURN VARCHAR2,
  6    PRAGMA RESTRICT_REFERENCES(ToString, RNDS, WNDS, RNPS, WNPS),
  7
  8    MEMBER FUNCTION Distance(p IN Point DEFAULT Point(0,0))
  9      RETURN NUMBER,
 10    PRAGMA RESTRICT_REFERENCES(Distance, RNDS, WNDS, RNPS, WNPS),
 11
 12    MEMBER FUNCTION Plus(p IN Point) RETURN Point,
 13    PRAGMA RESTRICT_REFERENCES(Plus, RNDS, WNDS, RNPS, WNPS),
 14
 15    MEMBER FUNCTION Times(n IN NUMBER) RETURN Point,
 16    PRAGMA RESTRICT_REFERENCES(Times, RNDS, WNDS, RNPS, WNPS)
 17  );
 18  /

SQL>
SQL> CREATE OR REPLACE TYPE BODY Point AS
  2    MEMBER FUNCTION ToString RETURN VARCHAR2 IS
  3      myResult VARCHAR2(20);
  4      v_xString VARCHAR2(8) := SUBSTR(TO_CHAR(x), 1, 8);
  5      v_yString VARCHAR2(8) := SUBSTR(TO_CHAR(y), 1, 8);
  6    BEGIN
  7      myResult := '(' || v_xString || ', ';
  8      myResult := myResult || v_yString || ')';
  9      RETURN myResult;
 10    END ToString;
 11
 12    MEMBER FUNCTION Distance(p IN Point DEFAULT Point(0,0))
 13      RETURN NUMBER IS
 14    BEGIN
 15      RETURN SQRT(POWER(x - p.x, 2) + POWER(y - p.y, 2));
 16    END Distance;
 17
 18    MEMBER FUNCTION Plus(p IN Point) RETURN Point IS
 19      myResult Point;
 20    BEGIN
 21      myResult := Point(x + p.x, y + p.y);
 22      RETURN myResult;
 23    END Plus;
 24
 25    MEMBER FUNCTION Times(n IN NUMBER) RETURN Point IS
 26      myResult Point;
 27    BEGIN
 28      myResult := Point(x * n, y * n);
 29      RETURN myResult;
 30    END Times;
 31  END;
 32  /

Type body created.

SQL> show errors
No errors.
SQL>
SQL>
SQL> DECLARE
  2    v_P1 Point := Point(-1, 5);
  3    v_P2 Point := Point(5, 2);
  4    myResult Point;
  5  BEGIN
  6    DBMS_OUTPUT.PUT_LINE('p1: ' || v_P1.toString);
  7    DBMS_OUTPUT.PUT_LINE('p2: ' || v_P2.toString);
  8
  9    DBMS_OUTPUT.PUT_LINE('Distance between p1 and p2 = ' || v_P1.Distance(v_P2));
 10
 11    DBMS_OUTPUT.PUT_LINE('Distance between p1 and the origin = ' ||
 12      v_P1.Distance);
 13
 14    myResult := v_P1.Times(n => 2.5);
 15    DBMS_OUTPUT.PUT_LINE('p1 * 2.5: ' || myResult.toString);
 16    myResult := v_P1.Plus(p => v_P2);
 17    DBMS_OUTPUT.PUT_LINE('p1 + p2: ' || myResult.toString);
 18  END;
 19  /
p1: (-1, 5)
p2: (5, 2)
Distance between p1 and p2 = 6.70820393249936908922752100619382870632
Distance between p1 and the origin = 5.09901951359278483002822410902278198956
p1 * 2.5: (-2.5, 12.5)
p1 + p2: (4, 7)

PL/SQL procedure successfully completed.

SQL>
SQL>

   
    
    
  








Related examples in the same category

1.Create Object
2.CREATE OR REPLACE TYPE
3.Create a stored type which is visible to SQL and PL/SQL.
4.reference user-defined data type in another block
5.Student type
6.Use user-defined type as parameter
7.This script demonstrates complex objects
8.Name type
9.Behavior of dependent objects.
10.Build data type with another user type
11.Create the object and collection types
12.Create type and use it in inner query
13.Create types and then use it in pl/sql block
14.Combine user-defined type to create new type
15.PriceType becomes the datatype of the price attribute in the ProductType object type
16.Use self to reference member variable in constructor
17.One to list using object references