Go to the first, previous, next, last section, table of contents.

Adding functions to MySQL

Adding new functions to MySQL

If you need it as SQL function (like SOUNDEX()), it's real easy:

  1. Add one line in sql_lex.cc defining the function name in the sql_functions array.
  2. Add two lines in sql_yacc.y. On defines the preprocessor symbol yacc can define (this should be added at the beginning of the file). Then define the function parameters and create an 'item' with these parameters. Check, for example, all occurrences of SOUNDEX in sql_yacc.y
  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: double *Item_func_newname::val() If you are defining a number function or String *Item_func_newname::Str(String *str) if you are defining a string function.
  5. You should probably also define the following function: void Item_func_newname::fix_length_and_dec() This should at least calcutate max_length based on the given arguments. max_length is the maximum number of chars the function may return. If the function can't return a NULL, one should set maybe_null = 0.

About string functions:

  1. For string functions the String *str argument provides a string buffer that may be used to hold the result.
  2. A string function should return the string that holds the result.
  3. All current string functions tries to avoid to alloc any memory unless absolutely necessary!

Go to the first, previous, next, last section, table of contents.

Casa de Bender