230
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
233
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
234
NET *net= &drizzle->net;
236
void *li_ptr; /* pass state to local_infile functions */
237
char *buf; /* buffer to be filled by local_infile_read */
238
struct st_drizzle_options *options= &drizzle->options;
240
/* check that we've got valid callback functions */
241
if (!(options->local_infile_init &&
242
options->local_infile_read &&
243
options->local_infile_end &&
244
options->local_infile_error))
246
/* if any of the functions is invalid, set the default */
247
drizzle_set_local_infile_default(drizzle);
250
/* copy filename into local memory and allocate read buffer */
251
if (!(buf=malloc(packet_length)))
253
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
257
/* initialize local infile (open file, usually) */
258
if ((*options->local_infile_init)(&li_ptr, net_filename,
259
options->local_infile_userdata))
261
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
263
strmov(net->sqlstate, unknown_sqlstate);
265
(*options->local_infile_error)(li_ptr,
267
sizeof(net->last_error)-1);
271
/* read blocks of data from local infile callback */
273
(*options->local_infile_read)(li_ptr, buf,
276
if (my_net_write(net, (uchar*) buf, readcount))
282
/* Send empty packet to mark end of file */
283
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
285
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
292
(*options->local_infile_error)(li_ptr,
294
sizeof(net->last_error)-1);
298
result=false; /* Ok */
301
/* free up memory allocated with _init, usually */
302
(*options->local_infile_end)(li_ptr);
309
/****************************************************************************
310
Default handlers for LOAD LOCAL INFILE
311
****************************************************************************/
313
typedef struct st_default_local_infile
317
const char *filename;
318
char error_msg[LOCAL_INFILE_ERROR_LEN];
319
} default_local_infile_data;
323
Open file for LOAD LOCAL INFILE
326
default_local_infile_init()
327
ptr Store pointer to internal data here
328
filename File name to open. This may be in unix format !
332
Even if this function returns an error, the load data interface
333
guarantees that default_local_infile_end() is called.
340
static int default_local_infile_init(void **ptr, const char *filename,
341
void *userdata __attribute__ ((unused)))
343
default_local_infile_data *data;
344
char tmp_name[FN_REFLEN];
346
if (!(*ptr= data= ((default_local_infile_data *)
347
malloc(sizeof(default_local_infile_data)))))
348
return 1; /* out of memory */
350
data->error_msg[0]= 0;
352
data->filename= filename;
354
fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
355
if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
357
data->error_num= my_errno;
358
snprintf(data->error_msg, sizeof(data->error_msg)-1,
359
EE(EE_FILENOTFOUND), tmp_name, data->error_num);
367
Read data for LOAD LOCAL INFILE
370
default_local_infile_read()
371
ptr Points to handle allocated by _init
373
buf_len Ammount of data to read
376
> 0 number of bytes read
381
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
384
default_local_infile_data*data = (default_local_infile_data *) ptr;
386
if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
388
data->error_num= EE_READ; /* the errmsg for not entire file read */
389
snprintf(data->error_msg, sizeof(data->error_msg)-1,
391
data->filename, my_errno);
398
Read data for LOAD LOCAL INFILE
401
default_local_infile_end()
402
ptr Points to handle allocated by _init
403
May be NULL if _init failed!
408
static void default_local_infile_end(void *ptr)
410
default_local_infile_data *data= (default_local_infile_data *) ptr;
411
if (data) /* If not error on open */
414
my_close(data->fd, MYF(MY_WME));
421
Return error from LOAD LOCAL INFILE
424
default_local_infile_end()
425
ptr Points to handle allocated by _init
426
May be NULL if _init failed!
427
error_msg Store error text here
428
error_msg_len Max lenght of error_msg
435
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
437
default_local_infile_data *data = (default_local_infile_data *) ptr;
438
if (data) /* If not error on open */
440
strmake(error_msg, data->error_msg, error_msg_len);
441
return data->error_num;
443
/* This can only happen if we got error on malloc of handle */
444
strmov(error_msg, ER(CR_OUT_OF_MEMORY));
445
return CR_OUT_OF_MEMORY;
450
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
451
int (*local_infile_init)(void **, const char *,
453
int (*local_infile_read)(void *, char *, uint),
454
void (*local_infile_end)(void *),
455
int (*local_infile_error)(void *, char *, uint),
458
drizzle->options.local_infile_init= local_infile_init;
459
drizzle->options.local_infile_read= local_infile_read;
460
drizzle->options.local_infile_end= local_infile_end;
461
drizzle->options.local_infile_error= local_infile_error;
462
drizzle->options.local_infile_userdata = userdata;
466
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
468
drizzle->options.local_infile_init= default_local_infile_init;
469
drizzle->options.local_infile_read= default_local_infile_read;
470
drizzle->options.local_infile_end= default_local_infile_end;
471
drizzle->options.local_infile_error= default_local_infile_error;
475
230
/**************************************************************************
476
231
Do a query. If query returned rows, free old rows.
477
232
Read data by drizzle_store_result or by repeat call of drizzle_fetch_row