sql_show.cc 46.1 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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 44 45
store_create_info(THD *thd, TABLE *table, String *packet);

static void
append_identifier(THD *thd, String *packet, const char *name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
46

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
47 48
extern struct st_VioSSLAcceptorFd * ssl_acceptor_fd;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/****************************************************************************
** Send list of databases
** A database is a directory in the mysql_data_home directory
****************************************************************************/


int
mysqld_show_dbs(THD *thd,const char *wild)
{
  Item_string *field=new Item_string("",0);
  List<Item> field_list;
  char *end;
  List<char> files;
  char *file_name;
  DBUG_ENTER("mysqld_show_dbs");

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

  if (send_fields(thd,field_list,1))
    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
76
  List_iterator_fast<char> it(files);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
77 78
  while ((file_name=it++))
  {
79
    if (thd->master_access & (DB_ACLS | SHOW_DB_ACL) ||
80 81 82
	acl_get(thd->host, thd->ip, (char*) &thd->remote.sin_addr,
		thd->priv_user, file_name) ||
	(grant_option && !check_grant_db(thd, file_name)))
83
    {
84
      thd->packet.length(0);
85
      net_store_data(&thd->packet, thd->variables.convert_set, file_name);
86 87 88 89
      if (my_net_write(&thd->net, (char*) thd->packet.ptr(),
		       thd->packet.length()))
	DBUG_RETURN(-1);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
90 91 92 93 94
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}

95 96 97 98
/***************************************************************************
** List all open tables in a database
***************************************************************************/

99
int mysqld_show_open_tables(THD *thd,const char *wild)
100 101
{
  List<Item> field_list;
102
  OPEN_TABLE_LIST *open_list;
103
  CONVERT *convert=thd->variables.convert_set;
104 105
  DBUG_ENTER("mysqld_show_open_tables");

106 107 108 109
  field_list.push_back(new Item_empty_string("Database",NAME_LEN));
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
  field_list.push_back(new Item_int("In_use",0, 4));
  field_list.push_back(new Item_int("Name_locked",0, 4));
110 111 112

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
113

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
114
  if (!(open_list=list_open_tables(thd,wild)) && thd->fatal_error)
115 116
    DBUG_RETURN(-1);

117
  for (; open_list ; open_list=open_list->next)
118 119
  {
    thd->packet.length(0);
120 121 122 123
    net_store_data(&thd->packet,convert, open_list->db);
    net_store_data(&thd->packet,convert, open_list->table);
    net_store_data(&thd->packet,open_list->in_use);
    net_store_data(&thd->packet,open_list->locked);
124
    if (my_net_write(&thd->net,(char*) thd->packet.ptr(),thd->packet.length()))
125
    {
126
      DBUG_RETURN(-1);
127
    }
128 129 130 131 132
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}

133

bk@work.mysql.com's avatar
bk@work.mysql.com committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
/***************************************************************************
** 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)
{
  Item_string *field=new Item_string("",0);
  List<Item> field_list;
  char path[FN_LEN],*end;
  List<char> files;
  char *file_name;
  DBUG_ENTER("mysqld_show_tables");

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 156 157 158 159
  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);
  if (send_fields(thd,field_list,1))
    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 163
  while ((file_name=it++))
  {
    thd->packet.length(0);
164
    net_store_data(&thd->packet, thd->variables.convert_set, file_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
165 166 167 168 169 170 171 172 173 174
    if (my_net_write(&thd->net,(char*) thd->packet.ptr(),thd->packet.length()))
      DBUG_RETURN(-1);
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


static int
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
175
                 const char *wild, bool dir)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
176 177 178 179 180 181 182 183 184
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
  uint col_access=thd->col_access;
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

185 186
  if (wild && !wild[0])
    wild=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
187 188 189 190 191
  bzero((char*) &table_list,sizeof(table_list));

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

192
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
193 194 195
  {
    file=dirp->dir_entry+i;
    if (dir)
196
    {                                           /* Return databases */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
197 198 199
#ifdef USE_SYMDIR
      char *ext;
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
200 201 202 203
      {
	/* Only show the sym file if it points to a directory */
	char buff[FN_REFLEN], *end;
	MY_STAT status;
204
        *ext=0;                                 /* Remove extension */
205 206 207 208 209 210 211 212
	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
213 214 215
      else
#endif
      {
dlenev@mysql.com's avatar
dlenev@mysql.com committed
216
        if (file->name[0] == '.' || !MY_S_ISDIR(file->mystat->st_mode) ||
217
            (wild && wild_compare(file->name,wild)))
218
          continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
219 220 221 222
      }
    }
    else
    {
223
        // Return only .frm files which aren't temp files.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
224
      if (my_strcasecmp(ext=fn_ext(file->name),reg_ext) ||
225
          is_prefix(file->name,tmp_file_prefix))
226
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
227
      *ext=0;
228 229 230 231 232 233 234 235 236 237
      if (wild)
      {
	if (lower_case_table_names)
	{
	  if (wild_case_compare(file->name,wild))
	    continue;
	}
	else if (wild_compare(file->name,wild))
	  continue;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
238 239 240 241 242 243 244
    }
    /* 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
245
      if (check_grant(thd,TABLE_ACLS,&table_list,1,1))
246
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
247
    }
248
    if (files->push_back(thd->strdup(file->name)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    {
      my_dirend(dirp);
      DBUG_RETURN(-1);
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
  DBUG_RETURN(0);
}

/***************************************************************************
** Extended version of mysqld_show_tables
***************************************************************************/

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;
  String *packet= &thd->packet;
272
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  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));
  item->maybe_null=1;
  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;
  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));
307
  item->maybe_null=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
308 309 310 311 312
  if (send_fields(thd,field_list,1))
    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
313
  List_iterator_fast<char> it(files);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
314 315 316 317 318
  while ((file_name=it++))
  {
    TABLE_LIST table_list;
    bzero((char*) &table_list,sizeof(table_list));
    packet->length(0);
319
    net_store_data(packet,convert, file_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
320
    table_list.db=(char*) db;
321
    table_list.real_name= table_list.alias= file_name;
322 323
    if (lower_case_table_names)
      casedn_str(file_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
324 325
    if (!(table = open_ltable(thd, &table_list, TL_READ)))
    {
326
      for (uint i=2 ; i < field_list.elements ; i++)
327
        net_store_null(packet);
328
      net_store_data(packet,convert, thd->net.last_error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
329 330 331 332 333 334 335
      thd->net.last_error[0]=0;
    }
    else
    {
      struct tm tm_tmp;
      handler *file=table->file;
      file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_NO_LOCK);
336 337
      net_store_data(packet, convert, file->table_type());
      net_store_data(packet, convert,
338 339
                     (table->db_options_in_use & HA_OPTION_COMPRESS_RECORD) ?
		     "Compressed" :
340
                     (table->db_options_in_use & HA_OPTION_PACK_RECORD) ?
341
                     "Dynamic" : "Fixed");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
342 343 344 345
      net_store_data(packet, (longlong) file->records);
      net_store_data(packet, (uint32) file->mean_rec_length);
      net_store_data(packet, (longlong) file->data_file_length);
      if (file->max_data_file_length)
346
        net_store_data(packet, (longlong) file->max_data_file_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
347
      else
348
        net_store_null(packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
349 350 351 352
      net_store_data(packet, (longlong) file->index_file_length);
      net_store_data(packet, (longlong) file->delete_length);
      if (table->found_next_number_field)
      {
353 354 355 356 357
        table->next_number_field=table->found_next_number_field;
        table->next_number_field->reset();
        file->update_auto_increment();
        net_store_data(packet, table->next_number_field->val_int());
        table->next_number_field=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
358 359
      }
      else
360
        net_store_null(packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
361
      if (!file->create_time)
362
        net_store_null(packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
363 364
      else
      {
365 366
        localtime_r(&file->create_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
367 368
      }
      if (!file->update_time)
369
        net_store_null(packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
370 371
      else
      {
372 373
        localtime_r(&file->update_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
374 375
      }
      if (!file->check_time)
376
        net_store_null(packet);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
377 378
      else
      {
379 380
        localtime_r(&file->check_time,&tm_tmp);
        net_store_data(packet, &tm_tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
381 382
      }
      {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        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)
409
          ptr=strxmov(ptr, " row_format=", ha_row_type[(uint) table->row_type],
410 411 412 413 414 415 416 417
                      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);
        }
418
        net_store_data(packet, convert, option_buff+1,
419
                       (ptr == option_buff ? 0 : (uint) (ptr-option_buff)-1));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
420
      }
421 422 423 424 425 426
      {
	char *comment=table->file->update_table_comment(table->comment);
	net_store_data(packet, comment);
	if (comment != table->comment)
	  my_free(comment,MYF(0));
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
427 428 429
      close_thread_tables(thd,0);
    }
    if (my_net_write(&thd->net,(char*) packet->ptr(),
430
                     packet->length()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
431 432 433 434 435 436 437 438 439
      DBUG_RETURN(-1);
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}



/***************************************************************************
440
** List all columns in a table_list->real_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
441 442 443
***************************************************************************/

int
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
444 445
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
446 447 448 449
{
  TABLE *table;
  handler *file;
  char tmp[MAX_FIELD_WIDTH];
450
  Item *item;
451
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
452 453
  DBUG_ENTER("mysqld_show_fields");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
454
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }
  file=table->file;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
  (void) get_table_grant(thd, table_list);

  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));
  field_list.push_back(new Item_empty_string("Null",1));
  field_list.push_back(new Item_empty_string("Key",3));
470 471
  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
472
  field_list.push_back(new Item_empty_string("Extra",20));
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
473 474
  if (verbose)
    field_list.push_back(new Item_empty_string("Privileges",80));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
475

476
        // Send first number of fields and records
bk@work.mysql.com's avatar
bk@work.mysql.com committed
477 478 479 480 481 482 483 484 485
  {
    char *pos;
    pos=net_store_length(tmp, (uint) field_list.elements);
    pos=net_store_length(pos,(ulonglong) file->records);
    (void) my_net_write(&thd->net,tmp,(uint) (pos-tmp));
  }

  if (send_fields(thd,field_list,0))
    DBUG_RETURN(1);
486
  restore_record(table,2);      // Get empty record
bk@work.mysql.com's avatar
bk@work.mysql.com committed
487 488

  Field **ptr,*field;
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
489
  String *packet= &thd->packet;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
490 491 492 493 494
  for (ptr=table->field; (field= *ptr) ; ptr++)
  {
    if (!wild || !wild[0] || !wild_case_compare(field->field_name,wild))
    {
      {
495 496
        byte *pos;
        uint flags=field->flags;
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
497
        String type(tmp,sizeof(tmp));
498 499 500 501
        uint col_access;
        bool null_default_value=0;

        packet->length(0);
502
        net_store_data(packet,convert,field->field_name);
503
        field->sql_type(type);
504
        net_store_data(packet,convert,type.ptr(),type.length());
505

506 507 508 509 510 511
        /*
          Altough TIMESTAMP fields can't contain NULL as its value they
          will accept NULL if you will try to insert such value and will
          convert it to current TIMESTAMP. So YES here means that NULL 
          is allowed for assignment but can't be returned.
        */
512 513 514
        pos=(byte*) ((flags & NOT_NULL_FLAG) &&
                     field->type() != FIELD_TYPE_TIMESTAMP ?
                     "" : "YES");
515
        net_store_data(packet,convert,(const char*) pos);
516 517 518
        pos=(byte*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
                     (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
                     (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
519
        net_store_data(packet,convert,(char*) pos);
520

521 522 523 524 525
        /*
          We handle first TIMESTAMP column in special way because its
          default value is ignored and current timestamp used instead.
        */
        if (table->timestamp_field == field ||
526 527 528 529 530 531
            field->unireg_check == Field::NEXT_NUMBER)
          null_default_value=1;
        if (!null_default_value && !field->is_null())
        {                                               // Not null by default
          type.set(tmp,sizeof(tmp));
          field->val_str(&type,&type);
532
          net_store_data(packet,convert,type.ptr(),type.length());
533 534 535 536
        }
        else if (field->maybe_null() || null_default_value)
          net_store_null(packet);                       // Null as default
        else
537
          net_store_data(packet,convert,tmp,0);
538 539 540 541

        char *end=tmp;
        if (field->unireg_check == Field::NEXT_NUMBER)
          end=strmov(tmp,"auto_increment");
542
        net_store_data(packet,convert,tmp,(uint) (end-tmp));
543

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
544 545 546 547 548 549 550 551 552 553 554 555 556
	if (verbose)
	{
	  /* Add grant options */
	  col_access= get_column_grant(thd,table_list,field) & COL_ACLS;
	  end=tmp;
	  for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
	  {
	    if (col_access & 1)
	    {
	      *end++=',';
	      end=strmov(end,grant_types.type_names[bitnr]);
	    }
	  }
557
	  net_store_data(packet,convert, tmp+1,end == tmp ? 0 : (uint) (end-tmp-1));
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
558
	}
559 560
        if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
          DBUG_RETURN(1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
561 562 563 564 565 566 567 568 569 570 571
      }
    }
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}

int
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
572
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
573 574
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
575
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
576

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
577
  /* Only one table for now */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
578 579 580 581 582 583
  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }

584 585
  char buff[1024];
  String packet(buff,sizeof(buff));
586
  packet.length(0);
serg@serg.mylan's avatar
serg@serg.mylan committed
587
  net_store_data(&packet,convert, table->table_name);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
  /*
    A hack - we need to reserve some space for the length before
    we know what it is - let's assume that the length of create table
    statement will fit into 3 bytes ( 16 MB max :-) )
  */
  ulong store_len_offset = packet.length();
  packet.length(store_len_offset + 4);
  if (store_create_info(thd, table, &packet))
    DBUG_RETURN(-1);
  ulong create_len = packet.length() - store_len_offset - 4;
  if (create_len > 0x00ffffff) // better readable in HEX ...
  {
    /*
      Just in case somebody manages to create a table
      with *that* much stuff in the definition
    */
    DBUG_RETURN(1);
  }

  /*
    Now we have to store the length in three bytes, even if it would fit
    into fewer bytes, so we cannot use net_store_data() anymore,
    and do it ourselves
  */
  char* p = (char*)packet.ptr() + store_len_offset;
  *p++ = (char) 253; // The client the length is stored using 3-bytes
  int3store(p, create_len);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
616 617
  List<Item> field_list;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
618 619
  field_list.push_back(new Item_empty_string("Create Table",
        max(packet.length(),1024))); // 1024 is for not to confuse old clients
bk@work.mysql.com's avatar
bk@work.mysql.com committed
620 621 622

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);
623

624 625
  if (my_net_write(&thd->net, (char*)packet.ptr(), packet.length()))
    DBUG_RETURN(1);
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
626

bk@work.mysql.com's avatar
bk@work.mysql.com committed
627 628 629 630 631
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
632 633 634 635 636 637 638 639 640 641 642 643 644
int
mysqld_show_logs(THD *thd)
{
  DBUG_ENTER("mysqld_show_logs");

  List<Item> field_list;
  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));

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);

645
#ifdef HAVE_BERKELEY_DB
646
  if (!berkeley_skip && berkeley_show_logs(thd))
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
647
    DBUG_RETURN(-1);
648
#endif
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
649 650 651 652 653 654

  send_eof(&thd->net);
  DBUG_RETURN(0);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
655 656 657 658 659
int
mysqld_show_keys(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
  char buff[256];
660
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
661 662
  DBUG_ENTER("mysqld_show_keys");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
663
                      table_list->real_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

  if (!(table = open_ltable(thd, table_list, TL_UNLOCK)))
  {
    send_error(&thd->net);
    DBUG_RETURN(1);
  }

  List<Item> field_list;
  Item *item;
  field_list.push_back(new Item_empty_string("Table",NAME_LEN));
  field_list.push_back(new Item_int("Non_unique",0,1));
  field_list.push_back(new Item_empty_string("Key_name",NAME_LEN));
  field_list.push_back(new Item_int("Seq_in_index",0,2));
  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;
680
  field_list.push_back(item=new Item_int("Cardinality",0,21));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
681 682 683 684 685
  item->maybe_null=1;
  field_list.push_back(item=new Item_int("Sub_part",0,3));
  item->maybe_null=1;
  field_list.push_back(item=new Item_empty_string("Packed",10));
  item->maybe_null=1;
686 687
  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
688 689 690 691 692 693
  field_list.push_back(new Item_empty_string("Comment",255));
  item->maybe_null=1;

  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1);

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
694
  String *packet= &thd->packet;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
695 696 697 698 699 700 701 702 703
  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;
    char *end;
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
      packet->length(0);
704 705 706
      net_store_data(packet,convert,table->table_name);
      net_store_data(packet,convert,((key_info->flags & HA_NOSAME) ? "0" :"1"), 1);
      net_store_data(packet,convert,key_info->name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
707
      end=int10_to_str((long) (j+1),(char*) buff,10);
708 709 710
      net_store_data(packet,convert,buff,(uint) (end-buff));
      net_store_data(packet,convert,
		     key_part->field ? key_part->field->field_name :
711
                     "?unknown field?");
712
      if (table->file->index_flags(i) & HA_READ_ORDER)
713 714 715
        net_store_data(packet,convert,
		       ((key_part->key_part_flag & HA_REVERSE_SORT) ?
			"D" : "A"), 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
716
      else
717
        net_store_null(packet); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
718 719 720
      KEY *key=table->key_info+i;
      if (key->rec_per_key[j])
      {
721 722
        ha_rows records=(table->file->records / key->rec_per_key[j]);
        end=longlong10_to_str((longlong) records, buff, 10);
723
        net_store_data(packet,convert,buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
724 725
      }
      else
726
        net_store_null(packet);
727 728

      /* 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
729
      if (!key_part->field ||
730 731
          key_part->length !=
          table->field[key_part->fieldnr-1]->key_length())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
732
      {
733
        end=int10_to_str((long) key_part->length, buff,10); /* purecov: inspected */
734
        net_store_data(packet,convert,buff,(uint) (end-buff)); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
735 736
      }
      else
737 738
        net_store_null(packet);
      net_store_null(packet);                   // No pack_information yet
739 740 741

      /* Null flag */
      uint flags= key_part->field ? key_part->field->flags : 0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
742
      char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
743 744 745
      net_store_data(packet,convert,(const char*) pos);
      net_store_data(packet,convert,table->file->index_type(i));
      /* Comment */
746 747 748 749
      if (!(table->keys_in_use & ((key_map) 1 << i)))
	net_store_data(packet,convert,"disabled",8);
      else
	net_store_data(packet,convert,"");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750
      if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
751
        DBUG_RETURN(1); /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    }
  }
  send_eof(&thd->net);
  DBUG_RETURN(0);
}


/****************************************************************************
** Return only fields for API mysql_list_fields
** Use "show table wildcard" in mysql instead of this
****************************************************************************/

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)))
  {
    send_error(&thd->net);
    DBUG_VOID_RETURN;
  }
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
    if (!wild || !wild[0] || !wild_case_compare(field->field_name,wild))
      field_list.push_back(new Item_field(field));
  }
784
  restore_record(table,2);              // Get empty record
bk@work.mysql.com's avatar
bk@work.mysql.com committed
785 786 787 788 789 790 791 792 793
  if (send_fields(thd,field_list,2))
    DBUG_VOID_RETURN;
  VOID(net_flush(&thd->net));
  DBUG_VOID_RETURN;
}

int
mysqld_dump_create_info(THD *thd, TABLE *table, int fd)
{
794
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
795 796
  DBUG_ENTER("mysqld_dump_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));
797

bk@work.mysql.com's avatar
bk@work.mysql.com committed
798 799
  String* packet = &thd->packet;
  packet->length(0);
800
  if (store_create_info(thd,table,packet))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
801
    DBUG_RETURN(-1);
802

803 804 805
  if (convert)
    convert->convert((char*) packet->ptr(), packet->length());
  if (fd < 0)
806
  {
807
    if (my_net_write(&thd->net, (char*)packet->ptr(), packet->length()))
808 809 810
      DBUG_RETURN(-1);
    VOID(net_flush(&thd->net));
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
811
  else
812
  {
813 814
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
815 816
      DBUG_RETURN(-1);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
817 818
  DBUG_RETURN(0);
}
819

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static void
append_identifier(THD *thd, String *packet, const char *name)
{
  if (thd->options & OPTION_QUOTE_SHOW_CREATE)
  {
    packet->append("`", 1);
    packet->append(name);
    packet->append("`", 1);
  }
  else
  {
    packet->append(name);
  }
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853

/* 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;
  if (filename && !(thd->sql_mode & MODE_NO_DIR_IN_CREATE))
  {
    length= dirname_length(filename);
    packet->append(' ');
    packet->append(dir_type);
    packet->append(" DIRECTORY='", 12);
    packet->append(filename, length);
    packet->append('\'');
  }
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
854
static int
855
store_create_info(THD *thd, TABLE *table, String *packet)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
856
{
857 858 859 860 861 862 863 864
  List<Item> field_list;
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end;
  String type(tmp, sizeof(tmp));
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
  handler *file= table->file;
  HA_CREATE_INFO create_info;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
865 866 867 868
  DBUG_ENTER("store_create_info");
  DBUG_PRINT("enter",("table: %s",table->real_name));

  restore_record(table,2); // Get empty record
869 870 871 872
  if (table->tmp_table)
    packet->append("CREATE TEMPORARY TABLE ", 23);
  else
    packet->append("CREATE TABLE ", 13);
873 874 875
  append_identifier(thd,packet,
		    (lower_case_table_names == 2 ? table->table_name :
		     table->real_name));
876
  packet->append(" (\n", 3);
877

bk@work.mysql.com's avatar
bk@work.mysql.com committed
878 879
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
880 881 882
    bool has_default;
    uint flags = field->flags;

883
    if (ptr != table->field)
884 885
      packet->append(",\n", 2);
    packet->append("  ", 2);
886
    append_identifier(thd,packet,field->field_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
887 888
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
889
    if (type.ptr() != tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
890
      type.set(tmp, sizeof(tmp));
891

bk@work.mysql.com's avatar
bk@work.mysql.com committed
892 893
    field->sql_type(type);
    packet->append(type.ptr(),type.length());
894

895
    has_default= (field->type() != FIELD_TYPE_BLOB &&
896
		  table->timestamp_field != field &&
897
		  field->unireg_check != Field::NEXT_NUMBER);
898 899
    if (flags & NOT_NULL_FLAG)
      packet->append(" NOT NULL", 9);
900

901
    if (has_default)
902 903
    {
      packet->append(" default ", 9);
904
      if (!field->is_null())
905 906 907 908
      {                                             // Not null by default
        type.set(tmp,sizeof(tmp));
        field->val_str(&type,&type);
        packet->append('\'');
909
	if (type.length())
910
          append_unescaped(packet, type.c_ptr());
911
        packet->append('\'');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
912
      }
913
      else if (field->maybe_null())
914 915 916 917
        packet->append("NULL", 4);                    // Null as default
      else
        packet->append(tmp,0);
    }
918

bk@work.mysql.com's avatar
bk@work.mysql.com committed
919
    if (field->unireg_check == Field::NEXT_NUMBER)
920
          packet->append(" auto_increment", 15 );
bk@work.mysql.com's avatar
bk@work.mysql.com committed
921 922
  }

923 924
  key_info= table->key_info;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
925
  bzero((char*) &create_info, sizeof(create_info));
926 927
  file->update_create_info(&create_info);
  primary_key= table->primary_key;
928

bk@work.mysql.com's avatar
bk@work.mysql.com committed
929 930
  for (uint i=0 ; i < table->keys ; i++,key_info++)
  {
931 932
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
933
    packet->append(",\n  ", 4);
934

935 936 937
    if (i == primary_key && !strcmp(key_info->name,"PRIMARY"))
    {
      found_primary=1;
938
      packet->append("PRIMARY ", 8);
939
    }
940
    else if (key_info->flags & HA_NOSAME)
941
      packet->append("UNIQUE ", 7);
942
    else if (key_info->flags & HA_FULLTEXT)
943 944
      packet->append("FULLTEXT ", 9);
    packet->append("KEY ", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
945

946
    if (!found_primary)
947
     append_identifier(thd,packet,key_info->name);
948

949
    packet->append(" (", 2);
950

bk@work.mysql.com's avatar
bk@work.mysql.com committed
951 952
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
953
      if (j)
954
        packet->append(',');
955

956
      if (key_part->field)
957
        append_identifier(thd,packet,key_part->field->field_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
958
      if (!key_part->field ||
959 960 961
          (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
962
      {
963 964 965 966
        buff[0] = '(';
        char* end=int10_to_str((long) key_part->length, buff + 1,10);
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
967 968 969 970
      }
    }
    packet->append(')');
  }
971

972 973 974 975
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
976

977 978 979 980
  if ((for_str= file->get_foreign_key_create_info()))
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
981 982 983
  }

  packet->append("\n)", 2);
984
  packet->append(" TYPE=", 6);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
985
  packet->append(file->table_type());
986

987
  if (table->min_rows)
988 989
  {
    packet->append(" MIN_ROWS=");
990 991
    end= longlong10_to_str(table->min_rows, buff, 10);
    packet->append(buff, (uint) (end- buff));
992
  }
993
  if (table->max_rows)
994 995
  {
    packet->append(" MAX_ROWS=");
996 997
    end= longlong10_to_str(table->max_rows, buff, 10);
    packet->append(buff, (uint) (end - buff));
998
  }
999 1000 1001
  if (table->avg_row_length)
  {
    packet->append(" AVG_ROW_LENGTH=");
1002 1003
    end= longlong10_to_str(table->avg_row_length, buff,10);
    packet->append(buff, (uint) (end - buff));
1004
  }
1005

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1006
  if (table->db_create_options & HA_OPTION_PACK_KEYS)
1007
    packet->append(" PACK_KEYS=1", 12);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1008
  if (table->db_create_options & HA_OPTION_NO_PACK_KEYS)
1009
    packet->append(" PACK_KEYS=0", 12);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1010
  if (table->db_create_options & HA_OPTION_CHECKSUM)
1011
    packet->append(" CHECKSUM=1", 11);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1012
  if (table->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1013
    packet->append(" DELAY_KEY_WRITE=1",18);
1014 1015 1016 1017 1018
  if (table->row_type != ROW_TYPE_DEFAULT)
  {
    packet->append(" ROW_FORMAT=",12);
    packet->append(ha_row_type[(uint) table->row_type]);
  }
1019
  table->file->append_create_info(packet);
monty@donna.mysql.com's avatar
merge  
monty@donna.mysql.com committed
1020
  if (table->comment && table->comment[0])
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1021 1022 1023 1024 1025
  {
    packet->append(" COMMENT='", 10);
    append_unescaped(packet, table->comment);
    packet->append('\'');
  }
1026 1027
  if (file->raid_type)
  {
1028 1029 1030 1031 1032 1033
    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);
1034
  }
1035 1036
  append_directory(thd, packet, "DATA",  create_info.data_file_name);
  append_directory(thd, packet, "INDEX", create_info.index_file_name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  DBUG_RETURN(0);
}


/****************************************************************************
** Return info about all processes
** returns for each thread: thread id, user, host, db, command, info
****************************************************************************/

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

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1052 1053
  ulong thread_id;
  time_t start_time;
1054
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1055 1056 1057 1058 1059 1060 1061 1062
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

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

1063
#define LIST_PROCESS_HOST_LEN 64
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1064 1065 1066 1067 1068 1069

void mysqld_list_processes(THD *thd,const char *user, bool verbose)
{
  Item *field;
  List<Item> field_list;
  I_List<thread_info> thread_infos;
1070 1071 1072
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
  CONVERT *convert=thd->variables.convert_set;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1073 1074 1075 1076
  DBUG_ENTER("mysqld_list_processes");

  field_list.push_back(new Item_int("Id",0,7));
  field_list.push_back(new Item_empty_string("User",16));
1077
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  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));
  field_list.push_back(new Item_empty_string("Time",7));
  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;
  if (send_fields(thd,field_list,1))
    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++))
    {
1096
      struct st_my_thread_var *mysys_var;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1097
      if ((tmp->net.vio || tmp->system_thread) &&
1098
          (!user || (tmp->user && !strcmp(tmp->user,user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1099
      {
1100 1101 1102
        thread_info *thd_info=new thread_info;

        thd_info->thread_id=tmp->thread_id;
1103 1104 1105
        thd_info->user=thd->strdup(tmp->user ? tmp->user :
				   (tmp->system_thread ?
				    "system user" : "unauthenticated user"));
1106
	if (tmp->peer_port && (tmp->host || tmp->ip) && thd->host_or_ip[0])
1107 1108
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1109
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1110
			"%s:%u", tmp->host_or_ip, tmp->peer_port);
1111 1112
	}
	else
1113
	  thd_info->host= thd->strdup(tmp->host_or_ip);
1114 1115 1116
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1117 1118
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
        thd_info->proc_info= (char*) (tmp->killed ? "Killed" : 0);
        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);
1130 1131
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1132 1133

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1134 1135
        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
1136
#endif
1137 1138 1139
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1140
        thd_info->start_time= tmp->start_time;
1141
#endif
1142 1143 1144
        thd_info->query=0;
        if (tmp->query)
        {
1145 1146
	  /* query_length is always set before tmp->query */
          uint length= min(max_query_length, tmp->query_length);
1147 1148 1149 1150
          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
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
  String *packet= &thd->packet;
  while ((thd_info=thread_infos.get()))
  {
    char buff[20],*end;
    packet->length(0);
    end=int10_to_str((long) thd_info->thread_id, buff,10);
1163 1164 1165
    net_store_data(packet,convert,buff,(uint) (end-buff));
    net_store_data(packet,convert,thd_info->user);
    net_store_data(packet,convert,thd_info->host);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1166
    if (thd_info->db)
1167
      net_store_data(packet,convert,thd_info->db);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1168 1169 1170
    else
      net_store_null(packet);
    if (thd_info->proc_info)
1171
      net_store_data(packet,convert,thd_info->proc_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1172
    else
1173
      net_store_data(packet,convert,command_name[thd_info->command]);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1174
    if (thd_info->start_time)
1175 1176
      net_store_data(packet,
		     (uint32) (time((time_t*) 0) - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1177 1178 1179
    else
      net_store_null(packet);
    if (thd_info->state_info)
1180
      net_store_data(packet,convert,thd_info->state_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1181 1182 1183
    else
      net_store_null(packet);
    if (thd_info->query)
1184
      net_store_data(packet,convert,thd_info->query);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    else
      net_store_null(packet);
    if (my_net_write(&thd->net,(char*) packet->ptr(),packet->length()))
      break; /* purecov: inspected */
  }
  send_eof(&thd->net);
  DBUG_VOID_RETURN;
}


/*****************************************************************************
** Status functions
*****************************************************************************/


1200
int mysqld_show(THD *thd, const char *wild, show_var_st *variables,
1201 1202
		enum enum_var_type value_type,
		pthread_mutex_t *mutex)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1203 1204 1205 1206
{
  char buff[8192];
  String packet2(buff,sizeof(buff));
  List<Item> field_list;
1207
  CONVERT *convert=thd->variables.convert_set;
1208

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1209 1210 1211 1212 1213 1214
  DBUG_ENTER("mysqld_show");
  field_list.push_back(new Item_empty_string("Variable_name",30));
  field_list.push_back(new Item_empty_string("Value",256));
  if (send_fields(thd,field_list,1))
    DBUG_RETURN(1); /* purecov: inspected */

1215
  pthread_mutex_lock(mutex);
1216
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1217
  {
1218
    if (!(wild && wild[0] && wild_case_compare(variables->name,wild)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1219 1220
    {
      packet2.length(0);
1221 1222 1223 1224 1225 1226
      net_store_data(&packet2,convert,variables->name);
      SHOW_TYPE show_type=variables->type;
      char *value=variables->value;
      if (show_type == SHOW_SYS)
      {
	show_type= ((sys_var*) value)->type();
1227
	value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type);
1228 1229 1230
      }

      switch (show_type) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1231 1232
      case SHOW_LONG:
      case SHOW_LONG_CONST:
1233 1234 1235 1236
        net_store_data(&packet2,(uint32) *(ulong*) value);
        break;
      case SHOW_LONGLONG:
        net_store_data(&packet2,(longlong) *(longlong*) value);
1237
        break;
1238 1239 1240
      case SHOW_HA_ROWS:
        net_store_data(&packet2,(longlong) *(ha_rows*) value);
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1241
      case SHOW_BOOL:
1242
        net_store_data(&packet2,(ulong) *(bool*) value ? "ON" : "OFF");
1243
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1244
      case SHOW_MY_BOOL:
1245
        net_store_data(&packet2,(ulong) *(my_bool*) value ? "ON" : "OFF");
1246
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1247 1248
      case SHOW_INT_CONST:
      case SHOW_INT:
1249
        net_store_data(&packet2,(uint32) *(int*) value);
1250
        break;
1251 1252
      case SHOW_HAVE:
      {
1253
	SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
1254 1255 1256 1257 1258
        net_store_data(&packet2, (tmp == SHOW_OPTION_NO ? "NO" :
				  tmp == SHOW_OPTION_YES ? "YES" :
				  "DISABLED"));
        break;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1259
      case SHOW_CHAR:
1260
        net_store_data(&packet2,convert, value);
1261
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1262
      case SHOW_STARTTIME:
1263 1264
        net_store_data(&packet2,(uint32) (thd->query_start() - start_time));
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1265
      case SHOW_QUESTION:
1266 1267
        net_store_data(&packet2,(uint32) thd->query_id);
        break;
1268 1269 1270
      case SHOW_RPL_STATUS:
	net_store_data(&packet2, rpl_status_type[(int)rpl_status]);
	break;
1271
#ifdef HAVE_REPLICATION
1272 1273 1274 1275 1276 1277 1278 1279 1280
      case SHOW_SLAVE_RUNNING:
      {
	LOCK_ACTIVE_MI;
	net_store_data(&packet2, (active_mi->slave_running &&
				  active_mi->rli.slave_running)
		        ? "ON" : "OFF");
	UNLOCK_ACTIVE_MI;
	break;
      }
miguel@hegel.br's avatar
miguel@hegel.br committed
1281
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1282
      case SHOW_OPENTABLES:
1283 1284
        net_store_data(&packet2,(uint32) cached_tables());
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1285
      case SHOW_CHAR_PTR:
1286 1287 1288 1289 1290
      {
	value= *(char**) value;
	net_store_data(&packet2,convert, value ? value : "");
	break;
      }
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1291
#ifdef HAVE_OPENSSL
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1292
	/* First group - functions relying on CTX */
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1293 1294
      case SHOW_SSL_CTX_SESS_ACCEPT:
	net_store_data(&packet2,(uint32) 
1295
		       (!ssl_acceptor_fd ? 0 :
1296
			SSL_CTX_sess_accept(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1297 1298 1299
        break;
      case SHOW_SSL_CTX_SESS_ACCEPT_GOOD:
	net_store_data(&packet2,(uint32) 
1300
		       (!ssl_acceptor_fd ? 0 :
1301
			SSL_CTX_sess_accept_good(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1302
        break;
1303 1304
      case SHOW_SSL_CTX_SESS_CONNECT_GOOD:
	net_store_data(&packet2,(uint32) 
1305
		       (!ssl_acceptor_fd ? 0 :
1306
			SSL_CTX_sess_connect_good(ssl_acceptor_fd->ssl_context)));
1307
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1308 1309
      case SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE:
	net_store_data(&packet2,(uint32) 
1310
		       (!ssl_acceptor_fd ? 0 :
1311
			SSL_CTX_sess_accept_renegotiate(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1312
        break;
1313 1314
      case SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE:
	net_store_data(&packet2,(uint32) 
1315
		       (!ssl_acceptor_fd ? 0 :
1316
			SSL_CTX_sess_connect_renegotiate(ssl_acceptor_fd->ssl_context)));
1317
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1318 1319
      case SHOW_SSL_CTX_SESS_CB_HITS:
	net_store_data(&packet2,(uint32) 
1320
		       (!ssl_acceptor_fd ? 0 :
1321
			SSL_CTX_sess_cb_hits(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1322
        break;
1323 1324
      case SHOW_SSL_CTX_SESS_HITS:
	net_store_data(&packet2,(uint32) 
1325
		       (!ssl_acceptor_fd ? 0 :
1326
			SSL_CTX_sess_hits(ssl_acceptor_fd->ssl_context)));
1327 1328 1329
        break;
      case SHOW_SSL_CTX_SESS_CACHE_FULL:
	net_store_data(&packet2,(uint32) 
1330
		       (!ssl_acceptor_fd ? 0 :
1331
			SSL_CTX_sess_cache_full(ssl_acceptor_fd->ssl_context)));
1332 1333 1334
        break;
      case SHOW_SSL_CTX_SESS_MISSES:
	net_store_data(&packet2,(uint32) 
1335
		       (!ssl_acceptor_fd ? 0 :
1336
			SSL_CTX_sess_misses(ssl_acceptor_fd->ssl_context)));
1337 1338 1339
        break;
      case SHOW_SSL_CTX_SESS_TIMEOUTS:
	net_store_data(&packet2,(uint32) 
1340
		       (!ssl_acceptor_fd ? 0 :
1341
			SSL_CTX_sess_timeouts(ssl_acceptor_fd->ssl_context)));
1342
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1343 1344
      case SHOW_SSL_CTX_SESS_NUMBER:
	net_store_data(&packet2,(uint32) 
1345
		       (!ssl_acceptor_fd ? 0 :
1346
			SSL_CTX_sess_number(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1347
        break;
1348 1349
      case SHOW_SSL_CTX_SESS_CONNECT:
	net_store_data(&packet2,(uint32) 
1350
		       (!ssl_acceptor_fd ? 0 :
1351
			SSL_CTX_sess_connect(ssl_acceptor_fd->ssl_context)));
1352
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1353 1354
      case SHOW_SSL_CTX_SESS_GET_CACHE_SIZE:
	net_store_data(&packet2,(uint32) 
1355
		       (!ssl_acceptor_fd ? 0 :
1356
			SSL_CTX_sess_get_cache_size(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1357 1358 1359
        break;
      case SHOW_SSL_CTX_GET_VERIFY_MODE:
	net_store_data(&packet2,(uint32) 
1360
		       (!ssl_acceptor_fd ? 0 :
1361
			SSL_CTX_get_verify_mode(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1362 1363 1364
        break;
      case SHOW_SSL_CTX_GET_VERIFY_DEPTH:
	net_store_data(&packet2,(uint32) 
1365
		       (!ssl_acceptor_fd ? 0 :
1366
			SSL_CTX_get_verify_depth(ssl_acceptor_fd->ssl_context)));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1367 1368
        break;
      case SHOW_SSL_CTX_GET_SESSION_CACHE_MODE:
1369 1370 1371 1372 1373
	if (!ssl_acceptor_fd)
	{
	  net_store_data(&packet2,"NONE" );
	  break;
	}
1374
	switch (SSL_CTX_get_session_cache_mode(ssl_acceptor_fd->ssl_context))
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
	{
          case SSL_SESS_CACHE_OFF:
            net_store_data(&packet2,"OFF" );
	    break;
          case SSL_SESS_CACHE_CLIENT:
            net_store_data(&packet2,"CLIENT" );
	    break;
          case SSL_SESS_CACHE_SERVER:
            net_store_data(&packet2,"SERVER" );
	    break;
          case SSL_SESS_CACHE_BOTH:
            net_store_data(&packet2,"BOTH" );
	    break;
          case SSL_SESS_CACHE_NO_AUTO_CLEAR:
            net_store_data(&packet2,"NO_AUTO_CLEAR" );
	    break;
          case SSL_SESS_CACHE_NO_INTERNAL_LOOKUP:
            net_store_data(&packet2,"NO_INTERNAL_LOOKUP" );
	    break;
	  default:
            net_store_data(&packet2,"Unknown");
	    break;
	}
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1399 1400
	/* First group - functions relying on SSL */
      case SHOW_SSL_GET_VERSION:
1401 1402
	net_store_data(&packet2, thd->net.vio->ssl_arg ? 
			SSL_get_version((SSL*) thd->net.vio->ssl_arg) : "");
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1403 1404
        break;
      case SHOW_SSL_SESSION_REUSED:
1405 1406
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_arg ? 
			SSL_session_reused((SSL*) thd->net.vio->ssl_arg) : 0));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1407 1408
        break;
      case SHOW_SSL_GET_DEFAULT_TIMEOUT:
1409 1410 1411
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_arg ?
			SSL_get_default_timeout((SSL*) thd->net.vio->ssl_arg) :
					  0));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1412 1413
        break;
      case SHOW_SSL_GET_VERIFY_MODE:
1414 1415
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_arg ?
			SSL_get_verify_mode((SSL*) thd->net.vio->ssl_arg):0));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1416 1417
        break;
      case SHOW_SSL_GET_VERIFY_DEPTH:
1418 1419
	net_store_data(&packet2,(uint32) (thd->net.vio->ssl_arg ?
			SSL_get_verify_depth((SSL*) thd->net.vio->ssl_arg):0));
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1420 1421
        break;
      case SHOW_SSL_GET_CIPHER:
1422 1423
	net_store_data(&packet2, thd->net.vio->ssl_arg ?
		       SSL_get_cipher((SSL*) thd->net.vio->ssl_arg) : "");
1424
	break;
1425
      case SHOW_SSL_GET_CIPHER_LIST:
1426
	if (thd->net.vio->ssl_arg)
1427
	{
1428 1429 1430
	  char buf[1024], *pos;
	  pos=buf;
	  for (int i=0 ; i++ ;)
1431
	  {
1432
	    const char *p=SSL_get_cipher_list((SSL*) thd->net.vio->ssl_arg,i);
1433 1434
	    if (p == NULL) 
	      break;
1435 1436
	    pos=strmov(pos, p);
	    *pos++= ':';
1437
	  }
1438 1439 1440
	  if (pos != buf)
	    pos--;				// Remove last ':'
	  *pos=0;
1441
 	  net_store_data(&packet2, buf);
1442 1443
        }
	else
1444
 	  net_store_data(&packet2, "");
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1445
        break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1446 1447

#endif /* HAVE_OPENSSL */
1448 1449 1450 1451
      case SHOW_UNDEF:				// Show never happen
      case SHOW_SYS:
	net_store_data(&packet2, "");		// Safety
	break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1452 1453
      }
      if (my_net_write(&thd->net, (char*) packet2.ptr(),packet2.length()))
1454
        goto err;                               /* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1455 1456
    }
  }
1457
  pthread_mutex_unlock(mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1458 1459 1460 1461
  send_eof(&thd->net);
  DBUG_RETURN(0);

 err:
1462
  pthread_mutex_unlock(mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1463 1464 1465 1466
  DBUG_RETURN(1);
}

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