CodeGen_MySQL_UDF

This PEAR package provides a command udf-gen that takes a XML specification file with embedded C code and generates a full working MySQL User Defined Function source project from that. The generated code includes:

  • the actual code and header file
  • autotools files for a working configure
  • a test framework
  • DocBook? documentation for the generated function(s)

Using a code generator like this when implementing a UDF lets you focus on the additional functionality you want to implement while shielding you from all the details surrounding the task.

A minimal working example looks like this:

<?xml version="1.0"?>
<udf name="minimal">
  <function name="twice" returns="int">
    <param name="val" type="int"/>
    <code>
      return val * 2;
    </code>
  </function>
</udf>

and you can create a UDF library from this using (on a unixoid system)

$ udf-gen minimal.xml
$ cd minimal
$ configure  --libdir=...
$ make
$ sudo make install

If the libdir given is in the MySQL servers library search path you can now load the newly created twice() function using

CREATE FUNCTION twice RETURNS INTEGER SONAME 'minimal.so';

and try it using

SELECT twice(1);

which should return 2.

Easy, isn't it? Your first UDF is only 5 minutes away ... :)

Prerequisites

  • for generating a source package from a XML description:
    • PHP 5 and a working PEAR installation
    • GNU autotools: autoconf, automake, libtool ...
  • for compiling the generated code:
    • a working C compiler and make tool
    • a MySQL installation including C header files

Installation

The package is hosted in the PEAR repository, you can install it using

  pear install --all-deps CodeGen_MySQL_UDF-beta

Resources

UDF Projects

MySQL User Defined Functions created using this tool: