sql_show.cc 176 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2004 MySQL AB
2

unknown's avatar
unknown 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

unknown's avatar
unknown 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

unknown's avatar
unknown 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
22
#include "sql_show.h"
23
#include "repl_failsafe.h"
24
#include "sp.h"
25
#include "sp_head.h"
26
#include "sql_trigger.h"
unknown's avatar
unknown committed
27
#include "authors.h"
28
#include "event.h"
unknown's avatar
unknown committed
29
#include <my_dir.h>
unknown's avatar
unknown committed
30

31 32 33
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#endif
unknown's avatar
unknown committed
34

unknown's avatar
unknown committed
35 36 37 38
static const char *grant_names[]={
  "select","insert","update","delete","create","drop","reload","shutdown",
  "process","file","grant","references","index","alter"};

unknown's avatar
unknown committed
39
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
40
static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
41
                               "grant_types",
42
                               grant_names, NULL};
unknown's avatar
unknown committed
43
#endif
unknown's avatar
unknown committed
44

45 46 47
static void store_key_options(THD *thd, String *packet, TABLE *table,
                              KEY *key_info);

unknown's avatar
unknown committed
48 49 50 51
/***************************************************************************
** List all table types supported 
***************************************************************************/

unknown's avatar
unknown committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static my_bool show_handlerton(THD *thd, st_plugin_int *plugin,
                               void *arg)
{
  handlerton *default_type= (handlerton *) arg;
  Protocol *protocol= thd->protocol;
  handlerton *hton= (handlerton *) plugin->plugin->info;

  if (!(hton->flags & HTON_HIDDEN))
  {
    protocol->prepare_for_resend();
    protocol->store(hton->name, system_charset_info);
    const char *option_name= show_comp_option_name[(int) hton->state];

    if (hton->state == SHOW_OPTION_YES && default_type == hton)
      option_name= "DEFAULT";
    protocol->store(option_name, system_charset_info);
    protocol->store(hton->comment, system_charset_info);
    protocol->store(hton->commit ? "YES" : "NO", system_charset_info);
    protocol->store(hton->prepare ? "YES" : "NO", system_charset_info);
    protocol->store(hton->savepoint_set ? "YES" : "NO", system_charset_info);
    
    return protocol->write() ? 1 : 0;
  }
  return 0;  
}

unknown's avatar
unknown committed
78
bool mysqld_show_storage_engines(THD *thd)
unknown's avatar
unknown committed
79 80
{
  List<Item> field_list;
81
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
82
  DBUG_ENTER("mysqld_show_storage_engines");
unknown's avatar
unknown committed
83

unknown's avatar
unknown committed
84
  field_list.push_back(new Item_empty_string("Engine",10));
unknown's avatar
unknown committed
85
  field_list.push_back(new Item_empty_string("Support",10));
86
  field_list.push_back(new Item_empty_string("Comment",80));
87 88 89
  field_list.push_back(new Item_empty_string("Transactions",3));
  field_list.push_back(new Item_empty_string("XA",3));
  field_list.push_back(new Item_empty_string("Savepoints",3));
unknown's avatar
unknown committed
90

91 92
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
93
    DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
94

unknown's avatar
unknown committed
95 96 97
  if (plugin_foreach(thd, show_handlerton, 
                     MYSQL_STORAGE_ENGINE_PLUGIN, thd->variables.table_type))
    DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
98

99
  send_eof(thd);
unknown's avatar
unknown committed
100
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
101 102
}

unknown's avatar
unknown committed
103 104
static int make_version_string(char *buf, int buf_length, uint version)
{
unknown's avatar
unknown committed
105
  return my_snprintf(buf, buf_length, "%d.%d", version>>8,version&0xff);
unknown's avatar
unknown committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

static my_bool show_plugins(THD *thd, st_plugin_int *plugin,
                            void *arg)
{
  TABLE *table= (TABLE*) arg;
  struct st_mysql_plugin *plug= plugin->plugin;
  Protocol *protocol= thd->protocol;
  CHARSET_INFO *cs= system_charset_info;
  char version_buf[20];

  restore_record(table, s->default_values);

  table->field[0]->store(plugin->name.str, plugin->name.length, cs);
unknown's avatar
unknown committed
120 121 122 123 124

  table->field[1]->store(version_buf,
        make_version_string(version_buf, sizeof(version_buf), plug->version),
        cs);

unknown's avatar
fixes  
unknown committed
125

unknown's avatar
unknown committed
126 127 128 129
  switch (plugin->state)
  {
  /* case PLUGIN_IS_FREED: does not happen */
  case PLUGIN_IS_DELETED:
unknown's avatar
unknown committed
130
    table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
unknown's avatar
unknown committed
131 132
    break;
  case PLUGIN_IS_UNINITIALIZED:
unknown's avatar
unknown committed
133
    table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
unknown's avatar
unknown committed
134 135
    break;
  case PLUGIN_IS_READY:
unknown's avatar
unknown committed
136
    table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
unknown's avatar
unknown committed
137 138 139 140 141
    break;
  default:
    DBUG_ASSERT(0);
  }

unknown's avatar
unknown committed
142 143 144
  table->field[3]->store(plugin_type_names[plug->type].str,
                         plugin_type_names[plug->type].length,
                         cs);
unknown's avatar
unknown committed
145
  table->field[4]->store(version_buf,
unknown's avatar
unknown committed
146
        make_version_string(version_buf, sizeof(version_buf),
unknown's avatar
unknown committed
147
                            *(uint *)plug->info), cs);
unknown's avatar
unknown committed
148 149 150

  if (plugin->plugin_dl)
  {
unknown's avatar
unknown committed
151
    table->field[5]->store(plugin->plugin_dl->dl.str,
unknown's avatar
unknown committed
152 153
                           plugin->plugin_dl->dl.length, cs);
    table->field[5]->set_notnull();
unknown's avatar
unknown committed
154
    table->field[6]->store(version_buf,
unknown's avatar
unknown committed
155
          make_version_string(version_buf, sizeof(version_buf),
unknown's avatar
unknown committed
156 157 158
                              plugin->plugin_dl->version),
          cs);
    table->field[6]->set_notnull();
unknown's avatar
unknown committed
159 160
  }
  else
unknown's avatar
unknown committed
161
  {
unknown's avatar
unknown committed
162
    table->field[5]->set_null();
unknown's avatar
unknown committed
163 164 165 166
    table->field[6]->set_null();
  }


unknown's avatar
unknown committed
167 168
  if (plug->author)
  {
unknown's avatar
unknown committed
169 170
    table->field[7]->store(plug->author, strlen(plug->author), cs);
    table->field[7]->set_notnull();
unknown's avatar
unknown committed
171 172
  }
  else
unknown's avatar
unknown committed
173
    table->field[7]->set_null();
unknown's avatar
unknown committed
174 175 176

  if (plug->descr)
  {
unknown's avatar
unknown committed
177 178
    table->field[8]->store(plug->descr, strlen(plug->descr), cs);
    table->field[8]->set_notnull();
unknown's avatar
unknown committed
179 180
  }
  else
unknown's avatar
unknown committed
181
    table->field[8]->set_null();
unknown's avatar
unknown committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  return schema_table_store_record(thd, table);
}


int fill_plugins(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_plugins");
  TABLE *table= tables->table;

  if (plugin_foreach(thd, show_plugins, MYSQL_ANY_PLUGIN, table))
    DBUG_RETURN(1);
    
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/***************************************************************************
** List all Authors.
** If you can update it, you get to be in it :)
***************************************************************************/

bool mysqld_show_authors(THD *thd)
{
  List<Item> field_list;
  Protocol *protocol= thd->protocol;
  DBUG_ENTER("mysqld_show_authors");

  field_list.push_back(new Item_empty_string("Name",40));
  field_list.push_back(new Item_empty_string("Location",40));
  field_list.push_back(new Item_empty_string("Comment",80));

  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
    DBUG_RETURN(TRUE);

  show_table_authors_st *authors;
  for (authors= show_table_authors; authors->name; authors++)
  {
    protocol->prepare_for_resend();
    protocol->store(authors->name, system_charset_info);
    protocol->store(authors->location, system_charset_info);
    protocol->store(authors->comment, system_charset_info);
    if (protocol->write())
      DBUG_RETURN(TRUE);
  }
  send_eof(thd);
  DBUG_RETURN(FALSE);
}
231

unknown's avatar
unknown committed
232
/***************************************************************************
233
 List all privileges supported
unknown's avatar
unknown committed
234 235
***************************************************************************/

236 237 238 239
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
unknown's avatar
unknown committed
240 241
};

242 243
static struct show_privileges_st sys_privileges[]=
{
unknown's avatar
unknown committed
244
  {"Alter", "Tables",  "To alter the table"},
245
  {"Alter routine", "Functions,Procedures",  "To alter or drop stored functions/procedures"},
246
  {"Create", "Databases,Tables,Indexes",  "To create new databases and tables"},
247
  {"Create routine","Functions,Procedures","To use CREATE FUNCTION/PROCEDURE"},
unknown's avatar
unknown committed
248 249
  {"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
  {"Create view", "Tables",  "To create new views"},
250
  {"Create user", "Server Admin",  "To create new users"},
unknown's avatar
unknown committed
251
  {"Delete", "Tables",  "To delete existing rows"},
unknown's avatar
unknown committed
252
  {"Drop", "Databases,Tables", "To drop databases, tables, and views"},
253
  {"Event","Server Admin","To create, alter, drop and execute events"},
254
  {"Execute", "Functions,Procedures", "To execute stored routines"},
unknown's avatar
unknown committed
255
  {"File", "File access on server",   "To read and write files on the server"},
256
  {"Grant option",  "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
unknown's avatar
unknown committed
257 258 259 260
  {"Index", "Tables",  "To create or drop indexes"},
  {"Insert", "Tables",  "To insert data into tables"},
  {"Lock tables","Databases","To use LOCK TABLES (together with SELECT privilege)"},
  {"Process", "Server Admin", "To view the plain text of currently executing queries"},
261
  {"References", "Databases,Tables", "To have references on tables"},
unknown's avatar
unknown committed
262 263 264 265 266
  {"Reload", "Server Admin", "To reload or refresh tables, logs and privileges"},
  {"Replication client","Server Admin","To ask where the slave or master servers are"},
  {"Replication slave","Server Admin","To read binary log events from the master"},
  {"Select", "Tables",  "To retrieve rows from table"},
  {"Show databases","Server Admin","To see all databases with SHOW DATABASES"},
unknown's avatar
unknown committed
267 268
  {"Show view","Tables","To see views with SHOW CREATE VIEW"},
  {"Shutdown","Server Admin", "To shut down the server"},
unknown's avatar
unknown committed
269
  {"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
270
  {"Trigger","Tables", "To use triggers"},
unknown's avatar
unknown committed
271 272
  {"Update", "Tables",  "To update existing rows"},
  {"Usage","Server Admin","No privileges - allow connect only"},
273 274 275
  {NullS, NullS, NullS}
};

unknown's avatar
unknown committed
276
bool mysqld_show_privileges(THD *thd)
unknown's avatar
unknown committed
277 278
{
  List<Item> field_list;
279
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
280 281 282 283 284 285
  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));

286 287
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
288
    DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
289

290 291
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
unknown's avatar
unknown committed
292
  {
293
    protocol->prepare_for_resend();
294 295 296
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
297
    if (protocol->write())
unknown's avatar
unknown committed
298
      DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
299
  }
300
  send_eof(thd);
unknown's avatar
unknown committed
301
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
302 303 304 305
}


/***************************************************************************
306
  List all column types
unknown's avatar
unknown committed
307 308
***************************************************************************/

309 310
struct show_column_type_st
{
unknown's avatar
unknown committed
311 312
  const char *type;
  uint size;
313 314 315 316 317 318 319 320 321 322 323 324
  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;
unknown's avatar
unknown committed
325
};
326 327 328 329 330

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
unknown's avatar
unknown committed
331 332
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
unknown's avatar
unknown committed
333 334
    "NO",   "YES", "YES",  "NO",  "NULL,0",
    "A very small integer"},
unknown's avatar
unknown committed
335
  {"tinyint unsigned",
unknown's avatar
unknown committed
336 337
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",
    "YES",  "YES",  "YES",  "NO",  "NULL,0",
unknown's avatar
unknown committed
338 339 340
    "A very small integer"},
};

unknown's avatar
unknown committed
341
bool mysqld_show_column_types(THD *thd)
unknown's avatar
unknown committed
342 343
{
  List<Item> field_list;
344
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
345 346 347 348 349 350
  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));
351 352
  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));
unknown's avatar
unknown committed
353 354 355 356 357 358 359 360 361
  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));

362 363
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
364
    DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
365

366
  /* TODO: Change the loop to not use 'i' */
unknown's avatar
unknown committed
367 368
  for (uint i=0; i < sizeof(sys_column_types)/sizeof(sys_column_types[0]); i++)
  {
369
    protocol->prepare_for_resend();
370
    protocol->store(sys_column_types[i].type, system_charset_info);
371
    protocol->store((ulonglong) sys_column_types[i].size);
372 373
    protocol->store(sys_column_types[i].min_value, system_charset_info);
    protocol->store(sys_column_types[i].max_value, system_charset_info);
374 375
    protocol->store_short((longlong) sys_column_types[i].precision);
    protocol->store_short((longlong) sys_column_types[i].scale);
376 377 378 379 380 381 382 383
    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);
384
    if (protocol->write())
unknown's avatar
unknown committed
385
      DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
386
  }
387
  send_eof(thd);
unknown's avatar
unknown committed
388
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
389 390 391
}


392
int
unknown's avatar
unknown committed
393
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
394
                 const char *wild, bool dir)
unknown's avatar
unknown committed
395 396 397 398 399
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
unknown's avatar
unknown committed
400
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
401
  uint col_access=thd->col_access;
unknown's avatar
unknown committed
402
#endif
unknown's avatar
unknown committed
403 404 405
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

406 407
  if (wild && !wild[0])
    wild=0;
408

unknown's avatar
unknown committed
409 410
  bzero((char*) &table_list,sizeof(table_list));

411 412 413 414 415 416
  if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0))))
  {
    if (my_errno == ENOENT)
      my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), db);
    else
      my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), path, my_errno);
unknown's avatar
unknown committed
417
    DBUG_RETURN(-1);
418
  }
unknown's avatar
unknown committed
419

420
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
unknown's avatar
unknown committed
421
  {
422
    char uname[NAME_LEN*3+1];                   /* Unencoded name */
unknown's avatar
unknown committed
423 424
    file=dirp->dir_entry+i;
    if (dir)
425
    {                                           /* Return databases */
426 427 428 429
      if ((file->name[0] == '.' && 
          ((file->name[1] == '.' && file->name[2] == '\0') ||
            file->name[1] == '\0')))
        continue;                               /* . or .. */
unknown's avatar
unknown committed
430 431
#ifdef USE_SYMDIR
      char *ext;
432
      char buff[FN_REFLEN];
unknown's avatar
unknown committed
433
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
434 435
      {
	/* Only show the sym file if it points to a directory */
436
	char *end;
437
        *ext=0;                                 /* Remove extension */
438 439 440 441
	unpack_dirname(buff, file->name);
	end= strend(buff);
	if (end != buff && end[-1] == FN_LIBCHAR)
	  end[-1]= 0;				// Remove end FN_LIBCHAR
442 443 444
        if (!my_stat(buff, file->mystat, MYF(0)))
               continue;
       }
unknown's avatar
unknown committed
445
#endif
446 447
      if (!MY_S_ISDIR(file->mystat->st_mode))
        continue;
448
      VOID(filename_to_tablename(file->name, uname, sizeof(uname)));
449
      if (wild && wild_compare(uname, wild, 0))
450 451
        continue;
      file->name= uname;
unknown's avatar
unknown committed
452 453 454
    }
    else
    {
455
        // Return only .frm files which aren't temp files.
456
      if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),reg_ext) ||
457
          is_prefix(file->name,tmp_file_prefix))
458
        continue;
unknown's avatar
unknown committed
459
      *ext=0;
460 461
      VOID(filename_to_tablename(file->name, uname, sizeof(uname)));
      file->name= uname;
462 463 464 465
      if (wild)
      {
	if (lower_case_table_names)
	{
466
	  if (wild_case_compare(files_charset_info, file->name, wild))
467 468
	    continue;
	}
469
	else if (wild_compare(file->name,wild,0))
470 471
	  continue;
      }
unknown's avatar
unknown committed
472
    }
unknown's avatar
unknown committed
473
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
474 475 476 477
    /* Don't show tables where we don't have any privileges */
    if (db && !(col_access & TABLE_ACLS))
    {
      table_list.db= (char*) db;
478
      table_list.db_length= strlen(db);
479
      table_list.table_name= file->name;
480
      table_list.table_name_length= strlen(file->name);
unknown's avatar
unknown committed
481
      table_list.grant.privilege=col_access;
482
      if (check_grant(thd, TABLE_ACLS, &table_list, 1, 1, 1))
483
        continue;
unknown's avatar
unknown committed
484
    }
unknown's avatar
unknown committed
485
#endif
486
    if (files->push_back(thd->strdup(file->name)))
unknown's avatar
unknown committed
487 488 489 490 491 492 493
    {
      my_dirend(dirp);
      DBUG_RETURN(-1);
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
unknown's avatar
unknown committed
494 495 496

  VOID(ha_find_files(thd,db,path,wild,dir,files));

unknown's avatar
unknown committed
497 498 499
  DBUG_RETURN(0);
}

500

unknown's avatar
unknown committed
501
bool
unknown's avatar
unknown committed
502 503
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
504 505 506
  Protocol *protocol= thd->protocol;
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
unknown's avatar
unknown committed
507 508
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
509
                      table_list->table_name));
unknown's avatar
unknown committed
510

511 512 513
  /* We want to preserve the tree for views. */
  thd->lex->view_prepare_mode= TRUE;

unknown's avatar
VIEW  
unknown committed
514
  /* Only one table for now, but VIEW can involve several tables */
515
  if (open_normal_and_derived_tables(thd, table_list, 0))
516 517 518
  {
    if (!table_list->view || thd->net.last_errno != ER_VIEW_INVALID)
      DBUG_RETURN(TRUE);
519

520 521 522 523 524 525
    /*
      Clear all messages with 'error' level status and
      issue a warning with 'warning' level status in 
      case of invalid view and last error is ER_VIEW_INVALID
    */
    mysql_reset_errors(thd, true);
526 527
    thd->clear_error();

528 529 530 531 532 533
    push_warning_printf(thd,MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_VIEW_INVALID,
                        ER(ER_VIEW_INVALID),
                        table_list->view_db.str,
                        table_list->view_name.str);
  }
534

unknown's avatar
VIEW  
unknown committed
535 536 537
  /* TODO: add environment variables show when it become possible */
  if (thd->lex->only_view && !table_list->view)
  {
538
    my_error(ER_WRONG_OBJECT, MYF(0),
539
             table_list->db, table_list->table_name, "VIEW");
unknown's avatar
unknown committed
540
    DBUG_RETURN(TRUE);
unknown's avatar
VIEW  
unknown committed
541
  }
unknown's avatar
unknown committed
542

543
  buffer.length(0);
unknown's avatar
VIEW  
unknown committed
544 545
  if ((table_list->view ?
       view_store_create_info(thd, table_list, &buffer) :
546
       store_create_info(thd, table_list, &buffer, NULL)))
unknown's avatar
unknown committed
547
    DBUG_RETURN(TRUE);
548

unknown's avatar
unknown committed
549
  List<Item> field_list;
unknown's avatar
unknown committed
550 551 552 553 554 555 556 557 558 559 560 561 562
  if (table_list->view)
  {
    field_list.push_back(new Item_empty_string("View",NAME_LEN));
    field_list.push_back(new Item_empty_string("Create View",
                                               max(buffer.length(),1024)));
  }
  else
  {
    field_list.push_back(new Item_empty_string("Table",NAME_LEN));
    // 1024 is for not to confuse old clients
    field_list.push_back(new Item_empty_string("Create Table",
                                               max(buffer.length(),1024)));
  }
unknown's avatar
unknown committed
563

564 565
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
566
    DBUG_RETURN(TRUE);
567
  protocol->prepare_for_resend();
unknown's avatar
VIEW  
unknown committed
568 569 570 571
  if (table_list->view)
    protocol->store(table_list->view_name.str, system_charset_info);
  else
  {
unknown's avatar
unknown committed
572
    if (table_list->schema_table)
573 574
      protocol->store(table_list->schema_table->table_name,
                      system_charset_info);
unknown's avatar
unknown committed
575
    else
576
      protocol->store(table_list->table->alias, system_charset_info);
unknown's avatar
VIEW  
unknown committed
577
  }
578
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
unknown's avatar
VIEW  
unknown committed
579

580
  if (protocol->write())
unknown's avatar
unknown committed
581
    DBUG_RETURN(TRUE);
582
  send_eof(thd);
unknown's avatar
unknown committed
583
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
584 585
}

unknown's avatar
unknown committed
586 587
bool mysqld_show_create_db(THD *thd, char *dbname,
                           HA_CREATE_INFO *create_info)
588
{
589
  Security_context *sctx= thd->security_ctx;
590
  int length;
591
  char path[FN_REFLEN];
592 593
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
unknown's avatar
unknown committed
594
#ifndef NO_EMBEDDED_ACCESS_CHECKS
595
  uint db_access;
unknown's avatar
unknown committed
596
#endif
597 598 599 600 601 602 603
  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");

#ifndef NO_EMBEDDED_ACCESS_CHECKS
604
  if (test_all_bits(sctx->master_access, DB_ACLS))
605 606
    db_access=DB_ACLS;
  else
607 608
    db_access= (acl_get(sctx->host, sctx->ip, sctx->priv_user, dbname, 0) |
		sctx->master_access);
609 610
  if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname)))
  {
611
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
612
             sctx->priv_user, sctx->host_or_ip, dbname);
613 614
    general_log_print(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR),
                      sctx->priv_user, sctx->host_or_ip, dbname);
unknown's avatar
unknown committed
615
    DBUG_RETURN(TRUE);
616 617
  }
#endif
618 619
  if (!my_strcasecmp(system_charset_info, dbname,
                     information_schema_name.str))
620
  {
621 622
    dbname= information_schema_name.str;
    create.default_table_charset= system_charset_info;
623
  }
624
  else
625
  {
626
    length= build_table_filename(path, sizeof(path), dbname, "", "");
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    found_libchar= 0;
    if (length && path[length-1] == FN_LIBCHAR)
    {
      found_libchar= 1;
      path[length-1]=0;				// remove ending '\'
    }
    if (access(path,F_OK))
    {
      my_error(ER_BAD_DB_ERROR, MYF(0), dbname);
      DBUG_RETURN(TRUE);
    }
    if (found_libchar)
      path[length-1]= FN_LIBCHAR;
    strmov(path+length, MY_DB_OPT_FILE);
    load_db_opt(thd, path, &create);
642 643 644 645 646
  }
  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));

647 648
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
649
    DBUG_RETURN(TRUE);
650 651 652 653

  protocol->prepare_for_resend();
  protocol->store(dbname, strlen(dbname), system_charset_info);
  buffer.length(0);
654
  buffer.append(STRING_WITH_LEN("CREATE DATABASE "));
655
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
656
    buffer.append(STRING_WITH_LEN("/*!32312 IF NOT EXISTS*/ "));
657 658 659 660
  append_identifier(thd, &buffer, dbname, strlen(dbname));

  if (create.default_table_charset)
  {
661 662
    buffer.append(STRING_WITH_LEN(" /*!40100"));
    buffer.append(STRING_WITH_LEN(" DEFAULT CHARACTER SET "));
663 664 665
    buffer.append(create.default_table_charset->csname);
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
    {
666
      buffer.append(STRING_WITH_LEN(" COLLATE "));
667 668
      buffer.append(create.default_table_charset->name);
    }
669
    buffer.append(STRING_WITH_LEN(" */"));
670 671 672 673
  }
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());

  if (protocol->write())
unknown's avatar
unknown committed
674
    DBUG_RETURN(TRUE);
675
  send_eof(thd);
unknown's avatar
unknown committed
676
  DBUG_RETURN(FALSE);
677
}
unknown's avatar
unknown committed
678

unknown's avatar
unknown committed
679 680


unknown's avatar
unknown committed
681
/****************************************************************************
682 683
  Return only fields for API mysql_list_fields
  Use "show table wildcard" in mysql instead of this
unknown's avatar
unknown committed
684 685 686 687 688 689 690
****************************************************************************/

void
mysqld_list_fields(THD *thd, TABLE_LIST *table_list, const char *wild)
{
  TABLE *table;
  DBUG_ENTER("mysqld_list_fields");
691
  DBUG_PRINT("enter",("table: %s",table_list->table_name));
unknown's avatar
unknown committed
692

693
  if (open_normal_and_derived_tables(thd, table_list, 0))
unknown's avatar
unknown committed
694
    DBUG_VOID_RETURN;
695 696
  table= table_list->table;

unknown's avatar
unknown committed
697 698 699 700 701
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
702 703
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
unknown's avatar
unknown committed
704 705
      field_list.push_back(new Item_field(field));
  }
706
  restore_record(table, s->default_values);              // Get empty record
707 708
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS |
                                              Protocol::SEND_EOF))
unknown's avatar
unknown committed
709
    DBUG_VOID_RETURN;
710
  thd->protocol->flush();
unknown's avatar
unknown committed
711 712 713
  DBUG_VOID_RETURN;
}

714

unknown's avatar
unknown committed
715
int
unknown's avatar
unknown committed
716
mysqld_dump_create_info(THD *thd, TABLE_LIST *table_list, int fd)
unknown's avatar
unknown committed
717
{
718 719
  Protocol *protocol= thd->protocol;
  String *packet= protocol->storage_packet();
unknown's avatar
unknown committed
720
  DBUG_ENTER("mysqld_dump_create_info");
unknown's avatar
unknown committed
721
  DBUG_PRINT("enter",("table: %s",table_list->table->s->table_name.str));
722

723
  protocol->prepare_for_resend();
724
  if (store_create_info(thd, table_list, packet, NULL))
unknown's avatar
unknown committed
725
    DBUG_RETURN(-1);
726

727
  if (fd < 0)
728
  {
729
    if (protocol->write())
730
      DBUG_RETURN(-1);
731
    protocol->flush();
732
  }
unknown's avatar
unknown committed
733
  else
734
  {
735 736
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
737 738
      DBUG_RETURN(-1);
  }
unknown's avatar
unknown committed
739 740
  DBUG_RETURN(0);
}
741

742
/*
743
  Go through all character combinations and ensure that sql_lex.cc can
744
  parse it as an identifier.
745 746

  SYNOPSIS
747 748 749 750 751 752 753
  require_quotes()
  name			attribute name
  name_length		length of name

  RETURN
    #	Pointer to conflicting character
    0	No conflicting character
754 755
*/

756
static const char *require_quotes(const char *name, uint name_length)
757
{
758
  uint length;
759
  bool pure_digit= TRUE;
760 761
  const char *end= name + name_length;

762
  for (; name < end ; name++)
763
  {
764 765 766 767
    uchar chr= (uchar) *name;
    length= my_mbcharlen(system_charset_info, chr);
    if (length == 1 && !system_charset_info->ident_map[chr])
      return name;
768 769
    if (length == 1 && (chr < '0' || chr > '9'))
      pure_digit= FALSE;
770
  }
771 772
  if (pure_digit)
    return name;
773 774
  return 0;
}
775

776

777 778 779 780 781 782 783 784 785 786 787 788
/*
  Quote the given identifier if needed and append it to the target string.
  If the given identifier is empty, it will be quoted.

  SYNOPSIS
  append_identifier()
  thd                   thread handler
  packet                target string
  name                  the identifier to be appended
  name_length           length of the appending identifier
*/

789 790
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
791
{
792 793
  const char *name_end;
  char quote_char;
794
  int q= get_quote_char_for_identifier(thd, name, length);
795

796
  if (q == EOF)
797
  {
798
    packet->append(name, length, system_charset_info);
799 800 801
    return;
  }

802 803 804 805
  /*
    The identifier must be quoted as it includes a quote character or
   it's a keyword
  */
806

807
  VOID(packet->reserve(length*2 + 2));
808
  quote_char= (char) q;
809 810 811 812
  packet->append(&quote_char, 1, system_charset_info);

  for (name_end= name+length ; name < name_end ; name+= length)
  {
813
    uchar chr= (uchar) *name;
814
    length= my_mbcharlen(system_charset_info, chr);
815 816 817 818 819 820 821 822 823
    /*
      my_mbcharlen can retur 0 on a wrong multibyte
      sequence. It is possible when upgrading from 4.0,
      and identifier contains some accented characters.
      The manual says it does not work. So we'll just
      change length to 1 not to hang in the endless loop.
    */
    if (!length)
      length= 1;
824
    if (length == 1 && chr == (uchar) quote_char)
825 826
      packet->append(&quote_char, 1, system_charset_info);
    packet->append(name, length, packet->charset());
827
  }
828
  packet->append(&quote_char, 1, system_charset_info);
829 830
}

831

832 833 834 835 836 837 838 839 840 841
/*
  Get the quote character for displaying an identifier.

  SYNOPSIS
    get_quote_char_for_identifier()
    thd		Thread handler
    name	name to quote
    length	length of name

  IMPLEMENTATION
842 843 844 845 846
    Force quoting in the following cases:
      - name is empty (for one, it is possible when we use this function for
        quoting user and host names for DEFINER clause);
      - name is a keyword;
      - name includes a special character;
847 848 849 850 851 852 853
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
    is set.

  RETURN
    EOF	  No quote character is needed
    #	  Quote character
*/
854 855 856

int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
{
unknown's avatar
unknown committed
857
  if (length &&
858
      !is_keyword(name,length) &&
859 860 861
      !require_quotes(name, length) &&
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
    return EOF;
862
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
863
    return '"';
864
  return '`';
865 866 867
}


868 869 870 871 872
/* Append directory name (if exists) to CREATE INFO */

static void append_directory(THD *thd, String *packet, const char *dir_type,
			     const char *filename)
{
unknown's avatar
unknown committed
873
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
874
  {
875
    uint length= dirname_length(filename);
876 877
    packet->append(' ');
    packet->append(dir_type);
878
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
879
#ifdef __WIN__
unknown's avatar
unknown committed
880 881 882 883 884 885 886 887 888
    /* Convert \ to / to be able to create table on unix */
    char *winfilename= (char*) thd->memdup(filename, length);
    char *pos, *end;
    for (pos= winfilename, end= pos+length ; pos < end ; pos++)
    {
      if (*pos == '\\')
        *pos = '/';
    }
    filename= winfilename;
889
#endif
unknown's avatar
unknown committed
890
    packet->append(filename, length);
891 892 893 894 895
    packet->append('\'');
  }
}


unknown's avatar
unknown committed
896
#define LIST_PROCESS_HOST_LEN 64
897

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
/*
  Build a CREATE TABLE statement for a table.

  SYNOPSIS
    store_create_info()
    thd               The thread
    table_list        A list containing one table to write statement
                      for.
    packet            Pointer to a string where statement will be
                      written.
    create_info_arg   Pointer to create information that can be used
                      to tailor the format of the statement.  Can be
                      NULL, in which case only SQL_MODE is considered
                      when building the statement.

  NOTE
    Currently always return 0, but might return error code in the
    future.

  RETURN
    0       OK
 */
int
store_create_info(THD *thd, TABLE_LIST *table_list, String *packet,
                  HA_CREATE_INFO *create_info_arg)
unknown's avatar
unknown committed
923
{
924
  List<Item> field_list;
925
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end, uname[NAME_LEN*3+1];
926
  const char *alias;
927
  String type(tmp, sizeof(tmp), system_charset_info);
928 929 930
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
unknown's avatar
unknown committed
931
  TABLE *table= table_list->table;
932
  handler *file= table->file;
933
  TABLE_SHARE *share= table->s;
934
  HA_CREATE_INFO create_info;
935 936 937 938 939 940
  bool foreign_db_mode=  (thd->variables.sql_mode & (MODE_POSTGRESQL |
                                                     MODE_ORACLE |
                                                     MODE_MSSQL |
                                                     MODE_DB2 |
                                                     MODE_MAXDB |
                                                     MODE_ANSI)) != 0;
unknown's avatar
unknown committed
941 942 943
  bool limited_mysql_mode= (thd->variables.sql_mode & (MODE_NO_FIELD_OPTIONS |
                                                       MODE_MYSQL323 |
                                                       MODE_MYSQL40)) != 0;
unknown's avatar
unknown committed
944
  DBUG_ENTER("store_create_info");
unknown's avatar
unknown committed
945
  DBUG_PRINT("enter",("table: %s", table->s->table_name.str));
unknown's avatar
unknown committed
946

947
  restore_record(table, s->default_values); // Get empty record
948

949
  if (share->tmp_table)
950
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
951
  else
952
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
unknown's avatar
unknown committed
953
  if (table_list->schema_table)
954
    alias= table_list->schema_table->table_name;
unknown's avatar
unknown committed
955
  else
956 957 958 959 960 961 962 963
  {
    if (lower_case_table_names == 2)
      alias= table->alias;
    else
    {
      alias= share->table_name.str;
    }
  }
unknown's avatar
unknown committed
964
  append_identifier(thd, packet, alias, strlen(alias));
965
  packet->append(STRING_WITH_LEN(" (\n"));
966

unknown's avatar
unknown committed
967 968
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
969
    bool has_default;
970
    bool has_now_default;
971 972
    uint flags = field->flags;

973
    if (ptr != table->field)
974
      packet->append(STRING_WITH_LEN(",\n"));
975

976
    packet->append(STRING_WITH_LEN("  "));
977
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
unknown's avatar
unknown committed
978 979
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
980
    if (type.ptr() != tmp)
981
      type.set(tmp, sizeof(tmp), system_charset_info);
unknown's avatar
unknown committed
982 983
    else
      type.set_charset(system_charset_info);
984

unknown's avatar
unknown committed
985
    field->sql_type(type);
986
    packet->append(type.ptr(), type.length(), system_charset_info);
987

988 989
    if (field->has_charset() && 
        !(thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
990
    {
991
      if (field->charset() != share->table_charset)
992
      {
993
	packet->append(STRING_WITH_LEN(" CHARACTER SET "));
994 995 996 997 998 999 1000 1001
	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))
      {
1002
	packet->append(STRING_WITH_LEN(" COLLATE "));
1003
	packet->append(field->charset()->name);
1004
      }
1005
    }
1006

1007
    if (flags & NOT_NULL_FLAG)
1008
      packet->append(STRING_WITH_LEN(" NOT NULL"));
1009 1010 1011 1012 1013 1014
    else if (field->type() == FIELD_TYPE_TIMESTAMP)
    {
      /*
        TIMESTAMP field require explicit NULL flag, because unlike
        all other fields they are treated as NOT NULL by default.
      */
1015
      packet->append(STRING_WITH_LEN(" NULL"));
1016
    }
1017 1018

    /* 
1019
      Again we are using CURRENT_TIMESTAMP instead of NOW because it is
1020 1021 1022 1023 1024
      more standard 
    */
    has_now_default= table->timestamp_field == field && 
                     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
    
1025
    has_default= (field->type() != FIELD_TYPE_BLOB &&
unknown's avatar
unknown committed
1026
                  !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
1027
		  field->unireg_check != Field::NEXT_NUMBER &&
unknown's avatar
unknown committed
1028 1029
                  !((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
		    && has_now_default));
1030

1031
    if (has_default)
1032
    {
1033
      packet->append(STRING_WITH_LEN(" DEFAULT "));
1034
      if (has_now_default)
1035
        packet->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
1036
      else if (!field->is_null())
1037
      {                                             // Not null by default
1038
        type.set(tmp, sizeof(tmp), field->charset());
1039
        field->val_str(&type);
1040
	if (type.length())
1041
	{
1042
	  String def_val;
1043
          uint dummy_errors;
1044 1045
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
1046
		       system_charset_info, &dummy_errors);
1047 1048
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
1049
        else
1050
	  packet->append(STRING_WITH_LEN("''"));
unknown's avatar
unknown committed
1051
      }
1052
      else if (field->maybe_null())
1053
        packet->append(STRING_WITH_LEN("NULL"));    // Null as default
1054
      else
1055
        packet->append(tmp);
1056
    }
1057

unknown's avatar
unknown committed
1058
    if (!limited_mysql_mode && table->timestamp_field == field && 
1059
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
1060
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
1061

1062 1063
    if (field->unireg_check == Field::NEXT_NUMBER && 
        !(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS))
1064
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
1065 1066 1067

    if (field->comment.length)
    {
1068
      packet->append(STRING_WITH_LEN(" COMMENT "));
1069 1070
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
unknown's avatar
unknown committed
1071 1072
  }

1073
  key_info= table->key_info;
1074
  bzero((char*) &create_info, sizeof(create_info));
1075
  file->update_create_info(&create_info);
1076
  primary_key= share->primary_key;
1077

1078
  for (uint i=0 ; i < share->keys ; i++,key_info++)
unknown's avatar
unknown committed
1079
  {
1080 1081
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
1082
    packet->append(STRING_WITH_LEN(",\n  "));
1083

1084
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
1085 1086
    {
      found_primary=1;
1087 1088 1089 1090 1091
      /*
        No space at end, because a space will be added after where the
        identifier would go, but that is not added for primary key.
      */
      packet->append(STRING_WITH_LEN("PRIMARY KEY"));
1092
    }
1093
    else if (key_info->flags & HA_NOSAME)
1094
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
1095
    else if (key_info->flags & HA_FULLTEXT)
1096
      packet->append(STRING_WITH_LEN("FULLTEXT KEY "));
unknown's avatar
unknown committed
1097
    else if (key_info->flags & HA_SPATIAL)
1098 1099 1100
      packet->append(STRING_WITH_LEN("SPATIAL KEY "));
    else
      packet->append(STRING_WITH_LEN("KEY "));
unknown's avatar
unknown committed
1101

1102
    if (!found_primary)
1103
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
unknown's avatar
unknown committed
1104

1105
    packet->append(STRING_WITH_LEN(" ("));
1106

unknown's avatar
unknown committed
1107 1108
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
1109
      if (j)
1110
        packet->append(',');
1111

1112
      if (key_part->field)
1113 1114
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
1115
      if (key_part->field &&
1116 1117 1118
          (key_part->length !=
           table->field[key_part->fieldnr-1]->key_length() &&
           !(key_info->flags & HA_FULLTEXT)))
unknown's avatar
unknown committed
1119
      {
1120
        buff[0] = '(';
1121
        char* end=int10_to_str((long) key_part->length /
1122 1123
			       key_part->field->charset()->mbmaxlen,
			       buff + 1,10);
1124 1125
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
unknown's avatar
unknown committed
1126 1127 1128
      }
    }
    packet->append(')');
1129
    store_key_options(thd, packet, table, key_info);
1130 1131 1132 1133 1134 1135
    if (key_info->parser)
    {
      packet->append(" WITH PARSER ", 13);
      append_identifier(thd, packet, key_info->parser->name.str,
                        key_info->parser->name.length);
    }
unknown's avatar
unknown committed
1136
  }
1137

1138 1139 1140 1141
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
1142

1143
  if ((for_str= file->get_foreign_key_create_info()))
1144 1145 1146
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
1147 1148
  }

1149
  packet->append(STRING_WITH_LEN("\n)"));
1150
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
1151
  {
1152 1153 1154 1155 1156
    /*
      Get possible table space definitions and append them
      to the CREATE TABLE statement
    */

1157
    if ((for_str= file->get_tablespace_name(thd)))
1158
    {
1159
      packet->append(" TABLESPACE ");
1160
      packet->append(for_str, strlen(for_str));
1161
      packet->append(" STORAGE DISK");
1162 1163 1164
      my_free(for_str, MYF(0));
    }

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    /*
      IF   check_create_info
      THEN add ENGINE only if it was used when creating the table
    */
    if (!create_info_arg ||
        (create_info_arg->used_fields & HA_CREATE_USED_ENGINE))
    {
      if (thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
        packet->append(STRING_WITH_LEN(" TYPE="));
      else
        packet->append(STRING_WITH_LEN(" ENGINE="));
1176
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
1177
    if (table->part_info)
unknown's avatar
unknown committed
1178 1179
      packet->append(ha_resolve_storage_engine_name(
                        table->part_info->default_engine_type));
1180
    else
1181
      packet->append(file->table_type());
1182
#else
1183
      packet->append(file->table_type());
1184
#endif
1185
    }
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    /*
      Add AUTO_INCREMENT=... if there is an AUTO_INCREMENT column,
      and NEXT_ID > 1 (the default).  We must not print the clause
      for engines that do not support this as it would break the
      import of dumps, but as of this writing, the test for whether
      AUTO_INCREMENT columns are allowed and wether AUTO_INCREMENT=...
      is supported is identical, !(file->table_flags() & HA_NO_AUTO_INCREMENT))
      Because of that, we do not explicitly test for the feature,
      but may extrapolate its existence from that of an AUTO_INCREMENT column.
    */

    if(create_info.auto_increment_value > 1)
    {
      packet->append(" AUTO_INCREMENT=", 16);
      end= longlong10_to_str(create_info.auto_increment_value, buff,10);
      packet->append(buff, (uint) (end - buff));
    }
1204

1205
    
1206
    if (share->table_charset &&
1207 1208
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
1209
    {
1210 1211 1212 1213 1214 1215
      /*
        IF   check_create_info
        THEN add DEFAULT CHARSET only if it was used when creating the table
      */
      if (!create_info_arg ||
          (create_info_arg->used_fields & HA_CREATE_USED_DEFAULT_CHARSET))
1216
      {
1217 1218 1219 1220 1221 1222 1223
        packet->append(STRING_WITH_LEN(" DEFAULT CHARSET="));
        packet->append(share->table_charset->csname);
        if (!(share->table_charset->state & MY_CS_PRIMARY))
        {
          packet->append(STRING_WITH_LEN(" COLLATE="));
          packet->append(table->s->table_charset->name);
        }
1224
      }
1225
    }
1226

1227
    if (share->min_rows)
1228
    {
1229
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
1230
      end= longlong10_to_str(share->min_rows, buff, 10);
unknown's avatar
unknown committed
1231
      packet->append(buff, (uint) (end- buff));
1232
    }
unknown's avatar
unknown committed
1233

1234
    if (share->max_rows && !table_list->schema_table)
1235
    {
1236
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
1237
      end= longlong10_to_str(share->max_rows, buff, 10);
unknown's avatar
unknown committed
1238
      packet->append(buff, (uint) (end - buff));
1239
    }
unknown's avatar
unknown committed
1240

1241
    if (share->avg_row_length)
1242
    {
1243
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
1244
      end= longlong10_to_str(share->avg_row_length, buff,10);
unknown's avatar
unknown committed
1245
      packet->append(buff, (uint) (end - buff));
1246
    }
1247

1248
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
1249
      packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
1250
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
1251
      packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
1252
    if (share->db_create_options & HA_OPTION_CHECKSUM)
1253
      packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
1254
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1255
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
1256
    if (share->row_type != ROW_TYPE_DEFAULT)
1257
    {
1258
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
1259
      packet->append(ha_row_type[(uint) share->row_type]);
1260
    }
1261 1262 1263 1264 1265 1266
    if (table->s->key_block_size)
    {
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
      end= longlong10_to_str(table->s->key_block_size, buff, 10);
      packet->append(buff, (uint) (end - buff));
    }
1267
    table->file->append_create_info(packet);
1268
    if (share->comment && share->comment[0])
1269
    {
1270
      packet->append(STRING_WITH_LEN(" COMMENT="));
1271
      append_unescaped(packet, share->comment, strlen(share->comment));
1272
    }
1273 1274
    if (share->connect_string.length)
    {
1275
      packet->append(STRING_WITH_LEN(" CONNECTION="));
1276 1277
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
    }
unknown's avatar
unknown committed
1278 1279
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1280
  }
1281
#ifdef WITH_PARTITION_STORAGE_ENGINE
1282 1283 1284 1285 1286 1287
  {
    /*
      Partition syntax for CREATE TABLE is at the end of the syntax.
    */
    uint part_syntax_len;
    char *part_syntax;
unknown's avatar
unknown committed
1288
    if (table->part_info &&
1289
        (!table->part_info->is_auto_partitioned) &&
unknown's avatar
unknown committed
1290
        ((part_syntax= generate_partition_syntax(table->part_info,
unknown's avatar
unknown committed
1291 1292
                                                  &part_syntax_len,
                                                  FALSE,FALSE))))
1293 1294 1295 1296 1297 1298
    {
       packet->append(part_syntax, part_syntax_len);
       my_free(part_syntax, MYF(0));
    }
  }
#endif
unknown's avatar
unknown committed
1299 1300 1301
  DBUG_RETURN(0);
}

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

static void store_key_options(THD *thd, String *packet, TABLE *table,
                              KEY *key_info)
{
  bool limited_mysql_mode= (thd->variables.sql_mode &
                            (MODE_NO_FIELD_OPTIONS | MODE_MYSQL323 |
                             MODE_MYSQL40)) != 0;
  bool foreign_db_mode=  (thd->variables.sql_mode & (MODE_POSTGRESQL |
                                                     MODE_ORACLE |
                                                     MODE_MSSQL |
                                                     MODE_DB2 |
                                                     MODE_MAXDB |
                                                     MODE_ANSI)) != 0;
  char *end, buff[32];

  if (!(thd->variables.sql_mode & MODE_NO_KEY_OPTIONS) &&
      !limited_mysql_mode && !foreign_db_mode)
  {

    if (key_info->algorithm == HA_KEY_ALG_BTREE)
      packet->append(STRING_WITH_LEN(" USING BTREE"));

    if (key_info->algorithm == HA_KEY_ALG_HASH)
      packet->append(STRING_WITH_LEN(" USING HASH"));

    /* 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(STRING_WITH_LEN(" USING RTREE"));

    if ((key_info->flags & HA_USES_BLOCK_SIZE) &&
        table->s->key_block_size != key_info->block_size)
    {
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
      end= longlong10_to_str(key_info->block_size, buff, 10);
      packet->append(buff, (uint) (end - buff));
    }
  }
}


1343 1344 1345
void
view_store_options(THD *thd, TABLE_LIST *table, String *buff)
{
1346
  buff->append(STRING_WITH_LEN("ALGORITHM="));
1347 1348
  switch ((int8)table->algorithm) {
  case VIEW_ALGORITHM_UNDEFINED:
1349
    buff->append(STRING_WITH_LEN("UNDEFINED "));
1350 1351
    break;
  case VIEW_ALGORITHM_TMPTABLE:
1352
    buff->append(STRING_WITH_LEN("TEMPTABLE "));
1353 1354
    break;
  case VIEW_ALGORITHM_MERGE:
1355
    buff->append(STRING_WITH_LEN("MERGE "));
1356 1357 1358 1359
    break;
  default:
    DBUG_ASSERT(0); // never should happen
  }
1360
  append_definer(thd, buff, &table->definer.user, &table->definer.host);
1361
  if (table->view_suid)
1362
    buff->append(STRING_WITH_LEN("SQL SECURITY DEFINER "));
1363
  else
1364
    buff->append(STRING_WITH_LEN("SQL SECURITY INVOKER "));
1365
}
unknown's avatar
unknown committed
1366

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
/*
  Append DEFINER clause to the given buffer.
  
  SYNOPSIS
    append_definer()
    thd           [in] thread handle
    buffer        [inout] buffer to hold DEFINER clause
    definer_user  [in] user name part of definer
    definer_host  [in] host name part of definer
*/

void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user,
                    const LEX_STRING *definer_host)
{
  buffer->append(STRING_WITH_LEN("DEFINER="));
  append_identifier(thd, buffer, definer_user->str, definer_user->length);
  buffer->append('@');
  append_identifier(thd, buffer, definer_host->str, definer_host->length);
  buffer->append(' ');
}


1389
int
unknown's avatar
VIEW  
unknown committed
1390 1391 1392 1393 1394 1395 1396 1397
view_store_create_info(THD *thd, TABLE_LIST *table, String *buff)
{
  my_bool foreign_db_mode= (thd->variables.sql_mode & (MODE_POSTGRESQL |
                                                       MODE_ORACLE |
                                                       MODE_MSSQL |
                                                       MODE_DB2 |
                                                       MODE_MAXDB |
                                                       MODE_ANSI)) != 0;
1398 1399 1400 1401 1402
  /*
     Compact output format for view can be used
     - if user has db of this view as current db
     - if this view only references table inside it's own db
  */
1403
  if (!thd->db || strcmp(thd->db, table->view_db.str))
1404 1405 1406 1407
    table->compact_view_format= FALSE;
  else
  {
    TABLE_LIST *tbl;
1408
    table->compact_view_format= TRUE;
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    for (tbl= thd->lex->query_tables;
         tbl;
         tbl= tbl->next_global)
    {
      if (strcmp(table->view_db.str, tbl->view ? tbl->view_db.str :tbl->db)!= 0)
      {
        table->compact_view_format= FALSE;
        break;
      }
    }
  }

1421
  buff->append(STRING_WITH_LEN("CREATE "));
1422
  if (!foreign_db_mode)
unknown's avatar
VIEW  
unknown committed
1423
  {
1424
    view_store_options(thd, table, buff);
unknown's avatar
VIEW  
unknown committed
1425
  }
1426
  buff->append(STRING_WITH_LEN("VIEW "));
1427 1428 1429 1430 1431
  if (!table->compact_view_format)
  {
    append_identifier(thd, buff, table->view_db.str, table->view_db.length);
    buff->append('.');
  }
1432
  append_identifier(thd, buff, table->view_name.str, table->view_name.length);
1433
  buff->append(STRING_WITH_LEN(" AS "));
1434 1435 1436 1437 1438 1439 1440

  /*
    We can't just use table->query, because our SQL_MODE may trigger
    a different syntax, like when ANSI_QUOTES is defined.
  */
  table->view->unit.print(buff);

1441 1442 1443
  if (table->with_check != VIEW_CHECK_NONE)
  {
    if (table->with_check == VIEW_CHECK_LOCAL)
1444
      buff->append(STRING_WITH_LEN(" WITH LOCAL CHECK OPTION"));
1445
    else
1446
      buff->append(STRING_WITH_LEN(" WITH CASCADED CHECK OPTION"));
1447
  }
unknown's avatar
VIEW  
unknown committed
1448 1449 1450 1451
  return 0;
}


unknown's avatar
unknown committed
1452
/****************************************************************************
1453 1454
  Return info about all processes
  returns for each thread: thread id, user, host, db, command, info
unknown's avatar
unknown committed
1455 1456 1457 1458
****************************************************************************/

class thread_info :public ilink {
public:
unknown's avatar
unknown committed
1459 1460 1461 1462
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
unknown's avatar
unknown committed
1463
  static void operator delete(void *ptr __attribute__((unused)),
unknown's avatar
unknown committed
1464 1465
                              size_t size __attribute__((unused)))
  { TRASH(ptr, size); }
unknown's avatar
unknown committed
1466

unknown's avatar
unknown committed
1467 1468
  ulong thread_id;
  time_t start_time;
1469
  uint   command;
unknown's avatar
unknown committed
1470 1471 1472 1473
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

1474
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
1475 1476 1477 1478 1479 1480 1481 1482
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;
unknown's avatar
unknown committed
1483 1484
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1485
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
1486 1487
  DBUG_ENTER("mysqld_list_processes");

1488
  field_list.push_back(new Item_int("Id",0,11));
unknown's avatar
unknown committed
1489
  field_list.push_back(new Item_empty_string("User",16));
1490
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
unknown's avatar
unknown committed
1491 1492 1493
  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));
1494
  field_list.push_back(new Item_return_int("Time",7, FIELD_TYPE_LONG));
unknown's avatar
unknown committed
1495 1496 1497 1498
  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;
1499 1500
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
1501 1502 1503 1504 1505 1506 1507 1508 1509
    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++))
    {
1510
      Security_context *tmp_sctx= tmp->security_ctx;
1511
      struct st_my_thread_var *mysys_var;
1512
      if ((tmp->vio_ok() || tmp->system_thread) &&
1513
          (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
unknown's avatar
unknown committed
1514
      {
1515
        thread_info *thd_info= new thread_info;
1516 1517

        thd_info->thread_id=tmp->thread_id;
1518 1519 1520 1521 1522
        thd_info->user= thd->strdup(tmp_sctx->user ? tmp_sctx->user :
                                    (tmp->system_thread ?
                                     "system user" : "unauthenticated user"));
	if (tmp->peer_port && (tmp_sctx->host || tmp_sctx->ip) &&
            thd->security_ctx->host_or_ip[0])
1523 1524
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
unknown's avatar
unknown committed
1525
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1526
			"%s:%u", tmp_sctx->host_or_ip, tmp->peer_port);
1527 1528
	}
	else
1529
	  thd_info->host= thd->strdup(tmp_sctx->host_or_ip);
1530 1531 1532
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1533 1534
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
unknown's avatar
SCRUM  
unknown committed
1535
        thd_info->proc_info= (char*) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0);
unknown's avatar
SCRUM  
unknown committed
1536
#ifndef EMBEDDED_LIBRARY
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
        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);
unknown's avatar
SCRUM  
unknown committed
1547 1548 1549
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1550 1551
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
unknown's avatar
unknown committed
1552 1553

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1554 1555
        if (pthread_kill(tmp->real_id,0))
          tmp->proc_info="*** DEAD ***";        // This shouldn't happen
unknown's avatar
unknown committed
1556
#endif
unknown's avatar
unknown committed
1557 1558 1559
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1560
        thd_info->start_time= tmp->start_time;
unknown's avatar
unknown committed
1561
#endif
1562 1563 1564
        thd_info->query=0;
        if (tmp->query)
        {
unknown's avatar
unknown committed
1565 1566 1567 1568 1569
	  /* 
            query_length is always set to 0 when we set query = NULL; see
	    the comment in sql_class.h why this prevents crashes in possible
            races with query_length
          */
unknown's avatar
unknown committed
1570
          uint length= min(max_query_length, tmp->query_length);
1571
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1572 1573
        }
        thread_infos.append(thd_info);
unknown's avatar
unknown committed
1574 1575 1576 1577 1578 1579
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1580
  time_t now= time(0);
unknown's avatar
unknown committed
1581 1582
  while ((thd_info=thread_infos.get()))
  {
1583 1584
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1585 1586 1587
    protocol->store(thd_info->user, system_charset_info);
    protocol->store(thd_info->host, system_charset_info);
    protocol->store(thd_info->db, system_charset_info);
unknown's avatar
unknown committed
1588
    if (thd_info->proc_info)
1589
      protocol->store(thd_info->proc_info, system_charset_info);
unknown's avatar
unknown committed
1590
    else
1591
      protocol->store(command_name[thd_info->command].str, system_charset_info);
unknown's avatar
unknown committed
1592
    if (thd_info->start_time)
1593
      protocol->store((uint32) (now - thd_info->start_time));
unknown's avatar
unknown committed
1594
    else
1595
      protocol->store_null();
1596 1597
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1598
    if (protocol->write())
unknown's avatar
unknown committed
1599 1600
      break; /* purecov: inspected */
  }
1601
  send_eof(thd);
unknown's avatar
unknown committed
1602 1603 1604
  DBUG_VOID_RETURN;
}

1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
{
  TABLE *table= tables->table;
  CHARSET_INFO *cs= system_charset_info;
  char *user;
  time_t now= time(0);
  DBUG_ENTER("fill_process_list");

  user= thd->security_ctx->master_access & PROCESS_ACL ?
        NullS : thd->security_ctx->priv_user;

  VOID(pthread_mutex_lock(&LOCK_thread_count));

  if (!thd->killed)
  {
    I_List_iterator<THD> it(threads);
    THD* tmp;

    while ((tmp= it++))
    {
      Security_context *tmp_sctx= tmp->security_ctx;
      struct st_my_thread_var *mysys_var;
      const char *val;

      if ((!tmp->vio_ok() && !tmp->system_thread) ||
          (user && (!tmp_sctx->user || strcmp(tmp_sctx->user, user))))
        continue;

      restore_record(table, s->default_values);
      /* ID */
      table->field[0]->store((longlong) tmp->thread_id, TRUE);
      /* USER */
      val= tmp_sctx->user ? tmp_sctx->user :
            (tmp->system_thread ? "system user" : "unauthenticated user");
      table->field[1]->store(val, strlen(val), cs);
      /* HOST */
      if (tmp->peer_port && (tmp_sctx->host || tmp_sctx->ip) &&
          thd->security_ctx->host_or_ip[0])
      {
        char host[LIST_PROCESS_HOST_LEN + 1];
        my_snprintf(host, LIST_PROCESS_HOST_LEN, "%s:%u",
                    tmp_sctx->host_or_ip, tmp->peer_port);
        table->field[2]->store(host, strlen(host), cs);
      }
      else
        table->field[2]->store(tmp_sctx->host_or_ip,
                               strlen(tmp_sctx->host_or_ip), cs);
      /* DB */
      if (tmp->db)
      {
        table->field[3]->store(tmp->db, strlen(tmp->db), cs);
        table->field[3]->set_notnull();
      }

      if ((mysys_var= tmp->mysys_var))
        pthread_mutex_lock(&mysys_var->mutex);
      /* COMMAND */
      if ((val= (char *) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0)))
        table->field[4]->store(val, strlen(val), cs);
      else
        table->field[4]->store(command_name[tmp->command].str,
                               command_name[tmp->command].length, cs);
      /* TIME */
      table->field[5]->store((uint32)(tmp->start_time ?
                                      now - tmp->start_time : 0), TRUE);
      /* STATE */
#ifndef EMBEDDED_LIBRARY
      val= (char*) (tmp->locked ? "Locked" :
                    tmp->net.reading_or_writing ?
                    (tmp->net.reading_or_writing == 2 ?
                     "Writing to net" :
                     tmp->command == COM_SLEEP ? "" :
                     "Reading from net") :
                    tmp->proc_info ? tmp->proc_info :
                    tmp->mysys_var &&
                    tmp->mysys_var->current_cond ?
                    "Waiting on cond" : NullS);
#else
      val= (char *) "Writing to net";
#endif
      if (val)
      {
        table->field[6]->store(val, strlen(val), cs);
        table->field[6]->set_notnull();
      }

      if (mysys_var)
        pthread_mutex_unlock(&mysys_var->mutex);

      /* INFO */
      if (tmp->query)
      {
        table->field[7]->store(tmp->query,
1698 1699
                               min(PROCESS_LIST_INFO_WIDTH,
                                   tmp->query_length), cs);
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
        table->field[7]->set_notnull();
      }

      if (schema_table_store_record(thd, table))
      {
        VOID(pthread_mutex_unlock(&LOCK_thread_count));
        DBUG_RETURN(1);
      }
    }
  }

  VOID(pthread_mutex_unlock(&LOCK_thread_count));
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
1715
/*****************************************************************************
unknown's avatar
unknown committed
1716
  Status functions
unknown's avatar
unknown committed
1717 1718
*****************************************************************************/

1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
static DYNAMIC_ARRAY all_status_vars;
static bool status_vars_inited= 0;
static int show_var_cmp(const void *var1, const void *var2)
{
  return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
}

/*
  deletes all the SHOW_UNDEF elements from the array and calls
  delete_dynamic() if it's completely empty.
*/
static void shrink_var_array(DYNAMIC_ARRAY *array)
{
1732
  uint a,b;
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);

  for (a= b= 0; b < array->elements; b++)
    if (all[b].type != SHOW_UNDEF)
      all[a++]= all[b];
  if (a)
  {
    bzero(all+a, sizeof(SHOW_VAR)); // writing NULL-element to the end
    array->elements= a;
  }
  else // array is completely empty - delete it
    delete_dynamic(array);
}

/*
  Adds an array of SHOW_VAR entries to the output of SHOW STATUS

  SYNOPSIS
    add_status_vars(SHOW_VAR *list)
    list - an array of SHOW_VAR entries to add to all_status_vars
           the last entry must be {0,0,SHOW_UNDEF}

  NOTE
    The handling of all_status_vars[] is completely internal, it's allocated
    automatically when something is added to it, and deleted completely when
    the last entry is removed.

    As a special optimization, if add_status_vars() is called before
    init_status_vars(), it assumes "startup mode" - neither concurrent access
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)

    The last entry of the all_status_vars[] should always be {0,0,SHOW_UNDEF}
*/
int add_status_vars(SHOW_VAR *list)
{
  int res= 0;
  if (status_vars_inited)
    pthread_mutex_lock(&LOCK_status);
  if (!all_status_vars.buffer && // array is not allocated yet - do it now
      my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20))
  {
    res= 1;
    goto err;
  }
  while (list->name)
    res|= insert_dynamic(&all_status_vars, (gptr)list++);
  res|= insert_dynamic(&all_status_vars, (gptr)list); // appending NULL-element
  all_status_vars.elements--; // but next insert_dynamic should overwite it
  if (status_vars_inited)
    sort_dynamic(&all_status_vars, show_var_cmp);
err:
  if (status_vars_inited)
    pthread_mutex_unlock(&LOCK_status);
  return res;
}

/*
  Make all_status_vars[] usable for SHOW STATUS

  NOTE
    See add_status_vars(). Before init_status_vars() call, add_status_vars()
    works in a special fast "startup" mode. Thus init_status_vars()
    should be called as late as possible but before enabling multi-threading.
*/
void init_status_vars()
{
  status_vars_inited=1;
  sort_dynamic(&all_status_vars, show_var_cmp);
}

/*
  catch-all cleanup function, cleans up everything no matter what

  DESCRIPTION
    This function is not strictly required if all add_to_status/
    remove_status_vars are properly paired, but it's a safety measure that
    deletes everything from the all_status_vars[] even if some
    remove_status_vars were forgotten
*/
void free_status_vars()
{
  delete_dynamic(&all_status_vars);
}

/*
  Removes an array of SHOW_VAR entries from the output of SHOW STATUS

  SYNOPSIS
    remove_status_vars(SHOW_VAR *list)
    list - an array of SHOW_VAR entries to remove to all_status_vars
           the last entry must be {0,0,SHOW_UNDEF}

  NOTE
    there's lots of room for optimizing this, especially in non-sorted mode,
    but nobody cares - it may be called only in case of failed plugin
    initialization in the mysqld startup.
*/
1830

1831 1832 1833 1834 1835 1836
void remove_status_vars(SHOW_VAR *list)
{
  if (status_vars_inited)
  {
    pthread_mutex_lock(&LOCK_status);
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1837
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
1838 1839 1840

    for (; list->name; list++)
    {
1841
      int res= 0;
1842 1843 1844 1845 1846 1847 1848
      for (a= 0, b= all_status_vars.elements; b-a > 1; c= (a+b)/2)
      {
        res= show_var_cmp(list, all+c);
        if (res < 0)
          b= c;
        else if (res > 0)
          a= c;
1849 1850
        else
          break;
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
      }
      if (res == 0)
        all[c].type= SHOW_UNDEF;
    }
    shrink_var_array(&all_status_vars);
    pthread_mutex_unlock(&LOCK_status);
  }
  else
  {
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1861
    uint i;
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    for (; list->name; list++)
    {
      for (i= 0; i < all_status_vars.elements; i++)
      {
        if (show_var_cmp(list, all+i))
          continue;
        all[i].type= SHOW_UNDEF;
        break;
      }
    }
    shrink_var_array(&all_status_vars);
  }
}

1876
static bool show_status_array(THD *thd, const char *wild,
1877
                              SHOW_VAR *variables,
1878 1879 1880
                              enum enum_var_type value_type,
                              struct system_status_var *status_var,
                              const char *prefix, TABLE *table)
unknown's avatar
unknown committed
1881
{
1882
  char buff[SHOW_VAR_FUNC_BUFF_SIZE], *prefix_end;
1883 1884 1885
  /* the variable name should not be longer then 80 characters */
  char name_buffer[80];
  int len;
1886
  LEX_STRING null_lex_str;
1887
  SHOW_VAR tmp, *var;
1888
  DBUG_ENTER("show_status_array");
unknown's avatar
unknown committed
1889

1890
  null_lex_str.str= 0;				// For sys_var->value_ptr()
1891
  null_lex_str.length= 0;
unknown's avatar
unknown committed
1892

1893
  prefix_end=strnmov(name_buffer, prefix, sizeof(name_buffer)-1);
1894 1895
  if (*prefix)
    *prefix_end++= '_';
1896 1897
  len=name_buffer + sizeof(name_buffer) - prefix_end;

unknown's avatar
unknown committed
1898
  for (; variables->name; variables++)
unknown's avatar
unknown committed
1899
  {
1900 1901
    strnmov(prefix_end, variables->name, len);
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
1902

1903 1904 1905 1906
    /*
      if var->type is SHOW_FUNC, call the function.
      Repeat as necessary, if new var is again SHOW_FUNC
    */
1907
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
1908
      ((mysql_show_var_func)(var->value))(thd, &tmp, buff);
1909 1910 1911

    SHOW_TYPE show_type=var->type;
    if (show_type == SHOW_ARRAY)
1912
    {
1913
      show_status_array(thd, wild, (SHOW_VAR *) var->value,
1914
                        value_type, status_var, name_buffer, table);
1915 1916
    }
    else
unknown's avatar
unknown committed
1917
    {
1918 1919
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
unknown's avatar
unknown committed
1920
      {
1921
        char *value=var->value;
unknown's avatar
unknown committed
1922
        const char *pos, *end;                  // We assign a lot of const's
1923 1924 1925 1926 1927 1928 1929 1930 1931
        long nr;
        if (show_type == SHOW_SYS)
        {
          show_type= ((sys_var*) value)->type();
          value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type,
                                                           &null_lex_str);
        }

        pos= end= buff;
1932 1933 1934 1935
        /*
          note that value may be == buff. All SHOW_xxx code below
          should still work in this case
        */
1936
        switch (show_type) {
1937 1938 1939 1940 1941 1942
        case SHOW_DOUBLE_STATUS:
        {
          value= ((char *) status_var + (ulong) value);
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
1943 1944 1945 1946
        case SHOW_LONG_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
1947
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
          end= int10_to_str(*(long*) value, buff, 10);
          break;
        case SHOW_LONGLONG:
          end= longlong10_to_str(*(longlong*) value, buff, 10);
          break;
        case SHOW_HA_ROWS:
          end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
          break;
        case SHOW_BOOL:
          end= strmov(buff, *(bool*) value ? "ON" : "OFF");
          break;
        case SHOW_MY_BOOL:
          end= strmov(buff, *(my_bool*) value ? "ON" : "OFF");
          break;
        case SHOW_INT:
          end= int10_to_str((long) *(uint32*) value, buff, 10);
          break;
        case SHOW_HAVE:
unknown's avatar
unknown committed
1966 1967 1968 1969 1970 1971
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
1972
        case SHOW_CHAR:
unknown's avatar
unknown committed
1973 1974 1975 1976 1977 1978
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
1979
        case SHOW_CHAR_PTR:
unknown's avatar
unknown committed
1980 1981 1982 1983 1984 1985
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
1986
        case SHOW_KEY_CACHE_LONG:
1987
          value= (char*) dflt_key_cache + (ulong)value;
1988 1989
          end= int10_to_str(*(long*) value, buff, 10);
          break;
1990
        case SHOW_KEY_CACHE_LONGLONG:
1991
          value= (char*) dflt_key_cache + (ulong)value;
1992 1993
	  end= longlong10_to_str(*(longlong*) value, buff, 10);
	  break;
1994
        case SHOW_UNDEF:
1995 1996
          break;                                        // Return empty string
        case SHOW_SYS:                                  // Cannot happen
1997
        default:
1998
          DBUG_ASSERT(0);
1999 2000
          break;
        }
2001
        restore_record(table, s->default_values);
2002 2003 2004
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
2005 2006
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
2007 2008 2009
      }
    }
  }
2010 2011 2012 2013 2014

  DBUG_RETURN(FALSE);
}


2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
/* collect status for all running threads */

void calc_sum_of_all_status(STATUS_VAR *to)
{
  DBUG_ENTER("calc_sum_of_all_status");

  /* Ensure that thread id not killed during loop */
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list

  I_List_iterator<THD> it(threads);
  THD *tmp;
  
  /* Get global values as base */
  *to= global_status_var;
  
  /* Add to this status from existing threads */
  while ((tmp= it++))
    add_to_status(to, &tmp->status_var);
  
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
  DBUG_VOID_RETURN;
}


2039 2040 2041 2042 2043 2044
LEX_STRING *make_lex_string(THD *thd, LEX_STRING *lex_str,
                            const char* str, uint length,
                            bool allocate_lex_string)
{
  MEM_ROOT *mem= thd->mem_root;
  if (allocate_lex_string)
2045 2046
    if (!(lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING))))
      return 0;
2047 2048 2049 2050 2051 2052 2053 2054
  lex_str->str= strmake_root(mem, str, length);
  lex_str->length= length;
  return lex_str;
}


/* INFORMATION_SCHEMA name */
LEX_STRING information_schema_name= {(char*)"information_schema", 18};
unknown's avatar
unknown committed
2055 2056

/* This is only used internally, but we need it here as a forward reference */
2057 2058 2059 2060 2061 2062 2063 2064
extern ST_SCHEMA_TABLE schema_tables[];

typedef struct st_index_field_values
{
  const char *db_value, *table_value;
} INDEX_FIELD_VALUES;


2065 2066 2067 2068 2069 2070 2071
/*
  Store record to I_S table, convert HEAP table
  to MyISAM if necessary

  SYNOPSIS
    schema_table_store_record()
    thd                   thread handler
2072 2073
    table                 Information schema table to be updated

2074 2075
  RETURN
    0	                  success
2076
    1	                  error
2077 2078
*/

2079
bool schema_table_store_record(THD *thd, TABLE *table)
2080 2081
{
  int error;
2082
  if ((error= table->file->ha_write_row(table->record[0])))
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
  {
    if (create_myisam_from_heap(thd, table, 
                                table->pos_in_table_list->schema_table_param,
                                error, 0))
      return 1;
  }
  return 0;
}


2093 2094 2095 2096 2097 2098 2099 2100 2101
void get_index_field_values(LEX *lex, INDEX_FIELD_VALUES *index_field_values)
{
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
  switch (lex->orig_sql_command) {
  case SQLCOM_SHOW_DATABASES:
    index_field_values->db_value= wild;
    break;
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_SHOW_TABLE_STATUS:
2102
  case SQLCOM_SHOW_TRIGGERS:
2103
  case SQLCOM_SHOW_EVENTS:
unknown's avatar
unknown committed
2104
    index_field_values->db_value= lex->select_lex.db;
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
    index_field_values->table_value= wild;
    break;
  default:
    index_field_values->db_value= NullS;
    index_field_values->table_value= NullS;
    break;
  }
}


int make_table_list(THD *thd, SELECT_LEX *sel,
                    char *db, char *table)
{
  Table_ident *table_ident;
  LEX_STRING ident_db, ident_table;
  ident_db.str= db; 
  ident_db.length= strlen(db);
  ident_table.str= table;
  ident_table.length= strlen(table);
  table_ident= new Table_ident(thd, ident_db, ident_table, 1);
  sel->init_query();
unknown's avatar
unknown committed
2126
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
                             (List<String> *) 0, (List<String> *) 0))
    return 1;
  return 0;
}


bool uses_only_table_name_fields(Item *item, TABLE_LIST *table)
{
  if (item->type() == Item::FUNC_ITEM)
  {
    Item_func *item_func= (Item_func*)item;
    Item **child;
    Item **item_end= (item_func->arguments()) + item_func->argument_count();
    for (child= item_func->arguments(); child != item_end; child++)
2141
    {
2142 2143
      if (!uses_only_table_name_fields(*child, table))
        return 0;
2144
    }
2145 2146 2147 2148 2149 2150 2151
  }
  else if (item->type() == Item::FIELD_ITEM)
  {
    Item_field *item_field= (Item_field*)item;
    CHARSET_INFO *cs= system_charset_info;
    ST_SCHEMA_TABLE *schema_table= table->schema_table;
    ST_FIELD_INFO *field_info= schema_table->fields_info;
unknown's avatar
unknown committed
2152 2153
    const char *field_name1= schema_table->idx_field1 >= 0 ? field_info[schema_table->idx_field1].field_name : "";
    const char *field_name2= schema_table->idx_field2 >= 0 ? field_info[schema_table->idx_field2].field_name : "";
2154 2155 2156 2157 2158 2159 2160
    if (table->table != item_field->field->table ||
        (cs->coll->strnncollsp(cs, (uchar *) field_name1, strlen(field_name1),
                               (uchar *) item_field->field_name, 
                               strlen(item_field->field_name), 0) &&
         cs->coll->strnncollsp(cs, (uchar *) field_name2, strlen(field_name2),
                               (uchar *) item_field->field_name, 
                               strlen(item_field->field_name), 0)))
2161 2162
      return 0;
  }
2163 2164
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
2165 2166 2167 2168
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

2169
  return 1;
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
}


static COND * make_cond_for_info_schema(COND *cond, TABLE_LIST *table)
{
  if (!cond)
    return (COND*) 0;
  if (cond->type() == Item::COND_ITEM)
  {
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
    {
      /* Create new top level AND item */
      Item_cond_and *new_cond=new Item_cond_and;
      if (!new_cond)
	return (COND*) 0;
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
      Item *item;
      while ((item=li++))
      {
	Item *fix= make_cond_for_info_schema(item, table);
	if (fix)
	  new_cond->argument_list()->push_back(fix);
      }
      switch (new_cond->argument_list()->elements) {
      case 0:
	return (COND*) 0;
      case 1:
	return new_cond->argument_list()->head();
      default:
	new_cond->quick_fix_field();
	return new_cond;
      }
    }
    else
    {						// Or list
      Item_cond_or *new_cond=new Item_cond_or;
      if (!new_cond)
	return (COND*) 0;
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
      Item *item;
      while ((item=li++))
      {
	Item *fix=make_cond_for_info_schema(item, table);
	if (!fix)
	  return (COND*) 0;
	new_cond->argument_list()->push_back(fix);
      }
      new_cond->quick_fix_field();
      new_cond->top_level_item();
      return new_cond;
    }
  }

  if (!uses_only_table_name_fields(cond, table))
    return (COND*) 0;
  return cond;
}


2229 2230 2231 2232 2233 2234
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
{
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
}


unknown's avatar
unknown committed
2235
/*
2236
  Create db names list. Information schema name always is first in list
unknown's avatar
unknown committed
2237 2238

  SYNOPSIS
2239
    make_db_list()
unknown's avatar
unknown committed
2240 2241 2242
    thd                   thread handler
    files                 list of db names
    wild                  wild string
2243 2244
    idx_field_vals        idx_field_vals->db_name contains db name or
                          wild string
unknown's avatar
unknown committed
2245 2246
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0
2247 2248
    is_wild_value         if value is 1 then idx_field_vals->db_name is
                          wild string otherwise it's db name; 
unknown's avatar
unknown committed
2249 2250 2251 2252 2253 2254

  RETURN
    1	                  error
    0	                  success
*/

2255 2256 2257
int make_db_list(THD *thd, List<char> *files,
                 INDEX_FIELD_VALUES *idx_field_vals,
                 bool *with_i_schema, bool is_wild_value)
2258
{
2259
  LEX *lex= thd->lex;
unknown's avatar
unknown committed
2260
  *with_i_schema= 0;
2261 2262
  get_index_field_values(lex, idx_field_vals);
  if (is_wild_value)
unknown's avatar
unknown committed
2263
  {
2264 2265 2266 2267 2268
    /*
      This part of code is only for SHOW DATABASES command.
      idx_field_vals->db_value can be 0 when we don't use
      LIKE clause (see also get_index_field_values() function)
    */
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
    if (!idx_field_vals->db_value ||
        !wild_case_compare(system_charset_info, 
                           information_schema_name.str,
                           idx_field_vals->db_value))
    {
      *with_i_schema= 1;
      if (files->push_back(thd->strdup(information_schema_name.str)))
        return 1;
    }
    return mysql_find_files(thd, files, NullS, mysql_data_home,
                            idx_field_vals->db_value, 1);
unknown's avatar
unknown committed
2280
  }
2281

2282 2283 2284 2285 2286 2287
  /*
    This part of code is for SHOW TABLES, SHOW TABLE STATUS commands.
    idx_field_vals->db_value can't be 0 (see get_index_field_values()
    function). lex->orig_sql_command can be not equal to SQLCOM_END
    only in case of executing of SHOW commands.
  */
2288 2289
  if (lex->orig_sql_command != SQLCOM_END)
  {
2290
    if (!my_strcasecmp(system_charset_info, information_schema_name.str,
2291 2292 2293 2294 2295 2296 2297 2298
                       idx_field_vals->db_value))
    {
      *with_i_schema= 1;
      return files->push_back(thd->strdup(information_schema_name.str));
    }
    return files->push_back(thd->strdup(idx_field_vals->db_value));
  }

2299 2300 2301 2302
  /*
    Create list of existing databases. It is used in case
    of select from information schema table
  */
2303 2304 2305 2306
  if (files->push_back(thd->strdup(information_schema_name.str)))
    return 1;
  *with_i_schema= 1;
  return mysql_find_files(thd, files, NullS, mysql_data_home, NullS, 1);
2307 2308 2309 2310 2311 2312
}


int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2313
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2314
  {
2315 2316
    if (tmp_schema_table->hidden)
      continue;
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
    if (wild)
    {
      if (lower_case_table_names)
      {
        if (wild_case_compare(files_charset_info,
                              tmp_schema_table->table_name,
                              wild))
          continue;
      }
      else if (wild_compare(tmp_schema_table->table_name, wild, 0))
        continue;
    }
    if (files->push_back(thd->strdup(tmp_schema_table->table_name)))
      return 1;
  }
  return 0;
}


2336 2337 2338 2339 2340
int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  LEX *lex= thd->lex;
  TABLE *table= tables->table;
  SELECT_LEX *select_lex= &lex->select_lex;
2341
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2342 2343
  TABLE_LIST **save_query_tables_last= lex->query_tables_last;
  enum_sql_command save_sql_command= lex->sql_command;
unknown's avatar
unknown committed
2344
  SELECT_LEX *lsel= tables->schema_select_lex;
2345
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2346 2347 2348 2349 2350 2351 2352
  SELECT_LEX sel;
  INDEX_FIELD_VALUES idx_field_vals;
  char path[FN_REFLEN], *end, *base_name, *file_name;
  uint len;
  bool with_i_schema;
  enum enum_schema_tables schema_table_idx;
  List<char> bases;
unknown's avatar
unknown committed
2353
  List_iterator_fast<char> it(bases);
2354
  COND *partial_cond;
2355
  Security_context *sctx= thd->security_ctx;
2356
  uint derived_tables= lex->derived_tables; 
unknown's avatar
unknown committed
2357
  int error= 1;
unknown's avatar
unknown committed
2358
  enum legacy_db_type not_used;
2359
  Open_tables_state open_tables_state_backup;
2360 2361
  bool save_view_prepare_mode= lex->view_prepare_mode;
  lex->view_prepare_mode= TRUE;
2362 2363 2364 2365
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
2366

2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
  /*
    Let us set fake sql_command so views won't try to merge
    themselves into main statement.
  */
  lex->sql_command= SQLCOM_SHOW_FIELDS;

  /*
    We should not introduce deadlocks even if we already have some
    tables open and locked, since we won't lock tables which we will
    open and will ignore possible name-locks for these tables.
  */
  thd->reset_n_backup_open_tables_state(&open_tables_state_backup);

2380 2381 2382
  if (lsel)
  {
    TABLE_LIST *show_table_list= (TABLE_LIST*) lsel->table_list.first;
2383 2384
    bool res;

2385
    lex->all_selects_list= lsel;
2386 2387 2388 2389 2390 2391 2392
    /*
      Restore thd->temporary_tables to be able to process
      temporary tables(only for 'show index' & 'show columns').
      This should be changed when processing of temporary tables for
      I_S tables will be done.
    */
    thd->temporary_tables= open_tables_state_backup.temporary_tables;
2393 2394
    res= open_normal_and_derived_tables(thd, show_table_list,
                                        MYSQL_LOCK_IGNORE_FLUSH);
2395 2396 2397
    /*
      get_all_tables() returns 1 on failure and 0 on success thus
      return only these and not the result code of ::process_table()
2398 2399 2400 2401 2402 2403 2404

      We should use show_table_list->alias instead of 
      show_table_list->table_name because table_name
      could be changed during opening of I_S tables. It's safe
      to use alias because alias contains original table name 
      in this case(this part of code is used only for 
      'show columns' & 'show statistics' commands).
2405 2406
    */
    error= test(schema_table->process_table(thd, show_table_list,
2407 2408 2409 2410 2411
                                            table, res, 
                                            (show_table_list->view ?
                                             show_table_list->view_db.str :
                                             show_table_list->db),
                                            show_table_list->alias));
2412
    thd->temporary_tables= 0;
2413
    close_thread_tables(thd);
2414
    show_table_list->table= 0;
unknown's avatar
unknown committed
2415
    goto err;
2416 2417
  }

2418
  schema_table_idx= get_schema_table_idx(schema_table);
2419

2420 2421
  if (make_db_list(thd, &bases, &idx_field_vals,
                   &with_i_schema, 0))
unknown's avatar
unknown committed
2422
    goto err;
2423 2424

  partial_cond= make_cond_for_info_schema(cond, tables);
unknown's avatar
unknown committed
2425
  it.rewind(); /* To get access to new elements in basis list */
2426
  while ((base_name= it++) ||
2427 2428 2429 2430 2431 2432 2433 2434 2435
	 /*
	   generate error for non existing database.
	   (to save old behaviour for SHOW TABLES FROM db)
	 */
	 ((lex->orig_sql_command == SQLCOM_SHOW_TABLES ||
           lex->orig_sql_command == SQLCOM_SHOW_TABLE_STATUS) &&
	  (base_name= select_lex->db) && !bases.elements))
  {
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2436 2437
    if (!check_access(thd,SELECT_ACL, base_name, 
                      &thd->col_access, 0, 1, with_i_schema) ||
2438 2439
        sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, base_name,0) ||
2440 2441 2442 2443
	(grant_option && !check_grant_db(thd, base_name)))
#endif
    {
      List<char> files;
2444 2445 2446
      if (with_i_schema)                      // information schema table names
      {
        if (schema_tables_add(thd, &files, idx_field_vals.table_value))
unknown's avatar
unknown committed
2447
          goto err;
2448 2449 2450
      }
      else
      {
2451 2452
        len= build_table_filename(path, sizeof(path), base_name, "", "");
        end= path + len;
2453 2454 2455
        len= FN_LEN - len;
        if (mysql_find_files(thd, &files, base_name, 
                             path, idx_field_vals.table_value, 0))
unknown's avatar
unknown committed
2456
          goto err;
2457
      }
2458

2459 2460
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
2461
      {
2462
	restore_record(table, s->default_values);
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
        table->field[schema_table->idx_field1]->
          store(base_name, strlen(base_name), system_charset_info);
        table->field[schema_table->idx_field2]->
          store(file_name, strlen(file_name),system_charset_info);
        if (!partial_cond || partial_cond->val_int())
        {
          if (schema_table_idx == SCH_TABLE_NAMES)
          {
            if (lex->verbose || lex->orig_sql_command == SQLCOM_END)
            {
2473
              if (with_i_schema)
2474
              {
2475 2476
                table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
                                       system_charset_info);
2477 2478 2479 2480
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
2481
                switch (mysql_frm_type(thd, path, &not_used)) {
2482
                case FRMTYPE_ERROR:
2483 2484
                  table->field[3]->store(STRING_WITH_LEN("ERROR"),
                                         system_charset_info);
2485 2486
                  break;
                case FRMTYPE_TABLE:
2487 2488
                  table->field[3]->store(STRING_WITH_LEN("BASE TABLE"),
                                         system_charset_info);
2489 2490
                  break;
                case FRMTYPE_VIEW:
2491 2492
                  table->field[3]->store(STRING_WITH_LEN("VIEW"),
                                         system_charset_info);
2493 2494 2495 2496
                  break;
                default:
                  DBUG_ASSERT(0);
                }
2497 2498
              }
            }
2499
            if (schema_table_store_record(thd, table))
2500
              goto err;
2501 2502 2503 2504
          }
          else
          {
            int res;
unknown's avatar
unknown committed
2505 2506 2507 2508 2509
            /*
              Set the parent lex of 'sel' because it is needed by sel.init_query()
              which is called inside make_table_list.
            */
            sel.parent_lex= lex;
2510
            if (make_table_list(thd, &sel, base_name, file_name))
unknown's avatar
unknown committed
2511
              goto err;
2512
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
2513 2514
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
2515 2516
            res= open_normal_and_derived_tables(thd, show_table_list,
                                                MYSQL_LOCK_IGNORE_FLUSH);
2517 2518 2519 2520 2521 2522 2523
            /*
              We should use show_table_list->alias instead of 
              show_table_list->table_name because table_name
              could be changed during opening of I_S tables. It's safe
              to use alias because alias contains original table name 
              in this case.
            */
2524
            res= schema_table->process_table(thd, show_table_list, table,
2525
                                            res, base_name,
2526
                                            show_table_list->alias);
2527
            close_thread_tables(thd);
2528
            if (res)
unknown's avatar
unknown committed
2529
              goto err;
2530 2531 2532
          }
        }
      }
2533 2534 2535 2536
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
2537
      with_i_schema= 0;
2538 2539
    }
  }
unknown's avatar
unknown committed
2540 2541 2542

  error= 0;
err:
2543
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2544 2545
  lex->derived_tables= derived_tables;
  lex->all_selects_list= old_all_select_lex;
2546
  lex->query_tables_last= save_query_tables_last;
2547
  lex->view_prepare_mode= save_view_prepare_mode;
2548 2549
  *save_query_tables_last= 0;
  lex->sql_command= save_sql_command;
unknown's avatar
unknown committed
2550
  DBUG_RETURN(error);
2551 2552 2553
}


2554
bool store_schema_shemata(THD* thd, TABLE *table, const char *db_name,
2555
                          CHARSET_INFO *cs)
2556
{
2557
  restore_record(table, s->default_values);
2558
  table->field[1]->store(db_name, strlen(db_name), system_charset_info);
2559 2560
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2561
  return schema_table_store_record(thd, table);
2562 2563 2564
}


2565 2566
int fill_schema_shemata(THD *thd, TABLE_LIST *tables, COND *cond)
{
2567
  char path[FN_REFLEN];
2568 2569 2570 2571 2572
  bool found_libchar;
  INDEX_FIELD_VALUES idx_field_vals;
  List<char> files;
  char *file_name;
  uint length;
unknown's avatar
unknown committed
2573
  bool with_i_schema;
2574 2575
  HA_CREATE_INFO create;
  TABLE *table= tables->table;
2576
  Security_context *sctx= thd->security_ctx;
2577
  DBUG_ENTER("fill_schema_shemata");
2578

2579 2580
  if (make_db_list(thd, &files, &idx_field_vals,
                   &with_i_schema, 1))
2581
    DBUG_RETURN(1);
2582

2583 2584 2585
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2586 2587
    if (with_i_schema)       // information schema name is always first in list
    {
2588
      if (store_schema_shemata(thd, table, file_name,
2589
                               system_charset_info))
2590
        DBUG_RETURN(1);
2591 2592 2593
      with_i_schema= 0;
      continue;
    }
2594
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2595 2596
    if (sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, file_name,0) ||
2597 2598 2599
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2600
      length= build_table_filename(path, sizeof(path), file_name, "", "");
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
      found_libchar= 0;
      if (length && path[length-1] == FN_LIBCHAR)
      {
	found_libchar= 1;
	path[length-1]=0;			// remove ending '\'
      }

      if (found_libchar)
	path[length-1]= FN_LIBCHAR;
      strmov(path+length, MY_DB_OPT_FILE);
      load_db_opt(thd, path, &create);
2612
      if (store_schema_shemata(thd, table, file_name, 
2613
                               create.default_table_charset))
2614
        DBUG_RETURN(1);
2615 2616
    }
  }
2617
  DBUG_RETURN(0);
2618 2619 2620
}


unknown's avatar
unknown committed
2621 2622 2623 2624
static int get_schema_tables_record(THD *thd, struct st_table_list *tables,
				    TABLE *table, bool res,
				    const char *base_name,
				    const char *file_name)
2625 2626 2627 2628 2629
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2630 2631

  restore_record(table, s->default_values);
2632 2633
  table->field[1]->store(base_name, strlen(base_name), cs);
  table->field[2]->store(file_name, strlen(file_name), cs);
unknown's avatar
unknown committed
2634 2635 2636 2637 2638 2639
  if (res)
  {
    /*
      there was errors during opening tables
    */
    const char *error= thd->net.last_error;
2640 2641 2642 2643 2644 2645
    if (tables->view)
      table->field[3]->store(STRING_WITH_LEN("VIEW"), cs);
    else if (tables->schema_table)
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
    else
      table->field[3]->store(STRING_WITH_LEN("BASE TABLE"), cs);
unknown's avatar
unknown committed
2646 2647 2648 2649
    table->field[20]->store(error, strlen(error), cs);
    thd->clear_error();
  }
  else if (tables->view)
2650
  {
2651 2652
    table->field[3]->store(STRING_WITH_LEN("VIEW"), cs);
    table->field[20]->store(STRING_WITH_LEN("VIEW"), cs);
2653 2654 2655 2656
  }
  else
  {
    TABLE *show_table= tables->table;
2657
    TABLE_SHARE *share= show_table->s;
2658
    handler *file= show_table->file;
2659

2660 2661
    file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_AUTO |
               HA_STATUS_NO_LOCK);
2662
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2663
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2664
    else if (share->tmp_table)
2665
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
2666
    else
2667
      table->field[3]->store(STRING_WITH_LEN("BASE TABLE"), cs);
2668

2669 2670
    for (int i= 4; i < 20; i++)
    {
2671
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2672 2673 2674 2675 2676
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2677
    table->field[5]->store((longlong) share->frm_version, TRUE);
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
    enum row_type row_type = file->get_row_type();
    switch (row_type) {
    case ROW_TYPE_NOT_USED:
    case ROW_TYPE_DEFAULT:
      tmp_buff= ((share->db_options_in_use &
		  HA_OPTION_COMPRESS_RECORD) ? "Compressed" :
		 (share->db_options_in_use & HA_OPTION_PACK_RECORD) ?
		 "Dynamic" : "Fixed");
      break;
    case ROW_TYPE_FIXED:
      tmp_buff= "Fixed";
      break;
    case ROW_TYPE_DYNAMIC:
      tmp_buff= "Dynamic";
      break;
    case ROW_TYPE_COMPRESSED:
      tmp_buff= "Compressed";
      break;
    case ROW_TYPE_REDUNDANT:
      tmp_buff= "Redundant";
      break;
    case ROW_TYPE_COMPACT:
      tmp_buff= "Compact";
      break;
    }
2703
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2704 2705
    if (!tables->schema_table)
    {
2706
      table->field[7]->store((longlong) file->records, TRUE);
2707 2708
      table->field[7]->set_notnull();
    }
2709 2710
    table->field[8]->store((longlong) file->mean_rec_length, TRUE);
    table->field[9]->store((longlong) file->data_file_length, TRUE);
2711 2712
    if (file->max_data_file_length)
    {
2713
      table->field[10]->store((longlong) file->max_data_file_length, TRUE);
2714
    }
2715 2716
    table->field[11]->store((longlong) file->index_file_length, TRUE);
    table->field[12]->store((longlong) file->delete_length, TRUE);
2717
    if (show_table->found_next_number_field)
2718
    {
2719
      table->field[13]->store((longlong) file->auto_increment_value, TRUE);
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
      table->field[13]->set_notnull();
    }
    if (file->create_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                                file->create_time);
      table->field[14]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[14]->set_notnull();
    }
    if (file->update_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                                file->update_time);
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[15]->set_notnull();
    }
    if (file->check_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time, file->check_time);
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[16]->set_notnull();
    }
2742 2743
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2744
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
unknown's avatar
unknown committed
2745
    if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
2746
    {
2747
      table->field[18]->store((longlong) file->checksum(), TRUE);
2748 2749 2750 2751 2752
      table->field[18]->set_notnull();
    }

    char option_buff[350],*ptr;
    ptr=option_buff;
2753
    if (share->min_rows)
2754 2755
    {
      ptr=strmov(ptr," min_rows=");
2756
      ptr=longlong10_to_str(share->min_rows,ptr,10);
2757
    }
2758
    if (share->max_rows)
2759 2760
    {
      ptr=strmov(ptr," max_rows=");
2761
      ptr=longlong10_to_str(share->max_rows,ptr,10);
2762
    }
2763
    if (share->avg_row_length)
2764 2765
    {
      ptr=strmov(ptr," avg_row_length=");
2766
      ptr=longlong10_to_str(share->avg_row_length,ptr,10);
2767
    }
2768
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2769
      ptr=strmov(ptr," pack_keys=1");
2770
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2771
      ptr=strmov(ptr," pack_keys=0");
2772
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2773
      ptr=strmov(ptr," checksum=1");
2774
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2775
      ptr=strmov(ptr," delay_key_write=1");
2776
    if (share->row_type != ROW_TYPE_DEFAULT)
2777
      ptr=strxmov(ptr, " row_format=", 
2778
                  ha_row_type[(uint) share->row_type],
2779
                  NullS);
2780
#ifdef WITH_PARTITION_STORAGE_ENGINE
unknown's avatar
unknown committed
2781 2782 2783
    if (show_table->s->db_type == &partition_hton && 
        show_table->part_info != NULL && 
        show_table->part_info->no_parts > 0)
2784 2785
      ptr= strmov(ptr, " partitioned");
#endif
2786 2787 2788
    table->field[19]->store(option_buff+1,
                            (ptr == option_buff ? 0 : 
                             (uint) (ptr-option_buff)-1), cs);
2789
    {
2790 2791
      char *comment;
      comment= show_table->file->update_table_comment(share->comment);
2792 2793 2794
      if (comment)
      {
        table->field[20]->store(comment, strlen(comment), cs);
2795 2796
        if (comment != share->comment)
          my_free(comment, MYF(0));
2797 2798
      }
    }
2799
  }
2800
  DBUG_RETURN(schema_table_store_record(thd, table));
2801 2802 2803
}


unknown's avatar
unknown committed
2804 2805 2806 2807
static int get_schema_column_record(THD *thd, struct st_table_list *tables,
				    TABLE *table, bool res,
				    const char *base_name,
				    const char *file_name)
2808
{
2809 2810
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2811
  CHARSET_INFO *cs= system_charset_info;
2812 2813 2814 2815 2816
  TABLE *show_table;
  handler *file;
  Field **ptr,*field;
  int count;
  uint base_name_length, file_name_length;
2817
  DBUG_ENTER("get_schema_column_record");
2818

2819 2820
  if (res)
  {
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
    if (lex->orig_sql_command != SQLCOM_SHOW_FIELDS)
    {
      /*
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
        rather than in SHOW COLUMNS
      */ 
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
      thd->clear_error();
      res= 0;
    }
    DBUG_RETURN(res);
2833 2834
  }

2835 2836 2837
  show_table= tables->table;
  file= show_table->file;
  count= 0;
2838
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2839
  restore_record(show_table, s->default_values);
2840 2841 2842
  base_name_length= strlen(base_name);
  file_name_length= strlen(file_name);

2843 2844
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
    const char *tmp_buff;
    byte *pos;
    bool is_blob;
    uint flags=field->flags;
    char tmp[MAX_FIELD_WIDTH];
    char tmp1[MAX_FIELD_WIDTH];
    String type(tmp,sizeof(tmp), system_charset_info);
    char *end;
    int decimals, field_length;

    if (wild && wild[0] &&
        wild_case_compare(system_charset_info, field->field_name,wild))
      continue;

    flags= field->flags;
    count++;
    /* Get default row, with all NULL fields set to NULL */
    restore_record(table, s->default_values);
2863 2864

#ifndef NO_EMBEDDED_ACCESS_CHECKS
2865 2866
    uint col_access;
    check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
2867
                 &tables->grant.privilege, 0, 0, test(tables->schema_table));
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
    col_access= get_column_grant(thd, &tables->grant, 
                                 base_name, file_name,
                                 field->field_name) & COL_ACLS;
    if (lex->orig_sql_command != SQLCOM_SHOW_FIELDS  && 
        !tables->schema_table && !col_access)
      continue;
    end= tmp;
    for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
    {
      if (col_access & 1)
2878
      {
2879 2880
        *end++=',';
        end=strmov(end,grant_types.type_names[bitnr]);
2881
      }
2882
    }
2883
    table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);
2884

unknown's avatar
unknown committed
2885
#endif
2886 2887 2888 2889
    table->field[1]->store(base_name, base_name_length, cs);
    table->field[2]->store(file_name, file_name_length, cs);
    table->field[3]->store(field->field_name, strlen(field->field_name),
                           cs);
2890
    table->field[4]->store((longlong) count, TRUE);
2891 2892 2893 2894 2895 2896 2897 2898 2899
    field->sql_type(type);
    table->field[14]->store(type.ptr(), type.length(), cs);		
    tmp_buff= strchr(type.ptr(), '(');
    table->field[7]->store(type.ptr(),
                           (tmp_buff ? tmp_buff - type.ptr() :
                            type.length()), cs);
    if (show_table->timestamp_field == field &&
        field->unireg_check != Field::TIMESTAMP_UN_FIELD)
    {
2900
      table->field[5]->store(STRING_WITH_LEN("CURRENT_TIMESTAMP"), cs);
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
      table->field[5]->set_notnull();
    }
    else if (field->unireg_check != Field::NEXT_NUMBER &&
             !field->is_null() &&
             !(field->flags & NO_DEFAULT_VALUE_FLAG))
    {
      String def(tmp1,sizeof(tmp1), cs);
      type.set(tmp, sizeof(tmp), field->charset());
      field->val_str(&type);
      uint dummy_errors;
      def.copy(type.ptr(), type.length(), type.charset(), cs, &dummy_errors);
      table->field[5]->store(def.ptr(), def.length(), def.charset());
      table->field[5]->set_notnull();
    }
    else if (field->unireg_check == Field::NEXT_NUMBER ||
             lex->orig_sql_command != SQLCOM_SHOW_FIELDS ||
             field->maybe_null())
      table->field[5]->set_null();                // Null as default
    else
    {
      table->field[5]->store("",0, cs);
      table->field[5]->set_notnull();
    }
    pos=(byte*) ((flags & NOT_NULL_FLAG) &&
                 field->type() != FIELD_TYPE_TIMESTAMP ?
                 "NO" : "YES");
    table->field[6]->store((const char*) pos,
                           strlen((const char*) pos), cs);
    is_blob= (field->type() == FIELD_TYPE_BLOB);
2930 2931 2932
    if (field->has_charset() || is_blob ||
        field->real_type() == MYSQL_TYPE_VARCHAR ||  // For varbinary type
        field->real_type() == MYSQL_TYPE_STRING)     // For binary type
2933
    {
unknown's avatar
unknown committed
2934
      uint32 octet_max_length= field->max_length();
2935
      if (is_blob && octet_max_length != (uint32) 4294967295U)
unknown's avatar
unknown committed
2936
        octet_max_length /= field->charset()->mbmaxlen;
2937
      longlong char_max_len= is_blob ? 
unknown's avatar
unknown committed
2938 2939
        (longlong) octet_max_length / field->charset()->mbminlen :
        (longlong) octet_max_length / field->charset()->mbmaxlen;
2940
      table->field[8]->store(char_max_len, TRUE);
2941
      table->field[8]->set_notnull();
unknown's avatar
unknown committed
2942
      table->field[9]->store((longlong) octet_max_length, TRUE);
2943 2944
      table->field[9]->set_notnull();
    }
2945

2946 2947 2948 2949
    /*
      Calculate field_length and decimals.
      They are set to -1 if they should not be set (we should return NULL)
    */
unknown's avatar
unknown committed
2950

2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
    decimals= field->decimals();
    switch (field->type()) {
    case FIELD_TYPE_NEWDECIMAL:
      field_length= ((Field_new_decimal*) field)->precision;
      break;
    case FIELD_TYPE_DECIMAL:
      field_length= field->field_length - (decimals  ? 2 : 1);
      break;
    case FIELD_TYPE_TINY:
    case FIELD_TYPE_SHORT:
    case FIELD_TYPE_LONG:
    case FIELD_TYPE_LONGLONG:
    case FIELD_TYPE_INT24:
      field_length= field->max_length() - 1;
      break;
    case FIELD_TYPE_BIT:
      field_length= field->max_length();
      decimals= -1;                             // return NULL
      break;
    case FIELD_TYPE_FLOAT:  
    case FIELD_TYPE_DOUBLE:
      field_length= field->field_length;
      if (decimals == NOT_FIXED_DEC)
        decimals= -1;                           // return NULL
    break;
    default:
      field_length= decimals= -1;
      break;
    }

    if (field_length >= 0)
    {
2983
      table->field[10]->store((longlong) field_length, TRUE);
2984
      table->field[10]->set_notnull();
2985
    }
2986 2987
    if (decimals >= 0)
    {
2988
      table->field[11]->store((longlong) decimals, TRUE);
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
      table->field[11]->set_notnull();
    }

    if (field->has_charset())
    {
      pos=(byte*) field->charset()->csname;
      table->field[12]->store((const char*) pos,
                              strlen((const char*) pos), cs);
      table->field[12]->set_notnull();
      pos=(byte*) field->charset()->name;
      table->field[13]->store((const char*) pos,
                              strlen((const char*) pos), cs);
      table->field[13]->set_notnull();
    }
    pos=(byte*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
                 (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
                 (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
    table->field[15]->store((const char*) pos,
                            strlen((const char*) pos), cs);

    end= tmp;
    if (field->unireg_check == Field::NEXT_NUMBER)
      end=strmov(tmp,"auto_increment");
    table->field[16]->store(tmp, (uint) (end-tmp), cs);

    table->field[18]->store(field->comment.str, field->comment.length, cs);
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
  }
  DBUG_RETURN(0);
}



int fill_schema_charsets(THD *thd, TABLE_LIST *tables, COND *cond)
{
  CHARSET_INFO **cs;
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
3029 3030

  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
3031 3032 3033 3034
  {
    CHARSET_INFO *tmp_cs= cs[0];
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
        (tmp_cs->state & MY_CS_AVAILABLE) &&
unknown's avatar
unknown committed
3035
        !(tmp_cs->state & MY_CS_HIDDEN) &&
3036 3037 3038
        !(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cs->csname,wild)))
    {
3039
      const char *comment;
3040
      restore_record(table, s->default_values);
3041
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
3042
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
3043 3044
      comment= tmp_cs->comment ? tmp_cs->comment : "";
      table->field[2]->store(comment, strlen(comment), scs);
3045
      table->field[3]->store((longlong) tmp_cs->mbmaxlen, TRUE);
3046 3047
      if (schema_table_store_record(thd, table))
        return 1;
3048 3049 3050 3051 3052 3053
    }
  }
  return 0;
}


unknown's avatar
unknown committed
3054 3055
static my_bool iter_schema_engines(THD *thd, st_plugin_int *plugin,
                                   void *ptable)
unknown's avatar
unknown committed
3056
{
unknown's avatar
unknown committed
3057 3058
  TABLE *table= (TABLE *) ptable;
  handlerton *hton= (handlerton *) plugin->plugin->info;
unknown's avatar
unknown committed
3059 3060
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  CHARSET_INFO *scs= system_charset_info;
unknown's avatar
unknown committed
3061
  DBUG_ENTER("iter_schema_engines");
unknown's avatar
unknown committed
3062

unknown's avatar
unknown committed
3063
  if (!(hton->flags & HTON_HIDDEN))
unknown's avatar
unknown committed
3064 3065
  {
    if (!(wild && wild[0] &&
unknown's avatar
unknown committed
3066
          wild_case_compare(scs, hton->name,wild)))
unknown's avatar
unknown committed
3067 3068 3069 3070
    {
      const char *tmp;
      restore_record(table, s->default_values);

unknown's avatar
unknown committed
3071 3072
      table->field[0]->store(hton->name, strlen(hton->name), scs);
      tmp= hton->state ? "DISABLED" : "ENABLED";
unknown's avatar
unknown committed
3073
      table->field[1]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3074 3075
      table->field[2]->store(hton->comment, strlen(hton->comment), scs);
      tmp= hton->commit ? "YES" : "NO";
unknown's avatar
unknown committed
3076
      table->field[3]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3077
      tmp= hton->prepare ? "YES" : "NO";
unknown's avatar
unknown committed
3078
      table->field[4]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3079
      tmp= hton->savepoint_set ? "YES" : "NO";
unknown's avatar
unknown committed
3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
      table->field[5]->store( tmp, strlen(tmp), scs);

      if (schema_table_store_record(thd, table))
        DBUG_RETURN(1);
    }
  }
  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
3090 3091 3092
int fill_schema_engines(THD *thd, TABLE_LIST *tables, COND *cond)
{
  return plugin_foreach(thd, iter_schema_engines, 
unknown's avatar
fixes  
unknown committed
3093
                        MYSQL_STORAGE_ENGINE_PLUGIN, tables->table);
unknown's avatar
unknown committed
3094 3095 3096
}


3097 3098 3099 3100 3101 3102
int fill_schema_collation(THD *thd, TABLE_LIST *tables, COND *cond)
{
  CHARSET_INFO **cs;
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
3103
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3104 3105 3106 3107
  {
    CHARSET_INFO **cl;
    CHARSET_INFO *tmp_cs= cs[0];
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) || 
unknown's avatar
unknown committed
3108
         (tmp_cs->state & MY_CS_HIDDEN) ||
3109 3110
        !(tmp_cs->state & MY_CS_PRIMARY))
      continue;
3111
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3112 3113 3114 3115 3116 3117 3118 3119 3120
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs, tmp_cl))
	continue;
      if (!(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cl->name,wild)))
      {
	const char *tmp_buff;
3121
	restore_record(table, s->default_values);
3122 3123
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3124
        table->field[2]->store((longlong) tmp_cl->number, TRUE);
3125 3126 3127 3128
        tmp_buff= (tmp_cl->state & MY_CS_PRIMARY) ? "Yes" : "";
	table->field[3]->store(tmp_buff, strlen(tmp_buff), scs);
        tmp_buff= (tmp_cl->state & MY_CS_COMPILED)? "Yes" : "";
	table->field[4]->store(tmp_buff, strlen(tmp_buff), scs);
3129
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply, TRUE);
3130 3131
        if (schema_table_store_record(thd, table))
          return 1;
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
      }
    }
  }
  return 0;
}


int fill_schema_coll_charset_app(THD *thd, TABLE_LIST *tables, COND *cond)
{
  CHARSET_INFO **cs;
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
3144
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3145 3146 3147 3148 3149 3150
  {
    CHARSET_INFO **cl;
    CHARSET_INFO *tmp_cs= cs[0];
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) || 
        !(tmp_cs->state & MY_CS_PRIMARY))
      continue;
3151
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3152 3153 3154 3155 3156
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
3157
      restore_record(table, s->default_values);
3158 3159
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3160 3161
      if (schema_table_store_record(thd, table))
        return 1;
3162 3163 3164 3165 3166 3167
    }
  }
  return 0;
}


3168
bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table,
3169
                       const char *wild, bool full_access, const char *sp_user)
3170 3171
{
  String tmp_string;
3172
  String sp_db, sp_name, definer;
3173 3174 3175
  TIME time;
  LEX *lex= thd->lex;
  CHARSET_INFO *cs= system_charset_info;
3176 3177 3178
  get_field(thd->mem_root, proc_table->field[0], &sp_db);
  get_field(thd->mem_root, proc_table->field[1], &sp_name);
  get_field(thd->mem_root, proc_table->field[11], &definer);
3179
  if (!full_access)
3180 3181 3182 3183
    full_access= !strcmp(sp_user, definer.ptr());
  if (!full_access && check_some_routine_access(thd, sp_db.ptr(), sp_name.ptr(),
                                                proc_table->field[2]->val_int() ==
                                                TYPE_ENUM_PROCEDURE))
3184
    return 0;
3185

3186 3187 3188 3189 3190 3191
  if (lex->orig_sql_command == SQLCOM_SHOW_STATUS_PROC &&
      proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE ||
      lex->orig_sql_command == SQLCOM_SHOW_STATUS_FUNC &&
      proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION ||
      lex->orig_sql_command == SQLCOM_END)
  {
3192
    restore_record(table, s->default_values);
3193
    if (!wild || !wild[0] || !wild_compare(sp_name.ptr(), wild, 0))
3194
    {
3195
      int enum_idx= proc_table->field[5]->val_int();
3196
      table->field[3]->store(sp_name.ptr(), sp_name.length(), cs);
3197 3198
      get_field(thd->mem_root, proc_table->field[3], &tmp_string);
      table->field[0]->store(tmp_string.ptr(), tmp_string.length(), cs);
3199
      table->field[2]->store(sp_db.ptr(), sp_db.length(), cs);
3200 3201
      get_field(thd->mem_root, proc_table->field[2], &tmp_string);
      table->field[4]->store(tmp_string.ptr(), tmp_string.length(), cs);
3202 3203 3204 3205 3206 3207
      if (proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION)
      {
        get_field(thd->mem_root, proc_table->field[9], &tmp_string);
        table->field[5]->store(tmp_string.ptr(), tmp_string.length(), cs);
        table->field[5]->set_notnull();
      }
3208 3209 3210 3211 3212
      if (full_access)
      {
        get_field(thd->mem_root, proc_table->field[10], &tmp_string);
        table->field[7]->store(tmp_string.ptr(), tmp_string.length(), cs);
      }
3213 3214
      table->field[6]->store(STRING_WITH_LEN("SQL"), cs);
      table->field[10]->store(STRING_WITH_LEN("SQL"), cs);
3215
      get_field(thd->mem_root, proc_table->field[6], &tmp_string);
3216
      table->field[11]->store(tmp_string.ptr(), tmp_string.length(), cs);
3217 3218
      table->field[12]->store(sp_data_access_name[enum_idx].str, 
                              sp_data_access_name[enum_idx].length , cs);
3219 3220
      get_field(thd->mem_root, proc_table->field[7], &tmp_string);
      table->field[14]->store(tmp_string.ptr(), tmp_string.length(), cs);
3221 3222
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[12])->get_time(&time);
3223
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3224 3225
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[13])->get_time(&time);
3226
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3227
      get_field(thd->mem_root, proc_table->field[14], &tmp_string);
3228
      table->field[17]->store(tmp_string.ptr(), tmp_string.length(), cs);
3229
      get_field(thd->mem_root, proc_table->field[15], &tmp_string);
3230
      table->field[18]->store(tmp_string.ptr(), tmp_string.length(), cs);
3231
      table->field[19]->store(definer.ptr(), definer.length(), cs);
3232
      return schema_table_store_record(thd, table);
3233 3234
    }
  }
3235
  return 0;
3236 3237 3238 3239 3240 3241 3242 3243 3244
}


int fill_schema_proc(THD *thd, TABLE_LIST *tables, COND *cond)
{
  TABLE *proc_table;
  TABLE_LIST proc_tables;
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  int res= 0;
3245
  TABLE *table= tables->table;
3246
  bool full_access;
3247
  char definer[USER_HOST_BUFF_SIZE];
3248
  Open_tables_state open_tables_state_backup;
3249 3250
  DBUG_ENTER("fill_schema_proc");

3251 3252
  strxmov(definer, thd->security_ctx->priv_user, "@",
          thd->security_ctx->priv_host, NullS);
3253
  /* We use this TABLE_LIST instance only for checking of privileges. */
3254 3255
  bzero((char*) &proc_tables,sizeof(proc_tables));
  proc_tables.db= (char*) "mysql";
3256
  proc_tables.db_length= 5;
3257
  proc_tables.table_name= proc_tables.alias= (char*) "proc";
3258
  proc_tables.table_name_length= 4;
3259
  proc_tables.lock_type= TL_READ;
3260
  full_access= !check_table_access(thd, SELECT_ACL, &proc_tables, 1);
3261
  if (!(proc_table= open_proc_table_for_read(thd, &open_tables_state_backup)))
3262 3263 3264
  {
    DBUG_RETURN(1);
  }
3265
  proc_table->file->ha_index_init(0, 1);
3266 3267 3268 3269 3270
  if ((res= proc_table->file->index_first(proc_table->record[0])))
  {
    res= (res == HA_ERR_END_OF_FILE) ? 0 : 1;
    goto err;
  }
3271 3272 3273 3274 3275
  if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
  {
    res= 1;
    goto err;
  }
3276
  while (!proc_table->file->index_next(proc_table->record[0]))
3277 3278 3279 3280 3281 3282 3283
  {
    if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
    {
      res= 1;
      goto err;
    }
  }
3284 3285 3286

err:
  proc_table->file->ha_index_end();
3287
  close_proc_table(thd, &open_tables_state_backup);
3288 3289 3290 3291
  DBUG_RETURN(res);
}


unknown's avatar
unknown committed
3292 3293 3294 3295
static int get_schema_stat_record(THD *thd, struct st_table_list *tables,
				  TABLE *table, bool res,
				  const char *base_name,
				  const char *file_name)
3296 3297 3298
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
3299 3300 3301 3302 3303 3304 3305
  if (res)
  {
    if (thd->lex->orig_sql_command != SQLCOM_SHOW_KEYS)
    {
      /*
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
        rather than in SHOW KEYS
3306
      */
3307 3308 3309 3310 3311 3312 3313 3314 3315
      if (!tables->view)
        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                     thd->net.last_errno, thd->net.last_error);
      thd->clear_error();
      res= 0;
    }
    DBUG_RETURN(res);
  }
  else if (!tables->view)
3316 3317 3318
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3319
    show_table->file->info(HA_STATUS_VARIABLE |
3320 3321
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3322
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3323 3324 3325 3326 3327
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
3328
        restore_record(table, s->default_values);
3329 3330
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
3331
        table->field[3]->store((longlong) ((key_info->flags &
3332
                                            HA_NOSAME) ? 0 : 1), TRUE);
3333 3334
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
3335
        table->field[6]->store((longlong) (j+1), TRUE);
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350
        str=(key_part->field ? key_part->field->field_name :
             "?unknown field?");
        table->field[7]->store(str, strlen(str), cs);
        if (show_table->file->index_flags(i, j, 0) & HA_READ_ORDER)
        {
          table->field[8]->store(((key_part->key_part_flag &
                                   HA_REVERSE_SORT) ?
                                  "D" : "A"), 1, cs);
          table->field[8]->set_notnull();
        }
        KEY *key=show_table->key_info+i;
        if (key->rec_per_key[j])
        {
          ha_rows records=(show_table->file->records /
                           key->rec_per_key[j]);
3351
          table->field[9]->store((longlong) records, TRUE);
3352 3353
          table->field[9]->set_notnull();
        }
3354 3355 3356
        if (!(key_info->flags & HA_FULLTEXT) &&
            (key_part->field &&
             key_part->length !=
3357 3358
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
3359
          table->field[10]->store((longlong) key_part->length /
unknown's avatar
unknown committed
3360
                                  key_part->field->charset()->mbmaxlen);
3361 3362 3363 3364 3365 3366 3367
          table->field[10]->set_notnull();
        }
        uint flags= key_part->field ? key_part->field->flags : 0;
        const char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
        table->field[12]->store(pos, strlen(pos), cs);
        pos= show_table->file->index_type(i);
        table->field[13]->store(pos, strlen(pos), cs);
3368
        if (!show_table->s->keys_in_use.is_set(i))
3369
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3370 3371 3372
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
3373 3374
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3375 3376 3377
      }
    }
  }
unknown's avatar
unknown committed
3378
  DBUG_RETURN(res);
3379 3380 3381
}


unknown's avatar
unknown committed
3382 3383 3384 3385
static int get_schema_views_record(THD *thd, struct st_table_list *tables,
				   TABLE *table, bool res,
				   const char *base_name,
				   const char *file_name)
3386 3387 3388
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
3389
  char definer[USER_HOST_BUFF_SIZE];
unknown's avatar
unknown committed
3390
  uint definer_len;
unknown's avatar
unknown committed
3391

3392 3393 3394 3395 3396 3397 3398
  if (tables->view)
  {
    restore_record(table, s->default_values);
    table->field[1]->store(tables->view_db.str, tables->view_db.length, cs);
    table->field[2]->store(tables->view_name.str, tables->view_name.length,
                           cs);
    table->field[3]->store(tables->query.str, tables->query.length, cs);
unknown's avatar
unknown committed
3399

3400 3401 3402 3403
    if (tables->with_check != VIEW_CHECK_NONE)
    {
      if (tables->with_check == VIEW_CHECK_LOCAL)
        table->field[4]->store(STRING_WITH_LEN("LOCAL"), cs);
unknown's avatar
unknown committed
3404
      else
3405
        table->field[4]->store(STRING_WITH_LEN("CASCADED"), cs);
3406
    }
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
    else
      table->field[4]->store(STRING_WITH_LEN("NONE"), cs);

    if (tables->updatable_view)
      table->field[5]->store(STRING_WITH_LEN("YES"), cs);
    else
      table->field[5]->store(STRING_WITH_LEN("NO"), cs);
    definer_len= (strxmov(definer, tables->definer.user.str, "@",
                          tables->definer.host.str, NullS) - definer);
    table->field[6]->store(definer, definer_len, cs);
    if (tables->view_suid)
      table->field[7]->store(STRING_WITH_LEN("DEFINER"), cs);
    else
      table->field[7]->store(STRING_WITH_LEN("INVOKER"), cs);
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
    if (res)
3424 3425 3426
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                   thd->net.last_errno, thd->net.last_error);
  }
3427 3428
  if (res) 
    thd->clear_error();
3429
  DBUG_RETURN(0);
3430 3431 3432
}


3433 3434 3435
bool store_constraints(THD *thd, TABLE *table, const char *db,
                       const char *tname, const char *key_name,
                       uint key_len, const char *con_type, uint con_len)
3436 3437
{
  CHARSET_INFO *cs= system_charset_info;
3438
  restore_record(table, s->default_values);
3439 3440 3441 3442 3443
  table->field[1]->store(db, strlen(db), cs);
  table->field[2]->store(key_name, key_len, cs);
  table->field[3]->store(db, strlen(db), cs);
  table->field[4]->store(tname, strlen(tname), cs);
  table->field[5]->store(con_type, con_len, cs);
3444
  return schema_table_store_record(thd, table);
3445 3446 3447
}


unknown's avatar
unknown committed
3448
static int get_schema_constraints_record(THD *thd, struct st_table_list *tables,
unknown's avatar
unknown committed
3449 3450 3451
					 TABLE *table, bool res,
					 const char *base_name,
					 const char *file_name)
3452
{
unknown's avatar
unknown committed
3453
  DBUG_ENTER("get_schema_constraints_record");
3454 3455 3456 3457 3458 3459 3460 3461 3462
  if (res)
  {
    if (!tables->view)
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
  else if (!tables->view)
3463 3464 3465 3466
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3467
    uint primary_key= show_table->s->primary_key;
3468 3469 3470
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3471
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3472 3473
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3474 3475
        continue;

3476
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
3477 3478
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3479 3480
                              strlen(key_info->name),
                              STRING_WITH_LEN("PRIMARY KEY")))
3481 3482
          DBUG_RETURN(1);
      }
3483
      else if (key_info->flags & HA_NOSAME)
3484 3485
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3486 3487
                              strlen(key_info->name),
                              STRING_WITH_LEN("UNIQUE")))
3488 3489
          DBUG_RETURN(1);
      }
3490 3491 3492 3493 3494 3495 3496
    }

    show_table->file->get_foreign_key_list(thd, &f_key_list);
    FOREIGN_KEY_INFO *f_key_info;
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
    while ((f_key_info=it++))
    {
3497 3498 3499 3500 3501
      if (store_constraints(thd, table, base_name, file_name, 
                            f_key_info->forein_id->str,
                            strlen(f_key_info->forein_id->str),
                            "FOREIGN KEY", 11))
        DBUG_RETURN(1);
3502 3503
    }
  }
unknown's avatar
unknown committed
3504
  DBUG_RETURN(res);
3505 3506 3507
}


3508 3509 3510 3511
static bool store_trigger(THD *thd, TABLE *table, const char *db,
                          const char *tname, LEX_STRING *trigger_name,
                          enum trg_event_type event,
                          enum trg_action_time_type timing,
3512
                          LEX_STRING *trigger_stmt,
3513 3514
                          ulong sql_mode,
                          LEX_STRING *definer_buffer)
3515 3516
{
  CHARSET_INFO *cs= system_charset_info;
3517 3518 3519
  byte *sql_mode_str;
  ulong sql_mode_len;

3520 3521 3522 3523 3524 3525 3526 3527
  restore_record(table, s->default_values);
  table->field[1]->store(db, strlen(db), cs);
  table->field[2]->store(trigger_name->str, trigger_name->length, cs);
  table->field[3]->store(trg_event_type_names[event].str,
                         trg_event_type_names[event].length, cs);
  table->field[5]->store(db, strlen(db), cs);
  table->field[6]->store(tname, strlen(tname), cs);
  table->field[9]->store(trigger_stmt->str, trigger_stmt->length, cs);
3528
  table->field[10]->store(STRING_WITH_LEN("ROW"), cs);
3529 3530
  table->field[11]->store(trg_action_time_type_names[timing].str,
                          trg_action_time_type_names[timing].length, cs);
3531 3532
  table->field[14]->store(STRING_WITH_LEN("OLD"), cs);
  table->field[15]->store(STRING_WITH_LEN("NEW"), cs);
3533 3534 3535 3536 3537

  sql_mode_str=
    sys_var_thd_sql_mode::symbolic_mode_representation(thd,
                                                       sql_mode,
                                                       &sql_mode_len);
unknown's avatar
unknown committed
3538
  table->field[17]->store((const char*)sql_mode_str, sql_mode_len, cs);
3539
  table->field[18]->store((const char *)definer_buffer->str, definer_buffer->length, cs);
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
  return schema_table_store_record(thd, table);
}


static int get_schema_triggers_record(THD *thd, struct st_table_list *tables,
				      TABLE *table, bool res,
				      const char *base_name,
				      const char *file_name)
{
  DBUG_ENTER("get_schema_triggers_record");
  /*
    res can be non zero value when processed table is a view or
    error happened during opening of processed table.
  */
  if (res)
  {
    if (!tables->view)
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
  if (!tables->view && tables->table->triggers)
  {
    Table_triggers_list *triggers= tables->table->triggers;
    int event, timing;
    for (event= 0; event < (int)TRG_EVENT_MAX; event++)
    {
      for (timing= 0; timing < (int)TRG_ACTION_MAX; timing++)
      {
        LEX_STRING trigger_name;
        LEX_STRING trigger_stmt;
3572
        ulong sql_mode;
3573
        char definer_holder[USER_HOST_BUFF_SIZE];
3574 3575
        LEX_STRING definer_buffer;
        definer_buffer.str= definer_holder;
3576 3577
        if (triggers->get_trigger_info(thd, (enum trg_event_type) event,
                                       (enum trg_action_time_type)timing,
3578
                                       &trigger_name, &trigger_stmt,
3579 3580
                                       &sql_mode,
                                       &definer_buffer))
3581
          continue;
3582

3583 3584
        if (store_trigger(thd, table, base_name, file_name, &trigger_name,
                         (enum trg_event_type) event,
3585
                         (enum trg_action_time_type) timing, &trigger_stmt,
3586 3587
                         sql_mode,
                         &definer_buffer))
3588 3589 3590 3591 3592 3593 3594 3595
          DBUG_RETURN(1);
      }
    }
  }
  DBUG_RETURN(0);
}


3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
void store_key_column_usage(TABLE *table, const char*db, const char *tname,
                            const char *key_name, uint key_len, 
                            const char *con_type, uint con_len, longlong idx)
{
  CHARSET_INFO *cs= system_charset_info;
  table->field[1]->store(db, strlen(db), cs);
  table->field[2]->store(key_name, key_len, cs);
  table->field[4]->store(db, strlen(db), cs);
  table->field[5]->store(tname, strlen(tname), cs);
  table->field[6]->store(con_type, con_len, cs);
3606
  table->field[7]->store((longlong) idx, TRUE);
3607 3608 3609
}


unknown's avatar
unknown committed
3610 3611 3612 3613 3614
static int get_schema_key_column_usage_record(THD *thd,
					      struct st_table_list *tables,
					      TABLE *table, bool res,
					      const char *base_name,
					      const char *file_name)
3615 3616
{
  DBUG_ENTER("get_schema_key_column_usage_record");
3617 3618 3619 3620 3621 3622 3623 3624 3625
  if (res)
  {
    if (!tables->view)
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
  else if (!tables->view)
3626 3627 3628 3629
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3630
    uint primary_key= show_table->s->primary_key;
3631 3632 3633
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3634
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3635 3636
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
unknown's avatar
unknown committed
3637
        continue;
3638 3639 3640 3641 3642 3643 3644
      uint f_idx= 0;
      KEY_PART_INFO *key_part= key_info->key_part;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
        if (key_part->field)
        {
          f_idx++;
3645
          restore_record(table, s->default_values);
3646 3647 3648 3649 3650 3651
          store_key_column_usage(table, base_name, file_name,
                                 key_info->name,
                                 strlen(key_info->name), 
                                 key_part->field->field_name, 
                                 strlen(key_part->field->field_name),
                                 (longlong) f_idx);
3652 3653
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
3654 3655 3656 3657 3658 3659 3660 3661 3662
        }
      }
    }

    show_table->file->get_foreign_key_list(thd, &f_key_list);
    FOREIGN_KEY_INFO *f_key_info;
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
    while ((f_key_info= it++))
    {
3663
      LEX_STRING *f_info;
3664
      LEX_STRING *r_info;
3665 3666 3667 3668 3669
      List_iterator_fast<LEX_STRING> it(f_key_info->foreign_fields),
        it1(f_key_info->referenced_fields);
      uint f_idx= 0;
      while ((f_info= it++))
      {
3670
        r_info= it1++;
3671
        f_idx++;
3672
        restore_record(table, s->default_values);
3673 3674 3675 3676 3677
        store_key_column_usage(table, base_name, file_name,
                               f_key_info->forein_id->str,
                               f_key_info->forein_id->length,
                               f_info->str, f_info->length,
                               (longlong) f_idx);
3678
        table->field[8]->store((longlong) f_idx, TRUE);
3679
        table->field[8]->set_notnull();
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690
        table->field[9]->store(f_key_info->referenced_db->str,
                               f_key_info->referenced_db->length,
                               system_charset_info);
        table->field[9]->set_notnull();
        table->field[10]->store(f_key_info->referenced_table->str,
                                f_key_info->referenced_table->length, 
                                system_charset_info);
        table->field[10]->set_notnull();
        table->field[11]->store(r_info->str, r_info->length,
                                system_charset_info);
        table->field[11]->set_notnull();
3691 3692
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3693 3694 3695
      }
    }
  }
unknown's avatar
unknown committed
3696
  DBUG_RETURN(res);
3697 3698 3699
}


3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
static void collect_partition_expr(List<char> &field_list, String *str)
{
  List_iterator<char> part_it(field_list);
  ulong no_fields= field_list.elements;
  const char *field_str;
  str->length(0);
  while ((field_str= part_it++))
  {
    str->append(field_str);
    if (--no_fields != 0)
      str->append(",");
  }
  return;
}


static void store_schema_partitions_record(THD *thd, TABLE *table,
                                           partition_element *part_elem,
                                           handler *file, uint part_id)
{
  CHARSET_INFO *cs= system_charset_info;
  PARTITION_INFO stat_info;
  TIME time;
  file->get_dynamic_partition_info(&stat_info, part_id);
  table->field[12]->store((longlong) stat_info.records, TRUE);
  table->field[13]->store((longlong) stat_info.mean_rec_length, TRUE);
  table->field[14]->store((longlong) stat_info.data_file_length, TRUE);
  if (stat_info.max_data_file_length)
  {
    table->field[15]->store((longlong) stat_info.max_data_file_length, TRUE);
    table->field[15]->set_notnull();
  }
  table->field[16]->store((longlong) stat_info.index_file_length, TRUE);
  table->field[17]->store((longlong) stat_info.delete_length, TRUE);
  if (stat_info.create_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                              stat_info.create_time);
    table->field[18]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[18]->set_notnull();
  }
  if (stat_info.update_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                              stat_info.update_time);
    table->field[19]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[19]->set_notnull();
  }
  if (stat_info.check_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time, stat_info.check_time);
    table->field[20]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[20]->set_notnull();
  }
  if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
  {
    table->field[21]->store((longlong) stat_info.check_sum, TRUE);
    table->field[21]->set_notnull();
  }
  if (part_elem)
  {
    if (part_elem->part_comment)
      table->field[22]->store(part_elem->part_comment,
                              strlen(part_elem->part_comment), cs);
    else
      table->field[22]->store(STRING_WITH_LEN("default"), cs);
    if (part_elem->nodegroup_id != UNDEF_NODEGROUP)
      table->field[23]->store((longlong) part_elem->nodegroup_id, TRUE);
    else
      table->field[23]->store(STRING_WITH_LEN("default"), cs);
    if (part_elem->tablespace_name)
      table->field[24]->store(part_elem->tablespace_name,
                              strlen(part_elem->tablespace_name), cs);
    else
      table->field[24]->store(STRING_WITH_LEN("default"), cs);
  }
  return;
}


static int get_schema_partitions_record(THD *thd, struct st_table_list *tables,
                                        TABLE *table, bool res,
                                        const char *base_name,
                                        const char *file_name)
{
  CHARSET_INFO *cs= system_charset_info;
  char buff[61];
  String tmp_res(buff, sizeof(buff), cs);
  String tmp_str;
  TIME time;
  TABLE *show_table= tables->table;
3791
  handler *file;
unknown's avatar
unknown committed
3792
#ifdef WITH_PARTITION_STORAGE_ENGINE
3793
  partition_info *part_info;
unknown's avatar
unknown committed
3794
#endif
3795 3796 3797 3798
  DBUG_ENTER("get_schema_partitions_record");

  if (res)
  {
3799
    if (!tables->view)
3800 3801 3802 3803 3804
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
3805
  file= show_table->file;
unknown's avatar
unknown committed
3806
#ifdef WITH_PARTITION_STORAGE_ENGINE
3807
  part_info= show_table->part_info;
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
  if (part_info)
  {
    partition_element *part_elem;
    List_iterator<partition_element> part_it(part_info->partitions);
    uint part_pos= 0, part_id= 0;
    uint no_parts= part_info->no_parts;
    handler *part_file;

    restore_record(table, s->default_values);
    table->field[1]->store(base_name, strlen(base_name), cs);
    table->field[2]->store(file_name, strlen(file_name), cs);


    /* Partition method*/
    switch (part_info->part_type) {
    case RANGE_PARTITION:
      table->field[7]->store(partition_keywords[PKW_RANGE].str,
                             partition_keywords[PKW_RANGE].length, cs);
      break;
    case LIST_PARTITION:
      table->field[7]->store(partition_keywords[PKW_LIST].str,
                             partition_keywords[PKW_LIST].length, cs);
      break;
    case HASH_PARTITION:
      tmp_res.length(0);
      if (part_info->linear_hash_ind)
        tmp_res.append(partition_keywords[PKW_LINEAR].str,
                       partition_keywords[PKW_LINEAR].length);
      if (part_info->list_of_part_fields)
        tmp_res.append(partition_keywords[PKW_KEY].str,
                       partition_keywords[PKW_KEY].length);
      else
        tmp_res.append(partition_keywords[PKW_HASH].str, 
                       partition_keywords[PKW_HASH].length);
      table->field[7]->store(tmp_res.ptr(), tmp_res.length(), cs);
      break;
    default:
      DBUG_ASSERT(0);
      current_thd->fatal_error();
      DBUG_RETURN(1);
    }
    table->field[7]->set_notnull();

    /* Partition expression */
    if (part_info->part_expr)
    {
      table->field[9]->store(part_info->part_func_string,
                             part_info->part_func_len, cs);
      table->field[9]->set_notnull();
    }
    else if (part_info->list_of_part_fields)
    {
      collect_partition_expr(part_info->part_field_list, &tmp_str);
      table->field[9]->store(tmp_str.ptr(), tmp_str.length(), cs);
      table->field[9]->set_notnull();
    }

3865
    if (part_info->is_sub_partitioned())
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
    {
      /* Subpartition method */
      if (part_info->list_of_subpart_fields)
        table->field[8]->store(partition_keywords[PKW_KEY].str,
                               partition_keywords[PKW_KEY].length, cs);
      else
        table->field[8]->store(partition_keywords[PKW_HASH].str,
                               partition_keywords[PKW_HASH].length, cs);
      table->field[8]->set_notnull();

      /* Subpartition expression */
      if (part_info->subpart_expr)
      {
        table->field[10]->store(part_info->subpart_func_string,
                                part_info->subpart_func_len, cs);
        table->field[10]->set_notnull();
      }
      else if (part_info->list_of_subpart_fields)
      {
        collect_partition_expr(part_info->subpart_field_list, &tmp_str);
        table->field[10]->store(tmp_str.ptr(), tmp_str.length(), cs);
        table->field[10]->set_notnull();
      }
    }

    while ((part_elem= part_it++))
    {


      table->field[3]->store(part_elem->partition_name,
                             strlen(part_elem->partition_name), cs);
      table->field[3]->set_notnull();
      /* PARTITION_ORDINAL_POSITION */
      table->field[5]->store((longlong) ++part_pos, TRUE);
      table->field[5]->set_notnull();

      /* Partition description */
      if (part_info->part_type == RANGE_PARTITION)
      {
        if (part_elem->range_value != LONGLONG_MAX)
          table->field[11]->store((longlong) part_elem->range_value, FALSE);
        else
          table->field[11]->store(partition_keywords[PKW_MAXVALUE].str,
                                 partition_keywords[PKW_MAXVALUE].length, cs);
        table->field[11]->set_notnull();
      }
      else if (part_info->part_type == LIST_PARTITION)
      {
        List_iterator<longlong> list_val_it(part_elem->list_val_list);
        longlong *list_value;
        uint no_items= part_elem->list_val_list.elements;
        tmp_str.length(0);
        tmp_res.length(0);
3919 3920 3921 3922 3923 3924
        if (part_elem->has_null_value)
        {
          tmp_str.append("NULL");
          if (no_items > 0)
            tmp_str.append(",");
        }
3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969
        while ((list_value= list_val_it++))
        {
          tmp_res.set(*list_value, cs);
          tmp_str.append(tmp_res);
          if (--no_items != 0)
            tmp_str.append(",");
        };
        table->field[11]->store(tmp_str.ptr(), tmp_str.length(), cs);
        table->field[11]->set_notnull();
      }

      if (part_elem->subpartitions.elements)
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        partition_element *subpart_elem;
        uint subpart_pos= 0;

        while ((subpart_elem= sub_it++))
        {
          table->field[4]->store(subpart_elem->partition_name,
                                 strlen(subpart_elem->partition_name), cs);
          table->field[4]->set_notnull();
          /* SUBPARTITION_ORDINAL_POSITION */
          table->field[6]->store((longlong) ++subpart_pos, TRUE);
          table->field[6]->set_notnull();
          
          store_schema_partitions_record(thd, table, subpart_elem,
                                         file, part_id);
          part_id++;
          if(schema_table_store_record(thd, table))
            DBUG_RETURN(1);
        }
      }
      else
      {
        store_schema_partitions_record(thd, table, part_elem,
                                       file, part_id);
        part_id++;
        if(schema_table_store_record(thd, table))
          DBUG_RETURN(1);
      }
    }
    DBUG_RETURN(0);
  }
  else
unknown's avatar
unknown committed
3970
#endif
3971 3972 3973 3974 3975 3976 3977 3978 3979
  {
    store_schema_partitions_record(thd, table, 0, file, 0);
    if(schema_table_store_record(thd, table))
      DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020
static interval_type get_real_interval_type(interval_type i_type)
{
  switch (i_type) {
  case INTERVAL_YEAR:
    return INTERVAL_YEAR;

  case INTERVAL_QUARTER:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    return INTERVAL_MONTH;

  case INTERVAL_WEEK:
  case INTERVAL_DAY:
    return INTERVAL_DAY;

  case INTERVAL_DAY_HOUR:
  case INTERVAL_HOUR:
    return INTERVAL_HOUR;

  case INTERVAL_DAY_MINUTE:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_MINUTE:
    return INTERVAL_MINUTE;

  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    return INTERVAL_SECOND;

  case INTERVAL_DAY_MICROSECOND:
  case INTERVAL_HOUR_MICROSECOND:
  case INTERVAL_MINUTE_MICROSECOND:
  case INTERVAL_SECOND_MICROSECOND:
  case INTERVAL_MICROSECOND:
    return INTERVAL_MICROSECOND;
  }
  DBUG_ASSERT(0);
  return INTERVAL_SECOND;
}

unknown's avatar
unknown committed
4021
extern LEX_STRING interval_type_to_name[];
4022 4023 4024 4025 4026 4027 4028

static int
fill_events_copy_to_schema_table(THD *thd, TABLE *sch_table, TABLE *event_table)
{
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  CHARSET_INFO *scs= system_charset_info;
  TIME time;
unknown's avatar
unknown committed
4029
  Event_timed et;    
4030 4031 4032 4033 4034 4035
  DBUG_ENTER("fill_events_copy_to_schema_tab");

  restore_record(sch_table, s->default_values);

  if (et.load_from_row(thd->mem_root, event_table))
  {
4036
    my_error(ER_CANNOT_LOAD_FROM_TABLE, MYF(0));
4037 4038 4039 4040 4041 4042
    DBUG_RETURN(1);
  }

  if (!(!wild || !wild[0] || !wild_compare(et.name.str, wild, 0)))
    DBUG_RETURN(0);
  
4043
  /* ->field[0] is EVENT_CATALOG and is by default NULL */
4044 4045 4046 4047 4048 4049
  
  sch_table->field[1]->store(et.dbname.str, et.dbname.length, scs);
  sch_table->field[2]->store(et.name.str, et.name.length, scs);
  sch_table->field[3]->store(et.definer.str, et.definer.length, scs);
  sch_table->field[4]->store(et.body.str, et.body.length, scs);

unknown's avatar
unknown committed
4050
  /* [9] is SQL_MODE */
4051
  {
4052
    byte *sql_mode_str;
4053 4054 4055 4056 4057 4058 4059
    ulong sql_mode_len=0;
    sql_mode_str=
           sys_var_thd_sql_mode::symbolic_mode_representation(thd, et.sql_mode,
                                                              &sql_mode_len);  
    sch_table->field[9]->store((const char*)sql_mode_str, sql_mode_len, scs);
  }
  
4060 4061
  if (et.expression)
  {
4062
    String show_str;
4063
    /* type */
4064
    sch_table->field[5]->store(STRING_WITH_LEN("RECURRING"), scs);
4065

4066 4067 4068
    if (event_reconstruct_interval_expression(&show_str, et.interval,
                                              et.expression))
      DBUG_RETURN(1);
4069

unknown's avatar
unknown committed
4070 4071 4072 4073
    sch_table->field[7]->set_notnull();
    sch_table->field[7]->store(show_str.c_ptr(), show_str.length(), scs);

    LEX_STRING *ival= &interval_type_to_name[et.interval];
4074
    sch_table->field[8]->set_notnull();
unknown's avatar
unknown committed
4075
    sch_table->field[8]->store(ival->str, ival->length, scs);
4076 4077

    //starts & ends    
4078 4079
    sch_table->field[10]->set_notnull();
    sch_table->field[10]->store_time(&et.starts, MYSQL_TIMESTAMP_DATETIME);
4080 4081 4082 4083 4084 4085

    if (!et.ends_null)
    {
      sch_table->field[11]->set_notnull();
      sch_table->field[11]->store_time(&et.ends, MYSQL_TIMESTAMP_DATETIME);
    }
4086 4087 4088 4089 4090
  }
  else
  {
    //type
    sch_table->field[5]->store(STRING_WITH_LEN("ONE TIME"), scs);
4091

4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
    sch_table->field[6]->set_notnull();
    sch_table->field[6]->store_time(&et.execute_at, MYSQL_TIMESTAMP_DATETIME);
  }

  //status
  if (et.status == MYSQL_EVENT_ENABLED)
    sch_table->field[12]->store(STRING_WITH_LEN("ENABLED"), scs);
  else
    sch_table->field[12]->store(STRING_WITH_LEN("DISABLED"), scs);

  //on_completion
  if (et.on_completion == MYSQL_EVENT_ON_COMPLETION_DROP)
    sch_table->field[13]->store(STRING_WITH_LEN("NOT PRESERVE"), scs);
  else
    sch_table->field[13]->store(STRING_WITH_LEN("PRESERVE"), scs);
    
  int not_used=0;
  number_to_datetime(et.created, &time, 0, &not_used);
  DBUG_ASSERT(not_used==0);
  sch_table->field[14]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);

  number_to_datetime(et.modified, &time, 0, &not_used);
  DBUG_ASSERT(not_used==0);
  sch_table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);

  if (et.last_executed.year)
4118 4119
  {
    sch_table->field[16]->set_notnull();
4120
    sch_table->field[16]->store_time(&et.last_executed,MYSQL_TIMESTAMP_DATETIME);
4121
  }
4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187

  sch_table->field[17]->store(et.comment.str, et.comment.length, scs);

  if (schema_table_store_record(thd, sch_table))
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}


int fill_schema_events(THD *thd, TABLE_LIST *tables, COND *cond)
{
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
  TABLE *event_table= NULL;
  Open_tables_state backup;
  int ret=0;
  bool verbose= false;
  char definer[HOSTNAME_LENGTH+USERNAME_LENGTH+2];
  bool use_prefix_scanning= true;
  uint key_len= 0;
  byte *key_buf= NULL;
  LINT_INIT(key_buf);

  DBUG_ENTER("fill_schema_events");

  strxmov(definer, thd->security_ctx->priv_user,"@",thd->security_ctx->priv_host,
          NullS);

  DBUG_PRINT("info",("db=%s current_user=%s", thd->lex->select_lex.db, definer));

  thd->reset_n_backup_open_tables_state(&backup);

  if ((ret= evex_open_event_table(thd, TL_READ, &event_table)))
  {
    sql_print_error("Table mysql.event is damaged.");
    ret= 1;
    goto err;
  }
  
  event_table->file->ha_index_init(0, 1);

  /* 
    see others' events only if you have PROCESS_ACL !!
    thd->lex->verbose is set either if SHOW FULL EVENTS or
    in case of SELECT FROM I_S.EVENTS
  */
  verbose= (thd->lex->verbose
            && (thd->security_ctx->master_access & PROCESS_ACL));

  if (verbose && thd->security_ctx->user)
  {    
    ret= event_table->file->index_first(event_table->record[0]);
    use_prefix_scanning= false;
  }
  else
  {
    event_table->field[EVEX_FIELD_DEFINER]->store(definer, strlen(definer), scs);    
    key_len= event_table->key_info->key_part[0].store_length;

    if (thd->lex->select_lex.db)
    {
      event_table->field[EVEX_FIELD_DB]->
            store(thd->lex->select_lex.db, strlen(thd->lex->select_lex.db), scs);
      key_len+= event_table->key_info->key_part[1].store_length;
    }
4188
    if (!(key_buf= (byte *)alloc_root(thd->mem_root, key_len)))
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
    {
      ret= 1;
      goto err;
    }
    
    key_copy(key_buf, event_table->record[0], event_table->key_info, key_len);
    ret= event_table->file->index_read(event_table->record[0], key_buf, key_len,
                                       HA_READ_PREFIX);
  }

  if (ret)
  {
    ret= (ret == HA_ERR_END_OF_FILE || ret == HA_ERR_KEY_NOT_FOUND) ? 0 : 1;
    goto err;
  }

  while (!ret)
  {
    if ((ret= fill_events_copy_to_schema_table(thd, table, event_table)))
      goto err;

    if (use_prefix_scanning)
      ret= event_table->file->
                       index_next_same(event_table->record[0], key_buf, key_len);                                  
    else
      ret= event_table->file->index_next(event_table->record[0]);
  }
  // ret is guaranteed to be != 0
  ret= (ret != HA_ERR_END_OF_FILE);
err:
  if (event_table)
  {
    event_table->file->ha_index_end();
    close_thread_tables(thd);
  }

  thd->restore_backup_open_tables_state(&backup);
  DBUG_RETURN(ret);
}


4230 4231 4232 4233 4234 4235 4236
int fill_open_tables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_open_tables");
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  TABLE *table= tables->table;
  CHARSET_INFO *cs= system_charset_info;
  OPEN_TABLE_LIST *open_list;
4237 4238
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
            && thd->is_fatal_error)
4239 4240 4241 4242
    DBUG_RETURN(1);

  for (; open_list ; open_list=open_list->next)
  {
4243
    restore_record(table, s->default_values);
4244 4245
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
4246 4247
    table->field[2]->store((longlong) open_list->in_use, TRUE);
    table->field[3]->store((longlong) open_list->locked, TRUE);
4248 4249
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
4250 4251 4252 4253 4254 4255 4256 4257
  }
  DBUG_RETURN(0);
}


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
4258
  int res= 0;
4259 4260
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
4261
  pthread_mutex_lock(&LOCK_global_system_variables);
4262
  res= show_status_array(thd, wild, init_vars,
4263 4264
                         lex->option_type, 0, "", tables->table);
  pthread_mutex_unlock(&LOCK_global_system_variables);
4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275
  DBUG_RETURN(res);
}


int fill_status(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_status");
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
  int res= 0;
  STATUS_VAR tmp;
4276
  pthread_mutex_lock(&LOCK_status);
4277 4278
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
4279 4280 4281 4282
  res= show_status_array(thd, wild,
                         (SHOW_VAR *)all_status_vars.buffer,
                         OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ?
4283 4284
                          &tmp: &thd->status_var), "",tables->table);
  pthread_mutex_unlock(&LOCK_status);
4285 4286 4287 4288
  DBUG_RETURN(res);
}


4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
/*
  Fill and store records into I_S.referential_constraints table

  SYNOPSIS
    get_referential_constraints_record()
    thd                 thread handle
    tables              table list struct(processed table)
    table               I_S table
    res                 1 means the error during opening of the processed table
                        0 means processed table is opened without error
    base_name           db name
    file_name           table name

  RETURN
    0	ok
    #   error
*/

static int
get_referential_constraints_record(THD *thd, struct st_table_list *tables,
                                   TABLE *table, bool res,
                                   const char *base_name, const char *file_name)
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_referential_constraints_record");

  if (res)
  {
    if (!tables->view)
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
  if (!tables->view)
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);

    show_table->file->get_foreign_key_list(thd, &f_key_list);
    FOREIGN_KEY_INFO *f_key_info;
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
    while ((f_key_info= it++))
    {
      restore_record(table, s->default_values);
      table->field[1]->store(base_name, strlen(base_name), cs);
      table->field[9]->store(file_name, strlen(file_name), cs);
      table->field[2]->store(f_key_info->forein_id->str,
                             f_key_info->forein_id->length, cs);
      table->field[4]->store(f_key_info->referenced_db->str, 
                             f_key_info->referenced_db->length, cs);
      table->field[5]->store(f_key_info->referenced_table->str, 
                             f_key_info->referenced_table->length, cs);
      table->field[6]->store(STRING_WITH_LEN("NONE"), cs);
      table->field[7]->store(f_key_info->update_method->str, 
                             f_key_info->update_method->length, cs);
      table->field[8]->store(f_key_info->delete_method->str, 
                             f_key_info->delete_method->length, cs);
      if (schema_table_store_record(thd, table))
        DBUG_RETURN(1);
    }
  }
  DBUG_RETURN(0);
}


4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
/*
  Find schema_tables elment by name

  SYNOPSIS
    find_schema_table()
    thd                 thread handler
    table_name          table name

  RETURN
    0	table not found
    #   pointer to 'shema_tables' element
*/

ST_SCHEMA_TABLE *find_schema_table(THD *thd, const char* table_name)
{
  ST_SCHEMA_TABLE *schema_table= schema_tables;
4374
  for (; schema_table->table_name; schema_table++)
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403
  {
    if (!my_strcasecmp(system_charset_info,
                       schema_table->table_name,
                       table_name))
      return schema_table;
  }
  return 0;
}


ST_SCHEMA_TABLE *get_schema_table(enum enum_schema_tables schema_table_idx)
{
  return &schema_tables[schema_table_idx];
}


/*
  Create information_schema table using schema_table data

  SYNOPSIS
    create_schema_table()
    thd	       	          thread handler
    schema_table          pointer to 'shema_tables' element

  RETURN
    #	                  Pointer to created table
    0	                  Can't create table
*/

4404
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
4405 4406 4407 4408 4409
{
  int field_count= 0;
  Item *item;
  TABLE *table;
  List<Item> field_list;
4410
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
4411
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
unknown's avatar
unknown committed
4412
  CHARSET_INFO *cs= system_charset_info;
4413 4414
  DBUG_ENTER("create_schema_table");

4415
  for (; fields_info->field_name; fields_info++)
4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
  {
    switch (fields_info->field_type) {
    case MYSQL_TYPE_LONG:
      if (!(item= new Item_int(fields_info->field_name,
                               fields_info->value,
                               fields_info->field_length)))
      {
        DBUG_RETURN(0);
      }
      break;
    case MYSQL_TYPE_TIMESTAMP:
      if (!(item=new Item_datetime(fields_info->field_name)))
      {
        DBUG_RETURN(0);
      }
      break;
    default:
4433 4434
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
4435 4436 4437
      {
        DBUG_RETURN(0);
      }
4438
      item->max_length= fields_info->field_length * cs->mbmaxlen;
unknown's avatar
unknown committed
4439 4440
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
4441 4442 4443 4444 4445 4446 4447 4448 4449
      break;
    }
    field_list.push_back(item);
    item->maybe_null= fields_info->maybe_null;
    field_count++;
  }
  TMP_TABLE_PARAM *tmp_table_param =
    (TMP_TABLE_PARAM*) (thd->calloc(sizeof(TMP_TABLE_PARAM)));
  tmp_table_param->init();
unknown's avatar
unknown committed
4450
  tmp_table_param->table_charset= cs;
4451
  tmp_table_param->field_count= field_count;
4452
  tmp_table_param->schema_table= 1;
4453 4454 4455 4456 4457
  SELECT_LEX *select_lex= thd->lex->current_select;
  if (!(table= create_tmp_table(thd, tmp_table_param,
                                field_list, (ORDER*) 0, 0, 0, 
                                (select_lex->options | thd->options |
                                 TMP_TABLE_ALL_COLUMNS),
unknown's avatar
unknown committed
4458
                                HA_POS_ERROR, table_list->alias)))
4459
    DBUG_RETURN(0);
4460
  table_list->schema_table_param= tmp_table_param;
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
  DBUG_RETURN(table);
}


/*
  For old SHOW compatibility. It is used when
  old SHOW doesn't have generated column names
  Make list of fields for SHOW

  SYNOPSIS
    make_old_format()
    thd			thread handler
    schema_table        pointer to 'schema_tables' element

  RETURN
   -1	errror
    0	success
*/

int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  ST_FIELD_INFO *field_info= schema_table->fields_info;
4483
  Name_resolution_context *context= &thd->lex->select_lex.context;
4484
  for (; field_info->field_name; field_info++)
4485 4486 4487
  {
    if (field_info->old_name)
    {
4488 4489
      Item_field *field= new Item_field(context,
                                        NullS, NullS, field_info->field_name);
4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
      if (field)
      {
        field->set_name(field_info->old_name,
                        strlen(field_info->old_name),
                        system_charset_info);
        if (add_item_to_list(thd, field))
          return 1;
      }
    }
  }
  return 0;
}


int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  char tmp[128];
  LEX *lex= thd->lex;
  SELECT_LEX *sel= lex->current_select;
4509
  Name_resolution_context *context= &sel->context;
4510 4511 4512 4513 4514

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
4515 4516
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4517 4518 4519 4520 4521 4522
    if (!field || add_item_to_list(thd, field))
      return 1;
    buffer.length(0);
    buffer.append(field_info->old_name);
    if (lex->wild && lex->wild->ptr())
    {
4523
      buffer.append(STRING_WITH_LEN(" ("));
4524
      buffer.append(lex->wild->ptr());
4525
      buffer.append(')');
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537
    }
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
  }
  return 0;
}


int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  char tmp[128];
  String buffer(tmp,sizeof(tmp), thd->charset());
  LEX *lex= thd->lex;
4538
  Name_resolution_context *context= &lex->select_lex.context;
4539 4540 4541 4542 4543 4544 4545

  ST_FIELD_INFO *field_info= &schema_table->fields_info[2];
  buffer.length(0);
  buffer.append(field_info->old_name);
  buffer.append(lex->select_lex.db);
  if (lex->wild && lex->wild->ptr())
  {
4546
    buffer.append(STRING_WITH_LEN(" ("));
4547
    buffer.append(lex->wild->ptr());
4548
    buffer.append(')');
4549
  }
4550 4551
  Item_field *field= new Item_field(context,
                                    NullS, NullS, field_info->field_name);
4552 4553 4554 4555 4556 4557 4558
  if (add_item_to_list(thd, field))
    return 1;
  field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
  if (thd->lex->verbose)
  {
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
    field_info= &schema_table->fields_info[3];
4559
    field= new Item_field(context, NullS, NullS, field_info->field_name);
4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570
    if (add_item_to_list(thd, field))
      return 1;
    field->set_name(field_info->old_name, strlen(field_info->old_name),
                    system_charset_info);
  }
  return 0;
}


int make_columns_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
4571 4572 4573
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4574 4575
  Name_resolution_context *context= &thd->lex->select_lex.context;

4576
  for (; *field_num >= 0; field_num++)
4577
  {
4578 4579 4580 4581 4582
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
4583 4584
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4585
    if (field)
4586
    {
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597
      field->set_name(field_info->old_name,
                      strlen(field_info->old_name),
                      system_charset_info);
      if (add_item_to_list(thd, field))
        return 1;
    }
  }
  return 0;
}


4598 4599 4600 4601 4602
int make_character_sets_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  int fields_arr[]= {0, 2, 1, 3, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4603 4604
  Name_resolution_context *context= &thd->lex->select_lex.context;

4605 4606 4607
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4608 4609
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622
    if (field)
    {
      field->set_name(field_info->old_name,
                      strlen(field_info->old_name),
                      system_charset_info);
      if (add_item_to_list(thd, field))
        return 1;
    }
  }
  return 0;
}


4623 4624 4625 4626 4627
int make_proc_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  int fields_arr[]= {2, 3, 4, 19, 16, 15, 14, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4628 4629
  Name_resolution_context *context= &thd->lex->select_lex.context;

4630 4631 4632
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4633 4634
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4635 4636 4637 4638 4639 4640 4641
    if (field)
    {
      field->set_name(field_info->old_name,
                      strlen(field_info->old_name),
                      system_charset_info);
      if (add_item_to_list(thd, field))
        return 1;
4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665
    }
  }
  return 0;
}


/*
  Create information_schema table

  SYNOPSIS
  mysql_schema_table()
    thd                thread handler
    lex                pointer to LEX
    table_list         pointer to table_list

  RETURN
    0	success
    1   error
*/

int mysql_schema_table(THD *thd, LEX *lex, TABLE_LIST *table_list)
{
  TABLE *table;
  DBUG_ENTER("mysql_schema_table");
4666
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4667 4668 4669
  {
    DBUG_RETURN(1);
  }
4670
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4671
  table->grant.privilege= SELECT_ACL;
4672 4673 4674 4675 4676 4677 4678
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
4679 4680 4681 4682
  if (table_list->schema_table_name)
    table->alias_name_used= my_strcasecmp(table_alias_charset,
                                          table_list->schema_table_name,
                                          table_list->alias);
unknown's avatar
unknown committed
4683 4684
  table_list->table_name= table->s->table_name.str;
  table_list->table_name_length= table->s->table_name.length;
4685 4686 4687 4688
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4689
  lex->safe_to_cache_query= 0;
4690 4691 4692 4693 4694

  if (table_list->schema_table_reformed) // show command
  {
    SELECT_LEX *sel= lex->current_select;
    Item *item;
unknown's avatar
unknown committed
4695
    Field_translator *transl, *org_transl;
4696 4697 4698

    if (table_list->field_translation)
    {
4699
      Field_translator *end= table_list->field_translation_end;
4700 4701 4702
      for (transl= table_list->field_translation; transl < end; transl++)
      {
        if (!transl->item->fixed &&
4703
            transl->item->fix_fields(thd, &transl->item))
4704 4705 4706 4707 4708 4709
          DBUG_RETURN(1);
      }
      DBUG_RETURN(0);
    }
    List_iterator_fast<Item> it(sel->item_list);
    if (!(transl=
unknown's avatar
unknown committed
4710
          (Field_translator*)(thd->stmt_arena->
4711 4712 4713 4714 4715
                              alloc(sel->item_list.elements *
                                    sizeof(Field_translator)))))
    {
      DBUG_RETURN(1);
    }
unknown's avatar
unknown committed
4716
    for (org_transl= transl; (item= it++); transl++)
4717
    {
unknown's avatar
unknown committed
4718 4719 4720 4721
      transl->item= item;
      transl->name= item->name;
      if (!item->fixed && item->fix_fields(thd, &transl->item))
      {
4722
        DBUG_RETURN(1);
unknown's avatar
unknown committed
4723
      }
4724
    }
unknown's avatar
unknown committed
4725 4726
    table_list->field_translation= org_transl;
    table_list->field_translation_end= transl;
4727 4728
  }

4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752
  DBUG_RETURN(0);
}


/*
  Generate select from information_schema table

  SYNOPSIS
    make_schema_select()
    thd                  thread handler
    sel                  pointer to SELECT_LEX
    schema_table_idx     index of 'schema_tables' element

  RETURN
    0	success
    1   error
*/

int make_schema_select(THD *thd, SELECT_LEX *sel,
		       enum enum_schema_tables schema_table_idx)
{
  ST_SCHEMA_TABLE *schema_table= get_schema_table(schema_table_idx);
  LEX_STRING db, table;
  DBUG_ENTER("mysql_schema_select");
unknown's avatar
unknown committed
4753
  DBUG_PRINT("enter", ("mysql_schema_select: %s", schema_table->table_name));
4754
  /*
4755 4756 4757 4758 4759 4760 4761
     We have to make non const db_name & table_name
     because of lower_case_table_names
  */
  make_lex_string(thd, &db, information_schema_name.str,
                  information_schema_name.length, 0);
  make_lex_string(thd, &table, schema_table->table_name,
                  strlen(schema_table->table_name), 0);
4762
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773
      !sel->add_table_to_list(thd, new Table_ident(thd, db, table, 0),
                              0, 0, TL_READ, (List<String> *) 0,
                              (List<String> *) 0))
  {
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


/*
4774
  Fill temporary schema tables before SELECT
4775 4776 4777 4778 4779 4780

  SYNOPSIS
    get_schema_tables_result()
    join  join which use schema tables

  RETURN
unknown's avatar
unknown committed
4781 4782
    FALSE success
    TRUE  error
4783 4784
*/

unknown's avatar
unknown committed
4785
bool get_schema_tables_result(JOIN *join)
4786 4787 4788
{
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
  THD *thd= join->thd;
unknown's avatar
unknown committed
4789 4790
  LEX *lex= thd->lex;
  bool result= 0;
4791 4792 4793
  DBUG_ENTER("get_schema_tables_result");

  thd->no_warnings_for_error= 1;
4794 4795 4796 4797
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
  {  
    if (!tab->table || !tab->table->pos_in_table_list)
      break;
unknown's avatar
unknown committed
4798

unknown's avatar
unknown committed
4799
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
unknown's avatar
unknown committed
4800
    if (table_list->schema_table && thd->fill_derived_tables())
4801
    {
4802 4803
      bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
                          lex->current_select->master_unit()->item);
4804 4805 4806 4807 4808 4809 4810 4811 4812
      /*
        The schema table is already processed and 
        the statement is not a subselect.
        So we don't need to handle this table again.
      */
      if (table_list->is_schema_table_processed && !is_subselect)
        continue;

      if (is_subselect) // is subselect
unknown's avatar
unknown committed
4813 4814 4815 4816 4817 4818 4819 4820 4821
      {
        table_list->table->file->extra(HA_EXTRA_RESET_STATE);
        table_list->table->file->delete_all_rows();
        free_io_cache(table_list->table);
        filesort_free_buffers(table_list->table);
      }
      else
        table_list->table->file->records= 0;

4822 4823
      if (table_list->schema_table->fill_table(thd, table_list,
                                               tab->select_cond))
unknown's avatar
unknown committed
4824
        result= 1;
4825
      table_list->is_schema_table_processed= TRUE;
4826 4827
    }
  }
4828
  thd->no_warnings_for_error= 0;
unknown's avatar
unknown committed
4829
  DBUG_RETURN(result);
4830 4831
}

4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
struct run_hton_fill_schema_files_args
{
  TABLE_LIST *tables;
  COND *cond;
};

static my_bool run_hton_fill_schema_files(THD *thd, st_plugin_int *plugin,
                                          void *arg)
{
  struct run_hton_fill_schema_files_args *args=
    (run_hton_fill_schema_files_args *) arg;
  handlerton *hton= (handlerton *) plugin->plugin->info;
  if(hton->fill_files_table)
    hton->fill_files_table(thd, args->tables, args->cond);
  return false;
}

int fill_schema_files(THD *thd, TABLE_LIST *tables, COND *cond)
{
  int i;
  TABLE *table= tables->table;
unknown's avatar
unknown committed
4853
  DBUG_ENTER("fill_schema_files");
4854 4855 4856 4857 4858 4859 4860 4861 4862 4863

  struct run_hton_fill_schema_files_args args;
  args.tables= tables;
  args.cond= cond;

  plugin_foreach(thd, run_hton_fill_schema_files,
                 MYSQL_STORAGE_ENGINE_PLUGIN, &args);

  DBUG_RETURN(0);
}
4864 4865 4866

ST_FIELD_INFO schema_fields_info[]=
{
unknown's avatar
unknown committed
4867 4868
  {"CATALOG_NAME", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SCHEMA_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
4869
  {"DEFAULT_CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4870
  {"DEFAULT_COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4871
  {"SQL_PATH", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
4872
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4873 4874 4875 4876 4877
};


ST_FIELD_INFO tables_fields_info[]=
{
unknown's avatar
unknown committed
4878 4879 4880 4881 4882 4883 4884
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
  {"TABLE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"ENGINE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Engine"},
  {"VERSION", 21 , MYSQL_TYPE_LONG, 0, 1, "Version"},
  {"ROW_FORMAT", 10, MYSQL_TYPE_STRING, 0, 1, "Row_format"},
4885
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
unknown's avatar
unknown committed
4886 4887 4888 4889 4890 4891 4892 4893 4894
  {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Avg_row_length"},
  {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_length"},
  {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Max_data_length"},
  {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Index_length"},
  {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_free"},
  {"AUTO_INCREMENT", 21 , MYSQL_TYPE_LONG, 0, 1, "Auto_increment"},
  {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Create_time"},
  {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Update_time"},
  {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Check_time"},
4895
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
unknown's avatar
unknown committed
4896 4897
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
4898
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
unknown's avatar
unknown committed
4899
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4900 4901 4902 4903 4904
};


ST_FIELD_INFO columns_fields_info[]=
{
unknown's avatar
unknown committed
4905 4906 4907 4908 4909
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Field"},
  {"ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
4910 4911
  {"COLUMN_DEFAULT", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Default"},
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
4912
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
4913 4914
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4915 4916
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4917 4918
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
unknown's avatar
unknown committed
4919
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
4920
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
unknown's avatar
unknown committed
4921 4922
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
4923
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
unknown's avatar
unknown committed
4924
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4925 4926 4927 4928 4929
};


ST_FIELD_INFO charsets_fields_info[]=
{
4930 4931
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
  {"DEFAULT_COLLATE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Default collation"},
4932
  {"DESCRIPTION", 60, MYSQL_TYPE_STRING, 0, 0, "Description"},
4933
  {"MAXLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Maxlen"},
unknown's avatar
unknown committed
4934
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4935 4936 4937 4938 4939
};


ST_FIELD_INFO collation_fields_info[]=
{
4940 4941
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
4942
  {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"},
4943 4944
  {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"},
  {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"},
4945
  {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"},
unknown's avatar
unknown committed
4946
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4947 4948 4949
};


unknown's avatar
unknown committed
4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961
ST_FIELD_INFO engines_fields_info[]=
{
  {"ENGINE", 64, MYSQL_TYPE_STRING, 0, 0, "Engine"},
  {"SUPPORT", 8, MYSQL_TYPE_STRING, 0, 0, "Support"},
  {"COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
  {"TRANSACTIONS", 3, MYSQL_TYPE_STRING, 0, 0, "Transactions"},
  {"XA", 3, MYSQL_TYPE_STRING, 0, 0, "XA"},
  {"SAVEPOINTS", 3 ,MYSQL_TYPE_STRING, 0, 0, "Savepoints"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


4962 4963 4964 4965 4966 4967 4968 4969 4970
ST_FIELD_INFO events_fields_info[]=
{
  {"EVENT_CATALOG", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EVENT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Db"},
  {"EVENT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
  {"EVENT_BODY", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
  {"EVENT_TYPE", 9, MYSQL_TYPE_STRING, 0, 0, "Type"},
  {"EXECUTE_AT", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Execute at"},
unknown's avatar
unknown committed
4971
  {"INTERVAL_VALUE", 256, MYSQL_TYPE_STRING, 0, 1, "Interval value"},
4972
  {"INTERVAL_FIELD", 18, MYSQL_TYPE_STRING, 0, 1, "Interval field"},
4973
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986
  {"STARTS", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Starts"},
  {"ENDS", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Ends"},
  {"STATUS", 8, MYSQL_TYPE_STRING, 0, 0, "Status"},
  {"ON_COMPLETION", 12, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_EXECUTED", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"EVENT_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};



4987 4988
ST_FIELD_INFO coll_charset_app_fields_info[]=
{
4989 4990
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
4991
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4992 4993 4994 4995 4996
};


ST_FIELD_INFO proc_fields_info[]=
{
unknown's avatar
unknown committed
4997 4998 4999 5000 5001
  {"SPECIFIC_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"ROUTINE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Db"},
  {"ROUTINE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
  {"ROUTINE_TYPE", 9, MYSQL_TYPE_STRING, 0, 0, "Type"},
5002 5003
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5004
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5005 5006
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
5007
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5008 5009 5010 5011
  {"IS_DETERMINISTIC", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SQL_DATA_ACCESS", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SQL_PATH", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, "Security_type"},
5012 5013
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
unknown's avatar
unknown committed
5014
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5015
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
5016
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
unknown's avatar
unknown committed
5017
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5018 5019 5020 5021 5022
};


ST_FIELD_INFO stat_fields_info[]=
{
unknown's avatar
unknown committed
5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table"},
  {"NON_UNIQUE", 1, MYSQL_TYPE_LONG, 0, 0, "Non_unique"},
  {"INDEX_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"INDEX_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Key_name"},
  {"SEQ_IN_INDEX", 2, MYSQL_TYPE_LONG, 0, 0, "Seq_in_index"},
  {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Column_name"},
  {"COLLATION", 1, MYSQL_TYPE_STRING, 0, 1, "Collation"},
  {"CARDINALITY", 21, MYSQL_TYPE_LONG, 0, 1, "Cardinality"},
  {"SUB_PART", 3, MYSQL_TYPE_LONG, 0, 1, "Sub_part"},
  {"PACKED", 10, MYSQL_TYPE_STRING, 0, 1, "Packed"},
  {"NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
  {"INDEX_TYPE", 16, MYSQL_TYPE_STRING, 0, 0, "Index_type"},
  {"COMMENT", 16, MYSQL_TYPE_STRING, 0, 1, "Comment"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5039 5040 5041 5042 5043
};


ST_FIELD_INFO view_fields_info[]=
{
unknown's avatar
unknown committed
5044 5045 5046 5047
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"VIEW_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5048
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5049
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5050 5051
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5052
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5053 5054 5055 5056 5057
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5058 5059 5060 5061 5062
  {"GRANTEE", 81, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PRIVILEGE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"IS_GRANTABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5063 5064 5065 5066 5067
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5068 5069 5070 5071 5072 5073
  {"GRANTEE", 81, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"PRIVILEGE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"IS_GRANTABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5074 5075 5076 5077 5078
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5079 5080 5081 5082 5083 5084 5085
  {"GRANTEE", 81, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"PRIVILEGE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"IS_GRANTABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5086 5087 5088 5089 5090
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5091 5092 5093 5094 5095 5096 5097 5098
  {"GRANTEE", 81, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"PRIVILEGE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"IS_GRANTABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5099 5100 5101 5102 5103
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
unknown's avatar
unknown committed
5104 5105 5106 5107 5108 5109 5110
  {"CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CONSTRAINT_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5111 5112 5113 5114 5115
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
unknown's avatar
unknown committed
5116 5117 5118
  {"CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
5119
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
5120 5121 5122 5123
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"ORDINAL_POSITION", 10 ,MYSQL_TYPE_LONG, 0, 0, 0},
5124
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
5125 5126 5127
  {"REFERENCED_TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"REFERENCED_TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"REFERENCED_COLUMN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
5128
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5129 5130 5131 5132 5133
};


ST_FIELD_INFO table_names_fields_info[]=
{
unknown's avatar
unknown committed
5134 5135 5136 5137 5138
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Tables_in_"},
  {"TABLE_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_type"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5139 5140 5141
};


5142 5143 5144 5145 5146 5147 5148 5149 5150 5151
ST_FIELD_INFO open_tables_fields_info[]=
{
  {"Database", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
  {"Table",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table"},
  {"In_use", 1, MYSQL_TYPE_LONG, 0, 0, "In_use"},
  {"Name_locked", 4, MYSQL_TYPE_LONG, 0, 0, "Name_locked"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
ST_FIELD_INFO triggers_fields_info[]=
{
  {"TRIGGER_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TRIGGER_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TRIGGER_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Trigger"},
  {"EVENT_MANIPULATION", 6, MYSQL_TYPE_STRING, 0, 0, "Event"},
  {"EVENT_OBJECT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EVENT_OBJECT_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"EVENT_OBJECT_TABLE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table"},
  {"ACTION_ORDER", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"ACTION_CONDITION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ACTION_STATEMENT", 65535, MYSQL_TYPE_STRING, 0, 0, "Statement"},
  {"ACTION_ORIENTATION", 9, MYSQL_TYPE_STRING, 0, 0, 0},
  {"ACTION_TIMING", 6, MYSQL_TYPE_STRING, 0, 0, "Timing"},
  {"ACTION_REFERENCE_OLD_TABLE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ACTION_REFERENCE_NEW_TABLE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ACTION_REFERENCE_OLD_ROW", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {"ACTION_REFERENCE_NEW_ROW", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Created"},
5171
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, "sql_mode"},
5172
  {"DEFINER", 65535, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5173 5174 5175 5176
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
ST_FIELD_INFO partitions_fields_info[]=
{
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"PARTITION_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SUBPARTITION_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PARTITION_ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"SUBPARTITION_ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"PARTITION_METHOD", 12, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SUBPARTITION_METHOD", 5, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PARTITION_EXPRESSION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SUBPARTITION_EXPRESSION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PARTITION_DESCRIPTION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"PARTITION_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, 0},
  {"NODEGROUP", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"TABLESPACE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5208 5209 5210 5211 5212 5213 5214 5215
ST_FIELD_INFO variables_fields_info[]=
{
  {"Variable_name", 80, MYSQL_TYPE_STRING, 0, 0, "Variable_name"},
  {"Value", 255, MYSQL_TYPE_STRING, 0, 0, "Value"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5216 5217 5218 5219 5220 5221 5222
ST_FIELD_INFO processlist_fields_info[]=
{
  {"ID", 4, MYSQL_TYPE_LONG, 0, 0, "Id"},
  {"USER", 16, MYSQL_TYPE_STRING, 0, 0, "User"},
  {"HOST", LIST_PROCESS_HOST_LEN,  MYSQL_TYPE_STRING, 0, 0, "Host"},
  {"DB", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Db"},
  {"COMMAND", 16, MYSQL_TYPE_STRING, 0, 0, "Command"},
5223
  {"TIME", 7, MYSQL_TYPE_LONG, 0, 0, "Time"},
5224
  {"STATE", 30, MYSQL_TYPE_STRING, 0, 1, "State"},
5225
  {"INFO", PROCESS_LIST_INFO_WIDTH, MYSQL_TYPE_STRING, 0, 1, "Info"},
5226 5227 5228 5229
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


unknown's avatar
unknown committed
5230 5231 5232
ST_FIELD_INFO plugin_fields_info[]=
{
  {"PLUGIN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
unknown's avatar
unknown committed
5233
  {"PLUGIN_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5234
  {"PLUGIN_STATUS", 10, MYSQL_TYPE_STRING, 0, 0, "Status"},
5235
  {"PLUGIN_TYPE", 80, MYSQL_TYPE_STRING, 0, 0, "Type"},
unknown's avatar
unknown committed
5236
  {"PLUGIN_TYPE_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5237
  {"PLUGIN_LIBRARY", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Library"},
unknown's avatar
unknown committed
5238
  {"PLUGIN_LIBRARY_VERSION", 20, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
5239 5240 5241 5242 5243
  {"PLUGIN_AUTHOR", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PLUGIN_DESCRIPTION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};

5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
ST_FIELD_INFO files_fields_info[]=
{
  {"FILE_ID", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"FILE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"FILE_TYPE", 20, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLESPACE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_CATALOG", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"LOGFILE_GROUP_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"LOGFILE_GROUP_NUMBER", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"ENGINE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"FULLTEXT_KEYS", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"DELETED_ROWS", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"UPDATE_COUNT", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"FREE_EXTENTS", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"TOTAL_EXTENTS", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"EXTENT_SIZE", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"INITIAL_SIZE", 8, MYSQL_TYPE_LONGLONG, 0, 0, 0},
  {"MAXIMUM_SIZE", 8, MYSQL_TYPE_LONGLONG, 0, 0, 0},
  {"AUTOEXTEND_SIZE", 8, MYSQL_TYPE_LONGLONG, 0, 0, 0},
  {"CREATION_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_ACCESS_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"RECOVER_TIME", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"TRANSACTION_COUNTER", 4, MYSQL_TYPE_LONG, 0, 0, 0},
  {"VERSION", 21 , MYSQL_TYPE_LONG, 0, 1, "Version"},
  {"ROW_FORMAT", 10, MYSQL_TYPE_STRING, 0, 1, "Row_format"},
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
  {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Avg_row_length"},
  {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_length"},
  {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Max_data_length"},
  {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Index_length"},
  {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_free"},
  {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Create_time"},
  {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Update_time"},
  {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Check_time"},
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"STATUS", 20, MYSQL_TYPE_STRING, 0, 0, 0},
  {"EXTRA", 255, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};
unknown's avatar
unknown committed
5286

5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302
ST_FIELD_INFO referential_constraints_fields_info[]=
{
  {"CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UNIQUE_CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"UNIQUE_CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UNIQUE_CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"MATCH_OPTION", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UPDATE_RULE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"DELETE_RULE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5303 5304
/*
  Description of ST_FIELD_INFO in table.h
unknown's avatar
unknown committed
5305 5306 5307

  Make sure that the order of schema_tables and enum_schema_tables are the same.

5308 5309 5310 5311 5312
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
5313
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
5314
  {"COLLATIONS", collation_fields_info, create_schema_table, 
5315
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
5316
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
5317
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
5318 5319
  {"COLUMNS", columns_fields_info, create_schema_table, 
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
5320
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
5321
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
unknown's avatar
unknown committed
5322
  {"ENGINES", engines_fields_info, create_schema_table,
5323
   fill_schema_engines, make_old_format, 0, -1, -1, 0},
5324 5325
  {"EVENTS", events_fields_info, create_schema_table,
   fill_schema_events, make_old_format, 0, -1, -1, 0},
5326 5327
  {"FILES", files_fields_info, create_schema_table,
   fill_schema_files, 0, 0, -1, -1, 0},
5328
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
5329
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
5330 5331
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
5332 5333
  {"PARTITIONS", partitions_fields_info, create_schema_table,
   get_all_tables, 0, get_schema_partitions_record, 1, 2, 0},
unknown's avatar
unknown committed
5334 5335
  {"PLUGINS", plugin_fields_info, create_schema_table,
    fill_plugins, make_old_format, 0, -1, -1, 0},
5336 5337
  {"PROCESSLIST", processlist_fields_info, create_schema_table,
    fill_schema_processlist, make_old_format, 0, -1, -1, 0},
5338 5339 5340
  {"REFERENTIAL_CONSTRAINTS", referential_constraints_fields_info,
   create_schema_table, get_all_tables, 0, get_referential_constraints_record,
   1, 9, 0},
5341 5342 5343 5344 5345 5346 5347 5348
  {"ROUTINES", proc_fields_info, create_schema_table, 
    fill_schema_proc, make_proc_old_format, 0, -1, -1, 0},
  {"SCHEMATA", schema_fields_info, create_schema_table,
   fill_schema_shemata, make_schemata_old_format, 0, 1, -1, 0},
  {"SCHEMA_PRIVILEGES", schema_privileges_fields_info, create_schema_table,
    fill_schema_schema_privileges, 0, 0, -1, -1, 0},
  {"STATISTICS", stat_fields_info, create_schema_table, 
    get_all_tables, make_old_format, get_schema_stat_record, 1, 2, 0},
5349 5350
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
5351 5352 5353 5354 5355 5356 5357 5358
  {"TABLES", tables_fields_info, create_schema_table, 
   get_all_tables, make_old_format, get_schema_tables_record, 1, 2, 0},
  {"TABLE_CONSTRAINTS", table_constraints_fields_info, create_schema_table,
    get_all_tables, 0, get_schema_constraints_record, 3, 4, 0},
  {"TABLE_NAMES", table_names_fields_info, create_schema_table,
   get_all_tables, make_table_names_old_format, 0, 1, 2, 1},
  {"TABLE_PRIVILEGES", table_privileges_fields_info, create_schema_table,
    fill_schema_table_privileges, 0, 0, -1, -1, 0},
5359
  {"TRIGGERS", triggers_fields_info, create_schema_table,
5360
   get_all_tables, make_old_format, get_schema_triggers_record, 5, 6, 0},
5361 5362
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
5363 5364
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
5365 5366
  {"VIEWS", view_fields_info, create_schema_table, 
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
5367
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
5368 5369 5370
};


5371
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
5372
template class List_iterator_fast<char>;
unknown's avatar
unknown committed
5373 5374
template class List<char>;
#endif