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

(singke) #1
{

strcpy(message,"Wrong arguments to Double_Proc");


return 1;


}


return 0;


}


void Double_Proc_deinit(UDF_INIT *initid)


{


}


long long Double_Proc(UDF_INIT initid, UDF_ARGS args,


char is_null, char error)


{


int ResultSet;


ResultSet = args * 2;


Return ResultSet;


}


All UDFs must include the libraries that are shown in the example. After the include statements, you
will see the function prototypes. There can be only three types of return arguments for your function.
They are REAL, INTEGER, and STRING. These types are equivalent to a C++ double, long, and char
*, respectively. If you needed an SQL STRING returned, you would declare your prototype as a char
*. If your return was an SQL INTEGER, it would be declared as a long, and if it were an SQL REAL, it
would be declared as a double.
It is important to remember that your UDF must conform to the layout that is shown in Listing 18.1. The
init and deinit must be named as shown also. If your naming convention does not match the one
shown in the listing, the UDF will not work. For example, in Listing 18.1, the function that will be called in
MySQL is Double_Proc. The init function must be called Double_Proc_init. If it were called
something else, you could replace the Double_Proc with your name but still keep the _init. The
same naming convention applies to the deinit function.
The declarations for the function arguments must match Listing 18.1. The argument types and number
must match. Examine the previous code carefully.
The best technique is to download the MySQL source code from the MySQL Web site. After you have it
downloaded, un-tar and gunzip it. Browse the directories until you find the sql directory. In this
directory, there is a UDF example called udf_example.cc. You can edit this file to create your own
UDFs. That way you are certain of the format and declarations.
Free download pdf