Example: Define the UDT and UDFs

Suppose you want to keep the electronic mail (e-mail) sent to your company in a table.

Ignoring any issues of privacy, you plan to write queries over such e-mail to find out their subject, how often your e-mail service is used to receive customer orders, and so on. E-mail can be quite large, and it has a complex internal structure (a sender, a receiver, the subject, date, and the e-mail content). Therefore, you decide to represent the e-mail by means of a UDT whose source type is a large object. You define a set of UDFs on your e-mail type, such as functions to extract the subject of the e-mail, the sender, the date, and so on. You also define functions that can perform searches on the content of the e-mail. You do the above using the following CREATE statements:

     CREATE DISTINCT TYPE E_MAIL AS BLOB (1M) 
     
     CREATE FUNCTION SUBJECT (E_MAIL) 
       RETURNS VARCHAR (200) 
       EXTERNAL NAME 'LIB/PGM(SUBJECT)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE FUNCTION SENDER (E_MAIL) 
       RETURNS VARCHAR (200) 
       EXTERNAL NAME 'LIB/PGM(SENDER)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE FUNCTION RECEIVER (E_MAIL) 
       RETURNS VARCHAR (200) 
       EXTERNAL NAME 'LIB/PGM(RECEIVER)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE FUNCTION SENDING_DATE (E_MAIL) 
       RETURNS DATE CAST FROM VARCHAR(10) 
       EXTERNAL NAME 'LIB/PGM(SENDING_DATE)'
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE FUNCTION CONTENTS (E_MAIL) 
       RETURNS BLOB (1M) 
       EXTERNAL NAME 'LIB/PGM(CONTENTS)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE FUNCTION CONTAINS (E_MAIL, VARCHAR (200)) 
       RETURNS INTEGER 
       EXTERNAL NAME 'LIB/PGM(CONTAINS)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       DETERMINISTIC
       NO EXTERNAL ACTION 
  
     CREATE TABLE ELECTRONIC_MAIL 
       (ARRIVAL_TIMESTAMP TIMESTAMP, 
        MESSAGE E_MAIL)