Databases Reference
In-Depth Information
Set out new long long to a value of 318749 and put the pointer to this variable into
initid->ptr . Notice that we have to typecast it as a char* so that we do not get
compiler warnings.
initid->const_item=1;
initid->maybe_null=0;
return 0;
}
The result of this UDF is always the same and it cannot be NULL , so we set these
initid members appropriately and return 0 to indicate a success.
void udf_staticexample_deinit(UDF_INIT *initid)
{
free(initid->ptr);}
Our udf_staticexample_deinit() function needs to clear up the initid->ptr so
that we do not end up with a memory leak. In this case, only a few bytes would be
leaked, but it could be a lot worse in larger UDFs.
long long udf_staticexample(UDF_INIT *initid,
UDF_ARGS *args, char *is_null, char *error)
{
return *(long long*) initid->ptr;
}
This UDF is of the INTEGER type so we define the main udf_staticexample()
function to return a long long . We return the value by dereferencing a pointer that
we set up in the initialization function.
Once this UDF is compiled this is what we will get from the MySQL command line:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.47 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current
input statement.
mysql> CREATE FUNCTION udf_staticexample RETURNS INTEGER SONAME 'udf_
staticexample.so';
Query OK, 0 rows affected (0.00 sec)
 
Search WWH ::




Custom Search