User-Defined Constructors : Constructor « Object Oriented « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE OR REPLACE TYPE EmployeeType AS OBJECT (
  2    id         NUMBER,
  3    first_name VARCHAR2(10),
  4    last_name  VARCHAR2(10),
  5    dob        DATE,
  6    phone      VARCHAR2(12),
  7    CONSTRUCTOR FUNCTION EmployeeType(
  8      p_id         NUMBER,
  9      p_first_name VARCHAR2,
 10      p_last_name  VARCHAR2
 11    ) RETURN SELF AS RESULT,
 12    CONSTRUCTOR FUNCTION EmployeeType(
 13      p_id         NUMBER,
 14      p_first_name VARCHAR2,
 15      p_last_name  VARCHAR2,
 16      p_dob        DATE,
 17      p_phone      VARCHAR2
 18    ) RETURN SELF AS RESULT
 19  );
 20  /

Type created.

SQL> CREATE OR REPLACE TYPE BODY EmployeeType AS
  2    CONSTRUCTOR FUNCTION EmployeeType(
  3      p_id         NUMBER,
  4      p_first_name VARCHAR2,
  5      p_last_name  VARCHAR2
  6    ) RETURN SELF AS RESULT IS
  7    BEGIN
  8      SELF.id := p_id;
  9      SELF.first_name := p_first_name;
 10      SELF.last_name := p_last_name;
 11      SELF.dob := SYSDATE;
 12      SELF.phone := '555-1212';
 13      RETURN;
 14    END;
 15    CONSTRUCTOR FUNCTION EmployeeType(
 16      p_id         NUMBER,
 17      p_first_name VARCHAR2,
 18      p_last_name  VARCHAR2,
 19      p_dob        DATE,
 20      p_phone      VARCHAR2
 21    ) RETURN SELF AS RESULT IS
 22    BEGIN
 23      SELF.id := p_id;
 24      SELF.first_name := p_first_name;
 25      SELF.last_name := p_last_name;
 26      SELF.dob := p_dob;
 27      SELF.phone := p_phone;
 28      RETURN;
 29    END;
 30  END;
 31  /

Type body created.

SQL>
SQL> DESC EmployeeType;
 Name               Null?    Type
----------------------------------------------------------------------------------------------------
 ID                          NUMBER
 FIRST_NAME                  VARCHAR2(10)
 LAST_NAME                   VARCHAR2(10)
 DOB                         DATE
 PHONE                       VARCHAR2(12)

METHOD
------
 FINAL CONSTRUCTOR FUNCTION EMPLOYEETYPE RETURNS SELF AS RESULT
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 P_ID                           NUMBER                  IN
 P_FIRST_NAME                   VARCHAR2                IN
 P_LAST_NAME                    VARCHAR2                IN

METHOD
------
 FINAL CONSTRUCTOR FUNCTION EMPLOYEETYPE RETURNS SELF AS RESULT
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 P_ID                           NUMBER                  IN
 P_FIRST_NAME                   VARCHAR2                IN
 P_LAST_NAME                    VARCHAR2                IN
 P_DOB                          DATE                    IN
 P_PHONE                        VARCHAR2                IN

SQL>
SQL> CREATE TABLE emp OF EmployeeType;

Table created.

SQL>
SQL> desc emp;
 Name                Null?    Type
----------------------------------------------------------------------------------------------------
 ID                           NUMBER
 FIRST_NAME                   VARCHAR2(10)
 LAST_NAME                    VARCHAR2(10)
 DOB                          DATE
 PHONE                        VARCHAR2(12)

SQL>
SQL> drop table emp;

Table dropped.

SQL>
SQL>








32.2.Constructor
32.2.1.Inserting a row into an object table using constructor
32.2.2.Using the default constructor (the name of the class)
32.2.3.User-Defined Constructors
32.2.4.Construct user-defined type with subquery
32.2.5.Use constructor to create new objects
32.2.6.Demonstrates object initialization.