PROJECT INDEX - UDF LIB HOME

MySQL UDF Library ( How to Extend )

Extending MyUDFLib :
  1. After all, you have to refer to MySQL manual Chapter ( Extending MySQL) to know every thing you need about how adding new UDF Functions.
  2. And, There are two ways to add new functions to MySQL:
    1. Through the MySQL standar user-defined function (UDF) interface ways,
    2. or using the UDF function definition/wrapper FUNCTION C macro. FUNCTION is set on MYSQL_UDF_API.h and do all the works for us (xxx_init, xxx_deinit, export as extern, ...), that I know isn't the best solution, but in some ways, Zend have the same view point :P :
           
           +----------------------------------------------------------------------+
           | FUNCTION(                                                            |
           |   name       , => function name                                      |
           |   type       , => function type retrun(STRING|REAL|LONG)             |
           |   /S;|R;|I;/ , => args->arg_type pattern (S:STRING;I:INTEGER;R:REAL) |
           |   arg_count  , => args->arg_count min , must be > 0                  |
           |   maybe_null , => initid->maybe_null (1|0)                           |
           |   max_length , => initid->max_length, -1 to no max                   |
           |   "USAGE: .."  => String to be used as error message                 |
           | )                                                                    |
           +----------------------------------------------------------------------+
                  
      For example :
      str_replace is a part of MyUDFLib::LibString and use BString library.
      needed args is :
      • Function name assignation as MySQL Server will call it : str_replace
      • Type : str_replace will reteurn a STRING
      • We have a very simple parametres pattern to check ("S;S;S;I") : first arg is STRING arg, the 2 is STRING, .., and the last is an INTEGER.
      • str_replace need 4 args min
      • str_replace will return String of 255 byte long.
      • And some help to display as error message, but one should try to keep the error message less than 80 bytes long! as a lucky MySQL developer say.
         /******************************************************************************
        ** str_replace
        **   use BString::extern int bfindreplace (bstring b, const_bstring find,
        **                           const_bstring replace, int position);
        **   Replace all occurrences of the find substring with a replace bstring 
        **   after a given position in the bstring b.
        ******************************************************************************/
        // {{{
        	FUNCTION( str_replace, STRING, "S;S;S;I", 4, 1, 255, 
            "USAGE: STRING str_replace(STRING str, STRING find, STRING replace, INT pos)" )
        	{
        		bstring       string_1 = bfromcstr ( ARGV(0) );
        		bstring       string_2 = bfromcstr ( ARGV(1) );
        		bstring       string_3 = bfromcstr ( ARGV(2) );
        		long long     pos      = *((long long*) ARGV(1) );  
        
        		bfindreplace ( string_1, string_2, string_3, pos ); 
        
        		RETURN_STRING( bdata( string_1 ) );
            }
        
        // }}} 
                            
INDEX - TOP - CONTACT