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 1289
    if (table->part_info &&
        ((part_syntax= generate_partition_syntax(table->part_info,
unknown's avatar
unknown committed
1290 1291
                                                  &part_syntax_len,
                                                  FALSE,FALSE))))
1292 1293 1294 1295 1296 1297
    {
       packet->append(part_syntax, part_syntax_len);
       my_free(part_syntax, MYF(0));
    }
  }
#endif
unknown's avatar
unknown committed
1298 1299 1300
  DBUG_RETURN(0);
}

1301 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

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));
    }
  }
}


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

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
/*
  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(' ');
}


1388
int
unknown's avatar
VIEW  
unknown committed
1389 1390 1391 1392 1393 1394 1395 1396
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;
1397 1398 1399 1400 1401
  /*
     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
  */
1402
  if (!thd->db || strcmp(thd->db, table->view_db.str))
1403 1404 1405 1406
    table->compact_view_format= FALSE;
  else
  {
    TABLE_LIST *tbl;
1407
    table->compact_view_format= TRUE;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
    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;
      }
    }
  }

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

  /*
    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);

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


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

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

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

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

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

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

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1553 1554
        if (pthread_kill(tmp->real_id,0))
          tmp->proc_info="*** DEAD ***";        // This shouldn't happen
unknown's avatar
unknown committed
1555
#endif
unknown's avatar
unknown committed
1556 1557 1558
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1559
        thd_info->start_time= tmp->start_time;
unknown's avatar
unknown committed
1560
#endif
1561 1562 1563
        thd_info->query=0;
        if (tmp->query)
        {
unknown's avatar
unknown committed
1564 1565 1566 1567 1568
	  /* 
            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
1569
          uint length= min(max_query_length, tmp->query_length);
1570
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1571 1572
        }
        thread_infos.append(thd_info);
unknown's avatar
unknown committed
1573 1574 1575 1576 1577 1578
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

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

1604 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
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,
1697 1698
                               min(PROCESS_LIST_INFO_WIDTH,
                                   tmp->query_length), cs);
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
        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
1714
/*****************************************************************************
unknown's avatar
unknown committed
1715
  Status functions
unknown's avatar
unknown committed
1716 1717
*****************************************************************************/

1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
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)
{
1731
  uint a,b;
1732 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
  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.
*/
1829

1830 1831 1832 1833 1834 1835
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 *);
1836
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
1837 1838 1839

    for (; list->name; list++)
    {
1840
      int res= 0;
1841 1842 1843 1844 1845 1846 1847
      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;
1848 1849
        else
          break;
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
      }
      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 *);
1860
    uint i;
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    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);
  }
}

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

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

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

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

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

    SHOW_TYPE show_type=var->type;
    if (show_type == SHOW_ARRAY)
1911
    {
1912
      show_status_array(thd, wild, (SHOW_VAR *) var->value,
1913
                        value_type, status_var, name_buffer, table);
1914 1915
    }
    else
unknown's avatar
unknown committed
1916
    {
1917 1918
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
unknown's avatar
unknown committed
1919
      {
1920
        char *value=var->value;
unknown's avatar
unknown committed
1921
        const char *pos, *end;                  // We assign a lot of const's
1922 1923 1924 1925 1926 1927 1928 1929 1930
        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;
1931 1932 1933 1934
        /*
          note that value may be == buff. All SHOW_xxx code below
          should still work in this case
        */
1935
        switch (show_type) {
1936 1937 1938 1939 1940 1941
        case SHOW_DOUBLE_STATUS:
        {
          value= ((char *) status_var + (ulong) value);
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
1942 1943 1944 1945
        case SHOW_LONG_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
1946
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
          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
1965 1966 1967 1968 1969 1970
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
1971
        case SHOW_CHAR:
unknown's avatar
unknown committed
1972 1973 1974 1975 1976 1977
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
1978
        case SHOW_CHAR_PTR:
unknown's avatar
unknown committed
1979 1980 1981 1982 1983 1984
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
1985
        case SHOW_KEY_CACHE_LONG:
1986
          value= (char*) dflt_key_cache + (ulong)value;
1987 1988
          end= int10_to_str(*(long*) value, buff, 10);
          break;
1989
        case SHOW_KEY_CACHE_LONGLONG:
1990
          value= (char*) dflt_key_cache + (ulong)value;
1991 1992
	  end= longlong10_to_str(*(longlong*) value, buff, 10);
	  break;
1993
        case SHOW_UNDEF:
1994 1995
          break;                                        // Return empty string
        case SHOW_SYS:                                  // Cannot happen
1996
        default:
1997
          DBUG_ASSERT(0);
1998 1999
          break;
        }
2000
        restore_record(table, s->default_values);
2001 2002 2003
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
2004 2005
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
2006 2007 2008
      }
    }
  }
2009 2010 2011 2012 2013

  DBUG_RETURN(FALSE);
}


2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
/* 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;
}


2038 2039 2040 2041 2042 2043
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)
2044 2045
    if (!(lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING))))
      return 0;
2046 2047 2048 2049 2050 2051 2052 2053
  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
2054 2055

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

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


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

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

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

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


2092 2093 2094 2095 2096 2097 2098 2099 2100
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:
2101
  case SQLCOM_SHOW_TRIGGERS:
2102
  case SQLCOM_SHOW_EVENTS:
unknown's avatar
unknown committed
2103
    index_field_values->db_value= lex->select_lex.db;
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
    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
2125
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
                             (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++)
2140
    {
2141 2142
      if (!uses_only_table_name_fields(*child, table))
        return 0;
2143
    }
2144 2145 2146 2147 2148 2149 2150
  }
  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
2151 2152
    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 : "";
2153 2154 2155 2156 2157 2158 2159
    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)))
2160 2161
      return 0;
  }
2162 2163
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
2164 2165 2166 2167
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

2168
  return 1;
2169 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
}


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;
}


2228 2229 2230 2231 2232 2233
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
2234
/*
2235
  Create db names list. Information schema name always is first in list
unknown's avatar
unknown committed
2236 2237

  SYNOPSIS
2238
    make_db_list()
unknown's avatar
unknown committed
2239 2240 2241
    thd                   thread handler
    files                 list of db names
    wild                  wild string
2242 2243
    idx_field_vals        idx_field_vals->db_name contains db name or
                          wild string
unknown's avatar
unknown committed
2244 2245
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0
2246 2247
    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
2248 2249 2250 2251 2252 2253

  RETURN
    1	                  error
    0	                  success
*/

2254 2255 2256
int make_db_list(THD *thd, List<char> *files,
                 INDEX_FIELD_VALUES *idx_field_vals,
                 bool *with_i_schema, bool is_wild_value)
2257
{
2258
  LEX *lex= thd->lex;
unknown's avatar
unknown committed
2259
  *with_i_schema= 0;
2260 2261
  get_index_field_values(lex, idx_field_vals);
  if (is_wild_value)
unknown's avatar
unknown committed
2262
  {
2263 2264 2265 2266 2267
    /*
      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)
    */
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
    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
2279
  }
2280

2281 2282 2283 2284 2285 2286
  /*
    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.
  */
2287 2288
  if (lex->orig_sql_command != SQLCOM_END)
  {
2289
    if (!my_strcasecmp(system_charset_info, information_schema_name.str,
2290 2291 2292 2293 2294 2295 2296 2297
                       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));
  }

2298 2299 2300 2301
  /*
    Create list of existing databases. It is used in case
    of select from information schema table
  */
2302 2303 2304 2305
  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);
2306 2307 2308 2309 2310 2311
}


int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2312
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2313
  {
2314 2315
    if (tmp_schema_table->hidden)
      continue;
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
    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;
}


2335 2336 2337 2338 2339
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;
2340
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2341 2342
  TABLE_LIST **save_query_tables_last= lex->query_tables_last;
  enum_sql_command save_sql_command= lex->sql_command;
unknown's avatar
unknown committed
2343
  SELECT_LEX *lsel= tables->schema_select_lex;
2344
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2345 2346 2347 2348 2349 2350 2351
  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
2352
  List_iterator_fast<char> it(bases);
2353
  COND *partial_cond;
2354
  Security_context *sctx= thd->security_ctx;
2355
  uint derived_tables= lex->derived_tables; 
unknown's avatar
unknown committed
2356
  int error= 1;
unknown's avatar
unknown committed
2357
  enum legacy_db_type not_used;
2358
  Open_tables_state open_tables_state_backup;
2359 2360
  bool save_view_prepare_mode= lex->view_prepare_mode;
  lex->view_prepare_mode= TRUE;
2361 2362 2363 2364
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
2365

2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
  /*
    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);

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

2384
    lex->all_selects_list= lsel;
2385 2386 2387 2388 2389 2390 2391
    /*
      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;
2392 2393
    res= open_normal_and_derived_tables(thd, show_table_list,
                                        MYSQL_LOCK_IGNORE_FLUSH);
2394 2395 2396
    /*
      get_all_tables() returns 1 on failure and 0 on success thus
      return only these and not the result code of ::process_table()
2397 2398 2399 2400 2401 2402 2403

      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).
2404 2405
    */
    error= test(schema_table->process_table(thd, show_table_list,
2406 2407 2408 2409 2410
                                            table, res, 
                                            (show_table_list->view ?
                                             show_table_list->view_db.str :
                                             show_table_list->db),
                                            show_table_list->alias));
2411
    thd->temporary_tables= 0;
2412
    close_thread_tables(thd);
2413
    show_table_list->table= 0;
unknown's avatar
unknown committed
2414
    goto err;
2415 2416
  }

2417
  schema_table_idx= get_schema_table_idx(schema_table);
2418

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

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

2458 2459
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
2460
      {
2461
	restore_record(table, s->default_values);
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
        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)
            {
2472
              if (with_i_schema)
2473
              {
2474 2475
                table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
                                       system_charset_info);
2476 2477 2478 2479
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
2480
                switch (mysql_frm_type(thd, path, &not_used)) {
2481
                case FRMTYPE_ERROR:
2482 2483
                  table->field[3]->store(STRING_WITH_LEN("ERROR"),
                                         system_charset_info);
2484 2485
                  break;
                case FRMTYPE_TABLE:
2486 2487
                  table->field[3]->store(STRING_WITH_LEN("BASE TABLE"),
                                         system_charset_info);
2488 2489
                  break;
                case FRMTYPE_VIEW:
2490 2491
                  table->field[3]->store(STRING_WITH_LEN("VIEW"),
                                         system_charset_info);
2492 2493 2494 2495
                  break;
                default:
                  DBUG_ASSERT(0);
                }
2496 2497
              }
            }
2498
            if (schema_table_store_record(thd, table))
2499
              goto err;
2500 2501 2502 2503
          }
          else
          {
            int res;
unknown's avatar
unknown committed
2504 2505 2506 2507 2508
            /*
              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;
2509
            if (make_table_list(thd, &sel, base_name, file_name))
unknown's avatar
unknown committed
2510
              goto err;
2511
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
2512 2513
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
2514 2515
            res= open_normal_and_derived_tables(thd, show_table_list,
                                                MYSQL_LOCK_IGNORE_FLUSH);
2516 2517 2518 2519 2520 2521 2522
            /*
              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.
            */
2523
            res= schema_table->process_table(thd, show_table_list, table,
2524
                                            res, base_name,
2525
                                            show_table_list->alias);
2526
            close_thread_tables(thd);
2527
            if (res)
unknown's avatar
unknown committed
2528
              goto err;
2529 2530 2531
          }
        }
      }
2532 2533 2534 2535
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
2536
      with_i_schema= 0;
2537 2538
    }
  }
unknown's avatar
unknown committed
2539 2540 2541

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


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


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

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

2582 2583 2584
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2585 2586
    if (with_i_schema)       // information schema name is always first in list
    {
2587
      if (store_schema_shemata(thd, table, file_name,
2588
                               system_charset_info))
2589
        DBUG_RETURN(1);
2590 2591 2592
      with_i_schema= 0;
      continue;
    }
2593
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2594 2595
    if (sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, file_name,0) ||
2596 2597 2598
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2599
      length= build_table_filename(path, sizeof(path), file_name, "", "");
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
      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);
2611
      if (store_schema_shemata(thd, table, file_name, 
2612
                               create.default_table_charset))
2613
        DBUG_RETURN(1);
2614 2615
    }
  }
2616
  DBUG_RETURN(0);
2617 2618 2619
}


unknown's avatar
unknown committed
2620 2621 2622 2623
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)
2624 2625 2626 2627 2628
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2629 2630

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

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

2668 2669
    for (int i= 4; i < 20; i++)
    {
2670
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2671 2672 2673 2674 2675
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2676
    table->field[5]->store((longlong) share->frm_version, TRUE);
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
    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;
    }
2702
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2703 2704
    if (!tables->schema_table)
    {
2705
      table->field[7]->store((longlong) file->records, TRUE);
2706 2707
      table->field[7]->set_notnull();
    }
2708 2709
    table->field[8]->store((longlong) file->mean_rec_length, TRUE);
    table->field[9]->store((longlong) file->data_file_length, TRUE);
2710 2711
    if (file->max_data_file_length)
    {
2712
      table->field[10]->store((longlong) file->max_data_file_length, TRUE);
2713
    }
2714 2715
    table->field[11]->store((longlong) file->index_file_length, TRUE);
    table->field[12]->store((longlong) file->delete_length, TRUE);
2716
    if (show_table->found_next_number_field)
2717
    {
2718
      table->field[13]->store((longlong) file->auto_increment_value, TRUE);
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
      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();
    }
2741 2742
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2743
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
unknown's avatar
unknown committed
2744
    if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
2745
    {
2746
      table->field[18]->store((longlong) file->checksum(), TRUE);
2747 2748 2749 2750 2751
      table->field[18]->set_notnull();
    }

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


unknown's avatar
unknown committed
2803 2804 2805 2806
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)
2807
{
2808 2809
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2810
  CHARSET_INFO *cs= system_charset_info;
2811 2812 2813 2814 2815
  TABLE *show_table;
  handler *file;
  Field **ptr,*field;
  int count;
  uint base_name_length, file_name_length;
2816
  DBUG_ENTER("get_schema_column_record");
2817

2818 2819
  if (res)
  {
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
    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);
2832 2833
  }

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

2842 2843
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
    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);
2862 2863

#ifndef NO_EMBEDDED_ACCESS_CHECKS
2864 2865
    uint col_access;
    check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
2866
                 &tables->grant.privilege, 0, 0, test(tables->schema_table));
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
    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)
2877
      {
2878 2879
        *end++=',';
        end=strmov(end,grant_types.type_names[bitnr]);
2880
      }
2881
    }
2882
    table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);
2883

unknown's avatar
unknown committed
2884
#endif
2885 2886 2887 2888
    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);
2889
    table->field[4]->store((longlong) count, TRUE);
2890 2891 2892 2893 2894 2895 2896 2897 2898
    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)
    {
2899
      table->field[5]->store(STRING_WITH_LEN("CURRENT_TIMESTAMP"), cs);
2900 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
      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);
2929 2930 2931
    if (field->has_charset() || is_blob ||
        field->real_type() == MYSQL_TYPE_VARCHAR ||  // For varbinary type
        field->real_type() == MYSQL_TYPE_STRING)     // For binary type
2932
    {
unknown's avatar
unknown committed
2933 2934 2935
      uint32 octet_max_length= field->max_length();
      if (octet_max_length != (uint32) 4294967295U)
        octet_max_length /= field->charset()->mbmaxlen;
2936
      longlong char_max_len= is_blob ? 
unknown's avatar
unknown committed
2937 2938
        (longlong) octet_max_length / field->charset()->mbminlen :
        (longlong) octet_max_length / field->charset()->mbmaxlen;
2939
      table->field[8]->store(char_max_len, TRUE);
2940
      table->field[8]->set_notnull();
unknown's avatar
unknown committed
2941
      table->field[9]->store((longlong) octet_max_length, TRUE);
2942 2943
      table->field[9]->set_notnull();
    }
2944

2945 2946 2947 2948
    /*
      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
2949

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
    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)
    {
2982
      table->field[10]->store((longlong) field_length, TRUE);
2983
      table->field[10]->set_notnull();
2984
    }
2985 2986
    if (decimals >= 0)
    {
2987
      table->field[11]->store((longlong) decimals, TRUE);
2988 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
      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);
3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
  }
  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;
3028 3029

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


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

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

unknown's avatar
unknown committed
3070 3071
      table->field[0]->store(hton->name, strlen(hton->name), scs);
      tmp= hton->state ? "DISABLED" : "ENABLED";
unknown's avatar
unknown committed
3072
      table->field[1]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3073 3074
      table->field[2]->store(hton->comment, strlen(hton->comment), scs);
      tmp= hton->commit ? "YES" : "NO";
unknown's avatar
unknown committed
3075
      table->field[3]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3076
      tmp= hton->prepare ? "YES" : "NO";
unknown's avatar
unknown committed
3077
      table->field[4]->store( tmp, strlen(tmp), scs);
unknown's avatar
unknown committed
3078
      tmp= hton->savepoint_set ? "YES" : "NO";
unknown's avatar
unknown committed
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
      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
3089 3090 3091
int fill_schema_engines(THD *thd, TABLE_LIST *tables, COND *cond)
{
  return plugin_foreach(thd, iter_schema_engines, 
unknown's avatar
fixes  
unknown committed
3092
                        MYSQL_STORAGE_ENGINE_PLUGIN, tables->table);
unknown's avatar
unknown committed
3093 3094 3095
}


3096 3097 3098 3099 3100 3101
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;
3102
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3103 3104 3105 3106
  {
    CHARSET_INFO **cl;
    CHARSET_INFO *tmp_cs= cs[0];
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) || 
unknown's avatar
unknown committed
3107
         (tmp_cs->state & MY_CS_HIDDEN) ||
3108 3109
        !(tmp_cs->state & MY_CS_PRIMARY))
      continue;
3110
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3111 3112 3113 3114 3115 3116 3117 3118 3119
    {
      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;
3120
	restore_record(table, s->default_values);
3121 3122
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3123
        table->field[2]->store((longlong) tmp_cl->number, TRUE);
3124 3125 3126 3127
        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);
3128
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply, TRUE);
3129 3130
        if (schema_table_store_record(thd, table))
          return 1;
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
      }
    }
  }
  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;
3143
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3144 3145 3146 3147 3148 3149
  {
    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;
3150
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3151 3152 3153 3154 3155
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
3156
      restore_record(table, s->default_values);
3157 3158
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3159 3160
      if (schema_table_store_record(thd, table))
        return 1;
3161 3162 3163 3164 3165 3166
    }
  }
  return 0;
}


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

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


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;
3243
  TABLE *table= tables->table;
3244
  bool full_access;
3245
  char definer[USER_HOST_BUFF_SIZE];
3246
  Open_tables_state open_tables_state_backup;
3247 3248
  DBUG_ENTER("fill_schema_proc");

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

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


unknown's avatar
unknown committed
3290 3291 3292 3293
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)
3294 3295 3296
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
3297 3298 3299 3300 3301 3302 3303
  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
3304
      */
3305 3306 3307 3308 3309 3310 3311 3312 3313
      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)
3314 3315 3316
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3317
    show_table->file->info(HA_STATUS_VARIABLE |
3318 3319
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3320
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3321 3322 3323 3324 3325
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
3326
        restore_record(table, s->default_values);
3327 3328
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
3329
        table->field[3]->store((longlong) ((key_info->flags &
3330
                                            HA_NOSAME) ? 0 : 1), TRUE);
3331 3332
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
3333
        table->field[6]->store((longlong) (j+1), TRUE);
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
        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]);
3349
          table->field[9]->store((longlong) records, TRUE);
3350 3351
          table->field[9]->set_notnull();
        }
3352 3353 3354
        if (!(key_info->flags & HA_FULLTEXT) &&
            (key_part->field &&
             key_part->length !=
3355 3356
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
3357
          table->field[10]->store((longlong) key_part->length /
unknown's avatar
unknown committed
3358
                                  key_part->field->charset()->mbmaxlen);
3359 3360 3361 3362 3363 3364 3365
          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);
3366
        if (!show_table->s->keys_in_use.is_set(i))
3367
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3368 3369 3370
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
3371 3372
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3373 3374 3375
      }
    }
  }
unknown's avatar
unknown committed
3376
  DBUG_RETURN(res);
3377 3378 3379
}


unknown's avatar
unknown committed
3380 3381 3382 3383
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)
3384 3385 3386
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
3387
  char definer[USER_HOST_BUFF_SIZE];
unknown's avatar
unknown committed
3388
  uint definer_len;
unknown's avatar
unknown committed
3389

3390 3391 3392 3393 3394 3395 3396
  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
3397

3398 3399 3400 3401
    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
3402
      else
3403
        table->field[4]->store(STRING_WITH_LEN("CASCADED"), cs);
3404
    }
3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421
    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)
3422 3423 3424
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                   thd->net.last_errno, thd->net.last_error);
  }
3425 3426
  if (res) 
    thd->clear_error();
3427
  DBUG_RETURN(0);
3428 3429 3430
}


3431 3432 3433
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)
3434 3435
{
  CHARSET_INFO *cs= system_charset_info;
3436
  restore_record(table, s->default_values);
3437 3438 3439 3440 3441
  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);
3442
  return schema_table_store_record(thd, table);
3443 3444 3445
}


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

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

    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++))
    {
3495 3496 3497 3498 3499
      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);
3500 3501
    }
  }
unknown's avatar
unknown committed
3502
  DBUG_RETURN(res);
3503 3504 3505
}


3506 3507 3508 3509
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,
3510
                          LEX_STRING *trigger_stmt,
3511 3512
                          ulong sql_mode,
                          LEX_STRING *definer_buffer)
3513 3514
{
  CHARSET_INFO *cs= system_charset_info;
3515 3516 3517
  byte *sql_mode_str;
  ulong sql_mode_len;

3518 3519 3520 3521 3522 3523 3524 3525
  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);
3526
  table->field[10]->store(STRING_WITH_LEN("ROW"), cs);
3527 3528
  table->field[11]->store(trg_action_time_type_names[timing].str,
                          trg_action_time_type_names[timing].length, cs);
3529 3530
  table->field[14]->store(STRING_WITH_LEN("OLD"), cs);
  table->field[15]->store(STRING_WITH_LEN("NEW"), cs);
3531 3532 3533 3534 3535

  sql_mode_str=
    sys_var_thd_sql_mode::symbolic_mode_representation(thd,
                                                       sql_mode,
                                                       &sql_mode_len);
unknown's avatar
unknown committed
3536
  table->field[17]->store((const char*)sql_mode_str, sql_mode_len, cs);
3537
  table->field[18]->store((const char *)definer_buffer->str, definer_buffer->length, cs);
3538 3539 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
  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;
3570
        ulong sql_mode;
3571
        char definer_holder[USER_HOST_BUFF_SIZE];
3572 3573
        LEX_STRING definer_buffer;
        definer_buffer.str= definer_holder;
3574 3575
        if (triggers->get_trigger_info(thd, (enum trg_event_type) event,
                                       (enum trg_action_time_type)timing,
3576
                                       &trigger_name, &trigger_stmt,
3577 3578
                                       &sql_mode,
                                       &definer_buffer))
3579
          continue;
3580

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


3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
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);
3604
  table->field[7]->store((longlong) idx, TRUE);
3605 3606 3607
}


unknown's avatar
unknown committed
3608 3609 3610 3611 3612
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)
3613 3614
{
  DBUG_ENTER("get_schema_key_column_usage_record");
3615 3616 3617 3618 3619 3620 3621 3622 3623
  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)
3624 3625 3626 3627
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3628
    uint primary_key= show_table->s->primary_key;
3629 3630 3631
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3632
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3633 3634
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
unknown's avatar
unknown committed
3635
        continue;
3636 3637 3638 3639 3640 3641 3642
      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++;
3643
          restore_record(table, s->default_values);
3644 3645 3646 3647 3648 3649
          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);
3650 3651
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
3652 3653 3654 3655 3656 3657 3658 3659 3660
        }
      }
    }

    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++))
    {
3661
      LEX_STRING *f_info;
3662
      LEX_STRING *r_info;
3663 3664 3665 3666 3667
      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++))
      {
3668
        r_info= it1++;
3669
        f_idx++;
3670
        restore_record(table, s->default_values);
3671 3672 3673 3674 3675
        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);
3676
        table->field[8]->store((longlong) f_idx, TRUE);
3677
        table->field[8]->set_notnull();
3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
        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();
3689 3690
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3691 3692 3693
      }
    }
  }
unknown's avatar
unknown committed
3694
  DBUG_RETURN(res);
3695 3696 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
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;
3789
  handler *file;
unknown's avatar
unknown committed
3790
#ifdef WITH_PARTITION_STORAGE_ENGINE
3791
  partition_info *part_info;
unknown's avatar
unknown committed
3792
#endif
3793 3794 3795 3796
  DBUG_ENTER("get_schema_partitions_record");

  if (res)
  {
3797
    if (!tables->view)
3798 3799 3800 3801 3802
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
3803
  file= show_table->file;
unknown's avatar
unknown committed
3804
#ifdef WITH_PARTITION_STORAGE_ENGINE
3805
  part_info= show_table->part_info;
3806 3807 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
  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();
    }

3863
    if (part_info->is_sub_partitioned())
3864 3865 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
    {
      /* 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);
3917 3918 3919 3920 3921 3922
        if (part_elem->has_null_value)
        {
          tmp_str.append("NULL");
          if (no_items > 0)
            tmp_str.append(",");
        }
3923 3924 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
        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
3968
#endif
3969 3970 3971 3972 3973 3974 3975 3976 3977
  {
    store_schema_partitions_record(thd, table, 0, file, 0);
    if(schema_table_store_record(thd, table))
      DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


3978 3979 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
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
4019
extern LEX_STRING interval_type_to_name[];
4020 4021 4022 4023 4024 4025 4026

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
4027
  Event_timed et;    
4028 4029 4030 4031 4032 4033
  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))
  {
4034
    my_error(ER_CANNOT_LOAD_FROM_TABLE, MYF(0));
4035 4036 4037 4038 4039 4040
    DBUG_RETURN(1);
  }

  if (!(!wild || !wild[0] || !wild_compare(et.name.str, wild, 0)))
    DBUG_RETURN(0);
  
4041
  /* ->field[0] is EVENT_CATALOG and is by default NULL */
4042 4043 4044 4045 4046 4047
  
  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
4048
  /* [9] is SQL_MODE */
4049
  {
4050
    byte *sql_mode_str;
4051 4052 4053 4054 4055 4056 4057
    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);
  }
  
4058 4059
  if (et.expression)
  {
4060
    String show_str;
4061
    /* type */
4062
    sch_table->field[5]->store(STRING_WITH_LEN("RECURRING"), scs);
4063

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

unknown's avatar
unknown committed
4068 4069 4070 4071
    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];
4072
    sch_table->field[8]->set_notnull();
unknown's avatar
unknown committed
4073
    sch_table->field[8]->store(ival->str, ival->length, scs);
4074 4075

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

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

4090 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
    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)
4116 4117
  {
    sch_table->field[16]->set_notnull();
4118
    sch_table->field[16]->store_time(&et.last_executed,MYSQL_TIMESTAMP_DATETIME);
4119
  }
4120 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

  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;
    }
4186
    if (!(key_buf= (byte *)alloc_root(thd->mem_root, key_len)))
4187 4188 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
    {
      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);
}


4228 4229 4230 4231 4232 4233 4234
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;
4235 4236
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
            && thd->is_fatal_error)
4237 4238 4239 4240
    DBUG_RETURN(1);

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


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
4256
  int res= 0;
4257 4258
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
4259
  pthread_mutex_lock(&LOCK_global_system_variables);
4260
  res= show_status_array(thd, wild, init_vars,
4261 4262
                         lex->option_type, 0, "", tables->table);
  pthread_mutex_unlock(&LOCK_global_system_variables);
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
  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;
4274
  pthread_mutex_lock(&LOCK_status);
4275 4276
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
4277 4278 4279 4280
  res= show_status_array(thd, wild,
                         (SHOW_VAR *)all_status_vars.buffer,
                         OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ?
4281 4282
                          &tmp: &thd->status_var), "",tables->table);
  pthread_mutex_unlock(&LOCK_status);
4283 4284 4285 4286
  DBUG_RETURN(res);
}


4287 4288 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
/*
  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);
}


4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
/*
  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;
4372
  for (; schema_table->table_name; schema_table++)
4373 4374 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
  {
    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
*/

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

4413
  for (; fields_info->field_name; fields_info++)
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430
  {
    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:
4431 4432
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
4433 4434 4435
      {
        DBUG_RETURN(0);
      }
4436
      item->max_length= fields_info->field_length * cs->mbmaxlen;
unknown's avatar
unknown committed
4437 4438
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
4439 4440 4441 4442 4443 4444 4445 4446 4447
      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
4448
  tmp_table_param->table_charset= cs;
4449
  tmp_table_param->field_count= field_count;
4450
  tmp_table_param->schema_table= 1;
4451 4452 4453 4454 4455
  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
4456
                                HA_POS_ERROR, table_list->alias)))
4457
    DBUG_RETURN(0);
4458
  table_list->schema_table_param= tmp_table_param;
4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
  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;
4481
  Name_resolution_context *context= &thd->lex->select_lex.context;
4482
  for (; field_info->field_name; field_info++)
4483 4484 4485
  {
    if (field_info->old_name)
    {
4486 4487
      Item_field *field= new Item_field(context,
                                        NullS, NullS, field_info->field_name);
4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506
      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;
4507
  Name_resolution_context *context= &sel->context;
4508 4509 4510 4511 4512

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
4513 4514
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4515 4516 4517 4518 4519 4520
    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())
    {
4521
      buffer.append(STRING_WITH_LEN(" ("));
4522
      buffer.append(lex->wild->ptr());
4523
      buffer.append(')');
4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
    }
    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;
4536
  Name_resolution_context *context= &lex->select_lex.context;
4537 4538 4539 4540 4541 4542 4543

  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())
  {
4544
    buffer.append(STRING_WITH_LEN(" ("));
4545
    buffer.append(lex->wild->ptr());
4546
    buffer.append(')');
4547
  }
4548 4549
  Item_field *field= new Item_field(context,
                                    NullS, NullS, field_info->field_name);
4550 4551 4552 4553 4554 4555 4556
  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];
4557
    field= new Item_field(context, NullS, NullS, field_info->field_name);
4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568
    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)
{
4569 4570 4571
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4572 4573
  Name_resolution_context *context= &thd->lex->select_lex.context;

4574
  for (; *field_num >= 0; field_num++)
4575
  {
4576 4577 4578 4579 4580
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
4581 4582
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4583
    if (field)
4584
    {
4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595
      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;
}


4596 4597 4598 4599 4600
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;
4601 4602
  Name_resolution_context *context= &thd->lex->select_lex.context;

4603 4604 4605
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4606 4607
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620
    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;
}


4621 4622 4623 4624 4625
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;
4626 4627
  Name_resolution_context *context= &thd->lex->select_lex.context;

4628 4629 4630
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4631 4632
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4633 4634 4635 4636 4637 4638 4639
    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;
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
    }
  }
  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");
4664
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4665 4666 4667
  {
    DBUG_RETURN(1);
  }
4668
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4669
  table->grant.privilege= SELECT_ACL;
4670 4671 4672 4673 4674 4675 4676
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
4677 4678 4679 4680
  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
4681 4682
  table_list->table_name= table->s->table_name.str;
  table_list->table_name_length= table->s->table_name.length;
4683 4684 4685 4686
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4687
  lex->safe_to_cache_query= 0;
4688 4689 4690 4691 4692

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

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

4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750
  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
4751
  DBUG_PRINT("enter", ("mysql_schema_select: %s", schema_table->table_name));
4752
  /*
4753 4754 4755 4756 4757 4758 4759
     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);
4760
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
      !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);
}


/*
4772
  Fill temporary schema tables before SELECT
4773 4774 4775 4776 4777 4778

  SYNOPSIS
    get_schema_tables_result()
    join  join which use schema tables

  RETURN
unknown's avatar
unknown committed
4779 4780
    FALSE success
    TRUE  error
4781 4782
*/

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

  thd->no_warnings_for_error= 1;
4792 4793 4794 4795
  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
4796

unknown's avatar
unknown committed
4797
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
unknown's avatar
unknown committed
4798
    if (table_list->schema_table && thd->fill_derived_tables())
4799
    {
4800 4801
      bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
                          lex->current_select->master_unit()->item);
4802 4803 4804 4805 4806 4807 4808 4809 4810
      /*
        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
4811 4812 4813 4814 4815 4816 4817 4818 4819
      {
        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;

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

4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
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
4851
  DBUG_ENTER("fill_schema_files");
4852 4853 4854 4855 4856 4857 4858 4859 4860 4861

  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);
}
4862 4863 4864

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


ST_FIELD_INFO tables_fields_info[]=
{
unknown's avatar
unknown committed
4876 4877 4878 4879 4880 4881 4882
  {"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"},
4883
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
unknown's avatar
unknown committed
4884 4885 4886 4887 4888 4889 4890 4891 4892
  {"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"},
4893
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
unknown's avatar
unknown committed
4894 4895
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
4896
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
unknown's avatar
unknown committed
4897
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4898 4899 4900 4901 4902
};


ST_FIELD_INFO columns_fields_info[]=
{
unknown's avatar
unknown committed
4903 4904 4905 4906 4907
  {"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},
4908 4909
  {"COLUMN_DEFAULT", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Default"},
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
4910
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
4911 4912
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4913 4914
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4915 4916
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
unknown's avatar
unknown committed
4917
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
4918
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
unknown's avatar
unknown committed
4919 4920
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
4921
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
unknown's avatar
unknown committed
4922
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4923 4924 4925 4926 4927
};


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


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


unknown's avatar
unknown committed
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
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}
};


4960 4961 4962 4963 4964 4965 4966 4967 4968
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
4969
  {"INTERVAL_VALUE", 256, MYSQL_TYPE_STRING, 0, 1, "Interval value"},
4970
  {"INTERVAL_FIELD", 18, MYSQL_TYPE_STRING, 0, 1, "Interval field"},
4971
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984
  {"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}
};



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


ST_FIELD_INFO proc_fields_info[]=
{
unknown's avatar
unknown committed
4995 4996 4997 4998 4999
  {"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"},
5000 5001
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5002
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5003 5004
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
5005
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5006 5007 5008 5009
  {"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"},
5010 5011
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
unknown's avatar
unknown committed
5012
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5013
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
5014
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
unknown's avatar
unknown committed
5015
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5016 5017 5018 5019 5020
};


ST_FIELD_INFO stat_fields_info[]=
{
unknown's avatar
unknown committed
5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036
  {"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}
5037 5038 5039 5040 5041
};


ST_FIELD_INFO view_fields_info[]=
{
unknown's avatar
unknown committed
5042 5043 5044 5045
  {"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},
5046
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5047
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5048 5049
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5050
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5051 5052 5053 5054 5055
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5056 5057 5058 5059 5060
  {"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}
5061 5062 5063 5064 5065
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5066 5067 5068 5069 5070 5071
  {"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}
5072 5073 5074 5075 5076
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5077 5078 5079 5080 5081 5082 5083
  {"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}
5084 5085 5086 5087 5088
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
unknown's avatar
unknown committed
5089 5090 5091 5092 5093 5094 5095 5096
  {"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}
5097 5098 5099 5100 5101
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
unknown's avatar
unknown committed
5102 5103 5104 5105 5106 5107 5108
  {"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}
5109 5110 5111 5112 5113
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
unknown's avatar
unknown committed
5114 5115 5116
  {"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},
5117
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
5118 5119 5120 5121
  {"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},
5122
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
5123 5124 5125
  {"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
5126
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5127 5128 5129 5130 5131
};


ST_FIELD_INFO table_names_fields_info[]=
{
unknown's avatar
unknown committed
5132 5133 5134 5135 5136
  {"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}
5137 5138 5139
};


5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
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}
};


5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168
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"},
5169
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, "sql_mode"},
5170
  {"DEFINER", 65535, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5171 5172 5173 5174
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5175 5176 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
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}
};


5206 5207 5208 5209 5210 5211 5212 5213
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}
};


5214 5215 5216 5217 5218 5219 5220
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"},
5221
  {"TIME", 7, MYSQL_TYPE_LONG, 0, 0, "Time"},
5222
  {"STATE", 30, MYSQL_TYPE_STRING, 0, 1, "State"},
5223
  {"INFO", PROCESS_LIST_INFO_WIDTH, MYSQL_TYPE_STRING, 0, 1, "Info"},
5224 5225 5226 5227
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


unknown's avatar
unknown committed
5228 5229 5230
ST_FIELD_INFO plugin_fields_info[]=
{
  {"PLUGIN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
unknown's avatar
unknown committed
5231
  {"PLUGIN_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5232
  {"PLUGIN_STATUS", 10, MYSQL_TYPE_STRING, 0, 0, "Status"},
5233
  {"PLUGIN_TYPE", 80, MYSQL_TYPE_STRING, 0, 0, "Type"},
unknown's avatar
unknown committed
5234
  {"PLUGIN_TYPE_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
unknown's avatar
unknown committed
5235
  {"PLUGIN_LIBRARY", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Library"},
unknown's avatar
unknown committed
5236
  {"PLUGIN_LIBRARY_VERSION", 20, MYSQL_TYPE_STRING, 0, 1, 0},
unknown's avatar
unknown committed
5237 5238 5239 5240 5241
  {"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}
};

5242 5243 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
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
5284

5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
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}
};


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

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

5306 5307 5308 5309 5310
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
5311
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
5312
  {"COLLATIONS", collation_fields_info, create_schema_table, 
5313
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
5314
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
5315
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
5316 5317
  {"COLUMNS", columns_fields_info, create_schema_table, 
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
5318
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
5319
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
unknown's avatar
unknown committed
5320
  {"ENGINES", engines_fields_info, create_schema_table,
5321
   fill_schema_engines, make_old_format, 0, -1, -1, 0},
5322 5323
  {"EVENTS", events_fields_info, create_schema_table,
   fill_schema_events, make_old_format, 0, -1, -1, 0},
5324 5325
  {"FILES", files_fields_info, create_schema_table,
   fill_schema_files, 0, 0, -1, -1, 0},
5326
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
5327
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
5328 5329
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
5330 5331
  {"PARTITIONS", partitions_fields_info, create_schema_table,
   get_all_tables, 0, get_schema_partitions_record, 1, 2, 0},
unknown's avatar
unknown committed
5332 5333
  {"PLUGINS", plugin_fields_info, create_schema_table,
    fill_plugins, make_old_format, 0, -1, -1, 0},
5334 5335
  {"PROCESSLIST", processlist_fields_info, create_schema_table,
    fill_schema_processlist, make_old_format, 0, -1, -1, 0},
5336 5337 5338
  {"REFERENTIAL_CONSTRAINTS", referential_constraints_fields_info,
   create_schema_table, get_all_tables, 0, get_referential_constraints_record,
   1, 9, 0},
5339 5340 5341 5342 5343 5344 5345 5346
  {"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},
5347 5348
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
5349 5350 5351 5352 5353 5354 5355 5356
  {"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},
5357
  {"TRIGGERS", triggers_fields_info, create_schema_table,
5358
   get_all_tables, make_old_format, get_schema_triggers_record, 5, 6, 0},
5359 5360
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
5361 5362
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
5363 5364
  {"VIEWS", view_fields_info, create_schema_table, 
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
5365
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
5366 5367 5368
};


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