231
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
234
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
235
NET *net= &drizzle->net;
237
void *li_ptr; /* pass state to local_infile functions */
238
char *buf; /* buffer to be filled by local_infile_read */
239
struct st_drizzle_options *options= &drizzle->options;
241
/* check that we've got valid callback functions */
242
if (!(options->local_infile_init &&
243
options->local_infile_read &&
244
options->local_infile_end &&
245
options->local_infile_error))
247
/* if any of the functions is invalid, set the default */
248
drizzle_set_local_infile_default(drizzle);
251
/* copy filename into local memory and allocate read buffer */
252
if (!(buf=malloc(packet_length)))
254
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
258
/* initialize local infile (open file, usually) */
259
if ((*options->local_infile_init)(&li_ptr, net_filename,
260
options->local_infile_userdata))
262
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
264
strmov(net->sqlstate, unknown_sqlstate);
266
(*options->local_infile_error)(li_ptr,
268
sizeof(net->last_error)-1);
272
/* read blocks of data from local infile callback */
274
(*options->local_infile_read)(li_ptr, buf,
277
if (my_net_write(net, (uchar*) buf, readcount))
283
/* Send empty packet to mark end of file */
284
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
286
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
293
(*options->local_infile_error)(li_ptr,
295
sizeof(net->last_error)-1);
299
result=false; /* Ok */
302
/* free up memory allocated with _init, usually */
303
(*options->local_infile_end)(li_ptr);
310
/****************************************************************************
311
Default handlers for LOAD LOCAL INFILE
312
****************************************************************************/
314
typedef struct st_default_local_infile
318
const char *filename;
319
char error_msg[LOCAL_INFILE_ERROR_LEN];
320
} default_local_infile_data;
324
Open file for LOAD LOCAL INFILE
327
default_local_infile_init()
328
ptr Store pointer to internal data here
329
filename File name to open. This may be in unix format !
333
Even if this function returns an error, the load data interface
334
guarantees that default_local_infile_end() is called.
341
static int default_local_infile_init(void **ptr, const char *filename,
342
void *userdata __attribute__ ((unused)))
344
default_local_infile_data *data;
345
char tmp_name[FN_REFLEN];
347
if (!(*ptr= data= ((default_local_infile_data *)
348
malloc(sizeof(default_local_infile_data)))))
349
return 1; /* out of memory */
351
data->error_msg[0]= 0;
353
data->filename= filename;
355
fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
356
if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
358
data->error_num= my_errno;
359
snprintf(data->error_msg, sizeof(data->error_msg)-1,
360
EE(EE_FILENOTFOUND), tmp_name, data->error_num);
368
Read data for LOAD LOCAL INFILE
371
default_local_infile_read()
372
ptr Points to handle allocated by _init
374
buf_len Ammount of data to read
377
> 0 number of bytes read
382
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
385
default_local_infile_data*data = (default_local_infile_data *) ptr;
387
if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
389
data->error_num= EE_READ; /* the errmsg for not entire file read */
390
snprintf(data->error_msg, sizeof(data->error_msg)-1,
392
data->filename, my_errno);
399
Read data for LOAD LOCAL INFILE
402
default_local_infile_end()
403
ptr Points to handle allocated by _init
404
May be NULL if _init failed!
409
static void default_local_infile_end(void *ptr)
411
default_local_infile_data *data= (default_local_infile_data *) ptr;
412
if (data) /* If not error on open */
415
my_close(data->fd, MYF(MY_WME));
422
Return error from LOAD LOCAL INFILE
425
default_local_infile_end()
426
ptr Points to handle allocated by _init
427
May be NULL if _init failed!
428
error_msg Store error text here
429
error_msg_len Max lenght of error_msg
436
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
438
default_local_infile_data *data = (default_local_infile_data *) ptr;
439
if (data) /* If not error on open */
441
strmake(error_msg, data->error_msg, error_msg_len);
442
return data->error_num;
444
/* This can only happen if we got error on malloc of handle */
445
strmov(error_msg, ER(CR_OUT_OF_MEMORY));
446
return CR_OUT_OF_MEMORY;
451
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
452
int (*local_infile_init)(void **, const char *,
454
int (*local_infile_read)(void *, char *, uint),
455
void (*local_infile_end)(void *),
456
int (*local_infile_error)(void *, char *, uint),
459
drizzle->options.local_infile_init= local_infile_init;
460
drizzle->options.local_infile_read= local_infile_read;
461
drizzle->options.local_infile_end= local_infile_end;
462
drizzle->options.local_infile_error= local_infile_error;
463
drizzle->options.local_infile_userdata = userdata;
467
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
469
drizzle->options.local_infile_init= default_local_infile_init;
470
drizzle->options.local_infile_read= default_local_infile_read;
471
drizzle->options.local_infile_end= default_local_infile_end;
472
drizzle->options.local_infile_error= default_local_infile_error;
476
231
/**************************************************************************
477
232
Do a query. If query returned rows, free old rows.
478
233
Read data by drizzle_store_result or by repeat call of drizzle_fetch_row