~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleimport.cc

  • Committer: Jay Pipes
  • Date: 2009-02-04 15:44:25 UTC
  • mfrom: (829 drizzle)
  • mto: This revision was merged to the branch mainline in revision 830.
  • Revision ID: jpipes@serialcoder-20090204154425-th8xfk2ujz2y8xwg
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
using namespace std;
38
38
 
39
39
/* Global Thread counter */
40
 
uint32_t counter;
 
40
uint counter;
41
41
pthread_mutex_t counter_mutex;
42
42
pthread_cond_t count_threshhold;
43
43
 
44
 
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
45
 
                     drizzle_return_t ret, char *table);
46
 
static char *field_escape(char *to,const char *from,uint32_t length);
 
44
static void db_error_with_table(DRIZZLE *drizzle, char *table);
 
45
static void db_error(DRIZZLE *drizzle);
 
46
static char *field_escape(char *to,const char *from,uint length);
47
47
static char *add_load_option(char *ptr,const char *object,
48
48
           const char *statement);
49
49
 
50
50
static bool verbose= false, lock_tables= false, ignore_errors= false,
51
51
            opt_delete= false, opt_replace= false, silent= false,
52
 
            ignore_unique= false, opt_compress= false, opt_low_priority= false,
 
52
            ignore= false, opt_compress= false, opt_low_priority= false,
53
53
            tty_password= false;
54
54
static bool debug_info_flag= false, debug_check_flag= false;
55
 
static uint32_t opt_use_threads= 0, opt_local_file= 0, my_end_arg= 0;
 
55
static uint opt_use_threads= 0, opt_local_file= 0, my_end_arg= 0;
56
56
static char  *opt_password= NULL, *current_user= NULL,
57
57
    *current_host= NULL, *current_db= NULL, *fields_terminated= NULL,
58
58
    *lines_terminated= NULL, *enclosed= NULL, *opt_enclosed= NULL,
59
59
    *escaped= NULL, *opt_columns= NULL,
60
60
    *default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
 
61
static uint opt_protocol= 0;
61
62
static uint32_t opt_drizzle_port= 0;
62
63
static char * opt_drizzle_unix_port= 0;
63
64
static int64_t opt_ignore_lines= -1;
65
66
 
66
67
static struct my_option my_long_options[] =
67
68
{
 
69
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
70
   "Directory where character sets are.", (char**) &charsets_dir,
 
71
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
68
72
  {"default-character-set", OPT_DEFAULT_CHARSET,
69
73
   "Set the default character set.", (char**) &default_charset,
70
74
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
105
109
  {"host", 'h', "Connect to host.", (char**) &current_host,
106
110
   (char**) &current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
107
111
  {"ignore", 'i', "If duplicate unique key was found, keep old row.",
108
 
   (char**) &ignore_unique, (char**) &ignore_unique, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
112
   (char**) &ignore, (char**) &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
109
113
  {"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
110
114
   (char**) &opt_ignore_lines, (char**) &opt_ignore_lines, 0, GET_LL,
111
115
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
158
162
static void print_version(void)
159
163
{
160
164
  printf("%s  Ver %s Distrib %s, for %s (%s)\n" ,my_progname,
161
 
    IMPORT_VERSION, drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
 
165
    IMPORT_VERSION, drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
162
166
}
163
167
 
164
168
 
264
268
    fprintf(stderr, "You can't use ..enclosed.. and ..optionally-enclosed.. at the same time.\n");
265
269
    return(1);
266
270
  }
267
 
  if (opt_replace && ignore_unique)
 
271
  if (opt_replace && ignore)
268
272
  {
269
 
    fprintf(stderr, "You can't use --ignore_unique (-i) and --replace (-r) at the same time.\n");
 
273
    fprintf(stderr, "You can't use --ignore (-i) and --replace (-r) at the same time.\n");
270
274
    return(1);
271
275
  }
272
276
  if (strcmp(default_charset, charset_info->csname) &&
273
 
      !(charset_info= get_charset_by_csname(default_charset, MY_CS_PRIMARY)))
 
277
      !(charset_info= get_charset_by_csname(default_charset,
 
278
                MY_CS_PRIMARY, MYF(MY_WME))))
274
279
    exit(1);
275
280
  if (*argc < 2)
276
281
  {
280
285
  current_db= *((*argv)++);
281
286
  (*argc)--;
282
287
  if (tty_password)
283
 
    opt_password=client_get_tty_password(NULL);
 
288
    opt_password=get_tty_password(NULL);
284
289
  return(0);
285
290
}
286
291
 
287
292
 
288
293
 
289
 
static int write_to_table(char *filename, drizzle_con_st *con)
 
294
static int write_to_table(char *filename, DRIZZLE *drizzle)
290
295
{
291
296
  char tablename[FN_REFLEN], hard_path[FN_REFLEN],
292
297
       sql_statement[FN_REFLEN*16+256], *end;
293
 
  drizzle_result_st result;
294
 
  drizzle_return_t ret;
295
298
 
296
299
  fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
297
300
  if (!opt_local_file)
308
311
#else
309
312
    sprintf(sql_statement, "DELETE FROM %s", tablename);
310
313
#endif
311
 
    if (drizzle_query_str(con, &result, sql_statement, &ret) == NULL ||
312
 
        ret != DRIZZLE_RETURN_OK)
 
314
    if (drizzle_query(drizzle, sql_statement))
313
315
    {
314
 
      db_error(con, &result, ret, tablename);
 
316
      db_error_with_table(drizzle, tablename);
315
317
      return(1);
316
318
    }
317
 
    drizzle_result_free(&result);
318
319
  }
319
320
  if (verbose)
320
321
  {
331
332
  end= strchr(sql_statement, '\0');
332
333
  if (opt_replace)
333
334
    end= strcpy(end, " REPLACE")+8;
334
 
  if (ignore_unique)
 
335
  if (ignore)
335
336
    end= strcpy(end, " IGNORE")+7;
336
337
 
337
338
  end+= sprintf(end, " INTO TABLE %s", tablename);
347
348
  if (opt_ignore_lines >= 0)
348
349
  {
349
350
    end= strcpy(end, " IGNORE ")+8;
350
 
    ostringstream buffer;
351
 
    buffer << opt_ignore_lines;
352
 
    end= strcpy(end, buffer.str().c_str())+ buffer.str().size();
 
351
    end= int64_t2str(opt_ignore_lines, end, 10);
353
352
    end= strcpy(end, " LINES")+6;
354
353
  }
355
354
  if (opt_columns)
360
359
  }
361
360
  *end= '\0';
362
361
 
363
 
  if (drizzle_query_str(con, &result, sql_statement, &ret) == NULL ||
364
 
      ret != DRIZZLE_RETURN_OK)
 
362
  if (drizzle_query(drizzle, sql_statement))
365
363
  {
366
 
    db_error(con, &result, ret, tablename);
 
364
    db_error_with_table(drizzle, tablename);
367
365
    return(1);
368
366
  }
369
367
  if (!silent)
370
368
  {
371
 
    if (strcmp(drizzle_result_info(&result), ""))
 
369
    if (drizzle_info(drizzle)) /* If NULL-pointer, print nothing */
372
370
    {
373
371
      fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
374
 
        drizzle_result_info(&result));
 
372
        drizzle_info(drizzle));
375
373
    }
376
374
  }
377
 
  drizzle_result_free(&result);
378
375
  return(0);
379
376
}
380
377
 
381
378
 
382
 
static void lock_table(drizzle_con_st *con, int tablecount, char **raw_tablename)
 
379
 
 
380
static void lock_table(DRIZZLE *drizzle, int tablecount, char **raw_tablename)
383
381
{
384
382
  string query;
385
383
  int i;
386
384
  char tablename[FN_REFLEN];
387
 
  drizzle_result_st result;
388
 
  drizzle_return_t ret;
389
385
 
390
386
  if (verbose)
391
387
    fprintf(stdout, "Locking tables for write\n");
396
392
    query.append(tablename);
397
393
    query.append(" WRITE,");
398
394
  }
399
 
  if (drizzle_query(con, &result, query.c_str(), query.length()-1,
400
 
                    &ret) == NULL ||
401
 
      ret != DRIZZLE_RETURN_OK)
402
 
  {
403
 
    db_error(con, &result, ret, NULL);
404
 
    /* We shall countinue here, if --force was given */
405
 
    return;
406
 
  }
407
 
  drizzle_result_free(&result);
 
395
  if (drizzle_real_query(drizzle, query.c_str(), query.length()-1))
 
396
    db_error(drizzle); /* We shall countinue here, if --force was given */
408
397
}
409
398
 
410
399
 
411
 
static drizzle_con_st *db_connect(char *host, char *database,
412
 
                                  char *user, char *passwd)
 
400
 
 
401
 
 
402
static DRIZZLE *db_connect(char *host, char *database,
 
403
                         char *user, char *passwd)
413
404
{
414
 
  drizzle_st *drizzle;
415
 
  drizzle_con_st *con;
416
 
  drizzle_return_t ret;
417
 
 
 
405
  DRIZZLE *drizzle;
418
406
  if (verbose)
419
407
    fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
420
408
  if (!(drizzle= drizzle_create(NULL)))
421
409
    return 0;
422
 
  if (!(con= drizzle_con_add_tcp(drizzle,NULL,host,opt_drizzle_port,user,passwd,
423
 
                                 database, DRIZZLE_CON_NONE)))
424
 
  {
425
 
    return 0;
426
 
  }
427
 
 
428
 
  if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
 
410
  if (opt_compress)
 
411
    drizzle_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
412
  if (opt_local_file)
 
413
    drizzle_options(drizzle,DRIZZLE_OPT_LOCAL_INFILE,
 
414
      (char*) &opt_local_file);
 
415
  if (opt_protocol)
 
416
    drizzle_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
417
  if (!(drizzle_connect(drizzle,host,user,passwd,
 
418
                           database,opt_drizzle_port,opt_drizzle_unix_port,
 
419
                           0)))
429
420
  {
430
421
    ignore_errors=0;    /* NO RETURN FROM db_error */
431
 
    db_error(con, NULL, ret, NULL);
 
422
    db_error(drizzle);
432
423
  }
433
 
 
 
424
  drizzle->reconnect= 0;
434
425
  if (verbose)
435
426
    fprintf(stdout, "Selecting database %s\n", database);
436
 
 
437
 
  return con;
 
427
  if (drizzle_select_db(drizzle, database))
 
428
  {
 
429
    ignore_errors=0;
 
430
    db_error(drizzle);
 
431
  }
 
432
  return drizzle;
438
433
}
439
434
 
440
435
 
441
436
 
442
 
static void db_disconnect(char *host, drizzle_con_st *con)
 
437
static void db_disconnect(char *host, DRIZZLE *drizzle)
443
438
{
444
439
  if (verbose)
445
440
    fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
446
 
  drizzle_free(drizzle_con_drizzle(con));
 
441
  drizzle_close(drizzle);
447
442
}
448
443
 
449
444
 
450
445
 
451
 
static void safe_exit(int error, drizzle_con_st *con)
 
446
static void safe_exit(int error, DRIZZLE *drizzle)
452
447
{
453
448
  if (ignore_errors)
454
449
    return;
455
 
  if (con)
456
 
    drizzle_free(drizzle_con_drizzle(con));
 
450
  if (drizzle)
 
451
    drizzle_close(drizzle);
457
452
  exit(error);
458
453
}
459
454
 
460
455
 
461
456
 
462
 
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
463
 
                     drizzle_return_t ret, char *table)
464
 
{
465
 
  if (ret == DRIZZLE_RETURN_ERROR_CODE)
466
 
  {
467
 
    my_printf_error(0,"Error: %d, %s%s%s", MYF(0),
468
 
                    drizzle_result_error_code(result),
469
 
                    drizzle_result_error(result),
470
 
                    table ? ", when using table: " : "", table ? table : "");
471
 
    drizzle_result_free(result);
472
 
  }
473
 
  else
474
 
  {
475
 
    my_printf_error(0,"Error: %d, %s%s%s", MYF(0), ret, drizzle_con_error(con),
476
 
                    table ? ", when using table: " : "", table ? table : "");
477
 
  }
478
 
 
479
 
  safe_exit(1, con);
 
457
static void db_error_with_table(DRIZZLE *drizzle, char *table)
 
458
{
 
459
  my_printf_error(0,"Error: %d, %s, when using table: %s",
 
460
      MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle), table);
 
461
  safe_exit(1, drizzle);
 
462
}
 
463
 
 
464
 
 
465
 
 
466
static void db_error(DRIZZLE *drizzle)
 
467
{
 
468
  my_printf_error(0,"Error: %d %s", MYF(0), drizzle_errno(drizzle), drizzle_error(drizzle));
 
469
  safe_exit(1, drizzle);
480
470
}
481
471
 
482
472
 
492
482
    {
493
483
      /* char constant; escape */
494
484
      ptr+= sprintf(ptr, " %s '", statement); 
495
 
      ptr= field_escape(ptr,object,(uint32_t) strlen(object));
 
485
      ptr= field_escape(ptr,object,(uint) strlen(object));
496
486
      *ptr++= '\'';
497
487
    }
498
488
  }
506
496
** syntax errors from the SQL parser.
507
497
*/
508
498
 
509
 
static char *field_escape(char *to,const char *from,uint32_t length)
 
499
static char *field_escape(char *to,const char *from,uint length)
510
500
{
511
501
  const char *end;
512
 
  uint32_t end_backslashes=0;
 
502
  uint end_backslashes=0;
513
503
 
514
504
  for (end= from+length; from != end; from++)
515
505
  {
536
526
{
537
527
  int error;
538
528
  char *raw_table_name= (char *)arg;
539
 
  drizzle_con_st *con= NULL;
540
 
  drizzle_result_st result;
541
 
  drizzle_return_t ret;
 
529
  DRIZZLE *drizzle= 0;
542
530
 
543
 
  if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
 
531
  if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
544
532
  {
545
533
    goto error;
546
534
  }
547
535
 
548
 
  if (drizzle_query_str(con, &result,
549
 
                        "/*!40101 set @@character_set_database=binary */;",
550
 
                        &ret) == NULL ||
551
 
      ret != DRIZZLE_RETURN_OK)
 
536
  if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
552
537
  {
553
 
    db_error(con, &result, ret, NULL);
554
 
    /* We shall countinue here, if --force was given */
 
538
    db_error(drizzle); /* We shall countinue here, if --force was given */
555
539
    goto error;
556
540
  }
557
541
 
558
542
  /*
559
543
    We are not currently catching the error here.
560
544
  */
561
 
  if((error= write_to_table(raw_table_name, con)))
 
545
  if((error= write_to_table(raw_table_name, drizzle)))
562
546
    if (exitcode == 0)
563
547
      exitcode= error;
564
548
 
565
549
error:
566
 
  if (con)
567
 
    db_disconnect(current_host, con);
 
550
  if (drizzle)
 
551
    db_disconnect(current_host, drizzle);
568
552
 
569
553
  pthread_mutex_lock(&counter_mutex);
570
554
  counter--;
647
631
  else
648
632
#endif
649
633
  {
650
 
    drizzle_con_st *con= 0;
651
 
    drizzle_result_st result;
652
 
    drizzle_return_t ret;
653
 
    if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
 
634
    DRIZZLE *drizzle= 0;
 
635
    if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
654
636
    {
655
637
      free_defaults(argv_to_free);
656
638
      return(1); /* purecov: deadcode */
657
639
    }
658
640
 
659
 
    if (drizzle_query_str(con, &result,
660
 
                          "/*!40101 set @@character_set_database=binary */;",
661
 
                          &ret) == NULL ||
662
 
        ret != DRIZZLE_RETURN_OK)
 
641
    if (drizzle_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
663
642
    {
664
 
      db_error(con, &result, ret, NULL);
665
 
      /* We shall countinue here, if --force was given */
 
643
      db_error(drizzle); /* We shall countinue here, if --force was given */
666
644
      return(1);
667
645
    }
668
646
 
669
 
    drizzle_result_free(&result);
670
 
 
671
647
    if (lock_tables)
672
 
      lock_table(con, argc, argv);
 
648
      lock_table(drizzle, argc, argv);
673
649
    for (; *argv != NULL; argv++)
674
 
      if ((error= write_to_table(*argv, con)))
 
650
      if ((error= write_to_table(*argv, drizzle)))
675
651
        if (exitcode == 0)
676
652
          exitcode= error;
677
 
    db_disconnect(current_host, con);
 
653
    db_disconnect(current_host, drizzle);
678
654
  }
679
655
  free(opt_password);
680
656
  free_defaults(argv_to_free);