Rechercher dans le manuel MySQL
29.4.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.cc
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
libstdc++
library. These constraints may
change in future versions of the server, so it is possible that
server upgrades will require revisions to UDFs that were
originally written for older servers. For information about
these constraints, see
Section 2.9.7, “MySQL Source-Configuration Options”, and
Section 2.9.8, “Dealing with Problems Compiling MySQL”.
To be able to use UDFs, you must link mysqld
dynamically. If you want to use a UDF that needs to access
symbols from mysqld (for example, the
metaphone
function in
sql/udf_example.cc
uses
default_charset_info
), you must link the
program with -rdynamic
(see man
dlopen
).
For each function that you want to use in SQL statements, you
should define corresponding C (or C++) functions. In the
following discussion, 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.
When using C++ you can encapsulate your C functions within:
extern "C" { ... }
This ensures that your C++ function names remain readable in the completed UDF.
The following list describes the C/C++ functions that you write
to implement the interface for a function named
XXX()
. The main function,
xxx()
, is required. In addition, a UDF
requires at least one of the other functions described here, for
reasons discussed in Section 29.4.2.6, “UDF Security Precautions”.
xxx()
The main function. This is where the function result is computed. The correspondence between the SQL function data type and the return type of your C/C++ function is shown here.
It is also possible to declare a
DECIMAL
function, but currently the value is returned as a string, so you should write the UDF as though it were aSTRING
function.ROW
functions are not implemented.xxx_init()
The initialization function for
xxx()
. If present, it can be used for the following purposes:To check the number of arguments to
XXX()
.To verify that the arguments are of a required type or, alternatively, to tell MySQL to coerce arguments to the required types when the main function is called.
To allocate any memory required by the main function.
To specify the maximum length of the result.
To specify (for
REAL
functions) the maximum number of decimal places in the result.To specify whether the result can be
NULL
.
xxx_deinit()
The deinitialization function for
xxx()
. If present, it should deallocate any memory allocated by the initialization function.
When an SQL statement invokes XXX()
, MySQL
calls the initialization function xxx_init()
to let it perform any required setup, such as argument checking
or memory allocation. If xxx_init()
returns
an error, MySQL aborts the SQL statement with an error message
and does not call the main or deinitialization functions.
Otherwise, MySQL calls the main function
xxx()
once for each row. After all rows have
been processed, MySQL calls the deinitialization function
xxx_deinit()
so that it can perform any
required cleanup.
For aggregate functions that work like
SUM()
, you must also provide the
following functions:
xxx_clear()
Reset the current aggregate value but do not insert the argument as the initial aggregate value for a new group.
xxx_add()
Add the argument to the current aggregate value.
MySQL handles aggregate UDFs as follows:
Call
xxx_init()
to let the aggregate function allocate any memory it needs for storing results.Sort the table according to the
GROUP BY
expression.Call
xxx_clear()
for the first row in each new group.Call
xxx_add()
for each row that belongs in the same group.Call
xxx()
to get the result for the aggregate when the group changes or after the last row has been processed.Repeat steps 3 to 5 until all rows has been processed
Call
xxx_deinit()
to let the UDF free any memory it has allocated.
All functions must be thread-safe. This includes not just the
main function, but the initialization and deinitialization
functions as well, and also the additional functions required by
aggregate functions. A consequence of this requirement is that
you are not permitted 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()
.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-adding-udf.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.