Adding a New Native Function
2819
21.2.2.6. User-Defined Function Security Precautions
MySQL takes several measures to prevent misuse of user-defined functions.
UDF object files cannot be placed in arbitrary directories. They must be located in some system
directory that the dynamic linker is configured to search. To enforce this restriction and prevent
attempts at specifying path names outside of directories searched by the dynamic linker, MySQL
checks the shared object file name specified in
CREATE FUNCTION
statements for path name delimiter
characters. As of MySQL 5.0.3, MySQL also checks for path name delimiters in file names stored in
the
mysql.func
table when it loads functions. This prevents attempts at specifying illegitimate path
names through direct manipulation of the
mysql.func
table. For information about UDFs and the
runtime linker, see
Section 21.2.2.5, “Compiling and Installing User-Defined Functions”
.
To use
CREATE FUNCTION
or
DROP FUNCTION
, you must have the
INSERT
[577]
or
DELETE
[577]
privilege, respectively, for the
mysql
database. This is necessary because those statements add and
delete rows from the
mysql.func
table.
UDFs should have at least one symbol defined in addition to the
xxx
symbol that corresponds to the
main
xxx()
function. These auxiliary symbols correspond to the
xxx_init()
,
xxx_deinit()
,
xxx_reset()
,
xxx_clear()
, and
xxx_add()
functions. As of MySQL 5.0.3,
mysqld
supports an
--allow-suspicious-udfs
[400]
option that controls whether UDFs that have only an
xxx
symbol
can be loaded. By default, the option is off, to prevent attempts at loading functions from shared object
files other than those containing legitimate UDFs. If you have older UDFs that contain only the
xxx
symbol and that cannot be recompiled to include an auxiliary symbol, it may be necessary to specify
the
--allow-suspicious-udfs
[400]
option. Otherwise, you should avoid enabling this capability.
21.2.3. Adding a New Native Function
To add a new native MySQL function, use the procedure described here, which requires that you use
a source distribution. You cannot add native functions to a binary distribution because it is necessary
to modify MySQL source code and compile MySQL from the modified source. If you migrate to another
version of MySQL (for example, when a new version is released), you must repeat the procedure with
the new version.
If the new native function will be referred to in statements that will be replicated to slave servers, you
must ensure that every slave server also has the function available. Otherwise, replication will fail on
the slaves when they attempt to invoke the function.
To add a new native function, follow these steps to modify source files in the
sql
directory:
1. Add one line to
lex.h
that defines the function name in the
sql_functions[]
array.
2. If the function prototype is simple (just takes zero, one, two, or three arguments), add a line to the
sql_functions[]
array in
lex.h
that specifies
SYM(FUNC_ARGN)
as the second argument
(where
N
is the number of arguments the function takes). Also, add a function in
item_create.cc
that creates a function object. Look at
"ABS"
and
create_funcs_abs()
for an example of this.
If the function prototype is not simple (for example, if it takes a variable number of arguments), you
should make two changes to
sql_yacc.yy
. One is a line that indicates the preprocessor symbol
that
yacc
should define; this should be added at the beginning of the file. The other is an “item” to
be added to the
simple_expr
parsing rule that defines the function parameters. You will need an
item for each syntax with which the function can be called. For an example that shows how this is
done, check all occurrences of
ATAN
in
sql_yacc.yy
.
3. In
item_func.h
, declare a class inheriting from
Item_num_func
or
Item_str_func
, depending
on whether your function returns a number or a string.
4. In
item_func.cc
, add one of the following declarations, depending on whether you are defining a
numeric or string function:
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 ...