MySQL的UDF(User Defined Function)類似于一種API, 用戶根據一定的規范用C/C++(或采用C調用規范的語言)編寫一組函數(UDF),然后編譯成動態鏈接庫,通過DROP FUNCTION語句來加載和卸載UDF。UDF被加載后可以像調用MySQL的內置函數一樣來調用它,并且服務器在啟動時會自動加載原來存在的UDF。
復制代碼 代碼如下:
#ifdef STANDARD/* STANDARD is defined, don't use any mysql functions */ #include <stdlib.h> #include <stdio.h> #include <string.h> #ifdef __WIN__ typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */ typedef __int64 longlong; #else typedef unsigned long long ulonglong; typedef long long longlong; #endif /*__WIN__*/ #else #include <my_global.h> #include <my_sys.h> #if defined(MYSQL_SERVER) #include <m_string.h> /* To get strmov() */ #else /* when compiled as standalone */ #include <string.h> #endif #endif #include <mysql.h> #include <m_ctype.h> #include <m_string.h> #include <stdlib.h> #include <errno.h> #include <netdb.h> #include <unistd.h> #include<fcntl.h> #include<sys time.h=""> #include<sys ioctl.h=""> #include <sys types.h=""> #include <netinet in.h=""> #include <sys socket.h=""> #include <sys wait.h=""> #include<arpa inet.h=""> #include<unistd.h> #include <mysql.h> #include <ctype.h> #ifdef HAVE_DLOPEN
my_bool http_post_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void http_post_deinit(UDF_INIT *initid); longlong http_post(UDF_INIT *initid, UDF_ARGS *args, char *is_null,char *error); /************************************************************************* ** Example of init function ** Arguments: ** initid Points to a structure that the init function should fill. ** char *ptr; A pointer that the function can use. ** message Error message **RETURN This function should return 1 if something goes wrong. In this case **************************************************************************/ my_bool http_post_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if (args->arg_count < 3 ) { strcpy(message,"Wrong arguments to http_post; "); return 1; }