patternsqlMinor
MySQL can't import a custom library for udf
Viewed 0 times
cancustommysqlforlibraryimportudf
Problem
I am working on Linux Mint 17 Qiana with MySQL Ver 14.14 Distrib 5.5.53, for debian-linux-gnu (x86_64).
Following the instructions found here:
http://dev.mysql.com/doc/refman/5.7/en/adding-udf.html
and subsequent pages, I have created a library file for my own user-defined function. But MySQL gives an error when I create the MySQL function from the library file.
Here are the steps I made.
I have created the following C file:
I compile the C code:
MySQL plugin directory is given by:
the answer is:
```
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| plugin_dir | /usr/lib/mysq
Following the instructions found here:
http://dev.mysql.com/doc/refman/5.7/en/adding-udf.html
and subsequent pages, I have created a library file for my own user-defined function. But MySQL gives an error when I create the MySQL function from the library file.
Here are the steps I made.
I have created the following C file:
#include
#include
#include
#include
#include
//*** Functions for compressing a protein sequence into a binary
my_bool compress_protein_sequence_init(UDF_INIT* initid, UDF_ARGS* args, char* message) {
if(args->arg_count != 1) {
strcpy(message, "Error: compress_protein_sequence needs a string");
return 1;
}
if( args->arg_type[0] != STRING_RESULT ) {
args->arg_type[0] = STRING_RESULT;
}
return 0;
}
char* compress_protein_sequence(UDF_INIT* initid, UDF_ARGS* args, char* result, unsigned long* ret_length, char* is_null, char* error) {
unsigned long length = args->lengths[0];
//*** Checking on the arguments
*ret_length = (unsigned long) length;
if(!(result = malloc(sizeof(char) * ((*ret_length) + 1)))) {
*is_null = 1;
*error=1;
*ret_length = 0;
return 0;
}
if(initid->ptr) {
free(initid->ptr);
}
initid->ptr = result;
memset( result, '\0', *ret_length + 1 );
//*** Some logic
result[*ret_length] = '\0';
return result;
}
void compress_protein_sequence_deinit(UDF_INIT* initid) {
if( initid->ptr )
free( initid->ptr );
}I compile the C code:
gcc -Wall -I/usr/local/include -shared -o libcompress_sequence.so -c compress_sequence.cMySQL plugin directory is given by:
SHOW VARIABLES LIKE '%plugin%';the answer is:
```
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| plugin_dir | /usr/lib/mysq
Solution
The issue has been solved by:
removing the
The
Then MySQL complained about not being able to detect the function
I am now able to use the MySQL function
removing the
-c option from the gcc command line and adding the -fPIC option at the end, turning the command line into:gcc -Wall -I/usr/local/include -shared -o libcompress_sequence.so compress_sequence.c -fPICThe
-c option removed the linking, which is not wished here since the library is compiled in one step. So removing the -c option tells to gcc to make the linking with the libraries included in the C file. But then gcc didn't compile anymore, and complained about the option -fPIC missing. I added it and gcc compiled.Then MySQL complained about not being able to detect the function
COMPRESS_PROTEIN_SEQUENCE in my library. So I put it in small letters: compress_protein_sequence, as the function is named into my C code, then MySQL imported the library. The actual MySQL command is the following:CREATE FUNCTION compress_protein_sequence RETURNS STRING SONAME 'libcompress_sequence.so';I am now able to use the MySQL function
COMPRESS_PROTEIN_SEQUENCE.Code Snippets
gcc -Wall -I/usr/local/include -shared -o libcompress_sequence.so compress_sequence.c -fPICCREATE FUNCTION compress_protein_sequence RETURNS STRING SONAME 'libcompress_sequence.so';Context
StackExchange Database Administrators Q#153496, answer score: 4
Revisions (0)
No revisions yet.