~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzlecheck.c

  • Committer: Monty Taylor
  • Date: 2008-07-28 02:08:00 UTC
  • mto: (212.5.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 219.
  • Revision ID: monty@inaugust.com-20080728020800-qx1h6nh8lo3ywzfz
Moved myisam headers to storage/myisam.

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);
185
185
static char *fix_table_name(char *dest, char *src);
186
186
int what_to_do = 0;
187
187
 
188
 
#include <help_start.h>
189
 
 
190
188
static void print_version(void)
191
189
{
192
190
  printf("%s  Ver %s Distrib %s, for %s (%s)\n", my_progname, CHECK_VERSION,
193
191
   MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
194
192
} /* print_version */
195
193
 
196
 
 
197
194
static void usage(void)
198
195
{
199
196
  print_version();
203
200
  puts("This program can be used to CHECK (-c,-m,-C), REPAIR (-r), ANALYZE (-a)");
204
201
  puts("or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be");
205
202
  puts("used at the same time. Not all options are supported by all storage engines.");
206
 
  puts("Please consult the MySQL manual for latest information about the");
 
203
  puts("Please consult the Drizzle manual for latest information about the");
207
204
  puts("above. The options -c,-r,-a and -o are exclusive to each other, which");
208
205
  puts("means that the last option will be used, if several was specified.\n");
209
206
  puts("The option -c will be used by default, if none was specified. You");
214
211
  puts("mysqloptimize: The default option will be -o\n");
215
212
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
216
213
  printf("OR     %s [OPTIONS] --databases DB1 [DB2 DB3...]\n",
217
 
         my_progname);
 
214
   my_progname);
218
215
  printf("OR     %s [OPTIONS] --all-databases\n", my_progname);
219
216
  print_defaults("my", load_default_groups);
220
217
  my_print_help(my_long_options);
221
218
  my_print_variables(my_long_options);
222
219
} /* usage */
223
220
 
224
 
#include <help_end.h>
225
 
 
226
221
static bool
227
222
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
228
 
               char *argument)
 
223
         char *argument)
229
224
{
230
225
  switch(optid) {
231
226
  case 'a':
264
259
      char *start = argument;
265
260
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
266
261
      opt_password = my_strdup(argument, MYF(MY_FAE));
267
 
      while (*argument) *argument++= 'x';               /* Destroy argument */
 
262
      while (*argument) *argument++= 'x';    /* Destroy argument */
268
263
      if (*start)
269
 
        start[1] = 0;                             /* Cut length of argument */
 
264
  start[1] = 0;                             /* Cut length of argument */
270
265
      tty_password= 0;
271
266
    }
272
267
    else
286
281
    verbose++;
287
282
    break;
288
283
  case 'V': print_version(); exit(0);
289
 
  case OPT_MYSQL_PROTOCOL:
 
284
  case OPT_DRIZZLE_PROTOCOL:
290
285
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
291
286
                                    opt->name);
292
287
    break;
328
323
 
329
324
  /* TODO: This variable is not yet used */
330
325
  if (strcmp(default_charset, charset_info->csname) &&
331
 
      !(charset_info= get_charset_by_csname(default_charset, 
332
 
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
326
      !(charset_info= get_charset_by_csname(default_charset,
 
327
                MY_CS_PRIMARY, MYF(MY_WME))))
333
328
      exit(1);
334
329
  if (*argc > 0 && opt_alldbs)
335
330
  {
336
331
    printf("You should give only options, no arguments at all, with option\n");
337
332
    printf("--all-databases. Please see %s --help for more information.\n",
338
 
           my_progname);
 
333
     my_progname);
339
334
    return 1;
340
335
  }
341
336
  if (*argc < 1 && !opt_alldbs)
342
337
  {
343
338
    printf("You forgot to give the arguments! Please see %s --help\n",
344
 
           my_progname);
 
339
     my_progname);
345
340
    printf("for more information.\n");
346
341
    return 1;
347
342
  }
357
352
 
358
353
static int process_all_databases()
359
354
{
360
 
  MYSQL_ROW row;
361
 
  MYSQL_RES *tableres;
 
355
  DRIZZLE_ROW row;
 
356
  DRIZZLE_RES *tableres;
362
357
  int result = 0;
363
358
 
364
 
  if (mysql_query(sock, "SHOW DATABASES") ||
365
 
      !(tableres = mysql_store_result(sock)))
 
359
  if (drizzle_query(sock, "SHOW DATABASES") ||
 
360
      !(tableres = drizzle_store_result(sock)))
366
361
  {
367
362
    my_printf_error(0, "Error: Couldn't execute 'SHOW DATABASES': %s",
368
 
                    MYF(0), mysql_error(sock));
 
363
        MYF(0), drizzle_error(sock));
369
364
    return 1;
370
365
  }
371
 
  while ((row = mysql_fetch_row(tableres)))
 
366
  while ((row = drizzle_fetch_row(tableres)))
372
367
  {
373
368
    if (process_one_db(row[0]))
374
369
      result = 1;
396
391
    return 1;
397
392
  if (opt_all_in_1)
398
393
  {
399
 
    /* 
 
394
    /*
400
395
      We need table list in form `a`, `b`, `c`
401
396
      that's why we need 2 more chars added to to each table name
402
397
      space is for more readable output in logs and in case of error
403
 
    */    
 
398
    */   
404
399
    char *table_names_comma_sep, *end;
405
400
    int i, tot_length = 0;
406
401
 
408
403
      tot_length+= fixed_name_length(*(table_names + i)) + 2;
409
404
 
410
405
    if (!(table_names_comma_sep = (char *)
411
 
          my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
 
406
    my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
412
407
      return 1;
413
408
 
414
409
    for (end = table_names_comma_sep + 1; tables > 0;
415
 
         tables--, table_names++)
 
410
   tables--, table_names++)
416
411
    {
417
412
      end= fix_table_name(end, *table_names);
418
413
      *end++= ',';
432
427
{
433
428
  const char *p;
434
429
  uint extra_length= 2;  /* count the first/last backticks */
435
 
  
 
430
 
436
431
  for (p= name; *p; p++)
437
432
  {
438
433
    if (*p == '`')
469
464
 
470
465
static int process_all_tables_in_db(char *database)
471
466
{
472
 
  MYSQL_RES *res;
473
 
  MYSQL_ROW row;
 
467
  DRIZZLE_RES *res;
 
468
  DRIZZLE_ROW row;
474
469
  uint num_columns;
475
470
 
476
471
  if (use_db(database))
477
472
    return 1;
478
 
  if (mysql_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
479
 
        !((res= mysql_store_result(sock))))
 
473
  if (drizzle_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
 
474
  !((res= drizzle_store_result(sock))))
480
475
    return 1;
481
476
 
482
 
  num_columns= mysql_num_fields(res);
 
477
  num_columns= drizzle_num_fields(res);
483
478
 
484
479
  if (opt_all_in_1)
485
480
  {
492
487
    char *tables, *end;
493
488
    uint tot_length = 0;
494
489
 
495
 
    while ((row = mysql_fetch_row(res)))
 
490
    while ((row = drizzle_fetch_row(res)))
496
491
      tot_length+= fixed_name_length(row[0]) + 2;
497
 
    mysql_data_seek(res, 0);
 
492
    drizzle_data_seek(res, 0);
498
493
 
499
494
    if (!(tables=(char *) my_malloc(sizeof(char)*tot_length+4, MYF(MY_WME))))
500
495
    {
501
 
      mysql_free_result(res);
 
496
      drizzle_free_result(res);
502
497
      return 1;
503
498
    }
504
 
    for (end = tables + 1; (row = mysql_fetch_row(res)) ;)
 
499
    for (end = tables + 1; (row = drizzle_fetch_row(res)) ;)
505
500
    {
506
501
      if ((num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
507
502
        continue;
516
511
  }
517
512
  else
518
513
  {
519
 
    while ((row = mysql_fetch_row(res)))
 
514
    while ((row = drizzle_fetch_row(res)))
520
515
    {
521
516
      /* Skip views if we don't perform renaming. */
522
517
      if ((what_to_do != DO_UPGRADE) && (num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
525
520
      handle_request_for_tables(row[0], fixed_name_length(row[0]));
526
521
    }
527
522
  }
528
 
  mysql_free_result(res);
 
523
  drizzle_free_result(res);
529
524
  return 0;
530
525
} /* process_all_tables_in_db */
531
526
 
538
533
  if (strncmp(name, "#mysql50#", 9))
539
534
    return 1;
540
535
  sprintf(qbuf, "RENAME TABLE `%s` TO `%s`", name, name + 9);
541
 
  if (mysql_query(sock, qbuf))
 
536
  if (drizzle_query(sock, qbuf))
542
537
  {
543
538
    fprintf(stderr, "Failed to %s\n", qbuf);
544
 
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
 
539
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
545
540
    rc= 1;
546
541
  }
547
542
  if (verbose)
556
551
  if (strncmp(name, "#mysql50#", 9))
557
552
    return 1;
558
553
  sprintf(qbuf, "ALTER DATABASE `%s` UPGRADE DATA DIRECTORY NAME", name);
559
 
  if (mysql_query(sock, qbuf))
 
554
  if (drizzle_query(sock, qbuf))
560
555
  {
561
556
    fprintf(stderr, "Failed to %s\n", qbuf);
562
 
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
 
557
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
563
558
    rc= 1;
564
559
  }
565
560
  if (verbose)
586
581
 
587
582
static int use_db(char *database)
588
583
{
589
 
  if (mysql_get_server_version(sock) >= 50003 &&
 
584
  if (drizzle_get_server_version(sock) >= 50003 &&
590
585
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
591
586
    return 1;
592
 
  if (mysql_select_db(sock, database))
 
587
  if (drizzle_select_db(sock, database))
593
588
  {
594
589
    DBerror(sock, "when selecting the database");
595
590
    return 1;
648
643
    ptr= strxmov(ptr, " ", options, NullS);
649
644
    query_length= (uint) (ptr - query);
650
645
  }
651
 
  if (mysql_real_query(sock, query, query_length))
 
646
  if (drizzle_real_query(sock, query, query_length))
652
647
  {
653
648
    sprintf(message, "when executing '%s TABLE ... %s'", op, options);
654
649
    DBerror(sock, message);
662
657
 
663
658
static void print_result()
664
659
{
665
 
  MYSQL_RES *res;
666
 
  MYSQL_ROW row;
 
660
  DRIZZLE_RES *res;
 
661
  DRIZZLE_ROW row;
667
662
  char prev[NAME_LEN*2+2];
668
663
  uint i;
669
664
  bool found_error=0;
670
665
 
671
 
  res = mysql_use_result(sock);
 
666
  res = drizzle_use_result(sock);
672
667
 
673
668
  prev[0] = '\0';
674
 
  for (i = 0; (row = mysql_fetch_row(res)); i++)
 
669
  for (i = 0; (row = drizzle_fetch_row(res)); i++)
675
670
  {
676
671
    int changed = strcmp(prev, row[0]);
677
672
    bool status = !strcmp(row[2], "status");
684
679
        list
685
680
      */
686
681
      if (found_error && opt_auto_repair && what_to_do != DO_REPAIR &&
687
 
          strcmp(row[3],"OK"))
688
 
        insert_dynamic(&tables4repair, (uchar*) prev);
 
682
    strcmp(row[3],"OK"))
 
683
  insert_dynamic(&tables4repair, (uchar*) prev);
689
684
      found_error=0;
690
685
      if (opt_silent)
691
 
        continue;
 
686
  continue;
692
687
    }
693
688
    if (status && changed)
694
689
      printf("%-50s %s", row[0], row[3]);
696
691
    {
697
692
      printf("%s\n%-9s: %s", row[0], row[2], row[3]);
698
693
      if (strcmp(row[2],"note"))
699
 
        found_error=1;
 
694
  found_error=1;
700
695
    }
701
696
    else
702
697
      printf("%-9s: %s", row[2], row[3]);
706
701
  /* add the last table to be repaired to the list */
707
702
  if (found_error && opt_auto_repair && what_to_do != DO_REPAIR)
708
703
    insert_dynamic(&tables4repair, (uchar*) prev);
709
 
  mysql_free_result(res);
 
704
  drizzle_free_result(res);
710
705
}
711
706
 
712
707
 
717
712
  {
718
713
    fprintf(stderr, "# Connecting to %s...\n", host ? host : "localhost");
719
714
  }
720
 
  mysql_init(&mysql_connection);
 
715
  drizzle_create(&drizzle_connection);
721
716
  if (opt_compress)
722
 
    mysql_options(&mysql_connection, MYSQL_OPT_COMPRESS, NullS);
 
717
    drizzle_options(&drizzle_connection, DRIZZLE_OPT_COMPRESS, NullS);
723
718
  if (opt_protocol)
724
 
    mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
725
 
  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,
726
721
         NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
727
722
  {
728
 
    DBerror(&mysql_connection, "when trying to connect");
 
723
    DBerror(&drizzle_connection, "when trying to connect");
729
724
    return 1;
730
725
  }
731
 
  mysql_connection.reconnect= 1;
 
726
  drizzle_connection.reconnect= 1;
732
727
  return 0;
733
728
} /* dbConnect */
734
729
 
737
732
{
738
733
  if (verbose)
739
734
    fprintf(stderr, "# Disconnecting from %s...\n", host ? host : "localhost");
740
 
  mysql_close(sock);
 
735
  drizzle_close(sock);
741
736
} /* dbDisconnect */
742
737
 
743
738
 
744
 
static void DBerror(MYSQL *mysql, const char *when)
 
739
static void DBerror(DRIZZLE *drizzle, const char *when)
745
740
{
746
741
  my_printf_error(0,"Got error: %d: %s %s", MYF(0),
747
 
                  mysql_errno(mysql), mysql_error(mysql), when);
 
742
      drizzle_errno(drizzle), drizzle_error(drizzle), when);
748
743
  safe_exit(EX_MYSQLERR);
749
744
  return;
750
745
} /* DBerror */
757
752
  if (ignore_errors)
758
753
    return;
759
754
  if (sock)
760
 
    mysql_close(sock);
 
755
    drizzle_close(sock);
761
756
  exit(error);
762
757
}
763
758