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

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Object Oriented Database » Object 
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 PointRETURN Point,
 13    PRAGMA RESTRICT_REFERENCES(Plus, RNDS, WNDS, RNPS, WNPS),
 14
 15    MEMBER FUNCTION Times(n IN NUMBERRETURN 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)18);
  5      v_yString VARCHAR2(8:= SUBSTR(TO_CHAR(y)18);
  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 PointRETURN 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 NUMBERRETURN 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(-15);
  3    v_P2 Point := Point(52);
  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: (-15)
p2: (52)
Distance between p1 and p2 = 6.70820393249936908922752100619382870632
Distance between p1 and the origin = 5.09901951359278483002822410902278198956
p1 * 2.5(-2.512.5)
p1 + p2: (47)

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
w___w_w___.ja__v_a___2s__.___c__o_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.