Just an example of how to do the casting when working with integer values. Only one function, square(), which squares a given value. eg: SELECT square(2) -> 4
============
/* Copyright ?1998 by Nem W Schlecht This file is public domain and comes with NO WARRANTY of any kind
MySQL SQL code for creating/deleting this function in the 'mysql' database:
CREATE FUNCTION square RETURNS INTEGER SONAME "int_example.so"; DROP FUNCTION square;
Compile line:
CC -DMYSQL_SERVER -DDEFAULT_MYSQL_HOME="\"/local/mysql\"" -DDATADIR="\"/local/mysql/var\"" -DSHAREDIR="\"/local/mysql/share/mysql\"" -DHAVE_CONFIG_H -DUSE_ALARM_THREAD -O3 -DDBUG_OFF -I/local/mysql/include/mysql -I/local/src/SQL/mysql-3.22.4-beta/include -I/local/src/SQL/mysql-3.22.4-beta -shared -o int_example.so int_example.cc
*/
#include <global.h> #include <my_sys.h> #include <mysql.h> #include <m_ctype.h>
#ifdef HAVE_DLOPEN
extern "C" {
// The initialization function proto. my_bool square_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
// The de-initialzition function proto. void square_deinit(UDF_INIT *initid);
// The actual function proto. longlong square(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); }
// // Initialization function - make sure correct number of arguments are passed // and that the argument is an integer value my_bool square_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count != 1 || args->arg_type[0] != INT_RESULT) { strcpy(message,"Wrong arguments to square"); return 1; }
initid->maybe_null = 0; return 0; }
// // De-initialization function - nothing spectial void square_deinit(UDF_INIT *initid) { }
// // 'square' function. Returns the square of the integer passed to it in the // args structure long long square(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { int number = *((long long*) args->args[0]); return number*number; }
#endif /* HAVE_DLOPEN */
|