UDF Example: my_toupper Definition

Use my_toupper function to convert an arbitrary size input string to upper case.

my_toupper definition

my_toupper function takes a CHAR(x) column, where x is the maximum length of the characters in each cell of the column, and converts all alphabetical characters to uppercase.

#include "extfnapiv3.h"
#include <stdlib.h>
#include <ctype.h>


//  A simple deterministic scalar UDF that 
//  takes an arbitrary size input string
//  and converts it to upper case.
//
//  Corresponding SQL declaration:
//
//	CREATE FUNCTION my_toupper(IN arg1 varchar(32767)) 
//			RETURNS varchar(32767)
//			DETERMINISTIC
//          IGNORE NULL VALUES
//			EXTERNAL NAME 'my_toupper@libudfex'
//
#if defined __cplusplus
extern "C" {
#endif

static void my_toupper_evaluate(a_v3_extfn_scalar_context *cntxt, 
                      void *arg_handle)
{
  an_extfn_value  arg;
  an_extfn_value  outval;
  char c,uc;
  a_sql_int32 len,offset;

  //  Get input string 
  (void) cntxt->get_value( arg_handle, 1, &arg );
  if (arg.data == NULL)
  {
      return;
  }
  c = *(char *)arg.data;
  len = arg.len.total_len;
  for (offset = 0; offset < len; offset++)
  {
	  uc = toupper(c);
	  outval.type = DT_VARCHAR;
	  outval.piece_len = 1;
	  outval.data = &uc;
	  cntxt->set_value( arg_handle, &outval, offset);
  	  (void) cntxt->get_piece( arg_handle, 1, &arg, offset + 1);
  	  c = *(char *)arg.data;
  }
}


static a_v3_extfn_scalar my_toupper_descriptor = { 
	0, 
	0, 
	&my_toupper_evaluate,
	0,			// Reserved - initialize to NULL
	0,			// Reserved - initialize to NULL
	0,			// Reserved - initialize to NULL
	0,			// Reserved - initialize to NULL
	0,			// Reserved - initialize to NULL
        NULL                    // _for_server_internal_use
};


a_v3_extfn_scalar *my_toupper()
{
  return &my_toupper_descriptor;
}

#if defined __cplusplus
}
#endif