~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleimport.cc

  • Committer: Brian Aker
  • Date: 2009-03-30 18:13:06 UTC
  • mfrom: (968.1.1 lib-merge)
  • Revision ID: brian@tangent.org-20090330181306-hzodbge1b0v57puh
Merge of Eric's libdrizzle work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
pthread_mutex_t counter_mutex;
42
42
pthread_cond_t count_threshhold;
43
43
 
44
 
static void db_error_with_table(DRIZZLE *drizzle, char *table);
45
 
static void db_error(DRIZZLE *drizzle);
 
44
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
 
45
                     drizzle_return_t ret, char *table);
46
46
static char *field_escape(char *to,const char *from,uint32_t length);
47
47
static char *add_load_option(char *ptr,const char *object,
48
48
           const char *statement);
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 uint32_t opt_protocol= 0;
62
61
static uint32_t opt_drizzle_port= 0;
63
62
static char * opt_drizzle_unix_port= 0;
64
63
static int64_t opt_ignore_lines= -1;
159
158
static void print_version(void)
160
159
{
161
160
  printf("%s  Ver %s Distrib %s, for %s (%s)\n" ,my_progname,
162
 
    IMPORT_VERSION, drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
161
    IMPORT_VERSION, drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
163
162
}
164
163
 
165
164
 
281
280
  current_db= *((*argv)++);
282
281
  (*argc)--;
283
282
  if (tty_password)
284
 
    opt_password=drizzleclient_get_tty_password(NULL);
 
283
    opt_password=client_get_tty_password(NULL);
285
284
  return(0);
286
285
}
287
286
 
288
287
 
289
288
 
290
 
static int write_to_table(char *filename, DRIZZLE *drizzle)
 
289
static int write_to_table(char *filename, drizzle_con_st *con)
291
290
{
292
291
  char tablename[FN_REFLEN], hard_path[FN_REFLEN],
293
292
       sql_statement[FN_REFLEN*16+256], *end;
 
293
  drizzle_result_st result;
 
294
  drizzle_return_t ret;
294
295
 
295
296
  fn_format(tablename, filename, "", "", 1 | 2); /* removes path & ext. */
296
297
  if (!opt_local_file)
307
308
#else
308
309
    sprintf(sql_statement, "DELETE FROM %s", tablename);
309
310
#endif
310
 
    if (drizzleclient_query(drizzle, sql_statement))
 
311
    if (drizzle_query_str(con, &result, sql_statement, &ret) == NULL ||
 
312
        ret != DRIZZLE_RETURN_OK)
311
313
    {
312
 
      db_error_with_table(drizzle, tablename);
 
314
      db_error(con, &result, ret, tablename);
313
315
      return(1);
314
316
    }
 
317
    drizzle_result_free(&result);
315
318
  }
316
319
  if (verbose)
317
320
  {
355
358
  }
356
359
  *end= '\0';
357
360
 
358
 
  if (drizzleclient_query(drizzle, sql_statement))
 
361
  if (drizzle_query_str(con, &result, sql_statement, &ret) == NULL ||
 
362
      ret != DRIZZLE_RETURN_OK)
359
363
  {
360
 
    db_error_with_table(drizzle, tablename);
 
364
    db_error(con, &result, ret, tablename);
361
365
    return(1);
362
366
  }
363
367
  if (!silent)
364
368
  {
365
 
    if (drizzleclient_info(drizzle)) /* If NULL-pointer, print nothing */
 
369
    if (strcmp(drizzle_result_info(&result), ""))
366
370
    {
367
371
      fprintf(stdout, "%s.%s: %s\n", current_db, tablename,
368
 
        drizzleclient_info(drizzle));
 
372
        drizzle_result_info(&result));
369
373
    }
370
374
  }
 
375
  drizzle_result_free(&result);
371
376
  return(0);
372
377
}
373
378
 
374
379
 
375
 
 
376
 
static void lock_table(DRIZZLE *drizzle, int tablecount, char **raw_tablename)
 
380
static void lock_table(drizzle_con_st *con, int tablecount, char **raw_tablename)
377
381
{
378
382
  string query;
379
383
  int i;
380
384
  char tablename[FN_REFLEN];
 
385
  drizzle_result_st result;
 
386
  drizzle_return_t ret;
381
387
 
382
388
  if (verbose)
383
389
    fprintf(stdout, "Locking tables for write\n");
388
394
    query.append(tablename);
389
395
    query.append(" WRITE,");
390
396
  }
391
 
  if (drizzleclient_real_query(drizzle, query.c_str(), query.length()-1))
392
 
    db_error(drizzle); /* We shall countinue here, if --force was given */
 
397
  if (drizzle_query(con, &result, query.c_str(), query.length()-1,
 
398
                    &ret) == NULL ||
 
399
      ret != DRIZZLE_RETURN_OK)
 
400
  {
 
401
    db_error(con, &result, ret, NULL);
 
402
    /* We shall countinue here, if --force was given */
 
403
    return;
 
404
  }
 
405
  drizzle_result_free(&result);
393
406
}
394
407
 
395
408
 
396
 
 
397
 
 
398
 
static DRIZZLE *db_connect(char *host, char *database,
399
 
                         char *user, char *passwd)
 
409
static drizzle_con_st *db_connect(char *host, char *database,
 
410
                                  char *user, char *passwd)
400
411
{
401
 
  DRIZZLE *drizzle;
 
412
  drizzle_st *drizzle;
 
413
  drizzle_con_st *con;
 
414
  drizzle_return_t ret;
 
415
 
402
416
  if (verbose)
403
417
    fprintf(stdout, "Connecting to %s\n", host ? host : "localhost");
404
 
  if (!(drizzle= drizzleclient_create(NULL)))
405
 
    return 0;
406
 
  if (opt_compress)
407
 
    drizzleclient_options(drizzle,DRIZZLE_OPT_COMPRESS,NULL);
408
 
  if (opt_protocol)
409
 
    drizzleclient_options(drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
410
 
  if (!(drizzleclient_connect(drizzle,host,user,passwd,
411
 
                           database,opt_drizzle_port,opt_drizzle_unix_port,
412
 
                           0)))
 
418
  if (!(drizzle= drizzle_create(NULL)))
 
419
    return 0;
 
420
  if (!(con= drizzle_con_add_tcp(drizzle,NULL,host,opt_drizzle_port,user,passwd,
 
421
                                 database, DRIZZLE_CON_NONE)))
 
422
  {
 
423
    return 0;
 
424
  }
 
425
 
 
426
  if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
413
427
  {
414
428
    ignore_errors=0;    /* NO RETURN FROM db_error */
415
 
    db_error(drizzle);
 
429
    db_error(con, NULL, ret, NULL);
416
430
  }
417
 
  drizzle->reconnect= 0;
 
431
 
418
432
  if (verbose)
419
433
    fprintf(stdout, "Selecting database %s\n", database);
420
 
  if (drizzleclient_select_db(drizzle, database))
421
 
  {
422
 
    ignore_errors=0;
423
 
    db_error(drizzle);
424
 
  }
425
 
  return drizzle;
 
434
 
 
435
  return con;
426
436
}
427
437
 
428
438
 
429
439
 
430
 
static void db_disconnect(char *host, DRIZZLE *drizzle)
 
440
static void db_disconnect(char *host, drizzle_con_st *con)
431
441
{
432
442
  if (verbose)
433
443
    fprintf(stdout, "Disconnecting from %s\n", host ? host : "localhost");
434
 
  drizzleclient_close(drizzle);
 
444
  drizzle_free(drizzle_con_drizzle(con));
435
445
}
436
446
 
437
447
 
438
448
 
439
 
static void safe_exit(int error, DRIZZLE *drizzle)
 
449
static void safe_exit(int error, drizzle_con_st *con)
440
450
{
441
451
  if (ignore_errors)
442
452
    return;
443
 
  if (drizzle)
444
 
    drizzleclient_close(drizzle);
 
453
  if (con)
 
454
    drizzle_free(drizzle_con_drizzle(con));
445
455
  exit(error);
446
456
}
447
457
 
448
458
 
449
459
 
450
 
static void db_error_with_table(DRIZZLE *drizzle, char *table)
451
 
{
452
 
  my_printf_error(0,"Error: %d, %s, when using table: %s",
453
 
      MYF(0), drizzleclient_errno(drizzle), drizzleclient_error(drizzle), table);
454
 
  safe_exit(1, drizzle);
455
 
}
456
 
 
457
 
 
458
 
 
459
 
static void db_error(DRIZZLE *drizzle)
460
 
{
461
 
  my_printf_error(0,"Error: %d %s", MYF(0), drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
462
 
  safe_exit(1, drizzle);
 
460
static void db_error(drizzle_con_st *con, drizzle_result_st *result,
 
461
                     drizzle_return_t ret, char *table)
 
462
{
 
463
  if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
464
  {
 
465
    my_printf_error(0,"Error: %d, %s%s%s", MYF(0),
 
466
                    drizzle_result_error_code(result),
 
467
                    drizzle_result_error(result),
 
468
                    table ? ", when using table: " : "", table ? table : "");
 
469
    drizzle_result_free(result);
 
470
  }
 
471
  else
 
472
  {
 
473
    my_printf_error(0,"Error: %d, %s%s%s", MYF(0), ret, drizzle_con_error(con),
 
474
                    table ? ", when using table: " : "", table ? table : "");
 
475
  }
 
476
 
 
477
  safe_exit(1, con);
463
478
}
464
479
 
465
480
 
519
534
{
520
535
  int error;
521
536
  char *raw_table_name= (char *)arg;
522
 
  DRIZZLE *drizzle= 0;
 
537
  drizzle_con_st *con= NULL;
 
538
  drizzle_result_st result;
 
539
  drizzle_return_t ret;
523
540
 
524
 
  if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
 
541
  if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
525
542
  {
526
543
    goto error;
527
544
  }
528
545
 
529
 
  if (drizzleclient_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
 
546
  if (drizzle_query_str(con, &result,
 
547
                        "/*!40101 set @@character_set_database=binary */;",
 
548
                        &ret) == NULL ||
 
549
      ret != DRIZZLE_RETURN_OK)
530
550
  {
531
 
    db_error(drizzle); /* We shall countinue here, if --force was given */
 
551
    db_error(con, &result, ret, NULL);
 
552
    /* We shall countinue here, if --force was given */
532
553
    goto error;
533
554
  }
534
555
 
535
556
  /*
536
557
    We are not currently catching the error here.
537
558
  */
538
 
  if((error= write_to_table(raw_table_name, drizzle)))
 
559
  if((error= write_to_table(raw_table_name, con)))
539
560
    if (exitcode == 0)
540
561
      exitcode= error;
541
562
 
542
563
error:
543
 
  if (drizzle)
544
 
    db_disconnect(current_host, drizzle);
 
564
  if (con)
 
565
    db_disconnect(current_host, con);
545
566
 
546
567
  pthread_mutex_lock(&counter_mutex);
547
568
  counter--;
624
645
  else
625
646
#endif
626
647
  {
627
 
    DRIZZLE *drizzle= 0;
628
 
    if (!(drizzle= db_connect(current_host,current_db,current_user,opt_password)))
 
648
    drizzle_con_st *con= 0;
 
649
    drizzle_result_st result;
 
650
    drizzle_return_t ret;
 
651
    if (!(con= db_connect(current_host,current_db,current_user,opt_password)))
629
652
    {
630
653
      free_defaults(argv_to_free);
631
654
      return(1); /* purecov: deadcode */
632
655
    }
633
656
 
634
 
    if (drizzleclient_query(drizzle, "/*!40101 set @@character_set_database=binary */;"))
 
657
    if (drizzle_query_str(con, &result,
 
658
                          "/*!40101 set @@character_set_database=binary */;",
 
659
                          &ret) == NULL ||
 
660
        ret != DRIZZLE_RETURN_OK)
635
661
    {
636
 
      db_error(drizzle); /* We shall countinue here, if --force was given */
 
662
      db_error(con, &result, ret, NULL);
 
663
      /* We shall countinue here, if --force was given */
637
664
      return(1);
638
665
    }
639
666
 
 
667
    drizzle_result_free(&result);
 
668
 
640
669
    if (lock_tables)
641
 
      lock_table(drizzle, argc, argv);
 
670
      lock_table(con, argc, argv);
642
671
    for (; *argv != NULL; argv++)
643
 
      if ((error= write_to_table(*argv, drizzle)))
 
672
      if ((error= write_to_table(*argv, con)))
644
673
        if (exitcode == 0)
645
674
          exitcode= error;
646
 
    db_disconnect(current_host, drizzle);
 
675
    db_disconnect(current_host, con);
647
676
  }
648
677
  free(opt_password);
649
678
  free_defaults(argv_to_free);