28.4 Adding Functions to MySQL
There are three ways to add functions to MySQL:
Through the user-defined function (UDF) interface. User-defined functions are compiled as library files and then added to and removed from the server dynamically using the
DROP FUNCTIONstatements. See Section 18.104.22.168, “CREATE FUNCTION Syntax for User-Defined Functions”.
By creating native (built-in) MySQL functions. Native functions are compiled into the mysqld server and become available on a permanent basis.
By creating stored functions. These are written using SQL statements rather than by compiling object code. The syntax for writing stored functions is not covered here. See Section 23.2, “Using Stored Routines”.
Each method of creating compiled functions has advantages and disadvantages:
If you write user-defined functions, you must install object files in addition to the server itself. If you compile your function into the server, you need not do that.
Native functions require you to modify a source distribution. UDFs do not. You can add UDFs to a binary MySQL distribution. No access to MySQL source is necessary.
If you upgrade your MySQL distribution, you can continue to use your previously installed UDFs, unless you upgrade to a newer version for which the UDF interface changes. For native functions, you must repeat your modifications each time you upgrade.
For the rules describing how the server interprets references to different kinds of functions, see Section 9.2.5, “Function Name Parsing and Resolution”.
The following sections describe features of the UDF interface, provide instructions for writing UDFs, discuss security precautions that MySQL takes to prevent UDF misuse, and describe how to add native MySQL functions.
For example source code that illustrates how to write UDFs, take a look at the
sql/udf_example.cc file that is provided in MySQL source distributions.