~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle.c

  • Committer: Brian Aker
  • Date: 2008-08-01 06:48:10 UTC
  • mfrom: (236.1.38 codestyle)
  • Revision ID: brian@tangent.org-20080801064810-5qhx3x6o099uf14r
Merging Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
  char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
157
157
  char *end= buff;
158
158
  int rc;
159
 
  CHARSET_INFO *saved_cs= drizzle->charset;
 
159
  const CHARSET_INFO *saved_cs= drizzle->charset;
160
160
 
161
161
  /* Get the connection-default character set. */
162
162
 
227
227
}
228
228
 
229
229
 
230
 
bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
231
 
{
232
 
  bool result= true;
233
 
  uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
234
 
  NET *net= &drizzle->net;
235
 
  int readcount;
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;
239
 
 
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))
245
 
  {
246
 
    /* if any of the functions is invalid, set the default */
247
 
    drizzle_set_local_infile_default(drizzle);
248
 
  }
249
 
 
250
 
  /* copy filename into local memory and allocate read buffer */
251
 
  if (!(buf=malloc(packet_length)))
252
 
  {
253
 
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
254
 
    return(1);
255
 
  }
256
 
 
257
 
  /* initialize local infile (open file, usually) */
258
 
  if ((*options->local_infile_init)(&li_ptr, net_filename,
259
 
    options->local_infile_userdata))
260
 
  {
261
 
    VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
262
 
    net_flush(net);
263
 
    strmov(net->sqlstate, unknown_sqlstate);
264
 
    net->last_errno=
265
 
      (*options->local_infile_error)(li_ptr,
266
 
                                     net->last_error,
267
 
                                     sizeof(net->last_error)-1);
268
 
    goto err;
269
 
  }
270
 
 
271
 
  /* read blocks of data from local infile callback */
272
 
  while ((readcount =
273
 
    (*options->local_infile_read)(li_ptr, buf,
274
 
          packet_length)) > 0)
275
 
  {
276
 
    if (my_net_write(net, (uchar*) buf, readcount))
277
 
    {
278
 
      goto err;
279
 
    }
280
 
  }
281
 
 
282
 
  /* Send empty packet to mark end of file */
283
 
  if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
284
 
  {
285
 
    set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
286
 
    goto err;
287
 
  }
288
 
 
289
 
  if (readcount < 0)
290
 
  {
291
 
    net->last_errno=
292
 
      (*options->local_infile_error)(li_ptr,
293
 
                                     net->last_error,
294
 
                                     sizeof(net->last_error)-1);
295
 
    goto err;
296
 
  }
297
 
 
298
 
  result=false;                                 /* Ok */
299
 
 
300
 
err:
301
 
  /* free up memory allocated with _init, usually */
302
 
  (*options->local_infile_end)(li_ptr);
303
 
  if(buf)
304
 
    free(buf);
305
 
  return(result);
306
 
}
307
 
 
308
 
 
309
 
/****************************************************************************
310
 
  Default handlers for LOAD LOCAL INFILE
311
 
****************************************************************************/
312
 
 
313
 
typedef struct st_default_local_infile
314
 
{
315
 
  int fd;
316
 
  int error_num;
317
 
  const char *filename;
318
 
  char error_msg[LOCAL_INFILE_ERROR_LEN];
319
 
} default_local_infile_data;
320
 
 
321
 
 
322
 
/*
323
 
  Open file for LOAD LOCAL INFILE
324
 
 
325
 
  SYNOPSIS
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 !
329
 
 
330
 
 
331
 
  NOTES
332
 
    Even if this function returns an error, the load data interface
333
 
    guarantees that default_local_infile_end() is called.
334
 
 
335
 
  RETURN
336
 
    0  ok
337
 
    1  error
338
 
*/
339
 
 
340
 
static int default_local_infile_init(void **ptr, const char *filename,
341
 
             void *userdata __attribute__ ((unused)))
342
 
{
343
 
  default_local_infile_data *data;
344
 
  char tmp_name[FN_REFLEN];
345
 
 
346
 
  if (!(*ptr= data= ((default_local_infile_data *)
347
 
                     malloc(sizeof(default_local_infile_data)))))
348
 
    return 1; /* out of memory */
349
 
 
350
 
  data->error_msg[0]= 0;
351
 
  data->error_num=    0;
352
 
  data->filename= filename;
353
 
 
354
 
  fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
355
 
  if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
356
 
  {
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);
360
 
    return 1;
361
 
  }
362
 
  return 0; /* ok */
363
 
}
364
 
 
365
 
 
366
 
/*
367
 
  Read data for LOAD LOCAL INFILE
368
 
 
369
 
  SYNOPSIS
370
 
    default_local_infile_read()
371
 
    ptr      Points to handle allocated by _init
372
 
    buf      Read data here
373
 
    buf_len    Ammount of data to read
374
 
 
375
 
  RETURN
376
 
    > 0    number of bytes read
377
 
    == 0  End of data
378
 
    < 0    Error
379
 
*/
380
 
 
381
 
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
382
 
{
383
 
  int count;
384
 
  default_local_infile_data*data = (default_local_infile_data *) ptr;
385
 
 
386
 
  if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
387
 
  {
388
 
    data->error_num= EE_READ; /* the errmsg for not entire file read */
389
 
    snprintf(data->error_msg, sizeof(data->error_msg)-1,
390
 
             EE(EE_READ),
391
 
             data->filename, my_errno);
392
 
  }
393
 
  return count;
394
 
}
395
 
 
396
 
 
397
 
/*
398
 
  Read data for LOAD LOCAL INFILE
399
 
 
400
 
  SYNOPSIS
401
 
    default_local_infile_end()
402
 
    ptr      Points to handle allocated by _init
403
 
      May be NULL if _init failed!
404
 
 
405
 
  RETURN
406
 
*/
407
 
 
408
 
static void default_local_infile_end(void *ptr)
409
 
{
410
 
  default_local_infile_data *data= (default_local_infile_data *) ptr;
411
 
  if (data)          /* If not error on open */
412
 
  {
413
 
    if (data->fd >= 0)
414
 
      my_close(data->fd, MYF(MY_WME));
415
 
    free(ptr);
416
 
  }
417
 
}
418
 
 
419
 
 
420
 
/*
421
 
  Return error from LOAD LOCAL INFILE
422
 
 
423
 
  SYNOPSIS
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
429
 
 
430
 
  RETURN
431
 
    error message number
432
 
*/
433
 
 
434
 
static int
435
 
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
436
 
{
437
 
  default_local_infile_data *data = (default_local_infile_data *) ptr;
438
 
  if (data)          /* If not error on open */
439
 
  {
440
 
    strmake(error_msg, data->error_msg, error_msg_len);
441
 
    return data->error_num;
442
 
  }
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;
446
 
}
447
 
 
448
 
 
449
 
void
450
 
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
451
 
                               int (*local_infile_init)(void **, const char *,
452
 
                               void *),
453
 
                               int (*local_infile_read)(void *, char *, uint),
454
 
                               void (*local_infile_end)(void *),
455
 
                               int (*local_infile_error)(void *, char *, uint),
456
 
                               void *userdata)
457
 
{
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;
463
 
}
464
 
 
465
 
 
466
 
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
467
 
{
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;
472
 
}
473
 
 
474
 
 
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