Features of the User-Defined Function Interface
2809
CREATE FUNCTION
and
DROP FUNCTION
statements. See
Section 13.7.3.1, “
CREATE FUNCTION
Syntax for User-defined Functions”
.
• You can add functions as native (built-in) MySQL functions. Native functions are compiled into the
mysqld
server and become available on a permanent basis.
• Another way to add functions is 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 18.2, “Using Stored Routines (Procedures and Functions)”
.
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 don't need to 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.
Whichever method you use to add new functions, they can be invoked in SQL statements just like
native functions such as
ABS()
[908]
or
SOUNDEX()
[893]
.
See
Section 9.2.3, “Function Name Parsing and Resolution”
, for the rules describing how the server
interprets references to different kinds of functions.
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.c
file that is provided in MySQL source distributions.
21.2.1. Features of the User-Defined Function Interface
The MySQL interface for user-defined functions provides the following features and capabilities:
• Functions can return string, integer, or real values and can accept arguments of those same types.
• You can define simple functions that operate on a single row at a time, or aggregate functions that
operate on groups of rows.
• Information is provided to functions that enables them to check the number, types, and names of the
arguments passed to them.
• You can tell MySQL to coerce arguments to a given type before passing them to a function.
• You can indicate that a function returns
NULL
or that an error occurred.
21.2.2. Adding a New User-Defined Function
For the UDF mechanism to work, functions must be written in C or C++ and your operating system
must support dynamic loading. MySQL source distributions include a file
sql/udf_example.c
that
defines five UDF functions. Consult this file to see how UDF calling conventions work. The
include/
mysql_com.h
header file defines UDF-related symbols and data structures, although you need not
include this header file directly; it is included by
mysql.h
.
A UDF contains code that becomes part of the running server, so when you write a UDF, you are
bound by any and all constraints that apply to writing server code. For example, you may have
problems if you attempt to use functions from the
+
library. Note that these constraints may
change in future versions of the server, so it is possible that server upgrades will require revisions
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...