CREATE FUNCTION/DROP FUNCTION
Syntax
For the UDF mechanism to work, functions must be written in C or C++ and your operating system must support dynamic loading. The MySQL source distribution includes a file `sql/udf_example.cc' that defines 5 new functions. Consult this file to see how UDF calling conventions work.
For mysqld
to be able to use UDF functions, you should configure MySQL
with --with-mysqld-ldflags=-rdynamic
The reason is that to on
many platforms (including Linux) you can load a dynamic library (with
dlopen()
) from a static linked program, which you would get if
you are using --with-mysqld-ldflags=-all-static
If you want to
use an UDF that needs to access symbols from mysqld
(like the
metaphone
example in `sql/udf_example.cc' that uses
default_charset_info
), you must link the program with
-rdynamic
(see man dlopen
).
If you are using a precompiled version of the server, use MySQL-Max, which supports dynamic loading.
For each function that you want to use in SQL statements, you should define
corresponding C (or C++) functions. In the discussion below, the name
``xxx'' is used for an example function name. To distinguish between SQL and
C/C++ usage, XXX()
(uppercase) indicates an SQL function call, and
xxx()
(lowercase) indicates a C/C++ function call.
The C/C++ functions that you write to implement the interface for
XXX()
are:
xxx()
(required)
SQL type | C/C++ type |
STRING | char *
|
INTEGER | long long
|
REAL | double
|
xxx_init()
(optional)
xxx()
. It can be used to:
XXX()
.
REAL
functions) the maximum number of decimals.
NULL
.
xxx_deinit()
(optional)
xxx()
. It should deallocate any
memory allocated by the initialisation function.
When an SQL statement invokes XXX()
, MySQL calls the
initialisation function xxx_init()
to let it perform any required
setup, such as argument checking or memory allocation. If xxx_init()
returns an error, the SQL statement is aborted with an error message and the
main and deinitialisation functions are not called. Otherwise, the main
function xxx()
is called once for each row. After all rows have been
processed, the deinitialisation function xxx_deinit()
is called so it
can perform any required cleanup.
For aggregate functions (like SUM()
), you must also provide the
following functions:
xxx_reset()
(required)
xxx_add()
(required)
When using aggregate UDFs, MySQL works the following way:
xxx_init()
to let the aggregate function allocate the memory it
will need to store results.
GROUP BY
expression.
xxx_reset()
function.
xxx_add()
function.
xxx()
to get the result for the aggregate.
xxx_deinit()
to let the UDF free any memory it has allocated.
All functions must be thread-safe (not just the main function,
but the initialisation and deinitialisation functions as well). This means
that you are not allowed to allocate any global or static variables that
change! If you need memory, you should allocate it in xxx_init()
and free it in xxx_deinit()
.