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

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

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

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


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

#include "mysql_priv.h"
21
#include "sql_select.h"                         // For select_describe
22
#include "repl_failsafe.h"
23
#include "sp_head.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
24
#include <my_dir.h>
25

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
26 27 28 29
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"			// For berkeley_show_logs
#endif

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

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

static int
41
store_create_info(THD *thd, TABLE_LIST *table_list, String *packet);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
42 43
static int
view_store_create_info(THD *thd, TABLE_LIST *table, String *packet);
44
static bool schema_table_store_record(THD *thd, TABLE *table);
45

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

47 48 49 50
/***************************************************************************
** List all table types supported 
***************************************************************************/

51
bool mysqld_show_storage_engines(THD *thd)
52 53
{
  List<Item> field_list;
54
  Protocol *protocol= thd->protocol;
55
  DBUG_ENTER("mysqld_show_storage_engines");
56

monty@mishka.local's avatar
monty@mishka.local committed
57
  field_list.push_back(new Item_empty_string("Engine",10));
58
  field_list.push_back(new Item_empty_string("Support",10));
59
  field_list.push_back(new Item_empty_string("Comment",80));
60

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

65
  const char *default_type_name= 
66
    ha_get_storage_engine((enum db_type)thd->variables.table_type);
67

68 69
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
70
  {
71
    protocol->prepare_for_resend();
72
    protocol->store(types->type, system_charset_info);
73 74 75
    const char *option_name= show_comp_option_name[(int) *types->value];

    if (*types->value == SHOW_OPTION_YES &&
76
	!my_strcasecmp(system_charset_info, default_type_name, types->type))
77
      option_name= "DEFAULT";
78 79
    protocol->store(option_name, system_charset_info);
    protocol->store(types->comment, system_charset_info);
80
    if (protocol->write())
81
      DBUG_RETURN(TRUE);
82
  }
83
  send_eof(thd);
84
  DBUG_RETURN(FALSE);
85 86
}

87

88
/***************************************************************************
89
 List all privileges supported
90 91
***************************************************************************/

92 93 94 95
struct show_privileges_st {
  const char *privilege;
  const char *context;
  const char *comment;
96 97
};

98 99
static struct show_privileges_st sys_privileges[]=
{
100
  {"Alter", "Tables",  "To alter the table"},
101
  {"Alter routine", "Functions,Procedures",  "To alter or drop stored functions/procedures"},
102
  {"Create", "Databases,Tables,Indexes",  "To create new databases and tables"},
103
  {"Create routine","Functions,Procedures","To use CREATE FUNCTION/PROCEDURE"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
104 105
  {"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
  {"Create view", "Tables",  "To create new views"},
106
  {"Create user", "Server Admin",  "To create new users"},
107
  {"Delete", "Tables",  "To delete existing rows"},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
108
  {"Drop", "Databases,Tables", "To drop databases, tables, and views"},
109
  {"Execute", "Functions,Procedures", "To execute stored routines"},
110
  {"File", "File access on server",   "To read and write files on the server"},
111
  {"Grant option",  "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
112 113 114 115
  {"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"},
116
  {"References", "Databases,Tables", "To have references on tables"},
117 118 119 120 121
  {"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
122 123
  {"Show view","Tables","To see views with SHOW CREATE VIEW"},
  {"Shutdown","Server Admin", "To shut down the server"},
124 125 126
  {"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
  {"Update", "Tables",  "To update existing rows"},
  {"Usage","Server Admin","No privileges - allow connect only"},
127 128 129
  {NullS, NullS, NullS}
};

130
bool mysqld_show_privileges(THD *thd)
131 132
{
  List<Item> field_list;
133
  Protocol *protocol= thd->protocol;
134 135 136 137 138 139
  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));

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

144 145
  show_privileges_st *privilege= sys_privileges;
  for (privilege= sys_privileges; privilege->privilege ; privilege++)
146
  {
147
    protocol->prepare_for_resend();
148 149 150
    protocol->store(privilege->privilege, system_charset_info);
    protocol->store(privilege->context, system_charset_info);
    protocol->store(privilege->comment, system_charset_info);
151
    if (protocol->write())
152
      DBUG_RETURN(TRUE);
153
  }
154
  send_eof(thd);
155
  DBUG_RETURN(FALSE);
156 157 158 159
}


/***************************************************************************
160
  List all column types
161 162
***************************************************************************/

163 164
struct show_column_type_st
{
165 166
  const char *type;
  uint size;
167 168 169 170 171 172 173 174 175 176 177 178
  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;
179
};
180 181 182 183 184

/* TODO: Add remaning types */

static struct show_column_type_st sys_column_types[]=
{
185 186
  {"tinyint",
    1,  "-128",  "127",  0,  0,  "YES",  "YES",
187 188
    "NO",   "YES", "YES",  "NO",  "NULL,0",
    "A very small integer"},
189
  {"tinyint unsigned",
190 191
    1,  "0"   ,  "255",  0,  0,  "YES",  "YES",
    "YES",  "YES",  "YES",  "NO",  "NULL,0",
192 193 194
    "A very small integer"},
};

195
bool mysqld_show_column_types(THD *thd)
196 197
{
  List<Item> field_list;
198
  Protocol *protocol= thd->protocol;
199 200 201 202 203 204
  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));
205 206
  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));
207 208 209 210 211 212 213 214 215
  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));

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

220
  /* TODO: Change the loop to not use 'i' */
221 222
  for (uint i=0; i < sizeof(sys_column_types)/sizeof(sys_column_types[0]); i++)
  {
223
    protocol->prepare_for_resend();
224
    protocol->store(sys_column_types[i].type, system_charset_info);
225
    protocol->store((ulonglong) sys_column_types[i].size);
226 227
    protocol->store(sys_column_types[i].min_value, system_charset_info);
    protocol->store(sys_column_types[i].max_value, system_charset_info);
228 229
    protocol->store_short((longlong) sys_column_types[i].precision);
    protocol->store_short((longlong) sys_column_types[i].scale);
230 231 232 233 234 235 236 237
    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);
238
    if (protocol->write())
239
      DBUG_RETURN(TRUE);
240
  }
241
  send_eof(thd);
242
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
243 244 245
}


246
int
bk@work.mysql.com's avatar
bk@work.mysql.com committed
247
mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path,
248
                 const char *wild, bool dir)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
249 250 251 252 253
{
  uint i;
  char *ext;
  MY_DIR *dirp;
  FILEINFO *file;
254
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
255
  uint col_access=thd->col_access;
256
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
257 258 259
  TABLE_LIST table_list;
  DBUG_ENTER("mysql_find_files");

260 261
  if (wild && !wild[0])
    wild=0;
262

bk@work.mysql.com's avatar
bk@work.mysql.com committed
263 264 265 266 267
  bzero((char*) &table_list,sizeof(table_list));

  if (!(dirp = my_dir(path,MYF(MY_WME | (dir ? MY_WANT_STAT : 0)))))
    DBUG_RETURN(-1);

268
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
269 270 271
  {
    file=dirp->dir_entry+i;
    if (dir)
272
    {                                           /* Return databases */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
273 274
#ifdef USE_SYMDIR
      char *ext;
275
      char buff[FN_REFLEN];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
276
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
277 278
      {
	/* Only show the sym file if it points to a directory */
279
	char *end;
280
        *ext=0;                                 /* Remove extension */
281 282 283 284
	unpack_dirname(buff, file->name);
	end= strend(buff);
	if (end != buff && end[-1] == FN_LIBCHAR)
	  end[-1]= 0;				// Remove end FN_LIBCHAR
285 286 287
        if (!my_stat(buff, file->mystat, MYF(0)))
               continue;
       }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
288
#endif
dlenev@mysql.com's avatar
dlenev@mysql.com committed
289
        if (file->name[0] == '.' || !MY_S_ISDIR(file->mystat->st_mode) ||
290
            (wild && wild_compare(file->name,wild,0)))
291
          continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
292 293 294
    }
    else
    {
295
        // Return only .frm files which aren't temp files.
296
      if (my_strcasecmp(system_charset_info, ext=fn_ext(file->name),reg_ext) ||
297
          is_prefix(file->name,tmp_file_prefix))
298
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
299
      *ext=0;
300 301 302 303
      if (wild)
      {
	if (lower_case_table_names)
	{
304
	  if (wild_case_compare(files_charset_info, file->name, wild))
305 306
	    continue;
	}
307
	else if (wild_compare(file->name,wild,0))
308 309
	  continue;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
310
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
311
#ifndef NO_EMBEDDED_ACCESS_CHECKS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
312 313 314 315
    /* Don't show tables where we don't have any privileges */
    if (db && !(col_access & TABLE_ACLS))
    {
      table_list.db= (char*) db;
316
      table_list.db_length= strlen(db);
317
      table_list.table_name= file->name;
318
      table_list.table_name_length= strlen(file->name);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
319
      table_list.grant.privilege=col_access;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
320
      if (check_grant(thd, TABLE_ACLS, &table_list, 1, UINT_MAX, 1))
321
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
322
    }
hf@deer.(none)'s avatar
hf@deer.(none) committed
323
#endif
324
    if (files->push_back(thd->strdup(file->name)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
325 326 327 328 329 330 331
    {
      my_dirend(dirp);
      DBUG_RETURN(-1);
    }
  }
  DBUG_PRINT("info",("found: %d files", files->elements));
  my_dirend(dirp);
332 333 334

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
335 336 337
  DBUG_RETURN(0);
}

338

339
bool
bk@work.mysql.com's avatar
bk@work.mysql.com committed
340 341 342
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
  TABLE *table;
343 344 345
  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
346 347
  DBUG_ENTER("mysqld_show_create");
  DBUG_PRINT("enter",("db: %s  table: %s",table_list->db,
348
                      table_list->table_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
349

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
350
  /* Only one table for now, but VIEW can involve several tables */
351
  if (open_and_lock_tables(thd, table_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
352
  {
353
    DBUG_RETURN(TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
354
  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
355 356 357
  /* TODO: add environment variables show when it become possible */
  if (thd->lex->only_view && !table_list->view)
  {
358
    my_error(ER_WRONG_OBJECT, MYF(0),
359
             table_list->db, table_list->table_name, "VIEW");
360
    DBUG_RETURN(TRUE);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
361
  }
362

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
363
  table= table_list->table;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
364

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
365 366
  if ((table_list->view ?
       view_store_create_info(thd, table_list, &buffer) :
367
       store_create_info(thd, table_list, &buffer)))
368
    DBUG_RETURN(TRUE);
369

bk@work.mysql.com's avatar
bk@work.mysql.com committed
370
  List<Item> field_list;
371 372 373 374 375 376 377 378 379 380 381 382 383
  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
384

385 386
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
387
    DBUG_RETURN(TRUE);
388
  protocol->prepare_for_resend();
389
  buffer.length(0);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
390 391 392 393
  if (table_list->view)
  {
    protocol->store(table_list->view_name.str, system_charset_info);
    if (view_store_create_info(thd, table_list, &buffer))
394
      DBUG_RETURN(TRUE);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
395 396 397
  }
  else
  {
398
    if (table_list->schema_table)
399
      protocol->store(table_list->schema_table_name, system_charset_info);
400
    else
401
      protocol->store(table->alias, system_charset_info);
402
    if (store_create_info(thd, table_list, &buffer))
403
      DBUG_RETURN(TRUE);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
404
  }
405
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
406

407
  if (protocol->write())
408
    DBUG_RETURN(TRUE);
409
  send_eof(thd);
410
  DBUG_RETURN(FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
411 412
}

413 414
bool mysqld_show_create_db(THD *thd, char *dbname,
                           HA_CREATE_INFO *create_info)
415 416 417 418 419
{
  int length;
  char	path[FN_REFLEN];
  char buff[2048];
  String buffer(buff, sizeof(buff), system_charset_info);
420
#ifndef NO_EMBEDDED_ACCESS_CHECKS
421
  uint db_access;
422
#endif
423 424 425 426 427 428 429 430
  bool found_libchar;
  HA_CREATE_INFO create;
  uint create_options = create_info ? create_info->options : 0;
  Protocol *protocol=thd->protocol;
  DBUG_ENTER("mysql_show_create_db");

  if (check_db_name(dbname))
  {
431
    my_error(ER_WRONG_DB_NAME, MYF(0), dbname);
432
    DBUG_RETURN(TRUE);
433 434 435 436 437 438 439 440 441 442
  }

#ifndef NO_EMBEDDED_ACCESS_CHECKS
  if (test_all_bits(thd->master_access,DB_ACLS))
    db_access=DB_ACLS;
  else
    db_access= (acl_get(thd->host,thd->ip, thd->priv_user,dbname,0) |
		thd->master_access);
  if (!(db_access & DB_ACLS) && (!grant_option || check_grant_db(thd,dbname)))
  {
443 444
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
             thd->priv_user, thd->host_or_ip, dbname);
445 446
    mysql_log.write(thd,COM_INIT_DB,ER(ER_DBACCESS_DENIED_ERROR),
		    thd->priv_user, thd->host_or_ip, dbname);
447
    DBUG_RETURN(TRUE);
448 449 450 451 452 453 454 455 456 457 458 459 460
  }
#endif

  (void) sprintf(path,"%s/%s",mysql_data_home, dbname);
  length=unpack_dirname(path,path);		// Convert if not unix
  found_libchar= 0;
  if (length && path[length-1] == FN_LIBCHAR)
  {
    found_libchar= 1;
    path[length-1]=0;				// remove ending '\'
  }
  if (access(path,F_OK))
  {
461
    my_error(ER_BAD_DB_ERROR, MYF(0), dbname);
462
    DBUG_RETURN(TRUE);
463 464 465 466 467 468 469 470 471 472
  }
  if (found_libchar)
    path[length-1]= FN_LIBCHAR;
  strmov(path+length, MY_DB_OPT_FILE);
  load_db_opt(thd, path, &create);

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

473 474
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
475
    DBUG_RETURN(TRUE);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

  protocol->prepare_for_resend();
  protocol->store(dbname, strlen(dbname), system_charset_info);
  buffer.length(0);
  buffer.append("CREATE DATABASE ", 16);
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
    buffer.append("/*!32312 IF NOT EXISTS*/ ", 25);
  append_identifier(thd, &buffer, dbname, strlen(dbname));

  if (create.default_table_charset)
  {
    buffer.append(" /*!40100", 9);
    buffer.append(" DEFAULT CHARACTER SET ", 23);
    buffer.append(create.default_table_charset->csname);
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
    {
      buffer.append(" COLLATE ", 9);
      buffer.append(create.default_table_charset->name);
    }
    buffer.append(" */", 3);
  }
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());

  if (protocol->write())
500
    DBUG_RETURN(TRUE);
501
  send_eof(thd);
502
  DBUG_RETURN(FALSE);
503
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
504

505
bool
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
506 507
mysqld_show_logs(THD *thd)
{
508 509
  List<Item> field_list;
  Protocol *protocol= thd->protocol;
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
510 511 512 513 514 515
  DBUG_ENTER("mysqld_show_logs");

  field_list.push_back(new Item_empty_string("File",FN_REFLEN));
  field_list.push_back(new Item_empty_string("Type",10));
  field_list.push_back(new Item_empty_string("Status",10));

516 517
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
518
    DBUG_RETURN(TRUE);
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
519

520
#ifdef HAVE_BERKELEY_DB
521
  if ((have_berkeley_db == SHOW_OPTION_YES) && berkeley_show_logs(protocol))
522
    DBUG_RETURN(TRUE);
523
#endif
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
524

525
  send_eof(thd);
526
  DBUG_RETURN(FALSE);
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
527 528 529
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
530
/****************************************************************************
531 532
  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
533 534 535 536 537 538 539
****************************************************************************/

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

542
  table_list->lock_type= TL_UNLOCK;
543
  if (open_and_lock_tables(thd, table_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
544
    DBUG_VOID_RETURN;
545 546
  table= table_list->table;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
547 548 549 550 551
  List<Item> field_list;

  Field **ptr,*field;
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
552 553
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
554 555
      field_list.push_back(new Item_field(field));
  }
556
  restore_record(table, s->default_values);              // Get empty record
557 558
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS |
                                              Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
559
    DBUG_VOID_RETURN;
560
  thd->protocol->flush();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
561 562 563
  DBUG_VOID_RETURN;
}

564

bk@work.mysql.com's avatar
bk@work.mysql.com committed
565
int
566
mysqld_dump_create_info(THD *thd, TABLE_LIST *table_list, int fd)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
567
{
568 569
  Protocol *protocol= thd->protocol;
  String *packet= protocol->storage_packet();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
570
  DBUG_ENTER("mysqld_dump_create_info");
571
  DBUG_PRINT("enter",("table: %s",table_list->table->s->table_name));
572

573
  protocol->prepare_for_resend();
574
  if (store_create_info(thd, table_list, packet))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
575
    DBUG_RETURN(-1);
576

577
  if (fd < 0)
578
  {
579
    if (protocol->write())
580
      DBUG_RETURN(-1);
581
    protocol->flush();
582
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
583
  else
584
  {
585 586
    if (my_write(fd, (const byte*) packet->ptr(), packet->length(),
		 MYF(MY_WME)))
587 588
      DBUG_RETURN(-1);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
589 590
  DBUG_RETURN(0);
}
591

592
/*
593
  Go through all character combinations and ensure that sql_lex.cc can
594
  parse it as an identifier.
595 596

  SYNOPSIS
597 598 599 600 601 602 603
  require_quotes()
  name			attribute name
  name_length		length of name

  RETURN
    #	Pointer to conflicting character
    0	No conflicting character
604 605
*/

606
static const char *require_quotes(const char *name, uint name_length)
607
{
608 609 610 611
  uint length;
  const char *end= name + name_length;

  for ( ; name < end ; name++)
612
  {
613 614 615 616
    uchar chr= (uchar) *name;
    length= my_mbcharlen(system_charset_info, chr);
    if (length == 1 && !system_charset_info->ident_map[chr])
      return name;
617 618 619
  }
  return 0;
}
620

621

622 623
void
append_identifier(THD *thd, String *packet, const char *name, uint length)
624
{
625 626
  const char *name_end;
  char quote_char;
627
  int q= get_quote_char_for_identifier(thd, name, length);
628

629
  if (q == EOF)
630
  {
631
    packet->append(name, length, system_charset_info);
632 633 634
    return;
  }

635 636 637 638
  /*
    The identifier must be quoted as it includes a quote character or
   it's a keyword
  */
639 640

  packet->reserve(length*2 + 2);
641
  quote_char= (char) q;
642 643 644 645
  packet->append(&quote_char, 1, system_charset_info);

  for (name_end= name+length ; name < name_end ; name+= length)
  {
646
    uchar chr= (uchar) *name;
647
    length= my_mbcharlen(system_charset_info, chr);
648 649 650 651 652 653 654 655 656
    /*
      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;
657
    if (length == 1 && chr == (uchar) quote_char)
658 659
      packet->append(&quote_char, 1, system_charset_info);
    packet->append(name, length, packet->charset());
660
  }
661
  packet->append(&quote_char, 1, system_charset_info);
662 663
}

664

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
/*
  Get the quote character for displaying an identifier.

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

  IMPLEMENTATION
    If name is a keyword or includes a special character, then force
    quoting.
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
    is set.

  RETURN
    EOF	  No quote character is needed
    #	  Quote character
*/
684 685 686 687 688 689 690

int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
{
  if (!is_keyword(name,length) &&
      !require_quotes(name, length) &&
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
    return EOF;
691
  if (thd->variables.sql_mode & MODE_ANSI_QUOTES)
692
    return '"';
693
  return '`';
694 695 696
}


697 698 699 700 701
/* 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
702
  if (filename && !(thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE))
703
  {
704
    uint length= dirname_length(filename);
705 706 707
    packet->append(' ');
    packet->append(dir_type);
    packet->append(" DIRECTORY='", 12);
708
#ifdef __WIN__
monty@mysql.com's avatar
monty@mysql.com committed
709 710 711 712 713 714 715 716 717
    /* 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;
718
#endif
monty@mysql.com's avatar
monty@mysql.com committed
719
    packet->append(filename, length);
720 721 722 723 724
    packet->append('\'');
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
725
#define LIST_PROCESS_HOST_LEN 64
726

bk@work.mysql.com's avatar
bk@work.mysql.com committed
727
static int
728
store_create_info(THD *thd, TABLE_LIST *table_list, String *packet)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
729
{
730
  List<Item> field_list;
731 732
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], *end;
  const char *alias;
733
  String type(tmp, sizeof(tmp), system_charset_info);
734 735 736
  Field **ptr,*field;
  uint primary_key;
  KEY *key_info;
737
  TABLE *table= table_list->table;
738
  handler *file= table->file;
739
  TABLE_SHARE *share= table->s;
740
  HA_CREATE_INFO create_info;
741 742 743 744
  my_bool foreign_db_mode=    (thd->variables.sql_mode & (MODE_POSTGRESQL |
							  MODE_ORACLE |
							  MODE_MSSQL |
							  MODE_DB2 |
745
							  MODE_MAXDB |
746 747 748 749
							  MODE_ANSI)) != 0;
  my_bool limited_mysql_mode= (thd->variables.sql_mode &
			       (MODE_NO_FIELD_OPTIONS | MODE_MYSQL323 |
				MODE_MYSQL40)) != 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750
  DBUG_ENTER("store_create_info");
751
  DBUG_PRINT("enter",("table: %s", table->s->table_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
752

753
  restore_record(table, s->default_values); // Get empty record
754

755
  if (share->tmp_table)
756 757 758
    packet->append("CREATE TEMPORARY TABLE ", 23);
  else
    packet->append("CREATE TABLE ", 13);
759
  if (table_list->schema_table)
760
    alias= table_list->schema_table_name;
761
  else
762 763
    alias= (lower_case_table_names == 2 ? table->alias :
            share->table_name);
monty@mysql.com's avatar
monty@mysql.com committed
764
  append_identifier(thd, packet, alias, strlen(alias));
765
  packet->append(" (\n", 3);
766

bk@work.mysql.com's avatar
bk@work.mysql.com committed
767 768
  for (ptr=table->field ; (field= *ptr); ptr++)
  {
769
    bool has_default;
770
    bool has_now_default;
771 772
    uint flags = field->flags;

773
    if (ptr != table->field)
774
      packet->append(",\n", 2);
775

776
    packet->append("  ", 2);
777
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
778 779
    packet->append(' ');
    // check for surprises from the previous call to Field::sql_type()
780
    if (type.ptr() != tmp)
781
      type.set(tmp, sizeof(tmp), system_charset_info);
782 783
    else
      type.set_charset(system_charset_info);
784

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

788
    if (field->has_charset() && !limited_mysql_mode && !foreign_db_mode)
789
    {
790
      if (field->charset() != share->table_charset)
791
      {
792 793 794 795 796 797 798 799 800 801 802
	packet->append(" character set ", 15);
	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))
      {
	packet->append(" collate ", 9);
	packet->append(field->charset()->name);
803
      }
804
    }
805

806 807
    if (flags & NOT_NULL_FLAG)
      packet->append(" NOT NULL", 9);
808 809 810 811 812 813 814 815
    else if (field->type() == FIELD_TYPE_TIMESTAMP)
    {
      /*
        TIMESTAMP field require explicit NULL flag, because unlike
        all other fields they are treated as NOT NULL by default.
      */
      packet->append(" NULL", 5);
    }
816 817

    /* 
818
      Again we are using CURRENT_TIMESTAMP instead of NOW because it is
819 820 821 822 823
      more standard 
    */
    has_now_default= table->timestamp_field == field && 
                     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
    
824
    has_default= (field->type() != FIELD_TYPE_BLOB &&
825
                  !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
826 827 828
		  field->unireg_check != Field::NEXT_NUMBER &&
                  !((foreign_db_mode || limited_mysql_mode) &&
                    has_now_default));
829

830
    if (has_default)
831 832
    {
      packet->append(" default ", 9);
833 834 835
      if (has_now_default)
        packet->append("CURRENT_TIMESTAMP",17);
      else if (!field->is_null())
836
      {                                             // Not null by default
837
        type.set(tmp, sizeof(tmp), field->charset());
838
        field->val_str(&type);
839
	if (type.length())
840
	{
841
	  String def_val;
842
          uint dummy_errors;
843 844
	  /* convert to system_charset_info == utf8 */
	  def_val.copy(type.ptr(), type.length(), field->charset(),
845
		       system_charset_info, &dummy_errors);
846 847
          append_unescaped(packet, def_val.ptr(), def_val.length());
	}
848 849
        else
	  packet->append("''",2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
850
      }
851
      else if (field->maybe_null())
852 853
        packet->append("NULL", 4);                    // Null as default
      else
854
        packet->append(tmp);
855
    }
856

857 858 859 860 861
    if (!foreign_db_mode && !limited_mysql_mode &&
        table->timestamp_field == field && 
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
      packet->append(" on update CURRENT_TIMESTAMP",28);

862
    if (field->unireg_check == Field::NEXT_NUMBER && !foreign_db_mode)
863 864 865 866 867 868 869
      packet->append(" auto_increment", 15 );

    if (field->comment.length)
    {
      packet->append(" COMMENT ",9);
      append_unescaped(packet, field->comment.str, field->comment.length);
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
870 871
  }

872 873
  key_info= table->key_info;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME);
874
  bzero((char*) &create_info, sizeof(create_info));
875
  file->update_create_info(&create_info);
876
  primary_key= share->primary_key;
877

878
  for (uint i=0 ; i < share->keys ; i++,key_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
879
  {
880 881
    KEY_PART_INFO *key_part= key_info->key_part;
    bool found_primary=0;
882
    packet->append(",\n  ", 4);
883

884
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
885 886
    {
      found_primary=1;
887
      packet->append("PRIMARY ", 8);
888
    }
889
    else if (key_info->flags & HA_NOSAME)
890
      packet->append("UNIQUE ", 7);
891
    else if (key_info->flags & HA_FULLTEXT)
892
      packet->append("FULLTEXT ", 9);
893 894
    else if (key_info->flags & HA_SPATIAL)
      packet->append("SPATIAL ", 8);
895
    packet->append("KEY ", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
896

897
    if (!found_primary)
898
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
899

900 901 902
    if (!(thd->variables.sql_mode & MODE_NO_KEY_OPTIONS) &&
	!limited_mysql_mode && !foreign_db_mode)
    {
903
      if (key_info->algorithm == HA_KEY_ALG_BTREE)
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
904 905
        packet->append(" USING BTREE", 12);

906
      if (key_info->algorithm == HA_KEY_ALG_HASH)
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
907 908
        packet->append(" USING HASH", 11);

909 910 911
      // +BAR: send USING only in non-default case: non-spatial rtree
      if ((key_info->algorithm == HA_KEY_ALG_RTREE) &&
	  !(key_info->flags & HA_SPATIAL))
jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
912
        packet->append(" USING RTREE", 12);
913

jimw@mysql.com's avatar
Merge  
jimw@mysql.com committed
914
      // No need to send USING FULLTEXT, it is sent as FULLTEXT KEY
915
    }
916
    packet->append(" (", 2);
917

bk@work.mysql.com's avatar
bk@work.mysql.com committed
918 919
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
    {
920
      if (j)
921
        packet->append(',');
922

923
      if (key_part->field)
924 925
        append_identifier(thd,packet,key_part->field->field_name,
			  strlen(key_part->field->field_name));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
926
      if (!key_part->field ||
927 928 929
          (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
930
      {
931
        buff[0] = '(';
932 933 934
        char* end=int10_to_str((long) key_part->length / 
			       key_part->field->charset()->mbmaxlen,
			       buff + 1,10);
935 936
        *end++ = ')';
        packet->append(buff,(uint) (end-buff));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
937 938 939 940
      }
    }
    packet->append(')');
  }
941

942 943 944 945
  /*
    Get possible foreign key definitions stored in InnoDB and append them
    to the CREATE TABLE statement
  */
946

947
  if ((for_str= file->get_foreign_key_create_info()))
948 949 950
  {
    packet->append(for_str, strlen(for_str));
    file->free_foreign_key_create_info(for_str);
951 952 953
  }

  packet->append("\n)", 2);
954
  if (!(thd->variables.sql_mode & MODE_NO_TABLE_OPTIONS) && !foreign_db_mode)
955
  {
956 957 958 959
    if (thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
      packet->append(" TYPE=", 6);
    else
      packet->append(" ENGINE=", 8);
960 961
    packet->append(file->table_type());
    
962
    if (share->table_charset &&
963 964
	!(thd->variables.sql_mode & MODE_MYSQL323) &&
	!(thd->variables.sql_mode & MODE_MYSQL40))
965
    {
966
      packet->append(" DEFAULT CHARSET=", 17);
967 968
      packet->append(share->table_charset->csname);
      if (!(share->table_charset->state & MY_CS_PRIMARY))
969
      {
970
	packet->append(" COLLATE=", 9);
971
	packet->append(table->s->table_charset->name);
972
      }
973
    }
974

975
    if (share->min_rows)
976
    {
977
      packet->append(" MIN_ROWS=", 10);
978
      end= longlong10_to_str(share->min_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
979
      packet->append(buff, (uint) (end- buff));
980
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
981

982
    if (share->max_rows && !table_list->schema_table)
983
    {
984
      packet->append(" MAX_ROWS=", 10);
985
      end= longlong10_to_str(share->max_rows, buff, 10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
986
      packet->append(buff, (uint) (end - buff));
987
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
988

989
    if (share->avg_row_length)
990
    {
991
      packet->append(" AVG_ROW_LENGTH=", 16);
992
      end= longlong10_to_str(share->avg_row_length, buff,10);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
993
      packet->append(buff, (uint) (end - buff));
994
    }
995

996
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
997
      packet->append(" PACK_KEYS=1", 12);
998
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
999
      packet->append(" PACK_KEYS=0", 12);
1000
    if (share->db_create_options & HA_OPTION_CHECKSUM)
1001
      packet->append(" CHECKSUM=1", 11);
1002
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
1003
      packet->append(" DELAY_KEY_WRITE=1",18);
1004
    if (share->row_type != ROW_TYPE_DEFAULT)
1005 1006
    {
      packet->append(" ROW_FORMAT=",12);
1007
      packet->append(ha_row_type[(uint) share->row_type]);
1008 1009
    }
    table->file->append_create_info(packet);
1010
    if (share->comment && share->comment[0])
1011 1012
    {
      packet->append(" COMMENT=", 9);
1013
      append_unescaped(packet, share->comment, strlen(share->comment));
1014 1015 1016
    }
    if (file->raid_type)
    {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1017 1018 1019 1020 1021 1022
      uint length;
      length= my_snprintf(buff,sizeof(buff),
			  " RAID_TYPE=%s RAID_CHUNKS=%d RAID_CHUNKSIZE=%ld",
			  my_raid_type(file->raid_type), file->raid_chunks,
			  file->raid_chunksize/RAID_BLOCK_SIZE);
      packet->append(buff, length);
1023
    }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1024 1025
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
1026
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1027 1028 1029 1030
  DBUG_RETURN(0);
}


bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static int
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;
  buff->append("CREATE ", 7);
1041
  if (!foreign_db_mode)
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1042 1043
  {
    buff->append("ALGORITHM=", 10);
1044
    switch((int8)table->algorithm)
1045 1046 1047 1048 1049
    {
    case VIEW_ALGORITHM_UNDEFINED:
      buff->append("UNDEFINED ", 10);
      break;
    case VIEW_ALGORITHM_TMPTABLE:
1050
      buff->append("TEMPTABLE ", 10);
1051 1052
      break;
    case VIEW_ALGORITHM_MERGE:
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1053
      buff->append("MERGE ", 6);
1054 1055 1056 1057
      break;
    default:
	DBUG_ASSERT(0); // never should happen
    }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1058 1059
  }
  buff->append("VIEW ", 5);
1060
  append_identifier(thd, buff, table->view_db.str, table->view_db.length);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1061
  buff->append('.');
1062
  append_identifier(thd, buff, table->view_name.str, table->view_name.length);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1063 1064
  buff->append(" AS ", 4);
  buff->append(table->query.str, table->query.length);
1065 1066 1067 1068 1069 1070 1071
  if (table->with_check != VIEW_CHECK_NONE)
  {
    if (table->with_check == VIEW_CHECK_LOCAL)
      buff->append(" WITH LOCAL CHECK OPTION", 24);
    else
      buff->append(" WITH CASCADED CHECK OPTION", 27);
  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1072 1073 1074 1075
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1076
/****************************************************************************
1077 1078
  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
1079 1080 1081 1082
****************************************************************************/

class thread_info :public ilink {
public:
1083 1084 1085 1086
  static void *operator new(size_t size)
  {
    return (void*) sql_alloc((uint) size);
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1087
  static void operator delete(void *ptr __attribute__((unused)),
1088 1089
                              size_t size __attribute__((unused)))
  { TRASH(ptr, size); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1090

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1091 1092
  ulong thread_id;
  time_t start_time;
1093
  uint   command;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
  const char *user,*host,*db,*proc_info,*state_info;
  char *query;
};

#ifdef __GNUC__
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;
1107 1108
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
			   PROCESS_LIST_WIDTH);
1109
  Protocol *protocol= thd->protocol;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1110 1111
  DBUG_ENTER("mysqld_list_processes");

1112
  field_list.push_back(new Item_int("Id",0,11));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1113
  field_list.push_back(new Item_empty_string("User",16));
1114
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1115 1116 1117
  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));
1118
  field_list.push_back(new Item_return_int("Time",7, FIELD_TYPE_LONG));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1119 1120 1121 1122
  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;
1123 1124
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1125 1126 1127 1128 1129 1130 1131 1132 1133
    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++))
    {
1134
      struct st_my_thread_var *mysys_var;
1135
      if ((tmp->vio_ok() || tmp->system_thread) &&
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1136
          (!user || (tmp->user && !strcmp(tmp->user,user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1137
      {
1138 1139 1140
        thread_info *thd_info=new thread_info;

        thd_info->thread_id=tmp->thread_id;
1141 1142 1143
        thd_info->user=thd->strdup(tmp->user ? tmp->user :
				   (tmp->system_thread ?
				    "system user" : "unauthenticated user"));
1144
	if (tmp->peer_port && (tmp->host || tmp->ip) && thd->host_or_ip[0])
1145 1146
	{
	  if ((thd_info->host= thd->alloc(LIST_PROCESS_HOST_LEN+1)))
1147
	    my_snprintf((char *) thd_info->host, LIST_PROCESS_HOST_LEN,
1148
			"%s:%u", tmp->host_or_ip, tmp->peer_port);
1149 1150
	}
	else
1151
	  thd_info->host= thd->strdup(tmp->host_or_ip);
1152 1153 1154
        if ((thd_info->db=tmp->db))             // Safe test
          thd_info->db=thd->strdup(thd_info->db);
        thd_info->command=(int) tmp->command;
1155 1156
        if ((mysys_var= tmp->mysys_var))
          pthread_mutex_lock(&mysys_var->mutex);
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1157
        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
1158
#ifndef EMBEDDED_LIBRARY
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
        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
1169 1170 1171
#else
        thd_info->state_info= (char*)"Writing to net";
#endif
1172 1173
        if (mysys_var)
          pthread_mutex_unlock(&mysys_var->mutex);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1174 1175

#if !defined(DONT_USE_THR_ALARM) && ! defined(SCO)
1176 1177
        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
1178
#endif
1179 1180 1181
#ifdef EXTRA_DEBUG
        thd_info->start_time= tmp->time_after_lock;
#else
1182
        thd_info->start_time= tmp->start_time;
1183
#endif
1184 1185 1186
        thd_info->query=0;
        if (tmp->query)
        {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1187 1188 1189 1190 1191
	  /* 
            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
          */
1192
          uint length= min(max_query_length, tmp->query_length);
1193
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1194 1195
        }
        thread_infos.append(thd_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1196 1197 1198 1199 1200 1201
      }
    }
  }
  VOID(pthread_mutex_unlock(&LOCK_thread_count));

  thread_info *thd_info;
1202
  time_t now= time(0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1203 1204
  while ((thd_info=thread_infos.get()))
  {
1205 1206
    protocol->prepare_for_resend();
    protocol->store((ulonglong) thd_info->thread_id);
1207 1208 1209
    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
1210
    if (thd_info->proc_info)
1211
      protocol->store(thd_info->proc_info, system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1212
    else
1213
      protocol->store(command_name[thd_info->command], system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1214
    if (thd_info->start_time)
1215
      protocol->store((uint32) (now - thd_info->start_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1216
    else
1217
      protocol->store_null();
1218 1219
    protocol->store(thd_info->state_info, system_charset_info);
    protocol->store(thd_info->query, system_charset_info);
1220
    if (protocol->write())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1221 1222
      break; /* purecov: inspected */
  }
1223
  send_eof(thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1224 1225 1226 1227
  DBUG_VOID_RETURN;
}

/*****************************************************************************
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1228
  Status functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1229 1230
*****************************************************************************/

1231 1232

static bool show_status_array(THD *thd, const char *wild,
1233 1234 1235 1236
                              show_var_st *variables,
                              enum enum_var_type value_type,
                              struct system_status_var *status_var,
                              const char *prefix, TABLE *table)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1237
{
1238 1239 1240 1241
  char buff[1024], *prefix_end;
  /* the variable name should not be longer then 80 characters */
  char name_buffer[80];
  int len;
1242
  LEX_STRING null_lex_str;
1243
  DBUG_ENTER("show_status_array");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1244

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

1248 1249 1250
  prefix_end=strnmov(name_buffer, prefix, sizeof(name_buffer)-1);
  len=name_buffer + sizeof(name_buffer) - prefix_end;

1251
  for (; variables->name; variables++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1252
  {
1253 1254 1255 1256 1257 1258
    strnmov(prefix_end, variables->name, len);
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
    SHOW_TYPE show_type=variables->type;
    if (show_type == SHOW_VARS)
    {
      show_status_array(thd, wild, (show_var_st *) variables->value,
1259
                        value_type, status_var, variables->name, table);
1260 1261
    }
    else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1262
    {
1263 1264
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
                                                 name_buffer, wild)))
1265
      {
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
        char *value=variables->value;
        const char *pos, *end;
        long nr;
        if (show_type == SHOW_SYS)
        {
          show_type= ((sys_var*) value)->type();
          value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type,
                                                           &null_lex_str);
        }

        pos= end= buff;
        switch (show_type) {
        case SHOW_LONG_STATUS:
        case SHOW_LONG_CONST_STATUS:
          value= ((char *) status_var + (ulong) value);
          /* fall through */
        case SHOW_LONG:
        case SHOW_LONG_CONST:
          end= int10_to_str(*(long*) value, buff, 10);
          break;
        case SHOW_LONGLONG:
          end= longlong10_to_str(*(longlong*) value, buff, 10);
          break;
        case SHOW_HA_ROWS:
          end= longlong10_to_str((longlong) *(ha_rows*) value, buff, 10);
          break;
        case SHOW_BOOL:
          end= strmov(buff, *(bool*) value ? "ON" : "OFF");
          break;
        case SHOW_MY_BOOL:
          end= strmov(buff, *(my_bool*) value ? "ON" : "OFF");
          break;
        case SHOW_INT_CONST:
        case SHOW_INT:
          end= int10_to_str((long) *(uint32*) value, buff, 10);
          break;
        case SHOW_HAVE:
petr@mysql.com's avatar
petr@mysql.com committed
1303 1304 1305 1306 1307 1308
        {
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
          pos= show_comp_option_name[(int) tmp];
          end= strend(pos);
          break;
        }
1309
        case SHOW_CHAR:
petr@mysql.com's avatar
petr@mysql.com committed
1310 1311 1312 1313 1314 1315
        {
          if (!(pos= value))
            pos= "";
          end= strend(pos);
          break;
        }
1316 1317 1318 1319 1320 1321 1322
        case SHOW_STARTTIME:
          nr= (long) (thd->query_start() - start_time);
          end= int10_to_str(nr, buff, 10);
          break;
        case SHOW_QUESTION:
          end= int10_to_str((long) thd->query_id, buff, 10);
          break;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1323
#ifdef HAVE_REPLICATION
1324 1325 1326 1327
        case SHOW_RPL_STATUS:
          end= strmov(buff, rpl_status_type[(int)rpl_status]);
          break;
        case SHOW_SLAVE_RUNNING:
petr@mysql.com's avatar
petr@mysql.com committed
1328 1329 1330 1331 1332 1333 1334
        {
          pthread_mutex_lock(&LOCK_active_mi);
          end= strmov(buff, (active_mi->slave_running &&
                             active_mi->rli.slave_running) ? "ON" : "OFF");
          pthread_mutex_unlock(&LOCK_active_mi);
          break;
        }
gbichot@quadita2.mysql.com's avatar
gbichot@quadita2.mysql.com committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        case SHOW_SLAVE_RETRIED_TRANS:
        {
          /*
            TODO: in 5.1 with multimaster, have one such counter per line in SHOW
            SLAVE STATUS, and have the sum over all lines here.
          */
	  pthread_mutex_lock(&LOCK_active_mi);
          pthread_mutex_lock(&active_mi->rli.data_lock);
	  end= int10_to_str(active_mi->rli.retried_trans, buff, 10);
          pthread_mutex_unlock(&active_mi->rli.data_lock);
	  pthread_mutex_unlock(&LOCK_active_mi);
	  break;
        }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1348
#endif /* HAVE_REPLICATION */
1349 1350 1351 1352
        case SHOW_OPENTABLES:
          end= int10_to_str((long) cached_tables(), buff, 10);
          break;
        case SHOW_CHAR_PTR:
petr@mysql.com's avatar
petr@mysql.com committed
1353 1354 1355 1356 1357 1358
        {
          if (!(pos= *(char**) value))
            pos= "";
          end= strend(pos);
          break;
        }
1359
        case SHOW_DOUBLE:
petr@mysql.com's avatar
petr@mysql.com committed
1360 1361 1362 1363
        {
          end= buff + sprintf(buff, "%f", *(double*) value);
          break;
        }
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1364
#ifdef HAVE_OPENSSL
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 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
          /* First group - functions relying on CTX */
        case SHOW_SSL_CTX_SESS_ACCEPT:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept(ssl_acceptor_fd->
                                                        ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_ACCEPT_GOOD:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept_good(ssl_acceptor_fd->
                                                             ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT_GOOD:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect_good(ssl_acceptor_fd->
                                                              ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_ACCEPT_RENEGOTIATE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_accept_renegotiate(ssl_acceptor_fd->ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT_RENEGOTIATE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect_renegotiate(ssl_acceptor_fd-> ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CB_HITS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_cb_hits(ssl_acceptor_fd->
                                                         ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_HITS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_hits(ssl_acceptor_fd->
                                                      ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_CACHE_FULL:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_cache_full(ssl_acceptor_fd->
                                                            ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_MISSES:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_misses(ssl_acceptor_fd->
                                                        ssl_context)),
                            buff, 10);
          break;
        case SHOW_SSL_CTX_SESS_TIMEOUTS:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_timeouts(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_NUMBER:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_number(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_CONNECT:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_connect(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_SESS_GET_CACHE_SIZE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_sess_get_cache_size(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_VERIFY_MODE:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_get_verify_mode(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_VERIFY_DEPTH:
          end= int10_to_str((long) (!ssl_acceptor_fd ? 0 :
                                    SSL_CTX_get_verify_depth(ssl_acceptor_fd->ssl_context)),
                            buff,10);
          break;
        case SHOW_SSL_CTX_GET_SESSION_CACHE_MODE:
          if (!ssl_acceptor_fd)
          {
            pos= "NONE";
            end= pos+4;
            break;
          }
          switch (SSL_CTX_get_session_cache_mode(ssl_acceptor_fd->ssl_context))
          {
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1457
          case SSL_SESS_CACHE_OFF:
1458
            pos= "OFF";
1459
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1460
          case SSL_SESS_CACHE_CLIENT:
1461
            pos= "CLIENT";
1462
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1463
          case SSL_SESS_CACHE_SERVER:
1464
            pos= "SERVER";
1465
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1466
          case SSL_SESS_CACHE_BOTH:
1467
            pos= "BOTH";
1468
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1469
          case SSL_SESS_CACHE_NO_AUTO_CLEAR:
1470
            pos= "NO_AUTO_CLEAR";
1471
            break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1472
          case SSL_SESS_CACHE_NO_INTERNAL_LOOKUP:
1473
            pos= "NO_INTERNAL_LOOKUP";
1474 1475
            break;
          default:
1476
            pos= "Unknown";
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
            break;
          }
          end= strend(pos);
          break;
          /* First group - functions relying on SSL */
        case SHOW_SSL_GET_VERSION:
          pos= (thd->net.vio->ssl_arg ?
                SSL_get_version((SSL*) thd->net.vio->ssl_arg) : "");
          end= strend(pos);
          break;
        case SHOW_SSL_SESSION_REUSED:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_session_reused((SSL*) thd->net.vio->
                                                       ssl_arg) :
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_DEFAULT_TIMEOUT:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_default_timeout((SSL*) thd->net.vio->
                                                            ssl_arg) :
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_VERIFY_MODE:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_verify_mode((SSL*) thd->net.vio->
                                                        ssl_arg):
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_VERIFY_DEPTH:
          end= int10_to_str((long) (thd->net.vio->ssl_arg ?
                                    SSL_get_verify_depth((SSL*) thd->net.vio->
                                                         ssl_arg):
                                    0),
                            buff, 10);
          break;
        case SHOW_SSL_GET_CIPHER:
          pos= (thd->net.vio->ssl_arg ?
                SSL_get_cipher((SSL*) thd->net.vio->ssl_arg) : "" );
          end= strend(pos);
          break;
        case SHOW_SSL_GET_CIPHER_LIST:
          if (thd->net.vio->ssl_arg)
          {
            char *to= buff;
            for (int i=0 ; i++ ;)
            {
              const char *p= SSL_get_cipher_list((SSL*) thd->net.vio->ssl_arg,i);
              if (p == NULL)
                break;
              to= strmov(to, p);
              *to++= ':';
            }
            if (to != buff)
              to--;				// Remove last ':'
            end= to;
          }
          break;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
1537 1538

#endif /* HAVE_OPENSSL */
1539 1540
        case SHOW_KEY_CACHE_LONG:
        case SHOW_KEY_CACHE_CONST_LONG:
1541
          value= (value-(char*) &dflt_key_cache_var)+ (char*) dflt_key_cache;
1542 1543 1544 1545 1546 1547 1548 1549
          end= int10_to_str(*(long*) value, buff, 10);
          break;
        case SHOW_UNDEF:				// Show never happen
        case SHOW_SYS:
          break;					// Return empty string
        default:
          break;
        }
1550
        restore_record(table, s->default_values);
1551 1552 1553
        table->field[0]->store(name_buffer, strlen(name_buffer),
                               system_charset_info);
        table->field[1]->store(pos, (uint32) (end - pos), system_charset_info);
1554 1555
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1556 1557 1558
      }
    }
  }
1559 1560 1561 1562 1563

  DBUG_RETURN(FALSE);
}


1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
/* 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;
}


1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
LEX_STRING *make_lex_string(THD *thd, LEX_STRING *lex_str,
                            const char* str, uint length,
                            bool allocate_lex_string)
{
  MEM_ROOT *mem= thd->mem_root;
  if (allocate_lex_string)
    lex_str= (LEX_STRING *)thd->alloc(sizeof(LEX_STRING));
  lex_str->str= strmake_root(mem, str, length);
  lex_str->length= length;
  return lex_str;
}


/* INFORMATION_SCHEMA name */
LEX_STRING information_schema_name= {(char*)"information_schema", 18};
extern ST_SCHEMA_TABLE schema_tables[];

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


1611 1612 1613 1614 1615 1616 1617
/*
  Store record to I_S table, convert HEAP table
  to MyISAM if necessary

  SYNOPSIS
    schema_table_store_record()
    thd                   thread handler
1618 1619
    table                 Information schema table to be updated

1620 1621
  RETURN
    0	                  success
1622
    1	                  error
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
*/

static bool schema_table_store_record(THD *thd, TABLE *table)
{
  int error;
  if ((error= table->file->write_row(table->record[0])))
  {
    if (create_myisam_from_heap(thd, table, 
                                table->pos_in_table_list->schema_table_param,
                                error, 0))
      return 1;
  }
  return 0;
}


1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
void get_index_field_values(LEX *lex, INDEX_FIELD_VALUES *index_field_values)
{
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
  switch (lex->orig_sql_command) {
  case SQLCOM_SHOW_DATABASES:
    index_field_values->db_value= wild;
    break;
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_SHOW_TABLE_STATUS:
    index_field_values->db_value= lex->current_select->db;
    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();
  if(!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
                             (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++)
1685
    {
1686 1687
      if (!uses_only_table_name_fields(*child, table))
        return 0;
1688
    }
1689 1690 1691 1692 1693 1694 1695 1696 1697
  }
  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;
    const char *field_name1= field_info[schema_table->idx_field1].field_name;
    const char *field_name2= field_info[schema_table->idx_field2].field_name;
1698 1699 1700 1701 1702 1703 1704
    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)))
1705 1706
      return 0;
  }
1707 1708
  else if (item->type() == Item::REF_ITEM)
    return uses_only_table_name_fields(item->real_item(), table);
1709 1710 1711 1712
  if (item->type() == Item::SUBSELECT_ITEM &&
      !item->const_item())
    return 0;

1713
  return 1;
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
}


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


1773 1774 1775 1776 1777 1778
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
{
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
}


1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
/*
  Add 'information_schema' name to db_names list

  SYNOPSIS
    schema_db_add()
    thd                   thread handler
    files                 list of db names
    wild                  wild string
    with_i_schema         returns 1 if we added 'IS' name to list
                          otherwise returns 0

  RETURN
    1	                  error
    0	                  success
*/

int schema_db_add(THD *thd, List<char> *files,
                  const char *wild, bool *with_i_schema)
1797
{
1798 1799 1800 1801 1802 1803 1804 1805
  *with_i_schema= 0;
  if (!wild || !wild_compare(information_schema_name.str, wild, 0))
  {
    *with_i_schema= 1;
    if (files->push_back(thd->strdup(information_schema_name.str)))
      return 1;
  }
  return 0;
1806 1807 1808 1809 1810 1811 1812 1813
}


int schema_tables_add(THD *thd, List<char> *files, const char *wild)
{
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
  for ( ; tmp_schema_table->table_name; tmp_schema_table++)
  {
1814 1815
    if (tmp_schema_table->hidden)
      continue;
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    if (wild)
    {
      if (lower_case_table_names)
      {
        if (wild_case_compare(files_charset_info,
                              tmp_schema_table->table_name,
                              wild))
          continue;
      }
      else if (wild_compare(tmp_schema_table->table_name, wild, 0))
        continue;
    }
    if (files->push_back(thd->strdup(tmp_schema_table->table_name)))
      return 1;
  }
  return 0;
}


1835 1836 1837 1838 1839
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;
1840
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
1841
  SELECT_LEX *lsel= tables->schema_select_lex;
1842
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
1843 1844 1845 1846 1847 1848 1849 1850
  SELECT_LEX sel;
  INDEX_FIELD_VALUES idx_field_vals;
  char path[FN_REFLEN], *end, *base_name, *file_name;
  uint len;
  bool with_i_schema;
  enum enum_schema_tables schema_table_idx;
  thr_lock_type lock_type;
  List<char> bases;
monty@mysql.com's avatar
monty@mysql.com committed
1851
  List_iterator_fast<char> it(bases);
1852
  COND *partial_cond; 
1853
  uint derived_tables= lex->derived_tables; 
monty@mysql.com's avatar
monty@mysql.com committed
1854
  int error= 1;
1855 1856 1857 1858
  DBUG_ENTER("get_all_tables");

  LINT_INIT(end);
  LINT_INIT(len);
1859 1860 1861 1862 1863

  if (lsel)
  {
    TABLE *old_open_tables= thd->open_tables;
    TABLE_LIST *show_table_list= (TABLE_LIST*) lsel->table_list.first;
1864 1865
    bool res;

1866
    lex->all_selects_list= lsel;
1867
    res= open_and_lock_tables(thd, show_table_list);
1868 1869
    if (schema_table->process_table(thd, show_table_list,
                                    table, res, show_table_list->db,
1870
                                    show_table_list->alias))
monty@mysql.com's avatar
monty@mysql.com committed
1871
      goto err;
1872 1873
    close_thread_tables(thd, 0, 0, old_open_tables);
    show_table_list->table= 0;
monty@mysql.com's avatar
monty@mysql.com committed
1874 1875
    error= 0;
    goto err;
1876 1877
  }

1878 1879 1880
  schema_table_idx= get_schema_table_idx(schema_table);
  lock_type= TL_UNLOCK;

1881 1882 1883
  if (schema_table_idx == SCH_TABLES)
    lock_type= TL_READ;
  get_index_field_values(lex, &idx_field_vals);
1884 1885

  /* information schema name always is first in list */
1886
  if (schema_db_add(thd, &bases, idx_field_vals.db_value, &with_i_schema))
monty@mysql.com's avatar
monty@mysql.com committed
1887
    goto err;
1888

1889 1890
  if (mysql_find_files(thd, &bases, NullS, mysql_data_home,
		       idx_field_vals.db_value, 1))
monty@mysql.com's avatar
monty@mysql.com committed
1891
    goto err;
1892 1893

  partial_cond= make_cond_for_info_schema(cond, tables);
monty@mysql.com's avatar
monty@mysql.com committed
1894
  it.rewind(); /* To get access to new elements in basis list */
1895
  while ((base_name= it++) ||
1896 1897 1898 1899 1900 1901 1902 1903 1904
	 /*
	   generate error for non existing database.
	   (to save old behaviour for SHOW TABLES FROM db)
	 */
	 ((lex->orig_sql_command == SQLCOM_SHOW_TABLES ||
           lex->orig_sql_command == SQLCOM_SHOW_TABLE_STATUS) &&
	  (base_name= select_lex->db) && !bases.elements))
  {
#ifndef NO_EMBEDDED_ACCESS_CHECKS
1905 1906
    if (with_i_schema ||   // don't check the rights if information schema db
        !check_access(thd,SELECT_ACL, base_name, &thd->col_access,0,1) ||
1907 1908 1909 1910 1911 1912
        thd->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(thd->host, thd->ip, thd->priv_user, base_name,0) ||
	(grant_option && !check_grant_db(thd, base_name)))
#endif
    {
      List<char> files;
1913 1914 1915
      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
1916
          goto err;
1917 1918 1919 1920 1921 1922 1923 1924
      }
      else
      {
        strxmov(path, mysql_data_home, "/", base_name, NullS);
        end= path + (len= unpack_dirname(path,path));
        len= FN_LEN - len;
        if (mysql_find_files(thd, &files, base_name, 
                             path, idx_field_vals.table_value, 0))
monty@mysql.com's avatar
monty@mysql.com committed
1925
          goto err;
1926
      }
1927

1928 1929
      List_iterator_fast<char> it_files(files);
      while ((file_name= it_files++))
1930
      {
1931
	restore_record(table, s->default_values);
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
        table->field[schema_table->idx_field1]->
          store(base_name, strlen(base_name), system_charset_info);
        table->field[schema_table->idx_field2]->
          store(file_name, strlen(file_name),system_charset_info);
        if (!partial_cond || partial_cond->val_int())
        {
          if (schema_table_idx == SCH_TABLE_NAMES)
          {
            if (lex->verbose || lex->orig_sql_command == SQLCOM_END)
            {
1942
              if (with_i_schema)
1943
              {
1944 1945 1946 1947 1948
                table->field[3]->store("TEMPORARY", 9, system_charset_info);
              }
              else
              {
                my_snprintf(end, len, "/%s%s", file_name, reg_ext);
1949
                switch (mysql_frm_type(path)) {
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
                case FRMTYPE_ERROR:
                  table->field[3]->store("ERROR", 5, system_charset_info);
                  break;
                case FRMTYPE_TABLE:
                  table->field[3]->store("BASE TABLE", 10, system_charset_info);
                  break;
                case FRMTYPE_VIEW:
                  table->field[3]->store("VIEW", 4, system_charset_info);
                  break;
                default:
                  DBUG_ASSERT(0);
                }
1962 1963
              }
            }
1964
            if (schema_table_store_record(thd, table))
1965
              goto err;
1966 1967 1968 1969 1970 1971
          }
          else
          {
            int res;
            TABLE *old_open_tables= thd->open_tables;
            if (make_table_list(thd, &sel, base_name, file_name))
monty@mysql.com's avatar
monty@mysql.com committed
1972
              goto err;
1973 1974
            TABLE_LIST *show_table_list= (TABLE_LIST*) sel.table_list.first;
            show_table_list->lock_type= lock_type;
1975 1976
            lex->all_selects_list= &sel;
            lex->derived_tables= 0;
1977 1978
            res= open_and_lock_tables(thd, show_table_list);
            if (schema_table->process_table(thd, show_table_list, table,
1979 1980
                                            res, base_name,
                                            show_table_list->alias))
monty@mysql.com's avatar
monty@mysql.com committed
1981
              goto err;
1982 1983 1984 1985
            close_thread_tables(thd, 0, 0, old_open_tables);
          }
        }
      }
1986 1987 1988 1989
      /*
        If we have information schema its always the first table and only
        the first table. Reset for other tables.
      */
1990
      with_i_schema= 0;
1991 1992
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
1993 1994 1995

  error= 0;
err:
1996 1997
  lex->derived_tables= derived_tables;
  lex->all_selects_list= old_all_select_lex;
monty@mysql.com's avatar
monty@mysql.com committed
1998
  DBUG_RETURN(error);
1999 2000 2001
}


2002
bool store_schema_shemata(THD* thd, TABLE *table, const char *db_name,
2003
                          CHARSET_INFO *cs)
2004
{
2005
  restore_record(table, s->default_values);
2006
  table->field[1]->store(db_name, strlen(db_name), system_charset_info);
2007 2008
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2009
  return schema_table_store_record(thd, table);
2010 2011 2012
}


2013 2014
int fill_schema_shemata(THD *thd, TABLE_LIST *tables, COND *cond)
{
2015
  char path[FN_REFLEN];
2016 2017 2018 2019 2020
  bool found_libchar;
  INDEX_FIELD_VALUES idx_field_vals;
  List<char> files;
  char *file_name;
  uint length;
2021
  bool with_i_schema;
2022 2023
  HA_CREATE_INFO create;
  TABLE *table= tables->table;
2024
  DBUG_ENTER("fill_schema_shemata");
2025 2026

  get_index_field_values(thd->lex, &idx_field_vals);
2027 2028
  /* information schema name always is first in list */
  if (schema_db_add(thd, &files, idx_field_vals.db_value, &with_i_schema))
2029
    DBUG_RETURN(1);
2030 2031
  if (mysql_find_files(thd, &files, NullS, mysql_data_home,
                       idx_field_vals.db_value, 1))
2032
    DBUG_RETURN(1);
2033 2034 2035
  List_iterator_fast<char> it(files);
  while ((file_name=it++))
  {
2036 2037
    if (with_i_schema)       // information schema name is always first in list
    {
2038
      if (store_schema_shemata(thd, table, file_name,
2039
                               system_charset_info))
2040
        DBUG_RETURN(1);
2041 2042 2043
      with_i_schema= 0;
      continue;
    }
2044 2045 2046 2047 2048 2049
#ifndef NO_EMBEDDED_ACCESS_CHECKS
    if (thd->master_access & (DB_ACLS | SHOW_DB_ACL) ||
	acl_get(thd->host, thd->ip, thd->priv_user, file_name,0) ||
	(grant_option && !check_grant_db(thd, file_name)))
#endif
    {
2050
      strxmov(path, mysql_data_home, "/", file_name, NullS);
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
      length=unpack_dirname(path,path);		// Convert if not unix
      found_libchar= 0;
      if (length && path[length-1] == FN_LIBCHAR)
      {
	found_libchar= 1;
	path[length-1]=0;			// remove ending '\'
      }

      if (found_libchar)
	path[length-1]= FN_LIBCHAR;
      strmov(path+length, MY_DB_OPT_FILE);
      load_db_opt(thd, path, &create);
2063
      if (store_schema_shemata(thd, table, file_name, 
2064
                               create.default_table_charset))
2065
        DBUG_RETURN(1);
2066 2067
    }
  }
2068
  DBUG_RETURN(0);
2069 2070 2071
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2072 2073 2074 2075
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)
2076 2077 2078 2079 2080
{
  const char *tmp_buff;
  TIME time;
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_tables_record");
2081 2082

  restore_record(table, s->default_values);
2083 2084
  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
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
  if (res)
  {
    /*
      there was errors during opening tables
    */
    const char *error= thd->net.last_error;
    table->field[20]->store(error, strlen(error), cs);
    thd->clear_error();
  }
  else if (tables->view)
2095 2096 2097 2098 2099 2100 2101
  {
    table->field[3]->store("VIEW", 4, cs);
    table->field[20]->store("view", 4, cs);
  }
  else
  {
    TABLE *show_table= tables->table;
2102
    TABLE_SHARE *share= show_table->s;
2103
    handler *file= show_table->file;
2104

2105
    file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_NO_LOCK);
2106
    if (share->tmp_table == TMP_TABLE)
2107 2108 2109 2110
      table->field[3]->store("TEMPORARY", 9, cs);
    else
      table->field[3]->store("BASE TABLE", 10, cs);

2111 2112
    for (int i= 4; i < 20; i++)
    {
2113
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2114 2115 2116 2117 2118
        continue;
      table->field[i]->set_notnull();
    }
    tmp_buff= file->table_type();
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2119
    table->field[5]->store((longlong) share->frm_version);
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
    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;
    }
2145
    table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2146 2147 2148 2149 2150
    if (!tables->schema_table)
    {
      table->field[7]->store((longlong) file->records);
      table->field[7]->set_notnull();
    }
2151 2152 2153 2154 2155 2156 2157 2158
    table->field[8]->store((longlong) file->mean_rec_length);
    table->field[9]->store((longlong) file->data_file_length);
    if (file->max_data_file_length)
    {
      table->field[10]->store((longlong) file->max_data_file_length);
    }
    table->field[11]->store((longlong) file->index_file_length);
    table->field[12]->store((longlong) file->delete_length);
2159
    if (show_table->found_next_number_field)
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
    {
      show_table->next_number_field=show_table->found_next_number_field;
      show_table->next_number_field->reset();
      file->update_auto_increment();
      table->field[13]->store((longlong) show_table->
                              next_number_field->val_int());
      table->field[13]->set_notnull();
      show_table->next_number_field=0;
    }
    if (file->create_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                                file->create_time);
      table->field[14]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[14]->set_notnull();
    }
    if (file->update_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time,
                                                file->update_time);
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[15]->set_notnull();
    }
    if (file->check_time)
    {
      thd->variables.time_zone->gmt_sec_to_TIME(&time, file->check_time);
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
      table->field[16]->set_notnull();
    }
2189 2190
    tmp_buff= (share->table_charset ?
               share->table_charset->name : "default");
2191
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
2192
    if (file->table_flags() & (ulong) HA_HAS_CHECKSUM)
2193 2194 2195 2196 2197 2198 2199
    {
      table->field[18]->store((longlong) file->checksum());
      table->field[18]->set_notnull();
    }

    char option_buff[350],*ptr;
    ptr=option_buff;
2200
    if (share->min_rows)
2201 2202
    {
      ptr=strmov(ptr," min_rows=");
2203
      ptr=longlong10_to_str(share->min_rows,ptr,10);
2204
    }
2205
    if (share->max_rows)
2206 2207
    {
      ptr=strmov(ptr," max_rows=");
2208
      ptr=longlong10_to_str(share->max_rows,ptr,10);
2209
    }
2210
    if (share->avg_row_length)
2211 2212
    {
      ptr=strmov(ptr," avg_row_length=");
2213
      ptr=longlong10_to_str(share->avg_row_length,ptr,10);
2214
    }
2215
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2216
      ptr=strmov(ptr," pack_keys=1");
2217
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2218
      ptr=strmov(ptr," pack_keys=0");
2219
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2220
      ptr=strmov(ptr," checksum=1");
2221
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2222
      ptr=strmov(ptr," delay_key_write=1");
2223
    if (share->row_type != ROW_TYPE_DEFAULT)
2224
      ptr=strxmov(ptr, " row_format=", 
2225
                  ha_row_type[(uint) share->row_type],
2226 2227 2228 2229
                  NullS);
    if (file->raid_type)
    {
      char buff[100];
2230 2231 2232 2233
      my_snprintf(buff,sizeof(buff),
                  " raid_type=%s raid_chunks=%d raid_chunksize=%ld",
                  my_raid_type(file->raid_type), file->raid_chunks,
                  file->raid_chunksize/RAID_BLOCK_SIZE);
2234 2235 2236 2237 2238
      ptr=strmov(ptr,buff);
    }
    table->field[19]->store(option_buff+1,
                            (ptr == option_buff ? 0 : 
                             (uint) (ptr-option_buff)-1), cs);
2239
    {
2240 2241
      char *comment;
      comment= show_table->file->update_table_comment(share->comment);
2242 2243 2244
      if (comment)
      {
        table->field[20]->store(comment, strlen(comment), cs);
2245 2246
        if (comment != share->comment)
          my_free(comment, MYF(0));
2247 2248
      }
    }
2249
  }
2250
  DBUG_RETURN(schema_table_store_record(thd, table));
2251 2252 2253
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2254 2255 2256 2257
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)
2258
{
2259 2260
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2261 2262 2263 2264
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_column_record");
  if (res)
  {
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
    if (lex->orig_sql_command != SQLCOM_SHOW_FIELDS)
    {
      /*
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
        rather than in SHOW COLUMNS
      */ 
      push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                   thd->net.last_errno, thd->net.last_error);
      thd->clear_error();
      res= 0;
    }
    DBUG_RETURN(res);
2277 2278 2279 2280 2281
  }

  TABLE *show_table= tables->table;
  handler *file= show_table->file;
  file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2282
  restore_record(show_table, s->default_values);
2283 2284 2285 2286 2287 2288 2289
  Field **ptr,*field;
  int count= 0;
  for (ptr=show_table->field; (field= *ptr) ; ptr++)
  {
    if (!wild || !wild[0] || 
        !wild_case_compare(system_charset_info, field->field_name,wild))
    {
2290
      const char *tmp_buff;
2291 2292 2293 2294 2295
      byte *pos;
      uint flags=field->flags;
      char tmp[MAX_FIELD_WIDTH];
      char tmp1[MAX_FIELD_WIDTH];
      String type(tmp,sizeof(tmp), system_charset_info);
2296
      char *end= tmp;
2297
      count++;
2298
      restore_record(table, s->default_values);
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317

#ifndef NO_EMBEDDED_ACCESS_CHECKS
      uint col_access;
      check_access(thd,SELECT_ACL | EXTRA_ACL, base_name,
                   &tables->grant.privilege, 0, 0);
      col_access= get_column_grant(thd, &tables->grant, tables->db,
                                   tables->table_name,
                                   field->field_name) & COL_ACLS;
      if (lex->orig_sql_command != SQLCOM_SHOW_FIELDS  && !col_access)
        continue;
      for (uint bitnr=0; col_access ; col_access>>=1,bitnr++)
      {
        if (col_access & 1)
        {
          *end++=',';
          end=strmov(end,grant_types.type_names[bitnr]);
        }
      }
#else
2318
      *end= 0;
2319 2320 2321
#endif
      table->field[17]->store(tmp+1,end == tmp ? 0 : (uint) (end-tmp-1), cs);

2322 2323 2324 2325 2326 2327
      table->field[1]->store(base_name, strlen(base_name), cs);
      table->field[2]->store(file_name, strlen(file_name), cs);
      table->field[3]->store(field->field_name, strlen(field->field_name),
                             cs);
      table->field[4]->store((longlong) count);
      field->sql_type(type);
2328
      table->field[14]->store(type.ptr(), type.length(), cs);		
2329
      tmp_buff= strchr(type.ptr(), '(');
2330
      table->field[7]->store(type.ptr(),
2331 2332 2333 2334 2335
                             (tmp_buff ? tmp_buff - type.ptr() :
                              type.length()), cs);
      if (show_table->timestamp_field == field &&
          field->unireg_check != Field::TIMESTAMP_UN_FIELD)
      {
2336 2337
        table->field[5]->store("CURRENT_TIMESTAMP", 17, cs);
        table->field[5]->set_notnull();
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
      }
      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);
2348 2349
        table->field[5]->store(def.ptr(), def.length(), def.charset());
        table->field[5]->set_notnull();
2350 2351 2352
      }
      else if (field->unireg_check == Field::NEXT_NUMBER ||
               field->maybe_null())
2353
        table->field[5]->set_null();                // Null as default
2354 2355
      else
      {
2356 2357
        table->field[5]->store("",0, cs);
        table->field[5]->set_notnull();
2358 2359 2360
      }
      pos=(byte*) ((flags & NOT_NULL_FLAG) &&
                   field->type() != FIELD_TYPE_TIMESTAMP ?
2361
                   "NO" : "YES");
2362 2363
      table->field[6]->store((const char*) pos,
                             strlen((const char*) pos), cs);
2364 2365
      if (field->has_charset())
      {
2366
        table->field[8]->store((longlong) field->field_length/
2367
                               field->charset()->mbmaxlen);
2368
        table->field[8]->set_notnull();
2369
        table->field[9]->store((longlong) field->field_length);
2370 2371
        table->field[9]->set_notnull();
      }
2372 2373 2374 2375

      {
        uint dec =field->decimals();
        switch (field->type()) {
2376
        case FIELD_TYPE_NEWDECIMAL:
2377 2378
          table->field[10]->store((longlong)
                                  ((Field_new_decimal*)field)->precision);
2379 2380 2381 2382
          table->field[10]->set_notnull();
          table->field[11]->store((longlong) field->decimals());
          table->field[11]->set_notnull();
          break;
2383 2384 2385 2386 2387 2388 2389
        case FIELD_TYPE_DECIMAL:
        {
          uint int_part=field->field_length - (dec  ? dec + 1 : 0);
          table->field[10]->store((longlong) (int_part + dec - 1));
          table->field[10]->set_notnull();
          table->field[11]->store((longlong) field->decimals());
          table->field[11]->set_notnull();
2390
          break;
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
        }
        case FIELD_TYPE_TINY:
        case FIELD_TYPE_SHORT:
        case FIELD_TYPE_LONG:
        case FIELD_TYPE_LONGLONG:
        case FIELD_TYPE_INT24:
        case FIELD_TYPE_FLOAT:  
        case FIELD_TYPE_DOUBLE:
        {
          table->field[10]->store((longlong) field->field_length);
          table->field[10]->set_notnull();
          if (dec != NOT_FIXED_DEC)
          {
            table->field[11]->store((longlong) dec);
            table->field[11]->set_notnull();
          }
2407
          break;
2408 2409 2410 2411
        }
        default:
          break;
        }
2412 2413 2414 2415 2416 2417 2418
      }
      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();
2419 2420 2421 2422
        pos=(byte*) field->charset()->name;
        table->field[13]->store((const char*) pos,
                                strlen((const char*) pos), cs);
        table->field[13]->set_notnull();
2423 2424 2425 2426
      }
      pos=(byte*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
                   (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
                   (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
2427
      table->field[15]->store((const char*) pos,
2428
                              strlen((const char*) pos), cs);
2429
      end= tmp;
2430 2431 2432
      if (field->unireg_check == Field::NEXT_NUMBER)
        end=strmov(tmp,"auto_increment");
      table->field[16]->store(tmp, (uint) (end-tmp), cs);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
2433 2434 2435

      end=tmp;
      table->field[18]->store(field->comment.str, field->comment.length, cs);
2436 2437
      if (schema_table_store_record(thd, table))
        DBUG_RETURN(1);
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
    }
  }
  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;
  for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
  {
    CHARSET_INFO *tmp_cs= cs[0];
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
        (tmp_cs->state & MY_CS_AVAILABLE) &&
        !(wild && wild[0] &&
	  wild_case_compare(scs, tmp_cs->csname,wild)))
    {
2459
      restore_record(table, s->default_values);
2460
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
2461 2462
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
      table->field[2]->store(tmp_cs->comment ? tmp_cs->comment : "",
2463 2464 2465
			     strlen(tmp_cs->comment ? tmp_cs->comment : ""),
                             scs);
      table->field[3]->store((longlong) tmp_cs->mbmaxlen);
2466 2467
      if (schema_table_store_record(thd, table))
        return 1;
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
    }
  }
  return 0;
}


int fill_schema_collation(THD *thd, TABLE_LIST *tables, COND *cond)
{
  CHARSET_INFO **cs;
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
  TABLE *table= tables->table;
  CHARSET_INFO *scs= system_charset_info;
  for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
  {
    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;
    for ( cl= all_charsets; cl < all_charsets+255 ;cl ++)
    {
      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;
2497
	restore_record(table, s->default_values);
2498 2499 2500 2501 2502 2503 2504 2505
	table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
        table->field[2]->store((longlong) tmp_cl->number);
        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);
        table->field[5]->store((longlong) tmp_cl->strxfrm_multiply);
2506 2507
        if (schema_table_store_record(thd, table))
          return 1;
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
      }
    }
  }
  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;
  for ( cs= all_charsets ; cs < all_charsets+255 ; cs++ )
  {
    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;
    for ( cl= all_charsets; cl < all_charsets+255 ;cl ++)
    {
      CHARSET_INFO *tmp_cl= cl[0];
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
          !my_charset_same(tmp_cs,tmp_cl))
	continue;
2533
      restore_record(table, s->default_values);
2534 2535
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
2536 2537
      if (schema_table_store_record(thd, table))
        return 1;
2538 2539 2540 2541 2542 2543
    }
  }
  return 0;
}


2544
bool store_schema_proc(THD *thd, TABLE *table, TABLE *proc_table,
2545
                       const char *wild, bool full_access, const char *sp_user)
2546 2547 2548 2549 2550
{
  String tmp_string;
  TIME time;
  LEX *lex= thd->lex;
  CHARSET_INFO *cs= system_charset_info;
2551 2552 2553 2554 2555 2556
  const char *sp_db, *sp_name, *definer;
  sp_db= get_field(thd->mem_root, proc_table->field[0]);
  sp_name= get_field(thd->mem_root, proc_table->field[1]);
  definer= get_field(thd->mem_root, proc_table->field[11]);
  if (!full_access)
    full_access= !strcmp(sp_user, definer);
2557
  if (!full_access && check_some_routine_access(thd, sp_db, sp_name))
2558
    return 0;
2559

2560 2561 2562 2563 2564 2565
  if (lex->orig_sql_command == SQLCOM_SHOW_STATUS_PROC &&
      proc_table->field[2]->val_int() == TYPE_ENUM_PROCEDURE ||
      lex->orig_sql_command == SQLCOM_SHOW_STATUS_FUNC &&
      proc_table->field[2]->val_int() == TYPE_ENUM_FUNCTION ||
      lex->orig_sql_command == SQLCOM_END)
  {
2566 2567
    restore_record(table, s->default_values);
    if (!wild || !wild[0] || !wild_compare(sp_name, wild, 0))
2568
    {
2569
      table->field[3]->store(sp_name, strlen(sp_name), cs);
2570 2571
      get_field(thd->mem_root, proc_table->field[3], &tmp_string);
      table->field[0]->store(tmp_string.ptr(), tmp_string.length(), cs);
2572
      table->field[2]->store(sp_db, strlen(sp_db), cs);
2573 2574
      get_field(thd->mem_root, proc_table->field[2], &tmp_string);
      table->field[4]->store(tmp_string.ptr(), tmp_string.length(), cs);
2575 2576 2577 2578 2579 2580
      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();
      }
2581 2582 2583 2584 2585
      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);
      }
2586
      table->field[6]->store("SQL", 3, cs);
2587
      table->field[10]->store("SQL", 3, cs);
2588
      get_field(thd->mem_root, proc_table->field[6], &tmp_string);
2589
      table->field[11]->store(tmp_string.ptr(), tmp_string.length(), cs);
2590 2591 2592 2593
      if (proc_table->field[5]->val_int() == SP_CONTAINS_SQL)
      {
        table->field[12]->store("CONTAINS SQL", 12 , cs);
      }
2594 2595
      get_field(thd->mem_root, proc_table->field[7], &tmp_string);
      table->field[14]->store(tmp_string.ptr(), tmp_string.length(), cs);
2596 2597
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[12])->get_time(&time);
2598
      table->field[15]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
2599 2600
      bzero((char *)&time, sizeof(time));
      ((Field_timestamp *) proc_table->field[13])->get_time(&time);
2601
      table->field[16]->store_time(&time, MYSQL_TIMESTAMP_DATETIME);
2602
      get_field(thd->mem_root, proc_table->field[14], &tmp_string);
2603
      table->field[17]->store(tmp_string.ptr(), tmp_string.length(), cs);
2604
      get_field(thd->mem_root, proc_table->field[15], &tmp_string);
2605
      table->field[18]->store(tmp_string.ptr(), tmp_string.length(), cs);
2606
      table->field[19]->store(definer, strlen(definer), cs);
2607
      return schema_table_store_record(thd, table);
2608 2609
    }
  }
2610
  return 0;
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
}


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;
  TABLE *table= tables->table, *old_open_tables= thd->open_tables;
2621 2622
  bool full_access;
  char definer[HOSTNAME_LENGTH+USERNAME_LENGTH+2];
2623 2624
  DBUG_ENTER("fill_schema_proc");

2625
  strxmov(definer, thd->priv_user, "@", thd->priv_host, NullS);
2626 2627
  bzero((char*) &proc_tables,sizeof(proc_tables));
  proc_tables.db= (char*) "mysql";
2628
  proc_tables.db_length= 5;
2629
  proc_tables.table_name= proc_tables.alias= (char*) "proc";
2630
  proc_tables.table_name_length= 4;
2631
  proc_tables.lock_type= TL_READ;
2632
  full_access= !check_table_access(thd, SELECT_ACL, &proc_tables, 1);
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
  if (!(proc_table= open_ltable(thd, &proc_tables, TL_READ)))
  {
    DBUG_RETURN(1);
  }
  proc_table->file->ha_index_init(0);
  if ((res= proc_table->file->index_first(proc_table->record[0])))
  {
    res= (res == HA_ERR_END_OF_FILE) ? 0 : 1;
    goto err;
  }
2643 2644 2645 2646 2647
  if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
  {
    res= 1;
    goto err;
  }
2648
  while (!proc_table->file->index_next(proc_table->record[0]))
2649 2650 2651 2652 2653 2654 2655
  {
    if (store_schema_proc(thd, table, proc_table, wild, full_access, definer))
    {
      res= 1;
      goto err;
    }
  }
2656 2657 2658 2659 2660 2661 2662 2663

err:
  proc_table->file->ha_index_end();
  close_thread_tables(thd, 0, 0, old_open_tables);
  DBUG_RETURN(res);
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2664 2665 2666 2667
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)
2668 2669 2670
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_stat_record");
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
  if (res)
  {
    if (thd->lex->orig_sql_command != SQLCOM_SHOW_KEYS)
    {
      /*
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
        rather than in SHOW KEYS
      */ 
      if (!tables->view)
        push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                     thd->net.last_errno, thd->net.last_error);
      thd->clear_error();
      res= 0;
    }
    DBUG_RETURN(res);
  }
  else if (!tables->view)
2688 2689 2690 2691 2692 2693
  {
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
2694
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
2695 2696 2697 2698 2699
    {
      KEY_PART_INFO *key_part= key_info->key_part;
      const char *str;
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
      {
2700
        restore_record(table, s->default_values);
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
        table->field[1]->store(base_name, strlen(base_name), cs);
        table->field[2]->store(file_name, strlen(file_name), cs);
        table->field[3]->store((longlong) ((key_info->flags & 
                                            HA_NOSAME) ? 0 :1));
        table->field[4]->store(base_name, strlen(base_name), cs);
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
        table->field[6]->store((longlong) (j+1));
        str=(key_part->field ? key_part->field->field_name :
             "?unknown field?");
        table->field[7]->store(str, strlen(str), cs);
        if (show_table->file->index_flags(i, j, 0) & HA_READ_ORDER)
        {
          table->field[8]->store(((key_part->key_part_flag &
                                   HA_REVERSE_SORT) ?
                                  "D" : "A"), 1, cs);
          table->field[8]->set_notnull();
        }
        KEY *key=show_table->key_info+i;
        if (key->rec_per_key[j])
        {
          ha_rows records=(show_table->file->records /
                           key->rec_per_key[j]);
          table->field[9]->store((longlong) records);
          table->field[9]->set_notnull();
        }
        if (!(key_info->flags & HA_FULLTEXT) && 
            (!key_part->field ||
             key_part->length != 
             show_table->field[key_part->fieldnr-1]->key_length()))
        {
bar@mysql.com's avatar
bar@mysql.com committed
2731 2732
          table->field[10]->store((longlong) key_part->length / 
                                  key_part->field->charset()->mbmaxlen);
2733 2734 2735 2736 2737 2738 2739
          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);
2740
        if (!show_table->s->keys_in_use.is_set(i))
2741 2742 2743 2744
          table->field[14]->store("disabled", 8, cs);
        else
          table->field[14]->store("", 0, cs);
        table->field[14]->set_notnull();
2745 2746
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
2747 2748 2749
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2750
  DBUG_RETURN(res);
2751 2752 2753
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2754 2755 2756 2757
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)
2758 2759 2760 2761 2762 2763 2764
{
  CHARSET_INFO *cs= system_charset_info;
  DBUG_ENTER("get_schema_views_record");
  if (!res)
  {
    if (tables->view)
    {
2765
      restore_record(table, s->default_values);
2766
      table->field[1]->store(tables->view_db.str, tables->view_db.length, cs);
2767 2768
      table->field[2]->store(tables->view_name.str, tables->view_name.length,
                             cs);
2769
      table->field[3]->store(tables->query.str, tables->query.length, cs);
2770 2771 2772 2773

      if (tables->with_check != VIEW_CHECK_NONE)
      {
        if (tables->with_check == VIEW_CHECK_LOCAL)
2774
          table->field[4]->store("LOCAL", 5, cs);
2775
        else
2776
          table->field[4]->store("CASCADED", 8, cs);
2777 2778 2779 2780
      }
      else
        table->field[4]->store("NONE", 4, cs);

2781 2782 2783 2784
      if (tables->updatable_view)
        table->field[5]->store("YES", 3, cs);
      else
        table->field[5]->store("NO", 2, cs);
2785
      DBUG_RETURN(schema_table_store_record(thd, table));
2786 2787
    }
  }
2788 2789 2790 2791 2792 2793 2794 2795
  else
  {
    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);
2796 2797 2798
}


2799 2800 2801
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)
2802 2803
{
  CHARSET_INFO *cs= system_charset_info;
2804
  restore_record(table, s->default_values);
2805 2806 2807 2808 2809
  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);
2810
  return schema_table_store_record(thd, table);
2811 2812 2813
}


gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
2814
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
2815 2816 2817
					 TABLE *table, bool res,
					 const char *base_name,
					 const char *file_name)
2818
{
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
2819
  DBUG_ENTER("get_schema_constraints_record");
2820 2821 2822 2823 2824 2825 2826 2827 2828
  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)
2829 2830 2831 2832
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
2833
    uint primary_key= show_table->s->primary_key;
2834 2835 2836
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
2837
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
2838 2839
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
2840 2841
        continue;

2842
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
2843 2844 2845 2846 2847
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
                              strlen(key_info->name), "PRIMARY KEY", 11))
          DBUG_RETURN(1);
      }
2848
      else if (key_info->flags & HA_NOSAME)
2849 2850 2851 2852 2853
      {
        if (store_constraints(thd, table, base_name, file_name, key_info->name,
                              strlen(key_info->name), "UNIQUE", 6))
          DBUG_RETURN(1);
      }
2854 2855 2856 2857 2858 2859 2860
    }

    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++))
    {
2861 2862 2863 2864 2865
      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);
2866 2867
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2868
  DBUG_RETURN(res);
2869 2870 2871
}


2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
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);
  table->field[7]->store((longlong) idx);
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2886 2887 2888 2889 2890
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)
2891 2892
{
  DBUG_ENTER("get_schema_key_column_usage_record");
2893 2894 2895 2896 2897 2898 2899 2900 2901
  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)
2902 2903 2904 2905
  {
    List<FOREIGN_KEY_INFO> f_key_list;
    TABLE *show_table= tables->table;
    KEY *key_info=show_table->key_info;
2906
    uint primary_key= show_table->s->primary_key;
2907 2908 2909
    show_table->file->info(HA_STATUS_VARIABLE | 
                           HA_STATUS_NO_LOCK |
                           HA_STATUS_TIME);
2910
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
2911 2912
    {
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2913
        continue;
2914 2915 2916 2917 2918 2919 2920
      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++;
2921
          restore_record(table, s->default_values);
2922 2923 2924 2925 2926 2927
          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);
2928 2929
          if (schema_table_store_record(thd, table))
            DBUG_RETURN(1);
2930 2931 2932 2933 2934 2935 2936 2937 2938
        }
      }
    }

    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++))
    {
2939
      LEX_STRING *f_info;
2940 2941 2942 2943 2944
      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++))
      {
2945
        it1++;                                  // Ignore r_info
2946
        f_idx++;
2947
        restore_record(table, s->default_values);
2948 2949 2950 2951 2952 2953 2954
        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);
        table->field[8]->store((longlong) f_idx);
        table->field[8]->set_notnull();
2955 2956
        if (schema_table_store_record(thd, table))
          DBUG_RETURN(1);
2957 2958 2959
      }
    }
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2960
  DBUG_RETURN(res);
2961 2962 2963
}


2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
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;
  if (!(open_list=list_open_tables(thd,wild)) && thd->is_fatal_error)
    DBUG_RETURN(1);

  for (; open_list ; open_list=open_list->next)
  {
2976
    restore_record(table, s->default_values);
2977 2978 2979 2980
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
    table->field[2]->store((longlong) open_list->in_use);
    table->field[3]->store((longlong)  open_list->locked);
2981 2982
    if (schema_table_store_record(thd, table))
      DBUG_RETURN(1);
2983 2984 2985 2986 2987 2988 2989 2990
  }
  DBUG_RETURN(0);
}


int fill_variables(THD *thd, TABLE_LIST *tables, COND *cond)
{
  DBUG_ENTER("fill_variables");
2991
  int res= 0;
2992 2993
  LEX *lex= thd->lex;
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
2994 2995 2996 2997
  pthread_mutex_lock(&LOCK_global_system_variables);
  res= show_status_array(thd, wild, init_vars, 
                         lex->option_type, 0, "", tables->table);
  pthread_mutex_unlock(&LOCK_global_system_variables);
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
  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;
3009 3010
  ha_update_statistics();                    /* Export engines statistics */
  pthread_mutex_lock(&LOCK_status);
3011 3012
  if (lex->option_type == OPT_GLOBAL)
    calc_sum_of_all_status(&tmp);
3013 3014 3015 3016
  res= show_status_array(thd, wild, status_vars, OPT_GLOBAL,
                         (lex->option_type == OPT_GLOBAL ? 
                          &tmp: &thd->status_var), "",tables->table);
  pthread_mutex_unlock(&LOCK_status);
3017 3018 3019 3020
  DBUG_RETURN(res);
}


3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
/*
  Find schema_tables elment by name

  SYNOPSIS
    find_schema_table()
    thd                 thread handler
    table_name          table name

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

ST_SCHEMA_TABLE *find_schema_table(THD *thd, const char* table_name)
{
  ST_SCHEMA_TABLE *schema_table= schema_tables;
  for ( ; schema_table->table_name; schema_table++)
  {
    if (!my_strcasecmp(system_charset_info,
                       schema_table->table_name,
                       table_name))
      return schema_table;
  }
  return 0;
}


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


/*
  Create information_schema table using schema_table data

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

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

3067
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
3068 3069 3070 3071 3072
{
  int field_count= 0;
  Item *item;
  TABLE *table;
  List<Item> field_list;
3073
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
3074
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
3075
  CHARSET_INFO *cs= system_charset_info;
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095
  DBUG_ENTER("create_schema_table");

  for ( ; fields_info->field_name; fields_info++)
  {
    switch (fields_info->field_type) {
    case MYSQL_TYPE_LONG:
      if (!(item= new Item_int(fields_info->field_name,
                               fields_info->value,
                               fields_info->field_length)))
      {
        DBUG_RETURN(0);
      }
      break;
    case MYSQL_TYPE_TIMESTAMP:
      if (!(item=new Item_datetime(fields_info->field_name)))
      {
        DBUG_RETURN(0);
      }
      break;
    default:
3096 3097
      /* this should be changed when Item_empty_string is fixed(in 4.1) */
      if (!(item= new Item_empty_string("", 0, cs)))
3098 3099 3100
      {
        DBUG_RETURN(0);
      }
3101
      item->max_length= fields_info->field_length * cs->mbmaxlen;
3102 3103
      item->set_name(fields_info->field_name,
                     strlen(fields_info->field_name), cs);
3104 3105 3106 3107 3108 3109 3110 3111 3112
      break;
    }
    field_list.push_back(item);
    item->maybe_null= fields_info->maybe_null;
    field_count++;
  }
  TMP_TABLE_PARAM *tmp_table_param =
    (TMP_TABLE_PARAM*) (thd->calloc(sizeof(TMP_TABLE_PARAM)));
  tmp_table_param->init();
3113
  tmp_table_param->table_charset= cs;
3114
  tmp_table_param->field_count= field_count;
3115
  tmp_table_param->schema_table= 1;
3116 3117 3118 3119 3120
  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),
3121
                                HA_POS_ERROR, table_list->alias)))
3122
    DBUG_RETURN(0);
3123
  table_list->schema_table_param= tmp_table_param;
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
  DBUG_RETURN(table);
}


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

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

  RETURN
   -1	errror
    0	success
*/

int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
{
  ST_FIELD_INFO *field_info= schema_table->fields_info;
  for ( ; field_info->field_name; field_info++)
  {
    if (field_info->old_name)
    {
      Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
      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;

  if (!sel->item_list.elements)
  {
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
    String buffer(tmp,sizeof(tmp), system_charset_info);
    Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
    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())
    {
      buffer.append(" (");
      buffer.append(lex->wild->ptr());
      buffer.append(")");
    }
    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;

  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())
  {
    buffer.append(" (");
    buffer.append(lex->wild->ptr());
    buffer.append(")");
  }
  Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
  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];
    field= new Item_field(NullS, NullS, field_info->field_name);
    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)
{
3228 3229 3230 3231
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
  int *field_num= fields_arr;
  ST_FIELD_INFO *field_info;
  for (; *field_num >= 0; field_num++)
3232
  {
3233 3234 3235 3236 3237 3238 3239
    field_info= &schema_table->fields_info[*field_num];
    if (!thd->lex->verbose && (*field_num == 13 ||
                               *field_num == 17 ||
                               *field_num == 18))
      continue;
    Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
    if (field)
3240
    {
3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
      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;
}


3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
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;
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
    Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
    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;
}


3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
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;
  for (; *field_num >= 0; field_num++)
  {
    field_info= &schema_table->fields_info[*field_num];
    Item_field *field= new Item_field(NullS, NullS, field_info->field_name);
    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;
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
    }
  }
  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");
3314
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
3315 3316 3317
  {
    DBUG_RETURN(1);
  }
3318
  table->s->tmp_table= TMP_TABLE;
3319
  table->grant.privilege= SELECT_ACL;
3320 3321 3322 3323 3324 3325 3326
  /*
    This test is necessary to make
    case insensitive file systems +
    upper case table names(information schema tables) +
    views
    working correctly
  */
3327 3328 3329 3330
  if (table_list->schema_table_name)
    table->alias_name_used= my_strcasecmp(table_alias_charset,
                                          table_list->schema_table_name,
                                          table_list->alias);
3331
  table_list->table_name= (char*) table->s->table_name;
3332
  table_list->table_name_length= strlen(table->s->table_name);
3333 3334 3335 3336
  table_list->table= table;
  table->next= thd->derived_tables;
  thd->derived_tables= table;
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
3337
  lex->safe_to_cache_query= 0;
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376

  if (table_list->schema_table_reformed) // show command
  {
    SELECT_LEX *sel= lex->current_select;
    uint i= 0;
    Item *item;
    Field_translator *transl;

    if (table_list->field_translation)
    {
      Field_translator *end= table_list->field_translation +
        sel->item_list.elements;
      for (transl= table_list->field_translation; transl < end; transl++)
      {
        if (!transl->item->fixed &&
            transl->item->fix_fields(thd, table_list, &transl->item))
          DBUG_RETURN(1);
      }
      DBUG_RETURN(0);
    }
    List_iterator_fast<Item> it(sel->item_list);
    if (!(transl=
          (Field_translator*)(thd->current_arena->
                              alloc(sel->item_list.elements *
                                    sizeof(Field_translator)))))
    {
      DBUG_RETURN(1);
    }
    while ((item= it++))
    {
      char *name= item->name;
      transl[i].item= item;
      if (!item->fixed && item->fix_fields(thd, table_list, &transl[i].item))
        DBUG_RETURN(1);
      transl[i++].name= name;
    }
    table_list->field_translation= transl;
  }

3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
  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");
  /* 
     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);
3409
  if (schema_table->old_format(thd, schema_table) ||      /* Handle old syntax */
3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
      !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);
}


/*
3421
  Fill temporary schema tables before SELECT
3422 3423 3424 3425 3426 3427

  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
3428 3429
    FALSE success
    TRUE  error
3430 3431
*/

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3432
bool get_schema_tables_result(JOIN *join)
3433 3434 3435
{
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
  THD *thd= join->thd;
3436 3437
  LEX *lex= thd->lex;
  bool result= 0;
3438 3439 3440
  DBUG_ENTER("get_schema_tables_result");

  thd->no_warnings_for_error= 1;
3441 3442 3443 3444
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
  {  
    if (!tab->table || !tab->table->pos_in_table_list)
      break;
3445

3446
    TABLE_LIST *table_list= tab->table->pos_in_table_list;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3447
    if (table_list->schema_table && thd->fill_derived_tables())
3448
    {
3449
      TABLE_LIST **query_tables_last= lex->query_tables_last;
3450 3451
      TABLE *old_derived_tables= thd->derived_tables;
      MYSQL_LOCK *sql_lock= thd->lock;
3452
      lex->sql_command= SQLCOM_SHOW_FIELDS;
3453
      DBUG_ASSERT(!*query_tables_last);
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
      if (&lex->unit != lex->current_select->master_unit()) // is subselect
      {
        table_list->table->file->extra(HA_EXTRA_RESET_STATE);
        table_list->table->file->delete_all_rows();
        free_io_cache(table_list->table);
        filesort_free_buffers(table_list->table);
      }
      else
        table_list->table->file->records= 0;

      thd->derived_tables= 0;
3465 3466 3467
      thd->lock=0;
      if (table_list->schema_table->fill_table(thd, table_list,
                                               tab->select_cond))
3468
        result= 1;
3469
      thd->lock= sql_lock;
3470
      lex->sql_command= SQLCOM_SELECT;
3471
      thd->derived_tables= old_derived_tables;
3472
      lex->query_tables_last= query_tables_last;
3473
      *query_tables_last= 0;
3474 3475
    }
  }
3476
  thd->no_warnings_for_error= 0;
3477
  DBUG_RETURN(result);
3478 3479 3480 3481 3482
}


ST_FIELD_INFO schema_fields_info[]=
{
3483 3484
  {"CATALOG_NAME", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"SCHEMA_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Database"},
3485
  {"DEFAULT_CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
3486
  {"DEFAULT_COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
3487
  {"SQL_PATH", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
3488
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3489 3490 3491 3492 3493
};


ST_FIELD_INFO tables_fields_info[]=
{
3494 3495 3496 3497 3498 3499 3500
  {"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"},
3501
  {"TABLE_ROWS", 21 , MYSQL_TYPE_LONG, 0, 1, "Rows"},
3502 3503 3504 3505 3506 3507 3508 3509 3510
  {"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"},
3511
  {"TABLE_COLLATION", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
3512 3513
  {"CHECKSUM", 21 , MYSQL_TYPE_LONG, 0, 1, "Checksum"},
  {"CREATE_OPTIONS", 255, MYSQL_TYPE_STRING, 0, 1, "Create_options"},
3514
  {"TABLE_COMMENT", 80, MYSQL_TYPE_STRING, 0, 0, "Comment"},
3515
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3516 3517 3518 3519 3520
};


ST_FIELD_INFO columns_fields_info[]=
{
3521 3522 3523 3524 3525
  {"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},
3526 3527
  {"COLUMN_DEFAULT", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, "Default"},
  {"IS_NULLABLE", 3, MYSQL_TYPE_STRING, 0, 0, "Null"},
3528
  {"DATA_TYPE", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, 0},
3529 3530
  {"CHARACTER_MAXIMUM_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"CHARACTER_OCTET_LENGTH", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
3531 3532
  {"NUMERIC_PRECISION", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
  {"NUMERIC_SCALE", 21 , MYSQL_TYPE_LONG, 0, 1, 0},
3533 3534
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 1, 0},
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 1, "Collation"},
3535
  {"COLUMN_TYPE", 65535, MYSQL_TYPE_STRING, 0, 0, "Type"},
3536
  {"COLUMN_KEY", 3, MYSQL_TYPE_STRING, 0, 0, "Key"},
3537 3538
  {"EXTRA", 20, MYSQL_TYPE_STRING, 0, 0, "Extra"},
  {"PRIVILEGES", 80, MYSQL_TYPE_STRING, 0, 0, "Privileges"},
3539
  {"COLUMN_COMMENT", 255, MYSQL_TYPE_STRING, 0, 0, "Comment"},
3540
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3541 3542 3543 3544 3545
};


ST_FIELD_INFO charsets_fields_info[]=
{
3546 3547
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
  {"DEFAULT_COLLATE_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Default collation"},
3548
  {"DESCRIPTION", 60, MYSQL_TYPE_STRING, 0, 0, "Description"},
3549
  {"MAXLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Maxlen"},
3550
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3551 3552 3553 3554 3555
};


ST_FIELD_INFO collation_fields_info[]=
{
3556 3557
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Collation"},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, "Charset"},
3558
  {"ID", 11, MYSQL_TYPE_LONG, 0, 0, "Id"},
3559 3560
  {"IS_DEFAULT", 3, MYSQL_TYPE_STRING, 0, 0, "Default"},
  {"IS_COMPILED", 3, MYSQL_TYPE_STRING, 0, 0, "Compiled"},
3561
  {"SORTLEN", 3 ,MYSQL_TYPE_LONG, 0, 0, "Sortlen"},
3562
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3563 3564 3565 3566 3567
};


ST_FIELD_INFO coll_charset_app_fields_info[]=
{
3568 3569
  {"COLLATION_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
  {"CHARACTER_SET_NAME", 64, MYSQL_TYPE_STRING, 0, 0, 0},
3570
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3571 3572 3573 3574 3575
};


ST_FIELD_INFO proc_fields_info[]=
{
3576 3577 3578 3579 3580
  {"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"},
3581 3582
  {"DTD_IDENTIFIER", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"ROUTINE_BODY", 8, MYSQL_TYPE_STRING, 0, 0, 0},
3583
  {"ROUTINE_DEFINITION", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
3584 3585
  {"EXTERNAL_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
  {"EXTERNAL_LANGUAGE", NAME_LEN, MYSQL_TYPE_STRING, 0, 1, 0},
3586
  {"PARAMETER_STYLE", 8, MYSQL_TYPE_STRING, 0, 0, 0},
3587 3588 3589 3590
  {"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"},
3591 3592
  {"CREATED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Created"},
  {"LAST_ALTERED", 0, MYSQL_TYPE_TIMESTAMP, 0, 0, "Modified"},
3593
  {"SQL_MODE", 65535, MYSQL_TYPE_STRING, 0, 0, 0},
3594
  {"ROUTINE_COMMENT", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Comment"},
3595
  {"DEFINER", 77, MYSQL_TYPE_STRING, 0, 0, "Definer"},
3596
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3597 3598 3599 3600 3601
};


ST_FIELD_INFO stat_fields_info[]=
{
3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
  {"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}
3618 3619 3620 3621 3622
};


ST_FIELD_INFO view_fields_info[]=
{
3623 3624 3625 3626
  {"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},
3627
  {"CHECK_OPTION", 8, MYSQL_TYPE_STRING, 0, 0, 0},
3628 3629
  {"IS_UPDATABLE", 3, MYSQL_TYPE_STRING, 0, 0, 0},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3630 3631 3632 3633 3634
};


ST_FIELD_INFO user_privileges_fields_info[]=
{
3635 3636 3637 3638 3639
  {"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}
3640 3641 3642 3643 3644
};


ST_FIELD_INFO schema_privileges_fields_info[]=
{
3645 3646 3647 3648 3649 3650
  {"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}
3651 3652 3653 3654 3655
};


ST_FIELD_INFO table_privileges_fields_info[]=
{
3656 3657 3658 3659 3660 3661 3662
  {"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}
3663 3664 3665 3666 3667
};


ST_FIELD_INFO column_privileges_fields_info[]=
{
3668 3669 3670 3671 3672 3673 3674 3675
  {"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}
3676 3677 3678 3679 3680
};


ST_FIELD_INFO table_constraints_fields_info[]=
{
3681 3682 3683 3684 3685 3686 3687
  {"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}
3688 3689 3690 3691 3692
};


ST_FIELD_INFO key_column_usage_fields_info[]=
{
3693 3694 3695
  {"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},
3696
  {"TABLE_CATALOG", FN_REFLEN, MYSQL_TYPE_STRING, 0, 1, 0},
3697 3698 3699 3700
  {"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},
3701
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,MYSQL_TYPE_LONG, 0, 1, 0},
3702
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
3703 3704 3705 3706 3707
};


ST_FIELD_INFO table_names_fields_info[]=
{
3708 3709 3710 3711 3712
  {"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}
3713 3714 3715
};


3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
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}
};


ST_FIELD_INFO variables_fields_info[]=
{
  {"Variable_name", 80, MYSQL_TYPE_STRING, 0, 0, "Variable_name"},
  {"Value", 255, MYSQL_TYPE_STRING, 0, 0, "Value"},
  {0, 0, MYSQL_TYPE_STRING, 0, 0, 0}
};


3734 3735 3736 3737 3738 3739 3740
/*
  Description of ST_FIELD_INFO in table.h
*/

ST_SCHEMA_TABLE schema_tables[]=
{
  {"SCHEMATA", schema_fields_info, create_schema_table,
3741
   fill_schema_shemata, make_schemata_old_format, 0, 1, -1, 0},
3742
  {"TABLES", tables_fields_info, create_schema_table, 
3743
   get_all_tables, make_old_format, get_schema_tables_record, 1, 2, 0},
3744
  {"COLUMNS", columns_fields_info, create_schema_table, 
3745
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0},
3746
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
3747
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0},
3748
  {"COLLATIONS", collation_fields_info, create_schema_table, 
3749
   fill_schema_collation, make_old_format, 0, -1, -1, 0},
3750
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
3751
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0},
3752
  {"ROUTINES", proc_fields_info, create_schema_table, 
3753
    fill_schema_proc, make_proc_old_format, 0, -1, -1, 0},
3754
  {"STATISTICS", stat_fields_info, create_schema_table, 
3755
    get_all_tables, make_old_format, get_schema_stat_record, 1, 2, 0},
3756
  {"VIEWS", view_fields_info, create_schema_table, 
3757
    get_all_tables, 0, get_schema_views_record, 1, 2, 0},
3758
  {"USER_PRIVILEGES", user_privileges_fields_info, create_schema_table, 
3759
    fill_schema_user_privileges, 0, 0, -1, -1, 0},
3760
  {"SCHEMA_PRIVILEGES", schema_privileges_fields_info, create_schema_table,
3761
    fill_schema_schema_privileges, 0, 0, -1, -1, 0},
3762
  {"TABLE_PRIVILEGES", table_privileges_fields_info, create_schema_table,
3763
    fill_schema_table_privileges, 0, 0, -1, -1, 0},
3764
  {"COLUMN_PRIVILEGES", column_privileges_fields_info, create_schema_table,
3765
    fill_schema_column_privileges, 0, 0, -1, -1, 0},
3766
  {"TABLE_CONSTRAINTS", table_constraints_fields_info, create_schema_table,
3767
    get_all_tables, 0, get_schema_constraints_record, 3, 4, 0},
3768
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
3769
    get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0},
3770
  {"TABLE_NAMES", table_names_fields_info, create_schema_table,
3771
   get_all_tables, make_table_names_old_format, 0, 1, 2, 1},
3772 3773 3774 3775 3776 3777
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
   fill_open_tables, make_old_format, 0, -1, -1, 1},
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
   make_old_format, 0, -1, -1, 1},
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
   make_old_format, 0, -1, -1, 1},
3778
  {0, 0, 0, 0, 0, 0, 0, 0, 0}
3779 3780 3781
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
3782
#ifdef __GNUC__
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3783
template class List_iterator_fast<char>;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3784 3785
template class List<char>;
#endif