sql_show.cc 61.2 KB
Newer Older
1
/* Copyright (C) 2000 MySQL AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   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.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   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.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20
   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 */


/* Function with list databases, tables or fields */

#include "mysql_priv.h"
21
#include "sql_select.h"                         // For select_describe
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22
#include "sql_acl.h"
23
#include "repl_failsafe.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
24
#include <my_dir.h>
25

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
26 27 28 29
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"			// For berkeley_show_logs
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
30 31 32 33 34
static const char *grant_names[]={
  "select","insert","update","delete","create","drop","reload","shutdown",
  "process","file","grant","references","index","alter"};

static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
35 36
                               "grant_types",
                               grant_names};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
37 38

static int mysql_find_files(THD *thd,List<char> *files, const char *db,
39
                            const char *path, const char *wild, bool dir);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
40 41

static int
42 43
store_create_info(THD *thd, TABLE *table, String *packet);

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
44

45 46 47 48
/*
  Report list of databases
  A database is a directory in the mysql_data_home directory
*/
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
49

bk@work.mysql.com's avatar
bk@work.mysql.com committed
50 51 52
int
mysqld_show_dbs(THD *thd,const char *wild)
{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
53
  Item_string *field=new Item_string("",0,thd->charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
54 55 56 57
  List<Item> field_list;
  char *end;
  List<char> files;
  char *file_name;
58
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
59 60
  DBUG_ENTER("mysqld_show_dbs");

61
  field->name=(char*) thd->alloc(20+ (wild ? (uint) strlen(wild)+4: 0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
62 63 64 65 66 67
  field->max_length=NAME_LEN;
  end=strmov(field->name,"Database");
  if (wild && wild[0])
    strxmov(end," (",wild,")",NullS);
  field_list.push_back(field);

68
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
69 70 71
    DBUG_RETURN(1);
  if (mysql_find_files(thd,&files,NullS,mysql_data_home,wild,1))
    DBUG_RETURN(1);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
72
  List_iterator_fast<char> it(files);
73

bk@work.mysql.com's avatar
bk@work.mysql.com committed
74 75
  while ((file_name=it++))
  {
hf@deer.(none)'s avatar
hf@deer.(none) committed
76
#ifndef NO_EMBEDDED_ACCESS_CHECKS
77
    if (thd->master_access & (DB_ACLS | SHOW_DB_ACL) ||
78
	acl_get(thd->host, thd->ip, thd->priv_user, file_name,0) ||
79
	(grant_option && !check_grant_db(thd, file_name)))
hf@deer.(none)'s avatar
hf@deer.(none) committed
80
#endif
81
    {
82
      protocol->prepare_for_resend();
83
      protocol->store(file_name, system_charset_info);
84
      if (protocol->write())
85 86
	DBUG_RETURN(-1);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
87
  }
88
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
89 90 91
  DBUG_RETURN(0);
}

92

93
/***************************************************************************
94
  List all open tables in a database
95 96
***************************************************************************/

97
int mysqld_show_open_tables(THD *thd,const char *wild)
98 99
{
  List<Item> field_list;
100
  OPEN_TABLE_LIST *open_list;
101
  Protocol *protocol= thd->protocol;
102 103
  DBUG_ENTER("mysqld_show_open_tables");

104 105
  field_list.push_back(new Item_empty_string("Database",NAME_LEN));
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
106 107
  field_list.push_back(new Item_return_int("In_use", 1, MYSQL_TYPE_TINY));
  field_list.push_back(new Item_return_int("Name_locked", 4, MYSQL_TYPE_TINY));
108

109
  if (protocol->send_fields(&field_list,1))
110
    DBUG_RETURN(1);
111

112
  if (!(open_list=list_open_tables(thd,wild)) && thd->is_fatal_error)
113 114
    DBUG_RETURN(-1);

115
  for (; open_list ; open_list=open_list->next)
116
  {
117
    protocol->prepare_for_resend();
118 119
    protocol->store(open_list->db, system_charset_info);
    protocol->store(open_list->table, system_charset_info);
120 121 122
    protocol->store_tiny((longlong) open_list->in_use);
    protocol->store_tiny((longlong) open_list->locked);
    if (protocol->write())
123
    {
124
      DBUG_RETURN(-1);
125
    }
126
  }
127
  send_eof(thd);
128 129 130
  DBUG_RETURN(0);
}

131

bk@work.mysql.com's avatar
bk@work.mysql.com committed
132 133 134 135 136 137 138
/***************************************************************************
** List all tables in a database (fast version)
** A table is a .frm file in the current databasedir
***************************************************************************/

int mysqld_show_tables(THD *thd,const char *db,const char *wild)
{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
139
  Item_string *field=new Item_string("",0,thd->charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140 141 142 143
  List<Item> field_list;
  char path[FN_LEN],*end;
  List<char> files;
  char *file_name;
144
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
145 146
  DBUG_ENTER("mysqld_show_tables");

147 148
  field->name=(char*) thd->alloc(20+(uint) strlen(db)+
				 (wild ? (uint) strlen(wild)+4:0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
149 150 151 152 153 154 155
  end=strxmov(field->name,"Tables_in_",db,NullS);
  if (wild && wild[0])
    strxmov(end," (",wild,")",NullS);
  field->max_length=NAME_LEN;
  (void) sprintf(path,"%s/%s",mysql_data_home,db);
  (void) unpack_dirname(path,path);
  field_list.push_back(field);
156
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
157 158 159
    DBUG_RETURN(1);
  if (mysql_find_files(thd,&files,db,path,wild,0))
    DBUG_RETURN(-1);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
160
  List_iterator_fast<char> it(files);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
161 162
  while ((file_name=it++))
  {
163
    protocol->prepare_for_resend();
164
    protocol->store(file_name, system_charset_info);
165
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
166 167
      DBUG_RETURN(-1);
  }
168
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
169 170 171
  DBUG_RETURN(0);
}

172 173 174 175
/***************************************************************************
** List all table types supported 
***************************************************************************/

176
int mysqld_show_storage_engines(THD *thd)
177 178
{
  List<Item> field_list;
179
  Protocol *protocol= thd->protocol;
180
  DBUG_ENTER("mysqld_show_storage_engines");
181 182 183

  field_list.push_back(new Item_empty_string("Type",10));
  field_list.push_back(new Item_empty_string("Support",10));
184
  field_list.push_back(new Item_empty_string("Comment",80));
185

186
  if (protocol->send_fields(&field_list,1))
187 188
    DBUG_RETURN(1);

189
  const char *default_type_name= 
190
    ha_get_storage_engine((enum db_type)thd->variables.table_type);
191

192 193
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
194
  {
195
    protocol->prepare_for_resend();
196
    protocol->store(types->type, system_charset_info);
197 198 199
    const char *option_name= show_comp_option_name[(int) *types->value];

    if (*types->value == SHOW_OPTION_YES &&
200
	!my_strcasecmp(system_charset_info, default_type_name, types->type))
201
      option_name= "DEFAULT";
202 203
    protocol->store(option_name, system_charset_info);
    protocol->store(types->comment, system_charset_info);
204
    if (protocol->write())
205 206
      DBUG_RETURN(-1);
  }
207
  send_eof(thd);
208 209 210
  DBUG_RETURN(0);
}

211

212
/***************************************************************************
213
 List all privileges supported
214 215
***************************************************************************/

216 217 218 219
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
220 221
};

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

/*
  TODO:  Update with new privileges
*/
static struct show_privileges_st sys_privileges[]=
{
  {"Select", "Tables",  "To retrieve rows from table"},
  {"Insert", "Tables",  "To insert data into tables"},
  {"Update", "Tables",  "To update existing rows "},
  {"Delete", "Tables",  "To delete existing rows"},
  {"Index",  "Tables",  "To create or drop indexes"},
  {"Alter",  "Tables",  "To alter the table"},
  {"Create", "Databases,Tables,Indexes",  "To create new databases and tables"},
  {"Drop",   "Databases,Tables", "To drop databases and tables"},
  {"Grant",  "Databases,Tables", "To give to other users those privileges you possess"},
  {"References", "Databases,Tables", "To have references on tables"},
  {"Reload",  "Server Admin", "To reload or refresh tables, logs and privileges"},
  {"Shutdown","Server Admin", "To shutdown the server"},
  {"Process", "Server Admin", "To view the plain text of currently executing queries"},
  {"File",    "File access on server",   "To read and write files on the server"},
  {NullS, NullS, NullS}
};


246 247 248
int mysqld_show_privileges(THD *thd)
{
  List<Item> field_list;
249
  Protocol *protocol= thd->protocol;
250 251 252 253 254 255
  DBUG_ENTER("mysqld_show_privileges");

  field_list.push_back(new Item_empty_string("Privilege",10));
  field_list.push_back(new Item_empty_string("Context",15));
  field_list.push_back(new Item_empty_string("Comment",NAME_LEN));

256
  if (protocol->send_fields(&field_list,1))
257 258
    DBUG_RETURN(1);

259 260
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
261
  {
262
    protocol->prepare_for_resend();
263 264 265
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
266
    if (protocol->write())
267 268
      DBUG_RETURN(-1);
  }
269
  send_eof(thd);
270 271 272 273 274
  DBUG_RETURN(0);
}


/***************************************************************************
275
  List all column types
276 277
***************************************************************************/

278 279
struct show_column_type_st
{
280 281
  const char *type;
  uint size;
282 283 284 285 286 287 288 289 290 291 292 293
  const char *min_value;
  const char *max_value;
  uint precision;
  uint scale;
  const char *nullable;
  const char *auto_increment;
  const char *unsigned_attr;
  const char *zerofill;
  const char *searchable;
  const char *case_sensitivity;
  const char *default_value;
  const char *comment;
294
};
295 296 297 298 299

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
300 301 302 303 304 305 306 307 308 309 310 311 312
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
    "NO",   "YES", "YES",  "NO",  "NULL,0",  
    "A very small integer"}, 
  {"tinyint unsigned",
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",  
    "YES",  "YES",  "YES",  "NO",  "NULL,0", 
    "A very small integer"},
};

int mysqld_show_column_types(THD *thd)
{
  List<Item> field_list;
313
  Protocol *protocol= thd->protocol;
314 315 316 317 318 319
  DBUG_ENTER("mysqld_show_column_types");

  field_list.push_back(new Item_empty_string("Type",30));
  field_list.push_back(new Item_int("Size",(longlong) 1,21));
  field_list.push_back(new Item_empty_string("Min_Value",20));
  field_list.push_back(new Item_empty_string("Max_Value",20));
320 321
  field_list.push_back(new Item_return_int("Prec", 4, MYSQL_TYPE_SHORT));
  field_list.push_back(new Item_return_int("Scale", 4, MYSQL_TYPE_SHORT));
322 323 324 325 326 327 328 329 330
  field_list.push_back(new Item_empty_string("Nullable",4));
  field_list.push_back(new Item_empty_string("Auto_Increment",4));
  field_list.push_back(new Item_empty_string("Unsigned",4));
  field_list.push_back(new Item_empty_string("Zerofill",4));
  field_list.push_back(new Item_empty_string("Searchable",4));
  field_list.push_back(new Item_empty_string("Case_Sensitive",4));
  field_list.push_back(new Item_empty_string("Default",NAME_LEN));
  field_list.push_back(new Item_empty_string("Comment",NAME_LEN));

331
  if (protocol->send_fields(&field_list,1))
332 333
    DBUG_RETURN(1);

334
  /* TODO: Change the loop to not use 'i' */
335 336
  for (uint i=0; i < sizeof(sys_column_types)/sizeof(sys_column_types[0]); i++)
  {
337
    protocol->prepare_for_resend();
338
    protocol->store(sys_column_types[i].type, system_charset_info);
339
    protocol->store((ulonglong) sys_column_types[i].size);
340 341
    protocol->store(sys_column_types[i].min_value, system_charset_info);
    protocol->store(sys_column_types[i].max_value, system_charset_info);
342 343
    protocol->store_short((longlong) sys_column_types[i].precision);
    protocol->store_short((longlong) sys_column_types[i].scale);
344 345 346 347 348 349 350 351
    protocol->store(sys_column_types[i].nullable, system_charset_info);
    protocol->store(sys_column_types[i].auto_increment, system_charset_info);
    protocol->store(sys_column_types[i].unsigned_attr, system_charset_info);
    protocol->store(sys_column_types[i].zerofill, system_charset_info);
    protocol->store(sys_column_types[i].searchable, system_charset_info);
    protocol->store(sys_column_types[i].case_sensitivity, system_charset_info);
    protocol->store(sys_column_types[i].default_value, system_charset_info);
    protocol->store(sys_column_types[i].comment, system_charset_info);
352
    if (protocol->write())
353 354
      DBUG_RETURN(-1);
  }
355
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
356 357 358 359 360 361
  DBUG_RETURN(0);
}


static int
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
362
                 const char *wild, bool dir)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
363 364 365 366 367 368 369 370 371
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
  uint col_access=thd->col_access;
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

372 373
  if (wild && !wild[0])
    wild=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
374 375 376 377 378
  bzero((char*) &table_list,sizeof(table_list));

  if (!(dirp = my_dir(path,MYF(MY_WME | (dir ? MY_WANT_STAT : 0)))))
    DBUG_RETURN(-1);

379
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
380 381 382
  {
    file=dirp->dir_entry+i;
    if (dir)
383
    {                                           /* Return databases */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
384 385 386
#ifdef USE_SYMDIR
      char *ext;
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
387 388 389 390
      {
	/* Only show the sym file if it points to a directory */
	char buff[FN_REFLEN], *end;
	MY_STAT status;
391
        *ext=0;                                 /* Remove extension */
392 393 394 395 396 397 398 399
	unpack_dirname(buff, file->name);
	end= strend(buff);
	if (end != buff && end[-1] == FN_LIBCHAR)
	  end[-1]= 0;				// Remove end FN_LIBCHAR
	if (!my_stat(buff, &status, MYF(0)) ||
	    !MY_S_ISDIR(status.st_mode))
	  continue;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
400 401 402
      else
#endif
      {
dlenev@mysql.com's avatar
dlenev@mysql.com committed
403
        if (file->name[0] == '.' || !MY_S_ISDIR(file->mystat->st_mode) ||
404
            (wild && wild_compare(file->name,wild,0)))
405
          continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
406 407 408 409
      }
    }
    else
    {
410
        // Return only .frm files which aren't temp files.
411
      if (my_strcasecmp(system_charset_info, ext=fn_ext(file->name),reg_ext) ||
412
          is_prefix(file->name,tmp_file_prefix))
413
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
414
      *ext=0;
415 416 417 418
      if (wild)
      {
	if (lower_case_table_names)
	{
419
	  if (wild_case_compare(system_charset_info,file->name,wild))
420 421
	    continue;
	}
422
	else if (wild_compare(file->name,wild,0))
423 424
	  continue;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
425
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
426
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
427 428 429 430 431 432
    /* Don't show tables where we don't have any privileges */
    if (db && !(col_access & TABLE_ACLS))
    {
      table_list.db= (char*) db;
      table_list.real_name=file->name;
      table_list.grant.privilege=col_access;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
433
      if (check_grant(thd,TABLE_ACLS,&table_list,1,1))
434
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
435
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
436
#endif
437
    if (files->push_back(thd->strdup(file->name)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
438 439 440 441 442 443 444 445 446 447
    {
      my_dirend(dirp);
      DBUG_RETURN(-1);
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
  DBUG_RETURN(0);
}

448

bk@work.mysql.com's avatar
bk@work.mysql.com committed
449
/***************************************************************************
450
 Extended version of mysqld_show_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
451 452 453 454 455 456 457 458 459 460
***************************************************************************/

int mysqld_extend_show_tables(THD *thd,const char *db,const char *wild)
{
  Item *item;
  List<char> files;
  List<Item> field_list;
  char path[FN_LEN];
  char *file_name;
  TABLE *table;
461 462
  Protocol *protocol= thd->protocol;
  TIME time;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  DBUG_ENTER("mysqld_extend_show_tables");

  (void) sprintf(path,"%s/%s",mysql_data_home,db);
  (void) unpack_dirname(path,path);
  field_list.push_back(item=new Item_empty_string("Name",NAME_LEN));
  field_list.push_back(item=new Item_empty_string("Type",10));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Row_format",10));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Rows",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Avg_row_length",(int32) 0,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Data_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Max_data_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Index_length",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Data_free",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Auto_increment",(longlong) 1,21));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Create_time"));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Update_time"));
  item->maybe_null=1;
  field_list.push_back(item=new Item_datetime("Check_time"));
  item->maybe_null=1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
492
  field_list.push_back(item=new Item_empty_string("Collation",32));
493
  item->maybe_null=1;
serg@serg.mylan's avatar
serg@serg.mylan committed
494 495
  field_list.push_back(item=new Item_int("Checksum",(longlong) 1,21));
  item->maybe_null=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
496 497 498
  field_list.push_back(item=new Item_empty_string("Create_options",255));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Comment",80));
499
  item->maybe_null=1;
500
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
501 502 503 504
    DBUG_RETURN(1);

  if (mysql_find_files(thd,&files,db,path,wild,0))
    DBUG_RETURN(-1);
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
505
  List_iterator_fast<char> it(files);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
506 507 508 509
  while ((file_name=it++))
  {
    TABLE_LIST table_list;
    bzero((char*) &table_list,sizeof(table_list));
510
    protocol->prepare_for_resend();
511
    protocol->store(file_name, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
512
    table_list.db=(char*) db;
513
    table_list.real_name= table_list.alias= file_name;
514
    if (lower_case_table_names)
515
      my_casedn_str(files_charset_info, file_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
516 517
    if (!(table = open_ltable(thd, &table_list, TL_READ)))
    {
518
      for (uint i=2 ; i < field_list.elements ; i++)
519
        protocol->store_null();
520
      // Send error to Comment field
521
      protocol->store(thd->net.last_error, system_charset_info);
522
      thd->clear_error();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
523 524 525 526
    }
    else
    {
      struct tm tm_tmp;
527
      const char *str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
528 529
      handler *file=table->file;
      file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_NO_LOCK);
530
      protocol->store(file->table_type(), system_charset_info);
531 532 533 534
      str= ((table->db_options_in_use & HA_OPTION_COMPRESS_RECORD) ?
	    "Compressed" :
	    (table->db_options_in_use & HA_OPTION_PACK_RECORD) ?
	    "Dynamic" : "Fixed");
535
      protocol->store(str, system_charset_info);
536 537 538
      protocol->store((ulonglong) file->records);
      protocol->store((ulonglong) file->mean_rec_length);
      protocol->store((ulonglong) file->data_file_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
539
      if (file->max_data_file_length)
540
        protocol->store((ulonglong) file->max_data_file_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
541
      else
542 543 544
        protocol->store_null();
      protocol->store((ulonglong) file->index_file_length);
      protocol->store((ulonglong) file->delete_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
545 546
      if (table->found_next_number_field)
      {
547 548 549
        table->next_number_field=table->found_next_number_field;
        table->next_number_field->reset();
        file->update_auto_increment();
550
        protocol->store(table->next_number_field->val_int());
551
        table->next_number_field=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
552 553
      }
      else
554
        protocol->store_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
555
      if (!file->create_time)
556
        protocol->store_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
557 558
      else
      {
559
        localtime_r(&file->create_time,&tm_tmp);
560 561
	localtime_to_TIME(&time, &tm_tmp);
        protocol->store(&time);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
562 563
      }
      if (!file->update_time)
564
        protocol->store_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
565 566
      else
      {
567
        localtime_r(&file->update_time,&tm_tmp);
568 569
	localtime_to_TIME(&time, &tm_tmp);
        protocol->store(&time);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
570 571
      }
      if (!file->check_time)
572
        protocol->store_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
573 574
      else
      {
575
        localtime_r(&file->check_time,&tm_tmp);
576 577
	localtime_to_TIME(&time, &tm_tmp);
        protocol->store(&time);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
578
      }
579
      str= (table->table_charset ? table->table_charset->name : "default");
580
      protocol->store(str, system_charset_info);
serg@serg.mylan's avatar
serg@serg.mylan committed
581 582 583 584
      if (file->table_flags() & HA_HAS_CHECKSUM)
        protocol->store((ulonglong)file->checksum());
      else
        protocol->store_null(); // Checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
585
      {
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        char option_buff[350],*ptr;
        ptr=option_buff;
        if (table->min_rows)
        {
          ptr=strmov(ptr," min_rows=");
          ptr=longlong10_to_str(table->min_rows,ptr,10);
        }
        if (table->max_rows)
        {
          ptr=strmov(ptr," max_rows=");
          ptr=longlong10_to_str(table->max_rows,ptr,10);
        }
        if (table->avg_row_length)
        {
          ptr=strmov(ptr," avg_row_length=");
          ptr=longlong10_to_str(table->avg_row_length,ptr,10);
        }
        if (table->db_create_options & HA_OPTION_PACK_KEYS)
          ptr=strmov(ptr," pack_keys=1");
        if (table->db_create_options & HA_OPTION_NO_PACK_KEYS)
          ptr=strmov(ptr," pack_keys=0");
        if (table->db_create_options & HA_OPTION_CHECKSUM)
          ptr=strmov(ptr," checksum=1");
        if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
          ptr=strmov(ptr," delay_key_write=1");
        if (table->row_type != ROW_TYPE_DEFAULT)
612
          ptr=strxmov(ptr, " row_format=", ha_row_type[(uint) table->row_type],
613 614 615 616 617 618 619 620
                      NullS);
        if (file->raid_type)
        {
          char buff[100];
          sprintf(buff," raid_type=%s raid_chunks=%d raid_chunksize=%ld",
                  my_raid_type(file->raid_type), file->raid_chunks, file->raid_chunksize/RAID_BLOCK_SIZE);
          ptr=strmov(ptr,buff);
        }
621
        protocol->store(option_buff+1,
622 623
			(ptr == option_buff ? 0 : (uint) (ptr-option_buff)-1)
			, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
624
      }
625 626
      {
	char *comment=table->file->update_table_comment(table->comment);
627
	protocol->store(comment, system_charset_info);
628 629 630
	if (comment != table->comment)
	  my_free(comment,MYF(0));
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
631 632
      close_thread_tables(thd,0);
    }
633
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
634 635
      DBUG_RETURN(-1);
  }
636
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
637 638 639 640 641
  DBUG_RETURN(0);
}


/***************************************************************************
642
** List all columns in a table_list->real_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
643
***************************************************************************/
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
644

bk@work.mysql.com's avatar
bk@work.mysql.com committed
645
int
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
646 647
mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild,
		   bool verbose)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
648 649 650 651
{
  TABLE *table;
  handler *file;
  char tmp[MAX_FIELD_WIDTH];
652
  Item *item;
653
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
654 655
  DBUG_ENTER("mysqld_show_fields");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
656
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657 658 659

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
660
    send_error(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
661 662 663 664
    DBUG_RETURN(1);
  }
  file=table->file;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
hf@deer.(none)'s avatar
hf@deer.(none) committed
665
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
666
  (void) get_table_grant(thd, table_list);
hf@deer.(none)'s avatar
hf@deer.(none) committed
667
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
668 669 670
  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Field",NAME_LEN));
  field_list.push_back(new Item_empty_string("Type",40));
671 672
  if (verbose)
    field_list.push_back(new Item_empty_string("Collation",40));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
673 674
  field_list.push_back(new Item_empty_string("Null",1));
  field_list.push_back(new Item_empty_string("Key",3));
675 676
  field_list.push_back(item=new Item_empty_string("Default",NAME_LEN));
  item->maybe_null=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
677
  field_list.push_back(new Item_empty_string("Extra",20));
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
678
  if (verbose)
679
  {
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
680
    field_list.push_back(new Item_empty_string("Privileges",80));
681 682
    field_list.push_back(new Item_empty_string("Comment",255));
  }
683
        // Send first number of fields and records
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
684 685
  if (protocol->send_records_num(&field_list, (ulonglong)file->records) ||
      protocol->send_fields(&field_list,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
686
    DBUG_RETURN(1);
687
  restore_record(table,default_values);      // Get empty record
bk@work.mysql.com's avatar
bk@work.mysql.com committed
688 689 690 691

  Field **ptr,*field;
  for (ptr=table->field; (field= *ptr) ; ptr++)
  {
692 693
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
694 695 696
    {
#ifdef NOT_USED
      if (thd->col_access & TABLE_ACLS ||
697 698
          ! check_grant_column(thd,table,field->field_name,
                               (uint) strlen(field->field_name),1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
699 700
#endif
      {
701 702
        byte *pos;
        uint flags=field->flags;
703
        String type(tmp,sizeof(tmp), system_charset_info);
704 705 706
        uint col_access;
        bool null_default_value=0;

707
	protocol->prepare_for_resend();
708
        protocol->store(field->field_name, system_charset_info);
709
        field->sql_type(type);
710
        protocol->store(type.ptr(), type.length(), system_charset_info);
711 712
	if (verbose)
	  protocol->store(field->has_charset() ? field->charset()->name : "NULL",
713
			system_charset_info);
714 715 716
        pos=(byte*) ((flags & NOT_NULL_FLAG) &&
                     field->type() != FIELD_TYPE_TIMESTAMP ?
                     "" : "YES");
717
        protocol->store((const char*) pos, system_charset_info);
718 719 720
        pos=(byte*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
                     (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
                     (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
721
        protocol->store((char*) pos, system_charset_info);
722 723 724 725 726 727

        if (field->type() == FIELD_TYPE_TIMESTAMP ||
            field->unireg_check == Field::NEXT_NUMBER)
          null_default_value=1;
        if (!null_default_value && !field->is_null())
        {                                               // Not null by default
728
          type.set(tmp, sizeof(tmp), field->charset());
729
          field->val_str(&type,&type);
730
          protocol->store(type.ptr(),type.length(),type.charset());
731 732
        }
        else if (field->maybe_null() || null_default_value)
733
          protocol->store_null();                       // Null as default
734
        else
735
          protocol->store("",0, system_charset_info);	// empty string
736 737 738 739

        char *end=tmp;
        if (field->unireg_check == Field::NEXT_NUMBER)
          end=strmov(tmp,"auto_increment");
740
        protocol->store(tmp,(uint) (end-tmp), system_charset_info);
741

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
742 743
	if (verbose)
	{
744
	  /* Add grant options & comments */
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
745
	  end=tmp;
hf@deer.(none)'s avatar
hf@deer.(none) committed
746
#ifndef NO_EMBEDDED_ACCESS_CHECKS
747
	  col_access= get_column_grant(thd,table_list,field) & COL_ACLS;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
748 749 750 751 752 753 754 755
	  for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
	  {
	    if (col_access & 1)
	    {
	      *end++=',';
	      end=strmov(end,grant_types.type_names[bitnr]);
	    }
	  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
756 757 758
#else
	  end=strmov(end,"");
#endif
759 760 761 762
	  protocol->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1),
			  system_charset_info);
	  protocol->store(field->comment.str, field->comment.length,
			  system_charset_info);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
763
	}
764
        if (protocol->write())
765
          DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
766 767 768
      }
    }
  }
769
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
770 771 772
  DBUG_RETURN(0);
}

773

bk@work.mysql.com's avatar
bk@work.mysql.com committed
774 775 776 777
int
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
778 779 780
  Protocol *protocol= thd->protocol;
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
781 782
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
783
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
784

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
785
  /* Only one table for now */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
786 787
  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
788
    send_error(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
789 790 791
    DBUG_RETURN(1);
  }

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
792
  if (store_create_info(thd, table, &buffer))
793 794
    DBUG_RETURN(-1);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
795 796
  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
797
  // 1024 is for not to confuse old clients
798
  field_list.push_back(new Item_empty_string("Create Table",
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
799
					     max(buffer.length(),1024)));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
800

801 802 803
  if (protocol->send_fields(&field_list, 1))
    DBUG_RETURN(1);
  protocol->prepare_for_resend();
804
  protocol->store(table->table_name, system_charset_info);
805
  buffer.length(0);
806 807
  if (store_create_info(thd, table, &buffer))
    DBUG_RETURN(-1);
808
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
809
  if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
810
    DBUG_RETURN(1);
811
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
812 813 814
  DBUG_RETURN(0);
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
int mysqld_show_create_db(THD *thd, char *dbname,
			  HA_CREATE_INFO *create_info)
{
  int length;
  char	path[FN_REFLEN];
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
  uint db_access;
  bool found_libchar;
  HA_CREATE_INFO create;
  uint create_options = create_info ? create_info->options : 0;
  Protocol *protocol=thd->protocol;
  DBUG_ENTER("mysql_show_create_db");

  if (check_db_name(dbname))
  {
    net_printf(thd,ER_WRONG_DB_NAME, dbname);
    DBUG_RETURN(1);
  }

#ifndef NO_EMBEDDED_ACCESS_CHECKS
  if (test_all_bits(thd->master_access,DB_ACLS))
    db_access=DB_ACLS;
  else
    db_access= (acl_get(thd->host,thd->ip, thd->priv_user,dbname,0) |
		thd->master_access);
  if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname)))
  {
    net_printf(thd,ER_DBACCESS_DENIED_ERROR,
	       thd->priv_user, thd->host_or_ip, dbname);
    mysql_log.write(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR),
		    thd->priv_user, thd->host_or_ip, dbname);
    DBUG_RETURN(1);
  }
#endif

  (void) sprintf(path,"%s/%s",mysql_data_home, dbname);
  length=unpack_dirname(path,path);		// Convert if not unix
  found_libchar= 0;
  if (length && path[length-1] == FN_LIBCHAR)
  {
    found_libchar= 1;
    path[length-1]=0;				// remove ending '\'
  }
  if (access(path,F_OK))
  {
    net_printf(thd,ER_BAD_DB_ERROR,dbname);
    DBUG_RETURN(1);
  }
  if (found_libchar)
    path[length-1]= FN_LIBCHAR;
  strmov(path+length, MY_DB_OPT_FILE);
  load_db_opt(thd, path, &create);

  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Database",NAME_LEN));
  field_list.push_back(new Item_empty_string("Create Database",1024));

  if (protocol->send_fields(&field_list,1))
    DBUG_RETURN(1);

  protocol->prepare_for_resend();
  protocol->store(dbname, strlen(dbname), system_charset_info);
  buffer.length(0);
  buffer.append("CREATE DATABASE ", 16);
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
    buffer.append("/*!32312 IF NOT EXISTS*/ ", 25);
  append_identifier(thd, &buffer, dbname, strlen(dbname));

  if (create.default_table_charset)
  {
    buffer.append(" /*!40100", 9);
    buffer.append(" DEFAULT CHARACTER SET ", 23);
    buffer.append(create.default_table_charset->csname);
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
    {
      buffer.append(" COLLATE ", 9);
      buffer.append(create.default_table_charset->name);
    }
    buffer.append(" */", 3);
  }
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());

  if (protocol->write())
    DBUG_RETURN(1);
  send_eof(thd);
  DBUG_RETURN(0);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
903

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
904 905 906
int
mysqld_show_logs(THD *thd)
{
907 908
  List<Item> field_list;
  Protocol *protocol= thd->protocol;
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
909 910 911 912 913 914
  DBUG_ENTER("mysqld_show_logs");

  field_list.push_back(new Item_empty_string("File",FN_REFLEN));
  field_list.push_back(new Item_empty_string("Type",10));
  field_list.push_back(new Item_empty_string("Status",10));

915
  if (protocol->send_fields(&field_list,1))
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
916 917
    DBUG_RETURN(1);

918
#ifdef HAVE_BERKELEY_DB
919
  if (!berkeley_skip && berkeley_show_logs(protocol))
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
920
    DBUG_RETURN(-1);
921
#endif
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
922

923
  send_eof(thd);
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
924 925 926 927
  DBUG_RETURN(0);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
928 929 930 931
int
mysqld_show_keys(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
932
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
933 934
  DBUG_ENTER("mysqld_show_keys");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
935
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
936 937 938

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
939
    send_error(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
940 941 942 943 944 945
    DBUG_RETURN(1);
  }

  List<Item> field_list;
  Item *item;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
946
  field_list.push_back(new Item_return_int("Non_unique",1, MYSQL_TYPE_TINY));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
947
  field_list.push_back(new Item_empty_string("Key_name",NAME_LEN));
948
  field_list.push_back(new Item_return_int("Seq_in_index",2, MYSQL_TYPE_TINY));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
949 950 951
  field_list.push_back(new Item_empty_string("Column_name",NAME_LEN));
  field_list.push_back(item=new Item_empty_string("Collation",1));
  item->maybe_null=1;
952
  field_list.push_back(item=new Item_int("Cardinality",0,21));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
953
  item->maybe_null=1;
954 955
  field_list.push_back(item=new Item_return_int("Sub_part",3,
						MYSQL_TYPE_TINY));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
956 957 958
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Packed",10));
  item->maybe_null=1;
959 960
  field_list.push_back(new Item_empty_string("Null",3));
  field_list.push_back(new Item_empty_string("Index_type",16));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
961 962 963
  field_list.push_back(new Item_empty_string("Comment",255));
  item->maybe_null=1;

964
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
965 966 967 968 969 970 971
    DBUG_RETURN(1);

  KEY *key_info=table->key_info;
  table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
  for (uint i=0 ; i < table->keys ; i++,key_info++)
  {
    KEY_PART_INFO *key_part= key_info->key_part;
972
    const char *str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973 974
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
975
      protocol->prepare_for_resend();
976
      protocol->store(table->table_name, system_charset_info);
977
      protocol->store_tiny((longlong) ((key_info->flags & HA_NOSAME) ? 0 :1));
978
      protocol->store(key_info->name, system_charset_info);
979 980 981
      protocol->store_tiny((longlong) (j+1));
      str=(key_part->field ? key_part->field->field_name :
	   "?unknown field?");
982
      protocol->store(str, system_charset_info);
983
      if (table->file->index_flags(i) & HA_READ_ORDER)
984
        protocol->store(((key_part->key_part_flag & HA_REVERSE_SORT) ?
985
			 "D" : "A"), 1, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
986
      else
987
        protocol->store_null(); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
988 989 990
      KEY *key=table->key_info+i;
      if (key->rec_per_key[j])
      {
991
        ha_rows records=(table->file->records / key->rec_per_key[j]);
992
        protocol->store((ulonglong) records);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
993 994
      }
      else
995
        protocol->store_null();
996 997

      /* Check if we have a key part that only uses part of the field */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
998
      if (!key_part->field ||
999 1000
          key_part->length !=
          table->field[key_part->fieldnr-1]->key_length())
1001
        protocol->store_tiny((longlong) key_part->length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1002
      else
1003 1004
        protocol->store_null();
      protocol->store_null();                   // No pack_information yet
1005 1006 1007

      /* Null flag */
      uint flags= key_part->field ? key_part->field->flags : 0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1008
      char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
1009 1010
      protocol->store((const char*) pos, system_charset_info);
      protocol->store(table->file->index_type(i), system_charset_info);
1011
      /* Comment */
1012
      if (!table->keys_in_use.is_set(i))
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1013
	protocol->store("disabled",8, system_charset_info);
1014
      else
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1015
        protocol->store("", 0, system_charset_info);
1016
      if (protocol->write())
1017
        DBUG_RETURN(1); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1018 1019
    }
  }
1020
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1021 1022 1023 1024 1025
  DBUG_RETURN(0);
}


/****************************************************************************
1026 1027
  Return only fields for API mysql_list_fields
  Use "show table wildcard" in mysql instead of this
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
****************************************************************************/

void
mysqld_list_fields(THD *thd, TABLE_LIST *table_list, const char *wild)
{
  TABLE *table;
  DBUG_ENTER("mysqld_list_fields");
  DBUG_PRINT("enter",("table: %s",table_list->real_name));

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
1039
    send_error(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1040 1041 1042 1043 1044 1045 1046
    DBUG_VOID_RETURN;
  }
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
1047 1048
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1049 1050
      field_list.push_back(new Item_field(field));
  }
1051
  restore_record(table,default_values);              // Get empty record
1052
  if (thd->protocol->send_fields(&field_list,2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1053
    DBUG_VOID_RETURN;
1054
  net_flush(&thd->net);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1055 1056 1057
  DBUG_VOID_RETURN;
}

1058

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1059 1060 1061
int
mysqld_dump_create_info(THD *thd, TABLE *table, int fd)
{
1062 1063
  Protocol *protocol= thd->protocol;
  String *packet= protocol->storage_packet();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1064 1065
  DBUG_ENTER("mysqld_dump_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));
1066

1067 1068
  protocol->prepare_for_resend();
  if (store_create_info(thd, table, packet))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1069
    DBUG_RETURN(-1);
1070

1071 1072
  //if (protocol->convert)
  //  protocol->convert->convert((char*) packet->ptr(), packet->length());
1073
  if (fd < 0)
1074
  {
1075
    if (protocol->write())
1076
      DBUG_RETURN(-1);
1077
    net_flush(&thd->net);
1078
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1079
  else
1080
  {
1081 1082
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
1083 1084
      DBUG_RETURN(-1);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1085 1086
  DBUG_RETURN(0);
}
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/* possible TODO: call find_keyword() from sql_lex.cc here */
static bool require_quotes(const char *name, uint length)
{
  uint i, d, c;
  for (i=0; i<length; i+=d)
  {
    c=((uchar *)name)[i];
    d=my_mbcharlen(system_charset_info, c);
    if (d==1 && !system_charset_info->ident_map[c])
      return 1;
  }
  return 0;
}
1101

1102 1103
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
1104
{
1105
  char qtype;
1106
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
1107 1108 1109 1110
    qtype= '\"';
  else
    qtype= '`';

1111 1112
  if ((thd->options & OPTION_QUOTE_SHOW_CREATE) ||
      require_quotes(name, length))
1113
  {
1114
    packet->append(&qtype, 1);
1115
    packet->append(name, length, system_charset_info);
1116
    packet->append(&qtype, 1);
1117 1118 1119
  }
  else
  {
1120
    packet->append(name, length, system_charset_info);
1121 1122 1123
  }
}

1124 1125 1126 1127 1128 1129 1130

/* Append directory name (if exists) to CREATE INFO */

static void append_directory(THD *thd, String *packet, const char *dir_type,
			     const char *filename)
{
  uint length;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1131
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
  {
    length= dirname_length(filename);
    packet->append(' ');
    packet->append(dir_type);
    packet->append(" DIRECTORY='", 12);
    packet->append(filename, length);
    packet->append('\'');
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1143
#define LIST_PROCESS_HOST_LEN 64
1144

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1145
static int
1146
store_create_info(THD *thd, TABLE *table, String *packet)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1147
{
1148 1149
  List<Item> field_list;
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1150
  String type(tmp, sizeof(tmp),&my_charset_bin);
1151 1152 1153 1154 1155
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
  handler *file= table->file;
  HA_CREATE_INFO create_info;
1156 1157 1158 1159
  my_bool foreign_db_mode=    (thd->variables.sql_mode & (MODE_POSTGRESQL |
							  MODE_ORACLE |
							  MODE_MSSQL |
							  MODE_DB2 |
1160
							  MODE_MAXDB |
1161 1162 1163 1164
							  MODE_ANSI)) != 0;
  my_bool limited_mysql_mode= (thd->variables.sql_mode &
			       (MODE_NO_FIELD_OPTIONS | MODE_MYSQL323 |
				MODE_MYSQL40)) != 0;
1165

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1166 1167 1168
  DBUG_ENTER("store_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));

1169
  restore_record(table,default_values); // Get empty record
1170

1171 1172 1173 1174
  if (table->tmp_table)
    packet->append("CREATE TEMPORARY TABLE ", 23);
  else
    packet->append("CREATE TABLE ", 13);
1175
  append_identifier(thd,packet, table->real_name, strlen(table->real_name));
1176
  packet->append(" (\n", 3);
1177

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1178 1179
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
1180 1181 1182
    bool has_default;
    uint flags = field->flags;

1183
    if (ptr != table->field)
1184
      packet->append(",\n", 2);
1185

1186
    packet->append("  ", 2);
1187
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1188 1189
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
1190
    if (type.ptr() != tmp)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1191
      type.set(tmp, sizeof(tmp),&my_charset_bin);
1192

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1193 1194
    field->sql_type(type);
    packet->append(type.ptr(),type.length());
1195

1196
    if (field->has_charset())
1197
    {
1198
      if (field->charset() == &my_charset_bin)
1199
        packet->append(" binary", 7);
1200 1201 1202 1203
      else if (!limited_mysql_mode && !foreign_db_mode)
      {
	if (field->charset() != table->table_charset)
	{
1204
	  packet->append(" character set ", 15);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	  packet->append(field->charset()->csname);
	}
	/* 
	  For string types dump collation name only if 
	  collation is not primary for the given charset
	*/
	if (!(field->charset()->state & MY_CS_PRIMARY))
	{
	  packet->append(" collate ", 9);
	  packet->append(field->charset()->name);
	}
      }
1217
    }
1218

1219 1220
    if (flags & NOT_NULL_FLAG)
      packet->append(" NOT NULL", 9);
1221

1222 1223 1224
    has_default= (field->type() != FIELD_TYPE_BLOB &&
		  field->type() != FIELD_TYPE_TIMESTAMP &&
		  field->unireg_check != Field::NEXT_NUMBER);
1225

1226
    if (has_default)
1227 1228
    {
      packet->append(" default ", 9);
1229
      if (!field->is_null())
1230
      {                                             // Not null by default
1231
        type.set(tmp, sizeof(tmp), field->charset());
1232
        field->val_str(&type,&type);
1233
	if (type.length())
1234 1235 1236 1237 1238 1239 1240
	{
   	  String def_val;
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
		       system_charset_info);
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
1241 1242
        else
	  packet->append("''",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1243
      }
1244
      else if (field->maybe_null())
1245 1246 1247 1248
        packet->append("NULL", 4);                    // Null as default
      else
        packet->append(tmp,0);
    }
1249

1250
    if (field->unireg_check == Field::NEXT_NUMBER && !foreign_db_mode)
1251 1252 1253 1254 1255 1256 1257
      packet->append(" auto_increment", 15 );

    if (field->comment.length)
    {
      packet->append(" COMMENT ",9);
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1258 1259
  }

1260 1261
  key_info= table->key_info;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
1262
  bzero((char*) &create_info, sizeof(create_info));
1263 1264
  file->update_create_info(&create_info);
  primary_key= table->primary_key;
1265

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1266 1267
  for (uint i=0 ; i < table->keys ; i++,key_info++)
  {
1268 1269
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
1270
    packet->append(",\n  ", 4);
1271

1272
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
1273 1274
    {
      found_primary=1;
1275
      packet->append("PRIMARY ", 8);
1276
    }
1277
    else if (key_info->flags & HA_NOSAME)
1278
      packet->append("UNIQUE ", 7);
1279
    else if (key_info->flags & HA_FULLTEXT)
1280
      packet->append("FULLTEXT ", 9);
1281 1282
    else if (key_info->flags & HA_SPATIAL)
      packet->append("SPATIAL ", 8);
1283
    packet->append("KEY ", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1284

1285
    if (!found_primary)
1286
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
1287

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    if (!(thd->variables.sql_mode & MODE_NO_KEY_OPTIONS) &&
	!limited_mysql_mode && !foreign_db_mode)
    {
      if (table->db_type == DB_TYPE_HEAP &&
	  key_info->algorithm == HA_KEY_ALG_BTREE)
	packet->append(" TYPE BTREE", 11);
      
      // +BAR: send USING only in non-default case: non-spatial rtree
      if ((key_info->algorithm == HA_KEY_ALG_RTREE) &&
	  !(key_info->flags & HA_SPATIAL))
	packet->append(" TYPE RTREE", 11);
    }
1300
    packet->append(" (", 2);
1301

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1302 1303
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
1304
      if (j)
1305
        packet->append(',');
1306

1307
      if (key_part->field)
1308 1309
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1310
      if (!key_part->field ||
1311 1312 1313
          (key_part->length !=
           table->field[key_part->fieldnr-1]->key_length() &&
           !(key_info->flags & HA_FULLTEXT)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1314
      {
1315
        buff[0] = '(';
1316 1317 1318
        char* end=int10_to_str((long) key_part->length / 
			       key_part->field->charset()->mbmaxlen,
			       buff + 1,10);
1319 1320
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1321 1322 1323 1324
      }
    }
    packet->append(')');
  }
1325

1326 1327 1328 1329
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
1330

1331
  if ((for_str= file->get_foreign_key_create_info()))
1332 1333 1334
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
1335 1336 1337
  }

  packet->append("\n)", 2);
1338
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
1339
  {
1340 1341 1342 1343
    if (thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
      packet->append(" TYPE=", 6);
    else
      packet->append(" ENGINE=", 8);
1344 1345
    packet->append(file->table_type());
    
1346 1347 1348
    if (table->table_charset &&
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
1349
    {
1350
      packet->append(" DEFAULT CHARSET=", 17);
1351 1352 1353
      packet->append(table->table_charset->csname);
      if (!(table->table_charset->state & MY_CS_PRIMARY))
      {
1354
	packet->append(" COLLATE=", 9);
1355 1356
	packet->append(table->table_charset->name);
      }
1357
    }
1358

1359 1360
    if (table->min_rows)
    {
1361
      packet->append(" MIN_ROWS=", 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1362 1363
      end= longlong10_to_str(table->min_rows, buff, 10);
      packet->append(buff, (uint) (end- buff));
1364
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1365

1366 1367
    if (table->max_rows)
    {
1368
      packet->append(" MAX_ROWS=", 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1369 1370
      end= longlong10_to_str(table->max_rows, buff, 10);
      packet->append(buff, (uint) (end - buff));
1371
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1372

1373 1374
    if (table->avg_row_length)
    {
1375
      packet->append(" AVG_ROW_LENGTH=", 16);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1376 1377
      end= longlong10_to_str(table->avg_row_length, buff,10);
      packet->append(buff, (uint) (end - buff));
1378
    }
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
    if (table->db_create_options & HA_OPTION_PACK_KEYS)
      packet->append(" PACK_KEYS=1", 12);
    if (table->db_create_options & HA_OPTION_NO_PACK_KEYS)
      packet->append(" PACK_KEYS=0", 12);
    if (table->db_create_options & HA_OPTION_CHECKSUM)
      packet->append(" CHECKSUM=1", 11);
    if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
      packet->append(" DELAY_KEY_WRITE=1",18);
    if (table->row_type != ROW_TYPE_DEFAULT)
    {
      packet->append(" ROW_FORMAT=",12);
      packet->append(ha_row_type[(uint) table->row_type]);
    }
    table->file->append_create_info(packet);
    if (table->comment && table->comment[0])
    {
      packet->append(" COMMENT=", 9);
      append_unescaped(packet, table->comment, strlen(table->comment));
    }
    if (file->raid_type)
    {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1401 1402 1403 1404 1405 1406
      uint length;
      length= my_snprintf(buff,sizeof(buff),
			  " RAID_TYPE=%s RAID_CHUNKS=%d RAID_CHUNKSIZE=%ld",
			  my_raid_type(file->raid_type), file->raid_chunks,
			  file->raid_chunksize/RAID_BLOCK_SIZE);
      packet->append(buff, length);
1407
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1408 1409
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1410
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1411 1412 1413 1414 1415
  DBUG_RETURN(0);
}


/****************************************************************************
1416 1417
  Return info about all processes
  returns for each thread: thread id, user, host, db, command, info
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1418 1419 1420 1421
****************************************************************************/

class thread_info :public ilink {
public:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1422
  static void *operator new(size_t size) {return (void*) sql_alloc((uint) size); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1423
  static void operator delete(void *ptr __attribute__((unused)),
1424
                              size_t size __attribute__((unused))) {} /*lint -e715 */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1425

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1426 1427
  ulong thread_id;
  time_t start_time;
1428
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

#ifdef __GNUC__
template class I_List<thread_info>;
#endif

void mysqld_list_processes(THD *thd,const char *user, bool verbose)
{
  Item *field;
  List<Item> field_list;
  I_List<thread_info> thread_infos;
1442 1443
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1444
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1445 1446
  DBUG_ENTER("mysqld_list_processes");

1447
  field_list.push_back(new Item_int("Id",0,11));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1448
  field_list.push_back(new Item_empty_string("User",16));
1449
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1450 1451 1452
  field_list.push_back(field=new Item_empty_string("db",NAME_LEN));
  field->maybe_null=1;
  field_list.push_back(new Item_empty_string("Command",16));
1453
  field_list.push_back(new Item_return_int("Time",7, FIELD_TYPE_LONG));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1454 1455 1456 1457
  field_list.push_back(field=new Item_empty_string("State",30));
  field->maybe_null=1;
  field_list.push_back(field=new Item_empty_string("Info",max_query_length));
  field->maybe_null=1;
1458
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1459 1460 1461 1462 1463 1464 1465 1466 1467
    DBUG_VOID_RETURN;

  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
  if (!thd->killed)
  {
    I_List_iterator<THD> it(threads);
    THD *tmp;
    while ((tmp=it++))
    {
1468
      struct st_my_thread_var *mysys_var;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1469
#ifndef EMBEDDED_LIBRARY
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1470
      if ((tmp->net.vio || tmp->system_thread) &&
1471
          (!user || (tmp->user && !strcmp(tmp->user,user))))
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1472 1473 1474 1475
#else
      if (tmp->system_thread &&
          (!user || (tmp->user && !strcmp(tmp->user,user))))
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1476
      {
1477 1478 1479
        thread_info *thd_info=new thread_info;

        thd_info->thread_id=tmp->thread_id;
1480 1481 1482
        thd_info->user=thd->strdup(tmp->user ? tmp->user :
				   (tmp->system_thread ?
				    "system user" : "unauthenticated user"));
1483
	if (tmp->peer_port && (tmp->host || tmp->ip) && thd->host_or_ip[0])
1484 1485
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1486
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1487
			"%s:%u", tmp->host_or_ip, tmp->peer_port);
1488 1489
	}
	else
1490
	  thd_info->host= thd->strdup(tmp->host_or_ip);
1491 1492 1493
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1494 1495
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
1496
        thd_info->proc_info= (char*) (tmp->killed ? "Killed" : 0);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1497
#ifndef EMBEDDED_LIBRARY
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
        thd_info->state_info= (char*) (tmp->locked ? "Locked" :
                                       tmp->net.reading_or_writing ?
                                       (tmp->net.reading_or_writing == 2 ?
                                        "Writing to net" :
                                        thd_info->command == COM_SLEEP ? "" :
                                        "Reading from net") :
                                       tmp->proc_info ? tmp->proc_info :
                                       tmp->mysys_var &&
                                       tmp->mysys_var->current_cond ?
                                       "Waiting on cond" : NullS);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1508 1509 1510
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1511 1512
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1513 1514

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1515 1516
        if (pthread_kill(tmp->real_id,0))
          tmp->proc_info="*** DEAD ***";        // This shouldn't happen
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1517
#endif
1518 1519 1520
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1521
        thd_info->start_time= tmp->start_time;
1522
#endif
1523 1524 1525
        thd_info->query=0;
        if (tmp->query)
        {
1526 1527
	  /* query_length is always set before tmp->query */
          uint length= min(max_query_length, tmp->query_length);
1528 1529 1530 1531
          thd_info->query=(char*) thd->memdup(tmp->query,length+1);
          thd_info->query[length]=0;
        }
        thread_infos.append(thd_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1532 1533 1534 1535 1536 1537
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1538
  time_t now= time(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1539 1540
  while ((thd_info=thread_infos.get()))
  {
1541 1542
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1543 1544 1545
    protocol->store(thd_info->user, system_charset_info);
    protocol->store(thd_info->host, system_charset_info);
    protocol->store(thd_info->db, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1546
    if (thd_info->proc_info)
1547
      protocol->store(thd_info->proc_info, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1548
    else
1549
      protocol->store(command_name[thd_info->command], system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1550
    if (thd_info->start_time)
1551
      protocol->store((uint32) (now - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1552
    else
1553
      protocol->store_null();
1554 1555
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1556
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1557 1558
      break; /* purecov: inspected */
  }
1559
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1560 1561 1562 1563
  DBUG_VOID_RETURN;
}

/*****************************************************************************
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1564
  Status functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1565 1566
*****************************************************************************/

1567 1568 1569 1570
static bool write_collation(Protocol *protocol, CHARSET_INFO *cs)
{
  protocol->prepare_for_resend();
  protocol->store(cs->name, system_charset_info);
1571
  protocol->store(cs->csname, system_charset_info);
1572
  protocol->store_short((longlong) cs->number);
1573 1574
  protocol->store((cs->state & MY_CS_PRIMARY) ? "Yes" : "",system_charset_info);
  protocol->store((cs->state & MY_CS_COMPILED)? "Yes" : "",system_charset_info);
1575 1576 1577 1578 1579
  protocol->store_short((longlong) cs->strxfrm_multiply);
  return protocol->write();
}

int mysqld_show_collations(THD *thd, const char *wild)
1580 1581
{
  char buff[8192];
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1582
  String packet2(buff,sizeof(buff),thd->charset());
1583
  List<Item> field_list;
1584
  CHARSET_INFO **cs;
1585
  Protocol *protocol= thd->protocol;
1586

1587 1588
  DBUG_ENTER("mysqld_show_charsets");

1589
  field_list.push_back(new Item_empty_string("Collation",30));
1590
  field_list.push_back(new Item_empty_string("Charset",30));
1591
  field_list.push_back(new Item_return_int("Id",11, FIELD_TYPE_SHORT));
1592 1593
  field_list.push_back(new Item_empty_string("Default",30));
  field_list.push_back(new Item_empty_string("Compiled",30));
1594
  field_list.push_back(new Item_return_int("Sortlen",3, FIELD_TYPE_SHORT));
1595

1596
  if (protocol->send_fields(&field_list, 1))
1597 1598
    DBUG_RETURN(1);

1599
  for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
1600
  {
1601
    CHARSET_INFO **cl;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1602 1603 1604
    if (!cs[0] || !(cs[0]->state & MY_CS_AVAILABLE) || 
        !(cs[0]->state & MY_CS_PRIMARY))
      continue;
1605 1606
    for ( cl= all_charsets; cl < all_charsets+255 ;cl ++)
    {
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1607 1608
      if (!cl[0] || !(cl[0]->state & MY_CS_AVAILABLE) || 
          !my_charset_same(cs[0],cl[0]))
1609
	continue;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1610
      if (!(wild && wild[0] &&
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1611
	  wild_case_compare(system_charset_info,cl[0]->name,wild)))
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
      {
        if (write_collation(protocol, cl[0]))
	  goto err;
      }
    }
  }
  send_eof(thd); 
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}

static bool write_charset(Protocol *protocol, CHARSET_INFO *cs)
{
  protocol->prepare_for_resend();
  protocol->store(cs->csname, system_charset_info);
1628
  protocol->store(cs->comment ? cs->comment : "", system_charset_info);
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
  protocol->store(cs->name, system_charset_info);
  protocol->store_short((longlong) cs->mbmaxlen);
  return protocol->write();
}

int mysqld_show_charsets(THD *thd, const char *wild)
{
  char buff[8192];
  String packet2(buff,sizeof(buff),thd->charset());
  List<Item> field_list;
  CHARSET_INFO **cs;
  Protocol *protocol= thd->protocol;

  DBUG_ENTER("mysqld_show_charsets");

  field_list.push_back(new Item_empty_string("Charset",30));
1645
  field_list.push_back(new Item_empty_string("Description",60));
1646 1647 1648 1649 1650 1651 1652 1653
  field_list.push_back(new Item_empty_string("Default collation",60));
  field_list.push_back(new Item_return_int("Maxlen",3, FIELD_TYPE_SHORT));

  if (protocol->send_fields(&field_list, 1))
    DBUG_RETURN(1);

  for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
  {
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1654 1655 1656 1657
    if (cs[0] && (cs[0]->state & MY_CS_PRIMARY) && 
        (cs[0]->state & MY_CS_AVAILABLE) &&
        !(wild && wild[0] &&
        wild_case_compare(system_charset_info,cs[0]->csname,wild)))
1658
    {
1659
      if (write_charset(protocol, cs[0]))
1660
	goto err;
1661 1662
    }
  }
1663
  send_eof(thd); 
1664 1665 1666 1667 1668
  DBUG_RETURN(0);
err:
  DBUG_RETURN(1);
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1669
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1670

1671
int mysqld_show(THD *thd, const char *wild, show_var_st *variables,
1672 1673
		enum enum_var_type value_type,
		pthread_mutex_t *mutex)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1674
{
1675
  char buff[1024];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1676
  List<Item> field_list;
1677
  Protocol *protocol= thd->protocol;
1678
  LEX_STRING null_lex_str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1679
  DBUG_ENTER("mysqld_show");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1680

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1681 1682
  field_list.push_back(new Item_empty_string("Variable_name",30));
  field_list.push_back(new Item_empty_string("Value",256));
1683
  if (protocol->send_fields(&field_list,1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1684
    DBUG_RETURN(1); /* purecov: inspected */
1685
  null_lex_str.str= 0;				// For sys_var->value_ptr()
1686
  null_lex_str.length= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1687

1688
  pthread_mutex_lock(mutex);
1689
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1690
  {
1691
    if (!(wild && wild[0] && wild_case_compare(system_charset_info,
1692
					       variables->name,wild)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1693
    {
1694
      protocol->prepare_for_resend();
1695
      protocol->store(variables->name, system_charset_info);
1696 1697
      SHOW_TYPE show_type=variables->type;
      char *value=variables->value;
1698 1699 1700
      const char *pos, *end;
      long nr;

1701 1702 1703
      if (show_type == SHOW_SYS)
      {
	show_type= ((sys_var*) value)->type();
1704 1705
	value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type,
							 &null_lex_str);
1706 1707
      }

1708
      pos= end= buff;
1709
      switch (show_type) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1710 1711
      case SHOW_LONG:
      case SHOW_LONG_CONST:
1712
	end= int10_to_str(*(long*) value, buff, 10);
1713 1714
        break;
      case SHOW_LONGLONG:
1715
	end= longlong10_to_str(*(longlong*) value, buff, 10);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1716
	break;
1717
      case SHOW_HA_ROWS:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1718
        end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
1719
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1720
      case SHOW_BOOL:
1721
	end= strmov(buff, *(bool*) value ? "ON" : "OFF");
1722
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1723
      case SHOW_MY_BOOL:
1724
	end= strmov(buff, *(my_bool*) value ? "ON" : "OFF");
1725
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1726 1727
      case SHOW_INT_CONST:
      case SHOW_INT:
1728
	end= int10_to_str((long) *(uint32*) value, buff, 10);
1729
        break;
1730 1731
      case SHOW_HAVE:
      {
1732
	SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
1733 1734
	pos= show_comp_option_name[(int) tmp];
	end= strend(pos);
1735 1736
        break;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1737
      case SHOW_CHAR:
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1738 1739 1740 1741
      {
        if (!(pos= value))
          pos= "";
        end= strend(pos);
1742
        break;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1743
       }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1744
      case SHOW_STARTTIME:
1745 1746
	nr= (long) (thd->query_start() - start_time);
	end= int10_to_str(nr, buff, 10);
1747
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1748
      case SHOW_QUESTION:
1749
	end= int10_to_str((long) thd->query_id, buff, 10);
1750
        break;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1751
#ifdef HAVE_REPLICATION
1752
      case SHOW_RPL_STATUS:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
1753
	end= strmov(buff, rpl_status_type[(int)rpl_status]);
1754
	break;
1755 1756 1757
      case SHOW_SLAVE_RUNNING:
      {
	LOCK_ACTIVE_MI;
1758 1759
	end= strmov(buff, (active_mi->slave_running &&
			   active_mi->rli.slave_running) ? "ON" : "OFF");
1760 1761 1762
	UNLOCK_ACTIVE_MI;
	break;
      }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1763
#endif /* HAVE_REPLICATION */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1764
      case SHOW_OPENTABLES:
1765
	end= int10_to_str((long) cached_tables(), buff, 10);
1766
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1767
      case SHOW_CHAR_PTR:
1768
      {
1769 1770 1771 1772
        if (!(pos= *(char**) value))
          pos= "";
        end= strend(pos);
        break;
1773
      }
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1774
#ifdef HAVE_OPENSSL
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1775
	/* First group - functions relying on CTX */
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1776
      case SHOW_SSL_CTX_SESS_ACCEPT:
1777 1778
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_accept(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1779
						      ssl_context)),
1780
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1781 1782
        break;
      case SHOW_SSL_CTX_SESS_ACCEPT_GOOD:
1783 1784
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_accept_good(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1785
							   ssl_context)),
1786
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1787
        break;
1788
      case SHOW_SSL_CTX_SESS_CONNECT_GOOD:
1789 1790
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_connect_good(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1791
							    ssl_context)),
1792
			  buff, 10);
1793
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1794
      case SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE:
1795
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1796
				  SSL_CTX_sess_accept_renegotiate(ssl_acceptor_fd->ssl_context)),
1797
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1798
        break;
1799
      case SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE:
1800
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1801
				  SSL_CTX_sess_connect_renegotiate(ssl_acceptor_fd-> ssl_context)),
1802
			  buff, 10);
1803
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1804
      case SHOW_SSL_CTX_SESS_CB_HITS:
1805 1806
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_cb_hits(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1807
						       ssl_context)),
1808
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1809
        break;
1810
      case SHOW_SSL_CTX_SESS_HITS:
1811 1812
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_hits(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1813
						    ssl_context)),
1814
			  buff, 10);
1815 1816
        break;
      case SHOW_SSL_CTX_SESS_CACHE_FULL:
1817 1818
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_cache_full(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1819
							  ssl_context)),
1820
			  buff, 10);
1821 1822
        break;
      case SHOW_SSL_CTX_SESS_MISSES:
1823 1824
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
				  SSL_CTX_sess_misses(ssl_acceptor_fd->
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1825
						      ssl_context)),
1826
			  buff, 10);
1827 1828
        break;
      case SHOW_SSL_CTX_SESS_TIMEOUTS:
1829
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1830
				  SSL_CTX_sess_timeouts(ssl_acceptor_fd->ssl_context)),
1831
			  buff,10);
1832
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1833
      case SHOW_SSL_CTX_SESS_NUMBER:
1834
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1835
				  SSL_CTX_sess_number(ssl_acceptor_fd->ssl_context)),
1836
			  buff,10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1837
        break;
1838
      case SHOW_SSL_CTX_SESS_CONNECT:
1839
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1840
				  SSL_CTX_sess_connect(ssl_acceptor_fd->ssl_context)),
1841
			  buff,10);
1842
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1843
      case SHOW_SSL_CTX_SESS_GET_CACHE_SIZE:
1844
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1845
				  SSL_CTX_sess_get_cache_size(ssl_acceptor_fd->ssl_context)),
1846
				  buff,10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1847 1848
        break;
      case SHOW_SSL_CTX_GET_VERIFY_MODE:
1849
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1850
				  SSL_CTX_get_verify_mode(ssl_acceptor_fd->ssl_context)),
1851
			  buff,10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1852 1853
        break;
      case SHOW_SSL_CTX_GET_VERIFY_DEPTH:
1854
	end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1855
				  SSL_CTX_get_verify_depth(ssl_acceptor_fd->ssl_context)),
1856
			  buff,10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1857 1858
        break;
      case SHOW_SSL_CTX_GET_SESSION_CACHE_MODE:
1859 1860
	if (!ssl_acceptor_fd)
	{
1861 1862
	  pos= "NONE";
	  end= pos+4;
1863 1864
	  break;
	}
1865
	switch (SSL_CTX_get_session_cache_mode(ssl_acceptor_fd->ssl_context))
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1866 1867
	{
          case SSL_SESS_CACHE_OFF:
1868
            pos= "OFF";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1869 1870
	    break;
          case SSL_SESS_CACHE_CLIENT:
1871
            pos= "CLIENT";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1872 1873
	    break;
          case SSL_SESS_CACHE_SERVER:
1874
            pos= "SERVER";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1875 1876
	    break;
          case SSL_SESS_CACHE_BOTH:
1877
            pos= "BOTH";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1878 1879
	    break;
          case SSL_SESS_CACHE_NO_AUTO_CLEAR:
1880
            pos= "NO_AUTO_CLEAR";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1881 1882
	    break;
          case SSL_SESS_CACHE_NO_INTERNAL_LOOKUP:
1883
            pos= "NO_INTERNAL_LOOKUP";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1884 1885
	    break;
	  default:
1886
            pos= "Unknown";
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1887 1888
	    break;
	}
1889
	end= strend(pos);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1890
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1891 1892
	/* First group - functions relying on SSL */
      case SHOW_SSL_GET_VERSION:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1893 1894
	pos= (thd->net.vio->ssl_arg ?
	      SSL_get_version((SSL*) thd->net.vio->ssl_arg) : "");
1895
	end= strend(pos);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1896 1897
        break;
      case SHOW_SSL_SESSION_REUSED:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1898 1899 1900 1901 1902
	end= int10_to_str((long) (thd->net.vio->ssl_arg ?
				  SSL_session_reused((SSL*) thd->net.vio->
						     ssl_arg) :
				  0),
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1903 1904
        break;
      case SHOW_SSL_GET_DEFAULT_TIMEOUT:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1905 1906 1907 1908 1909
	end= int10_to_str((long) (thd->net.vio->ssl_arg ?
				  SSL_get_default_timeout((SSL*) thd->net.vio->
							  ssl_arg) :
				  0),
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1910 1911
        break;
      case SHOW_SSL_GET_VERIFY_MODE:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1912 1913 1914 1915 1916
	end= int10_to_str((long) (thd->net.vio->ssl_arg ?
				  SSL_get_verify_mode((SSL*) thd->net.vio->
						      ssl_arg):
				  0),
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1917 1918
        break;
      case SHOW_SSL_GET_VERIFY_DEPTH:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1919 1920 1921 1922 1923
	end= int10_to_str((long) (thd->net.vio->ssl_arg ?
				  SSL_get_verify_depth((SSL*) thd->net.vio->
						       ssl_arg):
				  0),
			  buff, 10);
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1924 1925
        break;
      case SHOW_SSL_GET_CIPHER:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1926 1927
	pos= (thd->net.vio->ssl_arg ?
	      SSL_get_cipher((SSL*) thd->net.vio->ssl_arg) : "" );
1928
	end= strend(pos);
1929
	break;
1930
      case SHOW_SSL_GET_CIPHER_LIST:
1931
	if (thd->net.vio->ssl_arg)
1932
	{
1933
	  char *to= buff;
1934
	  for (int i=0 ; i++ ;)
1935
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1936
	    const char *p= SSL_get_cipher_list((SSL*) thd->net.vio->ssl_arg,i);
1937 1938
	    if (p == NULL) 
	      break;
1939 1940
	    to= strmov(to, p);
	    *to++= ':';
1941
	  }
1942 1943 1944
	  if (to != buff)
	    to--;				// Remove last ':'
	  end= to;
1945
        }
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1946
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1947 1948

#endif /* HAVE_OPENSSL */
1949 1950 1951 1952
      case SHOW_KEY_CACHE_LONG:
	value= (value-(char*) &dflt_key_cache_var)+ (char*) sql_key_cache;
	end= int10_to_str(*(long*) value, buff, 10);
        break;
1953 1954
      case SHOW_UNDEF:				// Show never happen
      case SHOW_SYS:
1955
	break;					// Return empty string
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1956 1957
      default:
	break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1958
      }
1959
      if (protocol->store(pos, (uint32) (end - pos), system_charset_info) ||
1960
	  protocol->write())
1961
        goto err;                               /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1962 1963
    }
  }
1964
  pthread_mutex_unlock(mutex);
1965
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1966 1967 1968
  DBUG_RETURN(0);

 err:
1969
  pthread_mutex_unlock(mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1970 1971 1972 1973
  DBUG_RETURN(1);
}

#ifdef __GNUC__
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
1974
template class List_iterator_fast<char>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1975 1976
template class List<char>;
#endif