sql_show.cc 193 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
   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
5
   the Free Software Foundation; version 2 of the License.
6

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7 8 9 10
   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.
11

bk@work.mysql.com's avatar
bk@work.mysql.com committed
12 13 14 15 16 17 18 19
   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"
20
#include "sql_select.h"                         // For select_describe
21
#include "sql_show.h"
22
#include "repl_failsafe.h"
23
#include "sp.h"
24
#include "sp_head.h"
25
#include "sql_trigger.h"
brian@grrr.local's avatar
brian@grrr.local committed
26
#include "authors.h"
27
#include "contributors.h"
28
#include "events.h"
29
#include "event_data_objects.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
30
#include <my_dir.h>
31

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

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
enum enum_i_s_events_fields
{
  ISE_EVENT_CATALOG= 0,
  ISE_EVENT_SCHEMA,
  ISE_EVENT_NAME,
  ISE_DEFINER,
  ISE_EVENT_BODY,
  ISE_EVENT_DEFINITION,
  ISE_EVENT_TYPE,
  ISE_EXECUTE_AT,
  ISE_INTERVAL_VALUE,
  ISE_INTERVAL_FIELD,
  ISE_SQL_MODE,
  ISE_STARTS,
  ISE_ENDS,
  ISE_STATUS,
  ISE_ON_COMPLETION,
  ISE_CREATED,
  ISE_LAST_ALTERED,
  ISE_LAST_EXECUTED,
  ISE_EVENT_COMMENT
};


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

64
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
65
static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
66
                               "grant_types",
67
                               grant_names, NULL};
68
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
69

70 71 72
static void store_key_options(THD *thd, String *packet, TABLE *table,
                              KEY *key_info);

73 74
static void
append_algorithm(TABLE_LIST *table, String *buff);
75

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

77
/***************************************************************************
78
** List all table types supported
79 80
***************************************************************************/

81 82 83 84 85
static my_bool show_handlerton(THD *thd, st_plugin_int *plugin,
                               void *arg)
{
  handlerton *default_type= (handlerton *) arg;
  Protocol *protocol= thd->protocol;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
86
  handlerton *hton= (handlerton *)plugin->data;
87 88 89 90

  if (!(hton->flags & HTON_HIDDEN))
  {
    protocol->prepare_for_resend();
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
91 92
    protocol->store(plugin->name.str, plugin->name.length,
                    system_charset_info);
93 94 95 96 97
    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);
98
    protocol->store(plugin->plugin->descr, system_charset_info);
99 100 101
    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);
102

103 104
    return protocol->write() ? 1 : 0;
  }
105
  return 0;
106 107
}

108
bool mysqld_show_storage_engines(THD *thd)
109 110
{
  List<Item> field_list;
111
  Protocol *protocol= thd->protocol;
112
  DBUG_ENTER("mysqld_show_storage_engines");
113

monty@mishka.local's avatar
monty@mishka.local committed
114
  field_list.push_back(new Item_empty_string("Engine",10));
115
  field_list.push_back(new Item_empty_string("Support",10));
116
  field_list.push_back(new Item_empty_string("Comment",80));
117 118 119
  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));
120

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

125
  if (plugin_foreach(thd, show_handlerton,
126 127
                     MYSQL_STORAGE_ENGINE_PLUGIN, thd->variables.table_type))
    DBUG_RETURN(TRUE);
128

129
  send_eof(thd);
130
  DBUG_RETURN(FALSE);
131 132
}

133 134
static int make_version_string(char *buf, int buf_length, uint version)
{
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
135
  return my_snprintf(buf, buf_length, "%d.%d", version>>8,version&0xff);
136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

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
150 151 152 153 154

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

serg@sergbook.mysql.com's avatar
fixes  
serg@sergbook.mysql.com committed
155

156 157 158 159
  switch (plugin->state)
  {
  /* case PLUGIN_IS_FREED: does not happen */
  case PLUGIN_IS_DELETED:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
160
    table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
161 162
    break;
  case PLUGIN_IS_UNINITIALIZED:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
163
    table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
164 165
    break;
  case PLUGIN_IS_READY:
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
166
    table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
167 168 169 170 171
    break;
  default:
    DBUG_ASSERT(0);
  }

serg@serg.mylan's avatar
serg@serg.mylan committed
172 173 174
  table->field[3]->store(plugin_type_names[plug->type].str,
                         plugin_type_names[plug->type].length,
                         cs);
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
175
  table->field[4]->store(version_buf,
serg@serg.mylan's avatar
serg@serg.mylan committed
176
        make_version_string(version_buf, sizeof(version_buf),
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
177
                            *(uint *)plug->info), cs);
178 179 180

  if (plugin->plugin_dl)
  {
serg@serg.mylan's avatar
serg@serg.mylan committed
181
    table->field[5]->store(plugin->plugin_dl->dl.str,
182 183
                           plugin->plugin_dl->dl.length, cs);
    table->field[5]->set_notnull();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
184
    table->field[6]->store(version_buf,
serg@serg.mylan's avatar
serg@serg.mylan committed
185
          make_version_string(version_buf, sizeof(version_buf),
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
186 187 188
                              plugin->plugin_dl->version),
          cs);
    table->field[6]->set_notnull();
189 190
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
191
  {
192
    table->field[5]->set_null();
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
193 194 195 196
    table->field[6]->set_null();
  }


197 198
  if (plug->author)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
199 200
    table->field[7]->store(plug->author, strlen(plug->author), cs);
    table->field[7]->set_notnull();
201 202
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
203
    table->field[7]->set_null();
204 205 206

  if (plug->descr)
  {
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
207 208
    table->field[8]->store(plug->descr, strlen(plug->descr), cs);
    table->field[8]->set_notnull();
209 210
  }
  else
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
211
    table->field[8]->set_null();
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  switch (plug->license) {
  case PLUGIN_LICENSE_GPL:
    table->field[9]->store(PLUGIN_LICENSE_GPL_STRING, 
                           strlen(PLUGIN_LICENSE_GPL_STRING), cs);
    break;
  case PLUGIN_LICENSE_BSD:
    table->field[9]->store(PLUGIN_LICENSE_BSD_STRING, 
                           strlen(PLUGIN_LICENSE_BSD_STRING), cs);
    break;
  default:
    table->field[9]->store(PLUGIN_LICENSE_PROPRIETARY_STRING, 
                           strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
    break;
  }
  table->field[9]->set_notnull();

229 230 231 232 233 234 235 236 237
  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;

238 239
  if (plugin_foreach_with_mask(thd, show_plugins, MYSQL_ANY_PLUGIN,
                               ~PLUGIN_IS_FREED, table))
240
    DBUG_RETURN(1);
241

242 243 244 245
  DBUG_RETURN(0);
}


brian@grrr.local's avatar
brian@grrr.local committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/***************************************************************************
** 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);
}
278

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

/***************************************************************************
** List all Contributors.
** Please get permission before updating
***************************************************************************/

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

  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_contributors_st *contributors;
  for (contributors= show_table_contributors; contributors->name; contributors++)
  {
    protocol->prepare_for_resend();
    protocol->store(contributors->name, system_charset_info);
    protocol->store(contributors->location, system_charset_info);
    protocol->store(contributors->comment, system_charset_info);
    if (protocol->write())
      DBUG_RETURN(TRUE);
  }
  send_eof(thd);
  DBUG_RETURN(FALSE);
}


314
/***************************************************************************
315
 List all privileges supported
316 317
***************************************************************************/

318 319 320 321
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
322 323
};

324 325
static struct show_privileges_st sys_privileges[]=
{
326
  {"Alter", "Tables",  "To alter the table"},
327
  {"Alter routine", "Functions,Procedures",  "To alter or drop stored functions/procedures"},
328
  {"Create", "Databases,Tables,Indexes",  "To create new databases and tables"},
329
  {"Create routine","Functions,Procedures","To use CREATE FUNCTION/PROCEDURE"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
330 331
  {"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
  {"Create view", "Tables",  "To create new views"},
332
  {"Create user", "Server Admin",  "To create new users"},
333
  {"Delete", "Tables",  "To delete existing rows"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
334
  {"Drop", "Databases,Tables", "To drop databases, tables, and views"},
335
  {"Event","Server Admin","To create, alter, drop and execute events"},
336
  {"Execute", "Functions,Procedures", "To execute stored routines"},
337
  {"File", "File access on server",   "To read and write files on the server"},
338
  {"Grant option",  "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
339 340 341 342
  {"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"},
343
  {"References", "Databases,Tables", "To have references on tables"},
344 345 346 347 348
  {"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
349 350
  {"Show view","Tables","To see views with SHOW CREATE VIEW"},
  {"Shutdown","Server Admin", "To shut down the server"},
351
  {"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
352
  {"Trigger","Tables", "To use triggers"},
353 354
  {"Update", "Tables",  "To update existing rows"},
  {"Usage","Server Admin","No privileges - allow connect only"},
355 356 357
  {NullS, NullS, NullS}
};

358
bool mysqld_show_privileges(THD *thd)
359 360
{
  List<Item> field_list;
361
  Protocol *protocol= thd->protocol;
362 363 364 365 366 367
  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));

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

372 373
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
374
  {
375
    protocol->prepare_for_resend();
376 377 378
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
379
    if (protocol->write())
380
      DBUG_RETURN(TRUE);
381
  }
382
  send_eof(thd);
383
  DBUG_RETURN(FALSE);
384 385 386 387
}


/***************************************************************************
388
  List all column types
389 390
***************************************************************************/

391 392
struct show_column_type_st
{
393 394
  const char *type;
  uint size;
395 396 397 398 399 400 401 402 403 404 405 406
  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;
407
};
408 409 410 411 412

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
413 414
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
415 416
    "NO",   "YES", "YES",  "NO",  "NULL,0",
    "A very small integer"},
417
  {"tinyint unsigned",
418 419
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",
    "YES",  "YES",  "YES",  "NO",  "NULL,0",
420 421 422
    "A very small integer"},
};

423
bool mysqld_show_column_types(THD *thd)
424 425
{
  List<Item> field_list;
426
  Protocol *protocol= thd->protocol;
427 428 429 430 431 432
  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));
433 434
  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));
435 436 437 438 439 440 441 442 443
  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));

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

448
  /* TODO: Change the loop to not use 'i' */
449 450
  for (uint i=0; i < sizeof(sys_column_types)/sizeof(sys_column_types[0]); i++)
  {
451
    protocol->prepare_for_resend();
452
    protocol->store(sys_column_types[i].type, system_charset_info);
453
    protocol->store((ulonglong) sys_column_types[i].size);
454 455
    protocol->store(sys_column_types[i].min_value, system_charset_info);
    protocol->store(sys_column_types[i].max_value, system_charset_info);
456 457
    protocol->store_short((longlong) sys_column_types[i].precision);
    protocol->store_short((longlong) sys_column_types[i].scale);
458 459 460 461 462 463 464 465
    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);
466
    if (protocol->write())
467
      DBUG_RETURN(TRUE);
468
  }
469
  send_eof(thd);
470
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
471 472 473
}


474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/*
  find_files() - find files in a given directory.

  SYNOPSIS
    find_files()
    thd                 thread handler
    files               put found files in this list
    db                  database name to set in TABLE_LIST structure
    path                path to database
    wild                filter for found files
    dir                 read databases in path if TRUE, read .frm files in
                        database otherwise

  RETURN
    FIND_FILES_OK       success
    FIND_FILES_OOM      out of memory error
    FIND_FILES_DIR      no such directory, or directory can't be read
*/


find_files_result
find_files(THD *thd, List<char> *files, const char *db,
           const char *path, const char *wild, bool dir)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
497 498 499 500 501
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
502
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
503
  uint col_access=thd->col_access;
504
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
505
  TABLE_LIST table_list;
506
  DBUG_ENTER("find_files");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
507

508 509
  if (wild && !wild[0])
    wild=0;
510

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

513 514 515 516 517 518
  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);
519
    DBUG_RETURN(FIND_FILES_DIR);
520
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
521

522
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
523
  {
524
    char uname[NAME_LEN*3+1];                   /* Unencoded name */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
525 526
    file=dirp->dir_entry+i;
    if (dir)
527
    {                                           /* Return databases */
528 529 530 531
      if ((file->name[0] == '.' && 
          ((file->name[1] == '.' && file->name[2] == '\0') ||
            file->name[1] == '\0')))
        continue;                               /* . or .. */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
532 533
#ifdef USE_SYMDIR
      char *ext;
534
      char buff[FN_REFLEN];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
535
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
536 537
      {
	/* Only show the sym file if it points to a directory */
538
	char *end;
539
        *ext=0;                                 /* Remove extension */
540 541 542 543
	unpack_dirname(buff, file->name);
	end= strend(buff);
	if (end != buff && end[-1] == FN_LIBCHAR)
	  end[-1]= 0;				// Remove end FN_LIBCHAR
544 545 546
        if (!my_stat(buff, file->mystat, MYF(0)))
               continue;
       }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
547
#endif
548 549
      if (!MY_S_ISDIR(file->mystat->st_mode))
        continue;
550
      VOID(filename_to_tablename(file->name, uname, sizeof(uname)));
551
      if (wild && wild_compare(uname, wild, 0))
552 553
        continue;
      file->name= uname;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
554 555 556
    }
    else
    {
557
        // Return only .frm files which aren't temp files.
558
      if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),reg_ext) ||
559
          is_prefix(file->name, tmp_file_prefix))
560
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
561
      *ext=0;
562 563
      VOID(filename_to_tablename(file->name, uname, sizeof(uname)));
      file->name= uname;
564 565 566 567
      if (wild)
      {
	if (lower_case_table_names)
	{
568
	  if (wild_case_compare(files_charset_info, file->name, wild))
569 570
	    continue;
	}
571
	else if (wild_compare(file->name,wild,0))
572 573
	  continue;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
574
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
575
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
576 577 578 579
    /* Don't show tables where we don't have any privileges */
    if (db && !(col_access & TABLE_ACLS))
    {
      table_list.db= (char*) db;
580
      table_list.db_length= strlen(db);
581
      table_list.table_name= file->name;
582
      table_list.table_name_length= strlen(file->name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
583
      table_list.grant.privilege=col_access;
584
      if (check_grant(thd, TABLE_ACLS, &table_list, 1, 1, 1))
585
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
586
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
587
#endif
588
    if (files->push_back(thd->strdup(file->name)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
589 590
    {
      my_dirend(dirp);
591
      DBUG_RETURN(FIND_FILES_OOM);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
592 593 594 595
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
596 597 598

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

599
  DBUG_RETURN(FIND_FILES_OK);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
600 601
}

602

603
bool
bk@work.mysql.com's avatar
bk@work.mysql.com committed
604 605
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
606 607 608
  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
609 610
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
611
                      table_list->table_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
612

613 614 615
  /* 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
616
  /* Only one table for now, but VIEW can involve several tables */
617
  if (open_normal_and_derived_tables(thd, table_list, 0))
618 619 620
  {
    if (!table_list->view || thd->net.last_errno != ER_VIEW_INVALID)
      DBUG_RETURN(TRUE);
621

622 623 624 625 626 627
    /*
      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);
628 629
    thd->clear_error();

630 631 632 633 634 635
    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);
  }
636

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
637 638 639
  /* TODO: add environment variables show when it become possible */
  if (thd->lex->only_view && !table_list->view)
  {
640
    my_error(ER_WRONG_OBJECT, MYF(0),
641
             table_list->db, table_list->table_name, "VIEW");
642
    DBUG_RETURN(TRUE);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
643
  }
644

645
  buffer.length(0);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
646 647
  if ((table_list->view ?
       view_store_create_info(thd, table_list, &buffer) :
648
       store_create_info(thd, table_list, &buffer, NULL)))
649
    DBUG_RETURN(TRUE);
650

bk@work.mysql.com's avatar
bk@work.mysql.com committed
651
  List<Item> field_list;
652 653 654 655 656 657 658 659 660 661 662 663 664
  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
665

666 667
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
668
    DBUG_RETURN(TRUE);
669
  protocol->prepare_for_resend();
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
670 671 672 673
  if (table_list->view)
    protocol->store(table_list->view_name.str, system_charset_info);
  else
  {
674
    if (table_list->schema_table)
675 676
      protocol->store(table_list->schema_table->table_name,
                      system_charset_info);
677
    else
678
      protocol->store(table_list->table->alias, system_charset_info);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
679
  }
680
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
681

682
  if (protocol->write())
683
    DBUG_RETURN(TRUE);
684
  send_eof(thd);
685
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
686 687
}

688 689
bool mysqld_show_create_db(THD *thd, char *dbname,
                           HA_CREATE_INFO *create_info)
690
{
691
  Security_context *sctx= thd->security_ctx;
692 693
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
694
#ifndef NO_EMBEDDED_ACCESS_CHECKS
695
  uint db_access;
696
#endif
697 698 699 700 701 702
  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
703
  if (test_all_bits(sctx->master_access, DB_ACLS))
704 705
    db_access=DB_ACLS;
  else
706 707
    db_access= (acl_get(sctx->host, sctx->ip, sctx->priv_user, dbname, 0) |
		sctx->master_access);
708 709
  if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname)))
  {
710
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
711
             sctx->priv_user, sctx->host_or_ip, dbname);
712 713
    general_log_print(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR),
                      sctx->priv_user, sctx->host_or_ip, dbname);
714
    DBUG_RETURN(TRUE);
715 716
  }
#endif
717 718
  if (!my_strcasecmp(system_charset_info, dbname,
                     information_schema_name.str))
719
  {
720 721
    dbname= information_schema_name.str;
    create.default_table_charset= system_charset_info;
722
  }
723
  else
724
  {
725
    if (check_db_dir_existence(dbname))
726 727 728 729
    {
      my_error(ER_BAD_DB_ERROR, MYF(0), dbname);
      DBUG_RETURN(TRUE);
    }
730 731

    load_db_opt_by_name(thd, dbname, &create);
732 733 734 735 736
  }
  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));

737 738
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
739
    DBUG_RETURN(TRUE);
740 741 742 743

  protocol->prepare_for_resend();
  protocol->store(dbname, strlen(dbname), system_charset_info);
  buffer.length(0);
744
  buffer.append(STRING_WITH_LEN("CREATE DATABASE "));
745
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
746
    buffer.append(STRING_WITH_LEN("/*!32312 IF NOT EXISTS*/ "));
747 748 749 750
  append_identifier(thd, &buffer, dbname, strlen(dbname));

  if (create.default_table_charset)
  {
751 752
    buffer.append(STRING_WITH_LEN(" /*!40100"));
    buffer.append(STRING_WITH_LEN(" DEFAULT CHARACTER SET "));
753 754 755
    buffer.append(create.default_table_charset->csname);
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
    {
756
      buffer.append(STRING_WITH_LEN(" COLLATE "));
757 758
      buffer.append(create.default_table_charset->name);
    }
759
    buffer.append(STRING_WITH_LEN(" */"));
760 761 762 763
  }
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());

  if (protocol->write())
764
    DBUG_RETURN(TRUE);
765
  send_eof(thd);
766
  DBUG_RETURN(FALSE);
767
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
768

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
769 770


bk@work.mysql.com's avatar
bk@work.mysql.com committed
771
/****************************************************************************
772 773
  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
774 775 776 777 778 779 780
****************************************************************************/

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

783
  if (open_normal_and_derived_tables(thd, table_list, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
784
    DBUG_VOID_RETURN;
785 786
  table= table_list->table;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
787 788 789 790 791
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
792 793
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
794 795 796 797 798 799 800 801
    {
      if (table_list->view)
        field_list.push_back(new Item_ident_for_show(field,
                                                     table_list->view_db.str,
                                                     table_list->view_name.str));
      else
        field_list.push_back(new Item_field(field));
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
802
  }
803
  restore_record(table, s->default_values);              // Get empty record
804
  table->use_all_columns();
805 806
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS |
                                              Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
807
    DBUG_VOID_RETURN;
808
  thd->protocol->flush();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
809 810 811
  DBUG_VOID_RETURN;
}

812

bk@work.mysql.com's avatar
bk@work.mysql.com committed
813
int
814
mysqld_dump_create_info(THD *thd, TABLE_LIST *table_list, int fd)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
815
{
816 817
  Protocol *protocol= thd->protocol;
  String *packet= protocol->storage_packet();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
818
  DBUG_ENTER("mysqld_dump_create_info");
819
  DBUG_PRINT("enter",("table: %s",table_list->table->s->table_name.str));
820

821
  protocol->prepare_for_resend();
822
  if (store_create_info(thd, table_list, packet, NULL))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
823
    DBUG_RETURN(-1);
824

825
  if (fd < 0)
826
  {
827
    if (protocol->write())
828
      DBUG_RETURN(-1);
829
    protocol->flush();
830
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
831
  else
832
  {
833 834
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
835 836
      DBUG_RETURN(-1);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837 838
  DBUG_RETURN(0);
}
839

840
/*
841
  Go through all character combinations and ensure that sql_lex.cc can
842
  parse it as an identifier.
843 844

  SYNOPSIS
845 846 847 848 849 850 851
  require_quotes()
  name			attribute name
  name_length		length of name

  RETURN
    #	Pointer to conflicting character
    0	No conflicting character
852 853
*/

854
static const char *require_quotes(const char *name, uint name_length)
855
{
856
  uint length;
857
  bool pure_digit= TRUE;
858 859
  const char *end= name + name_length;

860
  for (; name < end ; name++)
861
  {
862 863 864 865
    uchar chr= (uchar) *name;
    length= my_mbcharlen(system_charset_info, chr);
    if (length == 1 && !system_charset_info->ident_map[chr])
      return name;
866 867
    if (length == 1 && (chr < '0' || chr > '9'))
      pure_digit= FALSE;
868
  }
869 870
  if (pure_digit)
    return name;
871 872
  return 0;
}
873

874

875 876 877 878 879 880 881 882 883 884 885 886
/*
  Quote the given identifier if needed and append it to the target string.
  If the given identifier is empty, it will be quoted.

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

887 888
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
889
{
890 891
  const char *name_end;
  char quote_char;
892
  int q= get_quote_char_for_identifier(thd, name, length);
893

894
  if (q == EOF)
895
  {
896
    packet->append(name, length, system_charset_info);
897 898 899
    return;
  }

900 901 902 903
  /*
    The identifier must be quoted as it includes a quote character or
   it's a keyword
  */
904

905
  VOID(packet->reserve(length*2 + 2));
906
  quote_char= (char) q;
907 908 909 910
  packet->append(&quote_char, 1, system_charset_info);

  for (name_end= name+length ; name < name_end ; name+= length)
  {
911
    uchar chr= (uchar) *name;
912
    length= my_mbcharlen(system_charset_info, chr);
913 914 915 916 917 918 919 920 921
    /*
      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;
922
    if (length == 1 && chr == (uchar) quote_char)
923 924
      packet->append(&quote_char, 1, system_charset_info);
    packet->append(name, length, packet->charset());
925
  }
926
  packet->append(&quote_char, 1, system_charset_info);
927 928
}

929

930 931 932 933 934 935 936 937 938 939
/*
  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
940 941 942 943 944
    Force quoting in the following cases:
      - name is empty (for one, it is possible when we use this function for
        quoting user and host names for DEFINER clause);
      - name is a keyword;
      - name includes a special character;
945 946 947 948 949 950 951
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
    is set.

  RETURN
    EOF	  No quote character is needed
    #	  Quote character
*/
952 953 954

int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
{
anozdrin@mysql.com's avatar
anozdrin@mysql.com committed
955
  if (length &&
956
      !is_keyword(name,length) &&
957 958 959
      !require_quotes(name, length) &&
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
    return EOF;
960
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
961
    return '"';
962
  return '`';
963 964 965
}


966 967 968 969 970
/* 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
971
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
972
  {
973
    uint length= dirname_length(filename);
974 975
    packet->append(' ');
    packet->append(dir_type);
976
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
977
#ifdef __WIN__
monty@mysql.com's avatar
monty@mysql.com committed
978 979 980 981 982 983 984 985 986
    /* 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;
987
#endif
monty@mysql.com's avatar
monty@mysql.com committed
988
    packet->append(filename, length);
989 990 991 992 993
    packet->append('\'');
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
994
#define LIST_PROCESS_HOST_LEN 64
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
/*
  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
 */
1018 1019 1020

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
1021
{
1022
  List<Item> field_list;
1023
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end;
1024
  const char *alias;
1025
  String type(tmp, sizeof(tmp), system_charset_info);
1026 1027 1028
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
1029
  TABLE *table= table_list->table;
1030
  handler *file= table->file;
1031
  TABLE_SHARE *share= table->s;
1032
  HA_CREATE_INFO create_info;
1033
  bool show_table_options= FALSE;
1034 1035 1036 1037 1038 1039
  bool foreign_db_mode=  (thd->variables.sql_mode & (MODE_POSTGRESQL |
                                                     MODE_ORACLE |
                                                     MODE_MSSQL |
                                                     MODE_DB2 |
                                                     MODE_MAXDB |
                                                     MODE_ANSI)) != 0;
msvensson@neptunus.(none)'s avatar
msvensson@neptunus.(none) committed
1040 1041 1042
  bool limited_mysql_mode= (thd->variables.sql_mode & (MODE_NO_FIELD_OPTIONS |
                                                       MODE_MYSQL323 |
                                                       MODE_MYSQL40)) != 0;
1043
  my_bitmap_map *old_map;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1044
  DBUG_ENTER("store_create_info");
1045
  DBUG_PRINT("enter",("table: %s", table->s->table_name.str));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1046

1047
  restore_record(table, s->default_values); // Get empty record
1048

1049
  if (share->tmp_table)
1050
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
1051
  else
1052
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
1053 1054 1055
  if (create_info_arg &&
      (create_info_arg->options & HA_LEX_CREATE_IF_NOT_EXISTS))
    packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
1056
  if (table_list->schema_table)
1057
    alias= table_list->schema_table->table_name;
1058
  else
1059 1060 1061 1062 1063 1064 1065 1066
  {
    if (lower_case_table_names == 2)
      alias= table->alias;
    else
    {
      alias= share->table_name.str;
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
1067
  append_identifier(thd, packet, alias, strlen(alias));
1068
  packet->append(STRING_WITH_LEN(" (\n"));
1069 1070 1071 1072 1073 1074
  /*
    We need this to get default values from the table
    We have to restore the read_set if we are called from insert in case
    of row based replication.
  */
  old_map= tmp_use_all_columns(table, table->read_set);
1075

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1076 1077
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
1078
    bool has_default;
1079
    bool has_now_default;
1080 1081
    uint flags = field->flags;

1082
    if (ptr != table->field)
1083
      packet->append(STRING_WITH_LEN(",\n"));
1084

1085
    packet->append(STRING_WITH_LEN("  "));
1086
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1087 1088
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
1089
    if (type.ptr() != tmp)
1090
      type.set(tmp, sizeof(tmp), system_charset_info);
1091 1092
    else
      type.set_charset(system_charset_info);
1093

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

1097 1098
    if (field->has_charset() && 
        !(thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)))
1099
    {
1100
      if (field->charset() != share->table_charset)
1101
      {
1102
	packet->append(STRING_WITH_LEN(" CHARACTER SET "));
1103 1104 1105 1106 1107 1108 1109 1110
	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))
      {
1111
	packet->append(STRING_WITH_LEN(" COLLATE "));
1112
	packet->append(field->charset()->name);
1113
      }
1114
    }
1115

1116
    if (flags & NOT_NULL_FLAG)
1117
      packet->append(STRING_WITH_LEN(" NOT NULL"));
1118
    else if (field->type() == MYSQL_TYPE_TIMESTAMP)
1119 1120 1121 1122 1123
    {
      /*
        TIMESTAMP field require explicit NULL flag, because unlike
        all other fields they are treated as NOT NULL by default.
      */
1124
      packet->append(STRING_WITH_LEN(" NULL"));
1125
    }
1126 1127

    /* 
1128
      Again we are using CURRENT_TIMESTAMP instead of NOW because it is
1129 1130 1131 1132 1133
      more standard 
    */
    has_now_default= table->timestamp_field == field && 
                     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
    
1134
    has_default= (field->type() != MYSQL_TYPE_BLOB &&
1135
                  !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
1136
		  field->unireg_check != Field::NEXT_NUMBER &&
1137 1138
                  !((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
		    && has_now_default));
1139

1140
    if (has_default)
1141
    {
1142
      packet->append(STRING_WITH_LEN(" DEFAULT "));
1143
      if (has_now_default)
1144
        packet->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
1145
      else if (!field->is_null())
1146
      {                                             // Not null by default
1147
        type.set(tmp, sizeof(tmp), field->charset());
1148
        field->val_str(&type);
1149
	if (type.length())
1150
	{
1151
	  String def_val;
1152
          uint dummy_errors;
1153 1154
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
1155
		       system_charset_info, &dummy_errors);
1156 1157
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
1158
        else
1159
	  packet->append(STRING_WITH_LEN("''"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1160
      }
1161
      else if (field->maybe_null())
1162
        packet->append(STRING_WITH_LEN("NULL"));    // Null as default
1163
      else
1164
        packet->append(tmp);
1165
    }
1166

1167
    if (!limited_mysql_mode && table->timestamp_field == field && 
1168
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
1169
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
1170

1171 1172
    if (field->unireg_check == Field::NEXT_NUMBER && 
        !(thd->variables.sql_mode & MODE_NO_FIELD_OPTIONS))
1173
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
1174 1175 1176

    if (field->comment.length)
    {
1177
      packet->append(STRING_WITH_LEN(" COMMENT "));
1178 1179
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1180 1181
  }

1182
  key_info= table->key_info;
1183
  bzero((char*) &create_info, sizeof(create_info));
1184
  file->update_create_info(&create_info);
1185
  primary_key= share->primary_key;
1186

1187
  for (uint i=0 ; i < share->keys ; i++,key_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1188
  {
1189 1190
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
1191
    packet->append(STRING_WITH_LEN(",\n  "));
1192

1193
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
1194 1195
    {
      found_primary=1;
1196 1197 1198 1199 1200
      /*
        No space at end, because a space will be added after where the
        identifier would go, but that is not added for primary key.
      */
      packet->append(STRING_WITH_LEN("PRIMARY KEY"));
1201
    }
1202
    else if (key_info->flags & HA_NOSAME)
1203
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
1204
    else if (key_info->flags & HA_FULLTEXT)
1205
      packet->append(STRING_WITH_LEN("FULLTEXT KEY "));
1206
    else if (key_info->flags & HA_SPATIAL)
1207 1208 1209
      packet->append(STRING_WITH_LEN("SPATIAL KEY "));
    else
      packet->append(STRING_WITH_LEN("KEY "));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1210

1211
    if (!found_primary)
1212
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
1213

1214
    packet->append(STRING_WITH_LEN(" ("));
1215

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1216 1217
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
1218
      if (j)
1219
        packet->append(',');
1220

1221
      if (key_part->field)
1222 1223
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
1224
      if (key_part->field &&
1225 1226 1227
          (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
1228
      {
1229
        char *end;
1230
        buff[0] = '(';
1231 1232 1233
        end= int10_to_str((long) key_part->length /
                          key_part->field->charset()->mbmaxlen,
                          buff + 1,10);
1234 1235
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1236 1237 1238
      }
    }
    packet->append(')');
1239
    store_key_options(thd, packet, table, key_info);
1240 1241
    if (key_info->parser)
    {
1242
      packet->append(STRING_WITH_LEN(" /*!50100 WITH PARSER "));
1243 1244
      append_identifier(thd, packet, key_info->parser->name.str,
                        key_info->parser->name.length);
1245
      packet->append(STRING_WITH_LEN(" */ "));
1246
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1247
  }
1248

1249 1250 1251 1252
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
1253

1254
  if ((for_str= file->get_foreign_key_create_info()))
1255 1256 1257
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
1258 1259
  }

1260
  packet->append(STRING_WITH_LEN("\n)"));
1261
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
1262
  {
1263
    show_table_options= TRUE;
1264 1265 1266 1267 1268
    /*
      Get possible table space definitions and append them
      to the CREATE TABLE statement
    */

1269
    if ((for_str= file->get_tablespace_name(thd,0,0)))
1270
    {
1271
      packet->append(STRING_WITH_LEN(" /*!50100 TABLESPACE "));
1272
      packet->append(for_str, strlen(for_str));
1273
      packet->append(STRING_WITH_LEN(" STORAGE DISK */"));
1274 1275 1276
      my_free(for_str, MYF(0));
    }

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    /*
      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="));
1288
#ifdef WITH_PARTITION_STORAGE_ENGINE
1289
    if (table->part_info)
1290 1291
      packet->append(ha_resolve_storage_engine_name(
                        table->part_info->default_engine_type));
1292
    else
1293
      packet->append(file->table_type());
1294
#else
1295
      packet->append(file->table_type());
1296
#endif
1297
    }
1298

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    /*
      Add AUTO_INCREMENT=... if there is an AUTO_INCREMENT column,
      and NEXT_ID > 1 (the default).  We must not print the clause
      for engines that do not support this as it would break the
      import of dumps, but as of this writing, the test for whether
      AUTO_INCREMENT columns are allowed and wether AUTO_INCREMENT=...
      is supported is identical, !(file->table_flags() & HA_NO_AUTO_INCREMENT))
      Because of that, we do not explicitly test for the feature,
      but may extrapolate its existence from that of an AUTO_INCREMENT column.
    */

    if(create_info.auto_increment_value > 1)
    {
1312
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT="));
1313 1314 1315
      end= longlong10_to_str(create_info.auto_increment_value, buff,10);
      packet->append(buff, (uint) (end - buff));
    }
1316

1317
    
1318
    if (share->table_charset &&
1319 1320
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
1321
    {
1322 1323 1324 1325 1326 1327
      /*
        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))
1328
      {
1329 1330 1331 1332 1333 1334 1335
        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);
        }
1336
      }
1337
    }
1338

1339
    if (share->min_rows)
1340
    {
1341
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
1342
      end= longlong10_to_str(share->min_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1343
      packet->append(buff, (uint) (end- buff));
1344
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1345

1346
    if (share->max_rows && !table_list->schema_table)
1347
    {
1348
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
1349
      end= longlong10_to_str(share->max_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1350
      packet->append(buff, (uint) (end - buff));
1351
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1352

1353
    if (share->avg_row_length)
1354
    {
1355
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
1356
      end= longlong10_to_str(share->avg_row_length, buff,10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1357
      packet->append(buff, (uint) (end - buff));
1358
    }
1359

1360
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
1361
      packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
1362
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
1363
      packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
1364
    if (share->db_create_options & HA_OPTION_CHECKSUM)
1365
      packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
1366
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1367
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
1368
    if (share->row_type != ROW_TYPE_DEFAULT)
1369
    {
1370
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
1371
      packet->append(ha_row_type[(uint) share->row_type]);
1372
    }
1373 1374 1375 1376 1377 1378
    if (table->s->key_block_size)
    {
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
      end= longlong10_to_str(table->s->key_block_size, buff, 10);
      packet->append(buff, (uint) (end - buff));
    }
1379
    table->file->append_create_info(packet);
1380
    if (share->comment.length)
1381
    {
1382
      packet->append(STRING_WITH_LEN(" COMMENT="));
1383
      append_unescaped(packet, share->comment.str, share->comment.length);
1384
    }
1385 1386
    if (share->connect_string.length)
    {
1387
      packet->append(STRING_WITH_LEN(" CONNECTION="));
1388 1389
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1390 1391
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1392
  }
1393
#ifdef WITH_PARTITION_STORAGE_ENGINE
1394 1395 1396 1397 1398 1399
  {
    /*
      Partition syntax for CREATE TABLE is at the end of the syntax.
    */
    uint part_syntax_len;
    char *part_syntax;
1400
    if (table->part_info &&
1401
        (!table->part_info->is_auto_partitioned) &&
1402
        ((part_syntax= generate_partition_syntax(table->part_info,
1403
                                                  &part_syntax_len,
1404
                                                  FALSE,
1405
                                                  show_table_options))))
1406
    {
rburnett@bk-internal.mysql.com's avatar
rburnett@bk-internal.mysql.com committed
1407
       packet->append(STRING_WITH_LEN(" /*!50100"));
1408
       packet->append(part_syntax, part_syntax_len);
rburnett@bk-internal.mysql.com's avatar
rburnett@bk-internal.mysql.com committed
1409
       packet->append(STRING_WITH_LEN(" */"));
1410 1411 1412 1413
       my_free(part_syntax, MYF(0));
    }
  }
#endif
1414
  tmp_restore_column_map(table->read_set, old_map);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1415 1416 1417
  DBUG_RETURN(0);
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458

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

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

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

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

    /* send USING only in non-default case: non-spatial rtree */
    if ((key_info->algorithm == HA_KEY_ALG_RTREE) &&
        !(key_info->flags & HA_SPATIAL))
      packet->append(STRING_WITH_LEN(" USING RTREE"));

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


1459 1460
void
view_store_options(THD *thd, TABLE_LIST *table, String *buff)
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
{
  append_algorithm(table, buff);
  append_definer(thd, buff, &table->definer.user, &table->definer.host);
  if (table->view_suid)
    buff->append(STRING_WITH_LEN("SQL SECURITY DEFINER "));
  else
    buff->append(STRING_WITH_LEN("SQL SECURITY INVOKER "));
}


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

static void append_algorithm(TABLE_LIST *table, String *buff)
1483
{
1484
  buff->append(STRING_WITH_LEN("ALGORITHM="));
1485 1486
  switch ((int8)table->algorithm) {
  case VIEW_ALGORITHM_UNDEFINED:
1487
    buff->append(STRING_WITH_LEN("UNDEFINED "));
1488 1489
    break;
  case VIEW_ALGORITHM_TMPTABLE:
1490
    buff->append(STRING_WITH_LEN("TEMPTABLE "));
1491 1492
    break;
  case VIEW_ALGORITHM_MERGE:
1493
    buff->append(STRING_WITH_LEN("MERGE "));
1494 1495 1496 1497 1498
    break;
  default:
    DBUG_ASSERT(0); // never should happen
  }
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1499

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
/*
  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(' ');
}


1522
int
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1523 1524 1525 1526 1527 1528 1529 1530
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;
1531 1532 1533 1534 1535
  /*
     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
  */
1536
  if (!thd->db || strcmp(thd->db, table->view_db.str))
1537 1538 1539 1540
    table->compact_view_format= FALSE;
  else
  {
    TABLE_LIST *tbl;
1541
    table->compact_view_format= TRUE;
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
    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;
      }
    }
  }

1554
  buff->append(STRING_WITH_LEN("CREATE "));
1555
  if (!foreign_db_mode)
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1556
  {
1557
    view_store_options(thd, table, buff);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1558
  }
1559
  buff->append(STRING_WITH_LEN("VIEW "));
1560 1561 1562 1563 1564
  if (!table->compact_view_format)
  {
    append_identifier(thd, buff, table->view_db.str, table->view_db.length);
    buff->append('.');
  }
1565
  append_identifier(thd, buff, table->view_name.str, table->view_name.length);
1566
  buff->append(STRING_WITH_LEN(" AS "));
1567 1568 1569 1570 1571 1572 1573

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

1574 1575 1576
  if (table->with_check != VIEW_CHECK_NONE)
  {
    if (table->with_check == VIEW_CHECK_LOCAL)
1577
      buff->append(STRING_WITH_LEN(" WITH LOCAL CHECK OPTION"));
1578
    else
1579
      buff->append(STRING_WITH_LEN(" WITH CASCADED CHECK OPTION"));
1580
  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1581 1582 1583 1584
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1585
/****************************************************************************
1586 1587
  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
1588 1589 1590 1591
****************************************************************************/

class thread_info :public ilink {
public:
1592 1593 1594 1595
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1596
  static void operator delete(void *ptr __attribute__((unused)),
1597 1598
                              size_t size __attribute__((unused)))
  { TRASH(ptr, size); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1599

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1600 1601
  ulong thread_id;
  time_t start_time;
1602
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1603 1604 1605 1606
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

1607
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1608 1609 1610 1611 1612 1613 1614 1615
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;
1616 1617
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1618
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1619 1620
  DBUG_ENTER("mysqld_list_processes");

1621
  field_list.push_back(new Item_int("Id",0,11));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1622
  field_list.push_back(new Item_empty_string("User",16));
1623
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1624 1625 1626
  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));
1627
  field_list.push_back(new Item_return_int("Time",7, MYSQL_TYPE_LONG));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1628 1629 1630 1631
  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;
1632 1633
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1634 1635 1636 1637 1638 1639 1640 1641 1642
    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++))
    {
1643
      Security_context *tmp_sctx= tmp->security_ctx;
1644
      struct st_my_thread_var *mysys_var;
1645
      if ((tmp->vio_ok() || tmp->system_thread) &&
1646
          (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1647
      {
1648
        thread_info *thd_info= new thread_info;
1649 1650

        thd_info->thread_id=tmp->thread_id;
1651 1652 1653 1654 1655
        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])
1656 1657
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1658
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1659
			"%s:%u", tmp_sctx->host_or_ip, tmp->peer_port);
1660 1661
	}
	else
1662 1663 1664
	  thd_info->host= thd->strdup(tmp_sctx->host_or_ip[0] ? 
                                      tmp_sctx->host_or_ip : 
                                      tmp_sctx->host ? tmp_sctx->host : "");
1665 1666 1667
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1668 1669
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1670
        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
1671
#ifndef EMBEDDED_LIBRARY
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
        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
1682 1683 1684
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1685 1686
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1687 1688

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1689 1690
        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
1691
#endif
1692 1693 1694
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1695
        thd_info->start_time= tmp->start_time;
1696
#endif
1697 1698 1699
        thd_info->query=0;
        if (tmp->query)
        {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1700 1701 1702 1703 1704
	  /* 
            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
          */
1705
          uint length= min(max_query_length, tmp->query_length);
1706
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1707 1708
        }
        thread_infos.append(thd_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1709 1710 1711 1712 1713 1714
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1715
  time_t now= time(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1716 1717
  while ((thd_info=thread_infos.get()))
  {
1718 1719
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1720 1721 1722
    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
1723
    if (thd_info->proc_info)
1724
      protocol->store(thd_info->proc_info, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1725
    else
1726
      protocol->store(command_name[thd_info->command].str, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1727
    if (thd_info->start_time)
1728
      protocol->store((uint32) (now - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1729
    else
1730
      protocol->store_null();
1731 1732
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1733
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1734 1735
      break; /* purecov: inspected */
  }
1736
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1737 1738 1739
  DBUG_VOID_RETURN;
}

1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
{
  TABLE *table= tables->table;
  CHARSET_INFO *cs= system_charset_info;
  char *user;
  time_t now= time(0);
  DBUG_ENTER("fill_process_list");

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

  VOID(pthread_mutex_lock(&LOCK_thread_count));

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

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

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

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

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

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

      /* INFO */
      if (tmp->query)
      {
        table->field[7]->store(tmp->query,
1833 1834
                               min(PROCESS_LIST_INFO_WIDTH,
                                   tmp->query_length), cs);
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
        table->field[7]->set_notnull();
      }

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1850
/*****************************************************************************
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1851
  Status functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1852 1853
*****************************************************************************/

1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
static DYNAMIC_ARRAY all_status_vars;
static bool status_vars_inited= 0;
static int show_var_cmp(const void *var1, const void *var2)
{
  return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
}

/*
  deletes all the SHOW_UNDEF elements from the array and calls
  delete_dynamic() if it's completely empty.
*/
static void shrink_var_array(DYNAMIC_ARRAY *array)
{
1867
  uint a,b;
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);

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

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

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

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

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

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

/*
  Make all_status_vars[] usable for SHOW STATUS

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

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

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

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

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

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

1966 1967 1968 1969 1970 1971
void remove_status_vars(SHOW_VAR *list)
{
  if (status_vars_inited)
  {
    pthread_mutex_lock(&LOCK_status);
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1972
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
1973 1974 1975

    for (; list->name; list++)
    {
1976
      int res= 0;
1977 1978 1979 1980 1981 1982 1983
      for (a= 0, b= all_status_vars.elements; b-a > 1; c= (a+b)/2)
      {
        res= show_var_cmp(list, all+c);
        if (res < 0)
          b= c;
        else if (res > 0)
          a= c;
1984 1985
        else
          break;
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
      }
      if (res == 0)
        all[c].type= SHOW_UNDEF;
    }
    shrink_var_array(&all_status_vars);
    pthread_mutex_unlock(&LOCK_status);
  }
  else
  {
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1996
    uint i;
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
    for (; list->name; list++)
    {
      for (i= 0; i < all_status_vars.elements; i++)
      {
        if (show_var_cmp(list, all+i))
          continue;
        all[i].type= SHOW_UNDEF;
        break;
      }
    }
    shrink_var_array(&all_status_vars);
  }
}

2011 2012 2013 2014 2015 2016
inline void make_upper(char *buf)
{
  for (; *buf; buf++)
    *buf= my_toupper(system_charset_info, *buf);
}

2017
static bool show_status_array(THD *thd, const char *wild,
2018
                              SHOW_VAR *variables,
2019 2020
                              enum enum_var_type value_type,
                              struct system_status_var *status_var,
2021 2022
                              const char *prefix, TABLE *table,
                              bool ucase_names)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2023
{
2024
  char buff[SHOW_VAR_FUNC_BUFF_SIZE], *prefix_end;
2025 2026
  /* the variable name should not be longer than 64 characters */
  char name_buffer[64];
2027
  int len;
2028
  LEX_STRING null_lex_str;
2029
  SHOW_VAR tmp, *var;
2030
  DBUG_ENTER("show_status_array");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2031

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

2035
  prefix_end=strnmov(name_buffer, prefix, sizeof(name_buffer)-1);
2036 2037
  if (*prefix)
    *prefix_end++= '_';
2038 2039
  len=name_buffer + sizeof(name_buffer) - prefix_end;

2040
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2041
  {
2042 2043
    strnmov(prefix_end, variables->name, len);
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
2044 2045
    if (ucase_names)
      make_upper(name_buffer);
2046

2047 2048 2049 2050
    /*
      if var->type is SHOW_FUNC, call the function.
      Repeat as necessary, if new var is again SHOW_FUNC
    */
2051
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
2052
      ((mysql_show_var_func)(var->value))(thd, &tmp, buff);
2053 2054 2055

    SHOW_TYPE show_type=var->type;
    if (show_type == SHOW_ARRAY)
2056
    {
2057 2058
      show_status_array(thd, wild, (SHOW_VAR *) var->value, value_type,
                        status_var, name_buffer, table, ucase_names);
2059 2060
    }
    else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2061
    {
2062 2063
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
2064
      {
2065
        char *value=var->value;
2066
        const char *pos, *end;                  // We assign a lot of const's
2067

2068 2069 2070 2071 2072 2073 2074 2075
        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;
2076 2077 2078 2079
        /*
          note that value may be == buff. All SHOW_xxx code below
          should still work in this case
        */
2080
        switch (show_type) {
2081 2082 2083 2084 2085 2086
        case SHOW_DOUBLE_STATUS:
        {
          value= ((char *) status_var + (ulong) value);
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
2087 2088 2089 2090
        case SHOW_LONG_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
2091
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
          end= int10_to_str(*(long*) value, buff, 10);
          break;
        case SHOW_LONGLONG:
          end= longlong10_to_str(*(longlong*) value, buff, 10);
          break;
        case SHOW_HA_ROWS:
          end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
          break;
        case SHOW_BOOL:
          end= strmov(buff, *(bool*) value ? "ON" : "OFF");
          break;
        case SHOW_MY_BOOL:
          end= strmov(buff, *(my_bool*) value ? "ON" : "OFF");
          break;
        case SHOW_INT:
          end= int10_to_str((long) *(uint32*) value, buff, 10);
          break;
        case SHOW_HAVE:
petr@mysql.com's avatar
petr@mysql.com committed
2110 2111 2112 2113 2114 2115
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
2116
        case SHOW_CHAR:
petr@mysql.com's avatar
petr@mysql.com committed
2117 2118 2119 2120 2121 2122
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
2123
        case SHOW_CHAR_PTR:
petr@mysql.com's avatar
petr@mysql.com committed
2124 2125 2126 2127 2128 2129
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
2130
        case SHOW_KEY_CACHE_LONG:
2131
          value= (char*) dflt_key_cache + (ulong)value;
2132 2133
          end= int10_to_str(*(long*) value, buff, 10);
          break;
2134
        case SHOW_KEY_CACHE_LONGLONG:
2135
          value= (char*) dflt_key_cache + (ulong)value;
2136 2137
	  end= longlong10_to_str(*(longlong*) value, buff, 10);
	  break;
2138
        case SHOW_UNDEF:
2139 2140
          break;                                        // Return empty string
        case SHOW_SYS:                                  // Cannot happen
2141
        default:
2142
          DBUG_ASSERT(0);
2143 2144
          break;
        }
2145
        restore_record(table, s->default_values);
2146 2147 2148
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
2149
        table->field[1]->set_notnull();
2150 2151
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2152 2153 2154
      }
    }
  }
2155 2156 2157 2158 2159

  DBUG_RETURN(FALSE);
}


2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
/* 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;
}


2184 2185 2186 2187 2188 2189
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)
2190 2191
    if (!(lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING))))
      return 0;
2192 2193 2194 2195 2196 2197 2198
  lex_str->str= strmake_root(mem, str, length);
  lex_str->length= length;
  return lex_str;
}


/* INFORMATION_SCHEMA name */
andrey@example.com's avatar
andrey@example.com committed
2199
LEX_STRING information_schema_name= { C_STRING_WITH_LEN("information_schema")};
2200 2201

/* This is only used internally, but we need it here as a forward reference */
2202 2203 2204 2205 2206 2207 2208 2209
extern ST_SCHEMA_TABLE schema_tables[];

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


2210 2211 2212 2213 2214 2215 2216
/*
  Store record to I_S table, convert HEAP table
  to MyISAM if necessary

  SYNOPSIS
    schema_table_store_record()
    thd                   thread handler
2217 2218
    table                 Information schema table to be updated

2219 2220
  RETURN
    0	                  success
2221
    1	                  error
2222 2223
*/

2224
bool schema_table_store_record(THD *thd, TABLE *table)
2225 2226
{
  int error;
2227
  if ((error= table->file->ha_write_row(table->record[0])))
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
  {
    if (create_myisam_from_heap(thd, table, 
                                table->pos_in_table_list->schema_table_param,
                                error, 0))
      return 1;
  }
  return 0;
}


2238 2239 2240
void get_index_field_values(LEX *lex, INDEX_FIELD_VALUES *index_field_values)
{
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2241
  switch (lex->sql_command) {
2242 2243 2244 2245 2246
  case SQLCOM_SHOW_DATABASES:
    index_field_values->db_value= wild;
    break;
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_SHOW_TABLE_STATUS:
2247
  case SQLCOM_SHOW_TRIGGERS:
2248
  case SQLCOM_SHOW_EVENTS:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2249
    index_field_values->db_value= lex->select_lex.db;
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
    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
2271
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
                             (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++)
2286
    {
2287 2288
      if (!uses_only_table_name_fields(*child, table))
        return 0;
2289
    }
2290 2291 2292 2293 2294 2295 2296
  }
  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;
2297 2298
    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 : "";
2299 2300 2301 2302 2303 2304 2305
    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)))
2306 2307
      return 0;
  }
2308 2309
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
2310 2311 2312 2313
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

2314
  return 1;
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
}


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


2374 2375 2376 2377 2378 2379
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
{
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
}


2380
/*
2381
  Create db names list. Information schema name always is first in list
2382 2383

  SYNOPSIS
2384
    make_db_list()
2385 2386 2387
    thd                   thread handler
    files                 list of db names
    wild                  wild string
2388 2389
    idx_field_vals        idx_field_vals->db_name contains db name or
                          wild string
2390 2391
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0
2392 2393
    is_wild_value         if value is 1 then idx_field_vals->db_name is
                          wild string otherwise it's db name; 
2394 2395

  RETURN
2396 2397
    zero                  success
    non-zero              error
2398 2399
*/

2400 2401 2402
int make_db_list(THD *thd, List<char> *files,
                 INDEX_FIELD_VALUES *idx_field_vals,
                 bool *with_i_schema, bool is_wild_value)
2403
{
2404
  LEX *lex= thd->lex;
2405
  *with_i_schema= 0;
2406 2407
  get_index_field_values(lex, idx_field_vals);
  if (is_wild_value)
2408
  {
2409 2410 2411 2412 2413
    /*
      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)
    */
2414 2415 2416 2417 2418 2419 2420 2421 2422
    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;
    }
2423 2424
    return (find_files(thd, files, NullS, mysql_data_home,
                       idx_field_vals->db_value, 1) != FIND_FILES_OK);
2425
  }
2426

2427 2428 2429
  /*
    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()
2430
    function).
2431
  */
2432
  if (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND)
2433
  {
2434
    if (!my_strcasecmp(system_charset_info, information_schema_name.str,
2435 2436 2437 2438 2439 2440 2441 2442
                       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));
  }

2443 2444 2445 2446
  /*
    Create list of existing databases. It is used in case
    of select from information schema table
  */
2447 2448 2449
  if (files->push_back(thd->strdup(information_schema_name.str)))
    return 1;
  *with_i_schema= 1;
2450 2451
  return (find_files(thd, files, NullS,
                     mysql_data_home, NullS, 1) != FIND_FILES_OK);
2452 2453
}

brian@zim.(none)'s avatar
brian@zim.(none) committed
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
struct st_add_schema_table 
{
  List<char> *files;
  const char *wild;
};

static my_bool add_schema_table(THD *thd, st_plugin_int *plugin,
                                void* p_data)
{
  st_add_schema_table *data= (st_add_schema_table *)p_data;
  List<char> *file_list= data->files;
  const char *wild= data->wild;
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
  DBUG_ENTER("add_schema_table");

  if (schema_table->hidden)
      DBUG_RETURN(0);
  if (wild)
  {
    if (lower_case_table_names)
    {
      if (wild_case_compare(files_charset_info,
                            schema_table->table_name,
                            wild))
        DBUG_RETURN(0);
    }
    else if (wild_compare(schema_table->table_name, wild, 0))
      DBUG_RETURN(0);
  }

  if (file_list->push_back(thd->strdup(schema_table->table_name)))
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}
2489 2490 2491 2492

int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
brian@zim.(none)'s avatar
brian@zim.(none) committed
2493 2494 2495
  st_add_schema_table add_data;
  DBUG_ENTER("schema_tables_add");

2496
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2497
  {
2498 2499
    if (tmp_schema_table->hidden)
      continue;
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
    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)))
brian@zim.(none)'s avatar
brian@zim.(none) committed
2513
      DBUG_RETURN(1);
2514
  }
brian@zim.(none)'s avatar
brian@zim.(none) committed
2515 2516 2517 2518 2519 2520 2521 2522

  add_data.files= files;
  add_data.wild= wild;
  if (plugin_foreach(thd, add_schema_table,
                     MYSQL_INFORMATION_SCHEMA_PLUGIN, &add_data))
      DBUG_RETURN(1);

  DBUG_RETURN(0);
2523 2524 2525
}


2526 2527 2528 2529 2530
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;
2531
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2532
  enum_sql_command save_sql_command= lex->sql_command;
2533
  SELECT_LEX *lsel= tables->schema_select_lex;
2534
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2535 2536
  SELECT_LEX sel;
  INDEX_FIELD_VALUES idx_field_vals;
2537
  char path[FN_REFLEN], *end, *base_name, *orig_base_name, *file_name;
2538 2539 2540 2541
  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
2542
  List_iterator_fast<char> it(bases);
2543
  COND *partial_cond;
2544
  Security_context *sctx= thd->security_ctx;
2545
  uint derived_tables= lex->derived_tables; 
monty@mysql.com's avatar
monty@mysql.com committed
2546
  int error= 1;
2547
  enum legacy_db_type not_used;
2548
  Open_tables_state open_tables_state_backup;
2549
  bool save_view_prepare_mode= lex->view_prepare_mode;
2550
  Query_tables_list query_tables_list_backup;
2551
  lex->view_prepare_mode= TRUE;
2552 2553 2554 2555
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
2556

2557 2558
  lex->reset_n_backup_query_tables_list(&query_tables_list_backup);

2559 2560 2561 2562 2563 2564 2565
  /*
    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);

2566 2567 2568
  if (lsel)
  {
    TABLE_LIST *show_table_list= (TABLE_LIST*) lsel->table_list.first;
2569 2570
    bool res;

2571
    lex->all_selects_list= lsel;
2572 2573 2574 2575 2576 2577 2578
    /*
      Restore thd->temporary_tables to be able to process
      temporary tables(only for 'show index' & 'show columns').
      This should be changed when processing of temporary tables for
      I_S tables will be done.
    */
    thd->temporary_tables= open_tables_state_backup.temporary_tables;
2579 2580 2581 2582 2583 2584 2585
    /*
      Let us set fake sql_command so views won't try to merge
      themselves into main statement. If we don't do this,
      SELECT * from information_schema.xxxx will cause problems.
      SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()' 
    */
    lex->sql_command= SQLCOM_SHOW_FIELDS;
2586 2587
    res= open_normal_and_derived_tables(thd, show_table_list,
                                        MYSQL_LOCK_IGNORE_FLUSH);
2588
    lex->sql_command= save_sql_command;
2589 2590 2591
    /*
      get_all_tables() returns 1 on failure and 0 on success thus
      return only these and not the result code of ::process_table()
2592 2593 2594 2595 2596 2597 2598

      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).
2599 2600
    */
    error= test(schema_table->process_table(thd, show_table_list,
2601 2602 2603 2604 2605
                                            table, res, 
                                            (show_table_list->view ?
                                             show_table_list->view_db.str :
                                             show_table_list->db),
                                            show_table_list->alias));
2606
    thd->temporary_tables= 0;
2607
    close_tables_for_reopen(thd, &show_table_list);
monty@mysql.com's avatar
monty@mysql.com committed
2608
    goto err;
2609 2610
  }

2611
  schema_table_idx= get_schema_table_idx(schema_table);
2612

2613 2614
  if (make_db_list(thd, &bases, &idx_field_vals,
                   &with_i_schema, 0))
monty@mysql.com's avatar
monty@mysql.com committed
2615
    goto err;
2616 2617

  partial_cond= make_cond_for_info_schema(cond, tables);
monty@mysql.com's avatar
monty@mysql.com committed
2618
  it.rewind(); /* To get access to new elements in basis list */
2619 2620 2621 2622 2623

  /*
    Below we generate error for non existing database.
    (to save old behaviour for SHOW TABLES FROM db)
  */
2624
  while ((orig_base_name= base_name= it++) ||
2625
         ((sql_command_flags[save_sql_command] & CF_SHOW_TABLE_COMMAND) &&
2626 2627 2628
	  (base_name= select_lex->db) && !bases.elements))
  {
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2629 2630
    if (!check_access(thd,SELECT_ACL, base_name, 
                      &thd->col_access, 0, 1, with_i_schema) ||
2631 2632
        sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, base_name,0) ||
2633 2634 2635 2636
	(grant_option && !check_grant_db(thd, base_name)))
#endif
    {
      List<char> files;
2637 2638 2639
      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
2640
          goto err;
2641 2642 2643
      }
      else
      {
2644
        len= build_table_filename(path, sizeof(path), base_name, "", "", 0);
2645
        end= path + len;
2646
        len= FN_LEN - len;
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
        find_files_result res= find_files(thd, &files, base_name, 
                                          path, idx_field_vals.table_value, 0);
        if (res != FIND_FILES_OK)
        {
          /*
            Downgrade errors about problems with database directory to
            warnings if this is not a 'SHOW' command.  Another thread
            may have dropped database, and we may still have a name
            for that directory.
          */
2657
          if (res == FIND_FILES_DIR && lex->sql_command == SQLCOM_END)
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
          {
            push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                         thd->net.last_errno, thd->net.last_error);
            thd->clear_error();
            continue;
          }
          else
          {
            goto err;
          }
        }
2669 2670
        if (lower_case_table_names)
          orig_base_name= thd->strdup(base_name);
2671
      }
2672

2673 2674
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
2675
      {
2676
	restore_record(table, s->default_values);
2677 2678 2679 2680 2681 2682 2683 2684
        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)
          {
2685 2686
            if (lex->verbose ||
                (sql_command_flags[save_sql_command] & CF_STATUS_COMMAND) == 0)
2687
            {
2688
              if (with_i_schema)
2689
              {
2690 2691
                table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
                                       system_charset_info);
2692 2693 2694 2695
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
2696
                switch (mysql_frm_type(thd, path, &not_used)) {
2697
                case FRMTYPE_ERROR:
2698 2699
                  table->field[3]->store(STRING_WITH_LEN("ERROR"),
                                         system_charset_info);
2700 2701
                  break;
                case FRMTYPE_TABLE:
2702 2703
                  table->field[3]->store(STRING_WITH_LEN("BASE TABLE"),
                                         system_charset_info);
2704 2705
                  break;
                case FRMTYPE_VIEW:
2706 2707
                  table->field[3]->store(STRING_WITH_LEN("VIEW"),
                                         system_charset_info);
2708 2709 2710 2711
                  break;
                default:
                  DBUG_ASSERT(0);
                }
2712 2713
              }
            }
2714
            if (schema_table_store_record(thd, table))
2715
              goto err;
2716 2717 2718 2719
          }
          else
          {
            int res;
2720
            /*
2721 2722
              Set the parent lex of 'sel' because it is needed by
              sel.init_query() which is called inside make_table_list.
2723 2724
            */
            sel.parent_lex= lex;
2725
            if (make_table_list(thd, &sel, base_name, file_name))
monty@mysql.com's avatar
monty@mysql.com committed
2726
              goto err;
2727
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
2728 2729
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
2730
            lex->sql_command= SQLCOM_SHOW_FIELDS;
2731 2732
            res= open_normal_and_derived_tables(thd, show_table_list,
                                                MYSQL_LOCK_IGNORE_FLUSH);
2733
            lex->sql_command= save_sql_command;
2734 2735 2736 2737 2738 2739 2740
            /*
              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.
            */
2741
            res= schema_table->process_table(thd, show_table_list, table,
2742
                                             res, orig_base_name,
2743 2744 2745
                                             show_table_list->alias);
            close_tables_for_reopen(thd, &show_table_list);
            DBUG_ASSERT(!lex->query_tables_own_last);
2746
            if (res)
monty@mysql.com's avatar
monty@mysql.com committed
2747
              goto err;
2748 2749 2750
          }
        }
      }
2751 2752 2753 2754
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
2755
      with_i_schema= 0;
2756 2757
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
2758 2759 2760

  error= 0;
err:
2761
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2762
  lex->restore_backup_query_tables_list(&query_tables_list_backup);
2763 2764
  lex->derived_tables= derived_tables;
  lex->all_selects_list= old_all_select_lex;
2765
  lex->view_prepare_mode= save_view_prepare_mode;
2766
  lex->sql_command= save_sql_command;
monty@mysql.com's avatar
monty@mysql.com committed
2767
  DBUG_RETURN(error);
2768 2769 2770
}


2771
bool store_schema_shemata(THD* thd, TABLE *table, const char *db_name,
2772
                          CHARSET_INFO *cs)
2773
{
2774
  restore_record(table, s->default_values);
2775
  table->field[1]->store(db_name, strlen(db_name), system_charset_info);
2776 2777
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2778
  return schema_table_store_record(thd, table);
2779 2780 2781
}


2782 2783
int fill_schema_shemata(THD *thd, TABLE_LIST *tables, COND *cond)
{
2784 2785 2786 2787 2788
  /*
    TODO: fill_schema_shemata() is called when new client is connected.
    Returning error status in this case leads to client hangup.
  */

2789 2790 2791
  INDEX_FIELD_VALUES idx_field_vals;
  List<char> files;
  char *file_name;
2792
  bool with_i_schema;
2793 2794
  HA_CREATE_INFO create;
  TABLE *table= tables->table;
2795
  Security_context *sctx= thd->security_ctx;
2796
  DBUG_ENTER("fill_schema_shemata");
2797

2798 2799
  if (make_db_list(thd, &files, &idx_field_vals,
                   &with_i_schema, 1))
2800
    DBUG_RETURN(1);
2801

2802 2803 2804
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2805 2806
    if (with_i_schema)       // information schema name is always first in list
    {
2807
      if (store_schema_shemata(thd, table, file_name,
2808
                               system_charset_info))
2809
        DBUG_RETURN(1);
2810 2811 2812
      with_i_schema= 0;
      continue;
    }
2813
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2814 2815
    if (sctx->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(sctx->host, sctx->ip, sctx->priv_user, file_name,0) ||
2816 2817 2818
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2819
      load_db_opt_by_name(thd, file_name, &create);
2820

2821
      if (store_schema_shemata(thd, table, file_name,
2822
                               create.default_table_charset))
2823
        DBUG_RETURN(1);
2824 2825
    }
  }
2826
  DBUG_RETURN(0);
2827 2828 2829
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2830 2831 2832 2833
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)
2834 2835 2836 2837 2838
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2839 2840

  restore_record(table, s->default_values);
2841 2842
  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
2843 2844 2845 2846 2847 2848
  if (res)
  {
    /*
      there was errors during opening tables
    */
    const char *error= thd->net.last_error;
2849 2850 2851 2852 2853 2854
    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
2855 2856 2857 2858
    table->field[20]->store(error, strlen(error), cs);
    thd->clear_error();
  }
  else if (tables->view)
2859
  {
2860 2861
    table->field[3]->store(STRING_WITH_LEN("VIEW"), cs);
    table->field[20]->store(STRING_WITH_LEN("VIEW"), cs);
2862 2863 2864 2865
  }
  else
  {
    TABLE *show_table= tables->table;
2866
    TABLE_SHARE *share= show_table->s;
2867
    handler *file= show_table->file;
2868

2869 2870
    file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_AUTO |
               HA_STATUS_NO_LOCK);
2871
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2872
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2873
    else if (share->tmp_table)
2874
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
2875
    else
2876
      table->field[3]->store(STRING_WITH_LEN("BASE TABLE"), cs);
2877

2878 2879
    for (int i= 4; i < 20; i++)
    {
2880
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2881 2882 2883 2884 2885
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2886
    table->field[5]->store((longlong) share->frm_version, TRUE);
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
    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;
2911 2912 2913
    case ROW_TYPE_PAGES:
      tmp_buff= "Paged";
      break;
2914
    }
2915
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2916 2917
    if (!tables->schema_table)
    {
2918
      table->field[7]->store((longlong) file->stats.records, TRUE);
2919 2920
      table->field[7]->set_notnull();
    }
2921 2922 2923
    table->field[8]->store((longlong) file->stats.mean_rec_length, TRUE);
    table->field[9]->store((longlong) file->stats.data_file_length, TRUE);
    if (file->stats.max_data_file_length)
2924
    {
2925 2926
      table->field[10]->store((longlong) file->stats.max_data_file_length,
                              TRUE);
2927
    }
2928 2929
    table->field[11]->store((longlong) file->stats.index_file_length, TRUE);
    table->field[12]->store((longlong) file->stats.delete_length, TRUE);
2930
    if (show_table->found_next_number_field)
2931
    {
2932 2933
      table->field[13]->store((longlong) file->stats.auto_increment_value,
                              TRUE);
2934 2935
      table->field[13]->set_notnull();
    }
2936
    if (file->stats.create_time)
2937 2938
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
2939
                                                file->stats.create_time);
2940 2941 2942
      table->field[14]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[14]->set_notnull();
    }
2943
    if (file->stats.update_time)
2944 2945
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
2946
                                                file->stats.update_time);
2947 2948 2949
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[15]->set_notnull();
    }
2950
    if (file->stats.check_time)
2951
    {
2952
      thd->variables.time_zone->gmt_sec_to_TIME(&time, file->stats.check_time);
2953 2954 2955
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[16]->set_notnull();
    }
2956 2957
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2958
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
2959
    if (file->ha_table_flags() & (ulong) HA_HAS_CHECKSUM)
2960
    {
2961
      table->field[18]->store((longlong) file->checksum(), TRUE);
2962 2963 2964 2965 2966
      table->field[18]->set_notnull();
    }

    char option_buff[350],*ptr;
    ptr=option_buff;
2967
    if (share->min_rows)
2968 2969
    {
      ptr=strmov(ptr," min_rows=");
2970
      ptr=longlong10_to_str(share->min_rows,ptr,10);
2971
    }
2972
    if (share->max_rows)
2973 2974
    {
      ptr=strmov(ptr," max_rows=");
2975
      ptr=longlong10_to_str(share->max_rows,ptr,10);
2976
    }
2977
    if (share->avg_row_length)
2978 2979
    {
      ptr=strmov(ptr," avg_row_length=");
2980
      ptr=longlong10_to_str(share->avg_row_length,ptr,10);
2981
    }
2982
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2983
      ptr=strmov(ptr," pack_keys=1");
2984
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2985
      ptr=strmov(ptr," pack_keys=0");
2986
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2987
      ptr=strmov(ptr," checksum=1");
2988
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2989
      ptr=strmov(ptr," delay_key_write=1");
2990
    if (share->row_type != ROW_TYPE_DEFAULT)
2991
      ptr=strxmov(ptr, " row_format=", 
2992
                  ha_row_type[(uint) share->row_type],
2993
                  NullS);
2994
#ifdef WITH_PARTITION_STORAGE_ENGINE
2995
    if (show_table->s->db_type == partition_hton && 
2996 2997
        show_table->part_info != NULL && 
        show_table->part_info->no_parts > 0)
2998 2999
      ptr= strmov(ptr, " partitioned");
#endif
3000 3001 3002
    table->field[19]->store(option_buff+1,
                            (ptr == option_buff ? 0 : 
                             (uint) (ptr-option_buff)-1), cs);
3003
    {
3004
      char *comment;
3005
      comment= show_table->file->update_table_comment(share->comment.str);
3006 3007
      if (comment)
      {
3008 3009 3010 3011 3012
        table->field[20]->store(comment,
                                (comment == share->comment.str ?
                                 share->comment.length : 
                                 strlen(comment)), cs);
        if (comment != share->comment.str)
3013
          my_free(comment, MYF(0));
3014 3015
      }
    }
3016
  }
3017
  DBUG_RETURN(schema_table_store_record(thd, table));
3018 3019 3020
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3021 3022 3023 3024
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)
3025
{
3026 3027
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
3028
  CHARSET_INFO *cs= system_charset_info;
3029 3030 3031 3032 3033
  TABLE *show_table;
  handler *file;
  Field **ptr,*field;
  int count;
  uint base_name_length, file_name_length;
3034
  DBUG_ENTER("get_schema_column_record");
3035

3036 3037
  if (res)
  {
3038
    if (lex->sql_command != SQLCOM_SHOW_FIELDS)
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
    {
      /*
        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);
3050 3051
  }

3052 3053 3054
  show_table= tables->table;
  file= show_table->file;
  count= 0;
3055
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3056
  restore_record(show_table, s->default_values);
3057 3058
  base_name_length= strlen(base_name);
  file_name_length= strlen(file_name);
3059
  show_table->use_all_columns();               // Required for default
3060

3061 3062
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
    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);
3081 3082

#ifndef NO_EMBEDDED_ACCESS_CHECKS
3083 3084
    uint col_access;
    check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
3085
                 &tables->grant.privilege, 0, 0, test(tables->schema_table));
3086 3087 3088
    col_access= get_column_grant(thd, &tables->grant, 
                                 base_name, file_name,
                                 field->field_name) & COL_ACLS;
3089
    if (lex->sql_command != SQLCOM_SHOW_FIELDS  &&
3090 3091 3092 3093 3094 3095
        !tables->schema_table && !col_access)
      continue;
    end= tmp;
    for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
    {
      if (col_access & 1)
3096
      {
3097 3098
        *end++=',';
        end=strmov(end,grant_types.type_names[bitnr]);
3099
      }
3100
    }
3101
    table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);
3102

3103
#endif
3104 3105 3106 3107
    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);
3108
    table->field[4]->store((longlong) count, TRUE);
3109 3110 3111 3112 3113 3114 3115 3116 3117
    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)
    {
3118
      table->field[5]->store(STRING_WITH_LEN("CURRENT_TIMESTAMP"), cs);
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
      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 ||
3134
             lex->sql_command != SQLCOM_SHOW_FIELDS ||
3135 3136 3137 3138 3139 3140 3141
             field->maybe_null())
      table->field[5]->set_null();                // Null as default
    else
    {
      table->field[5]->store("",0, cs);
      table->field[5]->set_notnull();
    }
3142
    pos=(byte*) ((flags & NOT_NULL_FLAG) ?  "NO" : "YES");
3143 3144
    table->field[6]->store((const char*) pos,
                           strlen((const char*) pos), cs);
3145
    is_blob= (field->type() == MYSQL_TYPE_BLOB);
3146 3147 3148
    if (field->has_charset() || is_blob ||
        field->real_type() == MYSQL_TYPE_VARCHAR ||  // For varbinary type
        field->real_type() == MYSQL_TYPE_STRING)     // For binary type
3149
    {
bar@mysql.com's avatar
bar@mysql.com committed
3150
      uint32 octet_max_length= field->max_length();
3151
      if (is_blob && octet_max_length != (uint32) 4294967295U)
bar@mysql.com's avatar
bar@mysql.com committed
3152
        octet_max_length /= field->charset()->mbmaxlen;
3153
      longlong char_max_len= is_blob ? 
bar@mysql.com's avatar
bar@mysql.com committed
3154 3155
        (longlong) octet_max_length / field->charset()->mbminlen :
        (longlong) octet_max_length / field->charset()->mbmaxlen;
3156
      table->field[8]->store(char_max_len, TRUE);
3157
      table->field[8]->set_notnull();
bar@mysql.com's avatar
bar@mysql.com committed
3158
      table->field[9]->store((longlong) octet_max_length, TRUE);
3159 3160
      table->field[9]->set_notnull();
    }
3161

3162 3163 3164 3165
    /*
      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
3166

3167 3168
    decimals= field->decimals();
    switch (field->type()) {
3169
    case MYSQL_TYPE_NEWDECIMAL:
3170 3171
      field_length= ((Field_new_decimal*) field)->precision;
      break;
3172
    case MYSQL_TYPE_DECIMAL:
3173 3174
      field_length= field->field_length - (decimals  ? 2 : 1);
      break;
3175 3176 3177 3178 3179
    case MYSQL_TYPE_TINY:
    case MYSQL_TYPE_SHORT:
    case MYSQL_TYPE_LONG:
    case MYSQL_TYPE_LONGLONG:
    case MYSQL_TYPE_INT24:
3180 3181
      field_length= field->max_length() - 1;
      break;
3182
    case MYSQL_TYPE_BIT:
3183 3184 3185
      field_length= field->max_length();
      decimals= -1;                             // return NULL
      break;
3186 3187
    case MYSQL_TYPE_FLOAT:  
    case MYSQL_TYPE_DOUBLE:
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
      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)
    {
3199
      table->field[10]->store((longlong) field_length, TRUE);
3200
      table->field[10]->set_notnull();
3201
    }
3202 3203
    if (decimals >= 0)
    {
3204
      table->field[11]->store((longlong) decimals, TRUE);
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
      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);
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
  }
  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;
3245 3246

  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
3247 3248 3249 3250
  {
    CHARSET_INFO *tmp_cs= cs[0];
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
        (tmp_cs->state & MY_CS_AVAILABLE) &&
bar@mysql.com's avatar
bar@mysql.com committed
3251
        !(tmp_cs->state & MY_CS_HIDDEN) &&
3252 3253 3254
        !(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cs->csname,wild)))
    {
3255
      const char *comment;
3256
      restore_record(table, s->default_values);
3257
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
3258
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
3259 3260
      comment= tmp_cs->comment ? tmp_cs->comment : "";
      table->field[2]->store(comment, strlen(comment), scs);
3261
      table->field[3]->store((longlong) tmp_cs->mbmaxlen, TRUE);
3262 3263
      if (schema_table_store_record(thd, table))
        return 1;
3264 3265 3266 3267 3268 3269
    }
  }
  return 0;
}


acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3270 3271
static my_bool iter_schema_engines(THD *thd, st_plugin_int *plugin,
                                   void *ptable)
3272
{
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3273
  TABLE *table= (TABLE *) ptable;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
3274
  handlerton *hton= (handlerton *)plugin->data;
3275 3276
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  CHARSET_INFO *scs= system_charset_info;
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3277
  DBUG_ENTER("iter_schema_engines");
3278

acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3279
  if (!(hton->flags & HTON_HIDDEN))
3280 3281
  {
    if (!(wild && wild[0] &&
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
3282
          wild_case_compare(scs, plugin->name.str,wild)))
3283
    {
andrey@example.com's avatar
andrey@example.com committed
3284 3285 3286 3287
      LEX_STRING state[2]= {{ C_STRING_WITH_LEN("ENABLED") },
                            { C_STRING_WITH_LEN("DISABLED") }};
      LEX_STRING yesno[2]= {{ C_STRING_WITH_LEN("NO") },
                            { C_STRING_WITH_LEN("YES") }};
3288
      LEX_STRING *tmp;
3289 3290
      restore_record(table, s->default_values);

serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
3291
      table->field[0]->store(plugin->name.str, plugin->name.length, scs);
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
      tmp= &state[test(hton->state)];
      table->field[1]->store(tmp->str, tmp->length, scs);
      table->field[2]->store(plugin->plugin->descr,
                             strlen(plugin->plugin->descr), scs);
      tmp= &yesno[test(hton->commit)];
      table->field[3]->store(tmp->str, tmp->length, scs);
      tmp= &yesno[test(hton->prepare)];
      table->field[4]->store(tmp->str, tmp->length, scs);
      tmp= &yesno[test(hton->savepoint_set)];
      table->field[5]->store(tmp->str, tmp->length, scs);
3302 3303 3304 3305 3306 3307 3308 3309 3310

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


acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3311 3312
int fill_schema_engines(THD *thd, TABLE_LIST *tables, COND *cond)
{
3313
  return plugin_foreach(thd, iter_schema_engines,
serg@sergbook.mysql.com's avatar
fixes  
serg@sergbook.mysql.com committed
3314
                        MYSQL_STORAGE_ENGINE_PLUGIN, tables->table);
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3315 3316 3317
}


3318 3319 3320 3321 3322 3323
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;
3324
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3325 3326 3327
  {
    CHARSET_INFO **cl;
    CHARSET_INFO *tmp_cs= cs[0];
3328
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) ||
bar@mysql.com's avatar
bar@mysql.com committed
3329
         (tmp_cs->state & MY_CS_HIDDEN) ||
3330 3331
        !(tmp_cs->state & MY_CS_PRIMARY))
      continue;
3332
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3333 3334 3335 3336 3337 3338 3339 3340 3341
    {
      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;
3342
	restore_record(table, s->default_values);
3343 3344
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3345
        table->field[2]->store((longlong) tmp_cl->number, TRUE);
3346 3347 3348 3349
        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);
3350
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply, TRUE);
3351 3352
        if (schema_table_store_record(thd, table))
          return 1;
3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
      }
    }
  }
  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;
3365
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3366 3367 3368 3369 3370 3371
  {
    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;
3372
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3373 3374 3375 3376 3377
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
3378
      restore_record(table, s->default_values);
3379 3380
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3381 3382
      if (schema_table_store_record(thd, table))
        return 1;
3383 3384 3385 3386 3387 3388
    }
  }
  return 0;
}


3389
bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table,
3390
                       const char *wild, bool full_access, const char *sp_user)
3391 3392
{
  String tmp_string;
3393
  String sp_db, sp_name, definer;
3394 3395 3396
  TIME time;
  LEX *lex= thd->lex;
  CHARSET_INFO *cs= system_charset_info;
3397 3398 3399
  get_field(thd->mem_root, proc_table->field[0], &sp_db);
  get_field(thd->mem_root, proc_table->field[1], &sp_name);
  get_field(thd->mem_root, proc_table->field[11], &definer);
3400
  if (!full_access)
3401
    full_access= !strcmp(sp_user, definer.ptr());
3402 3403 3404 3405
  if (!full_access && check_some_routine_access(thd, sp_db.ptr(),
                                                sp_name.ptr(),
                                                proc_table->field[2]->
                                                val_int() ==
3406
                                                TYPE_ENUM_PROCEDURE))
3407
    return 0;
3408

3409
  if (lex->sql_command == SQLCOM_SHOW_STATUS_PROC &&
3410
      proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE ||
3411
      lex->sql_command == SQLCOM_SHOW_STATUS_FUNC &&
3412
      proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION ||
3413
      (sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0)
3414
  {
3415
    restore_record(table, s->default_values);
3416
    if (!wild || !wild[0] || !wild_compare(sp_name.ptr(), wild, 0))
3417
    {
3418
      int enum_idx= (int) proc_table->field[5]->val_int();
3419
      table->field[3]->store(sp_name.ptr(), sp_name.length(), cs);
3420 3421
      get_field(thd->mem_root, proc_table->field[3], &tmp_string);
      table->field[0]->store(tmp_string.ptr(), tmp_string.length(), cs);
3422
      table->field[2]->store(sp_db.ptr(), sp_db.length(), cs);
3423 3424
      get_field(thd->mem_root, proc_table->field[2], &tmp_string);
      table->field[4]->store(tmp_string.ptr(), tmp_string.length(), cs);
3425 3426 3427 3428 3429 3430
      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();
      }
3431 3432 3433 3434
      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);
3435
        table->field[7]->set_notnull();
3436
      }
3437 3438
      table->field[6]->store(STRING_WITH_LEN("SQL"), cs);
      table->field[10]->store(STRING_WITH_LEN("SQL"), cs);
3439
      get_field(thd->mem_root, proc_table->field[6], &tmp_string);
3440
      table->field[11]->store(tmp_string.ptr(), tmp_string.length(), cs);
3441 3442
      table->field[12]->store(sp_data_access_name[enum_idx].str, 
                              sp_data_access_name[enum_idx].length , cs);
3443 3444
      get_field(thd->mem_root, proc_table->field[7], &tmp_string);
      table->field[14]->store(tmp_string.ptr(), tmp_string.length(), cs);
3445 3446
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[12])->get_time(&time);
3447
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3448 3449
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[13])->get_time(&time);
3450
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
3451
      get_field(thd->mem_root, proc_table->field[14], &tmp_string);
3452
      table->field[17]->store(tmp_string.ptr(), tmp_string.length(), cs);
3453
      get_field(thd->mem_root, proc_table->field[15], &tmp_string);
3454
      table->field[18]->store(tmp_string.ptr(), tmp_string.length(), cs);
3455
      table->field[19]->store(definer.ptr(), definer.length(), cs);
3456
      return schema_table_store_record(thd, table);
3457 3458
    }
  }
3459
  return 0;
3460 3461 3462 3463 3464 3465 3466 3467 3468
}


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;
3469
  TABLE *table= tables->table;
3470
  bool full_access;
3471
  char definer[USER_HOST_BUFF_SIZE];
3472
  Open_tables_state open_tables_state_backup;
3473 3474
  DBUG_ENTER("fill_schema_proc");

3475 3476
  strxmov(definer, thd->security_ctx->priv_user, "@",
          thd->security_ctx->priv_host, NullS);
3477
  /* We use this TABLE_LIST instance only for checking of privileges. */
3478 3479
  bzero((char*) &proc_tables,sizeof(proc_tables));
  proc_tables.db= (char*) "mysql";
3480
  proc_tables.db_length= 5;
3481
  proc_tables.table_name= proc_tables.alias= (char*) "proc";
3482
  proc_tables.table_name_length= 4;
3483
  proc_tables.lock_type= TL_READ;
3484
  full_access= !check_table_access(thd, SELECT_ACL, &proc_tables, 1);
3485
  if (!(proc_table= open_proc_table_for_read(thd, &open_tables_state_backup)))
3486 3487 3488
  {
    DBUG_RETURN(1);
  }
3489
  proc_table->file->ha_index_init(0, 1);
3490 3491 3492 3493 3494
  if ((res= proc_table->file->index_first(proc_table->record[0])))
  {
    res= (res == HA_ERR_END_OF_FILE) ? 0 : 1;
    goto err;
  }
3495 3496 3497 3498 3499
  if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
  {
    res= 1;
    goto err;
  }
3500
  while (!proc_table->file->index_next(proc_table->record[0]))
3501 3502 3503 3504 3505 3506 3507
  {
    if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
    {
      res= 1;
      goto err;
    }
  }
3508 3509 3510

err:
  proc_table->file->ha_index_end();
3511
  close_proc_table(thd, &open_tables_state_backup);
3512 3513 3514 3515
  DBUG_RETURN(res);
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3516 3517 3518 3519
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)
3520 3521 3522
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
3523 3524
  if (res)
  {
3525
    if (thd->lex->sql_command != SQLCOM_SHOW_KEYS)
3526 3527 3528 3529
    {
      /*
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
        rather than in SHOW KEYS
3530
      */
3531 3532 3533 3534 3535 3536 3537 3538 3539
      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)
3540 3541 3542
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3543
    show_table->file->info(HA_STATUS_VARIABLE |
3544 3545
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3546
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3547 3548 3549 3550 3551
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
3552
        restore_record(table, s->default_values);
3553 3554
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
3555
        table->field[3]->store((longlong) ((key_info->flags &
3556
                                            HA_NOSAME) ? 0 : 1), TRUE);
3557 3558
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
3559
        table->field[6]->store((longlong) (j+1), TRUE);
3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
        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])
        {
3573
          ha_rows records=(show_table->file->stats.records /
3574
                           key->rec_per_key[j]);
3575
          table->field[9]->store((longlong) records, TRUE);
3576 3577
          table->field[9]->set_notnull();
        }
3578 3579 3580
        if (!(key_info->flags & HA_FULLTEXT) &&
            (key_part->field &&
             key_part->length !=
3581 3582
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
3583
          table->field[10]->store((longlong) key_part->length /
3584
                                  key_part->field->charset()->mbmaxlen, TRUE);
3585 3586 3587 3588 3589 3590 3591
          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);
3592
        if (!show_table->s->keys_in_use.is_set(i))
3593
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3594 3595 3596
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
3597 3598
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3599 3600 3601
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3602
  DBUG_RETURN(res);
3603 3604 3605
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3606 3607 3608 3609
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)
3610 3611 3612
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
3613
  char definer[USER_HOST_BUFF_SIZE];
3614
  uint definer_len;
3615

3616 3617
  if (tables->view)
  {
3618
    Security_context *sctx= thd->security_ctx;
3619
    if (!tables->allowed_show)
3620
    {
3621 3622 3623 3624 3625
      if (!my_strcasecmp(system_charset_info, tables->definer.user.str,
                         sctx->priv_user) &&
          !my_strcasecmp(system_charset_info, tables->definer.host.str,
                         sctx->priv_host))
        tables->allowed_show= TRUE;
3626
    }
3627 3628
    restore_record(table, s->default_values);
    table->field[1]->store(tables->view_db.str, tables->view_db.length, cs);
3629
    table->field[2]->store(tables->view_name.str, tables->view_name.length, cs);
3630
    if (tables->allowed_show)
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
    {
      char buff[2048];
      String qwe_str(buff, sizeof(buff), cs);
      qwe_str.length(0);
      qwe_str.append(STRING_WITH_LEN("/* "));
      append_algorithm(tables, &qwe_str);
      qwe_str.append(STRING_WITH_LEN("*/ "));
      qwe_str.append(tables->query.str, tables->query.length);
      table->field[3]->store(qwe_str.ptr(), qwe_str.length(), cs);
    }
3641

3642 3643 3644 3645
    if (tables->with_check != VIEW_CHECK_NONE)
    {
      if (tables->with_check == VIEW_CHECK_LOCAL)
        table->field[4]->store(STRING_WITH_LEN("LOCAL"), cs);
3646
      else
3647
        table->field[4]->store(STRING_WITH_LEN("CASCADED"), cs);
3648
    }
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
    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)
3666 3667 3668
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                   thd->net.last_errno, thd->net.last_error);
  }
3669 3670
  if (res) 
    thd->clear_error();
3671
  DBUG_RETURN(0);
3672 3673 3674
}


3675 3676 3677
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)
3678 3679
{
  CHARSET_INFO *cs= system_charset_info;
3680
  restore_record(table, s->default_values);
3681 3682 3683 3684 3685
  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);
3686
  return schema_table_store_record(thd, table);
3687 3688 3689
}


gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3690
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
3691 3692 3693
					 TABLE *table, bool res,
					 const char *base_name,
					 const char *file_name)
3694
{
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3695
  DBUG_ENTER("get_schema_constraints_record");
3696 3697 3698 3699 3700 3701 3702 3703 3704
  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)
3705 3706 3707 3708
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3709
    uint primary_key= show_table->s->primary_key;
3710 3711 3712
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3713
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3714 3715
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3716 3717
        continue;

3718
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
3719 3720
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3721 3722
                              strlen(key_info->name),
                              STRING_WITH_LEN("PRIMARY KEY")))
3723 3724
          DBUG_RETURN(1);
      }
3725
      else if (key_info->flags & HA_NOSAME)
3726 3727
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
3728 3729
                              strlen(key_info->name),
                              STRING_WITH_LEN("UNIQUE")))
3730 3731
          DBUG_RETURN(1);
      }
3732 3733 3734 3735 3736 3737 3738
    }

    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++))
    {
3739 3740 3741 3742 3743
      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);
3744 3745
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3746
  DBUG_RETURN(res);
3747 3748 3749
}


3750 3751 3752 3753
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,
3754
                          LEX_STRING *trigger_stmt,
3755 3756
                          ulong sql_mode,
                          LEX_STRING *definer_buffer)
3757 3758
{
  CHARSET_INFO *cs= system_charset_info;
3759 3760 3761
  byte *sql_mode_str;
  ulong sql_mode_len;

3762 3763 3764 3765 3766 3767 3768 3769
  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);
3770
  table->field[10]->store(STRING_WITH_LEN("ROW"), cs);
3771 3772
  table->field[11]->store(trg_action_time_type_names[timing].str,
                          trg_action_time_type_names[timing].length, cs);
3773 3774
  table->field[14]->store(STRING_WITH_LEN("OLD"), cs);
  table->field[15]->store(STRING_WITH_LEN("NEW"), cs);
3775 3776 3777 3778 3779

  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
3780
  table->field[17]->store((const char*)sql_mode_str, sql_mode_len, cs);
3781
  table->field[18]->store((const char *)definer_buffer->str, definer_buffer->length, cs);
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
  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;
3814
        ulong sql_mode;
3815
        char definer_holder[USER_HOST_BUFF_SIZE];
3816 3817
        LEX_STRING definer_buffer;
        definer_buffer.str= definer_holder;
3818 3819
        if (triggers->get_trigger_info(thd, (enum trg_event_type) event,
                                       (enum trg_action_time_type)timing,
3820
                                       &trigger_name, &trigger_stmt,
3821 3822
                                       &sql_mode,
                                       &definer_buffer))
3823
          continue;
3824

3825 3826
        if (store_trigger(thd, table, base_name, file_name, &trigger_name,
                         (enum trg_event_type) event,
3827
                         (enum trg_action_time_type) timing, &trigger_stmt,
3828 3829
                         sql_mode,
                         &definer_buffer))
3830 3831 3832 3833 3834 3835 3836 3837
          DBUG_RETURN(1);
      }
    }
  }
  DBUG_RETURN(0);
}


3838 3839 3840 3841 3842 3843 3844 3845 3846 3847
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);
3848
  table->field[7]->store((longlong) idx, TRUE);
3849 3850 3851
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3852 3853 3854 3855 3856
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)
3857 3858
{
  DBUG_ENTER("get_schema_key_column_usage_record");
3859 3860 3861 3862 3863 3864 3865 3866 3867
  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)
3868 3869 3870 3871
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
3872
    uint primary_key= show_table->s->primary_key;
3873 3874 3875
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
3876
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3877 3878
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3879
        continue;
3880 3881 3882 3883 3884 3885 3886
      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++;
3887
          restore_record(table, s->default_values);
3888 3889 3890 3891 3892 3893
          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);
3894 3895
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
3896 3897 3898 3899 3900 3901 3902 3903 3904
        }
      }
    }

    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++))
    {
3905
      LEX_STRING *f_info;
3906
      LEX_STRING *r_info;
3907 3908 3909 3910 3911
      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++))
      {
3912
        r_info= it1++;
3913
        f_idx++;
3914
        restore_record(table, s->default_values);
3915 3916 3917 3918 3919
        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);
3920
        table->field[8]->store((longlong) f_idx, TRUE);
3921
        table->field[8]->set_notnull();
3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932
        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();
3933 3934
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
3935 3936 3937
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3938
  DBUG_RETURN(res);
3939 3940 3941
}


3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
static void collect_partition_expr(List<char> &field_list, String *str)
{
  List_iterator<char> part_it(field_list);
  ulong no_fields= field_list.elements;
  const char *field_str;
  str->length(0);
  while ((field_str= part_it++))
  {
    str->append(field_str);
    if (--no_fields != 0)
      str->append(",");
  }
  return;
}


3958 3959
static void store_schema_partitions_record(THD *thd, TABLE *schema_table,
                                           TABLE *showing_table,
3960 3961 3962
                                           partition_element *part_elem,
                                           handler *file, uint part_id)
{
3963
  TABLE* table= schema_table;
3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997
  CHARSET_INFO *cs= system_charset_info;
  PARTITION_INFO stat_info;
  TIME time;
  file->get_dynamic_partition_info(&stat_info, part_id);
  table->field[12]->store((longlong) stat_info.records, TRUE);
  table->field[13]->store((longlong) stat_info.mean_rec_length, TRUE);
  table->field[14]->store((longlong) stat_info.data_file_length, TRUE);
  if (stat_info.max_data_file_length)
  {
    table->field[15]->store((longlong) stat_info.max_data_file_length, TRUE);
    table->field[15]->set_notnull();
  }
  table->field[16]->store((longlong) stat_info.index_file_length, TRUE);
  table->field[17]->store((longlong) stat_info.delete_length, TRUE);
  if (stat_info.create_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                              stat_info.create_time);
    table->field[18]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[18]->set_notnull();
  }
  if (stat_info.update_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                              stat_info.update_time);
    table->field[19]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[19]->set_notnull();
  }
  if (stat_info.check_time)
  {
    thd->variables.time_zone->gmt_sec_to_TIME(&time, stat_info.check_time);
    table->field[20]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
    table->field[20]->set_notnull();
  }
3998
  if (file->ha_table_flags() & (ulong) HA_HAS_CHECKSUM)
3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
  {
    table->field[21]->store((longlong) stat_info.check_sum, TRUE);
    table->field[21]->set_notnull();
  }
  if (part_elem)
  {
    if (part_elem->part_comment)
      table->field[22]->store(part_elem->part_comment,
                              strlen(part_elem->part_comment), cs);
    else
      table->field[22]->store(STRING_WITH_LEN("default"), cs);
    if (part_elem->nodegroup_id != UNDEF_NODEGROUP)
      table->field[23]->store((longlong) part_elem->nodegroup_id, TRUE);
    else
      table->field[23]->store(STRING_WITH_LEN("default"), cs);
4014 4015

    table->field[24]->set_notnull();
4016 4017 4018 4019
    if (part_elem->tablespace_name)
      table->field[24]->store(part_elem->tablespace_name,
                              strlen(part_elem->tablespace_name), cs);
    else
4020
    {
4021
      char *ts= showing_table->file->get_tablespace_name(thd,0,0);
4022
      if(ts)
4023
      {
4024
        table->field[24]->store(ts, strlen(ts), cs);
4025 4026
        my_free(ts, MYF(0));
      }
4027 4028 4029
      else
        table->field[24]->set_null();
    }
4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044
  }
  return;
}


static int get_schema_partitions_record(THD *thd, struct st_table_list *tables,
                                        TABLE *table, bool res,
                                        const char *base_name,
                                        const char *file_name)
{
  CHARSET_INFO *cs= system_charset_info;
  char buff[61];
  String tmp_res(buff, sizeof(buff), cs);
  String tmp_str;
  TABLE *show_table= tables->table;
4045
  handler *file;
4046
#ifdef WITH_PARTITION_STORAGE_ENGINE
4047
  partition_info *part_info;
4048
#endif
4049 4050 4051 4052
  DBUG_ENTER("get_schema_partitions_record");

  if (res)
  {
4053
    if (!tables->view)
4054 4055 4056 4057 4058
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
    thd->clear_error();
    DBUG_RETURN(0);
  }
4059
  file= show_table->file;
4060
#ifdef WITH_PARTITION_STORAGE_ENGINE
4061
  part_info= show_table->part_info;
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
  if (part_info)
  {
    partition_element *part_elem;
    List_iterator<partition_element> part_it(part_info->partitions);
    uint part_pos= 0, part_id= 0;
    uint no_parts= part_info->no_parts;

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


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

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

4117
    if (part_info->is_sub_partitioned())
4118 4119
    {
      /* Subpartition method */
4120 4121 4122 4123
      tmp_res.length(0);
      if (part_info->linear_hash_ind)
        tmp_res.append(partition_keywords[PKW_LINEAR].str,
                       partition_keywords[PKW_LINEAR].length);
4124
      if (part_info->list_of_subpart_fields)
4125 4126
        tmp_res.append(partition_keywords[PKW_KEY].str,
                       partition_keywords[PKW_KEY].length);
4127
      else
4128 4129 4130
        tmp_res.append(partition_keywords[PKW_HASH].str, 
                       partition_keywords[PKW_HASH].length);
      table->field[8]->store(tmp_res.ptr(), tmp_res.length(), cs);
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
      table->field[8]->set_notnull();

      /* Subpartition expression */
      if (part_info->subpart_expr)
      {
        table->field[10]->store(part_info->subpart_func_string,
                                part_info->subpart_func_len, cs);
      }
      else if (part_info->list_of_subpart_fields)
      {
        collect_partition_expr(part_info->subpart_field_list, &tmp_str);
        table->field[10]->store(tmp_str.ptr(), tmp_str.length(), cs);
      }
4144
      table->field[10]->set_notnull();
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167
    }

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

      /* Partition description */
      if (part_info->part_type == RANGE_PARTITION)
      {
        if (part_elem->range_value != LONGLONG_MAX)
          table->field[11]->store((longlong) part_elem->range_value, FALSE);
        else
          table->field[11]->store(partition_keywords[PKW_MAXVALUE].str,
                                 partition_keywords[PKW_MAXVALUE].length, cs);
        table->field[11]->set_notnull();
      }
      else if (part_info->part_type == LIST_PARTITION)
      {
4168 4169
        List_iterator<part_elem_value> list_val_it(part_elem->list_val_list);
        part_elem_value *list_value;
4170 4171 4172
        uint no_items= part_elem->list_val_list.elements;
        tmp_str.length(0);
        tmp_res.length(0);
4173 4174 4175 4176 4177 4178
        if (part_elem->has_null_value)
        {
          tmp_str.append("NULL");
          if (no_items > 0)
            tmp_str.append(",");
        }
4179 4180
        while ((list_value= list_val_it++))
        {
4181 4182 4183 4184
          if (!list_value->unsigned_flag)
            tmp_res.set(list_value->value, cs);
          else
            tmp_res.set((ulonglong)list_value->value, cs);
4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
          tmp_str.append(tmp_res);
          if (--no_items != 0)
            tmp_str.append(",");
        };
        table->field[11]->store(tmp_str.ptr(), tmp_str.length(), cs);
        table->field[11]->set_notnull();
      }

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

        while ((subpart_elem= sub_it++))
        {
          table->field[4]->store(subpart_elem->partition_name,
                                 strlen(subpart_elem->partition_name), cs);
          table->field[4]->set_notnull();
          /* SUBPARTITION_ORDINAL_POSITION */
          table->field[6]->store((longlong) ++subpart_pos, TRUE);
          table->field[6]->set_notnull();
          
4208
          store_schema_partitions_record(thd, table, show_table, subpart_elem,
4209 4210 4211 4212 4213 4214 4215 4216
                                         file, part_id);
          part_id++;
          if(schema_table_store_record(thd, table))
            DBUG_RETURN(1);
        }
      }
      else
      {
4217
        store_schema_partitions_record(thd, table, show_table, part_elem,
4218 4219 4220 4221 4222 4223 4224 4225 4226
                                       file, part_id);
        part_id++;
        if(schema_table_store_record(thd, table))
          DBUG_RETURN(1);
      }
    }
    DBUG_RETURN(0);
  }
  else
4227
#endif
4228
  {
4229
    store_schema_partitions_record(thd, table, show_table, 0, file, 0);
4230 4231 4232 4233 4234 4235 4236
    if(schema_table_store_record(thd, table))
      DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}


4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
static interval_type get_real_interval_type(interval_type i_type)
{
  switch (i_type) {
  case INTERVAL_YEAR:
    return INTERVAL_YEAR;

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

  case INTERVAL_WEEK:
  case INTERVAL_DAY:
    return INTERVAL_DAY;

  case INTERVAL_DAY_HOUR:
  case INTERVAL_HOUR:
    return INTERVAL_HOUR;

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

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

  case INTERVAL_DAY_MICROSECOND:
  case INTERVAL_HOUR_MICROSECOND:
  case INTERVAL_MINUTE_MICROSECOND:
  case INTERVAL_SECOND_MICROSECOND:
  case INTERVAL_MICROSECOND:
    return INTERVAL_MICROSECOND;
4273 4274
  case INTERVAL_LAST:
    DBUG_ASSERT(0);
4275 4276 4277 4278 4279
  }
  DBUG_ASSERT(0);
  return INTERVAL_SECOND;
}

4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295

/*
  Loads an event from mysql.event and copies it's data to a row of
  I_S.EVENTS

  Synopsis
    copy_event_to_schema_table()
      thd         Thread
      sch_table   The schema table (information_schema.event)
      event_table The event table to use for loading (mysql.event).

  Returns
    0  OK
    1  Error
*/

4296
int
4297
copy_event_to_schema_table(THD *thd, TABLE *sch_table, TABLE *event_table)
4298 4299 4300 4301
{
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  CHARSET_INFO *scs= system_charset_info;
  TIME time;
4302
  Event_timed et;    
4303 4304 4305 4306
  DBUG_ENTER("fill_events_copy_to_schema_tab");

  restore_record(sch_table, s->default_values);

4307
  if (et.load_from_row(event_table))
4308
  {
4309
    my_error(ER_CANNOT_LOAD_FROM_TABLE, MYF(0));
4310 4311 4312 4313 4314
    DBUG_RETURN(1);
  }

  if (!(!wild || !wild[0] || !wild_compare(et.name.str, wild, 0)))
    DBUG_RETURN(0);
4315 4316 4317 4318 4319 4320

  /*
    Skip events in schemas one does not have access to. The check is
    optimized. It's guaranteed in case of SHOW EVENTS that the user
    has access.
  */
4321
  if (thd->lex->sql_command != SQLCOM_SHOW_EVENTS &&
4322 4323 4324 4325
      check_access(thd, EVENT_ACL, et.dbname.str, 0, 0, 1,
                   is_schema_db(et.dbname.str)))
    DBUG_RETURN(0);

4326
  /* ->field[0] is EVENT_CATALOG and is by default NULL */
4327

4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
  sch_table->field[ISE_EVENT_SCHEMA]->
                                store(et.dbname.str, et.dbname.length,scs);
  sch_table->field[ISE_EVENT_NAME]->
                                store(et.name.str, et.name.length, scs);
  sch_table->field[ISE_DEFINER]->
                                store(et.definer.str, et.definer.length, scs);
  sch_table->field[ISE_EVENT_BODY]->
                                store(STRING_WITH_LEN("SQL"), scs);
  sch_table->field[ISE_EVENT_DEFINITION]->
                                store(et.body.str, et.body.length, scs);

  /* SQL_MODE */
4340
  {
4341
    byte *sql_mode_str;
4342
    ulong sql_mode_len= 0;
4343 4344
    sql_mode_str=
           sys_var_thd_sql_mode::symbolic_mode_representation(thd, et.sql_mode,
4345
                                                              &sql_mode_len);
4346 4347
    sch_table->field[ISE_SQL_MODE]->
                                store((const char*)sql_mode_str, sql_mode_len, scs);
4348
  }
4349

4350 4351
  if (et.expression)
  {
4352
    String show_str;
4353
    /* type */
4354
    sch_table->field[ISE_EVENT_TYPE]->store(STRING_WITH_LEN("RECURRING"), scs);
4355

4356 4357
    if (Events::reconstruct_interval_expression(&show_str, et.interval,
                                                et.expression))
4358
      DBUG_RETURN(1);
4359

4360 4361 4362
    sch_table->field[ISE_INTERVAL_VALUE]->set_notnull();
    sch_table->field[ISE_INTERVAL_VALUE]->
                                store(show_str.ptr(), show_str.length(), scs);
4363 4364

    LEX_STRING *ival= &interval_type_to_name[et.interval];
4365 4366
    sch_table->field[ISE_INTERVAL_FIELD]->set_notnull();
    sch_table->field[ISE_INTERVAL_FIELD]->store(ival->str, ival->length, scs);
4367

4368 4369 4370 4371
    /* starts & ends . STARTS is always set - see sql_yacc.yy */
    sch_table->field[ISE_STARTS]->set_notnull();
    sch_table->field[ISE_STARTS]->
                                store_time(&et.starts, MYSQL_TIMESTAMP_DATETIME);
4372 4373 4374

    if (!et.ends_null)
    {
4375 4376 4377
      sch_table->field[ISE_ENDS]->set_notnull();
      sch_table->field[ISE_ENDS]->
                                store_time(&et.ends, MYSQL_TIMESTAMP_DATETIME);
4378
    }
4379 4380 4381
  }
  else
  {
4382 4383
    /* type */
    sch_table->field[ISE_EVENT_TYPE]->store(STRING_WITH_LEN("ONE TIME"), scs);
4384

4385 4386 4387
    sch_table->field[ISE_EXECUTE_AT]->set_notnull();
    sch_table->field[ISE_EXECUTE_AT]->
                          store_time(&et.execute_at, MYSQL_TIMESTAMP_DATETIME);
4388 4389
  }

4390
  /* status */
4391
  if (et.status == Event_timed::ENABLED)
4392
    sch_table->field[ISE_STATUS]->store(STRING_WITH_LEN("ENABLED"), scs);
4393
  else
4394
    sch_table->field[ISE_STATUS]->store(STRING_WITH_LEN("DISABLED"), scs);
4395

4396
  /* on_completion */
4397
  if (et.on_completion == Event_timed::ON_COMPLETION_DROP)
4398 4399
    sch_table->field[ISE_ON_COMPLETION]->
                                store(STRING_WITH_LEN("NOT PRESERVE"), scs);
4400
  else
4401 4402
    sch_table->field[ISE_ON_COMPLETION]->
                                store(STRING_WITH_LEN("PRESERVE"), scs);
4403 4404 4405 4406
    
  int not_used=0;
  number_to_datetime(et.created, &time, 0, &not_used);
  DBUG_ASSERT(not_used==0);
4407
  sch_table->field[ISE_CREATED]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
4408 4409 4410

  number_to_datetime(et.modified, &time, 0, &not_used);
  DBUG_ASSERT(not_used==0);
4411 4412
  sch_table->field[ISE_LAST_ALTERED]->
                                store_time(&time, MYSQL_TIMESTAMP_DATETIME);
4413 4414

  if (et.last_executed.year)
4415
  {
4416 4417 4418
    sch_table->field[ISE_LAST_EXECUTED]->set_notnull();
    sch_table->field[ISE_LAST_EXECUTED]->
                       store_time(&et.last_executed, MYSQL_TIMESTAMP_DATETIME);
4419
  }
4420

4421 4422
  sch_table->field[ISE_EVENT_COMMENT]->
                      store(et.comment.str, et.comment.length, scs);
4423 4424 4425 4426 4427 4428 4429 4430

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

  DBUG_RETURN(0);
}


4431 4432 4433 4434 4435 4436 4437
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;
4438 4439
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
            && thd->is_fatal_error)
4440 4441 4442 4443
    DBUG_RETURN(1);

  for (; open_list ; open_list=open_list->next)
  {
4444
    restore_record(table, s->default_values);
4445 4446
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
4447 4448
    table->field[2]->store((longlong) open_list->in_use, TRUE);
    table->field[3]->store((longlong) open_list->locked, TRUE);
4449 4450
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
4451 4452 4453 4454 4455 4456 4457 4458
  }
  DBUG_RETURN(0);
}


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
4459
  int res= 0;
4460 4461
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
4462
  pthread_mutex_lock(&LOCK_global_system_variables);
4463
  res= show_status_array(thd, wild, init_vars,
4464
                         lex->option_type, 0, "", tables->table, 0);
4465
  pthread_mutex_unlock(&LOCK_global_system_variables);
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476
  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;
4477
  pthread_mutex_lock(&LOCK_status);
4478 4479
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
4480 4481 4482 4483
  res= show_status_array(thd, wild,
                         (SHOW_VAR *)all_status_vars.buffer,
                         OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ?
4484 4485
                          &tmp: thd->initial_status_var),
                         "", tables->table, 0);
4486
  pthread_mutex_unlock(&LOCK_status);
4487 4488 4489 4490
  DBUG_RETURN(res);
}


4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558
/*
  Fill and store records into I_S.referential_constraints table

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

  RETURN
    0	ok
    #   error
*/

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

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

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

brian@zim.(none)'s avatar
brian@zim.(none) committed
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596
struct schema_table_ref 
{
  const char *table_name;
  ST_SCHEMA_TABLE *schema_table;
};


/*
  Find schema_tables elment by name

  SYNOPSIS
    find_schema_table_in_plugin()
    thd                 thread handler
    plugin              plugin
    table_name          table name

  RETURN
    0	table not found
    1   found the schema table
*/
static my_bool find_schema_table_in_plugin(THD *thd, st_plugin_int *plugin,
                                           void* p_table)
{
  schema_table_ref *p_schema_table= (schema_table_ref *)p_table;
  const char* table_name= p_schema_table->table_name;
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
  DBUG_ENTER("find_schema_table_in_plugin");

  if (!my_strcasecmp(system_charset_info,
                     schema_table->table_name,
                     table_name)) {
    p_schema_table->schema_table= schema_table;
    DBUG_RETURN(1);
  }

  DBUG_RETURN(0);
}

4597

4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612
/*
  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)
{
brian@zim.(none)'s avatar
brian@zim.(none) committed
4613
  schema_table_ref schema_table_a;
4614
  ST_SCHEMA_TABLE *schema_table= schema_tables;
brian@zim.(none)'s avatar
brian@zim.(none) committed
4615 4616
  DBUG_ENTER("find_schema_table");

4617
  for (; schema_table->table_name; schema_table++)
4618 4619 4620 4621
  {
    if (!my_strcasecmp(system_charset_info,
                       schema_table->table_name,
                       table_name))
brian@zim.(none)'s avatar
brian@zim.(none) committed
4622
      DBUG_RETURN(schema_table);
4623
  }
brian@zim.(none)'s avatar
brian@zim.(none) committed
4624 4625 4626 4627 4628 4629 4630

  schema_table_a.table_name= table_name;
  if (plugin_foreach(thd, find_schema_table_in_plugin, 
                     MYSQL_INFORMATION_SCHEMA_PLUGIN, &schema_table_a))
    DBUG_RETURN(schema_table_a.schema_table);

  DBUG_RETURN(NULL);
4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652
}


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

4653
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
4654 4655 4656 4657 4658
{
  int field_count= 0;
  Item *item;
  TABLE *table;
  List<Item> field_list;
4659
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
4660
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
4661
  CHARSET_INFO *cs= system_charset_info;
4662 4663
  DBUG_ENTER("create_schema_table");

4664
  for (; fields_info->field_name; fields_info++)
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
  {
    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;
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695
    case MYSQL_TYPE_DECIMAL:
      if (!(item= new Item_decimal((longlong) fields_info->value, false)))
      {
        DBUG_RETURN(0);
      }
      item->unsigned_flag= (fields_info->field_length/10000)%10;
      item->decimals= fields_info->field_length%10;
      item->max_length= (fields_info->field_length/100)%100;
      if (item->unsigned_flag == 0)
        item->max_length+= 1;
      if (item->decimals > 0)
        item->max_length+= 1;
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
      break;
4696
    default:
4697 4698
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
4699 4700 4701
      {
        DBUG_RETURN(0);
      }
4702
      item->max_length= fields_info->field_length * cs->mbmaxlen;
4703 4704
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
4705 4706 4707 4708 4709 4710 4711
      break;
    }
    field_list.push_back(item);
    item->maybe_null= fields_info->maybe_null;
    field_count++;
  }
  TMP_TABLE_PARAM *tmp_table_param =
4712
    (TMP_TABLE_PARAM*) (thd->alloc(sizeof(TMP_TABLE_PARAM)));
4713
  tmp_table_param->init();
4714
  tmp_table_param->table_charset= cs;
4715
  tmp_table_param->field_count= field_count;
4716
  tmp_table_param->schema_table= 1;
4717 4718 4719 4720 4721
  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),
4722
                                HA_POS_ERROR, table_list->alias)))
4723
    DBUG_RETURN(0);
4724
  table_list->schema_table_param= tmp_table_param;
4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
  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
4740 4741
   1	error
   0	success
4742 4743 4744 4745 4746
*/

int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  ST_FIELD_INFO *field_info= schema_table->fields_info;
4747
  Name_resolution_context *context= &thd->lex->select_lex.context;
4748
  for (; field_info->field_name; field_info++)
4749 4750 4751
  {
    if (field_info->old_name)
    {
4752 4753
      Item_field *field= new Item_field(context,
                                        NullS, NullS, field_info->field_name);
4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
      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;
4773
  Name_resolution_context *context= &sel->context;
4774 4775 4776 4777 4778

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
4779 4780
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4781 4782 4783 4784 4785 4786
    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())
    {
4787
      buffer.append(STRING_WITH_LEN(" ("));
4788
      buffer.append(lex->wild->ptr());
4789
      buffer.append(')');
4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
    }
    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;
4802
  Name_resolution_context *context= &lex->select_lex.context;
4803 4804 4805 4806 4807 4808 4809

  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())
  {
4810
    buffer.append(STRING_WITH_LEN(" ("));
4811
    buffer.append(lex->wild->ptr());
4812
    buffer.append(')');
4813
  }
4814 4815
  Item_field *field= new Item_field(context,
                                    NullS, NullS, field_info->field_name);
4816 4817 4818 4819 4820 4821 4822
  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];
4823
    field= new Item_field(context, NullS, NullS, field_info->field_name);
4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834
    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)
{
4835 4836 4837
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
4838 4839
  Name_resolution_context *context= &thd->lex->select_lex.context;

4840
  for (; *field_num >= 0; field_num++)
4841
  {
4842 4843 4844 4845 4846
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
4847 4848
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4849
    if (field)
4850
    {
4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861
      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;
}


4862 4863 4864 4865 4866
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;
4867 4868
  Name_resolution_context *context= &thd->lex->select_lex.context;

4869 4870 4871
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4872 4873
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886
    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;
}


4887 4888 4889 4890 4891
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;
4892 4893
  Name_resolution_context *context= &thd->lex->select_lex.context;

4894 4895 4896
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
4897 4898
    Item_field *field= new Item_field(context,
                                      NullS, NullS, field_info->field_name);
4899 4900 4901 4902 4903 4904 4905
    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;
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
    }
  }
  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");
4930
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4931 4932 4933
  {
    DBUG_RETURN(1);
  }
4934
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4935
  table->grant.privilege= SELECT_ACL;
4936 4937 4938 4939 4940 4941 4942
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
4943 4944 4945 4946
  if (table_list->schema_table_name)
    table->alias_name_used= my_strcasecmp(table_alias_charset,
                                          table_list->schema_table_name,
                                          table_list->alias);
4947 4948
  table_list->table_name= table->s->table_name.str;
  table_list->table_name_length= table->s->table_name.length;
4949 4950 4951 4952
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4953
  lex->safe_to_cache_query= 0;
4954 4955 4956 4957 4958

  if (table_list->schema_table_reformed) // show command
  {
    SELECT_LEX *sel= lex->current_select;
    Item *item;
4959
    Field_translator *transl, *org_transl;
4960 4961 4962

    if (table_list->field_translation)
    {
4963
      Field_translator *end= table_list->field_translation_end;
4964 4965 4966
      for (transl= table_list->field_translation; transl < end; transl++)
      {
        if (!transl->item->fixed &&
4967
            transl->item->fix_fields(thd, &transl->item))
4968 4969 4970 4971 4972 4973
          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
4974
          (Field_translator*)(thd->stmt_arena->
4975 4976 4977 4978 4979
                              alloc(sel->item_list.elements *
                                    sizeof(Field_translator)))))
    {
      DBUG_RETURN(1);
    }
4980
    for (org_transl= transl; (item= it++); transl++)
4981
    {
4982 4983 4984 4985
      transl->item= item;
      transl->name= item->name;
      if (!item->fixed && item->fix_fields(thd, &transl->item))
      {
4986
        DBUG_RETURN(1);
4987
      }
4988
    }
4989 4990
    table_list->field_translation= org_transl;
    table_list->field_translation_end= transl;
4991 4992
  }

4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
  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");
5017
  DBUG_PRINT("enter", ("mysql_schema_select: %s", schema_table->table_name));
5018
  /*
5019 5020 5021 5022 5023 5024 5025
     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);
5026
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037
      !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);
}


/*
5038
  Fill temporary schema tables before SELECT
5039 5040 5041 5042 5043 5044

  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
5045 5046
    FALSE success
    TRUE  error
5047 5048
*/

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5049
bool get_schema_tables_result(JOIN *join)
5050 5051 5052
{
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
  THD *thd= join->thd;
5053 5054
  LEX *lex= thd->lex;
  bool result= 0;
5055 5056 5057
  DBUG_ENTER("get_schema_tables_result");

  thd->no_warnings_for_error= 1;
5058 5059 5060 5061
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
  {  
    if (!tab->table || !tab->table->pos_in_table_list)
      break;
5062

5063
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
5064
    if (table_list->schema_table && thd->fill_information_schema_tables())
5065
    {
5066 5067
      bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
                          lex->current_select->master_unit()->item);
5068 5069 5070 5071 5072 5073 5074 5075 5076
      /*
        The schema table is already processed and 
        the statement is not a subselect.
        So we don't need to handle this table again.
      */
      if (table_list->is_schema_table_processed && !is_subselect)
        continue;

      if (is_subselect) // is subselect
5077 5078 5079 5080
      {
        table_list->table->file->extra(HA_EXTRA_RESET_STATE);
        table_list->table->file->delete_all_rows();
        free_io_cache(table_list->table);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5081
        filesort_free_buffers(table_list->table,1);
5082
        table_list->table->null_row= 0;
5083 5084
      }
      else
5085
        table_list->table->file->stats.records= 0;
5086

5087 5088
      if (table_list->schema_table->fill_table(thd, table_list,
                                               tab->select_cond))
5089
      {
5090
        result= 1;
5091 5092 5093 5094
        join->error= 1;
        table_list->is_schema_table_processed= TRUE;
        break;
      }
5095
      table_list->is_schema_table_processed= TRUE;
5096 5097
    }
  }
5098
  thd->no_warnings_for_error= 0;
5099
  DBUG_RETURN(result);
5100 5101
}

5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
struct run_hton_fill_schema_files_args
{
  TABLE_LIST *tables;
  COND *cond;
};

static my_bool run_hton_fill_schema_files(THD *thd, st_plugin_int *plugin,
                                          void *arg)
{
  struct run_hton_fill_schema_files_args *args=
    (run_hton_fill_schema_files_args *) arg;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
5113
  handlerton *hton= (handlerton *)plugin->data;
5114
  if(hton->fill_files_table && hton->state == SHOW_OPTION_YES)
5115
    hton->fill_files_table(hton, thd, args->tables, args->cond);
5116 5117 5118 5119 5120 5121
  return false;
}

int fill_schema_files(THD *thd, TABLE_LIST *tables, COND *cond)
{
  TABLE *table= tables->table;
5122
  DBUG_ENTER("fill_schema_files");
5123 5124 5125 5126 5127 5128 5129 5130 5131 5132

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

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

  DBUG_RETURN(0);
}
5133

5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
int fill_schema_status(THD *thd, SHOW_VAR *variables,
                       struct system_status_var *status_var,
                       const char *prefix, TABLE *table)
{
  SHOW_VAR tmp, *var;
  SHOW_TYPE show_type;
  LEX_STRING null_lex_str;
  char buff[SHOW_VAR_FUNC_BUFF_SIZE];
  char name_buf[64], *name_pos;
  int name_len;
  DBUG_ENTER("fill_schema_status");
  
  null_lex_str.str= 0;
  null_lex_str.length= 0;
  
  name_pos= strnmov(name_buf, prefix, sizeof(name_buf) - 1);
  if (*prefix)
    *name_pos++= '_';
  name_len= name_buf + sizeof(name_buf) - name_pos;
  
  for (; variables->name; variables++)
  {
    strnmov(name_pos, variables->name, name_len);
    name_buf[sizeof(name_buf) - 1]= 0;
    make_upper(name_buf);
    
    for (var= variables; var->type == SHOW_FUNC; var= &tmp)
      ((mysql_show_var_func)(var->value))(thd, &tmp, buff);
      
    show_type= var->type;
    
    if (show_type == SHOW_ARRAY)
    {
      fill_schema_status(thd, (SHOW_VAR*) var->value,
                         status_var, name_buf, table);
    }
    else
    {
      char *value= var->value;
      
      restore_record(table, s->default_values);
      table->field[0]->store(name_buf, strlen(name_buf), system_charset_info);
      
      if (show_type == SHOW_SYS)
      {
        show_type= ((sys_var*) value)->type();
        value= (char*) ((sys_var*) value)->value_ptr(thd, OPT_GLOBAL,
                                                     &null_lex_str);
      }
                                                     
      switch (show_type)
      {
      case SHOW_DOUBLE_STATUS:
        value= (char*) status_var + (ulong) value;
        table->field[1]->store(*(double*) value);
        break;
      case SHOW_LONG_STATUS:
        value= (char*) status_var + (ulong) value;
        /* fall through */
      case SHOW_LONG:
      case SHOW_LONG_NOFLUSH: /* the difference lies in refresh_status() */
        table->field[1]->store((longlong) *(long*) value, false);
        break;
      case SHOW_LONGLONG:
        table->field[1]->store(*(longlong*) value, false);
        break;
      case SHOW_HA_ROWS:
        table->field[1]->store((longlong) *(ha_rows*) value, false);
        break;
      case SHOW_BOOL:
        table->field[1]->store((longlong) *(bool*) value, false);
        break;
      case SHOW_MY_BOOL:
        table->field[1]->store((longlong) *(my_bool*) value, false);
        break;
      case SHOW_INT:
        table->field[1]->store((longlong) *(uint32*) value, false);
        break;
      case SHOW_HAVE: /* always displayed as 0 */
        table->field[1]->store((longlong) 0, false);
        break;
      case SHOW_CHAR_PTR:
        value= *(char**) value;
        /* fall through */
      case SHOW_CHAR: /* always displayed as 0 */
        table->field[1]->store((longlong) 0, false);
        break;
      case SHOW_KEY_CACHE_LONG:
        value= (char*) dflt_key_cache + (ulong) value;
        table->field[1]->store((longlong) *(long*) value, false);
        break;
      case SHOW_KEY_CACHE_LONGLONG:
        value= (char*) dflt_key_cache + (ulong) value;
        table->field[1]->store(*(longlong*) value, false);
        break;
      case SHOW_UNDEF: /* always displayed as 0 */
        table->field[1]->store((longlong) 0, false);
        break;
      case SHOW_SYS: /* cannot happen */
      default:
        DBUG_ASSERT(0);
        break;
      }
      
      table->field[1]->set_notnull();
      if (schema_table_store_record(thd, table))
        DBUG_RETURN(1);
    }
  }
  
  DBUG_RETURN(0);
}

int fill_schema_global_status(THD *thd, TABLE_LIST *tables, COND *cond)
{
  STATUS_VAR tmp;
  int res= 0;
  DBUG_ENTER("fill_schema_global_status");
  
  pthread_mutex_lock(&LOCK_status);
  calc_sum_of_all_status(&tmp);
  res= fill_schema_status(thd, (SHOW_VAR*) all_status_vars.buffer,
                          &tmp, "", tables->table);
  pthread_mutex_unlock(&LOCK_status);
  
  DBUG_RETURN(res);
}

int fill_schema_session_status(THD *thd, TABLE_LIST *tables, COND *cond)
{
  int res= 0;
  DBUG_ENTER("fill_schema_session_status");
  
  pthread_mutex_lock(&LOCK_status);
  res= fill_schema_status(thd, (SHOW_VAR*) all_status_vars.buffer,
                          &thd->status_var, "", tables->table);
  pthread_mutex_unlock(&LOCK_status);
  
  DBUG_RETURN(res);
}

int fill_schema_global_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  int res= 0;
  DBUG_ENTER("fill_schema_global_variables");
  
  pthread_mutex_lock(&LOCK_global_system_variables);
  res= show_status_array(thd, "", init_vars, OPT_GLOBAL,
                         NULL, "", tables->table, 1);
  pthread_mutex_unlock(&LOCK_global_system_variables);
  
  DBUG_RETURN(res);
}

int fill_schema_session_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  int res= 0;
  DBUG_ENTER("fill_schema_session_variables");
  
  pthread_mutex_lock(&LOCK_global_system_variables);
  res= show_status_array(thd, "", init_vars, OPT_SESSION,
                         NULL, "", tables->table, 1);
  pthread_mutex_unlock(&LOCK_global_system_variables);
  
  DBUG_RETURN(res);
}

5301 5302
ST_FIELD_INFO schema_fields_info[]=
{
5303 5304
  {"CATALOG_NAME", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SCHEMA_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
5305
  {"DEFAULT_CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
5306
  {"DEFAULT_COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
5307
  {"SQL_PATH", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
5308
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5309 5310 5311 5312 5313
};


ST_FIELD_INFO tables_fields_info[]=
{
5314 5315 5316 5317 5318 5319 5320
  {"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"},
5321
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
5322 5323 5324 5325 5326 5327 5328 5329 5330
  {"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"},
5331
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
5332 5333
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
5334
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
5335
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5336 5337 5338 5339 5340
};


ST_FIELD_INFO columns_fields_info[]=
{
5341 5342 5343 5344 5345
  {"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},
5346
  {"COLUMN_DEFAULT", MAX_FIELD_VARCHARLENGTH, MYSQL_TYPE_STRING, 0, 1, "Default"},
5347
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
5348
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
5349 5350
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
5351 5352
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
5353 5354
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
5355
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
5356
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
5357 5358
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
5359
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
5360
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5361 5362 5363 5364 5365
};


ST_FIELD_INFO charsets_fields_info[]=
{
5366 5367
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
  {"DEFAULT_COLLATE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Default collation"},
5368
  {"DESCRIPTION", 60, MYSQL_TYPE_STRING, 0, 0, "Description"},
5369
  {"MAXLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Maxlen"},
5370
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5371 5372 5373 5374 5375
};


ST_FIELD_INFO collation_fields_info[]=
{
5376 5377
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
5378
  {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"},
5379 5380
  {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"},
  {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"},
5381
  {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"},
5382
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5383 5384 5385
};


5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397
ST_FIELD_INFO engines_fields_info[]=
{
  {"ENGINE", 64, MYSQL_TYPE_STRING, 0, 0, "Engine"},
  {"SUPPORT", 8, MYSQL_TYPE_STRING, 0, 0, "Support"},
  {"COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
  {"TRANSACTIONS", 3, MYSQL_TYPE_STRING, 0, 0, "Transactions"},
  {"XA", 3, MYSQL_TYPE_STRING, 0, 0, "XA"},
  {"SAVEPOINTS", 3 ,MYSQL_TYPE_STRING, 0, 0, "Savepoints"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5398 5399 5400 5401 5402 5403
ST_FIELD_INFO events_fields_info[]=
{
  {"EVENT_CATALOG", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EVENT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Db"},
  {"EVENT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Name"},
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5404 5405
  {"EVENT_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
  {"EVENT_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5406 5407
  {"EVENT_TYPE", 9, MYSQL_TYPE_STRING, 0, 0, "Type"},
  {"EXECUTE_AT", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Execute at"},
5408
  {"INTERVAL_VALUE", 256, MYSQL_TYPE_STRING, 0, 1, "Interval value"},
5409
  {"INTERVAL_FIELD", 18, MYSQL_TYPE_STRING, 0, 1, "Interval field"},
5410
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423
  {"STARTS", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Starts"},
  {"ENDS", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Ends"},
  {"STATUS", 8, MYSQL_TYPE_STRING, 0, 0, "Status"},
  {"ON_COMPLETION", 12, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, 0},
  {"LAST_EXECUTED", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"EVENT_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};



5424 5425
ST_FIELD_INFO coll_charset_app_fields_info[]=
{
5426 5427
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
5428
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5429 5430 5431 5432 5433
};


ST_FIELD_INFO proc_fields_info[]=
{
5434 5435 5436 5437 5438
  {"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"},
5439 5440
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
5441
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
5442 5443
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
5444
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
5445 5446 5447 5448
  {"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"},
5449 5450
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
5451
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
5452
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
5453
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5454
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5455 5456 5457 5458 5459
};


ST_FIELD_INFO stat_fields_info[]=
{
5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475
  {"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}
5476 5477 5478 5479 5480
};


ST_FIELD_INFO view_fields_info[]=
{
5481 5482 5483 5484
  {"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},
5485
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
5486
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
5487 5488
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, 0},
  {"SECURITY_TYPE", 7, MYSQL_TYPE_STRING, 0, 0, 0},
5489
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5490 5491 5492 5493 5494
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
5495 5496 5497 5498 5499
  {"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}
5500 5501 5502 5503 5504
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
5505 5506 5507 5508 5509 5510
  {"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}
5511 5512 5513 5514 5515
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
5516 5517 5518 5519 5520 5521 5522
  {"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}
5523 5524 5525 5526 5527
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
5528 5529 5530 5531 5532 5533 5534 5535
  {"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}
5536 5537 5538 5539 5540
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
5541 5542 5543 5544 5545 5546 5547
  {"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}
5548 5549 5550 5551 5552
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
5553 5554 5555
  {"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},
5556
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
5557 5558 5559 5560
  {"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},
5561
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
5562 5563 5564
  {"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},
5565
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
5566 5567 5568 5569 5570
};


ST_FIELD_INFO table_names_fields_info[]=
{
5571 5572 5573 5574 5575
  {"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}
5576 5577 5578
};


5579 5580 5581 5582 5583 5584 5585 5586 5587 5588
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}
};


5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607
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"},
5608
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, "sql_mode"},
5609
  {"DEFINER", 65535, MYSQL_TYPE_STRING, 0, 0, "Definer"},
5610 5611 5612 5613
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5614 5615 5616 5617 5618 5619 5620 5621 5622 5623
ST_FIELD_INFO partitions_fields_info[]=
{
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA",NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"PARTITION_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SUBPARTITION_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PARTITION_ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"SUBPARTITION_ORDINAL_POSITION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"PARTITION_METHOD", 12, MYSQL_TYPE_STRING, 0, 1, 0},
5624
  {"SUBPARTITION_METHOD", 12, MYSQL_TYPE_STRING, 0, 1, 0},
5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638
  {"PARTITION_EXPRESSION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SUBPARTITION_EXPRESSION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PARTITION_DESCRIPTION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 0, 0},
  {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"PARTITION_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, 0},
5639
  {"NODEGROUP", 12 , MYSQL_TYPE_STRING, 0, 0, 0},
5640
  {"TABLESPACE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
5641 5642 5643 5644
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5645 5646 5647
ST_FIELD_INFO variables_fields_info[]=
{
  {"Variable_name", 80, MYSQL_TYPE_STRING, 0, 0, "Variable_name"},
5648
  {"Value", FN_REFLEN, MYSQL_TYPE_STRING, 0, 0, "Value"},
5649 5650 5651 5652
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664
ST_FIELD_INFO status_fields_info[]=
{
  {"VARIABLE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Variable_name"},
  {"VARIABLE_VALUE", 2207, MYSQL_TYPE_DECIMAL, 0, 0, "Value"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


ST_FIELD_INFO system_variables_fields_info[]=
{
  {"VARIABLE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Variable_name"},
  {"VARIABLE_VALUE", 65535, MYSQL_TYPE_STRING, 0, 1, "Value"},
5665 5666 5667 5668
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5669 5670 5671 5672 5673 5674 5675
ST_FIELD_INFO processlist_fields_info[]=
{
  {"ID", 4, MYSQL_TYPE_LONG, 0, 0, "Id"},
  {"USER", 16, MYSQL_TYPE_STRING, 0, 0, "User"},
  {"HOST", LIST_PROCESS_HOST_LEN,  MYSQL_TYPE_STRING, 0, 0, "Host"},
  {"DB", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Db"},
  {"COMMAND", 16, MYSQL_TYPE_STRING, 0, 0, "Command"},
5676
  {"TIME", 7, MYSQL_TYPE_LONG, 0, 0, "Time"},
5677
  {"STATE", 64, MYSQL_TYPE_STRING, 0, 1, "State"},
5678
  {"INFO", PROCESS_LIST_INFO_WIDTH, MYSQL_TYPE_STRING, 0, 1, "Info"},
5679 5680 5681 5682
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5683 5684 5685
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
5686
  {"PLUGIN_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5687
  {"PLUGIN_STATUS", 10, MYSQL_TYPE_STRING, 0, 0, "Status"},
5688
  {"PLUGIN_TYPE", 80, MYSQL_TYPE_STRING, 0, 0, "Type"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
5689
  {"PLUGIN_TYPE_VERSION", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5690
  {"PLUGIN_LIBRARY", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Library"},
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
5691
  {"PLUGIN_LIBRARY_VERSION", 20, MYSQL_TYPE_STRING, 0, 1, 0},
5692 5693
  {"PLUGIN_AUTHOR", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"PLUGIN_DESCRIPTION", 65535, MYSQL_TYPE_STRING, 0, 1, 0},
5694
  {"PLUGIN_LICENSE", 80, MYSQL_TYPE_STRING, 0, 1, "License"},
5695 5696 5697
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};

5698 5699 5700
ST_FIELD_INFO files_fields_info[]=
{
  {"FILE_ID", 4, MYSQL_TYPE_LONG, 0, 0, 0},
5701
  {"FILE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
5702
  {"FILE_TYPE", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5703 5704 5705 5706 5707 5708
  {"TABLESPACE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_CATALOG", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"LOGFILE_GROUP_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"LOGFILE_GROUP_NUMBER", 4, MYSQL_TYPE_LONG, 0, 1, 0},
5709
  {"ENGINE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
5710 5711 5712 5713 5714
  {"FULLTEXT_KEYS", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"DELETED_ROWS", 4, MYSQL_TYPE_LONG, 0, 1, 0},
  {"UPDATE_COUNT", 4, MYSQL_TYPE_LONG, 0, 1, 0},
  {"FREE_EXTENTS", 4, MYSQL_TYPE_LONG, 0, 1, 0},
  {"TOTAL_EXTENTS", 4, MYSQL_TYPE_LONG, 0, 1, 0},
5715
  {"EXTENT_SIZE", 4, MYSQL_TYPE_LONG, 0, 0, 0},
5716 5717 5718 5719 5720 5721 5722 5723
  {"INITIAL_SIZE", 21, MYSQL_TYPE_LONG, 0, 1, 0},
  {"MAXIMUM_SIZE", 21, MYSQL_TYPE_LONG, 0, 1, 0},
  {"AUTOEXTEND_SIZE", 21, MYSQL_TYPE_LONG, 0, 1, 0},
  {"CREATION_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"LAST_UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"LAST_ACCESS_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, 0},
  {"RECOVER_TIME", 4, MYSQL_TYPE_LONG, 0, 1, 0},
  {"TRANSACTION_COUNTER", 4, MYSQL_TYPE_LONG, 0, 1, 0},
5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736
  {"VERSION", 21 , MYSQL_TYPE_LONG, 0, 1, "Version"},
  {"ROW_FORMAT", 10, MYSQL_TYPE_STRING, 0, 1, "Row_format"},
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
  {"AVG_ROW_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Avg_row_length"},
  {"DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_length"},
  {"MAX_DATA_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Max_data_length"},
  {"INDEX_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, "Index_length"},
  {"DATA_FREE", 21 , MYSQL_TYPE_LONG, 0, 1, "Data_free"},
  {"CREATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Create_time"},
  {"UPDATE_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Update_time"},
  {"CHECK_TIME", 0, MYSQL_TYPE_TIMESTAMP, 0, 1, "Check_time"},
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"STATUS", 20, MYSQL_TYPE_STRING, 0, 0, 0},
5737
  {"EXTRA", 255, MYSQL_TYPE_STRING, 0, 1, 0},
5738 5739
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};
5740

5741 5742 5743 5744 5745 5746 5747 5748 5749 5750
void init_fill_schema_files_row(TABLE* table)
{
  int i;
  for(i=0; files_fields_info[i].field_name!=NULL; i++)
    table->field[i]->set_null();

  table->field[IS_FILES_STATUS]->set_notnull();
  table->field[IS_FILES_STATUS]->store("NORMAL", 6, system_charset_info);
}

5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766
ST_FIELD_INFO referential_constraints_fields_info[]=
{
  {"CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UNIQUE_CONSTRAINT_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"UNIQUE_CONSTRAINT_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UNIQUE_CONSTRAINT_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"MATCH_OPTION", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"UPDATE_RULE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"DELETE_RULE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


5767 5768
/*
  Description of ST_FIELD_INFO in table.h
5769 5770 5771

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

5772 5773 5774 5775 5776
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
5777
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
5778
  {"COLLATIONS", collation_fields_info, create_schema_table, 
5779
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
5780
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
5781
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
5782 5783
  {"COLUMNS", columns_fields_info, create_schema_table, 
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
5784
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
5785
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
5786
  {"ENGINES", engines_fields_info, create_schema_table,
5787
   fill_schema_engines, make_old_format, 0, -1, -1, 0},
5788
  {"EVENTS", events_fields_info, create_schema_table,
5789
   Events::fill_schema_events, make_old_format, 0, -1, -1, 0},
5790 5791
  {"FILES", files_fields_info, create_schema_table,
   fill_schema_files, 0, 0, -1, -1, 0},
5792 5793 5794 5795
  {"GLOBAL_STATUS", status_fields_info, create_schema_table,
   fill_schema_global_status, make_old_format, 0, -1, -1, 0},
  {"GLOBAL_VARIABLES", system_variables_fields_info, create_schema_table,
   fill_schema_global_variables, make_old_format, 0, -1, -1, 0},
5796
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
5797
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
5798 5799
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
5800 5801
  {"PARTITIONS", partitions_fields_info, create_schema_table,
   get_all_tables, 0, get_schema_partitions_record, 1, 2, 0},
5802 5803
  {"PLUGINS", plugin_fields_info, create_schema_table,
    fill_plugins, make_old_format, 0, -1, -1, 0},
5804 5805
  {"PROCESSLIST", processlist_fields_info, create_schema_table,
    fill_schema_processlist, make_old_format, 0, -1, -1, 0},
5806 5807 5808
  {"REFERENTIAL_CONSTRAINTS", referential_constraints_fields_info,
   create_schema_table, get_all_tables, 0, get_referential_constraints_record,
   1, 9, 0},
5809 5810 5811 5812 5813 5814
  {"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},
5815 5816 5817 5818
  {"SESSION_STATUS", status_fields_info, create_schema_table,
    fill_schema_session_status, make_old_format, 0, -1, -1, 0},
  {"SESSION_VARIABLES", system_variables_fields_info, create_schema_table,
    fill_schema_session_variables, make_old_format, 0, -1, -1, 0},
5819 5820
  {"STATISTICS", stat_fields_info, create_schema_table, 
    get_all_tables, make_old_format, get_schema_stat_record, 1, 2, 0},
5821 5822
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
5823 5824 5825 5826 5827 5828 5829 5830
  {"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},
5831
  {"TRIGGERS", triggers_fields_info, create_schema_table,
5832
   get_all_tables, make_old_format, get_schema_triggers_record, 5, 6, 0},
5833 5834
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
5835 5836
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
5837 5838
  {"VIEWS", view_fields_info, create_schema_table, 
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
5839
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
5840 5841 5842
};


5843
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5844
template class List_iterator_fast<char>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5845 5846
template class List<char>;
#endif
brian@zim.(none)'s avatar
brian@zim.(none) committed
5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897

int initialize_schema_table(st_plugin_int *plugin)
{
  ST_SCHEMA_TABLE *schema_table;
  DBUG_ENTER("initialize_schema_table");

  if (!(schema_table= (ST_SCHEMA_TABLE *)my_malloc(sizeof(ST_SCHEMA_TABLE),
                                MYF(MY_WME | MY_ZEROFILL))))
      DBUG_RETURN(1);
  /* Historical Requirement */
  plugin->data= schema_table; // shortcut for the future
  if (plugin->plugin->init)
  {
    schema_table->create_table= create_schema_table;
    schema_table->old_format= make_old_format;
    schema_table->idx_field1= -1, 
    schema_table->idx_field2= -1; 

    if (plugin->plugin->init(schema_table))
    {
      sql_print_error("Plugin '%s' init function returned error.",
                      plugin->name.str);
      goto err;
    }
    schema_table->table_name= plugin->name.str;
  }

  DBUG_RETURN(0);
err:
  my_free((gptr)schema_table, MYF(0));
  DBUG_RETURN(1);
}

int finalize_schema_table(st_plugin_int *plugin)
{
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
  DBUG_ENTER("finalize_schema_table");

  if (schema_table && plugin->plugin->deinit)
  {
    DBUG_PRINT("info", ("Deinitializing plugin: '%s'", plugin->name.str));
    if (plugin->plugin->deinit(NULL))
    {
      DBUG_PRINT("warning", ("Plugin '%s' deinit function returned error.",
                             plugin->name.str));
    }
    my_free((gptr)schema_table, MYF(0));
  }

  DBUG_RETURN(0);
}