IBM Books

Building Applications for UNIX** Environments


C++ Considerations for UDFs and Stored Procedures

Note:DB2 for Solaris does not support UDFs or stored procedures written in C++ and compiled with the IBM C Set++ compiler.

Because function names can be 'overloaded' in C++, that is, two functions with the same name can coexist if they have different arguments, as in int foo( int i ) and int foo( char c ), C++ compilers 'type-decorate' or 'mangle' function names by default. This means that argument type names are appended to their function names to resolve them, as in foo__Fi and foo__Fc for the two earlier examples.

The type-decorated function name can be determined from the .o file using the nm command:

nm myprog.o

The command produces some output which includes a line similar to the following:

myprogen__FPlT1PsT3PcN35|     3792|unamex|         | ...

When registering such a UDF with CREATE FUNCTION, the EXTERNAL NAME clause must specify the mangled function name obtained from nm (not including the | character):

    CREATE FUNCTION myprogo(...) RETURNS...
           ...
           EXTERNAL NAME '/whatever/path/myprog!myprogen__FPlT1PsT3PcN35'
           ...

Likewise, when calling a stored procedure, the function name also specifies the mangled function name:

CALL '/whatever/path/myprog!myprogen__FPlT1PsT3PcN35' ( ... )

If your stored procedure or UDF library does not contain overloaded C++ function names, you have the option of using extern "C" to force the compiler to not type-decorate function names. (Note that you can always overload the SQL function names given to UDFs, since DB2 resolves what library function to call based on the name and the parameters it takes.)



#include <string.h>
#include <stdlib.h>
#include "sqludf.h"
 
/*---------------------------------------------------------------------*/
/* function fold: output = input string is folded at point indicated   */
/*                         by the second argument.                     */
/*         inputs: CLOB,                 input string                  */
/*                 LONG                  position to fold on           */
/*         output: CLOB                  folded string                 */
/*---------------------------------------------------------------------*/
extern "C" void fold(
      SQLUDF_CLOB       *in1,                    /* input CLOB to fold       */
   ...
   ...
}
/* end of UDF: fold */
 
/*---------------------------------------------------------------------*/
/* function find_vowel:                                                */
/*             returns the position of the first vowel.                */
/*             returns error if no vowel.                              */
/*             defined as NOT NULL CALL                                */
/*         inputs: VARCHAR(500)                                        */
/*         output: INTEGER                                             */
/*---------------------------------------------------------------------*/
extern "C" void findvwl(
      SQLUDF_VARCHAR    *in,                     /* input smallint           */
   ...
   ...
}
/* end of UDF: findvwl */

In this example, the UDFs fold and findvwl are not type-decorated by the compiler, and should be registered in the CREATE FUNCTION statement using their plain names. Similarly, if a C++ stored procedure is coded with extern "C", its undecorated function name would be used in the CALL statement.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]