Description:
Hi,
The mysql_set_local_infile_handler() and mysql_set_local_infile_default() functions of the C API are not documented. I wrote some documentation to explain them to someone else, so I'll paste it here in the hopes it will be useful in updating the MySQL manual:
The function prototypes are:
>>>>>
void
mysql_set_local_infile_handler(MYSQL *mysql,
int (*local_infile_init)(void **, const char *, void *),
int (*local_infile_read)(void *, char *, unsigned int),
void (*local_infile_end)(void *),
int (*local_infile_error)(void *, char *, unsigned int),
void *);
void
mysql_set_local_infile_default(MYSQL *mysql);
<<<<<
You have to write the following callback functions:
..._init(void **ptr, const char *filename, void *userdata)
Called once at the beginning to do any setup necessary, open files,
make connections, etc. Gets passed a void ** which is then passed
back to all the other functions (as void *) to allow your callbacks
to keep state. I don't remember what the userdata parameter does.
..._read(void *ptr, char *buf, uint buf_len)
Is called repeatedly, passed a pointer with a location to write to
and a maximum amount of data. You're under no obligation to write
that much, but don't write more. Return the number of bytes that
you actually wrote.
You have to do a little magic on your side to make sure you can keep
partial records, since you may not be able to write an entire record
at a time. (Hence part of the need for the void *.)
Return 0 when you run out of data to write. (Basically that means
you're sending an EOF.)
..._end(void *ptr)
Called once at the end of the reads (after _read() has returned 0)
to allow you to specifically clean up after yourself. You should
absolutely deallocate any memory allocated in _init() here.
..._error(void *ptr, char *error_msg, uint error_msg_len)
Called in order to get a textual error message to return to the user
in case any of your other functions returns an error condition. You
will need to store the error message yourself in the void * when it
is generated, so that you have it handy when the system calls your
_error() function.
After calling mysql_set_local_infile_handler() in your C code, and passing pointers to your callback functions, you can then issue a LOAD DATA LOCAL INFILE through mysql_query(), which will automatically call your callbacks from within libmysqlclient. You will get passed the filename specified in LOAD DATA LOCAL INFILE as the second parameter to your _init() function, and really, you can use it or not. You will always get passed one, but you don't really have to do anything with it.
For most applications you will probably have set things up beforehand and be ready to send data once you receive the _init() call, so you wouldn't need the filename for anything.
How to repeat:
Documentation bug, see description.