~mkindahl/drizzle/remove-mem-casts

« back to all changes in this revision

Viewing changes to client/drizzlecheck.c

  • Committer: Brian Aker
  • Date: 2008-07-26 04:51:46 UTC
  • mfrom: (202.1.25 codestyle)
  • Revision ID: brian@tangent.org-20080726045146-ax7ofn8aqnkycjl3
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000 MySQL AB
 
1
/* Copyright (C) 2008 Drizzle development team
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
/* By Jani Tolonen, 2001-04-20, MySQL Development Team */
 
16
/* By Jani Tolonen, 2001-04-20, MySQL, DRIZZLE Development Team */
17
17
 
18
18
#define CHECK_VERSION "2.5.0"
19
19
 
27
27
#define EX_USAGE 1
28
28
#define EX_MYSQLERR 2
29
29
 
30
 
static MYSQL mysql_connection, *sock = 0;
 
30
static DRIZZLE drizzle_connection, *sock = 0;
31
31
static bool opt_alldbs = 0, opt_check_only_changed = 0, opt_extended = 0,
32
32
               opt_compress = 0, opt_databases = 0, opt_fast = 0,
33
33
               opt_medium_check = 0, opt_quick = 0, opt_all_in_1 = 0,
38
38
static uint verbose = 0, opt_mysql_port=0;
39
39
static int my_end_arg;
40
40
static char * opt_mysql_unix_port = 0;
41
 
static char *opt_password = 0, *current_user = 0, 
42
 
            *default_charset = (char *)MYSQL_DEFAULT_CHARSET_NAME,
43
 
            *current_host = 0;
 
41
static char *opt_password = 0, *current_user = 0,
 
42
      *default_charset = (char *)MYSQL_DEFAULT_CHARSET_NAME,
 
43
      *current_host = 0;
44
44
static int first_error = 0;
45
45
DYNAMIC_ARRAY tables4repair;
46
46
static uint opt_protocol=0;
132
132
   (char**) &opt_mysql_port,
133
133
   (char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
134
134
   0},
135
 
  {"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
 
135
  {"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
136
136
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
137
137
  {"quick", 'q',
138
138
   "If you are using this option with CHECK TABLE, it prevents the check from scanning the rows to check for wrong links. This is the fastest check. If you are using this option with REPAIR TABLE, it will try to repair only the index tree. This is the fastest repair method for a table.",
178
178
static int handle_request_for_tables(char *tables, uint length);
179
179
static int dbConnect(char *host, char *user,char *passwd);
180
180
static void dbDisconnect(char *host);
181
 
static void DBerror(MYSQL *mysql, const char *when);
 
181
static void DBerror(DRIZZLE *drizzle, const char *when);
182
182
static void safe_exit(int error);
183
183
static void print_result(void);
184
184
static uint fixed_name_length(const char *name);
200
200
  puts("This program can be used to CHECK (-c,-m,-C), REPAIR (-r), ANALYZE (-a)");
201
201
  puts("or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be");
202
202
  puts("used at the same time. Not all options are supported by all storage engines.");
203
 
  puts("Please consult the MySQL manual for latest information about the");
 
203
  puts("Please consult the Drizzle manual for latest information about the");
204
204
  puts("above. The options -c,-r,-a and -o are exclusive to each other, which");
205
205
  puts("means that the last option will be used, if several was specified.\n");
206
206
  puts("The option -c will be used by default, if none was specified. You");
211
211
  puts("mysqloptimize: The default option will be -o\n");
212
212
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
213
213
  printf("OR     %s [OPTIONS] --databases DB1 [DB2 DB3...]\n",
214
 
         my_progname);
 
214
   my_progname);
215
215
  printf("OR     %s [OPTIONS] --all-databases\n", my_progname);
216
216
  print_defaults("my", load_default_groups);
217
217
  my_print_help(my_long_options);
220
220
 
221
221
static bool
222
222
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
223
 
               char *argument)
 
223
         char *argument)
224
224
{
225
225
  switch(optid) {
226
226
  case 'a':
259
259
      char *start = argument;
260
260
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
261
261
      opt_password = my_strdup(argument, MYF(MY_FAE));
262
 
      while (*argument) *argument++= 'x';               /* Destroy argument */
 
262
      while (*argument) *argument++= 'x';    /* Destroy argument */
263
263
      if (*start)
264
 
        start[1] = 0;                             /* Cut length of argument */
 
264
  start[1] = 0;                             /* Cut length of argument */
265
265
      tty_password= 0;
266
266
    }
267
267
    else
281
281
    verbose++;
282
282
    break;
283
283
  case 'V': print_version(); exit(0);
284
 
  case OPT_MYSQL_PROTOCOL:
 
284
  case OPT_DRIZZLE_PROTOCOL:
285
285
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
286
286
                                    opt->name);
287
287
    break;
323
323
 
324
324
  /* TODO: This variable is not yet used */
325
325
  if (strcmp(default_charset, charset_info->csname) &&
326
 
      !(charset_info= get_charset_by_csname(default_charset, 
327
 
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
326
      !(charset_info= get_charset_by_csname(default_charset,
 
327
                MY_CS_PRIMARY, MYF(MY_WME))))
328
328
      exit(1);
329
329
  if (*argc > 0 && opt_alldbs)
330
330
  {
331
331
    printf("You should give only options, no arguments at all, with option\n");
332
332
    printf("--all-databases. Please see %s --help for more information.\n",
333
 
           my_progname);
 
333
     my_progname);
334
334
    return 1;
335
335
  }
336
336
  if (*argc < 1 && !opt_alldbs)
337
337
  {
338
338
    printf("You forgot to give the arguments! Please see %s --help\n",
339
 
           my_progname);
 
339
     my_progname);
340
340
    printf("for more information.\n");
341
341
    return 1;
342
342
  }
352
352
 
353
353
static int process_all_databases()
354
354
{
355
 
  MYSQL_ROW row;
356
 
  MYSQL_RES *tableres;
 
355
  DRIZZLE_ROW row;
 
356
  DRIZZLE_RES *tableres;
357
357
  int result = 0;
358
358
 
359
 
  if (mysql_query(sock, "SHOW DATABASES") ||
360
 
      !(tableres = mysql_store_result(sock)))
 
359
  if (drizzle_query(sock, "SHOW DATABASES") ||
 
360
      !(tableres = drizzle_store_result(sock)))
361
361
  {
362
362
    my_printf_error(0, "Error: Couldn't execute 'SHOW DATABASES': %s",
363
 
                    MYF(0), mysql_error(sock));
 
363
        MYF(0), drizzle_error(sock));
364
364
    return 1;
365
365
  }
366
 
  while ((row = mysql_fetch_row(tableres)))
 
366
  while ((row = drizzle_fetch_row(tableres)))
367
367
  {
368
368
    if (process_one_db(row[0]))
369
369
      result = 1;
391
391
    return 1;
392
392
  if (opt_all_in_1)
393
393
  {
394
 
    /* 
 
394
    /*
395
395
      We need table list in form `a`, `b`, `c`
396
396
      that's why we need 2 more chars added to to each table name
397
397
      space is for more readable output in logs and in case of error
398
 
    */    
 
398
    */   
399
399
    char *table_names_comma_sep, *end;
400
400
    int i, tot_length = 0;
401
401
 
403
403
      tot_length+= fixed_name_length(*(table_names + i)) + 2;
404
404
 
405
405
    if (!(table_names_comma_sep = (char *)
406
 
          my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
 
406
    my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
407
407
      return 1;
408
408
 
409
409
    for (end = table_names_comma_sep + 1; tables > 0;
410
 
         tables--, table_names++)
 
410
   tables--, table_names++)
411
411
    {
412
412
      end= fix_table_name(end, *table_names);
413
413
      *end++= ',';
427
427
{
428
428
  const char *p;
429
429
  uint extra_length= 2;  /* count the first/last backticks */
430
 
  
 
430
 
431
431
  for (p= name; *p; p++)
432
432
  {
433
433
    if (*p == '`')
464
464
 
465
465
static int process_all_tables_in_db(char *database)
466
466
{
467
 
  MYSQL_RES *res;
468
 
  MYSQL_ROW row;
 
467
  DRIZZLE_RES *res;
 
468
  DRIZZLE_ROW row;
469
469
  uint num_columns;
470
470
 
471
471
  if (use_db(database))
472
472
    return 1;
473
 
  if (mysql_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
474
 
        !((res= mysql_store_result(sock))))
 
473
  if (drizzle_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
 
474
  !((res= drizzle_store_result(sock))))
475
475
    return 1;
476
476
 
477
 
  num_columns= mysql_num_fields(res);
 
477
  num_columns= drizzle_num_fields(res);
478
478
 
479
479
  if (opt_all_in_1)
480
480
  {
487
487
    char *tables, *end;
488
488
    uint tot_length = 0;
489
489
 
490
 
    while ((row = mysql_fetch_row(res)))
 
490
    while ((row = drizzle_fetch_row(res)))
491
491
      tot_length+= fixed_name_length(row[0]) + 2;
492
 
    mysql_data_seek(res, 0);
 
492
    drizzle_data_seek(res, 0);
493
493
 
494
494
    if (!(tables=(char *) my_malloc(sizeof(char)*tot_length+4, MYF(MY_WME))))
495
495
    {
496
 
      mysql_free_result(res);
 
496
      drizzle_free_result(res);
497
497
      return 1;
498
498
    }
499
 
    for (end = tables + 1; (row = mysql_fetch_row(res)) ;)
 
499
    for (end = tables + 1; (row = drizzle_fetch_row(res)) ;)
500
500
    {
501
501
      if ((num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
502
502
        continue;
511
511
  }
512
512
  else
513
513
  {
514
 
    while ((row = mysql_fetch_row(res)))
 
514
    while ((row = drizzle_fetch_row(res)))
515
515
    {
516
516
      /* Skip views if we don't perform renaming. */
517
517
      if ((what_to_do != DO_UPGRADE) && (num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
520
520
      handle_request_for_tables(row[0], fixed_name_length(row[0]));
521
521
    }
522
522
  }
523
 
  mysql_free_result(res);
 
523
  drizzle_free_result(res);
524
524
  return 0;
525
525
} /* process_all_tables_in_db */
526
526
 
533
533
  if (strncmp(name, "#mysql50#", 9))
534
534
    return 1;
535
535
  sprintf(qbuf, "RENAME TABLE `%s` TO `%s`", name, name + 9);
536
 
  if (mysql_query(sock, qbuf))
 
536
  if (drizzle_query(sock, qbuf))
537
537
  {
538
538
    fprintf(stderr, "Failed to %s\n", qbuf);
539
 
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
 
539
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
540
540
    rc= 1;
541
541
  }
542
542
  if (verbose)
551
551
  if (strncmp(name, "#mysql50#", 9))
552
552
    return 1;
553
553
  sprintf(qbuf, "ALTER DATABASE `%s` UPGRADE DATA DIRECTORY NAME", name);
554
 
  if (mysql_query(sock, qbuf))
 
554
  if (drizzle_query(sock, qbuf))
555
555
  {
556
556
    fprintf(stderr, "Failed to %s\n", qbuf);
557
 
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
 
557
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
558
558
    rc= 1;
559
559
  }
560
560
  if (verbose)
581
581
 
582
582
static int use_db(char *database)
583
583
{
584
 
  if (mysql_get_server_version(sock) >= 50003 &&
 
584
  if (drizzle_get_server_version(sock) >= 50003 &&
585
585
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
586
586
    return 1;
587
 
  if (mysql_select_db(sock, database))
 
587
  if (drizzle_select_db(sock, database))
588
588
  {
589
589
    DBerror(sock, "when selecting the database");
590
590
    return 1;
643
643
    ptr= strxmov(ptr, " ", options, NullS);
644
644
    query_length= (uint) (ptr - query);
645
645
  }
646
 
  if (mysql_real_query(sock, query, query_length))
 
646
  if (drizzle_real_query(sock, query, query_length))
647
647
  {
648
648
    sprintf(message, "when executing '%s TABLE ... %s'", op, options);
649
649
    DBerror(sock, message);
657
657
 
658
658
static void print_result()
659
659
{
660
 
  MYSQL_RES *res;
661
 
  MYSQL_ROW row;
 
660
  DRIZZLE_RES *res;
 
661
  DRIZZLE_ROW row;
662
662
  char prev[NAME_LEN*2+2];
663
663
  uint i;
664
664
  bool found_error=0;
665
665
 
666
 
  res = mysql_use_result(sock);
 
666
  res = drizzle_use_result(sock);
667
667
 
668
668
  prev[0] = '\0';
669
 
  for (i = 0; (row = mysql_fetch_row(res)); i++)
 
669
  for (i = 0; (row = drizzle_fetch_row(res)); i++)
670
670
  {
671
671
    int changed = strcmp(prev, row[0]);
672
672
    bool status = !strcmp(row[2], "status");
679
679
        list
680
680
      */
681
681
      if (found_error && opt_auto_repair && what_to_do != DO_REPAIR &&
682
 
          strcmp(row[3],"OK"))
683
 
        insert_dynamic(&tables4repair, (uchar*) prev);
 
682
    strcmp(row[3],"OK"))
 
683
  insert_dynamic(&tables4repair, (uchar*) prev);
684
684
      found_error=0;
685
685
      if (opt_silent)
686
 
        continue;
 
686
  continue;
687
687
    }
688
688
    if (status && changed)
689
689
      printf("%-50s %s", row[0], row[3]);
691
691
    {
692
692
      printf("%s\n%-9s: %s", row[0], row[2], row[3]);
693
693
      if (strcmp(row[2],"note"))
694
 
        found_error=1;
 
694
  found_error=1;
695
695
    }
696
696
    else
697
697
      printf("%-9s: %s", row[2], row[3]);
701
701
  /* add the last table to be repaired to the list */
702
702
  if (found_error && opt_auto_repair && what_to_do != DO_REPAIR)
703
703
    insert_dynamic(&tables4repair, (uchar*) prev);
704
 
  mysql_free_result(res);
 
704
  drizzle_free_result(res);
705
705
}
706
706
 
707
707
 
712
712
  {
713
713
    fprintf(stderr, "# Connecting to %s...\n", host ? host : "localhost");
714
714
  }
715
 
  mysql_init(&mysql_connection);
 
715
  drizzle_create(&drizzle_connection);
716
716
  if (opt_compress)
717
 
    mysql_options(&mysql_connection, MYSQL_OPT_COMPRESS, NullS);
 
717
    drizzle_options(&drizzle_connection, DRIZZLE_OPT_COMPRESS, NullS);
718
718
  if (opt_protocol)
719
 
    mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
720
 
  if (!(sock = mysql_real_connect(&mysql_connection, host, user, passwd,
 
719
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
720
  if (!(sock = drizzle_connect(&drizzle_connection, host, user, passwd,
721
721
         NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
722
722
  {
723
 
    DBerror(&mysql_connection, "when trying to connect");
 
723
    DBerror(&drizzle_connection, "when trying to connect");
724
724
    return 1;
725
725
  }
726
 
  mysql_connection.reconnect= 1;
 
726
  drizzle_connection.reconnect= 1;
727
727
  return 0;
728
728
} /* dbConnect */
729
729
 
732
732
{
733
733
  if (verbose)
734
734
    fprintf(stderr, "# Disconnecting from %s...\n", host ? host : "localhost");
735
 
  mysql_close(sock);
 
735
  drizzle_close(sock);
736
736
} /* dbDisconnect */
737
737
 
738
738
 
739
 
static void DBerror(MYSQL *mysql, const char *when)
 
739
static void DBerror(DRIZZLE *drizzle, const char *when)
740
740
{
741
741
  my_printf_error(0,"Got error: %d: %s %s", MYF(0),
742
 
                  mysql_errno(mysql), mysql_error(mysql), when);
 
742
      drizzle_errno(drizzle), drizzle_error(drizzle), when);
743
743
  safe_exit(EX_MYSQLERR);
744
744
  return;
745
745
} /* DBerror */
752
752
  if (ignore_errors)
753
753
    return;
754
754
  if (sock)
755
 
    mysql_close(sock);
 
755
    drizzle_close(sock);
756
756
  exit(error);
757
757
}
758
758