Rechercher dans le manuel MySQL

29.4.2.5 UDF Compiling and Installing

Files implementing UDFs must be compiled and installed on the host where the server runs. This process is described here for the example UDF file sql/udf_example.cc that is included in MySQL source distributions. For additional information about UDF installation, see Section 5.7.1, “Installing and Uninstalling User-Defined Functions”.

If a UDF will be referred to in statements that will be replicated to slave servers, you must ensure that every slave also has the function available. Otherwise, replication fails on the slaves when they attempt to invoke the function.

The immediately following instructions are for Unix. Instructions for Windows are given later in this section.

The udf_example.cc file contains the following functions:

  • metaphon() returns a metaphon string of the string argument. This is something like a soundex string, but it is more tuned for English.

  • myfunc_double() returns the sum of the ASCII values of the characters in its arguments, divided by the sum of the length of its arguments.

  • myfunc_int() returns the sum of the length of its arguments.

  • sequence([const int]) returns a sequence starting from the given number or 1 if no number has been given.

  • lookup() returns the IP address for a host name.

  • reverse_lookup() returns the host name for an IP address. The function may be called either with a single string argument of the form 'xxx.xxx.xxx.xxx' or with four numbers.

  • avgcost() returns an average cost. This is an aggregate function.

A dynamically loadable file should be compiled as a sharable library file, using a command something like this:

gcc -shared -o udf_example.so udf_example.cc

If you are using gcc with CMake (which is how MySQL itself is configured), you should be able to create udf_example.so with a simpler command:

make udf_example

After compiling a shared object containing UDFs, you must install it and tell MySQL about it. Compiling a shared object from udf_example.cc using gcc directly produces a file named udf_example.so. Copy the shared object to the server's plugin directory and name it udf_example.so. This directory is given by the value of the plugin_dir system variable.

On some systems, the ldconfig program that configures the dynamic linker does not recognize a shared object unless its name begins with lib. In this case you should rename a file such as udf_example.so to libudf_example.so.

On Windows, compile user-defined functions using the following procedure:

  1. Obtain a MySQL source distribution. See Section 2.1.2, “How to Get MySQL”.

  2. Obtain the CMake build utility, if necessary, from http://www.cmake.org. (Version 2.6 or later is required).

  3. In the source tree, look in the sql directory for files named udf_example.def and udf_example.cc. Copy both files from this directory to your working directory.

  4. Create a CMake makefile (CMakeLists.txt) with these contents:

    PROJECT(udf_example)
    
    # Path for MySQL include directory
    INCLUDE_DIRECTORIES("c:/mysql/include")
    
    ADD_DEFINITIONS("-DHAVE_DLOPEN")
    ADD_LIBRARY(udf_example MODULE udf_example.cc udf_example.def)
    TARGET_LINK_LIBRARIES(udf_example wsock32)
  5. Create the VC project and solution files, substituting an appropriate generator value:

    cmake -G "generator"

    Invoking cmake --help shows you a list of valid generators.

  6. Create udf_example.dll:

    devenv udf_example.sln /build Release

On all platforms, after the shared library file has been copied to the plugin_dir directory, notify mysqld about the new functions with the following statements. If library files have a suffix different from .so on your system, substitute the correct suffix throughout (for example, .dll on Windows).

  1. CREATE FUNCTION metaphon RETURNS STRING
  2.   SONAME 'udf_example.so';
  3. CREATE FUNCTION myfunc_double RETURNS REAL
  4.   SONAME 'udf_example.so';
  5.   SONAME 'udf_example.so';
  6.   SONAME 'udf_example.so';
  7. CREATE FUNCTION lookup RETURNS STRING
  8.   SONAME 'udf_example.so';
  9. CREATE FUNCTION reverse_lookup RETURNS STRING
  10.   SONAME 'udf_example.so';
  11. CREATE AGGREGATE FUNCTION avgcost RETURNS REAL
  12.   SONAME 'udf_example.so';

Once installed, a function remains installed until it is uninstalled.

To remove functions, use DROP FUNCTION:

  1. DROP FUNCTION metaphon;
  2. DROP FUNCTION myfunc_double;
  3. DROP FUNCTION myfunc_int;
  4. DROP FUNCTION sequence;
  5. DROP FUNCTION lookup;
  6. DROP FUNCTION reverse_lookup;
  7. DROP FUNCTION avgcost;

The CREATE FUNCTION and DROP FUNCTION statements update the func table in the mysql system database that serves as a UDF registry. The function's name, type and shared library name are saved in the mysql.func table. To create functions, you must have the INSERT privilege for the mysql database. To drop functions, you must have the DELETE privilege for the mysql database.

You cannnot use CREATE FUNCTION to reinstall a function that has previously been installed. To reinstall a function, first remove it with DROP FUNCTION, then install it with CREATE FUNCTION. You would need to do this, for example, if you upgrade to a new version of MySQL that provides an updated implementation of the function, or you recompile a new version of a function that you have written. Otherwise, the server continues to use the old version.

An active function is one that has been loaded with CREATE FUNCTION and not removed with DROP FUNCTION. All active functions are reloaded each time the server starts, unless you start mysqld with the --skip-grant-tables option. In this case, the server does not load UDFs during startup and UDFs are unavailable.


Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-udf-compiling.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut