Using object types in .NET v9.0.3.1

The SQL CREATE TYPE command creates a user-defined object type, which is stored in the EDB Postgres Advanced Server database. You can then reference these user-defined types in SPL procedures, SPL functions, and .NET programs.

Create the basic object type with the CREATE TYPE AS OBJECT command. Optionally, use the CREATE TYPE BODY command.

Using an object type

To use an object type, you must first create the object type in the EDB Postgres Advanced Server database. Object type addr_object_type defines the attributes of an address:

CREATE OR REPLACE TYPE addr_object_type AS OBJECT
(
    street          VARCHAR2(30),
    city            VARCHAR2(20),
    state           CHAR(2),
    zip             NUMBER(5)
);

Object type emp_obj_typ defines the attributes of an employee. One of these attributes is object type ADDR_OBJECT_TYPE, as previously described. The object type body contains a method that displays the employee information:

CREATE OR REPLACE TYPE emp_obj_typ AS OBJECT
(
    empno           NUMBER(4),
    ename           VARCHAR2(20),
    addr            ADDR_OBJECT_TYPE,
    MEMBER PROCEDURE display_emp(SELF IN OUT emp_obj_typ)
);

CREATE OR REPLACE TYPE BODY emp_obj_typ AS
  MEMBER PROCEDURE display_emp (SELF IN OUT emp_obj_typ)
  IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE('Employee No   : ' || SELF.empno);
    DBMS_OUTPUT.PUT_LINE('Name          : ' || SELF.ename);
    DBMS_OUTPUT.PUT_LINE('Street        : ' || SELF.addr.street);
    DBMS_OUTPUT.PUT_LINE('City/State/Zip: ' || SELF.addr.city || ', ' ||
      SELF.addr.state || ' ' || LPAD(SELF.addr.zip,5,'0'));
  END;
END;

This example is a complete .NET program that uses these user-defined object types:

This program should display the following output in the Console:

Connection opened successfully
Preparing database...
Server Notice: Employee No   : 9001
Server Notice: Name          : JONES
Server Notice: Street        : 123 MAIN STREET
Server Notice: City/State/Zip: EDISON, NJ 08817
Emp No: 9001
Emp Name: JONES
Emp Address Street: 123 MAIN STREET
Emp Address City: EDISON
Emp Address State: NJ
Emp Address Zip: 8817
Cleaning database...