getFileModifiedTime.c
#include <mysql.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <unistd.h> #include <stdint.h> /*Resource allocation*/ my_bool getFileModifiedTime_init(UDF_INIT *initid, UDF_ARGS *args, char *message); /*Custom function*/ char *getFileModifiedTime(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error); /*Recycle*/ void getFileModifiedTime_deinit(UDF_INIT *initid); /* Parameter Description: UDF_INT * initid UDF_INIT pointers can be used to pass allocated resources to other functions. UDF_ARG * args UDF_ARG pointer char *message error message pointer Return value 0 for success, 1 for failure A brief description of the members of UFD_INT and UDF_ARG. typedef struct st_udf_args { unsigned int arg_count; number of arguments enum Item_result *arg_type; parameter type char **args; parameter pointer unsigned long *lengths; parameter length char *maybe_null; Whether it can be null, 1 means it can be null char **attributes; pointer to parameter attributes unsigned long *attribute_lengths; The pointer of the parameter attribute points to the length of the content void *extension; extension pointer } UDF_ARGS; typedef struct st_udf_init { my_bool maybe_null; 1 means the return value can be null unsigned int decimals; can be used to set the length after the decimal point of the double type unsigned long max_length; the maximum length of the result returned by the custom string function char *ptr; The memory allocated in the general init of the string pointer can give the address to ptr for passing to other functions, such as releasing the allocated memory in deinit my_bool const_item; whether the function returns a fixed result void *extension; extension pointer } UDF_INIT; */ /* The init function is called before the getFileModifiedTime function executes */ my_bool getFileModifiedTime_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { if(args->arg_count==1 && args->arg_type[0]==STRING_RESULT ){ initid->maybe_null = 1; return 0; } else { strcpy( message , "Expected exactly one parameter(STRING filePath)" ); return 1; } } /* Parameter Description: UDF_INT * initid Same as init function UDF_ARG * args The information used to read the incoming parameters: the incoming value, the length of the incoming value, the type, etc., see the description of the structure above for details *result reserved parameters *length Used to set the length of the return value *is_null is empty *error If set to 1 the custom function will no longer be called */ char *getFileModifiedTime(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { struct stat attr; int tmpResult = -1; static char str_time[20]; struct tm *local_time = NULL; time_t utc_time; tmpResult=stat(args->args[0], &attr); if (tmpResult == -1 ) { *length=0; return NULL; } /**length=strlen(ctime(&attr.st_mtime)); return ctime(&attr.st_mtime);*/ utc_time = attr.st_mtime; local_time = localtime(&utc_time); strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", local_time); *length=sizeof(str_time); return str_time; } /* The deinit function is called after the getFileModifiedTime function is executed*/ void getFileModifiedTime_deinit(UDF_INIT *initid) { if(initid->ptr){ free(initid->ptr); } return; }
gcc -shared -o getFileModifiedTime.so getFileModifiedTime.c -I /usr/include/mysql
sudo cp getFileModifiedTime.so /usr/lib/mysql/plugin/
mysql@localhost:(none) 05:31:46>create function getFileModifiedTime returns string soname "getFileModifiedTime.so";
mysql@localhost:(none) 05:31:46>select getFileModifiedTime("/home/haibo/script/c/getFileModifiedTime.c");
+-------------------------------------------------------------------+
| getFileModifiedTime("/home/haibo/script/c/getFileModifiedTime.c") |
+-------------------------------------------------------------------+
| 2016-11-10 17:02:04 |
+-------------------------------------------------------------------+
备注:只适合[linux/unix]