sql_show.cc 170 KB
Newer Older
monty@mysql.com's avatar
monty@mysql.com committed
1
/* Copyright (C) 2000-2004 MySQL AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


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

#include "mysql_priv.h"
21
#include "sql_select.h"                         // For select_describe
22
#include "sql_show.h"
23
#include "repl_failsafe.h"
24
#include "sp.h"
25
#include "sp_head.h"
26
#include "sql_trigger.h"
brian@grrr.local's avatar
brian@grrr.local committed
27
#include "authors.h"
28
#include "event.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
29
#include <my_dir.h>
30

31 32 33
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#endif
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
34

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

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

45 46 47 48
/***************************************************************************
** List all table types supported 
***************************************************************************/

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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;  
}

75
bool mysqld_show_storage_engines(THD *thd)
76 77
{
  List<Item> field_list;
78
  Protocol *protocol= thd->protocol;
79
  DBUG_ENTER("mysqld_show_storage_engines");
80

monty@mishka.local's avatar
monty@mishka.local committed
81
  field_list.push_back(new Item_empty_string("Engine",10));
82
  field_list.push_back(new Item_empty_string("Support",10));
83
  field_list.push_back(new Item_empty_string("Comment",80));
84 85 86
  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));
87

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

92 93 94
  if (plugin_foreach(thd, show_handlerton, 
                     MYSQL_STORAGE_ENGINE_PLUGIN, thd->variables.table_type))
    DBUG_RETURN(TRUE);
95

96
  send_eof(thd);
97
  DBUG_RETURN(FALSE);
98 99
}

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

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);
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
117 118 119 120 121

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

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

serg@serg.mylan's avatar
serg@serg.mylan committed
139 140 141
  table->field[3]->store(plugin_type_names[plug->type].str,
                         plugin_type_names[plug->type].length,
                         cs);
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
142
  table->field[4]->store(version_buf,
serg@serg.mylan's avatar
serg@serg.mylan committed
143
        make_version_string(version_buf, sizeof(version_buf),
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
144
                            *(uint *)plug->info), cs);
145 146 147

  if (plugin->plugin_dl)
  {
serg@serg.mylan's avatar
serg@serg.mylan committed
148
    table->field[5]->store(plugin->plugin_dl->dl.str,
149 150
                           plugin->plugin_dl->dl.length, cs);
    table->field[5]->set_notnull();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
151
    table->field[6]->store(version_buf,
serg@serg.mylan's avatar
serg@serg.mylan committed
152
          make_version_string(version_buf, sizeof(version_buf),
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
153 154 155
                              plugin->plugin_dl->version),
          cs);
    table->field[6]->set_notnull();
156 157
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
158
  {
159
    table->field[5]->set_null();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
160 161 162 163
    table->field[6]->set_null();
  }


164 165
  if (plug->author)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
166 167
    table->field[7]->store(plug->author, strlen(plug->author), cs);
    table->field[7]->set_notnull();
168 169
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
170
    table->field[7]->set_null();
171 172 173

  if (plug->descr)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
174 175
    table->field[8]->store(plug->descr, strlen(plug->descr), cs);
    table->field[8]->set_notnull();
176 177
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
178
    table->field[8]->set_null();
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

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


brian@grrr.local's avatar
brian@grrr.local committed
196 197 198 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
/***************************************************************************
** 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);
}
228

229
/***************************************************************************
230
 List all privileges supported
231 232
***************************************************************************/

233 234 235 236
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
237 238
};

239 240
static struct show_privileges_st sys_privileges[]=
{
241
  {"Alter", "Tables",  "To alter the table"},
242
  {"Alter routine", "Functions,Procedures",  "To alter or drop stored functions/procedures"},
243
  {"Create", "Databases,Tables,Indexes",  "To create new databases and tables"},
244
  {"Create routine","Functions,Procedures","To use CREATE FUNCTION/PROCEDURE"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
245 246
  {"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
  {"Create view", "Tables",  "To create new views"},
247
  {"Create user", "Server Admin",  "To create new users"},
248
  {"Delete", "Tables",  "To delete existing rows"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
249
  {"Drop", "Databases,Tables", "To drop databases, tables, and views"},
250
  {"Event","Server Admin","To create, alter, drop and execute events"},
251
  {"Execute", "Functions,Procedures", "To execute stored routines"},
252
  {"File", "File access on server",   "To read and write files on the server"},
253
  {"Grant option",  "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
254 255 256 257
  {"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"},
258
  {"References", "Databases,Tables", "To have references on tables"},
259 260 261 262 263
  {"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"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
264 265
  {"Show view","Tables","To see views with SHOW CREATE VIEW"},
  {"Shutdown","Server Admin", "To shut down the server"},
266
  {"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
267
  {"Trigger","Tables", "To use triggers"},
268 269
  {"Update", "Tables",  "To update existing rows"},
  {"Usage","Server Admin","No privileges - allow connect only"},
270 271 272
  {NullS, NullS, NullS}
};

273
bool mysqld_show_privileges(THD *thd)
274 275
{
  List<Item> field_list;
276
  Protocol *protocol= thd->protocol;
277 278 279 280 281 282
  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));

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

287 288
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
289
  {
290
    protocol->prepare_for_resend();
291 292 293
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
294
    if (protocol->write())
295
      DBUG_RETURN(TRUE);
296
  }
297
  send_eof(thd);
298
  DBUG_RETURN(FALSE);
299 300 301 302
}


/***************************************************************************
303
  List all column types
304 305
***************************************************************************/

306 307
struct show_column_type_st
{
308 309
  const char *type;
  uint size;
310 311 312 313 314 315 316 317 318 319 320 321
  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;
322
};
323 324 325 326 327

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
328 329
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
330 331
    "NO",   "YES", "YES",  "NO",  "NULL,0",
    "A very small integer"},
332
  {"tinyint unsigned",
333 334
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",
    "YES",  "YES",  "YES",  "NO",  "NULL,0",
335 336 337
    "A very small integer"},
};

338
bool mysqld_show_column_types(THD *thd)
339 340
{
  List<Item> field_list;
341
  Protocol *protocol= thd->protocol;
342 343 344 345 346 347
  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));
348 349
  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));
350 351 352 353 354 355 356 357 358
  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));

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

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


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

403 404
  if (wild && !wild[0])
    wild=0;
405

bk@work.mysql.com's avatar
bk@work.mysql.com committed
406 407
  bzero((char*) &table_list,sizeof(table_list));

408 409 410 411 412 413
  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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
414
    DBUG_RETURN(-1);
415
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
416

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
494 495 496
  DBUG_RETURN(0);
}

497

498
bool
bk@work.mysql.com's avatar
bk@work.mysql.com committed
499 500
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
501 502 503
  Protocol *protocol= thd->protocol;
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
504 505
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
506
                      table_list->table_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
507

508 509 510
  /* We want to preserve the tree for views. */
  thd->lex->view_prepare_mode= TRUE;

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
511
  /* Only one table for now, but VIEW can involve several tables */
512
  if (open_normal_and_derived_tables(thd, table_list, 0))
513 514 515
  {
    if (!table_list->view || thd->net.last_errno != ER_VIEW_INVALID)
      DBUG_RETURN(TRUE);
516

517 518 519 520 521 522
    /*
      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);
523 524
    thd->clear_error();

525 526 527 528 529 530
    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);
  }
531

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
532 533 534
  /* TODO: add environment variables show when it become possible */
  if (thd->lex->only_view && !table_list->view)
  {
535
    my_error(ER_WRONG_OBJECT, MYF(0),
536
             table_list->db, table_list->table_name, "VIEW");
537
    DBUG_RETURN(TRUE);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
538
  }
539

540
  buffer.length(0);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
541 542
  if ((table_list->view ?
       view_store_create_info(thd, table_list, &buffer) :
543
       store_create_info(thd, table_list, &buffer, NULL)))
544
    DBUG_RETURN(TRUE);
545

bk@work.mysql.com's avatar
bk@work.mysql.com committed
546
  List<Item> field_list;
547 548 549 550 551 552 553 554 555 556 557 558 559
  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)));
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
560

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

577
  if (protocol->write())
578
    DBUG_RETURN(TRUE);
579
  send_eof(thd);
580
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
581 582
}

583 584
bool mysqld_show_create_db(THD *thd, char *dbname,
                           HA_CREATE_INFO *create_info)
585
{
586
  Security_context *sctx= thd->security_ctx;
587
  int length;
588
  char path[FN_REFLEN];
589 590
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
591
#ifndef NO_EMBEDDED_ACCESS_CHECKS
592
  uint db_access;
593
#endif
594 595 596 597 598 599 600
  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
601
  if (test_all_bits(sctx->master_access, DB_ACLS))
602 603
    db_access=DB_ACLS;
  else
604 605
    db_access= (acl_get(sctx->host, sctx->ip, sctx->priv_user, dbname, 0) |
		sctx->master_access);
606 607
  if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname)))
  {
608
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
609
             sctx->priv_user, sctx->host_or_ip, dbname);
610 611
    general_log_print(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR),
                      sctx->priv_user, sctx->host_or_ip, dbname);
612
    DBUG_RETURN(TRUE);
613 614
  }
#endif
615 616
  if (!my_strcasecmp(system_charset_info, dbname,
                     information_schema_name.str))
617
  {
618 619
    dbname= information_schema_name.str;
    create.default_table_charset= system_charset_info;
620
  }
621
  else
622
  {
623
    length= build_table_filename(path, sizeof(path), dbname, "", "");
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    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);
639 640 641 642 643
  }
  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));

644 645
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
646
    DBUG_RETURN(TRUE);
647 648 649 650

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

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

  if (protocol->write())
671
    DBUG_RETURN(TRUE);
672
  send_eof(thd);
673
  DBUG_RETURN(FALSE);
674
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
675

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
676 677


bk@work.mysql.com's avatar
bk@work.mysql.com committed
678
/****************************************************************************
679 680
  Return only fields for API mysql_list_fields
  Use "show table wildcard" in mysql instead of this
bk@work.mysql.com's avatar
bk@work.mysql.com committed
681 682 683 684 685 686 687
****************************************************************************/

void
mysqld_list_fields(THD *thd, TABLE_LIST *table_list, const char *wild)
{
  TABLE *table;
  DBUG_ENTER("mysqld_list_fields");
688
  DBUG_PRINT("enter",("table: %s",table_list->table_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
689

690
  if (open_normal_and_derived_tables(thd, table_list, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
691
    DBUG_VOID_RETURN;
692 693
  table= table_list->table;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
694 695 696 697 698
  List<Item> field_list;

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

711

bk@work.mysql.com's avatar
bk@work.mysql.com committed
712
int
713
mysqld_dump_create_info(THD *thd, TABLE_LIST *table_list, int fd)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
714
{
715 716
  Protocol *protocol= thd->protocol;
  String *packet= protocol->storage_packet();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
717
  DBUG_ENTER("mysqld_dump_create_info");
718
  DBUG_PRINT("enter",("table: %s",table_list->table->s->table_name.str));
719

720
  protocol->prepare_for_resend();
721
  if (store_create_info(thd, table_list, packet, NULL))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
722
    DBUG_RETURN(-1);
723

724
  if (fd < 0)
725
  {
726
    if (protocol->write())
727
      DBUG_RETURN(-1);
728
    protocol->flush();
729
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
730
  else
731
  {
732 733
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
734 735
      DBUG_RETURN(-1);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
736 737
  DBUG_RETURN(0);
}
738

739
/*
740
  Go through all character combinations and ensure that sql_lex.cc can
741
  parse it as an identifier.
742 743

  SYNOPSIS
744 745 746 747 748 749 750
  require_quotes()
  name			attribute name
  name_length		length of name

  RETURN
    #	Pointer to conflicting character
    0	No conflicting character
751 752
*/

753
static const char *require_quotes(const char *name, uint name_length)
754
{
755 756 757
  uint length;
  const char *end= name + name_length;

758
  for (; name < end ; name++)
759
  {
760 761 762 763
    uchar chr= (uchar) *name;
    length= my_mbcharlen(system_charset_info, chr);
    if (length == 1 && !system_charset_info->ident_map[chr])
      return name;
764 765 766
  }
  return 0;
}
767

768

769 770 771 772 773 774 775 776 777 778 779 780
/*
  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
*/

781 782
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
783
{
784 785
  const char *name_end;
  char quote_char;
786
  int q= get_quote_char_for_identifier(thd, name, length);
787

788
  if (q == EOF)
789
  {
790
    packet->append(name, length, system_charset_info);
791 792 793
    return;
  }

794 795 796 797
  /*
    The identifier must be quoted as it includes a quote character or
   it's a keyword
  */
798

799
  VOID(packet->reserve(length*2 + 2));
800
  quote_char= (char) q;
801 802 803 804
  packet->append(&quote_char, 1, system_charset_info);

  for (name_end= name+length ; name < name_end ; name+= length)
  {
805
    uchar chr= (uchar) *name;
806
    length= my_mbcharlen(system_charset_info, chr);
807 808 809 810 811 812 813 814 815
    /*
      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;
816
    if (length == 1 && chr == (uchar) quote_char)
817 818
      packet->append(&quote_char, 1, system_charset_info);
    packet->append(name, length, packet->charset());
819
  }
820
  packet->append(&quote_char, 1, system_charset_info);
821 822
}

823

824 825 826 827 828 829 830 831 832 833
/*
  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
834 835 836 837 838
    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;
839 840 841 842 843 844 845
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
    is set.

  RETURN
    EOF	  No quote character is needed
    #	  Quote character
*/
846 847 848

int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
{
anozdrin@mysql.com's avatar
anozdrin@mysql.com committed
849
  if (length &&
850
      !is_keyword(name,length) &&
851 852 853
      !require_quotes(name, length) &&
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
    return EOF;
854
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
855
    return '"';
856
  return '`';
857 858 859
}


860 861 862 863 864
/* Append directory name (if exists) to CREATE INFO */

static void append_directory(THD *thd, String *packet, const char *dir_type,
			     const char *filename)
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
865
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
866
  {
867
    uint length= dirname_length(filename);
868 869
    packet->append(' ');
    packet->append(dir_type);
870
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
871
#ifdef __WIN__
monty@mysql.com's avatar
monty@mysql.com committed
872 873 874 875 876 877 878 879 880
    /* 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;
881
#endif
monty@mysql.com's avatar
monty@mysql.com committed
882
    packet->append(filename, length);
883 884 885 886 887
    packet->append('\'');
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
888
#define LIST_PROCESS_HOST_LEN 64
889

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
/*
  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)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
915
{
916
  List<Item> field_list;
917
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end, uname[NAME_LEN*3+1];
918
  const char *alias;
919
  String type(tmp, sizeof(tmp), system_charset_info);
920 921 922
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
923
  TABLE *table= table_list->table;
924
  handler *file= table->file;
925
  TABLE_SHARE *share= table->s;
926
  HA_CREATE_INFO create_info;
927 928 929 930
  my_bool foreign_db_mode=    (thd->variables.sql_mode & (MODE_POSTGRESQL |
							  MODE_ORACLE |
							  MODE_MSSQL |
							  MODE_DB2 |
931
							  MODE_MAXDB |
932 933 934 935
							  MODE_ANSI)) != 0;
  my_bool limited_mysql_mode= (thd->variables.sql_mode &
			       (MODE_NO_FIELD_OPTIONS | MODE_MYSQL323 |
				MODE_MYSQL40)) != 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
936
  DBUG_ENTER("store_create_info");
937
  DBUG_PRINT("enter",("table: %s", table->s->table_name.str));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
938

939
  restore_record(table, s->default_values); // Get empty record
940

941
  if (share->tmp_table)
942
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
943
  else
944
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
945
  if (table_list->schema_table)
946
    alias= table_list->schema_table->table_name;
947
  else
948 949 950 951 952 953 954 955
  {
    if (lower_case_table_names == 2)
      alias= table->alias;
    else
    {
      alias= share->table_name.str;
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
956
  append_identifier(thd, packet, alias, strlen(alias));
957
  packet->append(STRING_WITH_LEN(" (\n"));
958

bk@work.mysql.com's avatar
bk@work.mysql.com committed
959 960
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
961
    bool has_default;
962
    bool has_now_default;
963 964
    uint flags = field->flags;

965
    if (ptr != table->field)
966
      packet->append(STRING_WITH_LEN(",\n"));
967

968
    packet->append(STRING_WITH_LEN("  "));
969
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
970 971
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
972
    if (type.ptr() != tmp)
973
      type.set(tmp, sizeof(tmp), system_charset_info);
974 975
    else
      type.set_charset(system_charset_info);
976

bk@work.mysql.com's avatar
bk@work.mysql.com committed
977
    field->sql_type(type);
978
    packet->append(type.ptr(), type.length(), system_charset_info);
979

980 981
    if (field->has_charset() && 
        !(thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
982
    {
983
      if (field->charset() != share->table_charset)
984
      {
985
	packet->append(STRING_WITH_LEN(" CHARACTER SET "));
986 987 988 989 990 991 992 993
	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))
      {
994
	packet->append(STRING_WITH_LEN(" COLLATE "));
995
	packet->append(field->charset()->name);
996
      }
997
    }
998

999
    if (flags & NOT_NULL_FLAG)
1000
      packet->append(STRING_WITH_LEN(" NOT NULL"));
1001 1002 1003 1004 1005 1006
    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.
      */
1007
      packet->append(STRING_WITH_LEN(" NULL"));
1008
    }
1009 1010

    /* 
1011
      Again we are using CURRENT_TIMESTAMP instead of NOW because it is
1012 1013 1014 1015 1016
      more standard 
    */
    has_now_default= table->timestamp_field == field && 
                     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
    
1017
    has_default= (field->type() != FIELD_TYPE_BLOB &&
1018
                  !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
1019
		  field->unireg_check != Field::NEXT_NUMBER &&
1020
                  !((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
1021
                    has_now_default));
1022

1023
    if (has_default)
1024
    {
1025
      packet->append(STRING_WITH_LEN(" DEFAULT "));
1026
      if (has_now_default)
1027
        packet->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
1028
      else if (!field->is_null())
1029
      {                                             // Not null by default
1030
        type.set(tmp, sizeof(tmp), field->charset());
1031
        field->val_str(&type);
1032
	if (type.length())
1033
	{
1034
	  String def_val;
1035
          uint dummy_errors;
1036 1037
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
1038
		       system_charset_info, &dummy_errors);
1039 1040
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
1041
        else
1042
	  packet->append(STRING_WITH_LEN("''"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1043
      }
1044
      else if (field->maybe_null())
1045
        packet->append(STRING_WITH_LEN("NULL"));    // Null as default
1046
      else
1047
        packet->append(tmp);
1048
    }
1049

1050
    if (!(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS) &&
1051 1052
        table->timestamp_field == field && 
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
1053
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
1054

1055 1056
    if (field->unireg_check == Field::NEXT_NUMBER && 
        !(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS))
1057
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
1058 1059 1060

    if (field->comment.length)
    {
1061
      packet->append(STRING_WITH_LEN(" COMMENT "));
1062 1063
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1064 1065
  }

1066
  key_info= table->key_info;
1067
  bzero((char*) &create_info, sizeof(create_info));
1068
  file->update_create_info(&create_info);
1069
  primary_key= share->primary_key;
1070

1071
  for (uint i=0 ; i < share->keys ; i++,key_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1072
  {
1073 1074
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
1075
    packet->append(STRING_WITH_LEN(",\n  "));
1076

1077
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
1078 1079
    {
      found_primary=1;
1080 1081 1082 1083 1084
      /*
        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"));
1085
    }
1086
    else if (key_info->flags & HA_NOSAME)
1087
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
1088
    else if (key_info->flags & HA_FULLTEXT)
1089
      packet->append(STRING_WITH_LEN("FULLTEXT KEY "));
1090
    else if (key_info->flags & HA_SPATIAL)
1091 1092 1093
      packet->append(STRING_WITH_LEN("SPATIAL KEY "));
    else
      packet->append(STRING_WITH_LEN("KEY "));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1094

1095
    if (!found_primary)
1096
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
1097

1098 1099 1100
    if (!(thd->variables.sql_mode & MODE_NO_KEY_OPTIONS) &&
	!limited_mysql_mode && !foreign_db_mode)
    {
1101
      if (key_info->algorithm == HA_KEY_ALG_BTREE)
1102
        packet->append(STRING_WITH_LEN(" USING BTREE"));
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
1103

1104
      if (key_info->algorithm == HA_KEY_ALG_HASH)
1105
        packet->append(STRING_WITH_LEN(" USING HASH"));
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
1106

1107 1108 1109
      // +BAR: send USING only in non-default case: non-spatial rtree
      if ((key_info->algorithm == HA_KEY_ALG_RTREE) &&
	  !(key_info->flags & HA_SPATIAL))
1110
        packet->append(STRING_WITH_LEN(" USING RTREE"));
1111

jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
1112
      // No need to send USING FULLTEXT, it is sent as FULLTEXT KEY
1113
    }
1114
    packet->append(STRING_WITH_LEN(" ("));
1115

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1116 1117
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
1118
      if (j)
1119
        packet->append(',');
1120

1121
      if (key_part->field)
1122 1123
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
1124
      if (key_part->field &&
1125 1126 1127
          (key_part->length !=
           table->field[key_part->fieldnr-1]->key_length() &&
           !(key_info->flags & HA_FULLTEXT)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1128
      {
1129
        buff[0] = '(';
1130
        char* end=int10_to_str((long) key_part->length /
1131 1132
			       key_part->field->charset()->mbmaxlen,
			       buff + 1,10);
1133 1134
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1135 1136 1137
      }
    }
    packet->append(')');
1138 1139 1140 1141 1142 1143
    if (key_info->parser)
    {
      packet->append(" WITH PARSER ", 13);
      append_identifier(thd, packet, key_info->parser->name.str,
                        key_info->parser->name.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1144
  }
1145

1146 1147 1148 1149
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
1150

1151
  if ((for_str= file->get_foreign_key_create_info()))
1152 1153 1154
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
1155 1156
  }

1157
  packet->append(STRING_WITH_LEN("\n)"));
1158
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
1159
  {
1160 1161 1162 1163 1164
    /*
      Get possible table space definitions and append them
      to the CREATE TABLE statement
    */

1165
    if ((for_str= file->get_tablespace_name(thd)))
1166
    {
1167
      packet->append(" TABLESPACE ");
1168
      packet->append(for_str, strlen(for_str));
1169
      packet->append(" STORAGE DISK");
1170 1171 1172
      my_free(for_str, MYF(0));
    }

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    /*
      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="));
1184
#ifdef WITH_PARTITION_STORAGE_ENGINE
1185
    if (table->part_info)
1186 1187
      packet->append(ha_resolve_storage_engine_name(
                        table->part_info->default_engine_type));
1188
    else
1189
      packet->append(file->table_type());
1190
#else
1191
      packet->append(file->table_type());
1192
#endif
1193
    }
1194
    
1195
    if (share->table_charset &&
1196 1197
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
1198
    {
1199 1200 1201 1202 1203 1204
      /*
        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))
1205
      {
1206 1207 1208 1209 1210 1211 1212
        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);
        }
1213
      }
1214
    }
1215

1216
    if (share->min_rows)
1217
    {
1218
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
1219
      end= longlong10_to_str(share->min_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1220
      packet->append(buff, (uint) (end- buff));
1221
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1222

1223
    if (share->max_rows && !table_list->schema_table)
1224
    {
1225
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
1226
      end= longlong10_to_str(share->max_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1227
      packet->append(buff, (uint) (end - buff));
1228
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1229

1230
    if (share->avg_row_length)
1231
    {
1232
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
1233
      end= longlong10_to_str(share->avg_row_length, buff,10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1234
      packet->append(buff, (uint) (end - buff));
1235
    }
1236

1237
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
1238
      packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
1239
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
1240
      packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
1241
    if (share->db_create_options & HA_OPTION_CHECKSUM)
1242
      packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
1243
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1244
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
1245
    if (share->row_type != ROW_TYPE_DEFAULT)
1246
    {
1247
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
1248
      packet->append(ha_row_type[(uint) share->row_type]);
1249 1250
    }
    table->file->append_create_info(packet);
1251
    if (share->comment && share->comment[0])
1252
    {
1253
      packet->append(STRING_WITH_LEN(" COMMENT="));
1254
      append_unescaped(packet, share->comment, strlen(share->comment));
1255
    }
1256 1257
    if (share->connect_string.length)
    {
1258
      packet->append(STRING_WITH_LEN(" CONNECTION="));
1259 1260
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1261 1262
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1263
  }
1264
#ifdef WITH_PARTITION_STORAGE_ENGINE
1265 1266 1267 1268 1269 1270
  {
    /*
      Partition syntax for CREATE TABLE is at the end of the syntax.
    */
    uint part_syntax_len;
    char *part_syntax;
1271 1272
    if (table->part_info &&
        ((part_syntax= generate_partition_syntax(table->part_info,
1273 1274
                                                  &part_syntax_len,
                                                  FALSE,FALSE))))
1275 1276 1277 1278 1279 1280
    {
       packet->append(part_syntax, part_syntax_len);
       my_free(part_syntax, MYF(0));
    }
  }
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1281 1282 1283
  DBUG_RETURN(0);
}

1284 1285 1286
void
view_store_options(THD *thd, TABLE_LIST *table, String *buff)
{
1287
  buff->append(STRING_WITH_LEN("ALGORITHM="));
1288 1289
  switch ((int8)table->algorithm) {
  case VIEW_ALGORITHM_UNDEFINED:
1290
    buff->append(STRING_WITH_LEN("UNDEFINED "));
1291 1292
    break;
  case VIEW_ALGORITHM_TMPTABLE:
1293
    buff->append(STRING_WITH_LEN("TEMPTABLE "));
1294 1295
    break;
  case VIEW_ALGORITHM_MERGE:
1296
    buff->append(STRING_WITH_LEN("MERGE "));
1297 1298 1299 1300
    break;
  default:
    DBUG_ASSERT(0); // never should happen
  }
1301
  append_definer(thd, buff, &table->definer.user, &table->definer.host);
1302
  if (table->view_suid)
1303
    buff->append(STRING_WITH_LEN("SQL SECURITY DEFINER "));
1304
  else
1305
    buff->append(STRING_WITH_LEN("SQL SECURITY INVOKER "));
1306
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
/*
  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(' ');
}


1330
int
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1331 1332 1333 1334 1335 1336 1337 1338
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;
1339 1340 1341 1342 1343
  /*
     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
  */
1344
  if (!thd->db || strcmp(thd->db, table->view_db.str))
1345 1346 1347 1348
    table->compact_view_format= FALSE;
  else
  {
    TABLE_LIST *tbl;
1349
    table->compact_view_format= TRUE;
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    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;
      }
    }
  }

1362
  buff->append(STRING_WITH_LEN("CREATE "));
1363
  if (!foreign_db_mode)
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1364
  {
1365
    view_store_options(thd, table, buff);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1366
  }
1367
  buff->append(STRING_WITH_LEN("VIEW "));
1368 1369 1370 1371 1372
  if (!table->compact_view_format)
  {
    append_identifier(thd, buff, table->view_db.str, table->view_db.length);
    buff->append('.');
  }
1373
  append_identifier(thd, buff, table->view_name.str, table->view_name.length);
1374
  buff->append(STRING_WITH_LEN(" AS "));
1375 1376 1377 1378 1379 1380 1381

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

1382 1383 1384
  if (table->with_check != VIEW_CHECK_NONE)
  {
    if (table->with_check == VIEW_CHECK_LOCAL)
1385
      buff->append(STRING_WITH_LEN(" WITH LOCAL CHECK OPTION"));
1386
    else
1387
      buff->append(STRING_WITH_LEN(" WITH CASCADED CHECK OPTION"));
1388
  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1389 1390 1391 1392
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1393
/****************************************************************************
1394 1395
  Return info about all processes
  returns for each thread: thread id, user, host, db, command, info
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1396 1397 1398 1399
****************************************************************************/

class thread_info :public ilink {
public:
1400 1401 1402 1403
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1404
  static void operator delete(void *ptr __attribute__((unused)),
1405 1406
                              size_t size __attribute__((unused)))
  { TRASH(ptr, size); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1407

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1408 1409
  ulong thread_id;
  time_t start_time;
1410
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1411 1412 1413 1414
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

1415
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1416 1417 1418 1419 1420 1421 1422 1423
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;
1424 1425
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1426
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1427 1428
  DBUG_ENTER("mysqld_list_processes");

1429
  field_list.push_back(new Item_int("Id",0,11));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1430
  field_list.push_back(new Item_empty_string("User",16));
1431
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1432 1433 1434
  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));
1435
  field_list.push_back(new Item_return_int("Time",7, FIELD_TYPE_LONG));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1436 1437 1438 1439
  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;
1440 1441
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1442 1443 1444 1445 1446 1447 1448 1449 1450
    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++))
    {
1451
      Security_context *tmp_sctx= tmp->security_ctx;
1452
      struct st_my_thread_var *mysys_var;
1453
      if ((tmp->vio_ok() || tmp->system_thread) &&
1454
          (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1455
      {
1456
        thread_info *thd_info= new thread_info;
1457 1458

        thd_info->thread_id=tmp->thread_id;
1459 1460 1461 1462 1463
        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])
1464 1465
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1466
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1467
			"%s:%u", tmp_sctx->host_or_ip, tmp->peer_port);
1468 1469
	}
	else
1470
	  thd_info->host= thd->strdup(tmp_sctx->host_or_ip);
1471 1472 1473
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1474 1475
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1476
        thd_info->proc_info= (char*) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1477
#ifndef EMBEDDED_LIBRARY
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
        thd_info->state_info= (char*) (tmp->locked ? "Locked" :
                                       tmp->net.reading_or_writing ?
                                       (tmp->net.reading_or_writing == 2 ?
                                        "Writing to net" :
                                        thd_info->command == COM_SLEEP ? "" :
                                        "Reading from net") :
                                       tmp->proc_info ? tmp->proc_info :
                                       tmp->mysys_var &&
                                       tmp->mysys_var->current_cond ?
                                       "Waiting on cond" : NullS);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1488 1489 1490
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1491 1492
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1493 1494

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1495 1496
        if (pthread_kill(tmp->real_id,0))
          tmp->proc_info="*** DEAD ***";        // This shouldn't happen
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1497
#endif
1498 1499 1500
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1501
        thd_info->start_time= tmp->start_time;
1502
#endif
1503 1504 1505
        thd_info->query=0;
        if (tmp->query)
        {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1506 1507 1508 1509 1510
	  /* 
            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
          */
1511
          uint length= min(max_query_length, tmp->query_length);
1512
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1513 1514
        }
        thread_infos.append(thd_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1515 1516 1517 1518 1519 1520
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1521
  time_t now= time(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1522 1523
  while ((thd_info=thread_infos.get()))
  {
1524 1525
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1526 1527 1528
    protocol->store(thd_info->user, system_charset_info);
    protocol->store(thd_info->host, system_charset_info);
    protocol->store(thd_info->db, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1529
    if (thd_info->proc_info)
1530
      protocol->store(thd_info->proc_info, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1531
    else
1532
      protocol->store(command_name[thd_info->command].str, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1533
    if (thd_info->start_time)
1534
      protocol->store((uint32) (now - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1535
    else
1536
      protocol->store_null();
1537 1538
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1539
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1540 1541
      break; /* purecov: inspected */
  }
1542
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1543 1544 1545
  DBUG_VOID_RETURN;
}

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 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
int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
{
  TABLE *table= tables->table;
  CHARSET_INFO *cs= system_charset_info;
  char *user;
  bool verbose;
  ulong max_query_length;
  time_t now= time(0);
  DBUG_ENTER("fill_process_list");

  user= thd->security_ctx->master_access & PROCESS_ACL ?
        NullS : thd->security_ctx->priv_user;
  verbose= thd->lex->verbose;
  max_query_length= PROCESS_LIST_WIDTH;

  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,
                               min(max_query_length, tmp->query_length), cs);
        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);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1659
/*****************************************************************************
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1660
  Status functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1661 1662
*****************************************************************************/

1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
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)
{
1676
  uint a,b;
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 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
  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.

*/
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 *);
    int a= 0, b= all_status_vars.elements, c= (a+b)/2, res;

    for (; list->name; list++)
    {
      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;
        else break;
      }
      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 *);
    int i;
    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);
  }
}

1818
static bool show_status_array(THD *thd, const char *wild,
1819
                              SHOW_VAR *variables,
1820 1821 1822
                              enum enum_var_type value_type,
                              struct system_status_var *status_var,
                              const char *prefix, TABLE *table)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1823
{
1824
  char buff[SHOW_VAR_FUNC_BUFF_SIZE], *prefix_end;
1825 1826 1827
  /* the variable name should not be longer then 80 characters */
  char name_buffer[80];
  int len;
1828
  LEX_STRING null_lex_str;
1829
  SHOW_VAR tmp, *var;
1830
  DBUG_ENTER("show_status_array");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1831

1832
  null_lex_str.str= 0;				// For sys_var->value_ptr()
1833
  null_lex_str.length= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1834

1835
  prefix_end=strnmov(name_buffer, prefix, sizeof(name_buffer)-1);
1836 1837
  if (*prefix)
    *prefix_end++= '_';
1838 1839
  len=name_buffer + sizeof(name_buffer) - prefix_end;

1840
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1841
  {
1842 1843
    strnmov(prefix_end, variables->name, len);
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
1844

1845 1846 1847 1848
    /*
      if var->type is SHOW_FUNC, call the function.
      Repeat as necessary, if new var is again SHOW_FUNC
    */
1849
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
1850
      ((mysql_show_var_func)(var->value))(thd, &tmp, buff);
1851 1852 1853

    SHOW_TYPE show_type=var->type;
    if (show_type == SHOW_ARRAY)
1854
    {
1855
      show_status_array(thd, wild, (SHOW_VAR *) var->value,
1856
                        value_type, status_var, name_buffer, table);
1857 1858
    }
    else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1859
    {
1860 1861
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
1862
      {
1863
        char *value=var->value;
1864
        const char *pos, *end;                  // We assign a lot of const's
1865 1866 1867 1868 1869 1870 1871 1872 1873
        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;
1874 1875 1876 1877
        /*
          note that value may be == buff. All SHOW_xxx code below
          should still work in this case
        */
1878
        switch (show_type) {
1879 1880 1881 1882 1883 1884
        case SHOW_DOUBLE_STATUS:
        {
          value= ((char *) status_var + (ulong) value);
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
1885 1886 1887 1888
        case SHOW_LONG_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
1889
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
          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:
petr@mysql.com's avatar
petr@mysql.com committed
1908 1909 1910 1911 1912 1913
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
1914
        case SHOW_CHAR:
petr@mysql.com's avatar
petr@mysql.com committed
1915 1916 1917 1918 1919 1920
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
1921
        case SHOW_CHAR_PTR:
petr@mysql.com's avatar
petr@mysql.com committed
1922 1923 1924 1925 1926 1927
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
1928
        case SHOW_KEY_CACHE_LONG:
1929
          value= (char*) dflt_key_cache + (ulong)value;
1930 1931
          end= int10_to_str(*(long*) value, buff, 10);
          break;
1932
        case SHOW_KEY_CACHE_LONGLONG:
1933
          value= (char*) dflt_key_cache + (ulong)value;
1934 1935
	  end= longlong10_to_str(*(longlong*) value, buff, 10);
	  break;
1936
        case SHOW_UNDEF:
1937 1938
          break;                                        // Return empty string
        case SHOW_SYS:                                  // Cannot happen
1939
        default:
1940
          DBUG_ASSERT(0);
1941 1942
          break;
        }
1943
        restore_record(table, s->default_values);
1944 1945 1946
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
1947 1948
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1949 1950 1951
      }
    }
  }
1952 1953 1954 1955 1956

  DBUG_RETURN(FALSE);
}


1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
/* 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;
}


1981 1982 1983 1984 1985 1986
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)
1987 1988
    if (!(lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING))))
      return 0;
1989 1990 1991 1992 1993 1994 1995 1996
  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};
1997 1998

/* This is only used internally, but we need it here as a forward reference */
1999 2000 2001 2002 2003 2004 2005 2006
extern ST_SCHEMA_TABLE schema_tables[];

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


2007 2008 2009 2010 2011 2012 2013
/*
  Store record to I_S table, convert HEAP table
  to MyISAM if necessary

  SYNOPSIS
    schema_table_store_record()
    thd                   thread handler
2014 2015
    table                 Information schema table to be updated

2016 2017
  RETURN
    0	                  success
2018
    1	                  error
2019 2020
*/

2021
bool schema_table_store_record(THD *thd, TABLE *table)
2022 2023
{
  int error;
2024
  if ((error= table->file->ha_write_row(table->record[0])))
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
  {
    if (create_myisam_from_heap(thd, table, 
                                table->pos_in_table_list->schema_table_param,
                                error, 0))
      return 1;
  }
  return 0;
}


2035 2036 2037 2038 2039 2040 2041 2042 2043
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:
2044
  case SQLCOM_SHOW_TRIGGERS:
2045
  case SQLCOM_SHOW_EVENTS:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2046
    index_field_values->db_value= lex->select_lex.db;
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    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();
monty@mishka.local's avatar
monty@mishka.local committed
2068
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
                             (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++)
2083
    {
2084 2085
      if (!uses_only_table_name_fields(*child, table))
        return 0;
2086
    }
2087 2088 2089 2090 2091 2092 2093
  }
  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;
2094 2095
    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 : "";
2096 2097 2098 2099 2100 2101 2102
    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)))
2103 2104
      return 0;
  }
2105 2106
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
2107 2108 2109 2110
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

2111
  return 1;
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
}


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


2171 2172 2173 2174 2175 2176
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
{
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
}


2177
/*
2178
  Create db names list. Information schema name always is first in list
2179 2180

  SYNOPSIS
2181
    make_db_list()
2182 2183 2184
    thd                   thread handler
    files                 list of db names
    wild                  wild string
2185 2186
    idx_field_vals        idx_field_vals->db_name contains db name or
                          wild string
2187 2188
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0
2189 2190
    is_wild_value         if value is 1 then idx_field_vals->db_name is
                          wild string otherwise it's db name; 
2191 2192 2193 2194 2195 2196

  RETURN
    1	                  error
    0	                  success
*/

2197 2198 2199
int make_db_list(THD *thd, List<char> *files,
                 INDEX_FIELD_VALUES *idx_field_vals,
                 bool *with_i_schema, bool is_wild_value)
2200
{
2201
  LEX *lex= thd->lex;
2202
  *with_i_schema= 0;
2203 2204
  get_index_field_values(lex, idx_field_vals);
  if (is_wild_value)
2205
  {
2206 2207 2208 2209 2210
    /*
      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)
    */
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
    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);
2222
  }
2223

2224 2225 2226 2227 2228 2229
  /*
    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.
  */
2230 2231
  if (lex->orig_sql_command != SQLCOM_END)
  {
2232
    if (!my_strcasecmp(system_charset_info, information_schema_name.str,
2233 2234 2235 2236 2237 2238 2239 2240
                       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));
  }

2241 2242 2243 2244
  /*
    Create list of existing databases. It is used in case
    of select from information schema table
  */
2245 2246 2247 2248
  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);
2249 2250 2251 2252 2253 2254
}


int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2255
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2256
  {
2257 2258
    if (tmp_schema_table->hidden)
      continue;
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
    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;
}


2278 2279 2280 2281 2282
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;
2283
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2284 2285
  TABLE_LIST **save_query_tables_last= lex->query_tables_last;
  enum_sql_command save_sql_command= lex->sql_command;
2286
  SELECT_LEX *lsel= tables->schema_select_lex;
2287
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2288 2289 2290 2291 2292 2293 2294
  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;
monty@mysql.com's avatar
monty@mysql.com committed
2295
  List_iterator_fast<char> it(bases);
2296
  COND *partial_cond;
2297
  Security_context *sctx= thd->security_ctx;
2298
  uint derived_tables= lex->derived_tables; 
monty@mysql.com's avatar
monty@mysql.com committed
2299
  int error= 1;
2300
  enum legacy_db_type not_used;
2301
  Open_tables_state open_tables_state_backup;
2302 2303
  bool save_view_prepare_mode= lex->view_prepare_mode;
  lex->view_prepare_mode= TRUE;
2304 2305 2306 2307
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
2308

2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
  /*
    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);

2322 2323 2324
  if (lsel)
  {
    TABLE_LIST *show_table_list= (TABLE_LIST*) lsel->table_list.first;
2325 2326
    bool res;

2327
    lex->all_selects_list= lsel;
2328 2329 2330 2331 2332 2333 2334
    /*
      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;
2335 2336
    res= open_normal_and_derived_tables(thd, show_table_list,
                                        MYSQL_LOCK_IGNORE_FLUSH);
2337 2338 2339
    /*
      get_all_tables() returns 1 on failure and 0 on success thus
      return only these and not the result code of ::process_table()
2340 2341 2342 2343 2344 2345 2346

      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).
2347 2348
    */
    error= test(schema_table->process_table(thd, show_table_list,
2349 2350 2351 2352 2353
                                            table, res, 
                                            (show_table_list->view ?
                                             show_table_list->view_db.str :
                                             show_table_list->db),
                                            show_table_list->alias));
2354
    thd->temporary_tables= 0;
2355
    close_thread_tables(thd);
2356
    show_table_list->table= 0;
monty@mysql.com's avatar
monty@mysql.com committed
2357
    goto err;
2358 2359
  }

2360
  schema_table_idx= get_schema_table_idx(schema_table);
2361

2362 2363
  if (make_db_list(thd, &bases, &idx_field_vals,
                   &with_i_schema, 0))
monty@mysql.com's avatar
monty@mysql.com committed
2364
    goto err;
2365 2366

  partial_cond= make_cond_for_info_schema(cond, tables);
monty@mysql.com's avatar
monty@mysql.com committed
2367
  it.rewind(); /* To get access to new elements in basis list */
2368
  while ((base_name= it++) ||
2369 2370 2371 2372 2373 2374 2375 2376 2377
	 /*
	   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
2378 2379
    if (!check_access(thd,SELECT_ACL, base_name, 
                      &thd->col_access, 0, 1, with_i_schema) ||
2380 2381
        sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, base_name,0) ||
2382 2383 2384 2385
	(grant_option && !check_grant_db(thd, base_name)))
#endif
    {
      List<char> files;
2386 2387 2388
      if (with_i_schema)                      // information schema table names
      {
        if (schema_tables_add(thd, &files, idx_field_vals.table_value))
monty@mysql.com's avatar
monty@mysql.com committed
2389
          goto err;
2390 2391 2392
      }
      else
      {
2393 2394
        len= build_table_filename(path, sizeof(path), base_name, "", "");
        end= path + len;
2395 2396 2397
        len= FN_LEN - len;
        if (mysql_find_files(thd, &files, base_name, 
                             path, idx_field_vals.table_value, 0))
monty@mysql.com's avatar
monty@mysql.com committed
2398
          goto err;
2399
      }
2400

2401 2402
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
2403
      {
2404
	restore_record(table, s->default_values);
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
        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)
            {
2415
              if (with_i_schema)
2416
              {
2417 2418
                table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
                                       system_charset_info);
2419 2420 2421 2422
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
2423
                switch (mysql_frm_type(thd, path, &not_used)) {
2424
                case FRMTYPE_ERROR:
2425 2426
                  table->field[3]->store(STRING_WITH_LEN("ERROR"),
                                         system_charset_info);
2427 2428
                  break;
                case FRMTYPE_TABLE:
2429 2430
                  table->field[3]->store(STRING_WITH_LEN("BASE TABLE"),
                                         system_charset_info);
2431 2432
                  break;
                case FRMTYPE_VIEW:
2433 2434
                  table->field[3]->store(STRING_WITH_LEN("VIEW"),
                                         system_charset_info);
2435 2436 2437 2438
                  break;
                default:
                  DBUG_ASSERT(0);
                }
2439 2440
              }
            }
2441
            if (schema_table_store_record(thd, table))
2442
              goto err;
2443 2444 2445 2446
          }
          else
          {
            int res;
2447 2448 2449 2450 2451
            /*
              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;
2452
            if (make_table_list(thd, &sel, base_name, file_name))
monty@mysql.com's avatar
monty@mysql.com committed
2453
              goto err;
2454
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
2455 2456
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
2457 2458
            res= open_normal_and_derived_tables(thd, show_table_list,
                                                MYSQL_LOCK_IGNORE_FLUSH);
2459 2460 2461 2462 2463 2464 2465
            /*
              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.
            */
2466
            res= schema_table->process_table(thd, show_table_list, table,
2467
                                            res, base_name,
2468
                                            show_table_list->alias);
2469
            close_thread_tables(thd);
2470
            if (res)
monty@mysql.com's avatar
monty@mysql.com committed
2471
              goto err;
2472 2473 2474
          }
        }
      }
2475 2476 2477 2478
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
2479
      with_i_schema= 0;
2480 2481
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
2482 2483 2484

  error= 0;
err:
2485
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2486 2487
  lex->derived_tables= derived_tables;
  lex->all_selects_list= old_all_select_lex;
2488
  lex->query_tables_last= save_query_tables_last;
2489
  lex->view_prepare_mode= save_view_prepare_mode;
2490 2491
  *save_query_tables_last= 0;
  lex->sql_command= save_sql_command;
monty@mysql.com's avatar
monty@mysql.com committed
2492
  DBUG_RETURN(error);
2493 2494 2495
}


2496
bool store_schema_shemata(THD* thd, TABLE *table, const char *db_name,
2497
                          CHARSET_INFO *cs)
2498
{
2499
  restore_record(table, s->default_values);
2500
  table->field[1]->store(db_name, strlen(db_name), system_charset_info);
2501 2502
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2503
  return schema_table_store_record(thd, table);
2504 2505 2506
}


2507 2508
int fill_schema_shemata(THD *thd, TABLE_LIST *tables, COND *cond)
{
2509
  char path[FN_REFLEN];
2510 2511 2512 2513 2514
  bool found_libchar;
  INDEX_FIELD_VALUES idx_field_vals;
  List<char> files;
  char *file_name;
  uint length;
2515
  bool with_i_schema;
2516 2517
  HA_CREATE_INFO create;
  TABLE *table= tables->table;
2518
  Security_context *sctx= thd->security_ctx;
2519
  DBUG_ENTER("fill_schema_shemata");
2520

2521 2522
  if (make_db_list(thd, &files, &idx_field_vals,
                   &with_i_schema, 1))
2523
    DBUG_RETURN(1);
2524

2525 2526 2527
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2528 2529
    if (with_i_schema)       // information schema name is always first in list
    {
2530
      if (store_schema_shemata(thd, table, file_name,
2531
                               system_charset_info))
2532
        DBUG_RETURN(1);
2533 2534 2535
      with_i_schema= 0;
      continue;
    }
2536
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2537 2538
    if (sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, file_name,0) ||
2539 2540 2541
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2542
      length= build_table_filename(path, sizeof(path), file_name, "", "");
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
      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);
2554
      if (store_schema_shemata(thd, table, file_name, 
2555
                               create.default_table_charset))
2556
        DBUG_RETURN(1);
2557 2558
    }
  }
2559
  DBUG_RETURN(0);
2560 2561 2562
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2563 2564 2565 2566
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)
2567 2568 2569 2570 2571
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2572 2573

  restore_record(table, s->default_values);
2574 2575
  table->field[1]->store(base_name, strlen(base_name), cs);
  table->field[2]->store(file_name, strlen(file_name), cs);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2576 2577 2578 2579 2580 2581
  if (res)
  {
    /*
      there was errors during opening tables
    */
    const char *error= thd->net.last_error;
2582 2583 2584 2585 2586 2587
    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);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2588 2589 2590 2591
    table->field[20]->store(error, strlen(error), cs);
    thd->clear_error();
  }
  else if (tables->view)
2592
  {
2593 2594
    table->field[3]->store(STRING_WITH_LEN("VIEW"), cs);
    table->field[20]->store(STRING_WITH_LEN("VIEW"), cs);
2595 2596 2597 2598
  }
  else
  {
    TABLE *show_table= tables->table;
2599
    TABLE_SHARE *share= show_table->s;
2600
    handler *file= show_table->file;
2601

2602 2603
    file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_AUTO |
               HA_STATUS_NO_LOCK);
2604
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2605
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2606
    else if (share->tmp_table)
2607
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
2608
    else
2609
      table->field[3]->store(STRING_WITH_LEN("BASE TABLE"), cs);
2610

2611 2612
    for (int i= 4; i < 20; i++)
    {
2613
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2614 2615 2616 2617 2618
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2619
    table->field[5]->store((longlong) share->frm_version, TRUE);
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
    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;
    }
2645
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2646 2647
    if (!tables->schema_table)
    {
2648
      table->field[7]->store((longlong) file->records, TRUE);
2649 2650
      table->field[7]->set_notnull();
    }
2651 2652
    table->field[8]->store((longlong) file->mean_rec_length, TRUE);
    table->field[9]->store((longlong) file->data_file_length, TRUE);
2653 2654
    if (file->max_data_file_length)
    {
2655
      table->field[10]->store((longlong) file->max_data_file_length, TRUE);
2656
    }
2657 2658
    table->field[11]->store((longlong) file->index_file_length, TRUE);
    table->field[12]->store((longlong) file->delete_length, TRUE);
2659
    if (show_table->found_next_number_field)
2660
    {
2661
      table->field[13]->store((longlong) file->auto_increment_value, TRUE);
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
      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();
    }
2684 2685
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2686
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
2687
    if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
2688
    {
2689
      table->field[18]->store((longlong) file->checksum(), TRUE);
2690 2691 2692 2693 2694
      table->field[18]->set_notnull();
    }

    char option_buff[350],*ptr;
    ptr=option_buff;
2695
    if (share->min_rows)
2696 2697
    {
      ptr=strmov(ptr," min_rows=");
2698
      ptr=longlong10_to_str(share->min_rows,ptr,10);
2699
    }
2700
    if (share->max_rows)
2701 2702
    {
      ptr=strmov(ptr," max_rows=");
2703
      ptr=longlong10_to_str(share->max_rows,ptr,10);
2704
    }
2705
    if (share->avg_row_length)
2706 2707
    {
      ptr=strmov(ptr," avg_row_length=");
2708
      ptr=longlong10_to_str(share->avg_row_length,ptr,10);
2709
    }
2710
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2711
      ptr=strmov(ptr," pack_keys=1");
2712
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2713
      ptr=strmov(ptr," pack_keys=0");
2714
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2715
      ptr=strmov(ptr," checksum=1");
2716
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2717
      ptr=strmov(ptr," delay_key_write=1");
2718
    if (share->row_type != ROW_TYPE_DEFAULT)
2719
      ptr=strxmov(ptr, " row_format=", 
2720
                  ha_row_type[(uint) share->row_type],
2721 2722 2723 2724
                  NullS);
    table->field[19]->store(option_buff+1,
                            (ptr == option_buff ? 0 : 
                             (uint) (ptr-option_buff)-1), cs);
2725
    {
2726 2727
      char *comment;
      comment= show_table->file->update_table_comment(share->comment);
2728 2729 2730
      if (comment)
      {
        table->field[20]->store(comment, strlen(comment), cs);
2731 2732
        if (comment != share->comment)
          my_free(comment, MYF(0));
2733 2734
      }
    }
2735
  }
2736
  DBUG_RETURN(schema_table_store_record(thd, table));
2737 2738 2739
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2740 2741 2742 2743
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)
2744
{
2745 2746
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2747
  CHARSET_INFO *cs= system_charset_info;
2748 2749 2750 2751 2752
  TABLE *show_table;
  handler *file;
  Field **ptr,*field;
  int count;
  uint base_name_length, file_name_length;
2753
  DBUG_ENTER("get_schema_column_record");
2754

2755 2756
  if (res)
  {
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
    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);
2769 2770
  }

2771 2772 2773
  show_table= tables->table;
  file= show_table->file;
  count= 0;
2774
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2775
  restore_record(show_table, s->default_values);
2776 2777 2778
  base_name_length= strlen(base_name);
  file_name_length= strlen(file_name);

2779 2780
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
    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);
2799 2800

#ifndef NO_EMBEDDED_ACCESS_CHECKS
2801 2802
    uint col_access;
    check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
2803
                 &tables->grant.privilege, 0, 0, test(tables->schema_table));
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
    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)
2814
      {
2815 2816
        *end++=',';
        end=strmov(end,grant_types.type_names[bitnr]);
2817
      }
2818
    }
2819
    table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);
2820

2821
#endif
2822 2823 2824 2825
    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);
2826
    table->field[4]->store((longlong) count, TRUE);
2827 2828 2829 2830 2831 2832 2833 2834 2835
    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)
    {
2836
      table->field[5]->store(STRING_WITH_LEN("CURRENT_TIMESTAMP"), cs);
2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
      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);
2866 2867 2868
    if (field->has_charset() || is_blob ||
        field->real_type() == MYSQL_TYPE_VARCHAR ||  // For varbinary type
        field->real_type() == MYSQL_TYPE_STRING)     // For binary type
2869
    {
bar@mysql.com's avatar
bar@mysql.com committed
2870 2871 2872
      uint32 octet_max_length= field->max_length();
      if (octet_max_length != (uint32) 4294967295U)
        octet_max_length /= field->charset()->mbmaxlen;
2873
      longlong char_max_len= is_blob ? 
bar@mysql.com's avatar
bar@mysql.com committed
2874 2875
        (longlong) octet_max_length / field->charset()->mbminlen :
        (longlong) octet_max_length / field->charset()->mbmaxlen;
2876
      table->field[8]->store(char_max_len, TRUE);
2877
      table->field[8]->set_notnull();
bar@mysql.com's avatar
bar@mysql.com committed
2878
      table->field[9]->store((longlong) octet_max_length, TRUE);
2879 2880
      table->field[9]->set_notnull();
    }
2881

2882 2883 2884 2885
    /*
      Calculate field_length and decimals.
      They are set to -1 if they should not be set (we should return NULL)
    */
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
2886

2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
    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)
    {
2919
      table->field[10]->store((longlong) field_length, TRUE);
2920
      table->field[10]->set_notnull();
2921
    }
2922 2923
    if (decimals >= 0)
    {
2924
      table->field[11]->store((longlong) decimals, TRUE);
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
      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);
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
  }
  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;
2965 2966

  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
2967 2968 2969 2970
  {
    CHARSET_INFO *tmp_cs= cs[0];
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
        (tmp_cs->state & MY_CS_AVAILABLE) &&
bar@mysql.com's avatar
bar@mysql.com committed
2971
        !(tmp_cs->state & MY_CS_HIDDEN) &&
2972 2973 2974
        !(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cs->csname,wild)))
    {
2975
      const char *comment;
2976
      restore_record(table, s->default_values);
2977
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
2978
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
2979 2980
      comment= tmp_cs->comment ? tmp_cs->comment : "";
      table->field[2]->store(comment, strlen(comment), scs);
2981
      table->field[3]->store((longlong) tmp_cs->mbmaxlen, TRUE);
2982 2983
      if (schema_table_store_record(thd, table))
        return 1;
2984 2985 2986 2987 2988 2989
    }
  }
  return 0;
}


2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
int fill_schema_engines(THD *thd, TABLE_LIST *tables, COND *cond)
{
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
  handlerton **types;

  DBUG_ENTER("fill_schema_engines");

  for (types= sys_table_types; *types; types++)
  {
    if ((*types)->flags & HTON_HIDDEN)
      continue;

    if (!(wild && wild[0] &&
          wild_case_compare(scs, (*types)->name,wild)))
    {
      const char *tmp;
      restore_record(table, s->default_values);

      table->field[0]->store((*types)->name, strlen((*types)->name), scs);
      tmp= (*types)->state ? "DISABLED" : "ENABLED";
      table->field[1]->store( tmp, strlen(tmp), scs);
      table->field[2]->store((*types)->comment, strlen((*types)->comment), scs);
      tmp= (*types)->commit ? "YES" : "NO";
      table->field[3]->store( tmp, strlen(tmp), scs);
      tmp= (*types)->prepare ? "YES" : "NO";
      table->field[4]->store( tmp, strlen(tmp), scs);
      tmp= (*types)->savepoint_set ? "YES" : "NO";
      table->field[5]->store( tmp, strlen(tmp), scs);

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

  DBUG_RETURN(0);
}


3030 3031 3032 3033 3034 3035
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;
3036
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3037 3038 3039 3040
  {
    CHARSET_INFO **cl;
    CHARSET_INFO *tmp_cs= cs[0];
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) || 
bar@mysql.com's avatar
bar@mysql.com committed
3041
         (tmp_cs->state & MY_CS_HIDDEN) ||
3042 3043
        !(tmp_cs->state & MY_CS_PRIMARY))
      continue;
3044
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3045 3046 3047 3048 3049 3050 3051 3052 3053
    {
      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;
3054
	restore_record(table, s->default_values);
3055 3056
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3057
        table->field[2]->store((longlong) tmp_cl->number, TRUE);
3058 3059 3060 3061
        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);
3062
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply, TRUE);
3063 3064
        if (schema_table_store_record(thd, table))
          return 1;
3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
      }
    }
  }
  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;
3077
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3078 3079 3080 3081 3082 3083
  {
    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;
3084
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3085 3086 3087 3088 3089
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
3090
      restore_record(table, s->default_values);
3091 3092
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3093 3094
      if (schema_table_store_record(thd, table))
        return 1;
3095 3096 3097 3098 3099 3100
    }
  }
  return 0;
}


3101
bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table,
3102
                       const char *wild, bool full_access, const char *sp_user)
3103 3104 3105 3106 3107
{
  String tmp_string;
  TIME time;
  LEX *lex= thd->lex;
  CHARSET_INFO *cs= system_charset_info;
3108 3109 3110 3111 3112 3113
  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);
3114 3115
  if (!full_access && check_some_routine_access(thd, sp_db, sp_name,
			proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE))
3116
    return 0;
3117

3118 3119 3120 3121 3122 3123
  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)
  {
3124 3125
    restore_record(table, s->default_values);
    if (!wild || !wild[0] || !wild_compare(sp_name, wild, 0))
3126
    {
3127
      int enum_idx= proc_table->field[5]->val_int();
3128
      table->field[3]->store(sp_name, strlen(sp_name), cs);
3129 3130
      get_field(thd->mem_root, proc_table->field[3], &tmp_string);
      table->field[0]->store(tmp_string.ptr(), tmp_string.length(), cs);
3131
      table->field[2]->store(sp_db, strlen(sp_db), cs);
3132 3133
      get_field(thd->mem_root, proc_table->field[2], &tmp_string);
      table->field[4]->store(tmp_string.ptr(), tmp_string.length(), cs);
3134 3135 3136 3137 3138 3139
      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();
      }
3140 3141 3142 3143 3144
      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);
      }
3145 3146
      table->field[6]->store(STRING_WITH_LEN("SQL"), cs);
      table->field[10]->store(STRING_WITH_LEN("SQL"), cs);
3147
      get_field(thd->mem_root, proc_table->field[6], &tmp_string);
3148
      table->field[11]->store(tmp_string.ptr(), tmp_string.length(), cs);
3149 3150
      table->field[12]->store(sp_data_access_name[enum_idx].str, 
                              sp_data_access_name[enum_idx].length , cs);
3151 3152
      get_field(thd->mem_root, proc_table->field[7], &tmp_string);
      table->field[14]->store(tmp_string.ptr(), tmp_string.length(), cs);
3153 3154
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[12])->get_time(&time);
3155
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3156 3157
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[13])->get_time(&time);
3158
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3159
      get_field(thd->mem_root, proc_table->field[14], &tmp_string);
3160
      table->field[17]->store(tmp_string.ptr(), tmp_string.length(), cs);
3161
      get_field(thd->mem_root, proc_table->field[15], &tmp_string);
3162
      table->field[18]->store(tmp_string.ptr(), tmp_string.length(), cs);
3163
      table->field[19]->store(definer, strlen(definer), cs);
3164
      return schema_table_store_record(thd, table);
3165 3166
    }
  }
3167
  return 0;
3168 3169 3170 3171 3172 3173 3174 3175 3176
}


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;
3177
  TABLE *table= tables->table;
3178
  bool full_access;
3179
  char definer[USER_HOST_BUFF_SIZE];
3180
  Open_tables_state open_tables_state_backup;
3181 3182
  DBUG_ENTER("fill_schema_proc");

3183 3184
  strxmov(definer, thd->security_ctx->priv_user, "@",
          thd->security_ctx->priv_host, NullS);
3185
  /* We use this TABLE_LIST instance only for checking of privileges. */
3186 3187
  bzero((char*) &proc_tables,sizeof(proc_tables));
  proc_tables.db= (char*) "mysql";
3188
  proc_tables.db_length= 5;
3189
  proc_tables.table_name= proc_tables.alias= (char*) "proc";
3190
  proc_tables.table_name_length= 4;
3191
  proc_tables.lock_type= TL_READ;
3192
  full_access= !check_table_access(thd, SELECT_ACL, &proc_tables, 1);
3193
  if (!(proc_table= open_proc_table_for_read(thd, &open_tables_state_backup)))
3194 3195 3196
  {
    DBUG_RETURN(1);
  }
3197
  proc_table->file->ha_index_init(0, 1);
3198 3199 3200 3201 3202
  if ((res= proc_table->file->index_first(proc_table->record[0])))
  {
    res= (res == HA_ERR_END_OF_FILE) ? 0 : 1;
    goto err;
  }
3203 3204 3205 3206 3207
  if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
  {
    res= 1;
    goto err;
  }
3208
  while (!proc_table->file->index_next(proc_table->record[0]))
3209 3210 3211 3212 3213 3214 3215
  {
    if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
    {
      res= 1;
      goto err;
    }
  }
3216 3217 3218

err:
  proc_table->file->ha_index_end();
3219
  close_proc_table(thd, &open_tables_state_backup);
3220 3221 3222 3223
  DBUG_RETURN(res);
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3224 3225 3226 3227
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)
3228 3229 3230
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
3231 3232 3233 3234 3235 3236 3237
  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
3238
      */
3239 3240 3241 3242 3243 3244 3245 3246 3247
      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)
3248 3249 3250
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3251
    show_table->file->info(HA_STATUS_VARIABLE |
3252 3253
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3254
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3255 3256 3257 3258 3259
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
3260
        restore_record(table, s->default_values);
3261 3262
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
3263
        table->field[3]->store((longlong) ((key_info->flags &
3264
                                            HA_NOSAME) ? 0 : 1), TRUE);
3265 3266
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
3267
        table->field[6]->store((longlong) (j+1), TRUE);
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
        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]);
3283
          table->field[9]->store((longlong) records, TRUE);
3284 3285
          table->field[9]->set_notnull();
        }
3286 3287 3288
        if (!(key_info->flags & HA_FULLTEXT) &&
            (key_part->field &&
             key_part->length !=
3289 3290
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
3291
          table->field[10]->store((longlong) key_part->length /
bar@mysql.com's avatar
bar@mysql.com committed
3292
                                  key_part->field->charset()->mbmaxlen);
3293 3294 3295 3296 3297 3298 3299
          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);
3300
        if (!show_table->s->keys_in_use.is_set(i))
3301
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3302 3303 3304
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
3305 3306
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3307 3308 3309
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3310
  DBUG_RETURN(res);
3311 3312 3313
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3314 3315 3316 3317
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)
3318 3319 3320
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
3321
  char definer[USER_HOST_BUFF_SIZE];
3322
  uint definer_len;
3323

3324 3325 3326 3327 3328 3329 3330
  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);
3331

3332 3333 3334 3335
    if (tables->with_check != VIEW_CHECK_NONE)
    {
      if (tables->with_check == VIEW_CHECK_LOCAL)
        table->field[4]->store(STRING_WITH_LEN("LOCAL"), cs);
3336
      else
3337
        table->field[4]->store(STRING_WITH_LEN("CASCADED"), cs);
3338
    }
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
    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)
3356 3357 3358
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                   thd->net.last_errno, thd->net.last_error);
  }
3359 3360
  if (res) 
    thd->clear_error();
3361
  DBUG_RETURN(0);
3362 3363 3364
}


3365 3366 3367
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)
3368 3369
{
  CHARSET_INFO *cs= system_charset_info;
3370
  restore_record(table, s->default_values);
3371 3372 3373 3374 3375
  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);
3376
  return schema_table_store_record(thd, table);
3377 3378 3379
}


gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3380
static int get_schema_constraints_record(THD *thd, struct st_table_list *tables,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3381 3382 3383
					 TABLE *table, bool res,
					 const char *base_name,
					 const char *file_name)
3384
{
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3385
  DBUG_ENTER("get_schema_constraints_record");
3386 3387 3388 3389 3390 3391 3392 3393 3394
  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)
3395 3396 3397 3398
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3399
    uint primary_key= show_table->s->primary_key;
3400 3401 3402
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3403
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3404 3405
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3406 3407
        continue;

3408
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
3409 3410
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3411 3412
                              strlen(key_info->name),
                              STRING_WITH_LEN("PRIMARY KEY")))
3413 3414
          DBUG_RETURN(1);
      }
3415
      else if (key_info->flags & HA_NOSAME)
3416 3417
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3418 3419
                              strlen(key_info->name),
                              STRING_WITH_LEN("UNIQUE")))
3420 3421
          DBUG_RETURN(1);
      }
3422 3423 3424 3425 3426 3427 3428
    }

    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++))
    {
3429 3430 3431 3432 3433
      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);
3434 3435
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3436
  DBUG_RETURN(res);
3437 3438 3439
}


3440 3441 3442 3443
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,
3444
                          LEX_STRING *trigger_stmt,
3445 3446
                          ulong sql_mode,
                          LEX_STRING *definer_buffer)
3447 3448
{
  CHARSET_INFO *cs= system_charset_info;
3449 3450 3451
  byte *sql_mode_str;
  ulong sql_mode_len;

3452 3453 3454 3455 3456 3457 3458 3459
  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);
3460
  table->field[10]->store(STRING_WITH_LEN("ROW"), cs);
3461 3462
  table->field[11]->store(trg_action_time_type_names[timing].str,
                          trg_action_time_type_names[timing].length, cs);
3463 3464
  table->field[14]->store(STRING_WITH_LEN("OLD"), cs);
  table->field[15]->store(STRING_WITH_LEN("NEW"), cs);
3465 3466 3467 3468 3469

  sql_mode_str=
    sys_var_thd_sql_mode::symbolic_mode_representation(thd,
                                                       sql_mode,
                                                       &sql_mode_len);
reggie@fedora.(none)'s avatar
reggie@fedora.(none) committed
3470
  table->field[17]->store((const char*)sql_mode_str, sql_mode_len, cs);
3471
  table->field[18]->store((const char *)definer_buffer->str, definer_buffer->length, cs);
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503
  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;
3504
        ulong sql_mode;
3505
        char definer_holder[USER_HOST_BUFF_SIZE];
3506 3507
        LEX_STRING definer_buffer;
        definer_buffer.str= definer_holder;
3508 3509
        if (triggers->get_trigger_info(thd, (enum trg_event_type) event,
                                       (enum trg_action_time_type)timing,
3510
                                       &trigger_name, &trigger_stmt,
3511 3512
                                       &sql_mode,
                                       &definer_buffer))
3513
          continue;
3514

3515 3516
        if (store_trigger(thd, table, base_name, file_name, &trigger_name,
                         (enum trg_event_type) event,
3517
                         (enum trg_action_time_type) timing, &trigger_stmt,
3518 3519
                         sql_mode,
                         &definer_buffer))
3520 3521 3522 3523 3524 3525 3526 3527
          DBUG_RETURN(1);
      }
    }
  }
  DBUG_RETURN(0);
}


3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
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);
3538
  table->field[7]->store((longlong) idx, TRUE);
3539 3540 3541
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3542 3543 3544 3545 3546
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)
3547 3548
{
  DBUG_ENTER("get_schema_key_column_usage_record");
3549 3550 3551 3552 3553 3554 3555 3556 3557
  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)
3558 3559 3560 3561
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3562
    uint primary_key= show_table->s->primary_key;
3563 3564 3565
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3566
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3567 3568
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3569
        continue;
3570 3571 3572 3573 3574 3575 3576
      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++;
3577
          restore_record(table, s->default_values);
3578 3579 3580 3581 3582 3583
          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);
3584 3585
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
3586 3587 3588 3589 3590 3591 3592 3593 3594
        }
      }
    }

    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++))
    {
3595
      LEX_STRING *f_info;
3596
      LEX_STRING *r_info;
3597 3598 3599 3600 3601
      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++))
      {
3602
        r_info= it1++;
3603
        f_idx++;
3604
        restore_record(table, s->default_values);
3605 3606 3607 3608 3609
        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);
3610
        table->field[8]->store((longlong) f_idx, TRUE);
3611
        table->field[8]->set_notnull();
3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622
        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();
3623 3624
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3625 3626 3627
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3628
  DBUG_RETURN(res);
3629 3630 3631
}


3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 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
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;
3723
  handler *file;
3724
#ifdef WITH_PARTITION_STORAGE_ENGINE
3725
  partition_info *part_info;
3726
#endif
3727 3728 3729 3730
  DBUG_ENTER("get_schema_partitions_record");

  if (res)
  {
3731
    if (!tables->view)
3732 3733 3734 3735 3736
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
3737
  file= show_table->file;
3738
#ifdef WITH_PARTITION_STORAGE_ENGINE
3739
  part_info= show_table->part_info;
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796
  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();
    }

3797
    if (part_info->is_sub_partitioned())
3798 3799 3800 3801 3802 3803 3804 3805 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
    {
      /* 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);
3851 3852 3853 3854 3855 3856
        if (part_elem->has_null_value)
        {
          tmp_str.append("NULL");
          if (no_items > 0)
            tmp_str.append(",");
        }
3857 3858 3859 3860 3861 3862 3863 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
        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
3902
#endif
3903 3904 3905 3906 3907 3908 3909 3910 3911
  {
    store_schema_partitions_record(thd, table, 0, file, 0);
    if(schema_table_store_record(thd, table))
      DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 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
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;
}

3953
extern LEX_STRING interval_type_to_name[];
3954 3955 3956 3957 3958 3959 3960

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;
3961
  Event_timed et;    
3962 3963 3964 3965 3966 3967
  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))
  {
3968
    my_error(ER_CANNOT_LOAD_FROM_TABLE, MYF(0));
3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981
    DBUG_RETURN(1);
  }

  if (!(!wild || !wild[0] || !wild_compare(et.name.str, wild, 0)))
    DBUG_RETURN(0);
  
  //->field[0] is EVENT_CATALOG and is by default NULL
  
  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);

andrey@lmy004's avatar
andrey@lmy004 committed
3982
  /* [9] is SQL_MODE */
3983
  {
3984
    byte *sql_mode_str;
3985 3986 3987 3988 3989 3990 3991
    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);
  }
  
3992 3993
  if (et.expression)
  {
3994
    String show_str;
3995 3996
    //type
    sch_table->field[5]->store(STRING_WITH_LEN("RECURRING"), scs);
andrey@lmy004's avatar
andrey@lmy004 committed
3997
    /* execute_at */
3998
    sch_table->field[6]->set_null();
andrey@lmy004's avatar
andrey@lmy004 committed
3999
    /* interval_value */
4000
    //interval_type
4001 4002 4003
    if (event_reconstruct_interval_expression(&show_str, et.interval,
                                              et.expression))
      DBUG_RETURN(1);
4004

4005 4006 4007 4008
    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];
4009
    sch_table->field[8]->set_notnull();
4010
    sch_table->field[8]->store(ival->str, ival->length, scs);
4011 4012

    //starts & ends    
4013 4014
    sch_table->field[10]->set_notnull();
    sch_table->field[10]->store_time(&et.starts, MYSQL_TIMESTAMP_DATETIME);
4015 4016 4017 4018 4019 4020

    if (!et.ends_null)
    {
      sch_table->field[11]->set_notnull();
      sch_table->field[11]->store_time(&et.ends, MYSQL_TIMESTAMP_DATETIME);
    }
4021 4022 4023 4024 4025
  }
  else
  {
    //type
    sch_table->field[5]->store(STRING_WITH_LEN("ONE TIME"), scs);
4026

4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 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 4116 4117 4118 4119 4120 4121
    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)
    sch_table->field[16]->store_time(&et.last_executed,MYSQL_TIMESTAMP_DATETIME);
  else
    sch_table->field[16]->set_null();

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


4164 4165 4166 4167 4168 4169 4170
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;
4171 4172
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
            && thd->is_fatal_error)
4173 4174 4175 4176
    DBUG_RETURN(1);

  for (; open_list ; open_list=open_list->next)
  {
4177
    restore_record(table, s->default_values);
4178 4179
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
4180 4181
    table->field[2]->store((longlong) open_list->in_use, TRUE);
    table->field[3]->store((longlong) open_list->locked, TRUE);
4182 4183
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
4184 4185 4186 4187 4188 4189 4190 4191
  }
  DBUG_RETURN(0);
}


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
4192
  int res= 0;
4193 4194
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
4195
  pthread_mutex_lock(&LOCK_global_system_variables);
4196
  res= show_status_array(thd, wild, init_vars,
4197 4198
                         lex->option_type, 0, "", tables->table);
  pthread_mutex_unlock(&LOCK_global_system_variables);
4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
  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;
4210
  pthread_mutex_lock(&LOCK_status);
4211 4212
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
4213 4214 4215 4216
  res= show_status_array(thd, wild,
                         (SHOW_VAR *)all_status_vars.buffer,
                         OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ?
4217 4218
                          &tmp: &thd->status_var), "",tables->table);
  pthread_mutex_unlock(&LOCK_status);
4219 4220 4221 4222
  DBUG_RETURN(res);
}


4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238
/*
  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;
4239
  for (; schema_table->table_name; schema_table++)
4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268
  {
    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
*/

4269
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
4270 4271 4272 4273 4274
{
  int field_count= 0;
  Item *item;
  TABLE *table;
  List<Item> field_list;
4275
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
4276
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
4277
  CHARSET_INFO *cs= system_charset_info;
4278 4279
  DBUG_ENTER("create_schema_table");

4280
  for (; fields_info->field_name; fields_info++)
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297
  {
    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:
4298 4299
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
4300 4301 4302
      {
        DBUG_RETURN(0);
      }
4303
      item->max_length= fields_info->field_length * cs->mbmaxlen;
4304 4305
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
4306 4307 4308 4309 4310 4311 4312 4313 4314
      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();
4315
  tmp_table_param->table_charset= cs;
4316
  tmp_table_param->field_count= field_count;
4317
  tmp_table_param->schema_table= 1;
4318 4319 4320 4321 4322
  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),
4323
                                HA_POS_ERROR, table_list->alias)))
4324
    DBUG_RETURN(0);
4325
  table_list->schema_table_param= tmp_table_param;
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347
  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;
4348
  Name_resolution_context *context= &thd->lex->select_lex.context;
4349
  for (; field_info->field_name; field_info++)
4350 4351 4352
  {
    if (field_info->old_name)
    {
4353 4354
      Item_field *field= new Item_field(context,
                                        NullS, NullS, field_info->field_name);
4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
      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;
4374
  Name_resolution_context *context= &sel->context;
4375 4376 4377 4378 4379

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
4380 4381
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4382 4383 4384 4385 4386 4387
    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())
    {
4388
      buffer.append(STRING_WITH_LEN(" ("));
4389
      buffer.append(lex->wild->ptr());
4390
      buffer.append(')');
4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402
    }
    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;
4403
  Name_resolution_context *context= &lex->select_lex.context;
4404 4405 4406 4407 4408 4409 4410

  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())
  {
4411
    buffer.append(STRING_WITH_LEN(" ("));
4412
    buffer.append(lex->wild->ptr());
4413
    buffer.append(')');
4414
  }
4415 4416
  Item_field *field= new Item_field(context,
                                    NullS, NullS, field_info->field_name);
4417 4418 4419 4420 4421 4422 4423
  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];
4424
    field= new Item_field(context, NullS, NullS, field_info->field_name);
4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
    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)
{
4436 4437 4438
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4439 4440
  Name_resolution_context *context= &thd->lex->select_lex.context;

4441
  for (; *field_num >= 0; field_num++)
4442
  {
4443 4444 4445 4446 4447
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
4448 4449
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4450
    if (field)
4451
    {
4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462
      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;
}


4463 4464 4465 4466 4467
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;
4468 4469
  Name_resolution_context *context= &thd->lex->select_lex.context;

4470 4471 4472
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4473 4474
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
    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;
}


4488 4489 4490 4491 4492
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;
4493 4494
  Name_resolution_context *context= &thd->lex->select_lex.context;

4495 4496 4497
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4498 4499
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
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;
4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530
    }
  }
  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");
4531
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4532 4533 4534
  {
    DBUG_RETURN(1);
  }
4535
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4536
  table->grant.privilege= SELECT_ACL;
4537 4538 4539 4540 4541 4542 4543
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
4544 4545 4546 4547
  if (table_list->schema_table_name)
    table->alias_name_used= my_strcasecmp(table_alias_charset,
                                          table_list->schema_table_name,
                                          table_list->alias);
4548 4549
  table_list->table_name= table->s->table_name.str;
  table_list->table_name_length= table->s->table_name.length;
4550 4551 4552 4553
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4554
  lex->safe_to_cache_query= 0;
4555 4556 4557 4558 4559

  if (table_list->schema_table_reformed) // show command
  {
    SELECT_LEX *sel= lex->current_select;
    Item *item;
4560
    Field_translator *transl, *org_transl;
4561 4562 4563

    if (table_list->field_translation)
    {
4564
      Field_translator *end= table_list->field_translation_end;
4565 4566 4567
      for (transl= table_list->field_translation; transl < end; transl++)
      {
        if (!transl->item->fixed &&
4568
            transl->item->fix_fields(thd, &transl->item))
4569 4570 4571 4572 4573 4574
          DBUG_RETURN(1);
      }
      DBUG_RETURN(0);
    }
    List_iterator_fast<Item> it(sel->item_list);
    if (!(transl=
konstantin@mysql.com's avatar
konstantin@mysql.com committed
4575
          (Field_translator*)(thd->stmt_arena->
4576 4577 4578 4579 4580
                              alloc(sel->item_list.elements *
                                    sizeof(Field_translator)))))
    {
      DBUG_RETURN(1);
    }
4581
    for (org_transl= transl; (item= it++); transl++)
4582
    {
4583 4584 4585 4586
      transl->item= item;
      transl->name= item->name;
      if (!item->fixed && item->fix_fields(thd, &transl->item))
      {
4587
        DBUG_RETURN(1);
4588
      }
4589
    }
4590 4591
    table_list->field_translation= org_transl;
    table_list->field_translation_end= transl;
4592 4593
  }

4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617
  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");
4618
  DBUG_PRINT("enter", ("mysql_schema_select: %s", schema_table->table_name));
4619
  /*
4620 4621 4622 4623 4624 4625 4626
     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);
4627
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638
      !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);
}


/*
4639
  Fill temporary schema tables before SELECT
4640 4641 4642 4643 4644 4645

  SYNOPSIS
    get_schema_tables_result()
    join  join which use schema tables

  RETURN
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4646 4647
    FALSE success
    TRUE  error
4648 4649
*/

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4650
bool get_schema_tables_result(JOIN *join)
4651 4652 4653
{
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
  THD *thd= join->thd;
4654 4655
  LEX *lex= thd->lex;
  bool result= 0;
4656 4657 4658
  DBUG_ENTER("get_schema_tables_result");

  thd->no_warnings_for_error= 1;
4659 4660 4661 4662
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
  {  
    if (!tab->table || !tab->table->pos_in_table_list)
      break;
4663

4664
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4665
    if (table_list->schema_table && thd->fill_derived_tables())
4666
    {
4667 4668
      bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
                          lex->current_select->master_unit()->item);
4669 4670 4671 4672 4673 4674 4675 4676 4677
      /*
        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
4678 4679 4680 4681 4682 4683 4684 4685 4686
      {
        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;

4687 4688
      if (table_list->schema_table->fill_table(thd, table_list,
                                               tab->select_cond))
4689
        result= 1;
4690
      table_list->is_schema_table_processed= TRUE;
4691 4692
    }
  }
4693
  thd->no_warnings_for_error= 0;
4694
  DBUG_RETURN(result);
4695 4696
}

4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717
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;
4718
  DBUG_ENTER("fill_schema_files");
4719 4720 4721 4722 4723 4724 4725 4726 4727 4728

  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);
}
4729 4730 4731

ST_FIELD_INFO schema_fields_info[]=
{
4732 4733
  {"CATALOG_NAME", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SCHEMA_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
4734
  {"DEFAULT_CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4735
  {"DEFAULT_COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4736
  {"SQL_PATH", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
4737
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4738 4739 4740 4741 4742
};


ST_FIELD_INFO tables_fields_info[]=
{
4743 4744 4745 4746 4747 4748 4749
  {"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"},
4750
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
4751 4752 4753 4754 4755 4756 4757 4758 4759
  {"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"},
4760
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
4761 4762
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
4763
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4764
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4765 4766 4767 4768 4769
};


ST_FIELD_INFO columns_fields_info[]=
{
4770 4771 4772 4773 4774
  {"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},
4775 4776
  {"COLUMN_DEFAULT", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Default"},
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
4777
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
4778 4779
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4780 4781
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4782 4783
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
4784
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
4785
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
4786 4787
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
4788
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4789
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4790 4791 4792 4793 4794
};


ST_FIELD_INFO charsets_fields_info[]=
{
4795 4796
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
  {"DEFAULT_COLLATE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Default collation"},
4797
  {"DESCRIPTION", 60, MYSQL_TYPE_STRING, 0, 0, "Description"},
4798
  {"MAXLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Maxlen"},
4799
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4800 4801 4802 4803 4804
};


ST_FIELD_INFO collation_fields_info[]=
{
4805 4806
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
4807
  {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"},
4808 4809
  {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"},
  {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"},
4810
  {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"},
4811
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4812 4813 4814
};


4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826
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}
};


4827 4828 4829 4830 4831 4832 4833 4834 4835
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"},
4836
  {"INTERVAL_VALUE", 256, MYSQL_TYPE_STRING, 0, 1, "Interval value"},
4837
  {"INTERVAL_FIELD", 18, MYSQL_TYPE_STRING, 0, 1, "Interval field"},
4838
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851
  {"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}
};



4852 4853
ST_FIELD_INFO coll_charset_app_fields_info[]=
{
4854 4855
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4856
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4857 4858 4859 4860 4861
};


ST_FIELD_INFO proc_fields_info[]=
{
4862 4863 4864 4865 4866
  {"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"},
4867 4868
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4869
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4870 4871
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
4872
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4873 4874 4875 4876
  {"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"},
4877 4878
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
4879
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4880
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4881
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
4882
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4883 4884 4885 4886 4887
};


ST_FIELD_INFO stat_fields_info[]=
{
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
  {"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}
4904 4905 4906 4907 4908
};


ST_FIELD_INFO view_fields_info[]=
{
4909 4910 4911 4912
  {"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},
4913
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4914
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
4915 4916
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, 0},
4917
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4918 4919 4920 4921 4922
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
4923 4924 4925 4926 4927
  {"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}
4928 4929 4930 4931 4932
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
4933 4934 4935 4936 4937 4938
  {"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}
4939 4940 4941 4942 4943
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
4944 4945 4946 4947 4948 4949 4950
  {"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}
4951 4952 4953 4954 4955
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
4956 4957 4958 4959 4960 4961 4962 4963
  {"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}
4964 4965 4966 4967 4968
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
4969 4970 4971 4972 4973 4974 4975
  {"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}
4976 4977 4978 4979 4980
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
4981 4982 4983
  {"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},
4984
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
4985 4986 4987 4988
  {"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},
4989
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
4990 4991 4992
  {"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},
4993
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4994 4995 4996 4997 4998
};


ST_FIELD_INFO table_names_fields_info[]=
{
4999 5000 5001 5002 5003
  {"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}
5004 5005 5006
};


5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
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}
};


5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035
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"},
5036
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, "sql_mode"},
5037
  {"DEFINER", 65535, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5038 5039 5040 5041
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072
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}
};


5073 5074 5075 5076 5077 5078 5079 5080
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}
};


5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094
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"},
  {"TIME", 4, MYSQL_TYPE_LONG, 0, 0, "Time"},
  {"STATE", 30, MYSQL_TYPE_STRING, 0, 1, "State"},
  {"INFO", PROCESS_LIST_WIDTH, MYSQL_TYPE_STRING, 0, 1, "Info"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5095 5096 5097
ST_FIELD_INFO plugin_fields_info[]=
{
  {"PLUGIN_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
5098
  {"PLUGIN_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5099
  {"PLUGIN_STATUS", 10, MYSQL_TYPE_STRING, 0, 0, "Status"},
5100
  {"PLUGIN_TYPE", 80, MYSQL_TYPE_STRING, 0, 0, "Type"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
5101
  {"PLUGIN_TYPE_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5102
  {"PLUGIN_LIBRARY", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Library"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
5103
  {"PLUGIN_LIBRARY_VERSION", 20, MYSQL_TYPE_STRING, 0, 1, 0},
5104 5105 5106 5107 5108
  {"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}
};

5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150
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}
};
5151

5152 5153
/*
  Description of ST_FIELD_INFO in table.h
5154 5155 5156

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

5157 5158 5159 5160 5161
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
5162
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
5163
  {"COLLATIONS", collation_fields_info, create_schema_table, 
5164
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
5165
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
5166
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
5167 5168
  {"COLUMNS", columns_fields_info, create_schema_table, 
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
5169
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
5170
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
5171
  {"ENGINES", engines_fields_info, create_schema_table,
5172
   fill_schema_engines, make_old_format, 0, -1, -1, 0},
5173 5174
  {"EVENTS", events_fields_info, create_schema_table,
   fill_schema_events, make_old_format, 0, -1, -1, 0},
5175 5176
  {"FILES", files_fields_info, create_schema_table,
   fill_schema_files, 0, 0, -1, -1, 0},
5177
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
5178
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
5179 5180
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
5181 5182
  {"PARTITIONS", partitions_fields_info, create_schema_table,
   get_all_tables, 0, get_schema_partitions_record, 1, 2, 0},
5183 5184
  {"PLUGINS", plugin_fields_info, create_schema_table,
    fill_plugins, make_old_format, 0, -1, -1, 0},
5185 5186
  {"PROCESSLIST", processlist_fields_info, create_schema_table,
    fill_schema_processlist, make_old_format, 0, -1, -1, 0},
5187 5188 5189 5190 5191 5192 5193 5194
  {"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},
5195 5196
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
5197 5198 5199 5200 5201 5202 5203 5204
  {"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},
5205
  {"TRIGGERS", triggers_fields_info, create_schema_table,
5206
   get_all_tables, make_old_format, get_schema_triggers_record, 5, 6, 0},
5207 5208
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
5209 5210
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
5211 5212
  {"VIEWS", view_fields_info, create_schema_table, 
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
5213
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
5214 5215 5216
};


5217
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5218
template class List_iterator_fast<char>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5219 5220
template class List<char>;
#endif