Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

Creating a UDF is quite an involved procedure. Each UDF should appear as a function in your C++
program. Each UDF can have an optional init and deinit function. These functions act like
constructors and destructors. You can check your arguments in the init function and perform cleanup
in the deinit function.
Listing 18.1 is an sample UDF that doubles a number that is passed as an argument. Your UDF will
probably be a little more complex. This example was kept simple so you could see how a UDF is
formatted.
Listing 18.1 User-Defined Functions in C


#ifdef STANDARD


#include <stdio.h>


#include <string.h>


#else


#include <global.h>


#include <my_sys.h>


#endif


#include <mysql.h>


#include <m_ctype.h>


#include <m_string.h>


extern "C" {


my_bool Double_Proc_init(UDF_INIT *initid,


UDF_ARGS args, char message);


void Double_Proc_deinit(UDF_INIT *initid);


long long Double_Proc(UDF_INIT initid, UDF_ARGS args,


char is_null, char error);


}

my_bool Double_Proc_init(UDF_INIT initid, UDF_ARGS args, char *message)


{


if (args->arg_count != 1 || args->arg_type[0] != INT_RESULT)

Free download pdf