In this section you will read how to define the function Interface, Create
function syntax , Drop function Syntax .
MySQL adding new user defined
function
Two ways are available to add new function to MySQL :
There are some advantage or disadvantage of User-defined function and Native function:-
After upgrading the MySQL distribution, you can continue to use previously installed user-defined function, but if in upgrade version UDF interface are changed then you can not. But for native function, you must repeat you modification each and every time you upgrade.
MySQL user defined function Interface
MySQL user-defined function interface provided the following feature:-
MySQL create function Syntax
The user-defined function is a way to extend mysql with new function.
It is work like a native mysql function as ABS( ) or CONCAT( ).
Example :
| CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL} SONAME shared_library_name |
The "function_name" means this function is identified by this name. And by this name SQL statements can invoke the function The RETURNS clause is used to define the function return value. Decimal function return the string value and write should be string value. The function "shared_library_name" it is a base name of the shared object file. It contains the code that is implement by the function. For creating a function, you must have the INSERT and privilege for the mysql database because in mysql.func system table a new row is added by the CREATE FUNTION that records the name of function, function type and shared library name. But if this table is not available then run the mysql_fix_privilege_tables script to create it.
MySQL AGGREGATE function works like a native function as
SUM( ) or COUNT( ). But your mysql.func table have to contain type column,
if it is not available then run the mysql_fix_privilege_tables script to
create it.
MySQL DROP Function Syntax
By this statement you can drop the user-defined function named function_name.
| DROP FUNCTION function_name |
For using this statement you must have the DELETED privilege for the MySQL
database. Because this function remove a row from the "mysql.func"
system table that record is the function name, function type, and share library name.
MySQL Adding new user defined function
There are different type of new user-defined functions are given below :-
| char *xxx(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error); |
Example of Integer function
| long long xxx(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); |
Example of real function
| double xxx(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); |
The initid parameter points to a UDF_INIT structure, which used to communicate information between functions.
| char *xxx_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); |
Before MySQL 4.1.1 version xxx_reset() function is used but after MySQL 4.1.1User-defined function interface uses xxx_clear( ) instead of xxx_reset().
| char *xxx_clear(UDF_INIT *initid, char *is_null, char *error); |
Here "Is_null" is set to point to char(0) before the calling xxx_clear( ) function. If something happen wrong then the value can be stored in the variable to which the error argument points. and it points to a single byte variable not a string buffer.
| char *xxx_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); |
The user-defined function use the pointer argument to is_null and error are the same call function for the xxx_reset( ), xxx_clear( ) and xxx_add( ) .
| if (args->arg_count != 3) { strcpy(message,"XXX() requires three arguments"); return 1; } |
| if (args->arg_type[0] != string_result || args->arg_type[1] != int_result) { strcpy(message,"XXX( ) requires a string and an integer"); return 1; } |
| memcpy(result, "result string", 13); *length = 13; |
The xxx() have the result buffer and its length is 255 bytes. If your result fit in the buffer then you don?t have to allocate more memory for results.
But if your string length is more than 255 bytes, then you have to allocate the space for it by malloc() in your xxx_init() or xxx() function and free it in your xxx_deinit() function. The allocated memory can be stored in the ptr slot in the UDF_INIT structure for reuse by future xxx() calls.
| double Item_func_newname::val() longlong Item_func_newname::val_int() String *Item_func_newname::Str(String *str) |
| void Item_func_newname::fix_length_and_dec() |
It is used to calculate max_length that is based on the given arguments. Max_length has the maximum number of characters that can return by the function. If main function is not returning a NULL value then this function can set maybe_=0. This function is also used to check the arguments of any function can return a NULL or not, by checking the arguments variable maybe_mull.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: MySQL User Interface View All Comments
Post your Comment