sql_show.cc 146 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"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
28
#include <my_dir.h>
29

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
30

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

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

41
static bool schema_table_store_record(THD *thd, TABLE *table);
42

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

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

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

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

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

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

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

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

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

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
116 117 118 119 120

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

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

  switch (plug->type)
  {
  case MYSQL_UDF_PLUGIN:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
141
    table->field[3]->store(STRING_WITH_LEN("UDF"), cs);
142 143
    break;
  case MYSQL_STORAGE_ENGINE_PLUGIN:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
144
    table->field[3]->store(STRING_WITH_LEN("STORAGE"), cs);
145 146
    break;
  case MYSQL_FTPARSER_PLUGIN:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
147
    table->field[3]->store(STRING_WITH_LEN("FTPARSER"), cs);
148 149
    break;
  default:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
150
    table->field[3]->store(STRING_WITH_LEN("UNKNOWN"), cs);
151 152 153
    break;
  }

acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
154 155 156
  table->field[4]->store(version_buf,
        make_version_string(version_buf, sizeof(version_buf), 
                            *(uint *)plug->info), cs);
157 158 159 160 161 162

  if (plugin->plugin_dl)
  {
    table->field[5]->store(plugin->plugin_dl->dl.str, 
                           plugin->plugin_dl->dl.length, cs);
    table->field[5]->set_notnull();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
163 164 165 166 167
    table->field[6]->store(version_buf,
          make_version_string(version_buf, sizeof(version_buf), 
                              plugin->plugin_dl->version),
          cs);
    table->field[6]->set_notnull();
168 169
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
170
  {
171
    table->field[5]->set_null();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
172 173 174 175
    table->field[6]->set_null();
  }


176 177
  if (plug->author)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
178 179
    table->field[7]->store(plug->author, strlen(plug->author), cs);
    table->field[7]->set_notnull();
180 181
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
182
    table->field[7]->set_null();
183 184 185

  if (plug->descr)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
186 187
    table->field[8]->store(plug->descr, strlen(plug->descr), cs);
    table->field[8]->set_notnull();
188 189
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
190
    table->field[8]->set_null();
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

  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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/***************************************************************************
** 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);
}
240

241
/***************************************************************************
242
 List all privileges supported
243 244
***************************************************************************/

245 246 247 248
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
249 250
};

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

283
bool mysqld_show_privileges(THD *thd)
284 285
{
  List<Item> field_list;
286
  Protocol *protocol= thd->protocol;
287 288 289 290 291 292
  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));

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

297 298
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
299
  {
300
    protocol->prepare_for_resend();
301 302 303
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
304
    if (protocol->write())
305
      DBUG_RETURN(TRUE);
306
  }
307
  send_eof(thd);
308
  DBUG_RETURN(FALSE);
309 310 311 312
}


/***************************************************************************
313
  List all column types
314 315
***************************************************************************/

316 317
struct show_column_type_st
{
318 319
  const char *type;
  uint size;
320 321 322 323 324 325 326 327 328 329 330 331
  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;
332
};
333 334 335 336 337

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
338 339
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
340 341
    "NO",   "YES", "YES",  "NO",  "NULL,0",
    "A very small integer"},
342
  {"tinyint unsigned",
343 344
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",
    "YES",  "YES",  "YES",  "NO",  "NULL,0",
345 346 347
    "A very small integer"},
};

348
bool mysqld_show_column_types(THD *thd)
349 350
{
  List<Item> field_list;
351
  Protocol *protocol= thd->protocol;
352 353 354 355 356 357
  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));
358 359
  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));
360 361 362 363 364 365 366 367 368
  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));

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

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


399
int
bk@work.mysql.com's avatar
bk@work.mysql.com committed
400
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
401
                 const char *wild, bool dir)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
402 403 404 405 406
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
407
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
408
  uint col_access=thd->col_access;
409
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
410 411 412
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

413 414
  if (wild && !wild[0])
    wild=0;
415

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

418 419 420 421 422 423
  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
424
    DBUG_RETURN(-1);
425
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
426

427
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
428 429 430
  {
    file=dirp->dir_entry+i;
    if (dir)
431
    {                                           /* Return databases */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
432 433
#ifdef USE_SYMDIR
      char *ext;
434
      char buff[FN_REFLEN];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
435
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
436 437
      {
	/* Only show the sym file if it points to a directory */
438
	char *end;
439
        *ext=0;                                 /* Remove extension */
440 441 442 443
	unpack_dirname(buff, file->name);
	end= strend(buff);
	if (end != buff && end[-1] == FN_LIBCHAR)
	  end[-1]= 0;				// Remove end FN_LIBCHAR
444 445 446
        if (!my_stat(buff, file->mystat, MYF(0)))
               continue;
       }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
447
#endif
dlenev@mysql.com's avatar
dlenev@mysql.com committed
448
        if (file->name[0] == '.' || !MY_S_ISDIR(file->mystat->st_mode) ||
449
            (wild && wild_compare(file->name,wild,0)))
450
          continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
451 452 453
    }
    else
    {
454
        // Return only .frm files which aren't temp files.
455
      if (my_strcasecmp(system_charset_info, ext=fn_ext(file->name),reg_ext) ||
456
          is_prefix(file->name,tmp_file_prefix))
457
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
458
      *ext=0;
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;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
479
      if (check_grant(thd, TABLE_ACLS, &table_list, 1, UINT_MAX, 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 516 517 518 519 520 521 522 523 524 525 526 527
  {
    if (!table_list->view || thd->net.last_errno != ER_VIEW_INVALID)
      DBUG_RETURN(TRUE);
    /*
      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);
    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);
  }
528

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

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

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

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

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

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

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

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

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

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

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
674 675


bk@work.mysql.com's avatar
bk@work.mysql.com committed
676
/****************************************************************************
677 678
  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
679 680 681 682 683 684 685
****************************************************************************/

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

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

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

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
697 698
    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
699 700
      field_list.push_back(new Item_field(field));
  }
701
  restore_record(table, s->default_values);              // Get empty record
702 703
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS |
                                              Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
704
    DBUG_VOID_RETURN;
705
  thd->protocol->flush();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
706 707 708
  DBUG_VOID_RETURN;
}

709

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

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

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

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

  SYNOPSIS
742 743 744 745 746 747 748
  require_quotes()
  name			attribute name
  name_length		length of name

  RETURN
    #	Pointer to conflicting character
    0	No conflicting character
749 750
*/

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

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

766

767 768
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
769
{
770 771
  const char *name_end;
  char quote_char;
772
  int q= get_quote_char_for_identifier(thd, name, length);
773

774
  if (q == EOF)
775
  {
776
    packet->append(name, length, system_charset_info);
777 778 779
    return;
  }

780 781 782 783
  /*
    The identifier must be quoted as it includes a quote character or
   it's a keyword
  */
784 785

  packet->reserve(length*2 + 2);
786
  quote_char= (char) q;
787 788 789 790
  packet->append(&quote_char, 1, system_charset_info);

  for (name_end= name+length ; name < name_end ; name+= length)
  {
791
    uchar chr= (uchar) *name;
792
    length= my_mbcharlen(system_charset_info, chr);
793 794 795 796 797 798 799 800 801
    /*
      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;
802
    if (length == 1 && chr == (uchar) quote_char)
803 804
      packet->append(&quote_char, 1, system_charset_info);
    packet->append(name, length, packet->charset());
805
  }
806
  packet->append(&quote_char, 1, system_charset_info);
807 808
}

809

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
/*
  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
    If name is a keyword or includes a special character, then force
    quoting.
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
    is set.

  RETURN
    EOF	  No quote character is needed
    #	  Quote character
*/
829 830 831 832 833 834 835

int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
{
  if (!is_keyword(name,length) &&
      !require_quotes(name, length) &&
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
    return EOF;
836
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
837
    return '"';
838
  return '`';
839 840 841
}


842 843 844 845 846
/* 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
847
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
848
  {
849
    uint length= dirname_length(filename);
850 851
    packet->append(' ');
    packet->append(dir_type);
852
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
853
#ifdef __WIN__
monty@mysql.com's avatar
monty@mysql.com committed
854 855 856 857 858 859 860 861 862
    /* 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;
863
#endif
monty@mysql.com's avatar
monty@mysql.com committed
864
    packet->append(filename, length);
865 866 867 868 869
    packet->append('\'');
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
870
#define LIST_PROCESS_HOST_LEN 64
871

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
/*
  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
897
{
898
  List<Item> field_list;
899 900
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end;
  const char *alias;
901
  String type(tmp, sizeof(tmp), system_charset_info);
902 903 904
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
905
  TABLE *table= table_list->table;
906
  handler *file= table->file;
907
  TABLE_SHARE *share= table->s;
908
  HA_CREATE_INFO create_info;
909 910 911 912
  my_bool foreign_db_mode=    (thd->variables.sql_mode & (MODE_POSTGRESQL |
							  MODE_ORACLE |
							  MODE_MSSQL |
							  MODE_DB2 |
913
							  MODE_MAXDB |
914 915 916 917
							  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
918
  DBUG_ENTER("store_create_info");
919
  DBUG_PRINT("enter",("table: %s", table->s->table_name.str));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
920

921
  restore_record(table, s->default_values); // Get empty record
922

923
  if (share->tmp_table)
924
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
925
  else
926
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
927
  if (table_list->schema_table)
928
    alias= table_list->schema_table->table_name;
929
  else
930
    alias= (lower_case_table_names == 2 ? table->alias :
931
            share->table_name.str);
monty@mysql.com's avatar
monty@mysql.com committed
932
  append_identifier(thd, packet, alias, strlen(alias));
933
  packet->append(STRING_WITH_LEN(" (\n"));
934

bk@work.mysql.com's avatar
bk@work.mysql.com committed
935 936
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
937
    bool has_default;
938
    bool has_now_default;
939 940
    uint flags = field->flags;

941
    if (ptr != table->field)
942
      packet->append(STRING_WITH_LEN(",\n"));
943

944
    packet->append(STRING_WITH_LEN("  "));
945
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
946 947
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
948
    if (type.ptr() != tmp)
949
      type.set(tmp, sizeof(tmp), system_charset_info);
950 951
    else
      type.set_charset(system_charset_info);
952

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

956 957
    if (field->has_charset() && 
        !(thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
958
    {
959
      if (field->charset() != share->table_charset)
960
      {
961
	packet->append(STRING_WITH_LEN(" character set "));
962 963 964 965 966 967 968 969
	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))
      {
970
	packet->append(STRING_WITH_LEN(" collate "));
971
	packet->append(field->charset()->name);
972
      }
973
    }
974

975
    if (flags & NOT_NULL_FLAG)
976
      packet->append(STRING_WITH_LEN(" NOT NULL"));
977 978 979 980 981 982
    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.
      */
983
      packet->append(STRING_WITH_LEN(" NULL"));
984
    }
985 986

    /* 
987
      Again we are using CURRENT_TIMESTAMP instead of NOW because it is
988 989 990 991 992
      more standard 
    */
    has_now_default= table->timestamp_field == field && 
                     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
    
993
    has_default= (field->type() != FIELD_TYPE_BLOB &&
994
                  !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
995
		  field->unireg_check != Field::NEXT_NUMBER &&
996
                  !((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
997
                    has_now_default));
998

999
    if (has_default)
1000
    {
1001
      packet->append(STRING_WITH_LEN(" default "));
1002
      if (has_now_default)
1003
        packet->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
1004
      else if (!field->is_null())
1005
      {                                             // Not null by default
1006
        type.set(tmp, sizeof(tmp), field->charset());
1007
        field->val_str(&type);
1008
	if (type.length())
1009
	{
1010
	  String def_val;
1011
          uint dummy_errors;
1012 1013
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
1014
		       system_charset_info, &dummy_errors);
1015 1016
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
1017
        else
1018
	  packet->append(STRING_WITH_LEN("''"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1019
      }
1020
      else if (field->maybe_null())
1021
        packet->append(STRING_WITH_LEN("NULL"));    // Null as default
1022
      else
1023
        packet->append(tmp);
1024
    }
1025

1026
    if (!(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS) &&
1027 1028
        table->timestamp_field == field && 
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
1029
      packet->append(STRING_WITH_LEN(" on update CURRENT_TIMESTAMP"));
1030

1031 1032
    if (field->unireg_check == Field::NEXT_NUMBER && 
        !(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS))
1033
      packet->append(STRING_WITH_LEN(" auto_increment"));
1034 1035 1036

    if (field->comment.length)
    {
1037
      packet->append(STRING_WITH_LEN(" COMMENT "));
1038 1039
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1040 1041
  }

1042
  key_info= table->key_info;
1043
  bzero((char*) &create_info, sizeof(create_info));
1044
  file->update_create_info(&create_info);
1045
  primary_key= share->primary_key;
1046

1047
  for (uint i=0 ; i < share->keys ; i++,key_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1048
  {
1049 1050
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
1051
    packet->append(STRING_WITH_LEN(",\n  "));
1052

1053
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
1054 1055
    {
      found_primary=1;
1056
      packet->append(STRING_WITH_LEN("PRIMARY "));
1057
    }
1058
    else if (key_info->flags & HA_NOSAME)
1059
      packet->append(STRING_WITH_LEN("UNIQUE "));
1060
    else if (key_info->flags & HA_FULLTEXT)
1061
      packet->append(STRING_WITH_LEN("FULLTEXT "));
1062
    else if (key_info->flags & HA_SPATIAL)
1063 1064
      packet->append(STRING_WITH_LEN("SPATIAL "));
    packet->append(STRING_WITH_LEN("KEY "));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1065

1066
    if (!found_primary)
1067
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
1068

1069 1070 1071
    if (!(thd->variables.sql_mode & MODE_NO_KEY_OPTIONS) &&
	!limited_mysql_mode && !foreign_db_mode)
    {
1072
      if (key_info->algorithm == HA_KEY_ALG_BTREE)
1073
        packet->append(STRING_WITH_LEN(" USING BTREE"));
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
1074

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

1078 1079 1080
      // +BAR: send USING only in non-default case: non-spatial rtree
      if ((key_info->algorithm == HA_KEY_ALG_RTREE) &&
	  !(key_info->flags & HA_SPATIAL))
1081
        packet->append(STRING_WITH_LEN(" USING RTREE"));
1082

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1087 1088
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
1089
      if (j)
1090
        packet->append(',');
1091

1092
      if (key_part->field)
1093 1094
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1095
      if (!key_part->field ||
1096 1097 1098
          (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
1099
      {
1100
        buff[0] = '(';
1101 1102 1103
        char* end=int10_to_str((long) key_part->length / 
			       key_part->field->charset()->mbmaxlen,
			       buff + 1,10);
1104 1105
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1106 1107 1108
      }
    }
    packet->append(')');
1109 1110 1111 1112 1113 1114
    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
1115
  }
1116

1117 1118 1119 1120
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
1121

1122
  if ((for_str= file->get_foreign_key_create_info()))
1123 1124 1125
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
1126 1127
  }

1128
  packet->append(STRING_WITH_LEN("\n)"));
1129
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
1130
  {
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    /*
      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="));
1142
#ifdef WITH_PARTITION_STORAGE_ENGINE
1143
    if (table->part_info)
1144 1145
      packet->append(ha_resolve_storage_engine_name(
                        table->part_info->default_engine_type));
1146
    else
1147
      packet->append(file->table_type());
1148
#else
1149
      packet->append(file->table_type());
1150
#endif
1151
    }
1152
    
1153
    if (share->table_charset &&
1154 1155
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
1156
    {
1157 1158 1159 1160 1161 1162
      /*
        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))
1163
      {
1164 1165 1166 1167 1168 1169 1170
        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);
        }
1171
      }
1172
    }
1173

1174
    if (share->min_rows)
1175
    {
1176
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
1177
      end= longlong10_to_str(share->min_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1178
      packet->append(buff, (uint) (end- buff));
1179
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1180

1181
    if (share->max_rows && !table_list->schema_table)
1182
    {
1183
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
1184
      end= longlong10_to_str(share->max_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1185
      packet->append(buff, (uint) (end - buff));
1186
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1187

1188
    if (share->avg_row_length)
1189
    {
1190
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
1191
      end= longlong10_to_str(share->avg_row_length, buff,10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1192
      packet->append(buff, (uint) (end - buff));
1193
    }
1194

1195
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
1196
      packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
1197
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
1198
      packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
1199
    if (share->db_create_options & HA_OPTION_CHECKSUM)
1200
      packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
1201
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1202
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
1203
    if (share->row_type != ROW_TYPE_DEFAULT)
1204
    {
1205
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
1206
      packet->append(ha_row_type[(uint) share->row_type]);
1207 1208
    }
    table->file->append_create_info(packet);
1209
    if (share->comment && share->comment[0])
1210
    {
1211
      packet->append(STRING_WITH_LEN(" COMMENT="));
1212
      append_unescaped(packet, share->comment, strlen(share->comment));
1213
    }
1214 1215
    if (share->connect_string.length)
    {
1216
      packet->append(STRING_WITH_LEN(" CONNECTION="));
1217 1218
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
    }
1219 1220
    if (file->raid_type)
    {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1221 1222 1223 1224 1225 1226
      uint length;
      length= my_snprintf(buff,sizeof(buff),
			  " RAID_TYPE=%s RAID_CHUNKS=%d RAID_CHUNKSIZE=%ld",
			  my_raid_type(file->raid_type), file->raid_chunks,
			  file->raid_chunksize/RAID_BLOCK_SIZE);
      packet->append(buff, length);
1227
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1228 1229
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1230
  }
1231
#ifdef WITH_PARTITION_STORAGE_ENGINE
1232 1233 1234 1235 1236 1237
  {
    /*
      Partition syntax for CREATE TABLE is at the end of the syntax.
    */
    uint part_syntax_len;
    char *part_syntax;
1238 1239 1240 1241
    if (table->part_info &&
        ((part_syntax= generate_partition_syntax(table->part_info,
                                                 &part_syntax_len,
                                                 FALSE,FALSE))))
1242 1243 1244 1245 1246 1247
    {
       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
1248 1249 1250
  DBUG_RETURN(0);
}

1251 1252 1253
void
view_store_options(THD *thd, TABLE_LIST *table, String *buff)
{
1254
  buff->append(STRING_WITH_LEN("ALGORITHM="));
1255 1256
  switch ((int8)table->algorithm) {
  case VIEW_ALGORITHM_UNDEFINED:
1257
    buff->append(STRING_WITH_LEN("UNDEFINED "));
1258 1259
    break;
  case VIEW_ALGORITHM_TMPTABLE:
1260
    buff->append(STRING_WITH_LEN("TEMPTABLE "));
1261 1262
    break;
  case VIEW_ALGORITHM_MERGE:
1263
    buff->append(STRING_WITH_LEN("MERGE "));
1264 1265 1266 1267
    break;
  default:
    DBUG_ASSERT(0); // never should happen
  }
1268
  append_definer(thd, buff, &table->definer.user, &table->definer.host);
1269
  if (table->view_suid)
1270
    buff->append(STRING_WITH_LEN("SQL SECURITY DEFINER "));
1271
  else
1272
    buff->append(STRING_WITH_LEN("SQL SECURITY INVOKER "));
1273
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1274

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
/*
  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(' ');
}


1297
int
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1298 1299 1300 1301 1302 1303 1304 1305
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;
1306 1307 1308 1309 1310
  /*
     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
  */
1311
  if (!thd->db || strcmp(thd->db, table->view_db.str))
1312 1313 1314 1315
    table->compact_view_format= FALSE;
  else
  {
    TABLE_LIST *tbl;
1316
    table->compact_view_format= TRUE;
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    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;
      }
    }
  }

1329
  buff->append(STRING_WITH_LEN("CREATE "));
1330
  if (!foreign_db_mode)
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1331
  {
1332
    view_store_options(thd, table, buff);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1333
  }
1334
  buff->append(STRING_WITH_LEN("VIEW "));
1335 1336 1337 1338 1339
  if (!table->compact_view_format)
  {
    append_identifier(thd, buff, table->view_db.str, table->view_db.length);
    buff->append('.');
  }
1340
  append_identifier(thd, buff, table->view_name.str, table->view_name.length);
1341
  buff->append(STRING_WITH_LEN(" AS "));
1342 1343 1344 1345 1346 1347 1348

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

1349 1350 1351
  if (table->with_check != VIEW_CHECK_NONE)
  {
    if (table->with_check == VIEW_CHECK_LOCAL)
1352
      buff->append(STRING_WITH_LEN(" WITH LOCAL CHECK OPTION"));
1353
    else
1354
      buff->append(STRING_WITH_LEN(" WITH CASCADED CHECK OPTION"));
1355
  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1356 1357 1358 1359
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1360
/****************************************************************************
1361 1362
  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
1363 1364 1365 1366
****************************************************************************/

class thread_info :public ilink {
public:
1367 1368 1369 1370
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1371
  static void operator delete(void *ptr __attribute__((unused)),
1372 1373
                              size_t size __attribute__((unused)))
  { TRASH(ptr, size); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1374

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1375 1376
  ulong thread_id;
  time_t start_time;
1377
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1378 1379 1380 1381
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

1382
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1383 1384 1385 1386 1387 1388 1389 1390
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;
1391 1392
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1393
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1394 1395
  DBUG_ENTER("mysqld_list_processes");

1396
  field_list.push_back(new Item_int("Id",0,11));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1397
  field_list.push_back(new Item_empty_string("User",16));
1398
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1399 1400 1401
  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));
1402
  field_list.push_back(new Item_return_int("Time",7, FIELD_TYPE_LONG));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1403 1404 1405 1406
  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;
1407 1408
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1409 1410 1411 1412 1413 1414 1415 1416 1417
    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++))
    {
1418
      Security_context *tmp_sctx= tmp->security_ctx;
1419
      struct st_my_thread_var *mysys_var;
1420
      if ((tmp->vio_ok() || tmp->system_thread) &&
1421
          (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1422
      {
1423
        thread_info *thd_info= new thread_info;
1424 1425

        thd_info->thread_id=tmp->thread_id;
1426 1427 1428 1429 1430
        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])
1431 1432
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1433
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1434
			"%s:%u", tmp_sctx->host_or_ip, tmp->peer_port);
1435 1436
	}
	else
1437
	  thd_info->host= thd->strdup(tmp_sctx->host_or_ip);
1438 1439 1440
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1441 1442
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1443
        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
1444
#ifndef EMBEDDED_LIBRARY
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
        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
1455 1456 1457
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1458 1459
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1460 1461

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1462 1463
        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
1464
#endif
1465 1466 1467
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1468
        thd_info->start_time= tmp->start_time;
1469
#endif
1470 1471 1472
        thd_info->query=0;
        if (tmp->query)
        {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1473 1474 1475 1476 1477
	  /* 
            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
          */
1478
          uint length= min(max_query_length, tmp->query_length);
1479
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1480 1481
        }
        thread_infos.append(thd_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1482 1483 1484 1485 1486 1487
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1488
  time_t now= time(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1489 1490
  while ((thd_info=thread_infos.get()))
  {
1491 1492
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1493 1494 1495
    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
1496
    if (thd_info->proc_info)
1497
      protocol->store(thd_info->proc_info, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1498
    else
1499
      protocol->store(command_name[thd_info->command], system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1500
    if (thd_info->start_time)
1501
      protocol->store((uint32) (now - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1502
    else
1503
      protocol->store_null();
1504 1505
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1506
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1507 1508
      break; /* purecov: inspected */
  }
1509
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1510 1511 1512 1513
  DBUG_VOID_RETURN;
}

/*****************************************************************************
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1514
  Status functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1515 1516
*****************************************************************************/

1517 1518

static bool show_status_array(THD *thd, const char *wild,
1519 1520 1521 1522
                              show_var_st *variables,
                              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
1523
{
1524 1525 1526 1527
  char buff[1024], *prefix_end;
  /* the variable name should not be longer then 80 characters */
  char name_buffer[80];
  int len;
1528
  LEX_STRING null_lex_str;
1529
  DBUG_ENTER("show_status_array");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1530

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

1534 1535 1536
  prefix_end=strnmov(name_buffer, prefix, sizeof(name_buffer)-1);
  len=name_buffer + sizeof(name_buffer) - prefix_end;

1537
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1538
  {
1539 1540 1541 1542 1543 1544
    strnmov(prefix_end, variables->name, len);
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
    SHOW_TYPE show_type=variables->type;
    if (show_type == SHOW_VARS)
    {
      show_status_array(thd, wild, (show_var_st *) variables->value,
1545
                        value_type, status_var, variables->name, table);
1546 1547
    }
    else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1548
    {
1549 1550
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
1551
      {
1552
        char *value=variables->value;
1553
        const char *pos, *end;                  // We assign a lot of const's
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
        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;
        switch (show_type) {
        case SHOW_LONG_STATUS:
        case SHOW_LONG_CONST_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
        case SHOW_LONG_CONST:
          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_CONST:
        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
1589 1590 1591 1592 1593 1594
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
1595
        case SHOW_CHAR:
petr@mysql.com's avatar
petr@mysql.com committed
1596 1597 1598 1599 1600 1601
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
1602 1603 1604 1605 1606 1607 1608
        case SHOW_STARTTIME:
          nr= (long) (thd->query_start() - start_time);
          end= int10_to_str(nr, buff, 10);
          break;
        case SHOW_QUESTION:
          end= int10_to_str((long) thd->query_id, buff, 10);
          break;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1609
#ifdef HAVE_REPLICATION
1610 1611 1612 1613
        case SHOW_RPL_STATUS:
          end= strmov(buff, rpl_status_type[(int)rpl_status]);
          break;
        case SHOW_SLAVE_RUNNING:
petr@mysql.com's avatar
petr@mysql.com committed
1614 1615
        {
          pthread_mutex_lock(&LOCK_active_mi);
1616
          end= strmov(buff, (active_mi && active_mi->slave_running &&
petr@mysql.com's avatar
petr@mysql.com committed
1617 1618 1619 1620
                             active_mi->rli.slave_running) ? "ON" : "OFF");
          pthread_mutex_unlock(&LOCK_active_mi);
          break;
        }
gbichot@quadita2.mysql.com's avatar
gbichot@quadita2.mysql.com committed
1621 1622 1623
        case SHOW_SLAVE_RETRIED_TRANS:
        {
          /*
1624 1625
            TODO: in 5.1 with multimaster, have one such counter per line in
            SHOW SLAVE STATUS, and have the sum over all lines here.
gbichot@quadita2.mysql.com's avatar
gbichot@quadita2.mysql.com committed
1626
          */
1627 1628 1629 1630 1631 1632 1633 1634 1635
          pthread_mutex_lock(&LOCK_active_mi);
          if (active_mi)
          {
            pthread_mutex_lock(&active_mi->rli.data_lock);
            end= int10_to_str(active_mi->rli.retried_trans, buff, 10);
            pthread_mutex_unlock(&active_mi->rli.data_lock);
          }
          pthread_mutex_unlock(&LOCK_active_mi);
          break;
gbichot@quadita2.mysql.com's avatar
gbichot@quadita2.mysql.com committed
1636
        }
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
        case SHOW_SLAVE_SKIP_ERRORS:
        {
          MY_BITMAP *bitmap= (MY_BITMAP *)value;
          if (!use_slave_mask || bitmap_is_clear_all(bitmap))
          {
            end= strmov(buff, "OFF");
          }
          else if (bitmap_is_set_all(bitmap))
          {
            end= strmov(buff, "ALL");
          }
          else
          {
1650 1651 1652 1653 1654
            /* 10 is enough assuming errors are max 4 digits */
            int i;
            for (i= 1;
                 i < MAX_SLAVE_ERROR && (uint) (end-buff) < sizeof(buff)-10;
                 i++)
1655 1656 1657
            {
              if (bitmap_is_set(bitmap, i))
              {
1658 1659
                end= int10_to_str(i, (char*) end, 10);
                *(char*) end++= ',';
1660 1661 1662 1663
              }
            }
            if (end != buff)
              end--;				// Remove last ','
1664 1665
            if (i < MAX_SLAVE_ERROR)
              end= strmov((char*) end, "...");  // Couldn't show all errors
1666 1667 1668
          }
          break;
        }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1669
#endif /* HAVE_REPLICATION */
1670 1671 1672 1673 1674
        case SHOW_OPEN_TABLES:
          end= int10_to_str((long) cached_open_tables(), buff, 10);
          break;
        case SHOW_TABLE_DEFINITIONS:
          end= int10_to_str((long) cached_table_definitions(), buff, 10);
1675 1676
          break;
        case SHOW_CHAR_PTR:
petr@mysql.com's avatar
petr@mysql.com committed
1677 1678 1679 1680 1681 1682
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
1683
        case SHOW_DOUBLE_STATUS:
petr@mysql.com's avatar
petr@mysql.com committed
1684
        {
1685
          value= ((char *) status_var + (ulong) value);
petr@mysql.com's avatar
petr@mysql.com committed
1686 1687 1688
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1689
#ifdef HAVE_OPENSSL
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
          /* First group - functions relying on CTX */
        case SHOW_SSL_CTX_SESS_ACCEPT:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept(ssl_acceptor_fd->
                                                        ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_ACCEPT_GOOD:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept_good(ssl_acceptor_fd->
                                                             ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT_GOOD:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect_good(ssl_acceptor_fd->
                                                              ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept_renegotiate(ssl_acceptor_fd->ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect_renegotiate(ssl_acceptor_fd-> ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CB_HITS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_cb_hits(ssl_acceptor_fd->
                                                         ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_HITS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_hits(ssl_acceptor_fd->
                                                      ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CACHE_FULL:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_cache_full(ssl_acceptor_fd->
                                                            ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_MISSES:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_misses(ssl_acceptor_fd->
                                                        ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_TIMEOUTS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_timeouts(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_NUMBER:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_number(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_GET_CACHE_SIZE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_get_cache_size(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_VERIFY_MODE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_get_verify_mode(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_VERIFY_DEPTH:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_get_verify_depth(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_SESSION_CACHE_MODE:
          if (!ssl_acceptor_fd)
          {
            pos= "NONE";
            end= pos+4;
            break;
          }
          switch (SSL_CTX_get_session_cache_mode(ssl_acceptor_fd->ssl_context))
          {
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1782
          case SSL_SESS_CACHE_OFF:
1783
            pos= "OFF";
1784
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1785
          case SSL_SESS_CACHE_CLIENT:
1786
            pos= "CLIENT";
1787
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1788
          case SSL_SESS_CACHE_SERVER:
1789
            pos= "SERVER";
1790
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1791
          case SSL_SESS_CACHE_BOTH:
1792
            pos= "BOTH";
1793
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1794
          case SSL_SESS_CACHE_NO_AUTO_CLEAR:
1795
            pos= "NO_AUTO_CLEAR";
1796
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1797
          case SSL_SESS_CACHE_NO_INTERNAL_LOOKUP:
1798
            pos= "NO_INTERNAL_LOOKUP";
1799 1800
            break;
          default:
1801
            pos= "Unknown";
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
            break;
          }
          end= strend(pos);
          break;
          /* First group - functions relying on SSL */
        case SHOW_SSL_GET_VERSION:
          pos= (thd->net.vio->ssl_arg ?
                SSL_get_version((SSL*) thd->net.vio->ssl_arg) : "");
          end= strend(pos);
          break;
        case SHOW_SSL_SESSION_REUSED:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_session_reused((SSL*) thd->net.vio->
                                                       ssl_arg) :
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_DEFAULT_TIMEOUT:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_default_timeout((SSL*) thd->net.vio->
                                                            ssl_arg) :
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_VERIFY_MODE:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_verify_mode((SSL*) thd->net.vio->
                                                        ssl_arg):
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_VERIFY_DEPTH:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_verify_depth((SSL*) thd->net.vio->
                                                         ssl_arg):
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_CIPHER:
          pos= (thd->net.vio->ssl_arg ?
                SSL_get_cipher((SSL*) thd->net.vio->ssl_arg) : "" );
          end= strend(pos);
          break;
        case SHOW_SSL_GET_CIPHER_LIST:
          if (thd->net.vio->ssl_arg)
          {
            char *to= buff;
            for (int i=0 ; i++ ;)
            {
              const char *p= SSL_get_cipher_list((SSL*) thd->net.vio->ssl_arg,i);
              if (p == NULL)
                break;
              to= strmov(to, p);
              *to++= ':';
            }
            if (to != buff)
              to--;				// Remove last ':'
            end= to;
          }
          break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1862 1863

#endif /* HAVE_OPENSSL */
1864 1865
        case SHOW_KEY_CACHE_LONG:
        case SHOW_KEY_CACHE_CONST_LONG:
1866
          value= (value-(char*) &dflt_key_cache_var)+ (char*) dflt_key_cache;
1867 1868
          end= int10_to_str(*(long*) value, buff, 10);
          break;
1869 1870 1871 1872
        case SHOW_KEY_CACHE_LONGLONG:
	  value= (value-(char*) &dflt_key_cache_var)+ (char*) dflt_key_cache;
	  end= longlong10_to_str(*(longlong*) value, buff, 10);
	  break;
1873 1874 1875
        case SHOW_NET_COMPRESSION:
          end= strmov(buff, thd->net.compress ? "ON" : "OFF");
          break;
1876 1877 1878 1879 1880 1881
        case SHOW_UNDEF:				// Show never happen
        case SHOW_SYS:
          break;					// Return empty string
        default:
          break;
        }
1882
        restore_record(table, s->default_values);
1883 1884 1885
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
1886 1887
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1888 1889 1890
      }
    }
  }
1891 1892 1893 1894 1895

  DBUG_RETURN(FALSE);
}


1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
/* 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;
}


1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
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)
    lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING));
  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};
1935 1936

/* This is only used internally, but we need it here as a forward reference */
1937 1938 1939 1940 1941 1942 1943 1944
extern ST_SCHEMA_TABLE schema_tables[];

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


1945 1946 1947 1948 1949 1950 1951
/*
  Store record to I_S table, convert HEAP table
  to MyISAM if necessary

  SYNOPSIS
    schema_table_store_record()
    thd                   thread handler
1952 1953
    table                 Information schema table to be updated

1954 1955
  RETURN
    0	                  success
1956
    1	                  error
1957 1958 1959 1960 1961
*/

static bool schema_table_store_record(THD *thd, TABLE *table)
{
  int error;
1962
  if ((error= table->file->ha_write_row(table->record[0])))
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
  {
    if (create_myisam_from_heap(thd, table, 
                                table->pos_in_table_list->schema_table_param,
                                error, 0))
      return 1;
  }
  return 0;
}


1973 1974 1975 1976 1977 1978 1979 1980 1981
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:
1982
  case SQLCOM_SHOW_TRIGGERS:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1983
    index_field_values->db_value= lex->select_lex.db;
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
    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
2005
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
                             (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++)
2020
    {
2021 2022
      if (!uses_only_table_name_fields(*child, table))
        return 0;
2023
    }
2024 2025 2026 2027 2028 2029 2030
  }
  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;
2031 2032
    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 : "";
2033 2034 2035 2036 2037 2038 2039
    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)))
2040 2041
      return 0;
  }
2042 2043
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
2044 2045 2046 2047
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

2048
  return 1;
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
}


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


2108 2109 2110 2111 2112 2113
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
{
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
}


2114
/*
2115
  Create db names list. Information schema name always is first in list
2116 2117

  SYNOPSIS
2118
    make_db_list()
2119 2120 2121
    thd                   thread handler
    files                 list of db names
    wild                  wild string
2122 2123
    idx_field_vals        idx_field_vals->db_name contains db name or
                          wild string
2124 2125
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0
2126 2127
    is_wild_value         if value is 1 then idx_field_vals->db_name is
                          wild string otherwise it's db name; 
2128 2129 2130 2131 2132 2133

  RETURN
    1	                  error
    0	                  success
*/

2134 2135 2136
int make_db_list(THD *thd, List<char> *files,
                 INDEX_FIELD_VALUES *idx_field_vals,
                 bool *with_i_schema, bool is_wild_value)
2137
{
2138
  LEX *lex= thd->lex;
2139
  *with_i_schema= 0;
2140 2141
  get_index_field_values(lex, idx_field_vals);
  if (is_wild_value)
2142
  {
2143 2144 2145 2146 2147
    /*
      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)
    */
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
    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);
2159
  }
2160

2161 2162 2163 2164 2165 2166
  /*
    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.
  */
2167 2168
  if (lex->orig_sql_command != SQLCOM_END)
  {
2169
    if (!my_strcasecmp(system_charset_info, information_schema_name.str,
2170 2171 2172 2173 2174 2175 2176 2177
                       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));
  }

2178 2179 2180 2181
  /*
    Create list of existing databases. It is used in case
    of select from information schema table
  */
2182 2183 2184 2185
  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);
2186 2187 2188 2189 2190 2191
}


int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2192
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2193
  {
2194 2195
    if (tmp_schema_table->hidden)
      continue;
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
    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;
}


2215 2216 2217 2218 2219
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;
2220
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2221 2222
  TABLE_LIST **save_query_tables_last= lex->query_tables_last;
  enum_sql_command save_sql_command= lex->sql_command;
2223
  SELECT_LEX *lsel= tables->schema_select_lex;
2224
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2225 2226 2227 2228 2229 2230 2231
  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
2232
  List_iterator_fast<char> it(bases);
2233
  COND *partial_cond;
2234
  Security_context *sctx= thd->security_ctx;
2235
  uint derived_tables= lex->derived_tables; 
monty@mysql.com's avatar
monty@mysql.com committed
2236
  int error= 1;
2237
  enum legacy_db_type not_used;
2238
  Open_tables_state open_tables_state_backup;
2239 2240 2241 2242
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
2243

2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
  /*
    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);

2257 2258 2259
  if (lsel)
  {
    TABLE_LIST *show_table_list= (TABLE_LIST*) lsel->table_list.first;
2260 2261
    bool res;

2262
    lex->all_selects_list= lsel;
2263 2264
    res= open_normal_and_derived_tables(thd, show_table_list,
                                        MYSQL_LOCK_IGNORE_FLUSH);
2265 2266 2267
    /*
      get_all_tables() returns 1 on failure and 0 on success thus
      return only these and not the result code of ::process_table()
2268 2269 2270 2271 2272 2273 2274

      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).
2275 2276
    */
    error= test(schema_table->process_table(thd, show_table_list,
2277 2278 2279 2280 2281
                                            table, res, 
                                            (show_table_list->view ?
                                             show_table_list->view_db.str :
                                             show_table_list->db),
                                            show_table_list->alias));
2282
    close_thread_tables(thd);
2283
    show_table_list->table= 0;
monty@mysql.com's avatar
monty@mysql.com committed
2284
    goto err;
2285 2286
  }

2287
  schema_table_idx= get_schema_table_idx(schema_table);
2288

2289 2290
  if (make_db_list(thd, &bases, &idx_field_vals,
                   &with_i_schema, 0))
monty@mysql.com's avatar
monty@mysql.com committed
2291
    goto err;
2292 2293

  partial_cond= make_cond_for_info_schema(cond, tables);
monty@mysql.com's avatar
monty@mysql.com committed
2294
  it.rewind(); /* To get access to new elements in basis list */
2295
  while ((base_name= it++) ||
2296 2297 2298 2299 2300 2301 2302 2303 2304
	 /*
	   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
2305 2306
    if (!check_access(thd,SELECT_ACL, base_name, 
                      &thd->col_access, 0, 1, with_i_schema) ||
2307 2308
        sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, base_name,0) ||
2309 2310 2311 2312
	(grant_option && !check_grant_db(thd, base_name)))
#endif
    {
      List<char> files;
2313 2314 2315
      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
2316
          goto err;
2317 2318 2319 2320 2321 2322 2323 2324
      }
      else
      {
        strxmov(path, mysql_data_home, "/", base_name, NullS);
        end= path + (len= unpack_dirname(path,path));
        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
2325
          goto err;
2326
      }
2327

2328 2329
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
2330
      {
2331
	restore_record(table, s->default_values);
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
        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)
            {
2342
              if (with_i_schema)
2343
              {
2344 2345
                table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
                                       system_charset_info);
2346 2347 2348 2349
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
2350
                switch (mysql_frm_type(thd, path, &not_used)) {
2351
                case FRMTYPE_ERROR:
2352 2353
                  table->field[3]->store(STRING_WITH_LEN("ERROR"),
                                         system_charset_info);
2354 2355
                  break;
                case FRMTYPE_TABLE:
2356 2357
                  table->field[3]->store(STRING_WITH_LEN("BASE TABLE"),
                                         system_charset_info);
2358 2359
                  break;
                case FRMTYPE_VIEW:
2360 2361
                  table->field[3]->store(STRING_WITH_LEN("VIEW"),
                                         system_charset_info);
2362 2363 2364 2365
                  break;
                default:
                  DBUG_ASSERT(0);
                }
2366 2367
              }
            }
2368
            if (schema_table_store_record(thd, table))
2369
              goto err;
2370 2371 2372 2373
          }
          else
          {
            int res;
2374 2375 2376 2377 2378
            /*
              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;
2379
            if (make_table_list(thd, &sel, base_name, file_name))
monty@mysql.com's avatar
monty@mysql.com committed
2380
              goto err;
2381
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
2382 2383
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
2384 2385
            res= open_normal_and_derived_tables(thd, show_table_list,
                                                MYSQL_LOCK_IGNORE_FLUSH);
2386 2387 2388 2389 2390 2391 2392
            /*
              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.
            */
2393
            res= schema_table->process_table(thd, show_table_list, table,
2394
                                            res, base_name,
2395
                                            show_table_list->alias);
2396
            close_thread_tables(thd);
2397
            if (res)
monty@mysql.com's avatar
monty@mysql.com committed
2398
              goto err;
2399 2400 2401
          }
        }
      }
2402 2403 2404 2405
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
2406
      with_i_schema= 0;
2407 2408
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
2409 2410 2411

  error= 0;
err:
2412
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2413 2414
  lex->derived_tables= derived_tables;
  lex->all_selects_list= old_all_select_lex;
2415 2416 2417
  lex->query_tables_last= save_query_tables_last;
  *save_query_tables_last= 0;
  lex->sql_command= save_sql_command;
monty@mysql.com's avatar
monty@mysql.com committed
2418
  DBUG_RETURN(error);
2419 2420 2421
}


2422
bool store_schema_shemata(THD* thd, TABLE *table, const char *db_name,
2423
                          CHARSET_INFO *cs)
2424
{
2425
  restore_record(table, s->default_values);
2426
  table->field[1]->store(db_name, strlen(db_name), system_charset_info);
2427 2428
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2429
  return schema_table_store_record(thd, table);
2430 2431 2432
}


2433 2434
int fill_schema_shemata(THD *thd, TABLE_LIST *tables, COND *cond)
{
2435
  char path[FN_REFLEN];
2436 2437 2438 2439 2440
  bool found_libchar;
  INDEX_FIELD_VALUES idx_field_vals;
  List<char> files;
  char *file_name;
  uint length;
2441
  bool with_i_schema;
2442 2443
  HA_CREATE_INFO create;
  TABLE *table= tables->table;
2444
  Security_context *sctx= thd->security_ctx;
2445
  DBUG_ENTER("fill_schema_shemata");
2446

2447 2448
  if (make_db_list(thd, &files, &idx_field_vals,
                   &with_i_schema, 1))
2449
    DBUG_RETURN(1);
2450

2451 2452 2453
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2454 2455
    if (with_i_schema)       // information schema name is always first in list
    {
2456
      if (store_schema_shemata(thd, table, file_name,
2457
                               system_charset_info))
2458
        DBUG_RETURN(1);
2459 2460 2461
      with_i_schema= 0;
      continue;
    }
2462
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2463 2464
    if (sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, file_name,0) ||
2465 2466 2467
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2468
      strxmov(path, mysql_data_home, "/", file_name, NullS);
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
      length=unpack_dirname(path,path);		// Convert if not unix
      found_libchar= 0;
      if (length && path[length-1] == FN_LIBCHAR)
      {
	found_libchar= 1;
	path[length-1]=0;			// remove ending '\'
      }

      if (found_libchar)
	path[length-1]= FN_LIBCHAR;
      strmov(path+length, MY_DB_OPT_FILE);
      load_db_opt(thd, path, &create);
2481
      if (store_schema_shemata(thd, table, file_name, 
2482
                               create.default_table_charset))
2483
        DBUG_RETURN(1);
2484 2485
    }
  }
2486
  DBUG_RETURN(0);
2487 2488 2489
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2490 2491 2492 2493
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)
2494 2495 2496 2497 2498
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2499 2500

  restore_record(table, s->default_values);
2501 2502
  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
2503 2504 2505 2506 2507 2508
  if (res)
  {
    /*
      there was errors during opening tables
    */
    const char *error= thd->net.last_error;
2509 2510 2511 2512 2513 2514
    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
2515 2516 2517 2518
    table->field[20]->store(error, strlen(error), cs);
    thd->clear_error();
  }
  else if (tables->view)
2519
  {
2520 2521
    table->field[3]->store(STRING_WITH_LEN("VIEW"), cs);
    table->field[20]->store(STRING_WITH_LEN("VIEW"), cs);
2522 2523 2524 2525
  }
  else
  {
    TABLE *show_table= tables->table;
2526
    TABLE_SHARE *share= show_table->s;
2527
    handler *file= show_table->file;
2528

2529 2530
    file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_AUTO |
               HA_STATUS_NO_LOCK);
2531
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2532
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2533
    else if (share->tmp_table)
2534
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
2535
    else
2536
      table->field[3]->store(STRING_WITH_LEN("BASE TABLE"), cs);
2537

2538 2539
    for (int i= 4; i < 20; i++)
    {
2540
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2541 2542 2543 2544 2545
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2546
    table->field[5]->store((longlong) share->frm_version, TRUE);
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
    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;
    }
2572
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2573 2574
    if (!tables->schema_table)
    {
2575
      table->field[7]->store((longlong) file->records, TRUE);
2576 2577
      table->field[7]->set_notnull();
    }
2578 2579
    table->field[8]->store((longlong) file->mean_rec_length, TRUE);
    table->field[9]->store((longlong) file->data_file_length, TRUE);
2580 2581
    if (file->max_data_file_length)
    {
2582
      table->field[10]->store((longlong) file->max_data_file_length, TRUE);
2583
    }
2584 2585
    table->field[11]->store((longlong) file->index_file_length, TRUE);
    table->field[12]->store((longlong) file->delete_length, TRUE);
2586
    if (show_table->found_next_number_field)
2587
    {
2588
      table->field[13]->store((longlong) file->auto_increment_value, TRUE);
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
      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();
    }
2611 2612
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2613
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
2614
    if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
2615
    {
2616
      table->field[18]->store((longlong) file->checksum(), TRUE);
2617 2618 2619 2620 2621
      table->field[18]->set_notnull();
    }

    char option_buff[350],*ptr;
    ptr=option_buff;
2622
    if (share->min_rows)
2623 2624
    {
      ptr=strmov(ptr," min_rows=");
2625
      ptr=longlong10_to_str(share->min_rows,ptr,10);
2626
    }
2627
    if (share->max_rows)
2628 2629
    {
      ptr=strmov(ptr," max_rows=");
2630
      ptr=longlong10_to_str(share->max_rows,ptr,10);
2631
    }
2632
    if (share->avg_row_length)
2633 2634
    {
      ptr=strmov(ptr," avg_row_length=");
2635
      ptr=longlong10_to_str(share->avg_row_length,ptr,10);
2636
    }
2637
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2638
      ptr=strmov(ptr," pack_keys=1");
2639
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2640
      ptr=strmov(ptr," pack_keys=0");
2641
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2642
      ptr=strmov(ptr," checksum=1");
2643
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2644
      ptr=strmov(ptr," delay_key_write=1");
2645
    if (share->row_type != ROW_TYPE_DEFAULT)
2646
      ptr=strxmov(ptr, " row_format=", 
2647
                  ha_row_type[(uint) share->row_type],
2648 2649 2650 2651
                  NullS);
    if (file->raid_type)
    {
      char buff[100];
2652 2653 2654 2655
      my_snprintf(buff,sizeof(buff),
                  " raid_type=%s raid_chunks=%d raid_chunksize=%ld",
                  my_raid_type(file->raid_type), file->raid_chunks,
                  file->raid_chunksize/RAID_BLOCK_SIZE);
2656 2657 2658 2659 2660
      ptr=strmov(ptr,buff);
    }
    table->field[19]->store(option_buff+1,
                            (ptr == option_buff ? 0 : 
                             (uint) (ptr-option_buff)-1), cs);
2661
    {
2662 2663
      char *comment;
      comment= show_table->file->update_table_comment(share->comment);
2664 2665 2666
      if (comment)
      {
        table->field[20]->store(comment, strlen(comment), cs);
2667 2668
        if (comment != share->comment)
          my_free(comment, MYF(0));
2669 2670
      }
    }
2671
  }
2672
  DBUG_RETURN(schema_table_store_record(thd, table));
2673 2674 2675
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2676 2677 2678 2679
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)
2680
{
2681 2682
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2683
  CHARSET_INFO *cs= system_charset_info;
2684 2685 2686 2687 2688
  TABLE *show_table;
  handler *file;
  Field **ptr,*field;
  int count;
  uint base_name_length, file_name_length;
2689
  DBUG_ENTER("get_schema_column_record");
2690

2691 2692
  if (res)
  {
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
    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);
2705 2706
  }

2707 2708 2709
  show_table= tables->table;
  file= show_table->file;
  count= 0;
2710
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2711
  restore_record(show_table, s->default_values);
2712 2713 2714
  base_name_length= strlen(base_name);
  file_name_length= strlen(file_name);

2715 2716
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
    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);
2735 2736

#ifndef NO_EMBEDDED_ACCESS_CHECKS
2737 2738
    uint col_access;
    check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
2739
                 &tables->grant.privilege, 0, 0, test(tables->schema_table));
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    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)
2750
      {
2751 2752
        *end++=',';
        end=strmov(end,grant_types.type_names[bitnr]);
2753
      }
2754
    }
2755
    table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);
2756

2757
#endif
2758 2759 2760 2761
    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);
2762
    table->field[4]->store((longlong) count, TRUE);
2763 2764 2765 2766 2767 2768 2769 2770 2771
    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)
    {
2772
      table->field[5]->store(STRING_WITH_LEN("CURRENT_TIMESTAMP"), cs);
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
      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);
    if (field->has_charset() || is_blob)
    {
2804 2805 2806
      longlong char_max_len= is_blob ? 
        (longlong) field->max_length() / field->charset()->mbminlen :
        (longlong) field->max_length() / field->charset()->mbmaxlen;
2807
      table->field[8]->store(char_max_len, TRUE);
2808
      table->field[8]->set_notnull();
2809
      table->field[9]->store((longlong) field->max_length(), TRUE);
2810 2811
      table->field[9]->set_notnull();
    }
2812

2813 2814 2815 2816
    /*
      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
2817

2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
    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)
    {
2850
      table->field[10]->store((longlong) field_length, TRUE);
2851
      table->field[10]->set_notnull();
2852
    }
2853 2854
    if (decimals >= 0)
    {
2855
      table->field[11]->store((longlong) decimals, TRUE);
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
      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);
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
  }
  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;
2896 2897

  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
2898 2899 2900 2901 2902 2903 2904
  {
    CHARSET_INFO *tmp_cs= cs[0];
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
        (tmp_cs->state & MY_CS_AVAILABLE) &&
        !(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cs->csname,wild)))
    {
2905
      const char *comment;
2906
      restore_record(table, s->default_values);
2907
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
2908
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
2909 2910
      comment= tmp_cs->comment ? tmp_cs->comment : "";
      table->field[2]->store(comment, strlen(comment), scs);
2911
      table->field[3]->store((longlong) tmp_cs->mbmaxlen, TRUE);
2912 2913
      if (schema_table_store_record(thd, table))
        return 1;
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
    }
  }
  return 0;
}


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;
2926
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
2927 2928 2929 2930 2931 2932
  {
    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;
2933
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
2934 2935 2936 2937 2938 2939 2940 2941 2942
    {
      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;
2943
	restore_record(table, s->default_values);
2944 2945
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
2946
        table->field[2]->store((longlong) tmp_cl->number, TRUE);
2947 2948 2949 2950
        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);
2951
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply, TRUE);
2952 2953
        if (schema_table_store_record(thd, table))
          return 1;
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
      }
    }
  }
  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;
2966
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
2967 2968 2969 2970 2971 2972
  {
    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;
2973
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
2974 2975 2976 2977 2978
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
2979
      restore_record(table, s->default_values);
2980 2981
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
2982 2983
      if (schema_table_store_record(thd, table))
        return 1;
2984 2985 2986 2987 2988 2989
    }
  }
  return 0;
}


2990
bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table,
2991
                       const char *wild, bool full_access, const char *sp_user)
2992 2993 2994 2995 2996
{
  String tmp_string;
  TIME time;
  LEX *lex= thd->lex;
  CHARSET_INFO *cs= system_charset_info;
2997 2998 2999 3000 3001 3002
  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);
3003 3004
  if (!full_access && check_some_routine_access(thd, sp_db, sp_name,
			proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE))
3005
    return 0;
3006

3007 3008 3009 3010 3011 3012
  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)
  {
3013 3014
    restore_record(table, s->default_values);
    if (!wild || !wild[0] || !wild_compare(sp_name, wild, 0))
3015
    {
3016
      int enum_idx= proc_table->field[5]->val_int();
3017
      table->field[3]->store(sp_name, strlen(sp_name), cs);
3018 3019
      get_field(thd->mem_root, proc_table->field[3], &tmp_string);
      table->field[0]->store(tmp_string.ptr(), tmp_string.length(), cs);
3020
      table->field[2]->store(sp_db, strlen(sp_db), cs);
3021 3022
      get_field(thd->mem_root, proc_table->field[2], &tmp_string);
      table->field[4]->store(tmp_string.ptr(), tmp_string.length(), cs);
3023 3024 3025 3026 3027 3028
      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();
      }
3029 3030 3031 3032 3033
      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);
      }
3034 3035
      table->field[6]->store(STRING_WITH_LEN("SQL"), cs);
      table->field[10]->store(STRING_WITH_LEN("SQL"), cs);
3036
      get_field(thd->mem_root, proc_table->field[6], &tmp_string);
3037
      table->field[11]->store(tmp_string.ptr(), tmp_string.length(), cs);
3038 3039
      table->field[12]->store(sp_data_access_name[enum_idx].str, 
                              sp_data_access_name[enum_idx].length , cs);
3040 3041
      get_field(thd->mem_root, proc_table->field[7], &tmp_string);
      table->field[14]->store(tmp_string.ptr(), tmp_string.length(), cs);
3042 3043
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[12])->get_time(&time);
3044
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3045 3046
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[13])->get_time(&time);
3047
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3048
      get_field(thd->mem_root, proc_table->field[14], &tmp_string);
3049
      table->field[17]->store(tmp_string.ptr(), tmp_string.length(), cs);
3050
      get_field(thd->mem_root, proc_table->field[15], &tmp_string);
3051
      table->field[18]->store(tmp_string.ptr(), tmp_string.length(), cs);
3052
      table->field[19]->store(definer, strlen(definer), cs);
3053
      return schema_table_store_record(thd, table);
3054 3055
    }
  }
3056
  return 0;
3057 3058 3059 3060 3061 3062 3063 3064 3065
}


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;
3066
  TABLE *table= tables->table;
3067 3068
  bool full_access;
  char definer[HOSTNAME_LENGTH+USERNAME_LENGTH+2];
3069
  Open_tables_state open_tables_state_backup;
3070 3071
  DBUG_ENTER("fill_schema_proc");

3072 3073
  strxmov(definer, thd->security_ctx->priv_user, "@",
          thd->security_ctx->priv_host, NullS);
3074
  /* We use this TABLE_LIST instance only for checking of privileges. */
3075 3076
  bzero((char*) &proc_tables,sizeof(proc_tables));
  proc_tables.db= (char*) "mysql";
3077
  proc_tables.db_length= 5;
3078
  proc_tables.table_name= proc_tables.alias= (char*) "proc";
3079
  proc_tables.table_name_length= 4;
3080
  proc_tables.lock_type= TL_READ;
3081
  full_access= !check_table_access(thd, SELECT_ACL, &proc_tables, 1);
3082
  if (!(proc_table= open_proc_table_for_read(thd, &open_tables_state_backup)))
3083 3084 3085
  {
    DBUG_RETURN(1);
  }
3086
  proc_table->file->ha_index_init(0, 1);
3087 3088 3089 3090 3091
  if ((res= proc_table->file->index_first(proc_table->record[0])))
  {
    res= (res == HA_ERR_END_OF_FILE) ? 0 : 1;
    goto err;
  }
3092 3093 3094 3095 3096
  if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
  {
    res= 1;
    goto err;
  }
3097
  while (!proc_table->file->index_next(proc_table->record[0]))
3098 3099 3100 3101 3102 3103 3104
  {
    if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
    {
      res= 1;
      goto err;
    }
  }
3105 3106 3107

err:
  proc_table->file->ha_index_end();
3108
  close_proc_table(thd, &open_tables_state_backup);
3109 3110 3111 3112
  DBUG_RETURN(res);
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3113 3114 3115 3116
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)
3117 3118 3119
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
  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
      */ 
      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)
3137 3138 3139 3140 3141 3142
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3143
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3144 3145 3146 3147 3148
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
3149
        restore_record(table, s->default_values);
3150 3151 3152
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
        table->field[3]->store((longlong) ((key_info->flags & 
3153
                                            HA_NOSAME) ? 0 : 1), TRUE);
3154 3155
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
3156
        table->field[6]->store((longlong) (j+1), TRUE);
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171
        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]);
3172
          table->field[9]->store((longlong) records, TRUE);
3173 3174 3175 3176 3177 3178 3179
          table->field[9]->set_notnull();
        }
        if (!(key_info->flags & HA_FULLTEXT) && 
            (!key_part->field ||
             key_part->length != 
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
bar@mysql.com's avatar
bar@mysql.com committed
3180 3181
          table->field[10]->store((longlong) key_part->length / 
                                  key_part->field->charset()->mbmaxlen);
3182 3183 3184 3185 3186 3187 3188
          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);
3189
        if (!show_table->s->keys_in_use.is_set(i))
3190
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3191 3192 3193
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
3194 3195
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3196 3197 3198
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3199
  DBUG_RETURN(res);
3200 3201 3202
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3203 3204 3205 3206
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)
3207 3208 3209
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
3210
  char definer[HOSTNAME_LENGTH + USERNAME_LENGTH + 2];
3211
  uint definer_len;
3212

3213 3214 3215 3216 3217 3218 3219
  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);
3220

3221 3222 3223 3224
    if (tables->with_check != VIEW_CHECK_NONE)
    {
      if (tables->with_check == VIEW_CHECK_LOCAL)
        table->field[4]->store(STRING_WITH_LEN("LOCAL"), cs);
3225
      else
3226
        table->field[4]->store(STRING_WITH_LEN("CASCADED"), cs);
3227
    }
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
    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)
3245 3246 3247
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                   thd->net.last_errno, thd->net.last_error);
  }
3248 3249
  if (res) 
    thd->clear_error();
3250
  DBUG_RETURN(0);
3251 3252 3253
}


3254 3255 3256
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)
3257 3258
{
  CHARSET_INFO *cs= system_charset_info;
3259
  restore_record(table, s->default_values);
3260 3261 3262 3263 3264
  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);
3265
  return schema_table_store_record(thd, table);
3266 3267 3268
}


gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3269
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
3270 3271 3272
					 TABLE *table, bool res,
					 const char *base_name,
					 const char *file_name)
3273
{
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3274
  DBUG_ENTER("get_schema_constraints_record");
3275 3276 3277 3278 3279 3280 3281 3282 3283
  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)
3284 3285 3286 3287
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3288
    uint primary_key= show_table->s->primary_key;
3289 3290 3291
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3292
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3293 3294
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3295 3296
        continue;

3297
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
3298 3299
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3300 3301
                              strlen(key_info->name),
                              STRING_WITH_LEN("PRIMARY KEY")))
3302 3303
          DBUG_RETURN(1);
      }
3304
      else if (key_info->flags & HA_NOSAME)
3305 3306
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3307 3308
                              strlen(key_info->name),
                              STRING_WITH_LEN("UNIQUE")))
3309 3310
          DBUG_RETURN(1);
      }
3311 3312 3313 3314 3315 3316 3317
    }

    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++))
    {
3318 3319 3320 3321 3322
      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);
3323 3324
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3325
  DBUG_RETURN(res);
3326 3327 3328
}


3329 3330 3331 3332
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,
3333
                          LEX_STRING *trigger_stmt,
3334 3335
                          ulong sql_mode,
                          LEX_STRING *definer_buffer)
3336 3337
{
  CHARSET_INFO *cs= system_charset_info;
3338 3339 3340
  byte *sql_mode_str;
  ulong sql_mode_len;

3341 3342 3343 3344 3345 3346 3347 3348
  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);
3349
  table->field[10]->store(STRING_WITH_LEN("ROW"), cs);
3350 3351
  table->field[11]->store(trg_action_time_type_names[timing].str,
                          trg_action_time_type_names[timing].length, cs);
3352 3353
  table->field[14]->store(STRING_WITH_LEN("OLD"), cs);
  table->field[15]->store(STRING_WITH_LEN("NEW"), cs);
3354 3355 3356 3357 3358

  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
3359
  table->field[17]->store((const char*)sql_mode_str, sql_mode_len, cs);
3360
  table->field[18]->store((const char *)definer_buffer->str, definer_buffer->length, cs);
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
  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;
3393
        ulong sql_mode;
3394 3395 3396
        char definer_holder[HOSTNAME_LENGTH + USERNAME_LENGTH + 2];
        LEX_STRING definer_buffer;
        definer_buffer.str= definer_holder;
3397 3398
        if (triggers->get_trigger_info(thd, (enum trg_event_type) event,
                                       (enum trg_action_time_type)timing,
3399
                                       &trigger_name, &trigger_stmt,
3400 3401
                                       &sql_mode,
                                       &definer_buffer))
3402
          continue;
3403

3404 3405
        if (store_trigger(thd, table, base_name, file_name, &trigger_name,
                         (enum trg_event_type) event,
3406
                         (enum trg_action_time_type) timing, &trigger_stmt,
3407 3408
                         sql_mode,
                         &definer_buffer))
3409 3410 3411 3412 3413 3414 3415 3416
          DBUG_RETURN(1);
      }
    }
  }
  DBUG_RETURN(0);
}


3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
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);
3427
  table->field[7]->store((longlong) idx, TRUE);
3428 3429 3430
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3431 3432 3433 3434 3435
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)
3436 3437
{
  DBUG_ENTER("get_schema_key_column_usage_record");
3438 3439 3440 3441 3442 3443 3444 3445 3446
  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)
3447 3448 3449 3450
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3451
    uint primary_key= show_table->s->primary_key;
3452 3453 3454
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3455
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3456 3457
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3458
        continue;
3459 3460 3461 3462 3463 3464 3465
      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++;
3466
          restore_record(table, s->default_values);
3467 3468 3469 3470 3471 3472
          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);
3473 3474
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
3475 3476 3477 3478 3479 3480 3481 3482 3483
        }
      }
    }

    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++))
    {
3484
      LEX_STRING *f_info;
3485
      LEX_STRING *r_info;
3486 3487 3488 3489 3490
      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++))
      {
3491
        r_info= it1++;
3492
        f_idx++;
3493
        restore_record(table, s->default_values);
3494 3495 3496 3497 3498
        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);
3499
        table->field[8]->store((longlong) f_idx, TRUE);
3500
        table->field[8]->set_notnull();
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511
        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();
3512 3513
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3514 3515 3516
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3517
  DBUG_RETURN(res);
3518 3519 3520
}


3521 3522 3523 3524 3525 3526 3527
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;
3528 3529
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
            && thd->is_fatal_error)
3530 3531 3532 3533
    DBUG_RETURN(1);

  for (; open_list ; open_list=open_list->next)
  {
3534
    restore_record(table, s->default_values);
3535 3536
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
3537 3538
    table->field[2]->store((longlong) open_list->in_use, TRUE);
    table->field[3]->store((longlong) open_list->locked, TRUE);
3539 3540
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
3541 3542 3543 3544 3545 3546 3547 3548
  }
  DBUG_RETURN(0);
}


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
3549
  int res= 0;
3550 3551
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
3552 3553 3554 3555
  pthread_mutex_lock(&LOCK_global_system_variables);
  res= show_status_array(thd, wild, init_vars, 
                         lex->option_type, 0, "", tables->table);
  pthread_mutex_unlock(&LOCK_global_system_variables);
3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
  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;
3567 3568
  ha_update_statistics();                    /* Export engines statistics */
  pthread_mutex_lock(&LOCK_status);
3569 3570
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
3571 3572 3573 3574
  res= show_status_array(thd, wild, status_vars, OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ? 
                          &tmp: &thd->status_var), "",tables->table);
  pthread_mutex_unlock(&LOCK_status);
3575 3576 3577 3578
  DBUG_RETURN(res);
}


3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
/*
  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;
3595
  for (; schema_table->table_name; schema_table++)
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
  {
    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
*/

3625
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
3626 3627 3628 3629 3630
{
  int field_count= 0;
  Item *item;
  TABLE *table;
  List<Item> field_list;
3631
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
3632
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
3633
  CHARSET_INFO *cs= system_charset_info;
3634 3635
  DBUG_ENTER("create_schema_table");

3636
  for (; fields_info->field_name; fields_info++)
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
  {
    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:
3654 3655
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
3656 3657 3658
      {
        DBUG_RETURN(0);
      }
3659
      item->max_length= fields_info->field_length * cs->mbmaxlen;
3660 3661
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
3662 3663 3664 3665 3666 3667 3668 3669 3670
      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();
3671
  tmp_table_param->table_charset= cs;
3672
  tmp_table_param->field_count= field_count;
3673
  tmp_table_param->schema_table= 1;
3674 3675 3676 3677 3678
  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),
3679
                                HA_POS_ERROR, table_list->alias)))
3680
    DBUG_RETURN(0);
3681
  table_list->schema_table_param= tmp_table_param;
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703
  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;
3704
  Name_resolution_context *context= &thd->lex->select_lex.context;
3705
  for (; field_info->field_name; field_info++)
3706 3707 3708
  {
    if (field_info->old_name)
    {
3709 3710
      Item_field *field= new Item_field(context,
                                        NullS, NullS, field_info->field_name);
3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729
      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;
3730
  Name_resolution_context *context= &sel->context;
3731 3732 3733 3734 3735

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
3736 3737
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
3738 3739 3740 3741 3742 3743
    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())
    {
3744
      buffer.append(STRING_WITH_LEN(" ("));
3745
      buffer.append(lex->wild->ptr());
3746
      buffer.append(')');
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
    }
    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;
3759
  Name_resolution_context *context= &lex->select_lex.context;
3760 3761 3762 3763 3764 3765 3766

  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())
  {
3767
    buffer.append(STRING_WITH_LEN(" ("));
3768
    buffer.append(lex->wild->ptr());
3769
    buffer.append(')');
3770
  }
3771 3772
  Item_field *field= new Item_field(context,
                                    NullS, NullS, field_info->field_name);
3773 3774 3775 3776 3777 3778 3779
  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];
3780
    field= new Item_field(context, NullS, NullS, field_info->field_name);
3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
    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)
{
3792 3793 3794
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
3795 3796
  Name_resolution_context *context= &thd->lex->select_lex.context;

3797
  for (; *field_num >= 0; field_num++)
3798
  {
3799 3800 3801 3802 3803
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
3804 3805
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
3806
    if (field)
3807
    {
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
      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;
}


3819 3820 3821 3822 3823
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;
3824 3825
  Name_resolution_context *context= &thd->lex->select_lex.context;

3826 3827 3828
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
3829 3830
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843
    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;
}


3844 3845 3846 3847 3848
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;
3849 3850
  Name_resolution_context *context= &thd->lex->select_lex.context;

3851 3852 3853
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
3854 3855
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
3856 3857 3858 3859 3860 3861 3862
    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;
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886
    }
  }
  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");
3887
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
3888 3889 3890
  {
    DBUG_RETURN(1);
  }
3891
  table->s->tmp_table= SYSTEM_TMP_TABLE;
3892
  table->grant.privilege= SELECT_ACL;
3893 3894 3895 3896 3897 3898 3899
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
3900 3901 3902 3903
  if (table_list->schema_table_name)
    table->alias_name_used= my_strcasecmp(table_alias_charset,
                                          table_list->schema_table_name,
                                          table_list->alias);
3904 3905
  table_list->table_name= table->s->table_name.str;
  table_list->table_name_length= table->s->table_name.length;
3906 3907 3908 3909
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
3910
  lex->safe_to_cache_query= 0;
3911 3912 3913 3914 3915

  if (table_list->schema_table_reformed) // show command
  {
    SELECT_LEX *sel= lex->current_select;
    Item *item;
3916
    Field_translator *transl, *org_transl;
3917 3918 3919

    if (table_list->field_translation)
    {
3920
      Field_translator *end= table_list->field_translation_end;
3921 3922 3923
      for (transl= table_list->field_translation; transl < end; transl++)
      {
        if (!transl->item->fixed &&
3924
            transl->item->fix_fields(thd, &transl->item))
3925 3926 3927 3928 3929 3930
          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
3931
          (Field_translator*)(thd->stmt_arena->
3932 3933 3934 3935 3936
                              alloc(sel->item_list.elements *
                                    sizeof(Field_translator)))))
    {
      DBUG_RETURN(1);
    }
3937
    for (org_transl= transl; (item= it++); transl++)
3938
    {
3939 3940 3941 3942
      transl->item= item;
      transl->name= item->name;
      if (!item->fixed && item->fix_fields(thd, &transl->item))
      {
3943
        DBUG_RETURN(1);
3944
      }
3945
    }
3946 3947
    table_list->field_translation= org_transl;
    table_list->field_translation_end= transl;
3948 3949
  }

3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973
  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");
3974
  /*
3975 3976 3977 3978 3979 3980 3981
     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);
3982
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993
      !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);
}


/*
3994
  Fill temporary schema tables before SELECT
3995 3996 3997 3998 3999 4000

  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
4001 4002
    FALSE success
    TRUE  error
4003 4004
*/

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4005
bool get_schema_tables_result(JOIN *join)
4006 4007 4008
{
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
  THD *thd= join->thd;
4009 4010
  LEX *lex= thd->lex;
  bool result= 0;
4011 4012 4013
  DBUG_ENTER("get_schema_tables_result");

  thd->no_warnings_for_error= 1;
4014 4015 4016 4017
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
  {  
    if (!tab->table || !tab->table->pos_in_table_list)
      break;
4018

4019
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4020
    if (table_list->schema_table && thd->fill_derived_tables())
4021
    {
4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
      if (&lex->unit != lex->current_select->master_unit()) // is subselect
      {
        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;

4032 4033
      if (table_list->schema_table->fill_table(thd, table_list,
                                               tab->select_cond))
4034
        result= 1;
4035 4036
    }
  }
4037
  thd->no_warnings_for_error= 0;
4038
  DBUG_RETURN(result);
4039 4040 4041 4042 4043
}


ST_FIELD_INFO schema_fields_info[]=
{
4044 4045
  {"CATALOG_NAME", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SCHEMA_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
4046
  {"DEFAULT_CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4047
  {"DEFAULT_COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4048
  {"SQL_PATH", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
4049
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4050 4051 4052 4053 4054
};


ST_FIELD_INFO tables_fields_info[]=
{
4055 4056 4057 4058 4059 4060 4061
  {"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"},
4062
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
4063 4064 4065 4066 4067 4068 4069 4070 4071
  {"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"},
4072
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
4073 4074
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
4075
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4076
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4077 4078 4079 4080 4081
};


ST_FIELD_INFO columns_fields_info[]=
{
4082 4083 4084 4085 4086
  {"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},
4087 4088
  {"COLUMN_DEFAULT", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Default"},
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
4089
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
4090 4091
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4092 4093
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
4094 4095
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
4096
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
4097
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
4098 4099
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
4100
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4101
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4102 4103 4104 4105 4106
};


ST_FIELD_INFO charsets_fields_info[]=
{
4107 4108
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
  {"DEFAULT_COLLATE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Default collation"},
4109
  {"DESCRIPTION", 60, MYSQL_TYPE_STRING, 0, 0, "Description"},
4110
  {"MAXLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Maxlen"},
4111
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4112 4113 4114 4115 4116
};


ST_FIELD_INFO collation_fields_info[]=
{
4117 4118
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
4119
  {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"},
4120 4121
  {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"},
  {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"},
4122
  {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"},
4123
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4124 4125 4126 4127 4128
};


ST_FIELD_INFO coll_charset_app_fields_info[]=
{
4129 4130
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
4131
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4132 4133 4134 4135 4136
};


ST_FIELD_INFO proc_fields_info[]=
{
4137 4138 4139 4140 4141
  {"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"},
4142 4143
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4144
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4145 4146
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
4147
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4148 4149 4150 4151
  {"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"},
4152 4153
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
4154
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
4155
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
4156
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
4157
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4158 4159 4160 4161 4162
};


ST_FIELD_INFO stat_fields_info[]=
{
4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178
  {"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}
4179 4180 4181 4182 4183
};


ST_FIELD_INFO view_fields_info[]=
{
4184 4185 4186 4187
  {"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},
4188
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
4189
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
4190 4191
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, 0},
4192
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4193 4194 4195 4196 4197
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
4198 4199 4200 4201 4202
  {"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}
4203 4204 4205 4206 4207
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
4208 4209 4210 4211 4212 4213
  {"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}
4214 4215 4216 4217 4218
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
4219 4220 4221 4222 4223 4224 4225
  {"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}
4226 4227 4228 4229 4230
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
4231 4232 4233 4234 4235 4236 4237 4238
  {"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}
4239 4240 4241 4242 4243
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
4244 4245 4246 4247 4248 4249 4250
  {"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}
4251 4252 4253 4254 4255
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
4256 4257 4258
  {"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},
4259
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
4260 4261 4262 4263
  {"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},
4264
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
4265 4266 4267
  {"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},
4268
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
4269 4270 4271 4272 4273
};


ST_FIELD_INFO table_names_fields_info[]=
{
4274 4275 4276 4277 4278
  {"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}
4279 4280 4281
};


4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
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}
};


4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
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"},
4311
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, "sql_mode"},
4312
  {"DEFINER", 65535, MYSQL_TYPE_STRING, 0, 0, "Definer"},
4313 4314 4315 4316
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


4317 4318 4319 4320 4321 4322 4323 4324
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}
};


4325 4326 4327
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
4328
  {"PLUGIN_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
4329 4330
  {"PLUGIN_STATUS", 10, MYSQL_TYPE_STRING, 0, 0, "Status"},
  {"PLUGIN_TYPE", 10, MYSQL_TYPE_STRING, 0, 0, "Type"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4331
  {"PLUGIN_TYPE_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
4332
  {"PLUGIN_LIBRARY", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Library"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4333
  {"PLUGIN_LIBRARY_VERSION", 20, MYSQL_TYPE_STRING, 0, 1, 0},
4334 4335 4336 4337 4338 4339
  {"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}
};


4340 4341 4342 4343 4344 4345 4346
/*
  Description of ST_FIELD_INFO in table.h
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
4347
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
4348
  {"COLLATIONS", collation_fields_info, create_schema_table, 
4349
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
4350
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
4351
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
4352 4353
  {"COLUMNS", columns_fields_info, create_schema_table, 
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
4354
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
4355
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
4356
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
4357
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
4358 4359
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
4360 4361
  {"PLUGINS", plugin_fields_info, create_schema_table,
    fill_plugins, make_old_format, 0, -1, -1, 0},
4362 4363 4364 4365 4366 4367 4368 4369
  {"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},
4370 4371
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
4372 4373 4374 4375 4376 4377 4378 4379
  {"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},
4380
  {"TRIGGERS", triggers_fields_info, create_schema_table,
4381
   get_all_tables, make_old_format, get_schema_triggers_record, 5, 6, 0},
4382 4383
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
4384 4385 4386 4387
  {"VIEWS", view_fields_info, create_schema_table, 
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
4388
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
4389 4390 4391
};


4392
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4393
template class List_iterator_fast<char>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4394 4395
template class List<char>;
#endif