Application Development Guide


Using LOB Locators as UDF Parameters or Results

You can append AS LOCATOR to any of the LOB data types, or any distinct types based on LOB types in a CREATE FUNCTION statement. This applies both to the parameters that are passed and the results that are returned. When this happens, DB2 does the following:

Do not modify the locator values as this makes them unusable, and the APIs will return errors.

These special APIs can only be used in UDFs which are defined as NOT FENCED. This implies that these UDFs in test phase should not be used on a production database, because of the possibility that a UDF with bugs could cause the system harm. When operating on a test database, no lasting harm can result from the UDF if it should have bugs. When the UDF is known to be free of errors it can then be applied to the production database.

The APIs which follow are defined using the function prototypes contained in the sqludf.h UDF include file.

 
    extern int sqludf_length( 
        sqludf_locator*     udfloc_p,       /* in:  User-supplied LOB locator value */
        sqlint32*           Return_len_p    /* out: Return the length of the LOB value */ 
    ); 
    extern int sqludf_substr( 
        sqludf_locator*     udfloc_p,       /* in:  User-supplied LOB locator value */ 
        sqlint32            start,          /* in:  Substring start value (starts at 1) */ 
        sqlint32            length,         /* in:  Get this many bytes */ 
        unsigned char*      buffer_p,       /* in:  Read into this buffer */ 
        sqlint32*           Return_len_p    /* out: Return the length of the LOB value */ 
    ); 
    extern int sqludf_append( 
        sqludf_locator*     udfloc_p,       /* in:  User-supplied LOB locator value */ 
        unsigned char*      buffer_p,       /* in:  User's data buffer */ 
        sqlint32            length,         /* in:  Length of data to be appended */ 
        sqlint32*           Return_len_p    /* out: Return the length of the LOB value */ 
    ); 
    extern int sqludf_create_locator( 
        int                 loc_type,       /* in:  BLOB, CLOB or DBCLOB? */ 
        sqludf_locator**    Loc_p           /* out: Return a ptr to a new locator */ 
    ); 
    extern int sqludf_free_locator( 
        sqludf_locator*     loc_p           /* in: User-supplied LOB locator value */ 
    );

The following is a discussion of how these APIs operate. Note that all lengths are in bytes, regardless of the data type, and not in single or double-byte characters.

Return codes. Interpret the return code passed back to the UDF by DB2 for each API as follows:

0
Success.
-1
Locator passed to the API was freed by sqludf_free_locator() prior to making the call.
-2
Call was attempted in FENCED mode UDF.
-3
Bad input value was provided to the API. For examples of bad input values specific to each API, see its description below.
other
Invalid locator or other error (for example, memory error). The value that is returned for these cases is the SQLCODE corresponding to the error condition. For example, -423 means invalid locator. Please note that before returning to the UDF with one of these "other" codes, DB2 makes a judgment as to the severity of the error. For severe errors, DB2 remembers that the error occurred, and when the UDF returns to DB2, regardless of whether the UDF returns an error SQLSTATE to DB2, DB2 takes action appropriate for the error condition. For non-severe errors, DB2 forgets that the error has occurred, and leaves it up to the UDF to decide whether it can take corrective action, or return an error SQLSTATE to DB2.

The following notes apply to the use of these APIs:

Notes:

  1. A UDF which is defined to return a LOB locator has several possibilities available to it. It can return:

  2. A table function can be defined as returning one or more LOB locators. Each of them can be any of the possibilities discussed in the preceding item. It is also valid for such a table function to return the same locator as an output for several table function columns.

  3. A LOB locator passed to a table function as an input argument remains alive for the entire duration of the row generation process. In fact, the table function can append to a LOB using such a LOB locator while generating one row, and see the appended bytes on a subsequent row.

  4. The internal control mechanisms used to represent a LOB which originated in DB2 as a LOB locator output from a UDF (table or scalar function), take 1950 bytes. For this reason, and because there are limitations on the size of a row which is input to a sort, a query which attempts to sort multiple such LOBs which originated as UDF LOB locators will be limited to (at most) two such values per row, depending on the sizes of the other columns involved. The same limitation applies to rows being inserted into a table.

Scenarios for Using LOB Locators

This is a brief summary of possible scenarios that show the usefulness of LOB locators. These four scenarios outline the use of locators, and show how you can reduce space requirements and increase efficiency.


[ Top of Page | Previous Page | Next Page ]