This is a discussion on constructor in dynamicly loaded library containing user defined functions not run when loading from mysql? within the MySQL Database forums, part of the Database Forums category; Hi, I've been writing a user defined function with a very expensive initialisation which only needs to run once. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I've been writing a user defined function with a very expensive initialisation which only needs to run once. I thought it might be possible to put it in the constructor section of the shared library object (__attribute__((constructor)) function) to be run at dlopen time, but this does not seem to be run when loading from mysql. Is this really the case? If so, what's the correct way to go about solving the situation? Would creating a soliton be ok? (eg. the following dummy library returns 0 when called from mysql, but should return 3 if the constructor would be run. //start #ifdef STANDARD #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> #endif #include <mysql.h> #ifdef HAVE_DLOPEN extern "C" { my_bool dummy_init(UDF_INIT *initid, UDF_ARGS *args, char *message); void dummy_deinit(UDF_INIT *initid); long long dummy(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error); void __attribute__((constructor)) dummy_real_init(void); void __attribute__((destructor)) dummy_real_deinit(void); } static long long secret = 0; void __attribute__((constructor)) dummy_real_init(void) { secret = 3; } void __attribute__((destructor)) dummy_real_deinit(void) { secret = 1; } my_bool dummy_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { return 0; } void dummy_deinit(UDF_INIT *initid) { } long long dummy(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { return secret; } #endif /* HAVE_DLOPEN */ //end Thanks, alejandro |
|
|||
|
Alejandro Dubrovsky wrote:
> I've been writing a user defined function with a very expensive > initialisation which only needs to run once. The above immediately gives me a gut feeling that the work done by your UDF should be implemented in the application, not in a UDF. Or alternately by using additional tables in the database. That is, there are ways to keep predefined data in the database and use operators like JOIN to perform work that could have been done by a UDF. Regards, Bill K. |