udf_blob

The sample table UDF udf_blob illustrates how a table UDF or TPF can read LOB input parameters using the blob API.

udf_blob counts the number of occurrences of a letter in the first input parameter. The data type of parameter 1 can be LONG VARCHAR or VARCHAR(64). If the type is LONG VARCHAR, the table UDF uses the blob API to read in the value. If the type is VARCHAR(64), the entire value is available using get_value.

This code snippett from the _open_extfn method illustrates how parameter 1 is read using the blob API:

static short UDF_CALLBACK udf_blob_open(
	a_v4_extfn_table_context * tctx )
/***************************************/    
{
…
…
    a_v4_extfn_blob 	*blob 		= NULL;

    ret = tctx->proc_context->get_value( tctx->args_handle, 2, &value );
    UDF_SQLERROR_RT( tctx->proc_context,
		     "get_value for argument 2 failed",
		     ret == 1,
		     0 );
    
    letter_to_find = *(char *)value.data;

    ret = tctx->proc_context->get_value( tctx->args_handle, 1, &value );
    UDF_SQLERROR_RT( tctx->proc_context,
		     "get_value for argument 1 failed",
		     ret == 1,
		     0 );

    if( EXTFN_IS_NULL(value) || EXTFN_IS_EMPTY(value) ) {
	state->return_value = 0;
	return 1;
    }
    
    if( EXTFN_IS_INCOMPLETE(value) ) {
	// If the value is incomplete, then that means we
// are dealing with a blob.
	tctx->proc_context->get_blob( tctx->args_handle, 1, &blob );
	return_value = ProcessBlob( tctx->proc_context,
 blob,
 letter_to_find );
	blob->release( blob );
    } else {
	// The entire value was put into the value pointer.
	return_value = CountNum( (char *)value.data,
value.piece_len,
letter_to_find );
    }
…
…
}


Parameter 1 is retrieved using get_value. If the value is empty or NULL, then no further processing is required. If the value is determined to be a blob using the macro EXTFN_IS_INCOMPLETE, then the Table UDF gets an instance of a_v4_extfn_blob using the get_blob method of a_v4_extfn_proc_context. The ProcessBlob method reads from the blob to determine how many occurrences of the specified letter are present.

Related reference
Blob (a_v4_extfn_blob)
_open_extfn
get_blob
External Procedure Context (a_v4_extfn_proc_context)