~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqlcheck.c

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 Drizzle development team
 
1
/* Copyright (C) 2000 MySQL AB
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, MYSQL Development Team */
 
16
/* By Jani Tolonen, 2001-04-20, MySQL Development Team */
17
17
 
18
18
#define CHECK_VERSION "2.5.0"
19
19
 
20
 
#include "config.h"
21
 
#include <vector>
22
 
#include <string>
23
20
#include "client_priv.h"
24
 
#include <mystrings/m_ctype.h>
25
 
 
26
 
template class std::vector<std::string>;
27
 
 
28
 
using namespace std;
 
21
#include <m_ctype.h>
 
22
#include <mysql_version.h>
 
23
#include <mysqld_error.h>
 
24
 
29
25
/* Exit codes */
30
26
 
31
27
#define EX_USAGE 1
32
28
#define EX_MYSQLERR 2
33
29
 
34
 
static DRIZZLE drizzle_connection, *sock = 0;
35
 
static bool opt_alldbs = 0, opt_check_only_changed = 0, opt_extended = 0,
 
30
static MYSQL mysql_connection, *sock = 0;
 
31
static my_bool opt_alldbs = 0, opt_check_only_changed = 0, opt_extended = 0,
36
32
               opt_compress = 0, opt_databases = 0, opt_fast = 0,
37
33
               opt_medium_check = 0, opt_quick = 0, opt_all_in_1 = 0,
38
34
               opt_silent = 0, opt_auto_repair = 0, ignore_errors = 0,
42
38
static uint verbose = 0, opt_mysql_port=0;
43
39
static int my_end_arg;
44
40
static char * opt_mysql_unix_port = 0;
45
 
static char *opt_password = 0, *current_user = 0,
46
 
      *default_charset = (char *)DRIZZLE_DEFAULT_CHARSET_NAME,
47
 
      *current_host = 0;
 
41
static char *opt_password = 0, *current_user = 0, 
 
42
            *default_charset = (char *)MYSQL_DEFAULT_CHARSET_NAME,
 
43
            *current_host = 0;
48
44
static int first_error = 0;
49
 
vector<string> tables4repair;
 
45
DYNAMIC_ARRAY tables4repair;
 
46
#ifdef HAVE_SMEM
 
47
static char *shared_memory_base_name=0;
 
48
#endif
50
49
static uint opt_protocol=0;
51
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
50
static CHARSET_INFO *charset_info= &my_charset_latin1;
52
51
 
53
52
enum operations { DO_CHECK, DO_REPAIR, DO_ANALYZE, DO_OPTIMIZE, DO_UPGRADE };
54
53
 
56
55
{
57
56
  {"all-databases", 'A',
58
57
   "Check all the databases. This will be same as  --databases with all databases selected.",
59
 
   (char**) &opt_alldbs, (char**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
58
   (uchar**) &opt_alldbs, (uchar**) &opt_alldbs, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
60
59
   0, 0},
61
60
  {"analyze", 'a', "Analyze given tables.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
62
61
   0, 0, 0, 0},
63
62
  {"all-in-1", '1',
64
63
   "Instead of issuing one query for each table, use one query per database, naming all tables in the database in a comma-separated list.",
65
 
   (char**) &opt_all_in_1, (char**) &opt_all_in_1, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
64
   (uchar**) &opt_all_in_1, (uchar**) &opt_all_in_1, 0, GET_BOOL, NO_ARG, 0, 0, 0,
66
65
   0, 0, 0},
67
66
  {"auto-repair", OPT_AUTO_REPAIR,
68
67
   "If a checked table is corrupted, automatically fix it. Repairing will be done after all tables have been checked, if corrupted ones were found.",
69
 
   (char**) &opt_auto_repair, (char**) &opt_auto_repair, 0, GET_BOOL, NO_ARG, 0,
 
68
   (uchar**) &opt_auto_repair, (uchar**) &opt_auto_repair, 0, GET_BOOL, NO_ARG, 0,
70
69
   0, 0, 0, 0, 0},
71
70
  {"character-sets-dir", OPT_CHARSETS_DIR,
72
 
   "Directory where character sets are.", (char**) &charsets_dir,
73
 
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
71
   "Directory where character sets are.", (uchar**) &charsets_dir,
 
72
   (uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
74
73
  {"check", 'c', "Check table for errors.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0,
75
74
   0, 0, 0, 0},
76
75
  {"check-only-changed", 'C',
80
79
   "Check tables for version-dependent changes. May be used with --auto-repair to correct tables requiring version-dependent updates.",
81
80
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
82
81
  {"compress", OPT_COMPRESS, "Use compression in server/client protocol.",
83
 
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
82
   (uchar**) &opt_compress, (uchar**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
84
83
   0, 0, 0},
85
84
  {"databases", 'B',
86
85
   "To check several databases. Note the difference in usage; In this case no tables are given. All name arguments are regarded as databasenames.",
87
 
   (char**) &opt_databases, (char**) &opt_databases, 0, GET_BOOL, NO_ARG,
 
86
   (uchar**) &opt_databases, (uchar**) &opt_databases, 0, GET_BOOL, NO_ARG,
88
87
   0, 0, 0, 0, 0, 0},
 
88
#ifdef DBUG_OFF
 
89
  {"debug", '#', "This is a non-debug version. Catch this and exit.",
 
90
   0, 0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
91
#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
89
95
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
90
 
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
96
   (uchar**) &debug_check_flag, (uchar**) &debug_check_flag, 0,
91
97
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
92
98
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
93
 
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
99
   (uchar**) &debug_info_flag, (uchar**) &debug_info_flag,
94
100
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
95
101
  {"default-character-set", OPT_DEFAULT_CHARSET,
96
 
   "Set the default character set.", (char**) &default_charset,
97
 
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
102
   "Set the default character set.", (uchar**) &default_charset,
 
103
   (uchar**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
98
104
  {"fast",'F', "Check only tables that haven't been closed properly.",
99
 
   (char**) &opt_fast, (char**) &opt_fast, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
105
   (uchar**) &opt_fast, (uchar**) &opt_fast, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
100
106
   0},
101
107
  {"fix-db-names", OPT_FIX_DB_NAMES, "Fix database names.",
102
 
    (char**) &opt_fix_db_names, (char**) &opt_fix_db_names,
 
108
    (uchar**) &opt_fix_db_names, (uchar**) &opt_fix_db_names,
103
109
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
104
110
  {"fix-table-names", OPT_FIX_TABLE_NAMES, "Fix table names.",
105
 
    (char**) &opt_fix_table_names, (char**) &opt_fix_table_names,
 
111
    (uchar**) &opt_fix_table_names, (uchar**) &opt_fix_table_names,
106
112
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
107
113
  {"force", 'f', "Continue even if we get an sql-error.",
108
 
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
 
114
   (uchar**) &ignore_errors, (uchar**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
109
115
   0, 0, 0, 0},
110
116
  {"extended", 'e',
111
117
   "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.",
112
 
   (char**) &opt_extended, (char**) &opt_extended, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
118
   (uchar**) &opt_extended, (uchar**) &opt_extended, 0, GET_BOOL, NO_ARG, 0, 0, 0,
113
119
   0, 0, 0},
114
120
  {"help", '?', "Display this help message and exit.", 0, 0, 0, GET_NO_ARG,
115
121
   NO_ARG, 0, 0, 0, 0, 0, 0},
116
 
  {"host",'h', "Connect to host.", (char**) &current_host,
117
 
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
122
  {"host",'h', "Connect to host.", (uchar**) &current_host,
 
123
   (uchar**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
118
124
  {"medium-check", 'm',
119
125
   "Faster than extended-check, but only finds 99.99 percent of all errors. Should be good enough for most cases.",
120
126
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
121
127
  {"write-binlog", OPT_WRITE_BINLOG,
122
128
   "Log ANALYZE, OPTIMIZE and REPAIR TABLE commands. Use --skip-write-binlog when commands should not be sent to replication slaves.",
123
 
   (char**) &opt_write_binlog, (char**) &opt_write_binlog, 0, GET_BOOL, NO_ARG,
 
129
   (uchar**) &opt_write_binlog, (uchar**) &opt_write_binlog, 0, GET_BOOL, NO_ARG,
124
130
   1, 0, 0, 0, 0, 0},
125
131
  {"optimize", 'o', "Optimize table.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0,
126
132
   0, 0},
128
134
   "Password to use when connecting to server. If password is not given it's solicited on the tty.",
129
135
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
130
136
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
131
 
   "order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
132
 
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
133
 
   (char**) &opt_mysql_port,
134
 
   (char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
 
137
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
138
#if MYSQL_PORT_DEFAULT == 0
 
139
   "/etc/services, "
 
140
#endif
 
141
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
 
142
   (uchar**) &opt_mysql_port,
 
143
   (uchar**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,
135
144
   0},
136
 
  {"protocol", OPT_DRIZZLE_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
 
145
  {"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
137
146
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
138
147
  {"quick", 'q',
139
148
   "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.",
140
 
   (char**) &opt_quick, (char**) &opt_quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
149
   (uchar**) &opt_quick, (uchar**) &opt_quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
141
150
   0},
142
151
  {"repair", 'r',
143
152
   "Can fix almost anything except unique keys that aren't unique.",
144
153
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
145
 
  {"silent", 's', "Print only error messages.", (char**) &opt_silent,
146
 
   (char**) &opt_silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
154
#ifdef HAVE_SMEM
 
155
  {"shared-memory-base-name", OPT_SHARED_MEMORY_BASE_NAME,
 
156
   "Base name of shared memory.", (uchar**) &shared_memory_base_name, (uchar**) &shared_memory_base_name,
 
157
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
158
#endif
 
159
  {"silent", 's', "Print only error messages.", (uchar**) &opt_silent,
 
160
   (uchar**) &opt_silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
147
161
  {"socket", 'S', "Socket file to use for connection.",
148
 
   (char**) &opt_mysql_unix_port, (char**) &opt_mysql_unix_port, 0, GET_STR,
 
162
   (uchar**) &opt_mysql_unix_port, (uchar**) &opt_mysql_unix_port, 0, GET_STR,
149
163
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
150
164
  {"tables", OPT_TABLES, "Overrides option --databases (-B).", 0, 0, 0,
151
165
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
152
166
  {"use-frm", OPT_FRM,
153
167
   "When used with REPAIR, get table structure from .frm file, so the table can be repaired even if .MYI header is corrupted.",
154
 
   (char**) &opt_frm, (char**) &opt_frm, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
168
   (uchar**) &opt_frm, (uchar**) &opt_frm, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
155
169
   0},
156
170
#ifndef DONT_ALLOW_USER_CHANGE
157
 
  {"user", 'u', "User for login if not current user.", (char**) &current_user,
158
 
   (char**) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
171
  {"user", 'u', "User for login if not current user.", (uchar**) &current_user,
 
172
   (uchar**) &current_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
159
173
#endif
160
174
  {"verbose", 'v', "Print info about the various stages.", 0, 0, 0, GET_NO_ARG,
161
175
   NO_ARG, 0, 0, 0, 0, 0, 0},
176
190
static int process_all_tables_in_db(char *database);
177
191
static int process_one_db(char *database);
178
192
static int use_db(char *database);
179
 
static int handle_request_for_tables(const char *tables, uint length);
 
193
static int handle_request_for_tables(char *tables, uint length);
180
194
static int dbConnect(char *host, char *user,char *passwd);
181
195
static void dbDisconnect(char *host);
182
 
static void DBerror(DRIZZLE *drizzle, const char *when);
 
196
static void DBerror(MYSQL *mysql, const char *when);
183
197
static void safe_exit(int error);
184
198
static void print_result(void);
185
199
static uint fixed_name_length(const char *name);
186
 
static char *fix_table_name(char *dest, const char *src);
 
200
static char *fix_table_name(char *dest, char *src);
187
201
int what_to_do = 0;
188
202
 
 
203
#include <help_start.h>
 
204
 
189
205
static void print_version(void)
190
206
{
191
207
  printf("%s  Ver %s Distrib %s, for %s (%s)\n", my_progname, CHECK_VERSION,
192
 
   drizzle_get_client_info(), SYSTEM_TYPE, MACHINE_TYPE);
 
208
   MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
193
209
} /* print_version */
194
210
 
 
211
 
195
212
static void usage(void)
196
213
{
197
214
  print_version();
201
218
  puts("This program can be used to CHECK (-c,-m,-C), REPAIR (-r), ANALYZE (-a)");
202
219
  puts("or OPTIMIZE (-o) tables. Some of the options (like -e or -q) can be");
203
220
  puts("used at the same time. Not all options are supported by all storage engines.");
204
 
  puts("Please consult the Drizzle manual for latest information about the");
 
221
  puts("Please consult the MySQL manual for latest information about the");
205
222
  puts("above. The options -c,-r,-a and -o are exclusive to each other, which");
206
223
  puts("means that the last option will be used, if several was specified.\n");
207
224
  puts("The option -c will be used by default, if none was specified. You");
212
229
  puts("mysqloptimize: The default option will be -o\n");
213
230
  printf("Usage: %s [OPTIONS] database [tables]\n", my_progname);
214
231
  printf("OR     %s [OPTIONS] --databases DB1 [DB2 DB3...]\n",
215
 
   my_progname);
 
232
         my_progname);
216
233
  printf("OR     %s [OPTIONS] --all-databases\n", my_progname);
217
234
  print_defaults("my", load_default_groups);
218
235
  my_print_help(my_long_options);
219
236
  my_print_variables(my_long_options);
220
237
} /* usage */
221
238
 
222
 
static bool
 
239
#include <help_end.h>
 
240
 
 
241
static my_bool
223
242
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
224
 
         char *argument)
 
243
               char *argument)
225
244
{
226
245
  switch(optid) {
227
246
  case 'a':
258
277
    if (argument)
259
278
    {
260
279
      char *start = argument;
261
 
      free(opt_password);
 
280
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
262
281
      opt_password = my_strdup(argument, MYF(MY_FAE));
263
 
      while (*argument) *argument++= 'x';    /* Destroy argument */
 
282
      while (*argument) *argument++= 'x';               /* Destroy argument */
264
283
      if (*start)
265
 
  start[1] = 0;                             /* Cut length of argument */
 
284
        start[1] = 0;                             /* Cut length of argument */
266
285
      tty_password= 0;
267
286
    }
268
287
    else
275
294
    what_to_do= DO_CHECK;
276
295
    opt_upgrade= 1;
277
296
    break;
 
297
  case '#':
 
298
    DBUG_PUSH(argument ? argument : "d:t:o");
 
299
    debug_check_flag= 1;
 
300
    break;
278
301
  case OPT_TABLES:
279
302
    opt_databases = 0;
280
303
    break;
282
305
    verbose++;
283
306
    break;
284
307
  case 'V': print_version(); exit(0);
285
 
  case OPT_DRIZZLE_PROTOCOL:
 
308
  case OPT_MYSQL_PROTOCOL:
 
309
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
 
310
                                    opt->name);
286
311
    break;
287
312
  }
288
313
  return 0;
322
347
 
323
348
  /* TODO: This variable is not yet used */
324
349
  if (strcmp(default_charset, charset_info->csname) &&
325
 
      !(charset_info= get_charset_by_csname(default_charset,
326
 
                MY_CS_PRIMARY, MYF(MY_WME))))
 
350
      !(charset_info= get_charset_by_csname(default_charset, 
 
351
                                            MY_CS_PRIMARY, MYF(MY_WME))))
327
352
      exit(1);
328
353
  if (*argc > 0 && opt_alldbs)
329
354
  {
330
355
    printf("You should give only options, no arguments at all, with option\n");
331
356
    printf("--all-databases. Please see %s --help for more information.\n",
332
 
     my_progname);
 
357
           my_progname);
333
358
    return 1;
334
359
  }
335
360
  if (*argc < 1 && !opt_alldbs)
336
361
  {
337
362
    printf("You forgot to give the arguments! Please see %s --help\n",
338
 
     my_progname);
 
363
           my_progname);
339
364
    printf("for more information.\n");
340
365
    return 1;
341
366
  }
342
367
  if (tty_password)
343
 
    opt_password = get_tty_password(NULL);
 
368
    opt_password = get_tty_password(NullS);
344
369
  if (debug_info_flag)
345
370
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
346
371
  if (debug_check_flag)
351
376
 
352
377
static int process_all_databases()
353
378
{
354
 
  DRIZZLE_ROW row;
355
 
  DRIZZLE_RES *tableres;
 
379
  MYSQL_ROW row;
 
380
  MYSQL_RES *tableres;
356
381
  int result = 0;
357
382
 
358
 
  if (drizzle_query(sock, "SHOW DATABASES") ||
359
 
      !(tableres = drizzle_store_result(sock)))
 
383
  if (mysql_query(sock, "SHOW DATABASES") ||
 
384
      !(tableres = mysql_store_result(sock)))
360
385
  {
361
386
    my_printf_error(0, "Error: Couldn't execute 'SHOW DATABASES': %s",
362
 
        MYF(0), drizzle_error(sock));
 
387
                    MYF(0), mysql_error(sock));
363
388
    return 1;
364
389
  }
365
 
  while ((row = drizzle_fetch_row(tableres)))
 
390
  while ((row = mysql_fetch_row(tableres)))
366
391
  {
367
392
    if (process_one_db(row[0]))
368
393
      result = 1;
390
415
    return 1;
391
416
  if (opt_all_in_1)
392
417
  {
393
 
    /*
 
418
    /* 
394
419
      We need table list in form `a`, `b`, `c`
395
420
      that's why we need 2 more chars added to to each table name
396
421
      space is for more readable output in logs and in case of error
397
 
    */   
 
422
    */    
398
423
    char *table_names_comma_sep, *end;
399
424
    int i, tot_length = 0;
400
425
 
402
427
      tot_length+= fixed_name_length(*(table_names + i)) + 2;
403
428
 
404
429
    if (!(table_names_comma_sep = (char *)
405
 
    my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
 
430
          my_malloc((sizeof(char) * tot_length) + 4, MYF(MY_WME))))
406
431
      return 1;
407
432
 
408
433
    for (end = table_names_comma_sep + 1; tables > 0;
409
 
   tables--, table_names++)
 
434
         tables--, table_names++)
410
435
    {
411
436
      end= fix_table_name(end, *table_names);
412
437
      *end++= ',';
413
438
    }
414
439
    *--end = 0;
415
440
    handle_request_for_tables(table_names_comma_sep + 1, tot_length - 1);
416
 
    free(table_names_comma_sep);
 
441
    my_free(table_names_comma_sep, MYF(0));
417
442
  }
418
443
  else
419
444
    for (; tables > 0; tables--, table_names++)
426
451
{
427
452
  const char *p;
428
453
  uint extra_length= 2;  /* count the first/last backticks */
429
 
 
 
454
  
430
455
  for (p= name; *p; p++)
431
456
  {
432
457
    if (*p == '`')
438
463
}
439
464
 
440
465
 
441
 
static char *fix_table_name(char *dest, const char *src)
 
466
static char *fix_table_name(char *dest, char *src)
442
467
{
443
468
  *dest++= '`';
444
469
  for (; *src; src++)
463
488
 
464
489
static int process_all_tables_in_db(char *database)
465
490
{
466
 
  DRIZZLE_RES *res;
467
 
  DRIZZLE_ROW row;
 
491
  MYSQL_RES *res;
 
492
  MYSQL_ROW row;
468
493
  uint num_columns;
469
494
 
470
495
  if (use_db(database))
471
496
    return 1;
472
 
  if (drizzle_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
473
 
  !((res= drizzle_store_result(sock))))
 
497
  if (mysql_query(sock, "SHOW /*!50002 FULL*/ TABLES") ||
 
498
        !((res= mysql_store_result(sock))))
474
499
    return 1;
475
500
 
476
 
  num_columns= drizzle_num_fields(res);
 
501
  num_columns= mysql_num_fields(res);
477
502
 
478
503
  if (opt_all_in_1)
479
504
  {
486
511
    char *tables, *end;
487
512
    uint tot_length = 0;
488
513
 
489
 
    while ((row = drizzle_fetch_row(res)))
 
514
    while ((row = mysql_fetch_row(res)))
490
515
      tot_length+= fixed_name_length(row[0]) + 2;
491
 
    drizzle_data_seek(res, 0);
 
516
    mysql_data_seek(res, 0);
492
517
 
493
518
    if (!(tables=(char *) my_malloc(sizeof(char)*tot_length+4, MYF(MY_WME))))
494
519
    {
495
 
      drizzle_free_result(res);
 
520
      mysql_free_result(res);
496
521
      return 1;
497
522
    }
498
 
    for (end = tables + 1; (row = drizzle_fetch_row(res)) ;)
 
523
    for (end = tables + 1; (row = mysql_fetch_row(res)) ;)
499
524
    {
500
525
      if ((num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
501
526
        continue;
506
531
    *--end = 0;
507
532
    if (tot_length)
508
533
      handle_request_for_tables(tables + 1, tot_length - 1);
509
 
    free(tables);
 
534
    my_free(tables, MYF(0));
510
535
  }
511
536
  else
512
537
  {
513
 
    while ((row = drizzle_fetch_row(res)))
 
538
    while ((row = mysql_fetch_row(res)))
514
539
    {
515
540
      /* Skip views if we don't perform renaming. */
516
541
      if ((what_to_do != DO_UPGRADE) && (num_columns == 2) && (strcmp(row[1], "VIEW") == 0))
519
544
      handle_request_for_tables(row[0], fixed_name_length(row[0]));
520
545
    }
521
546
  }
522
 
  drizzle_free_result(res);
 
547
  mysql_free_result(res);
523
548
  return 0;
524
549
} /* process_all_tables_in_db */
525
550
 
532
557
  if (strncmp(name, "#mysql50#", 9))
533
558
    return 1;
534
559
  sprintf(qbuf, "RENAME TABLE `%s` TO `%s`", name, name + 9);
535
 
  if (drizzle_query(sock, qbuf))
 
560
  if (mysql_query(sock, qbuf))
536
561
  {
537
562
    fprintf(stderr, "Failed to %s\n", qbuf);
538
 
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
 
563
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
539
564
    rc= 1;
540
565
  }
541
566
  if (verbose)
550
575
  if (strncmp(name, "#mysql50#", 9))
551
576
    return 1;
552
577
  sprintf(qbuf, "ALTER DATABASE `%s` UPGRADE DATA DIRECTORY NAME", name);
553
 
  if (drizzle_query(sock, qbuf))
 
578
  if (mysql_query(sock, qbuf))
554
579
  {
555
580
    fprintf(stderr, "Failed to %s\n", qbuf);
556
 
    fprintf(stderr, "Error: %s\n", drizzle_error(sock));
 
581
    fprintf(stderr, "Error: %s\n", mysql_error(sock));
557
582
    rc= 1;
558
583
  }
559
584
  if (verbose)
580
605
 
581
606
static int use_db(char *database)
582
607
{
583
 
  if (drizzle_get_server_version(sock) >= 50003 &&
584
 
      !my_strcasecmp(&my_charset_utf8_general_ci, database, "information_schema"))
 
608
  if (mysql_get_server_version(sock) >= 50003 &&
 
609
      !my_strcasecmp(&my_charset_latin1, database, "information_schema"))
585
610
    return 1;
586
 
  if (drizzle_select_db(sock, database))
 
611
  if (mysql_select_db(sock, database))
587
612
  {
588
613
    DBerror(sock, "when selecting the database");
589
614
    return 1;
592
617
} /* use_db */
593
618
 
594
619
 
595
 
static int handle_request_for_tables(const char *tables, uint length)
 
620
static int handle_request_for_tables(char *tables, uint length)
596
621
{
597
622
  char *query, *end, options[100], message[100];
598
623
  uint query_length= 0;
603
628
  switch (what_to_do) {
604
629
  case DO_CHECK:
605
630
    op = "CHECK";
606
 
    if (opt_quick)              end = my_stpcpy(end, " QUICK");
607
 
    if (opt_fast)               end = my_stpcpy(end, " FAST");
608
 
    if (opt_medium_check)       end = my_stpcpy(end, " MEDIUM"); /* Default */
609
 
    if (opt_extended)           end = my_stpcpy(end, " EXTENDED");
610
 
    if (opt_check_only_changed) end = my_stpcpy(end, " CHANGED");
611
 
    if (opt_upgrade)            end = my_stpcpy(end, " FOR UPGRADE");
 
631
    if (opt_quick)              end = strmov(end, " QUICK");
 
632
    if (opt_fast)               end = strmov(end, " FAST");
 
633
    if (opt_medium_check)       end = strmov(end, " MEDIUM"); /* Default */
 
634
    if (opt_extended)           end = strmov(end, " EXTENDED");
 
635
    if (opt_check_only_changed) end = strmov(end, " CHANGED");
 
636
    if (opt_upgrade)            end = strmov(end, " FOR UPGRADE");
612
637
    break;
613
638
  case DO_REPAIR:
614
639
    op= (opt_write_binlog) ? "REPAIR" : "REPAIR NO_WRITE_TO_BINLOG";
615
 
    if (opt_quick)              end = my_stpcpy(end, " QUICK");
616
 
    if (opt_extended)           end = my_stpcpy(end, " EXTENDED");
617
 
    if (opt_frm)                end = my_stpcpy(end, " USE_FRM");
 
640
    if (opt_quick)              end = strmov(end, " QUICK");
 
641
    if (opt_extended)           end = strmov(end, " EXTENDED");
 
642
    if (opt_frm)                end = strmov(end, " USE_FRM");
618
643
    break;
619
644
  case DO_ANALYZE:
620
645
    op= (opt_write_binlog) ? "ANALYZE" : "ANALYZE NO_WRITE_TO_BINLOG";
631
656
  if (opt_all_in_1)
632
657
  {
633
658
    /* No backticks here as we added them before */
634
 
    query_length= sprintf(query, "%s TABLE %s %s", op, tables, options);
 
659
    query_length= my_sprintf(query,
 
660
                             (query, "%s TABLE %s %s", op, tables, options));
635
661
  }
636
662
  else
637
663
  {
638
664
    char *ptr;
639
665
 
640
 
    ptr= my_stpcpy(my_stpcpy(query, op), " TABLE ");
 
666
    ptr= strmov(strmov(query, op), " TABLE ");
641
667
    ptr= fix_table_name(ptr, tables);
642
 
    ptr= strxmov(ptr, " ", options, NULL);
 
668
    ptr= strxmov(ptr, " ", options, NullS);
643
669
    query_length= (uint) (ptr - query);
644
670
  }
645
 
  if (drizzle_real_query(sock, query, query_length))
 
671
  if (mysql_real_query(sock, query, query_length))
646
672
  {
647
673
    sprintf(message, "when executing '%s TABLE ... %s'", op, options);
648
674
    DBerror(sock, message);
649
675
    return 1;
650
676
  }
651
677
  print_result();
652
 
  free(query);
 
678
  my_free(query, MYF(0));
653
679
  return 0;
654
680
}
655
681
 
656
682
 
657
683
static void print_result()
658
684
{
659
 
  DRIZZLE_RES *res;
660
 
  DRIZZLE_ROW row;
 
685
  MYSQL_RES *res;
 
686
  MYSQL_ROW row;
661
687
  char prev[NAME_LEN*2+2];
662
688
  uint i;
663
 
  bool found_error=0;
 
689
  my_bool found_error=0;
664
690
 
665
 
  res = drizzle_use_result(sock);
 
691
  res = mysql_use_result(sock);
666
692
 
667
693
  prev[0] = '\0';
668
 
  for (i = 0; (row = drizzle_fetch_row(res)); i++)
 
694
  for (i = 0; (row = mysql_fetch_row(res)); i++)
669
695
  {
670
696
    int changed = strcmp(prev, row[0]);
671
 
    bool status = !strcmp(row[2], "status");
 
697
    my_bool status = !strcmp(row[2], "status");
672
698
 
673
699
    if (status)
674
700
    {
678
704
        list
679
705
      */
680
706
      if (found_error && opt_auto_repair && what_to_do != DO_REPAIR &&
681
 
          strcmp(row[3],"OK"))
682
 
        tables4repair.push_back(string(prev));
 
707
          strcmp(row[3],"OK"))
 
708
        insert_dynamic(&tables4repair, (uchar*) prev);
683
709
      found_error=0;
684
710
      if (opt_silent)
685
 
  continue;
 
711
        continue;
686
712
    }
687
713
    if (status && changed)
688
714
      printf("%-50s %s", row[0], row[3]);
690
716
    {
691
717
      printf("%s\n%-9s: %s", row[0], row[2], row[3]);
692
718
      if (strcmp(row[2],"note"))
693
 
  found_error=1;
 
719
        found_error=1;
694
720
    }
695
721
    else
696
722
      printf("%-9s: %s", row[2], row[3]);
697
 
    my_stpcpy(prev, row[0]);
 
723
    strmov(prev, row[0]);
698
724
    putchar('\n');
699
725
  }
700
726
  /* add the last table to be repaired to the list */
701
727
  if (found_error && opt_auto_repair && what_to_do != DO_REPAIR)
702
 
    tables4repair.push_back(string(prev));
703
 
  drizzle_free_result(res);
 
728
    insert_dynamic(&tables4repair, (uchar*) prev);
 
729
  mysql_free_result(res);
704
730
}
705
731
 
706
732
 
707
733
static int dbConnect(char *host, char *user, char *passwd)
708
734
{
709
 
 
 
735
  DBUG_ENTER("dbConnect");
710
736
  if (verbose)
711
737
  {
712
738
    fprintf(stderr, "# Connecting to %s...\n", host ? host : "localhost");
713
739
  }
714
 
  drizzle_create(&drizzle_connection);
 
740
  mysql_init(&mysql_connection);
715
741
  if (opt_compress)
716
 
    drizzle_options(&drizzle_connection, DRIZZLE_OPT_COMPRESS, NULL);
 
742
    mysql_options(&mysql_connection, MYSQL_OPT_COMPRESS, NullS);
717
743
  if (opt_protocol)
718
 
    drizzle_options(&drizzle_connection,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
719
 
  if (!(sock = drizzle_connect(&drizzle_connection, host, user, passwd,
 
744
    mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
 
745
#ifdef HAVE_SMEM
 
746
  if (shared_memory_base_name)
 
747
    mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
 
748
#endif
 
749
  if (!(sock = mysql_real_connect(&mysql_connection, host, user, passwd,
720
750
         NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
721
751
  {
722
 
    DBerror(&drizzle_connection, "when trying to connect");
 
752
    DBerror(&mysql_connection, "when trying to connect");
723
753
    return 1;
724
754
  }
725
 
  drizzle_connection.reconnect= 1;
 
755
  mysql_connection.reconnect= 1;
726
756
  return 0;
727
757
} /* dbConnect */
728
758
 
731
761
{
732
762
  if (verbose)
733
763
    fprintf(stderr, "# Disconnecting from %s...\n", host ? host : "localhost");
734
 
  drizzle_close(sock);
 
764
  mysql_close(sock);
735
765
} /* dbDisconnect */
736
766
 
737
767
 
738
 
static void DBerror(DRIZZLE *drizzle, const char *when)
 
768
static void DBerror(MYSQL *mysql, const char *when)
739
769
{
 
770
  DBUG_ENTER("DBerror");
740
771
  my_printf_error(0,"Got error: %d: %s %s", MYF(0),
741
 
      drizzle_errno(drizzle), drizzle_error(drizzle), when);
 
772
                  mysql_errno(mysql), mysql_error(mysql), when);
742
773
  safe_exit(EX_MYSQLERR);
743
 
  return;
 
774
  DBUG_VOID_RETURN;
744
775
} /* DBerror */
745
776
 
746
777
 
751
782
  if (ignore_errors)
752
783
    return;
753
784
  if (sock)
754
 
    drizzle_close(sock);
 
785
    mysql_close(sock);
755
786
  exit(error);
756
787
}
757
788
 
770
801
  if (dbConnect(current_host, current_user, opt_password))
771
802
    exit(EX_MYSQLERR);
772
803
 
773
 
  if (opt_auto_repair)
 
804
  if (opt_auto_repair &&
 
805
      my_init_dynamic_array(&tables4repair, sizeof(char)*(NAME_LEN*2+2),16,64))
774
806
  {
775
 
    tables4repair.reserve(64);
776
 
    if (tables4repair.capacity() == 0)
777
 
    {
778
 
      first_error = 1;
779
 
      goto end;
780
 
    }
 
807
    first_error = 1;
 
808
    goto end;
781
809
  }
782
810
 
783
 
 
784
811
  if (opt_alldbs)
785
812
    process_all_databases();
786
813
  /* Only one database and selected table(s) */
791
818
    process_databases(argv);
792
819
  if (opt_auto_repair)
793
820
  {
 
821
    uint i;
794
822
 
795
 
    if (!opt_silent && (tables4repair.size() > 0))
 
823
    if (!opt_silent && tables4repair.elements)
796
824
      puts("\nRepairing tables");
797
825
    what_to_do = DO_REPAIR;
798
 
    vector<string>::iterator i;
799
 
    for ( i= tables4repair.begin() ; i < tables4repair.end() ; i++)
 
826
    for (i = 0; i < tables4repair.elements ; i++)
800
827
    {
801
 
      const char *name= (*i).c_str();
 
828
      char *name= (char*) dynamic_array_ptr(&tables4repair, i);
802
829
      handle_request_for_tables(name, fixed_name_length(name));
803
830
    }
804
831
  }
805
832
 end:
806
833
  dbDisconnect(current_host);
807
 
  free(opt_password);
 
834
  if (opt_auto_repair)
 
835
    delete_dynamic(&tables4repair);
 
836
  my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
 
837
#ifdef HAVE_SMEM
 
838
  my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
 
839
#endif
808
840
  my_end(my_end_arg);
809
841
  return(first_error!=0);
810
842
} /* main */