mysqlcheck.c 22.4 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* By Jani Tolonen, 2001-04-20, MySQL Development Team */

19
#define CHECK_VERSION "2.4.4"
20

unknown's avatar
unknown committed
21
#include "client_priv.h"
22
#include <m_ctype.h>
23 24 25
#include <mysql_version.h>
#include <mysqld_error.h>
#include <sslopt-vars.h>
26 27 28 29 30 31 32 33 34 35

/* Exit codes */

#define EX_USAGE 1
#define EX_MYSQLERR 2

static MYSQL mysql_connection, *sock = 0;
static my_bool opt_alldbs = 0, opt_check_only_changed = 0, opt_extended = 0,
               opt_compress = 0, opt_databases = 0, opt_fast = 0,
               opt_medium_check = 0, opt_quick = 0, opt_all_in_1 = 0,
36
               opt_silent = 0, opt_auto_repair = 0, ignore_errors = 0,
unknown's avatar
unknown committed
37
               tty_password = 0, opt_frm = 0;
38 39
static uint verbose = 0, opt_mysql_port=0;
static my_string opt_mysql_unix_port = 0;
40
static char *opt_password = 0, *current_user = 0, 
41 42
	    *default_charset = (char *)MYSQL_DEFAULT_CHARSET_NAME,
	    *current_host = 0;
43
static int first_error = 0;
unknown's avatar
unknown committed
44
DYNAMIC_ARRAY tables4repair;
45 46 47 48
#ifdef HAVE_SMEM
static char *shared_memory_base_name=0;
#endif
static uint opt_protocol=0;
49
static CHARSET_INFO *charset_info= &my_charset_latin1;
50 51 52

enum operations {DO_CHECK, DO_REPAIR, DO_ANALYZE, DO_OPTIMIZE};

53
static struct my_option my_long_options[] =
54
{
55 56 57 58 59 60 61
  {"all-databases", 'A',
   "Check all the databases. This will be same as  --databases with all databases selected.",
   (gptr*) &opt_alldbs, (gptr*) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
   0, 0},
  {"analyze", 'a', "Analyze given tables.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
   0, 0, 0, 0},
  {"all-in-1", '1',
unknown's avatar
unknown committed
62
   "Instead of issuing one query for each table, use one query per database, naming all tables in the database in a comma-separated list.",
63 64
   (gptr*) &opt_all_in_1, (gptr*) &opt_all_in_1, 0, GET_BOOL, NO_ARG, 0, 0, 0,
   0, 0, 0},
unknown's avatar
unknown committed
65 66 67 68
#ifdef __NETWARE__
  {"auto-close", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
69 70 71 72 73
  {"auto-repair", OPT_AUTO_REPAIR,
   "If a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked, if corrupted ones were found.",
   (gptr*) &opt_auto_repair, (gptr*) &opt_auto_repair, 0, GET_BOOL, NO_ARG, 0,
   0, 0, 0, 0, 0},
  {"character-sets-dir", OPT_CHARSETS_DIR,
74
   "Directory where character sets are.", (gptr*) &charsets_dir,
75
   (gptr*) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
76
  {"check", 'c', "Check table for errors.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
77 78 79 80 81 82 83 84 85 86 87
   0, 0, 0, 0},
  {"check-only-changed", 'C',
   "Check only tables that have changed since last check or haven't been closed properly.",
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"compress", OPT_COMPRESS, "Use compression in server/client protocol.",
   (gptr*) &opt_compress, (gptr*) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
   0, 0, 0},
  {"databases", 'B',
   "To check several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames.",
   (gptr*) &opt_databases, (gptr*) &opt_databases, 0, GET_BOOL, NO_ARG,
   0, 0, 0, 0, 0, 0},
88 89 90 91
#ifdef DBUG_OFF
  {"debug", '#', "This is a non-debug version. Catch this and exit.",
   0, 0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
#else
92
  {"debug", '#', "Output debug log. Often this is 'd:t:o,filename'.",
93
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
94
#endif
95
  {"default-character-set", OPT_DEFAULT_CHARSET,
96
   "Set the default character set.", (gptr*) &default_charset,
97
   (gptr*) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
98
  {"fast",'F', "Check only tables that haven't been closed properly.",
99 100 101 102 103 104
   (gptr*) &opt_fast, (gptr*) &opt_fast, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
   0},
  {"force", 'f', "Continue even if we get an sql-error.",
   (gptr*) &ignore_errors, (gptr*) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
   0, 0, 0, 0},
  {"extended", 'e',
unknown's avatar
unknown committed
105
   "If you are using this option with CHECK TABLE, it will ensure that the table is 100 percent consistent, but will take a long time. If you are using this option with REPAIR TABLE, it will force using old slow repair with keycache method, instead of much faster repair by sorting.",
106 107 108 109 110
   (gptr*) &opt_extended, (gptr*) &opt_extended, 0, GET_BOOL, NO_ARG, 0, 0, 0,
   0, 0, 0},
  {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
   NO_ARG, 0, 0, 0, 0, 0, 0},
  {"host",'h', "Connect to host.", (gptr*) &current_host,
unknown's avatar
unknown committed
111
   (gptr*) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
112 113 114 115 116 117 118 119
  {"medium-check", 'm',
   "Faster than extended-check, but only finds 99.99 percent of all errors. Should be good enough for most cases.",
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"optimize", 'o', "Optimize table.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0,
   0, 0},
  {"password", 'p',
   "Password to use when connecting to server. If password is not given it's solicited on the tty.",
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
120
#ifdef __WIN__
121 122
  {"pipe", 'W', "Use named pipes to connect to server.", 0, 0, 0, GET_NO_ARG,
   NO_ARG, 0, 0, 0, 0, 0, 0},
123
#endif
124 125 126
  {"port", 'P', "Port number to use for connection.", (gptr*) &opt_mysql_port,
   (gptr*) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, MYSQL_PORT, 0, 0, 0, 0,
   0},
127
  {"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
128
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
129 130 131 132 133 134 135
  {"quick", 'q',
   "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.",
   (gptr*) &opt_quick, (gptr*) &opt_quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
   0},
  {"repair", 'r',
   "Can fix almost anything except unique keys that aren't unique.",
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
136
#ifdef HAVE_SMEM
137
  {"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
138
   "Base name of shared memory.", (gptr*) &shared_memory_base_name, (gptr*) &shared_memory_base_name,
139 140
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
#endif
141 142 143 144 145
  {"silent", 's', "Print only error messages.", (gptr*) &opt_silent,
   (gptr*) &opt_silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"socket", 'S', "Socket file to use for connection.",
   (gptr*) &opt_mysql_unix_port, (gptr*) &opt_mysql_unix_port, 0, GET_STR,
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
146
#include <sslopt-longopts.h>
147 148
  {"tables", OPT_TABLES, "Overrides option --databases (-B).", 0, 0, 0,
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
149
#ifndef DONT_ALLOW_USER_CHANGE
150 151
  {"user", 'u', "User for login if not current user.", (gptr*) &current_user,
   (gptr*) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
152
#endif
unknown's avatar
unknown committed
153 154 155 156
  {"use-frm", OPT_FRM,
   "When used with REPAIR, get table structure from .frm file, so the table can be repaired even if .MYI header is corrupted.",
   (gptr*) &opt_frm, (gptr*) &opt_frm, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
   0},
157 158 159 160 161
  {"verbose", 'v', "Print info about the various stages.", 0, 0, 0, GET_NO_ARG,
   NO_ARG, 0, 0, 0, 0, 0, 0},
  {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
   NO_ARG, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
162
};
unknown's avatar
unknown committed
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static const char *load_default_groups[] = { "mysqlcheck", "client", 0 };


static void print_version(void);
static void usage(void);
static int get_options(int *argc, char ***argv);
static int process_all_databases();
static int process_databases(char **db_names);
static int process_selected_tables(char *db, char **table_names, int tables);
static int process_all_tables_in_db(char *database);
static int use_db(char *database);
static int handle_request_for_tables(char *tables, uint length);
static int dbConnect(char *host, char *user,char *passwd);
static void dbDisconnect(char *host);
static void DBerror(MYSQL *mysql, const char *when);
static void safe_exit(int error);
static void print_result();
unknown's avatar
unknown committed
181
static char *fix_table_name(char *dest, char *src);
182 183
int what_to_do = 0;

unknown's avatar
unknown committed
184 185
#include <help_start.h>

186 187 188 189
static void print_version(void)
{
  printf("%s  Ver %s Distrib %s, for %s (%s)\n", my_progname, CHECK_VERSION,
   MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
unknown's avatar
unknown committed
190
  NETWARE_SET_SCREEN_MODE(1);
191 192 193 194 195 196 197
} /* print_version */


static void usage(void)
{
  print_version();
  puts("By Jani Tolonen, 2001-04-20, MySQL Development Team\n");
198 199
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n");
  puts("and you are welcome to modify and redistribute it under the GPL license.\n");
200 201
  puts("This program can be used to CHECK (-c,-m,-C), REPAIR (-r), ANALYZE (-a)");
  puts("or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be");
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");
unknown's avatar
unknown committed
204 205 206 207 208 209 210 211
  puts("above. The options -c,-r,-a and -o are exclusive to each other, which");
  puts("means that the last option will be used, if several was specified.\n");
  puts("The option -c will be used by default, if none was specified. You");
  puts("can change the default behavior by making a symbolic link, or");
  puts("copying this file somewhere with another name, the alternatives are:");
  puts("mysqlrepair:   The default option will be -r");
  puts("mysqlanalyze:  The default option will be -a");
  puts("mysqloptimize: The default option will be -o\n");
212 213 214 215 216
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
  printf("OR     %s [OPTIONS] --databases DB1 [DB2 DB3...]\n",
	 my_progname);
  printf("OR     %s [OPTIONS] --all-databases\n", my_progname);
  print_defaults("my", load_default_groups);
217 218
  my_print_help(my_long_options);
  my_print_variables(my_long_options);
219 220
} /* usage */

unknown's avatar
unknown committed
221
#include <help_end.h>
unknown's avatar
unknown committed
222

223 224 225 226 227
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
{
  switch(optid) {
unknown's avatar
unknown committed
228 229 230 231 232
#ifdef __NETWARE__
  case OPT_AUTO_CLOSE:
    setscreenmode(SCR_AUTOCLOSE_ON_EXIT);
    break;
#endif
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  case 'a':
    what_to_do = DO_ANALYZE;
    break;
  case 'c':
    what_to_do = DO_CHECK;
    break;
  case 'C':
    what_to_do = DO_CHECK;
    opt_check_only_changed = 1;
    break;
  case 'I': /* Fall through */
  case '?':
    usage();
    exit(0);
  case 'm':
    what_to_do = DO_CHECK;
    opt_medium_check = 1;
    break;
  case 'o':
    what_to_do = DO_OPTIMIZE;
    break;
  case 'p':
    if (argument)
    {
      char *start = argument;
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
      opt_password = my_strdup(argument, MYF(MY_FAE));
      while (*argument) *argument++= 'x';		/* Destroy argument */
      if (*start)
unknown's avatar
unknown committed
262
	start[1] = 0;                             /* Cut length of argument */
263
      tty_password= 0;
264 265 266 267 268 269 270 271 272
    }
    else
      tty_password = 1;
    break;
  case 'r':
    what_to_do = DO_REPAIR;
    break;
  case 'W':
#ifdef __WIN__
273
    opt_protocol = MYSQL_PROTOCOL_PIPE;
274 275 276 277 278
#endif
    break;
  case '#':
    DBUG_PUSH(argument ? argument : "d:t:o");
    break;
279
#include <sslopt-case.h>
280 281 282 283 284 285 286
  case OPT_TABLES:
    opt_databases = 0;
    break;
  case 'v':
    verbose++;
    break;
  case 'V': print_version(); exit(0);
287 288
  case OPT_MYSQL_PROTOCOL:
  {
289
    if ((opt_protocol= find_type(argument, &sql_protocol_typelib,0)) <= 0)
290 291 292 293 294 295
    {
      fprintf(stderr, "Unknown option to protocol: %s\n", argument);
      exit(1);
    }
    break;
  }
296 297 298 299 300
  }
  return 0;
}


301 302
static int get_options(int *argc, char ***argv)
{
303
  int ho_error;
304

unknown's avatar
unknown committed
305 306 307 308 309 310
  if (*argc == 1)
  {
    usage();
    exit(0);
  }

311
  load_defaults("my", load_default_groups, argc, argv);
312

313
  if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
314
    exit(ho_error);
315

316
  if (!what_to_do)
unknown's avatar
unknown committed
317 318 319
  {
    int pnlen = strlen(my_progname);

320
    if (pnlen < 6) /* name too short */
unknown's avatar
unknown committed
321 322 323 324 325 326 327 328 329 330
      what_to_do = DO_CHECK;
    else if (!strcmp("repair", my_progname + pnlen - 6))
      what_to_do = DO_REPAIR;
    else if (!strcmp("analyze", my_progname + pnlen - 7))
      what_to_do = DO_ANALYZE;
    else if  (!strcmp("optimize", my_progname + pnlen - 8))
      what_to_do = DO_OPTIMIZE;
    else
      what_to_do = DO_CHECK;
  }
unknown's avatar
unknown committed
331 332

  /* TODO: This variable is not yet used */
333 334
  if (strcmp(default_charset, charset_info->csname) &&
      !(charset_info= get_charset_by_csname(default_charset, 
335
  					    MY_CS_PRIMARY, MYF(MY_WME))))
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
      exit(1);
  if (*argc > 0 && opt_alldbs)
  {
    printf("You should give only options, no arguments at all, with option\n");
    printf("--all-databases. Please see %s --help for more information.\n",
	   my_progname);
    return 1;
  }
  if (*argc < 1 && !opt_alldbs)
  {
    printf("You forgot to give the arguments! Please see %s --help\n",
	   my_progname);
    printf("for more information.\n");
    return 1;
  }
  if (tty_password)
    opt_password = get_tty_password(NullS);
  return(0);
} /* get_options */


static int process_all_databases()
{
  MYSQL_ROW row;
  MYSQL_RES *tableres;
  int result = 0;

  if (mysql_query(sock, "SHOW DATABASES") ||
      !(tableres = mysql_store_result(sock)))
  {
    my_printf_error(0, "Error: Couldn't execute 'SHOW DATABASES': %s",
		    MYF(0), mysql_error(sock));
    return 1;
  }
  while ((row = mysql_fetch_row(tableres)))
  {
    if (process_all_tables_in_db(row[0]))
      result = 1;
  }
  return result;
}
/* process_all_databases */


static int process_databases(char **db_names)
{
  int result = 0;
  for ( ; *db_names ; db_names++)
  {
    if (process_all_tables_in_db(*db_names))
      result = 1;
  }
  return result;
} /* process_databases */


static int process_selected_tables(char *db, char **table_names, int tables)
{
  if (use_db(db))
    return 1;
  if (opt_all_in_1)
  {
398 399 400 401 402
    /* 
      We need table list in form `a`, `b`, `c`
      that's why we need 4 more chars added to to each table name
      space is for more readable output in logs and in case of error
    */	  
403 404 405 406
    char *table_names_comma_sep, *end;
    int i, tot_length = 0;

    for (i = 0; i < tables; i++)
407
      tot_length += strlen(*(table_names + i)) + 4;
unknown's avatar
unknown committed
408

409
    if (!(table_names_comma_sep = (char *)
410
	  my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
411 412 413 414 415
      return 1;

    for (end = table_names_comma_sep + 1; tables > 0;
	 tables--, table_names++)
    {
unknown's avatar
unknown committed
416 417
      end= fix_table_name(end, *table_names);
      *end++= ',';
418 419 420 421 422 423 424 425 426 427 428 429
    }
    *--end = 0;
    handle_request_for_tables(table_names_comma_sep + 1, tot_length - 1);
    my_free(table_names_comma_sep, MYF(0));
  }
  else
    for (; tables > 0; tables--, table_names++)
      handle_request_for_tables(*table_names, strlen(*table_names));
  return 0;
} /* process_selected_tables */


unknown's avatar
unknown committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
static char *fix_table_name(char *dest, char *src)
{
  char *db_sep;

  *dest++= '`';
  if ((db_sep= strchr(src, '.')))
  {
    dest= strmake(dest, src, (uint) (db_sep - src));
    dest= strmov(dest, "`.`");
    src= db_sep + 1;
  }
  dest= strxmov(dest, src, "`", NullS);
  return dest;
}


446 447 448 449 450 451 452 453
static int process_all_tables_in_db(char *database)
{
  MYSQL_RES *res;
  MYSQL_ROW row;

  LINT_INIT(res);
  if (use_db(database))
    return 1;
454 455
  if (mysql_query(sock, "SHOW TABLES") ||
	!((res= mysql_store_result(sock))))
456
    return 1;
unknown's avatar
unknown committed
457

458 459
  if (opt_all_in_1)
  {
460
    /*
461 462 463 464
      We need table list in form `a`, `b`, `c`
      that's why we need 4 more chars added to to each table name
      space is for more readable output in logs and in case of error
     */
465

466 467 468 469
    char *tables, *end;
    uint tot_length = 0;

    while ((row = mysql_fetch_row(res)))
470
      tot_length += strlen(row[0]) + 4;
471
    mysql_data_seek(res, 0);
unknown's avatar
unknown committed
472

473
    if (!(tables=(char *) my_malloc(sizeof(char)*tot_length+4, MYF(MY_WME))))
474 475 476 477 478 479
    {
      mysql_free_result(res);
      return 1;
    }
    for (end = tables + 1; (row = mysql_fetch_row(res)) ;)
    {
unknown's avatar
unknown committed
480 481
      end= fix_table_name(end, row[0]);
      *end++= ',';
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    }
    *--end = 0;
    if (tot_length)
      handle_request_for_tables(tables + 1, tot_length - 1);
    my_free(tables, MYF(0));
  }
  else
  {
    while ((row = mysql_fetch_row(res)))
      handle_request_for_tables(row[0], strlen(row[0]));
  }
  mysql_free_result(res);
  return 0;
} /* process_all_tables_in_db */


static int use_db(char *database)
{
500 501 502
  if (mysql_get_server_version(sock) >= 50003 &&
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
    return 1;
503 504 505 506 507 508 509 510 511 512 513
  if (mysql_select_db(sock, database))
  {
    DBerror(sock, "when selecting the database");
    return 1;
  }
  return 0;
} /* use_db */


static int handle_request_for_tables(char *tables, uint length)
{
514
  char *query, *end, options[100], message[100];
unknown's avatar
unknown committed
515
  uint query_length= 0;
516 517 518
  const char *op = 0;

  options[0] = 0;
519
  end = options;
520 521 522
  switch (what_to_do) {
  case DO_CHECK:
    op = "CHECK";
523 524 525 526 527
    if (opt_quick)              end = strmov(end, " QUICK");
    if (opt_fast)               end = strmov(end, " FAST");
    if (opt_medium_check)       end = strmov(end, " MEDIUM"); /* Default */
    if (opt_extended)           end = strmov(end, " EXTENDED");
    if (opt_check_only_changed) end = strmov(end, " CHANGED");
528 529 530
    break;
  case DO_REPAIR:
    op = "REPAIR";
531 532
    if (opt_quick)              end = strmov(end, " QUICK");
    if (opt_extended)           end = strmov(end, " EXTENDED");
unknown's avatar
unknown committed
533
    if (opt_frm)                end = strmov(end, " USE_FRM");
534 535 536 537 538 539 540 541 542 543 544
    break;
  case DO_ANALYZE:
    op = "ANALYZE";
    break;
  case DO_OPTIMIZE:
    op = "OPTIMIZE";
    break;
  }

  if (!(query =(char *) my_malloc((sizeof(char)*(length+110)), MYF(MY_WME))))
    return 1;
545
  if (opt_all_in_1)
unknown's avatar
unknown committed
546
  {
547
    /* No backticks here as we added them before */
unknown's avatar
unknown committed
548 549 550
    query_length= my_sprintf(query,
			     (query, "%s TABLE %s %s", op, tables, options));
  }
551
  else
unknown's avatar
unknown committed
552 553 554 555 556 557 558 559 560
  {
    char *ptr;

    ptr= strmov(strmov(query, op), " TABLE ");
    ptr= fix_table_name(ptr, tables);
    ptr= strxmov(ptr, " ", options, NullS);
    query_length= (uint) (ptr - query);
  }
  if (mysql_real_query(sock, query, query_length))
561
  {
562
    sprintf(message, "when executing '%s TABLE ... %s'", op, options);
563
    DBerror(sock, message);
564 565 566 567 568 569 570 571 572 573 574 575
    return 1;
  }
  print_result();
  my_free(query, MYF(0));
  return 0;
}


static void print_result()
{
  MYSQL_RES *res;
  MYSQL_ROW row;
unknown's avatar
unknown committed
576
  char prev[NAME_LEN*2+2];
577 578
  uint i;
  my_bool found_error=0;
579 580

  res = mysql_use_result(sock);
581

582 583 584
  prev[0] = '\0';
  for (i = 0; (row = mysql_fetch_row(res)); i++)
  {
585
    int changed = strcmp(prev, row[0]);
586 587 588 589
    my_bool status = !strcmp(row[2], "status");

    if (status)
    {
590 591 592 593 594
      /*
        if there was an error with the table, we have --auto-repair set,
        and this isn't a repair op, then add the table to the tables4repair
        list
      */
595
      if (found_error && opt_auto_repair && what_to_do != DO_REPAIR &&
596
	  strcmp(row[3],"OK"))
597
	insert_dynamic(&tables4repair, prev);
598 599 600 601
      found_error=0;
      if (opt_silent)
	continue;
    }
602 603 604
    if (status && changed)
      printf("%-50s %s", row[0], row[3]);
    else if (!status && changed)
unknown's avatar
unknown committed
605
    {
606
      printf("%s\n%-9s: %s", row[0], row[2], row[3]);
607 608
      if (strcmp(row[2],"note"))
	found_error=1;
unknown's avatar
unknown committed
609
    }
610 611 612
    else
      printf("%-9s: %s", row[2], row[3]);
    strmov(prev, row[0]);
613
    putchar('\n');
614
  }
615 616
  /* add the last table to be repaired to the list */
  if (found_error && opt_auto_repair && what_to_do != DO_REPAIR)
617
    insert_dynamic(&tables4repair, prev);
618 619 620 621
  mysql_free_result(res);
}


622
static int dbConnect(char *host, char *user, char *passwd)
623 624 625 626 627 628 629 630 631 632 633 634
{
  DBUG_ENTER("dbConnect");
  if (verbose)
  {
    fprintf(stderr, "# Connecting to %s...\n", host ? host : "localhost");
  }
  mysql_init(&mysql_connection);
  if (opt_compress)
    mysql_options(&mysql_connection, MYSQL_OPT_COMPRESS, NullS);
#ifdef HAVE_OPENSSL
  if (opt_use_ssl)
    mysql_ssl_set(&mysql_connection, opt_ssl_key, opt_ssl_cert, opt_ssl_ca,
635
		  opt_ssl_capath, opt_ssl_cipher);
636 637 638 639 640 641
#endif
  if (opt_protocol)
    mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
#ifdef HAVE_SMEM
  if (shared_memory_base_name)
    mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
642 643 644 645 646 647 648
#endif
  if (!(sock = mysql_real_connect(&mysql_connection, host, user, passwd,
         NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
  {
    DBerror(&mysql_connection, "when trying to connect");
    return 1;
  }
649
  mysql_connection.reconnect= 1;
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  return 0;
} /* dbConnect */


static void dbDisconnect(char *host)
{
  if (verbose)
    fprintf(stderr, "# Disconnecting from %s...\n", host ? host : "localhost");
  mysql_close(sock);
} /* dbDisconnect */


static void DBerror(MYSQL *mysql, const char *when)
{
  DBUG_ENTER("DBerror");
  my_printf_error(0,"Got error: %d: %s %s", MYF(0),
		  mysql_errno(mysql), mysql_error(mysql), when);
  safe_exit(EX_MYSQLERR);
  DBUG_VOID_RETURN;
} /* DBerror */


static void safe_exit(int error)
{
  if (!first_error)
    first_error= error;
  if (ignore_errors)
    return;
  if (sock)
    mysql_close(sock);
  exit(error);
}


int main(int argc, char **argv)
{
  MY_INIT(argv[0]);
  /*
  ** Check out the args
  */
  if (get_options(&argc, &argv))
  {
    my_end(0);
    exit(EX_USAGE);
  }
  if (dbConnect(current_host, current_user, opt_password))
    exit(EX_MYSQLERR);

698 699
  if (opt_auto_repair &&
      my_init_dynamic_array(&tables4repair, sizeof(char)*(NAME_LEN*2+2),16,64))
unknown's avatar
unknown committed
700 701 702 703 704
  {
    first_error = 1;
    goto end;
  }

705 706 707 708 709 710 711 712
  if (opt_alldbs)
    process_all_databases();
  /* Only one database and selected table(s) */
  else if (argc > 1 && !opt_databases)
    process_selected_tables(*argv, (argv + 1), (argc - 1));
  /* One or more databases, all tables */
  else
    process_databases(argv);
unknown's avatar
unknown committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726
  if (opt_auto_repair)
  {
    uint i;

    if (!opt_silent && tables4repair.elements)
      puts("\nRepairing tables");
    what_to_do = DO_REPAIR;
    for (i = 0; i < tables4repair.elements ; i++)
    {
      char *name= (char*) dynamic_array_ptr(&tables4repair, i);
      handle_request_for_tables(name, strlen(name));
    }
  }
 end:
727
  dbDisconnect(current_host);
unknown's avatar
unknown committed
728 729
  if (opt_auto_repair)
    delete_dynamic(&tables4repair);
730
  my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
731 732 733
#ifdef HAVE_SMEM
  my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
#endif
734
  my_end(0);
unknown's avatar
unknown committed
735
  return(first_error!=0);
736
} /* main */