~drizzle-trunk/drizzle/development

520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
1 by brian
clean slate
19
20
21
/* Function with list databases, tables or fields */
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
22
#include <drizzled/server_includes.h>
23
#include <drizzled/sql_select.h>
575.4.7 by Monty Taylor
More header cleanup.
24
#include <drizzled/show.h>
212.5.38 by Monty Taylor
Moved my_dir (and removed references to a lot of places)
25
#include <mysys/my_dir.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
26
#include <drizzled/gettext.h>
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
27
#include <drizzled/util/convert.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
28
#include <drizzled/error.h>
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
29
#include <drizzled/tztime.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
30
#include <drizzled/data_home.h>
642.1.63 by Lee
more header file cleanup
31
#include <drizzled/item/blob.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
32
#include <drizzled/item/cmpfunc.h>
642.1.17 by Lee
header file clean up
33
#include <drizzled/item/return_int.h>
642.1.23 by Lee
more header file cleanup
34
#include <drizzled/item/empty_string.h>
642.1.18 by Lee
header file clean up
35
#include <drizzled/item/return_date_time.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
36
#include <drizzled/virtual_column_info.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
37
#include <drizzled/sql_base.h>
38
#include <drizzled/db.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
39
#include <drizzled/field/timestamp.h>
670.1.1 by Monty Taylor
Renamed fdecimal.* to decimal.*. Let's see how many things we can break!
40
#include <drizzled/field/decimal.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
41
#include <drizzled/lock.h>
675 by Brian Aker
Cleanup around item includes.
42
#include <drizzled/item/return_date_time.h>
43
#include <drizzled/item/empty_string.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
44
45
#include <string>
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
46
#include <iostream>
47
#include <sstream>
48
49
using namespace std;
1 by brian
clean slate
50
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
51
inline const char *
52
str_or_nil(const char *str)
53
{
54
  return str ? str : "<nil>";
55
}
1 by brian
clean slate
56
57
/* Match the values of enum ha_choice */
58
static const char *ha_choice_values[] = {"", "0", "1"};
59
520.1.22 by Brian Aker
Second pass of thd cleanup
60
static void store_key_options(Session *session, String *packet, Table *table,
1 by brian
clean slate
61
                              KEY *key_info);
62
63
64
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
65
int wild_case_compare(const CHARSET_INFO * const cs, const char *str,const char *wildstr)
1 by brian
clean slate
66
{
67
  register int flag;
68
  while (*wildstr)
69
  {
70
    while (*wildstr && *wildstr != wild_many && *wildstr != wild_one)
71
    {
72
      if (*wildstr == wild_prefix && wildstr[1])
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
73
        wildstr++;
74
      if (my_toupper(cs, *wildstr++) != my_toupper(cs, *str++))
75
        return(1);
1 by brian
clean slate
76
    }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
77
    if (! *wildstr )
78
      return (*str != 0);
1 by brian
clean slate
79
    if (*wildstr++ == wild_one)
80
    {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
81
      if (! *str++)
82
        return (1);	/* One char; skip */
1 by brian
clean slate
83
    }
84
    else
85
    {						/* Found '*' */
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
86
      if (!*wildstr)
87
        return(0);		/* '*' as last char: OK */
1 by brian
clean slate
88
      flag=(*wildstr != wild_many && *wildstr != wild_one);
89
      do
90
      {
91
	if (flag)
92
	{
93
	  char cmp;
94
	  if ((cmp= *wildstr) == wild_prefix && wildstr[1])
95
	    cmp=wildstr[1];
96
	  cmp=my_toupper(cs, cmp);
97
	  while (*str && my_toupper(cs, *str) != cmp)
98
	    str++;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
99
    if (!*str)
100
      return (1);
1 by brian
clean slate
101
	}
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
102
  if (wild_case_compare(cs, str,wildstr) == 0)
103
      return (0);
1 by brian
clean slate
104
      } while (*str++);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
105
      return(1);
1 by brian
clean slate
106
    }
107
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
108
  return (*str != '\0');
1 by brian
clean slate
109
}
110
111
/***************************************************************************
112
** List all table types supported
113
***************************************************************************/
114
520.1.22 by Brian Aker
Second pass of thd cleanup
115
static bool show_plugins(Session *session, plugin_ref plugin,
1 by brian
clean slate
116
                            void *arg)
117
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
118
  Table *table= (Table*) arg;
1 by brian
clean slate
119
  struct st_mysql_plugin *plug= plugin_decl(plugin);
120
  struct st_plugin_dl *plugin_dl= plugin_dlib(plugin);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
121
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
122
123
  restore_record(table, s->default_values);
124
125
  table->field[0]->store(plugin_name(plugin)->str,
126
                         plugin_name(plugin)->length, cs);
127
177.4.3 by mark
ripped out more plugin ABI and API version checking, and plugin versions are now strings
128
  if (plug->version)
129
  {
130
    table->field[1]->store(plug->version, strlen(plug->version), cs);
131
    table->field[1]->set_notnull();
132
  }
133
  else
134
    table->field[1]->set_null();
1 by brian
clean slate
135
136
  switch (plugin_state(plugin)) {
137
  /* case PLUGIN_IS_FREED: does not happen */
138
  case PLUGIN_IS_DELETED:
139
    table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
140
    break;
141
  case PLUGIN_IS_UNINITIALIZED:
142
    table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
143
    break;
144
  case PLUGIN_IS_READY:
145
    table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
146
    break;
147
  default:
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
148
    assert(0);
1 by brian
clean slate
149
  }
150
151
  table->field[3]->store(plugin_type_names[plug->type].str,
152
                         plugin_type_names[plug->type].length,
153
                         cs);
154
155
  if (plugin_dl)
156
  {
177.4.4 by mark
remove now unused columns from SHOW PLUGINS
157
    table->field[4]->store(plugin_dl->dl.str, plugin_dl->dl.length, cs);
158
    table->field[4]->set_notnull();
159
  }
160
  else
161
  {
162
    table->field[4]->set_null();
163
  }
164
165
  if (plug->author)
166
  {
167
    table->field[5]->store(plug->author, strlen(plug->author), cs);
1 by brian
clean slate
168
    table->field[5]->set_notnull();
169
  }
170
  else
171
    table->field[5]->set_null();
172
173
  if (plug->descr)
174
  {
177.4.4 by mark
remove now unused columns from SHOW PLUGINS
175
    table->field[6]->store(plug->descr, strlen(plug->descr), cs);
176
    table->field[6]->set_notnull();
1 by brian
clean slate
177
  }
178
  else
177.4.4 by mark
remove now unused columns from SHOW PLUGINS
179
    table->field[6]->set_null();
1 by brian
clean slate
180
181
  switch (plug->license) {
182
  case PLUGIN_LICENSE_GPL:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
183
    table->field[7]->store(PLUGIN_LICENSE_GPL_STRING,
1 by brian
clean slate
184
                           strlen(PLUGIN_LICENSE_GPL_STRING), cs);
185
    break;
186
  case PLUGIN_LICENSE_BSD:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
187
    table->field[7]->store(PLUGIN_LICENSE_BSD_STRING,
1 by brian
clean slate
188
                           strlen(PLUGIN_LICENSE_BSD_STRING), cs);
189
    break;
190
  default:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
191
    table->field[7]->store(PLUGIN_LICENSE_PROPRIETARY_STRING,
1 by brian
clean slate
192
                           strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
193
    break;
194
  }
177.4.4 by mark
remove now unused columns from SHOW PLUGINS
195
  table->field[7]->set_notnull();
1 by brian
clean slate
196
520.1.22 by Brian Aker
Second pass of thd cleanup
197
  return schema_table_store_record(session, table);
1 by brian
clean slate
198
}
199
200
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
201
int fill_plugins(Session *session, TableList *tables, COND *)
1 by brian
clean slate
202
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
203
  Table *table= tables->table;
1 by brian
clean slate
204
520.1.22 by Brian Aker
Second pass of thd cleanup
205
  if (plugin_foreach_with_mask(session, show_plugins, DRIZZLE_ANY_PLUGIN,
1 by brian
clean slate
206
                               ~PLUGIN_IS_FREED, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
207
    return(1);
1 by brian
clean slate
208
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
209
  return(0);
1 by brian
clean slate
210
}
211
212
213
/*
214
  find_files() - find files in a given directory.
215
216
  SYNOPSIS
217
    find_files()
520.1.22 by Brian Aker
Second pass of thd cleanup
218
    session                 thread handler
1 by brian
clean slate
219
    files               put found files in this list
327.2.4 by Brian Aker
Refactoring table.h
220
    db                  database name to set in TableList structure
1 by brian
clean slate
221
    path                path to database
222
    wild                filter for found files
163 by Brian Aker
Merge Monty's code.
223
    dir                 read databases in path if true, read .frm files in
1 by brian
clean slate
224
                        database otherwise
225
226
  RETURN
227
    FIND_FILES_OK       success
228
    FIND_FILES_OOM      out of memory error
229
    FIND_FILES_DIR      no such directory, or directory can't be read
230
*/
231
232
233
find_files_result
520.1.22 by Brian Aker
Second pass of thd cleanup
234
find_files(Session *session, List<LEX_STRING> *files, const char *db,
1 by brian
clean slate
235
           const char *path, const char *wild, bool dir)
236
{
482 by Brian Aker
Remove uint.
237
  uint32_t i;
1 by brian
clean slate
238
  char *ext;
239
  MY_DIR *dirp;
240
  FILEINFO *file;
241
  LEX_STRING *file_name= 0;
482 by Brian Aker
Remove uint.
242
  uint32_t file_name_len;
327.2.4 by Brian Aker
Refactoring table.h
243
  TableList table_list;
1 by brian
clean slate
244
245
  if (wild && !wild[0])
246
    wild=0;
247
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
248
  memset(&table_list, 0, sizeof(table_list));
1 by brian
clean slate
249
250
  if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0))))
251
  {
252
    if (my_errno == ENOENT)
253
      my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), db);
254
    else
255
      my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), path, my_errno);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
256
    return(FIND_FILES_DIR);
1 by brian
clean slate
257
  }
258
259
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
260
  {
261
    char uname[NAME_LEN + 1];                   /* Unencoded name */
262
    file=dirp->dir_entry+i;
263
    if (dir)
264
    {                                           /* Return databases */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
265
      if ((file->name[0] == '.' &&
1 by brian
clean slate
266
          ((file->name[1] == '.' && file->name[2] == '\0') ||
267
            file->name[1] == '\0')))
268
        continue;                               /* . or .. */
269
#ifdef USE_SYMDIR
270
      char *ext;
271
      char buff[FN_REFLEN];
272
      if (my_use_symdir && !strcmp(ext=fn_ext(file->name), ".sym"))
273
      {
274
	/* Only show the sym file if it points to a directory */
275
	char *end;
276
        *ext=0;                                 /* Remove extension */
277
	unpack_dirname(buff, file->name);
376 by Brian Aker
strend remove
278
	end= strchr(buff, '\0');
1 by brian
clean slate
279
	if (end != buff && end[-1] == FN_LIBCHAR)
280
	  end[-1]= 0;				// Remove end FN_LIBCHAR
15 by brian
Fix for stat, NETWARE removal
281
        if (stat(buff, file->mystat))
1 by brian
clean slate
282
               continue;
283
       }
284
#endif
212.5.37 by Monty Taylor
Removed my_stat.
285
      if (!S_ISDIR(file->mystat->st_mode))
1 by brian
clean slate
286
        continue;
287
288
      file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
289
      if (wild && wild_compare(uname, wild, 0))
290
        continue;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
291
      if (!(file_name=
520.1.22 by Brian Aker
Second pass of thd cleanup
292
            session->make_lex_string(file_name, uname, file_name_len, true)))
1 by brian
clean slate
293
      {
294
        my_dirend(dirp);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
295
        return(FIND_FILES_OOM);
1 by brian
clean slate
296
      }
297
    }
298
    else
299
    {
300
        // Return only .frm files which aren't temp files.
301
      if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),reg_ext) ||
584.2.1 by Stewart Smith
convert tmp_file_prefix to TMP_FILE_PREFIX to indicate it's a macro
302
          is_prefix(file->name, TMP_FILE_PREFIX))
1 by brian
clean slate
303
        continue;
304
      *ext=0;
305
      file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
306
      if (wild)
307
      {
308
	if (lower_case_table_names)
309
	{
310
	  if (wild_case_compare(files_charset_info, uname, wild))
311
	    continue;
312
	}
313
	else if (wild_compare(uname, wild, 0))
314
	  continue;
315
      }
316
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
317
    if (!(file_name=
520.1.22 by Brian Aker
Second pass of thd cleanup
318
          session->make_lex_string(file_name, uname, file_name_len, true)) ||
1 by brian
clean slate
319
        files->push_back(file_name))
320
    {
321
      my_dirend(dirp);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
322
      return(FIND_FILES_OOM);
1 by brian
clean slate
323
    }
324
  }
325
  my_dirend(dirp);
326
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
327
  return(FIND_FILES_OK);
1 by brian
clean slate
328
}
329
330
331
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
332
mysqld_show_create(Session *session, TableList *table_list)
1 by brian
clean slate
333
{
520.1.22 by Brian Aker
Second pass of thd cleanup
334
  Protocol *protocol= session->protocol;
1 by brian
clean slate
335
  char buff[2048];
336
  String buffer(buff, sizeof(buff), system_charset_info);
337
338
  /* Only one table for now, but VIEW can involve several tables */
520.1.22 by Brian Aker
Second pass of thd cleanup
339
  if (open_normal_and_derived_tables(session, table_list, 0))
1 by brian
clean slate
340
  {
737 by Brian Aker
Updates for dead code removal (and forced assert() in delete).
341
    if (session->is_error())
163 by Brian Aker
Merge Monty's code.
342
      return(true);
1 by brian
clean slate
343
344
    /*
345
      Clear all messages with 'error' level status and
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
346
      issue a warning with 'warning' level status in
1 by brian
clean slate
347
      case of invalid view and last error is ER_VIEW_INVALID
348
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
349
    drizzle_reset_errors(session, true);
350
    session->clear_error();
1 by brian
clean slate
351
  }
352
353
  buffer.length(0);
354
520.1.22 by Brian Aker
Second pass of thd cleanup
355
  if (store_create_info(session, table_list, &buffer, NULL))
163 by Brian Aker
Merge Monty's code.
356
    return(true);
1 by brian
clean slate
357
358
  List<Item> field_list;
359
  {
360
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
361
    // 1024 is for not to confuse old clients
362
    field_list.push_back(new Item_empty_string("Create Table",
398.1.4 by Monty Taylor
Renamed max/min.
363
                                               cmax(buffer.length(),(uint32_t)1024)));
1 by brian
clean slate
364
  }
365
366
  if (protocol->send_fields(&field_list,
367
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
163 by Brian Aker
Merge Monty's code.
368
    return(true);
1 by brian
clean slate
369
  protocol->prepare_for_resend();
370
  {
371
    if (table_list->schema_table)
372
      protocol->store(table_list->schema_table->table_name,
373
                      system_charset_info);
374
    else
375
      protocol->store(table_list->table->alias, system_charset_info);
376
  }
377
378
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
379
380
  if (protocol->write())
163 by Brian Aker
Merge Monty's code.
381
    return(true);
1 by brian
clean slate
382
520.1.22 by Brian Aker
Second pass of thd cleanup
383
  my_eof(session);
163 by Brian Aker
Merge Monty's code.
384
  return(false);
1 by brian
clean slate
385
}
386
520.1.22 by Brian Aker
Second pass of thd cleanup
387
bool mysqld_show_create_db(Session *session, char *dbname,
1 by brian
clean slate
388
                           HA_CREATE_INFO *create_info)
389
{
390
  char buff[2048];
391
  String buffer(buff, sizeof(buff), system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
392
  Protocol *protocol=session->protocol;
1 by brian
clean slate
393
520.1.22 by Brian Aker
Second pass of thd cleanup
394
  if (store_db_create_info(session, dbname, &buffer, create_info))
1 by brian
clean slate
395
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
396
    /*
1 by brian
clean slate
397
      This assumes that the only reason for which store_db_create_info()
398
      can fail is incorrect database name (which is the case now).
399
    */
400
    my_error(ER_BAD_DB_ERROR, MYF(0), dbname);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
401
    return(true);
1 by brian
clean slate
402
  }
403
404
  List<Item> field_list;
405
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
406
  field_list.push_back(new Item_empty_string("Create Database",1024));
407
408
  if (protocol->send_fields(&field_list,
409
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
163 by Brian Aker
Merge Monty's code.
410
    return(true);
1 by brian
clean slate
411
412
  protocol->prepare_for_resend();
413
  protocol->store(dbname, strlen(dbname), system_charset_info);
414
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
415
416
  if (protocol->write())
163 by Brian Aker
Merge Monty's code.
417
    return(true);
520.1.22 by Brian Aker
Second pass of thd cleanup
418
  my_eof(session);
163 by Brian Aker
Merge Monty's code.
419
  return(false);
1 by brian
clean slate
420
}
421
422
423
424
/****************************************************************************
425
  Return only fields for API mysql_list_fields
426
  Use "show table wildcard" in mysql instead of this
427
****************************************************************************/
428
429
void
520.1.22 by Brian Aker
Second pass of thd cleanup
430
mysqld_list_fields(Session *session, TableList *table_list, const char *wild)
1 by brian
clean slate
431
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
432
  Table *table;
1 by brian
clean slate
433
520.1.22 by Brian Aker
Second pass of thd cleanup
434
  if (open_normal_and_derived_tables(session, table_list, 0))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
435
    return;
1 by brian
clean slate
436
  table= table_list->table;
437
438
  List<Item> field_list;
439
440
  Field **ptr,*field;
441
  for (ptr=table->field ; (field= *ptr); ptr++)
442
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
443
    if (!wild || !wild[0] ||
1 by brian
clean slate
444
        !wild_case_compare(system_charset_info, field->field_name,wild))
445
    {
446
      field_list.push_back(new Item_field(field));
447
    }
448
  }
449
  restore_record(table, s->default_values);              // Get empty record
450
  table->use_all_columns();
520.1.22 by Brian Aker
Second pass of thd cleanup
451
  if (session->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
452
    return;
520.1.22 by Brian Aker
Second pass of thd cleanup
453
  my_eof(session);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
454
  return;
1 by brian
clean slate
455
}
456
457
458
/*
459
  Go through all character combinations and ensure that sql_lex.cc can
460
  parse it as an identifier.
461
462
  SYNOPSIS
463
  require_quotes()
464
  name			attribute name
465
  name_length		length of name
466
467
  RETURN
468
    #	Pointer to conflicting character
469
    0	No conflicting character
470
*/
471
482 by Brian Aker
Remove uint.
472
static const char *require_quotes(const char *name, uint32_t name_length)
1 by brian
clean slate
473
{
482 by Brian Aker
Remove uint.
474
  uint32_t length;
163 by Brian Aker
Merge Monty's code.
475
  bool pure_digit= true;
1 by brian
clean slate
476
  const char *end= name + name_length;
477
478
  for (; name < end ; name++)
479
  {
481 by Brian Aker
Remove all of uchar.
480
    unsigned char chr= (unsigned char) *name;
1 by brian
clean slate
481
    length= my_mbcharlen(system_charset_info, chr);
482
    if (length == 1 && !system_charset_info->ident_map[chr])
483
      return name;
484
    if (length == 1 && (chr < '0' || chr > '9'))
163 by Brian Aker
Merge Monty's code.
485
      pure_digit= false;
1 by brian
clean slate
486
  }
487
  if (pure_digit)
488
    return name;
489
  return 0;
490
}
491
492
493
/*
494
  Quote the given identifier if needed and append it to the target string.
495
  If the given identifier is empty, it will be quoted.
496
497
  SYNOPSIS
498
  append_identifier()
520.1.22 by Brian Aker
Second pass of thd cleanup
499
  session                   thread handler
1 by brian
clean slate
500
  packet                target string
501
  name                  the identifier to be appended
502
  name_length           length of the appending identifier
503
*/
504
505
void
520.1.22 by Brian Aker
Second pass of thd cleanup
506
append_identifier(Session *session, String *packet, const char *name, uint32_t length)
1 by brian
clean slate
507
{
508
  const char *name_end;
509
  char quote_char;
520.1.22 by Brian Aker
Second pass of thd cleanup
510
  int q= get_quote_char_for_identifier(session, name, length);
1 by brian
clean slate
511
512
  if (q == EOF)
513
  {
514
    packet->append(name, length, packet->charset());
515
    return;
516
  }
517
518
  /*
519
    The identifier must be quoted as it includes a quote character or
520
   it's a keyword
521
  */
522
398.1.6 by Monty Taylor
Removed __alpha__ references.
523
  packet->reserve(length*2 + 2);
1 by brian
clean slate
524
  quote_char= (char) q;
525
  packet->append(&quote_char, 1, system_charset_info);
526
527
  for (name_end= name+length ; name < name_end ; name+= length)
528
  {
481 by Brian Aker
Remove all of uchar.
529
    unsigned char chr= (unsigned char) *name;
1 by brian
clean slate
530
    length= my_mbcharlen(system_charset_info, chr);
531
    /*
532
      my_mbcharlen can return 0 on a wrong multibyte
533
      sequence. It is possible when upgrading from 4.0,
534
      and identifier contains some accented characters.
535
      The manual says it does not work. So we'll just
536
      change length to 1 not to hang in the endless loop.
537
    */
538
    if (!length)
539
      length= 1;
481 by Brian Aker
Remove all of uchar.
540
    if (length == 1 && chr == (unsigned char) quote_char)
1 by brian
clean slate
541
      packet->append(&quote_char, 1, system_charset_info);
542
    packet->append(name, length, system_charset_info);
543
  }
544
  packet->append(&quote_char, 1, system_charset_info);
545
}
546
547
548
/*
549
  Get the quote character for displaying an identifier.
550
551
  SYNOPSIS
552
    get_quote_char_for_identifier()
520.1.22 by Brian Aker
Second pass of thd cleanup
553
    session		Thread handler
1 by brian
clean slate
554
    name	name to quote
555
    length	length of name
556
557
  IMPLEMENTATION
558
    Force quoting in the following cases:
559
      - name is empty (for one, it is possible when we use this function for
560
        quoting user and host names for DEFINER clause);
561
      - name is a keyword;
562
      - name includes a special character;
563
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
564
    is set.
565
566
  RETURN
567
    EOF	  No quote character is needed
568
    #	  Quote character
569
*/
570
520.1.22 by Brian Aker
Second pass of thd cleanup
571
int get_quote_char_for_identifier(Session *session, const char *name, uint32_t length)
1 by brian
clean slate
572
{
573
  if (length &&
574
      !is_keyword(name,length) &&
575
      !require_quotes(name, length) &&
520.1.22 by Brian Aker
Second pass of thd cleanup
576
      !(session->options & OPTION_QUOTE_SHOW_CREATE))
1 by brian
clean slate
577
    return EOF;
352.2.1 by Harrison Fisk
Fix for bugs 259843 and 256482
578
  return '`';
1 by brian
clean slate
579
}
580
581
582
/* Append directory name (if exists) to CREATE INFO */
583
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
584
static void append_directory(Session *,
77.1.46 by Monty Taylor
Finished the warnings work!
585
                             String *packet, const char *dir_type,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
586
                             const char *filename)
1 by brian
clean slate
587
{
588
  if (filename)
589
  {
482 by Brian Aker
Remove uint.
590
    uint32_t length= dirname_length(filename);
1 by brian
clean slate
591
    packet->append(' ');
592
    packet->append(dir_type);
593
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
594
    packet->append(filename, length);
595
    packet->append('\'');
596
  }
597
}
598
599
600
#define LIST_PROCESS_HOST_LEN 64
601
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
602
static bool get_field_default_value(Session *,
77.1.46 by Monty Taylor
Finished the warnings work!
603
                                    Field *timestamp_field,
1 by brian
clean slate
604
                                    Field *field, String *def_value,
605
                                    bool quoted)
606
{
607
  bool has_default;
608
  bool has_now_default;
609
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
610
  /*
1 by brian
clean slate
611
     We are using CURRENT_TIMESTAMP instead of NOW because it is
612
     more standard
613
  */
614
  has_now_default= (timestamp_field == field &&
615
                    field->unireg_check != Field::TIMESTAMP_UN_FIELD);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
616
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
617
  has_default= (field->type() != DRIZZLE_TYPE_BLOB &&
1 by brian
clean slate
618
                !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
619
                field->unireg_check != Field::NEXT_NUMBER
620
                  && has_now_default);
621
622
  def_value->length(0);
623
  if (has_default)
624
  {
625
    if (has_now_default)
626
      def_value->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
627
    else if (!field->is_null())
628
    {                                             // Not null by default
629
      char tmp[MAX_FIELD_WIDTH];
630
      String type(tmp, sizeof(tmp), field->charset());
631
      field->val_str(&type);
632
      if (type.length())
633
      {
634
        String def_val;
482 by Brian Aker
Remove uint.
635
        uint32_t dummy_errors;
1 by brian
clean slate
636
        /* convert to system_charset_info == utf8 */
637
        def_val.copy(type.ptr(), type.length(), field->charset(),
638
                     system_charset_info, &dummy_errors);
639
        if (quoted)
640
          append_unescaped(def_value, def_val.ptr(), def_val.length());
641
        else
642
          def_value->append(def_val.ptr(), def_val.length());
643
      }
644
      else if (quoted)
645
        def_value->append(STRING_WITH_LEN("''"));
646
    }
647
    else if (field->maybe_null() && quoted)
648
      def_value->append(STRING_WITH_LEN("NULL"));    // Null as default
649
    else
650
      return 0;
651
652
  }
653
  return has_default;
654
}
655
656
/*
657
  Build a CREATE TABLE statement for a table.
658
659
  SYNOPSIS
660
    store_create_info()
520.1.22 by Brian Aker
Second pass of thd cleanup
661
    session               The thread
1 by brian
clean slate
662
    table_list        A list containing one table to write statement
663
                      for.
664
    packet            Pointer to a string where statement will be
665
                      written.
666
    create_info_arg   Pointer to create information that can be used
667
                      to tailor the format of the statement.  Can be
668
                      NULL, in which case only SQL_MODE is considered
669
                      when building the statement.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
670
1 by brian
clean slate
671
  NOTE
672
    Currently always return 0, but might return error code in the
673
    future.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
674
1 by brian
clean slate
675
  RETURN
676
    0       OK
677
 */
678
520.1.22 by Brian Aker
Second pass of thd cleanup
679
int store_create_info(Session *session, TableList *table_list, String *packet,
1 by brian
clean slate
680
                      HA_CREATE_INFO *create_info_arg)
681
{
682
  List<Item> field_list;
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
683
  char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
1 by brian
clean slate
684
  const char *alias;
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
685
  string buff;
1 by brian
clean slate
686
  String type(tmp, sizeof(tmp), system_charset_info);
687
  String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
688
  Field **ptr,*field;
482 by Brian Aker
Remove uint.
689
  uint32_t primary_key;
1 by brian
clean slate
690
  KEY *key_info;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
691
  Table *table= table_list->table;
1 by brian
clean slate
692
  handler *file= table->file;
693
  TABLE_SHARE *share= table->s;
694
  HA_CREATE_INFO create_info;
163 by Brian Aker
Merge Monty's code.
695
  bool show_table_options= false;
1 by brian
clean slate
696
  my_bitmap_map *old_map;
697
698
  restore_record(table, s->default_values); // Get empty record
699
700
  if (share->tmp_table)
701
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
702
  else
703
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
704
  if (create_info_arg &&
705
      (create_info_arg->options & HA_LEX_CREATE_IF_NOT_EXISTS))
706
    packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
707
  if (table_list->schema_table)
708
    alias= table_list->schema_table->table_name;
709
  else
710
  {
711
    if (lower_case_table_names == 2)
712
      alias= table->alias;
713
    else
714
    {
715
      alias= share->table_name.str;
716
    }
717
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
718
  append_identifier(session, packet, alias, strlen(alias));
1 by brian
clean slate
719
  packet->append(STRING_WITH_LEN(" (\n"));
720
  /*
721
    We need this to get default values from the table
722
    We have to restore the read_set if we are called from insert in case
723
    of row based replication.
724
  */
354 by Brian Aker
Refactor of Table methods.
725
  old_map= table->use_all_columns(table->read_set);
1 by brian
clean slate
726
727
  for (ptr=table->field ; (field= *ptr); ptr++)
728
  {
482 by Brian Aker
Remove uint.
729
    uint32_t flags = field->flags;
1 by brian
clean slate
730
731
    if (ptr != table->field)
732
      packet->append(STRING_WITH_LEN(",\n"));
733
734
    packet->append(STRING_WITH_LEN("  "));
520.1.22 by Brian Aker
Second pass of thd cleanup
735
    append_identifier(session,packet,field->field_name, strlen(field->field_name));
1 by brian
clean slate
736
    packet->append(' ');
737
    // check for surprises from the previous call to Field::sql_type()
738
    if (type.ptr() != tmp)
739
      type.set(tmp, sizeof(tmp), system_charset_info);
740
    else
741
      type.set_charset(system_charset_info);
742
383.7.1 by Andrey Zhakov
Initial submit of code and tests
743
    if (field->vcol_info)
744
    {
745
      packet->append(STRING_WITH_LEN("VIRTUAL "));
746
    }
747
1 by brian
clean slate
748
    field->sql_type(type);
749
    packet->append(type.ptr(), type.length(), system_charset_info);
750
383.7.1 by Andrey Zhakov
Initial submit of code and tests
751
    if (field->vcol_info)
752
    {
753
      packet->append(STRING_WITH_LEN(" AS ("));
754
      packet->append(field->vcol_info->expr_str.str,
755
                     field->vcol_info->expr_str.length,
756
                     system_charset_info);
757
      packet->append(STRING_WITH_LEN(")"));
758
      if (field->is_stored)
759
        packet->append(STRING_WITH_LEN(" STORED"));
760
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
761
1 by brian
clean slate
762
    if (field->has_charset())
763
    {
764
      if (field->charset() != share->table_charset)
765
      {
766
	packet->append(STRING_WITH_LEN(" CHARACTER SET "));
767
	packet->append(field->charset()->csname);
768
      }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
769
      /*
770
	For string types dump collation name only if
1 by brian
clean slate
771
	collation is not primary for the given charset
772
      */
773
      if (!(field->charset()->state & MY_CS_PRIMARY))
774
      {
775
	packet->append(STRING_WITH_LEN(" COLLATE "));
776
	packet->append(field->charset()->name);
777
      }
778
    }
779
780
    if (flags & NOT_NULL_FLAG)
781
      packet->append(STRING_WITH_LEN(" NOT NULL"));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
782
    else if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
783
    {
784
      /*
785
        TIMESTAMP field require explicit NULL flag, because unlike
786
        all other fields they are treated as NOT NULL by default.
787
      */
788
      packet->append(STRING_WITH_LEN(" NULL"));
789
    }
790
    {
791
      /*
792
        Add field flags about FIELD FORMAT (FIXED or DYNAMIC)
793
        and about STORAGE (DISK or MEMORY).
794
      */
795
      enum column_format_type column_format= (enum column_format_type)
796
        ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
797
      if (column_format)
798
      {
799
        packet->append(STRING_WITH_LEN(" /*!"));
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
800
        packet->append(STRING_WITH_LEN(DRIZZLE_VERSION_TABLESPACE_IN_FRM_STR));
1 by brian
clean slate
801
        packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
802
        if (column_format == COLUMN_FORMAT_TYPE_FIXED)
803
          packet->append(STRING_WITH_LEN(" FIXED */"));
804
        else
805
          packet->append(STRING_WITH_LEN(" DYNAMIC */"));
806
      }
807
    }
383.7.1 by Andrey Zhakov
Initial submit of code and tests
808
    if (!field->vcol_info &&
520.1.22 by Brian Aker
Second pass of thd cleanup
809
        get_field_default_value(session, table->timestamp_field,
1 by brian
clean slate
810
                                field, &def_value, 1))
811
    {
812
      packet->append(STRING_WITH_LEN(" DEFAULT "));
813
      packet->append(def_value.ptr(), def_value.length(), system_charset_info);
814
    }
815
816
    if (table->timestamp_field == field && field->unireg_check != Field::TIMESTAMP_DN_FIELD)
817
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
818
819
    if (field->unireg_check == Field::NEXT_NUMBER)
820
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
821
822
    if (field->comment.length)
823
    {
824
      packet->append(STRING_WITH_LEN(" COMMENT "));
825
      append_unescaped(packet, field->comment.str, field->comment.length);
826
    }
827
  }
828
829
  key_info= table->key_info;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
830
  memset(&create_info, 0, sizeof(create_info));
1 by brian
clean slate
831
  /* Allow update_create_info to update row type */
832
  create_info.row_type= share->row_type;
833
  file->update_create_info(&create_info);
834
  primary_key= share->primary_key;
835
482 by Brian Aker
Remove uint.
836
  for (uint32_t i=0 ; i < share->keys ; i++,key_info++)
1 by brian
clean slate
837
  {
838
    KEY_PART_INFO *key_part= key_info->key_part;
839
    bool found_primary=0;
840
    packet->append(STRING_WITH_LEN(",\n  "));
841
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
842
    if (i == primary_key && is_primary_key(key_info))
1 by brian
clean slate
843
    {
844
      found_primary=1;
845
      /*
846
        No space at end, because a space will be added after where the
847
        identifier would go, but that is not added for primary key.
848
      */
849
      packet->append(STRING_WITH_LEN("PRIMARY KEY"));
850
    }
851
    else if (key_info->flags & HA_NOSAME)
852
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
853
    else
854
      packet->append(STRING_WITH_LEN("KEY "));
855
856
    if (!found_primary)
520.1.22 by Brian Aker
Second pass of thd cleanup
857
     append_identifier(session, packet, key_info->name, strlen(key_info->name));
1 by brian
clean slate
858
859
    packet->append(STRING_WITH_LEN(" ("));
860
482 by Brian Aker
Remove uint.
861
    for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
1 by brian
clean slate
862
    {
863
      if (j)
864
        packet->append(',');
865
866
      if (key_part->field)
520.1.22 by Brian Aker
Second pass of thd cleanup
867
        append_identifier(session,packet,key_part->field->field_name,
1 by brian
clean slate
868
			  strlen(key_part->field->field_name));
869
      if (key_part->field &&
870
          (key_part->length !=
871
           table->field[key_part->fieldnr-1]->key_length()))
872
      {
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
873
        buff= "(";
874
        buff= to_string(buff, (int32_t) key_part->length /
875
                              key_part->field->charset()->mbmaxlen);
876
        buff += ")";
877
        packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
878
      }
879
    }
880
    packet->append(')');
520.1.22 by Brian Aker
Second pass of thd cleanup
881
    store_key_options(session, packet, table, key_info);
1 by brian
clean slate
882
  }
883
884
  /*
885
    Get possible foreign key definitions stored in InnoDB and append them
886
    to the CREATE TABLE statement
887
  */
888
889
  if ((for_str= file->get_foreign_key_create_info()))
890
  {
891
    packet->append(for_str, strlen(for_str));
892
    file->free_foreign_key_create_info(for_str);
893
  }
894
895
  packet->append(STRING_WITH_LEN("\n)"));
896
  {
163 by Brian Aker
Merge Monty's code.
897
    show_table_options= true;
1 by brian
clean slate
898
    /*
899
      Get possible table space definitions and append them
900
      to the CREATE TABLE statement
901
    */
902
903
    /*
904
      IF   check_create_info
905
      THEN add ENGINE only if it was used when creating the table
906
    */
907
    if (!create_info_arg ||
908
        (create_info_arg->used_fields & HA_CREATE_USED_ENGINE))
909
    {
910
      packet->append(STRING_WITH_LEN(" ENGINE="));
911
      packet->append(file->table_type());
912
    }
913
914
    /*
915
      Add AUTO_INCREMENT=... if there is an AUTO_INCREMENT column,
916
      and NEXT_ID > 1 (the default).  We must not print the clause
917
      for engines that do not support this as it would break the
918
      import of dumps, but as of this writing, the test for whether
919
      AUTO_INCREMENT columns are allowed and wether AUTO_INCREMENT=...
920
      is supported is identical, !(file->table_flags() & HA_NO_AUTO_INCREMENT))
921
      Because of that, we do not explicitly test for the feature,
922
      but may extrapolate its existence from that of an AUTO_INCREMENT column.
923
    */
924
925
    if (create_info.auto_increment_value > 1)
926
    {
927
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
928
      buff= to_string(create_info.auto_increment_value);
929
      packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
930
    }
931
932
    if (share->min_rows)
933
    {
934
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
935
      buff= to_string(share->min_rows);
936
      packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
937
    }
938
939
    if (share->max_rows && !table_list->schema_table)
940
    {
941
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
942
      buff= to_string(share->max_rows);
943
      packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
944
    }
945
946
    if (share->avg_row_length)
947
    {
948
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
949
      buff= to_string(share->avg_row_length);
950
      packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
951
    }
952
953
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
954
      packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
955
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
956
      packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
957
    /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */
958
    if (share->db_create_options & HA_OPTION_CHECKSUM)
959
      packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
960
    if (share->page_checksum != HA_CHOICE_UNDEF)
961
    {
962
      packet->append(STRING_WITH_LEN(" PAGE_CHECKSUM="));
963
      packet->append(ha_choice_values[(uint) share->page_checksum], 1);
964
    }
965
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
966
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
967
    if (create_info.row_type != ROW_TYPE_DEFAULT)
968
    {
969
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
970
      packet->append(ha_row_type[(uint) create_info.row_type]);
971
    }
972
    if (share->transactional != HA_CHOICE_UNDEF)
973
    {
974
      packet->append(STRING_WITH_LEN(" TRANSACTIONAL="));
975
      packet->append(ha_choice_values[(uint) share->transactional], 1);
976
    }
977
    if (table->s->key_block_size)
978
    {
979
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
980
      buff= to_string(table->s->key_block_size);
981
      packet->append(buff.c_str(), buff.length());
1 by brian
clean slate
982
    }
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
983
    if (share->block_size)
984
    {
985
      packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
986
      buff= to_string(share->block_size);
987
      packet->append(buff.c_str(), buff.length());
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
988
    }
1 by brian
clean slate
989
    table->file->append_create_info(packet);
990
    if (share->comment.length)
991
    {
992
      packet->append(STRING_WITH_LEN(" COMMENT="));
993
      append_unescaped(packet, share->comment.str, share->comment.length);
994
    }
995
    if (share->connect_string.length)
996
    {
997
      packet->append(STRING_WITH_LEN(" CONNECTION="));
998
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
999
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1000
    append_directory(session, packet, "DATA",  create_info.data_file_name);
1001
    append_directory(session, packet, "INDEX", create_info.index_file_name);
1 by brian
clean slate
1002
  }
354 by Brian Aker
Refactor of Table methods.
1003
  table->restore_column_map(old_map);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1004
  return(0);
1 by brian
clean slate
1005
}
1006
1007
/**
1008
  Get a CREATE statement for a given database.
1009
1010
  The database is identified by its name, passed as @c dbname parameter.
1011
  The name should be encoded using the system character set (UTF8 currently).
1012
1013
  Resulting statement is stored in the string pointed by @c buffer. The string
1014
  is emptied first and its character set is set to the system character set.
1015
1016
  If HA_LEX_CREATE_IF_NOT_EXISTS flag is set in @c create_info->options, then
1017
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
1018
  in @c create_options are ignored.
1019
520.1.22 by Brian Aker
Second pass of thd cleanup
1020
  @param  session           The current thread instance.
1 by brian
clean slate
1021
  @param  dbname        The name of the database.
1022
  @param  buffer        A String instance where the statement is stored.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1023
  @param  create_info   If not NULL, the options member influences the resulting
1 by brian
clean slate
1024
                        CRATE statement.
1025
163 by Brian Aker
Merge Monty's code.
1026
  @returns true if errors are detected, false otherwise.
1 by brian
clean slate
1027
*/
1028
520.1.22 by Brian Aker
Second pass of thd cleanup
1029
bool store_db_create_info(Session *session, const char *dbname, String *buffer,
1 by brian
clean slate
1030
                          HA_CREATE_INFO *create_info)
1031
{
1032
  HA_CREATE_INFO create;
482 by Brian Aker
Remove uint.
1033
  uint32_t create_options = create_info ? create_info->options : 0;
1 by brian
clean slate
1034
1035
  if (!my_strcasecmp(system_charset_info, dbname,
575.4.7 by Monty Taylor
More header cleanup.
1036
                     INFORMATION_SCHEMA_NAME.c_str()))
1 by brian
clean slate
1037
  {
575.4.7 by Monty Taylor
More header cleanup.
1038
    dbname= INFORMATION_SCHEMA_NAME.c_str();
1 by brian
clean slate
1039
    create.default_table_charset= system_charset_info;
1040
  }
1041
  else
1042
  {
1043
    if (check_db_dir_existence(dbname))
163 by Brian Aker
Merge Monty's code.
1044
      return(true);
1 by brian
clean slate
1045
520.1.22 by Brian Aker
Second pass of thd cleanup
1046
    load_db_opt_by_name(session, dbname, &create);
1 by brian
clean slate
1047
  }
1048
1049
  buffer->length(0);
1050
  buffer->free();
1051
  buffer->set_charset(system_charset_info);
1052
  buffer->append(STRING_WITH_LEN("CREATE DATABASE "));
1053
1054
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
1055
    buffer->append(STRING_WITH_LEN("/*!32312 IF NOT EXISTS*/ "));
1056
520.1.22 by Brian Aker
Second pass of thd cleanup
1057
  append_identifier(session, buffer, dbname, strlen(dbname));
1 by brian
clean slate
1058
1059
  if (create.default_table_charset)
1060
  {
1061
    buffer->append(STRING_WITH_LEN(" /*!40100"));
1062
    buffer->append(STRING_WITH_LEN(" DEFAULT CHARACTER SET "));
1063
    buffer->append(create.default_table_charset->csname);
1064
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
1065
    {
1066
      buffer->append(STRING_WITH_LEN(" COLLATE "));
1067
      buffer->append(create.default_table_charset->name);
1068
    }
1069
    buffer->append(STRING_WITH_LEN(" */"));
1070
  }
1071
163 by Brian Aker
Merge Monty's code.
1072
  return(false);
1 by brian
clean slate
1073
}
1074
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1075
static void store_key_options(Session *,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1076
                              String *packet, Table *table,
1 by brian
clean slate
1077
                              KEY *key_info)
1078
{
1079
  char *end, buff[32];
1080
1081
  if (key_info->algorithm == HA_KEY_ALG_BTREE)
1082
    packet->append(STRING_WITH_LEN(" USING BTREE"));
1083
1084
  if (key_info->algorithm == HA_KEY_ALG_HASH)
1085
    packet->append(STRING_WITH_LEN(" USING HASH"));
1086
1087
  if ((key_info->flags & HA_USES_BLOCK_SIZE) &&
1088
      table->s->key_block_size != key_info->block_size)
1089
  {
1090
    packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
152 by Brian Aker
longlong replacement
1091
    end= int64_t10_to_str(key_info->block_size, buff, 10);
1 by brian
clean slate
1092
    packet->append(buff, (uint) (end - buff));
1093
  }
1094
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1095
  assert(test(key_info->flags & HA_USES_COMMENT) ==
1 by brian
clean slate
1096
              (key_info->comment.length > 0));
1097
  if (key_info->flags & HA_USES_COMMENT)
1098
  {
1099
    packet->append(STRING_WITH_LEN(" COMMENT "));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1100
    append_unescaped(packet, key_info->comment.str,
1 by brian
clean slate
1101
                     key_info->comment.length);
1102
  }
1103
}
1104
1105
1106
/****************************************************************************
1107
  Return info about all processes
1108
  returns for each thread: thread id, user, host, db, command, info
1109
****************************************************************************/
1110
1111
class thread_info :public ilink {
1112
public:
1113
  static void *operator new(size_t size)
1114
  {
1115
    return (void*) sql_alloc((uint) size);
1116
  }
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1117
  static void operator delete(void *, size_t)
1 by brian
clean slate
1118
  { TRASH(ptr, size); }
1119
1120
  ulong thread_id;
1121
  time_t start_time;
482 by Brian Aker
Remove uint.
1122
  uint32_t   command;
1 by brian
clean slate
1123
  const char *user,*host,*db,*proc_info,*state_info;
1124
  char *query;
1125
};
1126
1127
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
1128
template class I_List<thread_info>;
1129
#endif
1130
520.1.22 by Brian Aker
Second pass of thd cleanup
1131
void mysqld_list_processes(Session *session,const char *user, bool verbose)
1 by brian
clean slate
1132
{
1133
  Item *field;
1134
  List<Item> field_list;
1135
  I_List<thread_info> thread_infos;
520.1.22 by Brian Aker
Second pass of thd cleanup
1136
  ulong max_query_length= (verbose ? session->variables.max_allowed_packet :
1 by brian
clean slate
1137
			   PROCESS_LIST_WIDTH);
520.1.22 by Brian Aker
Second pass of thd cleanup
1138
  Protocol *protocol= session->protocol;
1 by brian
clean slate
1139
1140
  field_list.push_back(new Item_int("Id", 0, MY_INT32_NUM_DECIMAL_DIGITS));
1141
  field_list.push_back(new Item_empty_string("User",16));
1142
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
1143
  field_list.push_back(field=new Item_empty_string("db",NAME_CHAR_LEN));
1144
  field->maybe_null=1;
1145
  field_list.push_back(new Item_empty_string("Command",16));
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1146
  field_list.push_back(new Item_return_int("Time",7, DRIZZLE_TYPE_LONG));
1 by brian
clean slate
1147
  field_list.push_back(field=new Item_empty_string("State",30));
1148
  field->maybe_null=1;
1149
  field_list.push_back(field=new Item_empty_string("Info",max_query_length));
1150
  field->maybe_null=1;
1151
  if (protocol->send_fields(&field_list,
1152
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1153
    return;
1 by brian
clean slate
1154
398.1.6 by Monty Taylor
Removed __alpha__ references.
1155
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
520.1.22 by Brian Aker
Second pass of thd cleanup
1156
  if (!session->killed)
1 by brian
clean slate
1157
  {
520.1.21 by Brian Aker
THD -> Session rename
1158
    I_List_iterator<Session> it(threads);
1159
    Session *tmp;
1 by brian
clean slate
1160
    while ((tmp=it++))
1161
    {
694 by Brian Aker
Refactor out char* strdup for string class in user.
1162
      Security_context *tmp_sctx= &tmp->security_ctx;
1 by brian
clean slate
1163
      struct st_my_thread_var *mysys_var;
694 by Brian Aker
Refactor out char* strdup for string class in user.
1164
      if ((tmp->vio_ok() || tmp->system_thread) && (!user || (tmp_sctx->user.c_str() && !strcmp(tmp_sctx->user.c_str(), user))))
1 by brian
clean slate
1165
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1166
        thread_info *session_info= new thread_info;
1 by brian
clean slate
1167
520.1.22 by Brian Aker
Second pass of thd cleanup
1168
        session_info->thread_id=tmp->thread_id;
694 by Brian Aker
Refactor out char* strdup for string class in user.
1169
        session_info->user= session->strdup(tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() :
1 by brian
clean slate
1170
                                    (tmp->system_thread ?
1171
                                     "system user" : "unauthenticated user"));
694 by Brian Aker
Refactor out char* strdup for string class in user.
1172
        session_info->host= session->strdup(tmp_sctx->ip.c_str());
520.1.22 by Brian Aker
Second pass of thd cleanup
1173
        if ((session_info->db=tmp->db))             // Safe test
1174
          session_info->db=session->strdup(session_info->db);
1175
        session_info->command=(int) tmp->command;
1 by brian
clean slate
1176
        if ((mysys_var= tmp->mysys_var))
1177
          pthread_mutex_lock(&mysys_var->mutex);
520.1.22 by Brian Aker
Second pass of thd cleanup
1178
        session_info->proc_info= (char*) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0);
1179
        session_info->state_info= (char*) (tmp->net.reading_or_writing ?
1 by brian
clean slate
1180
                                       (tmp->net.reading_or_writing == 2 ?
1181
                                        "Writing to net" :
520.1.22 by Brian Aker
Second pass of thd cleanup
1182
                                        session_info->command == COM_SLEEP ? NULL :
1 by brian
clean slate
1183
                                        "Reading from net") :
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1184
                                       tmp->get_proc_info() ? tmp->get_proc_info() :
1 by brian
clean slate
1185
                                       tmp->mysys_var &&
1186
                                       tmp->mysys_var->current_cond ?
461 by Monty Taylor
Removed NullS. bu-bye.
1187
                                       "Waiting on cond" : NULL);
1 by brian
clean slate
1188
        if (mysys_var)
1189
          pthread_mutex_unlock(&mysys_var->mutex);
1190
520.1.22 by Brian Aker
Second pass of thd cleanup
1191
        session_info->start_time= tmp->start_time;
1192
        session_info->query=0;
1 by brian
clean slate
1193
        if (tmp->query)
1194
        {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1195
	  /*
1 by brian
clean slate
1196
            query_length is always set to 0 when we set query = NULL; see
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
1197
	          the comment in session.h why this prevents crashes in possible
1 by brian
clean slate
1198
            races with query_length
1199
          */
482 by Brian Aker
Remove uint.
1200
          uint32_t length= cmin((uint32_t)max_query_length, tmp->query_length);
520.1.22 by Brian Aker
Second pass of thd cleanup
1201
          session_info->query=(char*) session->strmake(tmp->query,length);
1 by brian
clean slate
1202
        }
520.1.22 by Brian Aker
Second pass of thd cleanup
1203
        thread_infos.append(session_info);
1 by brian
clean slate
1204
      }
1205
    }
1206
  }
398.1.6 by Monty Taylor
Removed __alpha__ references.
1207
  pthread_mutex_unlock(&LOCK_thread_count);
1 by brian
clean slate
1208
520.1.22 by Brian Aker
Second pass of thd cleanup
1209
  thread_info *session_info;
685.3.4 by Toru Maesaka
Fixed the issues pointed out by Jay's code review
1210
  time_t now= time(NULL);
520.1.22 by Brian Aker
Second pass of thd cleanup
1211
  while ((session_info=thread_infos.get()))
1 by brian
clean slate
1212
  {
1213
    protocol->prepare_for_resend();
520.1.22 by Brian Aker
Second pass of thd cleanup
1214
    protocol->store((uint64_t) session_info->thread_id);
1215
    protocol->store(session_info->user, system_charset_info);
1216
    protocol->store(session_info->host, system_charset_info);
1217
    protocol->store(session_info->db, system_charset_info);
1218
    if (session_info->proc_info)
1219
      protocol->store(session_info->proc_info, system_charset_info);
1 by brian
clean slate
1220
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
1221
      protocol->store(command_name[session_info->command].str, system_charset_info);
1222
    if (session_info->start_time)
1223
      protocol->store((uint32_t) (now - session_info->start_time));
1 by brian
clean slate
1224
    else
1225
      protocol->store_null();
520.1.22 by Brian Aker
Second pass of thd cleanup
1226
    protocol->store(session_info->state_info, system_charset_info);
1227
    protocol->store(session_info->query, system_charset_info);
1 by brian
clean slate
1228
    if (protocol->write())
1229
      break; /* purecov: inspected */
1230
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1231
  my_eof(session);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1232
  return;
1 by brian
clean slate
1233
}
1234
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1235
int fill_schema_processlist(Session* session, TableList* tables, COND*)
1 by brian
clean slate
1236
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1237
  Table *table= tables->table;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1238
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
1239
  char *user;
685.3.4 by Toru Maesaka
Fixed the issues pointed out by Jay's code review
1240
  time_t now= time(NULL);
685.3.2 by Toru Maesaka
Removed my_time() and added error checking
1241
685.3.4 by Toru Maesaka
Fixed the issues pointed out by Jay's code review
1242
  if (now == (time_t)-1)
685.3.2 by Toru Maesaka
Removed my_time() and added error checking
1243
    return 1;
1 by brian
clean slate
1244
461 by Monty Taylor
Removed NullS. bu-bye.
1245
  user= NULL;
1 by brian
clean slate
1246
398.1.6 by Monty Taylor
Removed __alpha__ references.
1247
  pthread_mutex_lock(&LOCK_thread_count);
1 by brian
clean slate
1248
520.1.22 by Brian Aker
Second pass of thd cleanup
1249
  if (!session->killed)
1 by brian
clean slate
1250
  {
520.1.21 by Brian Aker
THD -> Session rename
1251
    I_List_iterator<Session> it(threads);
1252
    Session* tmp;
1 by brian
clean slate
1253
1254
    while ((tmp= it++))
1255
    {
694 by Brian Aker
Refactor out char* strdup for string class in user.
1256
      Security_context *tmp_sctx= &tmp->security_ctx;
1 by brian
clean slate
1257
      struct st_my_thread_var *mysys_var;
1258
      const char *val;
1259
1260
      if ((!tmp->vio_ok() && !tmp->system_thread))
1261
        continue;
1262
1263
      restore_record(table, s->default_values);
1264
      /* ID */
163 by Brian Aker
Merge Monty's code.
1265
      table->field[0]->store((int64_t) tmp->thread_id, true);
1 by brian
clean slate
1266
      /* USER */
694 by Brian Aker
Refactor out char* strdup for string class in user.
1267
      val= tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() :
1 by brian
clean slate
1268
            (tmp->system_thread ? "system user" : "unauthenticated user");
1269
      table->field[1]->store(val, strlen(val), cs);
1270
      /* HOST */
694 by Brian Aker
Refactor out char* strdup for string class in user.
1271
      table->field[2]->store(tmp_sctx->ip.c_str(), strlen(tmp_sctx->ip.c_str()), cs);
1 by brian
clean slate
1272
      /* DB */
1273
      if (tmp->db)
1274
      {
1275
        table->field[3]->store(tmp->db, strlen(tmp->db), cs);
1276
        table->field[3]->set_notnull();
1277
      }
1278
1279
      if ((mysys_var= tmp->mysys_var))
1280
        pthread_mutex_lock(&mysys_var->mutex);
1281
      /* COMMAND */
520.1.21 by Brian Aker
THD -> Session rename
1282
      if ((val= (char *) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0)))
1 by brian
clean slate
1283
        table->field[4]->store(val, strlen(val), cs);
1284
      else
1285
        table->field[4]->store(command_name[tmp->command].str,
1286
                               command_name[tmp->command].length, cs);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
1287
      /* DRIZZLE_TIME */
205 by Brian Aker
uint32 -> uin32_t
1288
      table->field[5]->store((uint32_t)(tmp->start_time ?
163 by Brian Aker
Merge Monty's code.
1289
                                      now - tmp->start_time : 0), true);
1 by brian
clean slate
1290
      /* STATE */
1291
      val= (char*) (tmp->net.reading_or_writing ?
1292
                    (tmp->net.reading_or_writing == 2 ?
1293
                     "Writing to net" :
461 by Monty Taylor
Removed NullS. bu-bye.
1294
                     tmp->command == COM_SLEEP ? NULL :
1 by brian
clean slate
1295
                     "Reading from net") :
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
1296
                    tmp->get_proc_info() ? tmp->get_proc_info() :
1 by brian
clean slate
1297
                    tmp->mysys_var &&
1298
                    tmp->mysys_var->current_cond ?
461 by Monty Taylor
Removed NullS. bu-bye.
1299
                    "Waiting on cond" : NULL);
1 by brian
clean slate
1300
      if (val)
1301
      {
1302
        table->field[6]->store(val, strlen(val), cs);
1303
        table->field[6]->set_notnull();
1304
      }
1305
1306
      if (mysys_var)
1307
        pthread_mutex_unlock(&mysys_var->mutex);
1308
1309
      /* INFO */
1310
      if (tmp->query)
1311
      {
1312
        table->field[7]->store(tmp->query,
398.1.4 by Monty Taylor
Renamed max/min.
1313
                               cmin((uint32_t)PROCESS_LIST_INFO_WIDTH,
1 by brian
clean slate
1314
                                   tmp->query_length), cs);
1315
        table->field[7]->set_notnull();
1316
      }
1317
520.1.22 by Brian Aker
Second pass of thd cleanup
1318
      if (schema_table_store_record(session, table))
1 by brian
clean slate
1319
      {
398.1.6 by Monty Taylor
Removed __alpha__ references.
1320
        pthread_mutex_unlock(&LOCK_thread_count);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1321
        return(1);
1 by brian
clean slate
1322
      }
1323
    }
1324
  }
1325
398.1.6 by Monty Taylor
Removed __alpha__ references.
1326
  pthread_mutex_unlock(&LOCK_thread_count);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1327
  return(0);
1 by brian
clean slate
1328
}
1329
1330
/*****************************************************************************
1331
  Status functions
1332
*****************************************************************************/
1333
1334
static DYNAMIC_ARRAY all_status_vars;
1335
static bool status_vars_inited= 0;
1336
static int show_var_cmp(const void *var1, const void *var2)
1337
{
1338
  return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
1339
}
1340
1341
/*
1342
  deletes all the SHOW_UNDEF elements from the array and calls
1343
  delete_dynamic() if it's completely empty.
1344
*/
1345
static void shrink_var_array(DYNAMIC_ARRAY *array)
1346
{
482 by Brian Aker
Remove uint.
1347
  uint32_t a,b;
1 by brian
clean slate
1348
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);
1349
1350
  for (a= b= 0; b < array->elements; b++)
1351
    if (all[b].type != SHOW_UNDEF)
1352
      all[a++]= all[b];
1353
  if (a)
1354
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1355
    memset(all+a, 0, sizeof(SHOW_VAR)); // writing NULL-element to the end
1 by brian
clean slate
1356
    array->elements= a;
1357
  }
1358
  else // array is completely empty - delete it
1359
    delete_dynamic(array);
1360
}
1361
1362
/*
1363
  Adds an array of SHOW_VAR entries to the output of SHOW STATUS
1364
1365
  SYNOPSIS
1366
    add_status_vars(SHOW_VAR *list)
1367
    list - an array of SHOW_VAR entries to add to all_status_vars
1368
           the last entry must be {0,0,SHOW_UNDEF}
1369
1370
  NOTE
1371
    The handling of all_status_vars[] is completely internal, it's allocated
1372
    automatically when something is added to it, and deleted completely when
1373
    the last entry is removed.
1374
1375
    As a special optimization, if add_status_vars() is called before
1376
    init_status_vars(), it assumes "startup mode" - neither concurrent access
1377
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
1378
1379
    The last entry of the all_status_vars[] should always be {0,0,SHOW_UNDEF}
1380
*/
1381
int add_status_vars(SHOW_VAR *list)
1382
{
1383
  int res= 0;
1384
  if (status_vars_inited)
1385
    pthread_mutex_lock(&LOCK_status);
1386
  if (!all_status_vars.buffer && // array is not allocated yet - do it now
1387
      my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20))
1388
  {
1389
    res= 1;
1390
    goto err;
1391
  }
1392
  while (list->name)
481 by Brian Aker
Remove all of uchar.
1393
    res|= insert_dynamic(&all_status_vars, (unsigned char*)list++);
1394
  res|= insert_dynamic(&all_status_vars, (unsigned char*)list); // appending NULL-element
1 by brian
clean slate
1395
  all_status_vars.elements--; // but next insert_dynamic should overwite it
1396
  if (status_vars_inited)
1397
    sort_dynamic(&all_status_vars, show_var_cmp);
1398
err:
1399
  if (status_vars_inited)
1400
    pthread_mutex_unlock(&LOCK_status);
1401
  return res;
1402
}
1403
1404
/*
1405
  Make all_status_vars[] usable for SHOW STATUS
1406
1407
  NOTE
1408
    See add_status_vars(). Before init_status_vars() call, add_status_vars()
1409
    works in a special fast "startup" mode. Thus init_status_vars()
1410
    should be called as late as possible but before enabling multi-threading.
1411
*/
1412
void init_status_vars()
1413
{
1414
  status_vars_inited=1;
1415
  sort_dynamic(&all_status_vars, show_var_cmp);
1416
}
1417
1418
void reset_status_vars()
1419
{
1420
  SHOW_VAR *ptr= (SHOW_VAR*) all_status_vars.buffer;
1421
  SHOW_VAR *last= ptr + all_status_vars.elements;
1422
  for (; ptr < last; ptr++)
1423
  {
1424
    /* Note that SHOW_LONG_NOFLUSH variables are not reset */
1425
    if (ptr->type == SHOW_LONG)
1426
      *(ulong*) ptr->value= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1427
  }
1 by brian
clean slate
1428
}
1429
1430
/*
1431
  catch-all cleanup function, cleans up everything no matter what
1432
1433
  DESCRIPTION
1434
    This function is not strictly required if all add_to_status/
1435
    remove_status_vars are properly paired, but it's a safety measure that
1436
    deletes everything from the all_status_vars[] even if some
1437
    remove_status_vars were forgotten
1438
*/
1439
void free_status_vars()
1440
{
1441
  delete_dynamic(&all_status_vars);
1442
}
1443
1444
/*
1445
  Removes an array of SHOW_VAR entries from the output of SHOW STATUS
1446
1447
  SYNOPSIS
1448
    remove_status_vars(SHOW_VAR *list)
1449
    list - an array of SHOW_VAR entries to remove to all_status_vars
1450
           the last entry must be {0,0,SHOW_UNDEF}
1451
1452
  NOTE
1453
    there's lots of room for optimizing this, especially in non-sorted mode,
1454
    but nobody cares - it may be called only in case of failed plugin
1455
    initialization in the mysqld startup.
1456
*/
1457
1458
void remove_status_vars(SHOW_VAR *list)
1459
{
1460
  if (status_vars_inited)
1461
  {
1462
    pthread_mutex_lock(&LOCK_status);
1463
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1464
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
1465
1466
    for (; list->name; list++)
1467
    {
1468
      int res= 0;
1469
      for (a= 0, b= all_status_vars.elements; b-a > 1; c= (a+b)/2)
1470
      {
1471
        res= show_var_cmp(list, all+c);
1472
        if (res < 0)
1473
          b= c;
1474
        else if (res > 0)
1475
          a= c;
1476
        else
1477
          break;
1478
      }
1479
      if (res == 0)
1480
        all[c].type= SHOW_UNDEF;
1481
    }
1482
    shrink_var_array(&all_status_vars);
1483
    pthread_mutex_unlock(&LOCK_status);
1484
  }
1485
  else
1486
  {
1487
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
482 by Brian Aker
Remove uint.
1488
    uint32_t i;
1 by brian
clean slate
1489
    for (; list->name; list++)
1490
    {
1491
      for (i= 0; i < all_status_vars.elements; i++)
1492
      {
1493
        if (show_var_cmp(list, all+i))
1494
          continue;
1495
        all[i].type= SHOW_UNDEF;
1496
        break;
1497
      }
1498
    }
1499
    shrink_var_array(&all_status_vars);
1500
  }
1501
}
1502
1503
inline void make_upper(char *buf)
1504
{
1505
  for (; *buf; buf++)
1506
    *buf= my_toupper(system_charset_info, *buf);
1507
}
1508
520.1.22 by Brian Aker
Second pass of thd cleanup
1509
static bool show_status_array(Session *session, const char *wild,
1 by brian
clean slate
1510
                              SHOW_VAR *variables,
1511
                              enum enum_var_type value_type,
1512
                              struct system_status_var *status_var,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1513
                              const char *prefix, Table *table,
1 by brian
clean slate
1514
                              bool ucase_names)
1515
{
673.4.1 by Stewart Smith
fix show variables alignment problem (SIGBUS on linux sparc)
1516
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1 by brian
clean slate
1517
  char * const buff= (char *) &buff_data;
1518
  char *prefix_end;
1519
  /* the variable name should not be longer than 64 characters */
1520
  char name_buffer[64];
1521
  int len;
1522
  LEX_STRING null_lex_str;
1523
  SHOW_VAR tmp, *var;
1524
1525
  null_lex_str.str= 0;				// For sys_var->value_ptr()
1526
  null_lex_str.length= 0;
1527
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
1528
  prefix_end= strncpy(name_buffer, prefix, sizeof(name_buffer)-1);
1529
  prefix_end+= strlen(prefix);
1530
1 by brian
clean slate
1531
  if (*prefix)
1532
    *prefix_end++= '_';
1533
  len=name_buffer + sizeof(name_buffer) - prefix_end;
1534
1535
  for (; variables->name; variables++)
1536
  {
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
1537
    strncpy(prefix_end, variables->name, len);
1 by brian
clean slate
1538
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
1539
    if (ucase_names)
1540
      make_upper(name_buffer);
1541
1542
    /*
1543
      if var->type is SHOW_FUNC, call the function.
1544
      Repeat as necessary, if new var is again SHOW_FUNC
1545
    */
1546
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
520.1.22 by Brian Aker
Second pass of thd cleanup
1547
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(session, &tmp, buff);
1 by brian
clean slate
1548
1549
    SHOW_TYPE show_type=var->type;
1550
    if (show_type == SHOW_ARRAY)
1551
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1552
      show_status_array(session, wild, (SHOW_VAR *) var->value, value_type,
1 by brian
clean slate
1553
                        status_var, name_buffer, table, ucase_names);
1554
    }
1555
    else
1556
    {
1557
      if (!(wild && wild[0] && wild_case_compare(system_charset_info,
1558
                                                 name_buffer, wild)))
1559
      {
1560
        char *value=var->value;
1561
        const char *pos, *end;                  // We assign a lot of const's
1562
        pthread_mutex_lock(&LOCK_global_system_variables);
1563
1564
        if (show_type == SHOW_SYS)
1565
        {
1566
          show_type= ((sys_var*) value)->show_type();
520.1.22 by Brian Aker
Second pass of thd cleanup
1567
          value=     (char*) ((sys_var*) value)->value_ptr(session, value_type,
1 by brian
clean slate
1568
                                                           &null_lex_str);
1569
        }
1570
1571
        pos= end= buff;
1572
        /*
1573
          note that value may be == buff. All SHOW_xxx code below
1574
          should still work in this case
1575
        */
1576
        switch (show_type) {
1577
        case SHOW_DOUBLE_STATUS:
1578
          value= ((char *) status_var + (ulong) value);
1579
          /* fall through */
1580
        case SHOW_DOUBLE:
1581
          /* 6 is the default precision for '%f' in sprintf() */
1582
          end= buff + my_fcvt(*(double *) value, 6, buff, NULL);
1583
          break;
1584
        case SHOW_LONG_STATUS:
1585
          value= ((char *) status_var + (ulong) value);
1586
          /* fall through */
1587
        case SHOW_LONG:
1588
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
1589
          end= int10_to_str(*(long*) value, buff, 10);
1590
          break;
1591
        case SHOW_LONGLONG_STATUS:
151 by Brian Aker
Ulonglong to uint64_t
1592
          value= ((char *) status_var + (uint64_t) value);
1 by brian
clean slate
1593
          /* fall through */
1594
        case SHOW_LONGLONG:
152 by Brian Aker
longlong replacement
1595
          end= int64_t10_to_str(*(int64_t*) value, buff, 10);
1 by brian
clean slate
1596
          break;
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
1597
        case SHOW_SIZE:
1598
          {
1599
            stringstream ss (stringstream::in);
1600
            ss << *(size_t*) value;
1601
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1602
            string str= ss.str();
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
1603
            strncpy(buff, str.c_str(), str.length());
1604
            end= buff+ str.length();
1605
          }
1606
          break;
1 by brian
clean slate
1607
        case SHOW_HA_ROWS:
152 by Brian Aker
longlong replacement
1608
          end= int64_t10_to_str((int64_t) *(ha_rows*) value, buff, 10);
1 by brian
clean slate
1609
          break;
1610
        case SHOW_BOOL:
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
1611
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
1 by brian
clean slate
1612
          break;
1613
        case SHOW_MY_BOOL:
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
1614
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
1 by brian
clean slate
1615
          break;
1616
        case SHOW_INT:
205 by Brian Aker
uint32 -> uin32_t
1617
          end= int10_to_str((long) *(uint32_t*) value, buff, 10);
1 by brian
clean slate
1618
          break;
1619
        case SHOW_HAVE:
1620
        {
1621
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
1622
          pos= show_comp_option_name[(int) tmp];
376 by Brian Aker
strend remove
1623
          end= strchr(pos, '\0');
1 by brian
clean slate
1624
          break;
1625
        }
1626
        case SHOW_CHAR:
1627
        {
1628
          if (!(pos= value))
1629
            pos= "";
376 by Brian Aker
strend remove
1630
          end= strchr(pos, '\0');
1 by brian
clean slate
1631
          break;
1632
        }
1633
       case SHOW_CHAR_PTR:
1634
        {
1635
          if (!(pos= *(char**) value))
1636
            pos= "";
376 by Brian Aker
strend remove
1637
          end= strchr(pos, '\0');
1 by brian
clean slate
1638
          break;
1639
        }
1640
        case SHOW_KEY_CACHE_LONG:
1641
          value= (char*) dflt_key_cache + (ulong)value;
1642
          end= int10_to_str(*(long*) value, buff, 10);
1643
          break;
1644
        case SHOW_KEY_CACHE_LONGLONG:
1645
          value= (char*) dflt_key_cache + (ulong)value;
152 by Brian Aker
longlong replacement
1646
	  end= int64_t10_to_str(*(int64_t*) value, buff, 10);
1 by brian
clean slate
1647
	  break;
1648
        case SHOW_UNDEF:
1649
          break;                                        // Return empty string
1650
        case SHOW_SYS:                                  // Cannot happen
1651
        default:
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1652
          assert(0);
1 by brian
clean slate
1653
          break;
1654
        }
1655
        restore_record(table, s->default_values);
1656
        table->field[0]->store(name_buffer, strlen(name_buffer),
1657
                               system_charset_info);
205 by Brian Aker
uint32 -> uin32_t
1658
        table->field[1]->store(pos, (uint32_t) (end - pos), system_charset_info);
1 by brian
clean slate
1659
        table->field[1]->set_notnull();
1660
1661
        pthread_mutex_unlock(&LOCK_global_system_variables);
1662
520.1.22 by Brian Aker
Second pass of thd cleanup
1663
        if (schema_table_store_record(session, table))
163 by Brian Aker
Merge Monty's code.
1664
          return(true);
1 by brian
clean slate
1665
      }
1666
    }
1667
  }
1668
163 by Brian Aker
Merge Monty's code.
1669
  return(false);
1 by brian
clean slate
1670
}
1671
1672
1673
/* collect status for all running threads */
1674
1675
void calc_sum_of_all_status(STATUS_VAR *to)
1676
{
1677
1678
  /* Ensure that thread id not killed during loop */
398.1.6 by Monty Taylor
Removed __alpha__ references.
1679
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1 by brian
clean slate
1680
520.1.21 by Brian Aker
THD -> Session rename
1681
  I_List_iterator<Session> it(threads);
1682
  Session *tmp;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1683
1 by brian
clean slate
1684
  /* Get global values as base */
1685
  *to= global_status_var;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1686
1 by brian
clean slate
1687
  /* Add to this status from existing threads */
1688
  while ((tmp= it++))
1689
    add_to_status(to, &tmp->status_var);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1690
398.1.6 by Monty Taylor
Removed __alpha__ references.
1691
  pthread_mutex_unlock(&LOCK_thread_count);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
1692
  return;
1 by brian
clean slate
1693
}
1694
1695
1696
/* This is only used internally, but we need it here as a forward reference */
1697
extern ST_SCHEMA_TABLE schema_tables[];
1698
1699
typedef struct st_lookup_field_values
1700
{
1701
  LEX_STRING db_value, table_value;
1702
  bool wild_db_value, wild_table_value;
1703
} LOOKUP_FIELD_VALUES;
1704
1705
1706
/*
1707
  Store record to I_S table, convert HEAP table
1708
  to MyISAM if necessary
1709
1710
  SYNOPSIS
1711
    schema_table_store_record()
520.1.22 by Brian Aker
Second pass of thd cleanup
1712
    session                   thread handler
1 by brian
clean slate
1713
    table                 Information schema table to be updated
1714
1715
  RETURN
1716
    0	                  success
1717
    1	                  error
1718
*/
1719
520.1.22 by Brian Aker
Second pass of thd cleanup
1720
bool schema_table_store_record(Session *session, Table *table)
1 by brian
clean slate
1721
{
1722
  int error;
1723
  if ((error= table->file->ha_write_row(table->record[0])))
1724
  {
1725
    TMP_TABLE_PARAM *param= table->pos_in_table_list->schema_table_param;
1726
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1727
    if (create_myisam_from_heap(session, table, param->start_recinfo,
1 by brian
clean slate
1728
                                &param->recinfo, error, 0))
1729
      return 1;
1730
  }
1731
  return 0;
1732
}
1733
1734
520.1.22 by Brian Aker
Second pass of thd cleanup
1735
int make_table_list(Session *session, SELECT_LEX *sel,
1 by brian
clean slate
1736
                    LEX_STRING *db_name, LEX_STRING *table_name)
1737
{
1738
  Table_ident *table_ident;
520.1.22 by Brian Aker
Second pass of thd cleanup
1739
  table_ident= new Table_ident(session, *db_name, *table_name, 1);
1 by brian
clean slate
1740
  sel->init_query();
520.1.22 by Brian Aker
Second pass of thd cleanup
1741
  if (!sel->add_table_to_list(session, table_ident, 0, 0, TL_READ))
1 by brian
clean slate
1742
    return 1;
1743
  return 0;
1744
}
1745
1746
1747
/**
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1748
  @brief    Get lookup value from the part of 'WHERE' condition
1 by brian
clean slate
1749
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1750
  @details This function gets lookup value from
1751
           the part of 'WHERE' condition if it's possible and
1 by brian
clean slate
1752
           fill appropriate lookup_field_vals struct field
1753
           with this value.
1754
520.1.22 by Brian Aker
Second pass of thd cleanup
1755
  @param[in]      session                   thread handler
1 by brian
clean slate
1756
  @param[in]      item_func             part of WHERE condition
1757
  @param[in]      table                 I_S table
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1758
  @param[in, out] lookup_field_vals     Struct which holds lookup values
1 by brian
clean slate
1759
1760
  @return
1761
    0             success
1762
    1             error, there can be no matching records for the condition
1763
*/
1764
520.1.22 by Brian Aker
Second pass of thd cleanup
1765
bool get_lookup_value(Session *session, Item_func *item_func,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1766
                      TableList *table,
1 by brian
clean slate
1767
                      LOOKUP_FIELD_VALUES *lookup_field_vals)
1768
{
1769
  ST_SCHEMA_TABLE *schema_table= table->schema_table;
1770
  ST_FIELD_INFO *field_info= schema_table->fields_info;
1771
  const char *field_name1= schema_table->idx_field1 >= 0 ?
1772
    field_info[schema_table->idx_field1].field_name : "";
1773
  const char *field_name2= schema_table->idx_field2 >= 0 ?
1774
    field_info[schema_table->idx_field2].field_name : "";
1775
1776
  if (item_func->functype() == Item_func::EQ_FUNC ||
1777
      item_func->functype() == Item_func::EQUAL_FUNC)
1778
  {
1779
    int idx_field, idx_val;
1780
    char tmp[MAX_FIELD_WIDTH];
1781
    String *tmp_str, str_buff(tmp, sizeof(tmp), system_charset_info);
1782
    Item_field *item_field;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1783
    const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
1784
1785
    if (item_func->arguments()[0]->type() == Item::FIELD_ITEM &&
1786
        item_func->arguments()[1]->const_item())
1787
    {
1788
      idx_field= 0;
1789
      idx_val= 1;
1790
    }
1791
    else if (item_func->arguments()[1]->type() == Item::FIELD_ITEM &&
1792
             item_func->arguments()[0]->const_item())
1793
    {
1794
      idx_field= 1;
1795
      idx_val= 0;
1796
    }
1797
    else
1798
      return 0;
1799
1800
    item_field= (Item_field*) item_func->arguments()[idx_field];
1801
    if (table->table != item_field->field->table)
1802
      return 0;
1803
    tmp_str= item_func->arguments()[idx_val]->val_str(&str_buff);
1804
1805
    /* impossible value */
1806
    if (!tmp_str)
1807
      return 1;
1808
1809
    /* Lookup value is database name */
481 by Brian Aker
Remove all of uchar.
1810
    if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1811
                               (unsigned char *) item_field->field_name,
1 by brian
clean slate
1812
                               strlen(item_field->field_name), 0))
1813
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1814
      session->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
163 by Brian Aker
Merge Monty's code.
1815
                           tmp_str->length(), false);
1 by brian
clean slate
1816
    }
1817
    /* Lookup value is table name */
481 by Brian Aker
Remove all of uchar.
1818
    else if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name2,
1 by brian
clean slate
1819
                                    strlen(field_name2),
481 by Brian Aker
Remove all of uchar.
1820
                                    (unsigned char *) item_field->field_name,
1 by brian
clean slate
1821
                                    strlen(item_field->field_name), 0))
1822
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1823
      session->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
163 by Brian Aker
Merge Monty's code.
1824
                           tmp_str->length(), false);
1 by brian
clean slate
1825
    }
1826
  }
1827
  return 0;
1828
}
1829
1830
1831
/**
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1832
  @brief    Calculates lookup values from 'WHERE' condition
1 by brian
clean slate
1833
1834
  @details This function calculates lookup value(database name, table name)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1835
           from 'WHERE' condition if it's possible and
1 by brian
clean slate
1836
           fill lookup_field_vals struct fields with these values.
1837
520.1.22 by Brian Aker
Second pass of thd cleanup
1838
  @param[in]      session                   thread handler
1 by brian
clean slate
1839
  @param[in]      cond                  WHERE condition
1840
  @param[in]      table                 I_S table
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1841
  @param[in, out] lookup_field_vals     Struct which holds lookup values
1 by brian
clean slate
1842
1843
  @return
1844
    0             success
1845
    1             error, there can be no matching records for the condition
1846
*/
1847
520.1.22 by Brian Aker
Second pass of thd cleanup
1848
bool calc_lookup_values_from_cond(Session *session, COND *cond, TableList *table,
1 by brian
clean slate
1849
                                  LOOKUP_FIELD_VALUES *lookup_field_vals)
1850
{
1851
  if (!cond)
1852
    return 0;
1853
1854
  if (cond->type() == Item::COND_ITEM)
1855
  {
1856
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1857
    {
1858
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1859
      Item *item;
1860
      while ((item= li++))
1861
      {
1862
        if (item->type() == Item::FUNC_ITEM)
1863
        {
520.1.22 by Brian Aker
Second pass of thd cleanup
1864
          if (get_lookup_value(session, (Item_func*)item, table, lookup_field_vals))
1 by brian
clean slate
1865
            return 1;
1866
        }
1867
        else
1868
        {
520.1.22 by Brian Aker
Second pass of thd cleanup
1869
          if (calc_lookup_values_from_cond(session, item, table, lookup_field_vals))
1 by brian
clean slate
1870
            return 1;
1871
        }
1872
      }
1873
    }
1874
    return 0;
1875
  }
1876
  else if (cond->type() == Item::FUNC_ITEM &&
520.1.22 by Brian Aker
Second pass of thd cleanup
1877
           get_lookup_value(session, (Item_func*) cond, table, lookup_field_vals))
1 by brian
clean slate
1878
    return 1;
1879
  return 0;
1880
}
1881
1882
327.2.4 by Brian Aker
Refactoring table.h
1883
bool uses_only_table_name_fields(Item *item, TableList *table)
1 by brian
clean slate
1884
{
1885
  if (item->type() == Item::FUNC_ITEM)
1886
  {
1887
    Item_func *item_func= (Item_func*)item;
482 by Brian Aker
Remove uint.
1888
    for (uint32_t i=0; i<item_func->argument_count(); i++)
1 by brian
clean slate
1889
    {
1890
      if (!uses_only_table_name_fields(item_func->arguments()[i], table))
1891
        return 0;
1892
    }
1893
  }
1894
  else if (item->type() == Item::FIELD_ITEM)
1895
  {
1896
    Item_field *item_field= (Item_field*)item;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1897
    const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
1898
    ST_SCHEMA_TABLE *schema_table= table->schema_table;
1899
    ST_FIELD_INFO *field_info= schema_table->fields_info;
1900
    const char *field_name1= schema_table->idx_field1 >= 0 ?
1901
      field_info[schema_table->idx_field1].field_name : "";
1902
    const char *field_name2= schema_table->idx_field2 >= 0 ?
1903
      field_info[schema_table->idx_field2].field_name : "";
1904
    if (table->table != item_field->field->table ||
481 by Brian Aker
Remove all of uchar.
1905
        (cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1906
                               (unsigned char *) item_field->field_name,
1 by brian
clean slate
1907
                               strlen(item_field->field_name), 0) &&
481 by Brian Aker
Remove all of uchar.
1908
         cs->coll->strnncollsp(cs, (unsigned char *) field_name2, strlen(field_name2),
1909
                               (unsigned char *) item_field->field_name,
1 by brian
clean slate
1910
                               strlen(item_field->field_name), 0)))
1911
      return 0;
1912
  }
1913
  else if (item->type() == Item::REF_ITEM)
1914
    return uses_only_table_name_fields(item->real_item(), table);
1915
1916
  if (item->type() == Item::SUBSELECT_ITEM && !item->const_item())
1917
    return 0;
1918
1919
  return 1;
1920
}
1921
1922
327.2.4 by Brian Aker
Refactoring table.h
1923
static COND * make_cond_for_info_schema(COND *cond, TableList *table)
1 by brian
clean slate
1924
{
1925
  if (!cond)
1926
    return (COND*) 0;
1927
  if (cond->type() == Item::COND_ITEM)
1928
  {
1929
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1930
    {
1931
      /* Create new top level AND item */
1932
      Item_cond_and *new_cond=new Item_cond_and;
1933
      if (!new_cond)
1934
	return (COND*) 0;
1935
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1936
      Item *item;
1937
      while ((item=li++))
1938
      {
1939
	Item *fix= make_cond_for_info_schema(item, table);
1940
	if (fix)
1941
	  new_cond->argument_list()->push_back(fix);
1942
      }
1943
      switch (new_cond->argument_list()->elements) {
1944
      case 0:
1945
	return (COND*) 0;
1946
      case 1:
1947
	return new_cond->argument_list()->head();
1948
      default:
1949
	new_cond->quick_fix_field();
1950
	return new_cond;
1951
      }
1952
    }
1953
    else
1954
    {						// Or list
1955
      Item_cond_or *new_cond=new Item_cond_or;
1956
      if (!new_cond)
1957
	return (COND*) 0;
1958
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1959
      Item *item;
1960
      while ((item=li++))
1961
      {
1962
	Item *fix=make_cond_for_info_schema(item, table);
1963
	if (!fix)
1964
	  return (COND*) 0;
1965
	new_cond->argument_list()->push_back(fix);
1966
      }
1967
      new_cond->quick_fix_field();
1968
      new_cond->top_level_item();
1969
      return new_cond;
1970
    }
1971
  }
1972
1973
  if (!uses_only_table_name_fields(cond, table))
1974
    return (COND*) 0;
1975
  return cond;
1976
}
1977
1978
1979
/**
1980
  @brief   Calculate lookup values(database name, table name)
1981
1982
  @details This function calculates lookup values(database name, table name)
1983
           from 'WHERE' condition or wild values (for 'SHOW' commands only)
1984
           from LEX struct and fill lookup_field_vals struct field
1985
           with these values.
1986
520.1.22 by Brian Aker
Second pass of thd cleanup
1987
  @param[in]      session                   thread handler
1 by brian
clean slate
1988
  @param[in]      cond                  WHERE condition
1989
  @param[in]      tables                I_S table
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1990
  @param[in, out] lookup_field_values   Struct which holds lookup values
1 by brian
clean slate
1991
1992
  @return
1993
    0             success
1994
    1             error, there can be no matching records for the condition
1995
*/
1996
520.1.22 by Brian Aker
Second pass of thd cleanup
1997
bool get_lookup_field_values(Session *session, COND *cond, TableList *tables,
1 by brian
clean slate
1998
                             LOOKUP_FIELD_VALUES *lookup_field_values)
1999
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2000
  LEX *lex= session->lex;
461 by Monty Taylor
Removed NullS. bu-bye.
2001
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
2002
  memset(lookup_field_values, 0, sizeof(LOOKUP_FIELD_VALUES));
1 by brian
clean slate
2003
  switch (lex->sql_command) {
2004
  case SQLCOM_SHOW_DATABASES:
2005
    if (wild)
2006
    {
2007
      lookup_field_values->db_value.str= (char*) wild;
2008
      lookup_field_values->db_value.length= strlen(wild);
2009
      lookup_field_values->wild_db_value= 1;
2010
    }
2011
    return 0;
2012
  case SQLCOM_SHOW_TABLES:
2013
  case SQLCOM_SHOW_TABLE_STATUS:
2014
    lookup_field_values->db_value.str= lex->select_lex.db;
2015
    lookup_field_values->db_value.length=strlen(lex->select_lex.db);
2016
    if (wild)
2017
    {
2018
      lookup_field_values->table_value.str= (char*)wild;
2019
      lookup_field_values->table_value.length= strlen(wild);
2020
      lookup_field_values->wild_table_value= 1;
2021
    }
2022
    return 0;
2023
  default:
2024
    /*
2025
      The "default" is for queries over I_S.
2026
      All previous cases handle SHOW commands.
2027
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
2028
    return calc_lookup_values_from_cond(session, cond, tables, lookup_field_values);
1 by brian
clean slate
2029
  }
2030
}
2031
2032
2033
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
2034
{
2035
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
2036
}
2037
2038
2039
/*
2040
  Create db names list. Information schema name always is first in list
2041
2042
  SYNOPSIS
2043
    make_db_list()
520.1.22 by Brian Aker
Second pass of thd cleanup
2044
    session                   thread handler
1 by brian
clean slate
2045
    files                 list of db names
2046
    wild                  wild string
2047
    idx_field_vals        idx_field_vals->db_name contains db name or
2048
                          wild string
2049
    with_i_schema         returns 1 if we added 'IS' name to list
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2050
                          otherwise returns 0
1 by brian
clean slate
2051
2052
  RETURN
2053
    zero                  success
2054
    non-zero              error
2055
*/
2056
520.1.22 by Brian Aker
Second pass of thd cleanup
2057
int make_db_list(Session *session, List<LEX_STRING> *files,
1 by brian
clean slate
2058
                 LOOKUP_FIELD_VALUES *lookup_field_vals,
2059
                 bool *with_i_schema)
2060
{
2061
  LEX_STRING *i_s_name_copy= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
2062
  i_s_name_copy= session->make_lex_string(i_s_name_copy,
575.4.7 by Monty Taylor
More header cleanup.
2063
                                      INFORMATION_SCHEMA_NAME.c_str(),
2064
                                      INFORMATION_SCHEMA_NAME.length(), true);
1 by brian
clean slate
2065
  *with_i_schema= 0;
2066
  if (lookup_field_vals->wild_db_value)
2067
  {
2068
    /*
2069
      This part of code is only for SHOW DATABASES command.
2070
      idx_field_vals->db_value can be 0 when we don't use
2071
      LIKE clause (see also get_index_field_values() function)
2072
    */
2073
    if (!lookup_field_vals->db_value.str ||
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2074
        !wild_case_compare(system_charset_info,
575.4.7 by Monty Taylor
More header cleanup.
2075
                           INFORMATION_SCHEMA_NAME.c_str(),
1 by brian
clean slate
2076
                           lookup_field_vals->db_value.str))
2077
    {
2078
      *with_i_schema= 1;
2079
      if (files->push_back(i_s_name_copy))
2080
        return 1;
2081
    }
575.4.1 by ysano
Rename mysql to drizzle.
2082
    return (find_files(session, files, NULL, drizzle_data_home,
1 by brian
clean slate
2083
                       lookup_field_vals->db_value.str, 1) != FIND_FILES_OK);
2084
  }
2085
2086
2087
  /*
2088
    If we have db lookup vaule we just add it to list and
2089
    exit from the function
2090
  */
2091
  if (lookup_field_vals->db_value.str)
2092
  {
575.4.7 by Monty Taylor
More header cleanup.
2093
    if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.c_str(),
1 by brian
clean slate
2094
                       lookup_field_vals->db_value.str))
2095
    {
2096
      *with_i_schema= 1;
2097
      if (files->push_back(i_s_name_copy))
2098
        return 1;
2099
      return 0;
2100
    }
2101
    if (files->push_back(&lookup_field_vals->db_value))
2102
      return 1;
2103
    return 0;
2104
  }
2105
2106
  /*
2107
    Create list of existing databases. It is used in case
2108
    of select from information schema table
2109
  */
2110
  if (files->push_back(i_s_name_copy))
2111
    return 1;
2112
  *with_i_schema= 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
2113
  return (find_files(session, files, NULL,
575.4.1 by ysano
Rename mysql to drizzle.
2114
                     drizzle_data_home, NULL, 1) != FIND_FILES_OK);
1 by brian
clean slate
2115
}
2116
2117
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2118
struct st_add_schema_table
1 by brian
clean slate
2119
{
2120
  List<LEX_STRING> *files;
2121
  const char *wild;
2122
};
2123
2124
520.1.22 by Brian Aker
Second pass of thd cleanup
2125
static bool add_schema_table(Session *session, plugin_ref plugin,
1 by brian
clean slate
2126
                                void* p_data)
2127
{
2128
  LEX_STRING *file_name= 0;
2129
  st_add_schema_table *data= (st_add_schema_table *)p_data;
2130
  List<LEX_STRING> *file_list= data->files;
2131
  const char *wild= data->wild;
2132
  ST_SCHEMA_TABLE *schema_table= plugin_data(plugin, ST_SCHEMA_TABLE *);
2133
2134
  if (schema_table->hidden)
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2135
      return(0);
1 by brian
clean slate
2136
  if (wild)
2137
  {
2138
    if (lower_case_table_names)
2139
    {
2140
      if (wild_case_compare(files_charset_info,
2141
                            schema_table->table_name,
2142
                            wild))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2143
        return(0);
1 by brian
clean slate
2144
    }
2145
    else if (wild_compare(schema_table->table_name, wild, 0))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2146
      return(0);
1 by brian
clean slate
2147
  }
2148
520.1.22 by Brian Aker
Second pass of thd cleanup
2149
  if ((file_name= session->make_lex_string(file_name, schema_table->table_name,
1 by brian
clean slate
2150
                                       strlen(schema_table->table_name),
163 by Brian Aker
Merge Monty's code.
2151
                                       true)) &&
1 by brian
clean slate
2152
      !file_list->push_back(file_name))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2153
    return(0);
2154
  return(1);
1 by brian
clean slate
2155
}
2156
2157
520.1.22 by Brian Aker
Second pass of thd cleanup
2158
int schema_tables_add(Session *session, List<LEX_STRING> *files, const char *wild)
1 by brian
clean slate
2159
{
2160
  LEX_STRING *file_name= 0;
2161
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2162
  st_add_schema_table add_data;
2163
2164
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2165
  {
2166
    if (tmp_schema_table->hidden)
2167
      continue;
2168
    if (wild)
2169
    {
2170
      if (lower_case_table_names)
2171
      {
2172
        if (wild_case_compare(files_charset_info,
2173
                              tmp_schema_table->table_name,
2174
                              wild))
2175
          continue;
2176
      }
2177
      else if (wild_compare(tmp_schema_table->table_name, wild, 0))
2178
        continue;
2179
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2180
    if ((file_name=
520.1.22 by Brian Aker
Second pass of thd cleanup
2181
         session->make_lex_string(file_name, tmp_schema_table->table_name,
163 by Brian Aker
Merge Monty's code.
2182
                              strlen(tmp_schema_table->table_name), true)) &&
1 by brian
clean slate
2183
        !files->push_back(file_name))
2184
      continue;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2185
    return(1);
1 by brian
clean slate
2186
  }
2187
2188
  add_data.files= files;
2189
  add_data.wild= wild;
520.1.22 by Brian Aker
Second pass of thd cleanup
2190
  if (plugin_foreach(session, add_schema_table,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2191
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &add_data))
259 by Brian Aker
First pass on PAM auth
2192
    return(1);
1 by brian
clean slate
2193
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2194
  return(0);
1 by brian
clean slate
2195
}
2196
2197
2198
/**
2199
  @brief          Create table names list
2200
2201
  @details        The function creates the list of table names in
2202
                  database
2203
520.1.22 by Brian Aker
Second pass of thd cleanup
2204
  @param[in]      session                   thread handler
1 by brian
clean slate
2205
  @param[in]      table_names           List of table names in database
2206
  @param[in]      lex                   pointer to LEX struct
2207
  @param[in]      lookup_field_vals     pointer to LOOKUP_FIELD_VALUE struct
163 by Brian Aker
Merge Monty's code.
2208
  @param[in]      with_i_schema         true means that we add I_S tables to list
1 by brian
clean slate
2209
  @param[in]      db_name               database name
2210
2211
  @return         Operation status
2212
    @retval       0           ok
2213
    @retval       1           fatal error
2214
    @retval       2           Not fatal error; Safe to ignore this file list
2215
*/
2216
2217
static int
520.1.22 by Brian Aker
Second pass of thd cleanup
2218
make_table_name_list(Session *session, List<LEX_STRING> *table_names, LEX *lex,
1 by brian
clean slate
2219
                     LOOKUP_FIELD_VALUES *lookup_field_vals,
2220
                     bool with_i_schema, LEX_STRING *db_name)
2221
{
2222
  char path[FN_REFLEN];
2223
  build_table_filename(path, sizeof(path), db_name->str, "", "", 0);
2224
  if (!lookup_field_vals->wild_table_value &&
2225
      lookup_field_vals->table_value.str)
2226
  {
2227
    if (with_i_schema)
2228
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2229
      if (find_schema_table(session, lookup_field_vals->table_value.str))
1 by brian
clean slate
2230
      {
2231
        if (table_names->push_back(&lookup_field_vals->table_value))
2232
          return 1;
2233
      }
2234
    }
2235
    else
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2236
    {
1 by brian
clean slate
2237
      if (table_names->push_back(&lookup_field_vals->table_value))
2238
        return 1;
2239
    }
2240
    return 0;
2241
  }
2242
2243
  /*
2244
    This call will add all matching the wildcards (if specified) IS tables
2245
    to the list
2246
  */
2247
  if (with_i_schema)
520.1.22 by Brian Aker
Second pass of thd cleanup
2248
    return (schema_tables_add(session, table_names,
1 by brian
clean slate
2249
                              lookup_field_vals->table_value.str));
2250
520.1.22 by Brian Aker
Second pass of thd cleanup
2251
  find_files_result res= find_files(session, table_names, db_name->str, path,
1 by brian
clean slate
2252
                                    lookup_field_vals->table_value.str, 0);
2253
  if (res != FIND_FILES_OK)
2254
  {
2255
    /*
2256
      Downgrade errors about problems with database directory to
2257
      warnings if this is not a 'SHOW' command.  Another thread
2258
      may have dropped database, and we may still have a name
2259
      for that directory.
2260
    */
2261
    if (res == FIND_FILES_DIR)
2262
    {
2263
      if (lex->sql_command != SQLCOM_SELECT)
2264
        return 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
2265
      session->clear_error();
1 by brian
clean slate
2266
      return 2;
2267
    }
2268
    return 1;
2269
  }
2270
  return 0;
2271
}
2272
2273
2274
/**
2275
  @brief          Fill I_S table for SHOW COLUMNS|INDEX commands
2276
520.1.22 by Brian Aker
Second pass of thd cleanup
2277
  @param[in]      session                      thread handler
327.2.4 by Brian Aker
Refactoring table.h
2278
  @param[in]      tables                   TableList for I_S table
1 by brian
clean slate
2279
  @param[in]      schema_table             pointer to I_S structure
2280
  @param[in]      open_tables_state_backup pointer to Open_tables_state object
2281
                                           which is used to save|restore original
2282
                                           status of variables related to
2283
                                           open tables state
2284
2285
  @return         Operation status
2286
    @retval       0           success
2287
    @retval       1           error
2288
*/
2289
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2290
static int
520.1.22 by Brian Aker
Second pass of thd cleanup
2291
fill_schema_show_cols_or_idxs(Session *session, TableList *tables,
1 by brian
clean slate
2292
                              ST_SCHEMA_TABLE *schema_table,
2293
                              Open_tables_state *open_tables_state_backup)
2294
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2295
  LEX *lex= session->lex;
1 by brian
clean slate
2296
  bool res;
2297
  LEX_STRING tmp_lex_string, tmp_lex_string1, *db_name, *table_name;
2298
  enum_sql_command save_sql_command= lex->sql_command;
327.2.4 by Brian Aker
Refactoring table.h
2299
  TableList *show_table_list= (TableList*) tables->schema_select_lex->
1 by brian
clean slate
2300
    table_list.first;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2301
  Table *table= tables->table;
1 by brian
clean slate
2302
  int error= 1;
2303
2304
  lex->all_selects_list= tables->schema_select_lex;
2305
  /*
520.1.22 by Brian Aker
Second pass of thd cleanup
2306
    Restore session->temporary_tables to be able to process
1 by brian
clean slate
2307
    temporary tables(only for 'show index' & 'show columns').
2308
    This should be changed when processing of temporary tables for
2309
    I_S tables will be done.
2310
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
2311
  session->temporary_tables= open_tables_state_backup->temporary_tables;
1 by brian
clean slate
2312
  /*
2313
    Let us set fake sql_command so views won't try to merge
2314
    themselves into main statement. If we don't do this,
2315
    SELECT * from information_schema.xxxx will cause problems.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2316
    SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()'
1 by brian
clean slate
2317
  */
2318
  lex->sql_command= SQLCOM_SHOW_FIELDS;
520.1.22 by Brian Aker
Second pass of thd cleanup
2319
  res= open_normal_and_derived_tables(session, show_table_list,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2320
                                      DRIZZLE_LOCK_IGNORE_FLUSH);
1 by brian
clean slate
2321
  lex->sql_command= save_sql_command;
2322
  /*
2323
    get_all_tables() returns 1 on failure and 0 on success thus
2324
    return only these and not the result code of ::process_table()
2325
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2326
    We should use show_table_list->alias instead of
1 by brian
clean slate
2327
    show_table_list->table_name because table_name
2328
    could be changed during opening of I_S tables. It's safe
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2329
    to use alias because alias contains original table name
2330
    in this case(this part of code is used only for
1 by brian
clean slate
2331
    'show columns' & 'show statistics' commands).
2332
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
2333
   table_name= session->make_lex_string(&tmp_lex_string1, show_table_list->alias,
163 by Brian Aker
Merge Monty's code.
2334
                                    strlen(show_table_list->alias), false);
520.1.22 by Brian Aker
Second pass of thd cleanup
2335
   db_name= session->make_lex_string(&tmp_lex_string, show_table_list->db,
163 by Brian Aker
Merge Monty's code.
2336
                                 show_table_list->db_length, false);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2337
1 by brian
clean slate
2338
520.1.22 by Brian Aker
Second pass of thd cleanup
2339
   error= test(schema_table->process_table(session, show_table_list,
1 by brian
clean slate
2340
                                           table, res, db_name,
2341
                                           table_name));
520.1.22 by Brian Aker
Second pass of thd cleanup
2342
   session->temporary_tables= 0;
2343
   close_tables_for_reopen(session, &show_table_list);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2344
   return(error);
1 by brian
clean slate
2345
}
2346
2347
2348
/**
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2349
  @brief          Fill I_S table for SHOW Table NAMES commands
1 by brian
clean slate
2350
520.1.22 by Brian Aker
Second pass of thd cleanup
2351
  @param[in]      session                      thread handler
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2352
  @param[in]      table                    Table struct for I_S table
1 by brian
clean slate
2353
  @param[in]      db_name                  database name
2354
  @param[in]      table_name               table name
163 by Brian Aker
Merge Monty's code.
2355
  @param[in]      with_i_schema            I_S table if true
1 by brian
clean slate
2356
2357
  @return         Operation status
2358
    @retval       0           success
2359
    @retval       1           error
2360
*/
2361
520.1.22 by Brian Aker
Second pass of thd cleanup
2362
static int fill_schema_table_names(Session *session, Table *table,
1 by brian
clean slate
2363
                                   LEX_STRING *db_name, LEX_STRING *table_name,
2364
                                   bool with_i_schema)
2365
{
2366
  if (with_i_schema)
2367
  {
2368
    table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
2369
                           system_charset_info);
2370
  }
2371
  else
2372
  {
2373
    char path[FN_REFLEN];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2374
    (void) build_table_filename(path, sizeof(path), db_name->str,
1 by brian
clean slate
2375
                                table_name->str, reg_ext, 0);
590.1.7 by Stewart Smith
remove mysql_frm_type
2376
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2377
      table->field[3]->store(STRING_WITH_LEN("BASE Table"),
2378
                             system_charset_info);
2379
520.1.22 by Brian Aker
Second pass of thd cleanup
2380
    if (session->is_error() && session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
1 by brian
clean slate
2381
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2382
      session->clear_error();
1 by brian
clean slate
2383
      return 0;
2384
    }
2385
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
2386
  if (schema_table_store_record(session, table))
1 by brian
clean slate
2387
    return 1;
2388
  return 0;
2389
}
2390
2391
2392
/**
2393
  @brief          Get open table method
2394
2395
  @details        The function calculates the method which will be used
2396
                  for table opening:
2397
                  SKIP_OPEN_TABLE - do not open table
2398
                  OPEN_FRM_ONLY   - open FRM file only
2399
                  OPEN_FULL_TABLE - open FRM, data, index files
2400
  @param[in]      tables               I_S table table_list
2401
  @param[in]      schema_table         I_S table struct
2402
  @param[in]      schema_table_idx     I_S table index
2403
2404
  @return         return a set of flags
2405
    @retval       SKIP_OPEN_TABLE | OPEN_FRM_ONLY | OPEN_FULL_TABLE
2406
*/
2407
482 by Brian Aker
Remove uint.
2408
static uint32_t get_table_open_method(TableList *tables,
758 by Brian Aker
Randowm class cleanup.
2409
                                      ST_SCHEMA_TABLE *schema_table,
2410
                                      enum enum_schema_tables)
1 by brian
clean slate
2411
{
2412
  /*
2413
    determine which method will be used for table opening
2414
  */
2415
  if (schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
2416
  {
2417
    Field **ptr, *field;
2418
    int table_open_method= 0, field_indx= 0;
2419
    for (ptr=tables->table->field; (field= *ptr) ; ptr++)
2420
    {
2421
      if (bitmap_is_set(tables->table->read_set, field->field_index))
2422
        table_open_method|= schema_table->fields_info[field_indx].open_method;
2423
      field_indx++;
2424
    }
2425
    return table_open_method;
2426
  }
2427
  /* I_S tables which use get_all_tables but can not be optimized */
2428
  return (uint) OPEN_FULL_TABLE;
2429
}
2430
2431
2432
/**
2433
  @brief          Fill I_S table with data from FRM file only
2434
520.1.22 by Brian Aker
Second pass of thd cleanup
2435
  @param[in]      session                      thread handler
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2436
  @param[in]      table                    Table struct for I_S table
1 by brian
clean slate
2437
  @param[in]      schema_table             I_S table struct
2438
  @param[in]      db_name                  database name
2439
  @param[in]      table_name               table name
2440
  @param[in]      schema_table_idx         I_S table index
2441
2442
  @return         Operation status
2443
    @retval       0           Table is processed and we can continue
2444
                              with new table
2445
    @retval       1           It's view and we have to use
2446
                              open_tables function for this table
2447
*/
2448
520.1.22 by Brian Aker
Second pass of thd cleanup
2449
static int fill_schema_table_from_frm(Session *session,TableList *tables,
77.1.46 by Monty Taylor
Finished the warnings work!
2450
                                      ST_SCHEMA_TABLE *schema_table,
1 by brian
clean slate
2451
                                      LEX_STRING *db_name,
2452
                                      LEX_STRING *table_name,
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2453
                                      enum enum_schema_tables)
1 by brian
clean slate
2454
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2455
  Table *table= tables->table;
1 by brian
clean slate
2456
  TABLE_SHARE *share;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2457
  Table tbl;
327.2.4 by Brian Aker
Refactoring table.h
2458
  TableList table_list;
482 by Brian Aker
Remove uint.
2459
  uint32_t res= 0;
1 by brian
clean slate
2460
  int error;
2461
  char key[MAX_DBKEY_LENGTH];
482 by Brian Aker
Remove uint.
2462
  uint32_t key_length;
1 by brian
clean slate
2463
327.2.4 by Brian Aker
Refactoring table.h
2464
  memset(&table_list, 0, sizeof(TableList));
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2465
  memset(&tbl, 0, sizeof(Table));
1 by brian
clean slate
2466
2467
  table_list.table_name= table_name->str;
2468
  table_list.db= db_name->str;
2469
520.1.22 by Brian Aker
Second pass of thd cleanup
2470
  key_length= create_table_def_key(session, key, &table_list, 0);
1 by brian
clean slate
2471
  pthread_mutex_lock(&LOCK_open);
520.1.22 by Brian Aker
Second pass of thd cleanup
2472
  share= get_table_share(session, &table_list, key,
357 by Brian Aker
flag cleanup
2473
                         key_length, 0, &error);
1 by brian
clean slate
2474
  if (!share)
2475
  {
2476
    res= 0;
2477
    goto err;
2478
  }
2479
2480
  {
2481
    tbl.s= share;
2482
    table_list.table= &tbl;
520.1.22 by Brian Aker
Second pass of thd cleanup
2483
    res= schema_table->process_table(session, &table_list, table,
1 by brian
clean slate
2484
                                     res, db_name, table_name);
2485
  }
2486
2487
  release_table_share(share, RELEASE_NORMAL);
2488
2489
err:
2490
  pthread_mutex_unlock(&LOCK_open);
520.1.22 by Brian Aker
Second pass of thd cleanup
2491
  session->clear_error();
1 by brian
clean slate
2492
  return res;
2493
}
2494
2495
2496
2497
/**
2498
  @brief          Fill I_S tables whose data are retrieved
2499
                  from frm files and storage engine
2500
2501
  @details        The information schema tables are internally represented as
2502
                  temporary tables that are filled at query execution time.
2503
                  Those I_S tables whose data are retrieved
2504
                  from frm files and storage engine are filled by the function
2505
                  get_all_tables().
2506
520.1.22 by Brian Aker
Second pass of thd cleanup
2507
  @param[in]      session                      thread handler
1 by brian
clean slate
2508
  @param[in]      tables                   I_S table
2509
  @param[in]      cond                     'WHERE' condition
2510
2511
  @return         Operation status
2512
    @retval       0                        success
2513
    @retval       1                        error
2514
*/
2515
520.1.22 by Brian Aker
Second pass of thd cleanup
2516
int get_all_tables(Session *session, TableList *tables, COND *cond)
1 by brian
clean slate
2517
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2518
  LEX *lex= session->lex;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2519
  Table *table= tables->table;
1 by brian
clean slate
2520
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2521
  enum_sql_command save_sql_command= lex->sql_command;
2522
  SELECT_LEX *lsel= tables->schema_select_lex;
2523
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2524
  SELECT_LEX sel;
2525
  LOOKUP_FIELD_VALUES lookup_field_vals;
2526
  LEX_STRING *db_name, *table_name;
2527
  bool with_i_schema;
2528
  enum enum_schema_tables schema_table_idx;
2529
  List<LEX_STRING> db_names;
2530
  List_iterator_fast<LEX_STRING> it(db_names);
2531
  COND *partial_cond= 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2532
  uint32_t derived_tables= lex->derived_tables;
1 by brian
clean slate
2533
  int error= 1;
2534
  Open_tables_state open_tables_state_backup;
2535
  Query_tables_list query_tables_list_backup;
482 by Brian Aker
Remove uint.
2536
  uint32_t table_open_method;
520.1.22 by Brian Aker
Second pass of thd cleanup
2537
  bool old_value= session->no_warnings_for_error;
1 by brian
clean slate
2538
2539
  /*
2540
    We should not introduce deadlocks even if we already have some
2541
    tables open and locked, since we won't lock tables which we will
2542
    open and will ignore possible name-locks for these tables.
2543
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
2544
  session->reset_n_backup_open_tables_state(&open_tables_state_backup);
1 by brian
clean slate
2545
2546
  schema_table_idx= get_schema_table_idx(schema_table);
2547
  tables->table_open_method= table_open_method=
2548
    get_table_open_method(tables, schema_table, schema_table_idx);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2549
  /*
1 by brian
clean slate
2550
    this branch processes SHOW FIELDS, SHOW INDEXES commands.
2551
    see sql_parse.cc, prepare_schema_table() function where
2552
    this values are initialized
2553
  */
2554
  if (lsel && lsel->table_list.first)
2555
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2556
    error= fill_schema_show_cols_or_idxs(session, tables, schema_table,
1 by brian
clean slate
2557
                                         &open_tables_state_backup);
2558
    goto err;
2559
  }
2560
520.1.22 by Brian Aker
Second pass of thd cleanup
2561
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
1 by brian
clean slate
2562
  {
2563
    error= 0;
2564
    goto err;
2565
  }
2566
2567
  if (!lookup_field_vals.wild_db_value && !lookup_field_vals.wild_table_value)
2568
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2569
    /*
1 by brian
clean slate
2570
      if lookup value is empty string then
2571
      it's impossible table name or db name
2572
    */
140 by Brian Aker
Remove dead class, clean up warning.
2573
    if ((lookup_field_vals.db_value.str && !lookup_field_vals.db_value.str[0]) ||
2574
        (lookup_field_vals.table_value.str && !lookup_field_vals.table_value.str[0]))
1 by brian
clean slate
2575
    {
2576
      error= 0;
2577
      goto err;
2578
    }
2579
  }
2580
2581
  if (lookup_field_vals.db_value.length &&
2582
      !lookup_field_vals.wild_db_value)
163 by Brian Aker
Merge Monty's code.
2583
    tables->has_db_lookup_value= true;
1 by brian
clean slate
2584
  if (lookup_field_vals.table_value.length &&
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2585
      !lookup_field_vals.wild_table_value)
163 by Brian Aker
Merge Monty's code.
2586
    tables->has_table_lookup_value= true;
1 by brian
clean slate
2587
2588
  if (tables->has_db_lookup_value && tables->has_table_lookup_value)
2589
    partial_cond= 0;
2590
  else
2591
    partial_cond= make_cond_for_info_schema(cond, tables);
2592
2593
  if (lex->describe)
2594
  {
2595
    /* EXPLAIN SELECT */
2596
    error= 0;
2597
    goto err;
2598
  }
2599
520.1.22 by Brian Aker
Second pass of thd cleanup
2600
  if (make_db_list(session, &db_names, &lookup_field_vals, &with_i_schema))
1 by brian
clean slate
2601
    goto err;
2602
  it.rewind(); /* To get access to new elements in basis list */
2603
  while ((db_name= it++))
2604
  {
2605
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2606
      session->no_warnings_for_error= 1;
1 by brian
clean slate
2607
      List<LEX_STRING> table_names;
520.1.22 by Brian Aker
Second pass of thd cleanup
2608
      int res= make_table_name_list(session, &table_names, lex,
1 by brian
clean slate
2609
                                    &lookup_field_vals,
2610
                                    with_i_schema, db_name);
2611
      if (res == 2)   /* Not fatal error, continue */
2612
        continue;
2613
      if (res)
2614
        goto err;
2615
2616
      List_iterator_fast<LEX_STRING> it_files(table_names);
2617
      while ((table_name= it_files++))
2618
      {
2619
	restore_record(table, s->default_values);
2620
        table->field[schema_table->idx_field1]->
2621
          store(db_name->str, db_name->length, system_charset_info);
2622
        table->field[schema_table->idx_field2]->
2623
          store(table_name->str, table_name->length, system_charset_info);
2624
2625
        if (!partial_cond || partial_cond->val_int())
2626
        {
2627
          /*
2628
            If table is I_S.tables and open_table_method is 0 (eg SKIP_OPEN)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2629
            we can skip table opening and we don't have lookup value for
1 by brian
clean slate
2630
            table name or lookup value is wild string(table name list is
2631
            already created by make_table_name_list() function).
2632
          */
2633
          if (!table_open_method && schema_table_idx == SCH_TABLES &&
2634
              (!lookup_field_vals.table_value.length ||
2635
               lookup_field_vals.wild_table_value))
2636
          {
520.1.22 by Brian Aker
Second pass of thd cleanup
2637
            if (schema_table_store_record(session, table))
1 by brian
clean slate
2638
              goto err;      /* Out of space in temporary table */
2639
            continue;
2640
          }
2641
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2642
          /* SHOW Table NAMES command */
1 by brian
clean slate
2643
          if (schema_table_idx == SCH_TABLE_NAMES)
2644
          {
520.1.22 by Brian Aker
Second pass of thd cleanup
2645
            if (fill_schema_table_names(session, tables->table, db_name,
1 by brian
clean slate
2646
                                        table_name, with_i_schema))
2647
              continue;
2648
          }
2649
          else
2650
          {
2651
            if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2652
                !with_i_schema)
2653
            {
520.1.22 by Brian Aker
Second pass of thd cleanup
2654
              if (!fill_schema_table_from_frm(session, tables, schema_table, db_name,
1 by brian
clean slate
2655
                                              table_name, schema_table_idx))
2656
                continue;
2657
            }
2658
2659
            int res;
2660
            LEX_STRING tmp_lex_string, orig_db_name;
2661
            /*
2662
              Set the parent lex of 'sel' because it is needed by
2663
              sel.init_query() which is called inside make_table_list.
2664
            */
520.1.22 by Brian Aker
Second pass of thd cleanup
2665
            session->no_warnings_for_error= 1;
1 by brian
clean slate
2666
            sel.parent_lex= lex;
2667
            /* db_name can be changed in make_table_list() func */
520.1.22 by Brian Aker
Second pass of thd cleanup
2668
            if (!session->make_lex_string(&orig_db_name, db_name->str,
163 by Brian Aker
Merge Monty's code.
2669
                                      db_name->length, false))
1 by brian
clean slate
2670
              goto err;
520.1.22 by Brian Aker
Second pass of thd cleanup
2671
            if (make_table_list(session, &sel, db_name, table_name))
1 by brian
clean slate
2672
              goto err;
327.2.4 by Brian Aker
Refactoring table.h
2673
            TableList *show_table_list= (TableList*) sel.table_list.first;
1 by brian
clean slate
2674
            lex->all_selects_list= &sel;
2675
            lex->derived_tables= 0;
2676
            lex->sql_command= SQLCOM_SHOW_FIELDS;
2677
            show_table_list->i_s_requested_object=
2678
              schema_table->i_s_requested_object;
520.1.22 by Brian Aker
Second pass of thd cleanup
2679
            res= open_normal_and_derived_tables(session, show_table_list,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2680
                                                DRIZZLE_LOCK_IGNORE_FLUSH);
1 by brian
clean slate
2681
            lex->sql_command= save_sql_command;
2682
            /*
2683
              XXX:  show_table_list has a flag i_is_requested,
2684
              and when it's set, open_normal_and_derived_tables()
2685
              can return an error without setting an error message
520.1.21 by Brian Aker
THD -> Session rename
2686
              in Session, which is a hack. This is why we have to
520.1.22 by Brian Aker
Second pass of thd cleanup
2687
              check for res, then for session->is_error() only then
2688
              for session->main_da.sql_errno().
1 by brian
clean slate
2689
            */
520.1.22 by Brian Aker
Second pass of thd cleanup
2690
            if (res && session->is_error() &&
2691
                session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
1 by brian
clean slate
2692
            {
2693
              /*
2694
                Hide error for not existing table.
2695
                This error can occur for example when we use
2696
                where condition with db name and table name and this
2697
                table does not exist.
2698
              */
2699
              res= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
2700
              session->clear_error();
1 by brian
clean slate
2701
            }
2702
            else
2703
            {
2704
              /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2705
                We should use show_table_list->alias instead of
1 by brian
clean slate
2706
                show_table_list->table_name because table_name
2707
                could be changed during opening of I_S tables. It's safe
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2708
                to use alias because alias contains original table name
1 by brian
clean slate
2709
                in this case.
2710
              */
520.1.22 by Brian Aker
Second pass of thd cleanup
2711
              session->make_lex_string(&tmp_lex_string, show_table_list->alias,
163 by Brian Aker
Merge Monty's code.
2712
                                   strlen(show_table_list->alias), false);
520.1.22 by Brian Aker
Second pass of thd cleanup
2713
              res= schema_table->process_table(session, show_table_list, table,
1 by brian
clean slate
2714
                                               res, &orig_db_name,
2715
                                               &tmp_lex_string);
520.1.22 by Brian Aker
Second pass of thd cleanup
2716
              close_tables_for_reopen(session, &show_table_list);
1 by brian
clean slate
2717
            }
51.1.75 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2718
            assert(!lex->query_tables_own_last);
1 by brian
clean slate
2719
            if (res)
2720
              goto err;
2721
          }
2722
        }
2723
      }
2724
      /*
2725
        If we have information schema its always the first table and only
2726
        the first table. Reset for other tables.
2727
      */
2728
      with_i_schema= 0;
2729
    }
2730
  }
2731
2732
  error= 0;
2733
err:
520.1.22 by Brian Aker
Second pass of thd cleanup
2734
  session->restore_backup_open_tables_state(&open_tables_state_backup);
1 by brian
clean slate
2735
  lex->derived_tables= derived_tables;
2736
  lex->all_selects_list= old_all_select_lex;
2737
  lex->sql_command= save_sql_command;
520.1.22 by Brian Aker
Second pass of thd cleanup
2738
  session->no_warnings_for_error= old_value;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2739
  return(error);
1 by brian
clean slate
2740
}
2741
2742
520.1.22 by Brian Aker
Second pass of thd cleanup
2743
bool store_schema_shemata(Session* session, Table *table, LEX_STRING *db_name,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
2744
                          const CHARSET_INFO * const cs)
1 by brian
clean slate
2745
{
2746
  restore_record(table, s->default_values);
2747
  table->field[1]->store(db_name->str, db_name->length, system_charset_info);
2748
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
2749
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
2750
  return schema_table_store_record(session, table);
1 by brian
clean slate
2751
}
2752
2753
520.1.22 by Brian Aker
Second pass of thd cleanup
2754
int fill_schema_schemata(Session *session, TableList *tables, COND *cond)
1 by brian
clean slate
2755
{
2756
  /*
2757
    TODO: fill_schema_shemata() is called when new client is connected.
2758
    Returning error status in this case leads to client hangup.
2759
  */
2760
2761
  LOOKUP_FIELD_VALUES lookup_field_vals;
2762
  List<LEX_STRING> db_names;
2763
  LEX_STRING *db_name;
2764
  bool with_i_schema;
2765
  HA_CREATE_INFO create;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2766
  Table *table= tables->table;
1 by brian
clean slate
2767
520.1.22 by Brian Aker
Second pass of thd cleanup
2768
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2769
    return(0);
520.1.22 by Brian Aker
Second pass of thd cleanup
2770
  if (make_db_list(session, &db_names, &lookup_field_vals,
1 by brian
clean slate
2771
                   &with_i_schema))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2772
    return(1);
1 by brian
clean slate
2773
2774
  /*
2775
    If we have lookup db value we should check that the database exists
2776
  */
2777
  if(lookup_field_vals.db_value.str && !lookup_field_vals.wild_db_value &&
2778
     !with_i_schema)
2779
  {
2780
    char path[FN_REFLEN+16];
482 by Brian Aker
Remove uint.
2781
    uint32_t path_len;
15 by brian
Fix for stat, NETWARE removal
2782
    struct stat stat_info;
1 by brian
clean slate
2783
    if (!lookup_field_vals.db_value.str[0])
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2784
      return(0);
1 by brian
clean slate
2785
    path_len= build_table_filename(path, sizeof(path),
2786
                                   lookup_field_vals.db_value.str, "", "", 0);
2787
    path[path_len-1]= 0;
15 by brian
Fix for stat, NETWARE removal
2788
    if (stat(path,&stat_info))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2789
      return(0);
1 by brian
clean slate
2790
  }
2791
2792
  List_iterator_fast<LEX_STRING> it(db_names);
2793
  while ((db_name=it++))
2794
  {
2795
    if (with_i_schema)       // information schema name is always first in list
2796
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2797
      if (store_schema_shemata(session, table, db_name,
1 by brian
clean slate
2798
                               system_charset_info))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2799
        return(1);
1 by brian
clean slate
2800
      with_i_schema= 0;
2801
      continue;
2802
    }
2803
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2804
      load_db_opt_by_name(session, db_name->str, &create);
2805
      if (store_schema_shemata(session, table, db_name,
1 by brian
clean slate
2806
                               create.default_table_charset))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2807
        return(1);
1 by brian
clean slate
2808
    }
2809
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
2810
  return(0);
1 by brian
clean slate
2811
}
2812
2813
520.1.22 by Brian Aker
Second pass of thd cleanup
2814
static int get_schema_tables_record(Session *session, TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2815
				    Table *table, bool res,
1 by brian
clean slate
2816
				    LEX_STRING *db_name,
2817
				    LEX_STRING *table_name)
2818
{
2819
  const char *tmp_buff;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
2820
  DRIZZLE_TIME time;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
2821
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
2822
2823
  restore_record(table, s->default_values);
2824
  table->field[1]->store(db_name->str, db_name->length, cs);
2825
  table->field[2]->store(table_name->str, table_name->length, cs);
2826
  if (res)
2827
  {
2828
    /*
2829
      there was errors during opening tables
2830
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
2831
    const char *error= session->is_error() ? session->main_da.message() : "";
1 by brian
clean slate
2832
    if (tables->schema_table)
2833
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2834
    else
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2835
      table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
1 by brian
clean slate
2836
    table->field[20]->store(error, strlen(error), cs);
520.1.22 by Brian Aker
Second pass of thd cleanup
2837
    session->clear_error();
1 by brian
clean slate
2838
  }
2839
  else
2840
  {
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
2841
    char option_buff[400],*ptr;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2842
    Table *show_table= tables->table;
1 by brian
clean slate
2843
    TABLE_SHARE *share= show_table->s;
2844
    handler *file= show_table->file;
2845
    handlerton *tmp_db_type= share->db_type();
2846
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2847
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2848
    else if (share->tmp_table)
2849
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
2850
    else
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2851
      table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
1 by brian
clean slate
2852
2853
    for (int i= 4; i < 20; i++)
2854
    {
2855
      if (i == 7 || (i > 12 && i < 17) || i == 18)
2856
        continue;
2857
      table->field[i]->set_notnull();
2858
    }
2859
    tmp_buff= (char *) ha_resolve_storage_engine_name(tmp_db_type);
2860
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
590.1.4 by Stewart Smith
remove frm_version from TABLE_SHARE
2861
    table->field[5]->store((int64_t) 0, true);
1 by brian
clean slate
2862
2863
    ptr=option_buff;
2864
    if (share->min_rows)
2865
    {
641.4.2 by Toru Maesaka
Second pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2866
      ptr= strcpy(ptr," min_rows=")+10;
2867
      ptr= int64_t10_to_str(share->min_rows,ptr,10);
1 by brian
clean slate
2868
    }
2869
    if (share->max_rows)
2870
    {
641.4.2 by Toru Maesaka
Second pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2871
      ptr= strcpy(ptr," max_rows=")+10;
2872
      ptr= int64_t10_to_str(share->max_rows,ptr,10);
1 by brian
clean slate
2873
    }
2874
    if (share->avg_row_length)
2875
    {
641.4.2 by Toru Maesaka
Second pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2876
      ptr= strcpy(ptr," avg_row_length=")+16;
2877
      ptr= int64_t10_to_str(share->avg_row_length,ptr,10);
1 by brian
clean slate
2878
    }
2879
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2880
      ptr= strcpy(ptr," pack_keys=1")+12;
1 by brian
clean slate
2881
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2882
      ptr= strcpy(ptr," pack_keys=0")+12;
1 by brian
clean slate
2883
    /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */
2884
    if (share->db_create_options & HA_OPTION_CHECKSUM)
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2885
      ptr= strcpy(ptr," checksum=1")+11;
1 by brian
clean slate
2886
    if (share->page_checksum != HA_CHOICE_UNDEF)
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
2887
      ptr+= sprintf(ptr, " page_checksum=%s",
2888
                    ha_choice_values[(uint) share->page_checksum]);
1 by brian
clean slate
2889
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
641.4.2 by Toru Maesaka
Second pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2890
      ptr= strcpy(ptr," delay_key_write=1")+18;
1 by brian
clean slate
2891
    if (share->row_type != ROW_TYPE_DEFAULT)
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
2892
      ptr+= sprintf(ptr, " row_format=%s", ha_row_type[(uint)share->row_type]);
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
2893
    if (share->block_size)
2894
    {
641.4.2 by Toru Maesaka
Second pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2895
      ptr= strcpy(ptr, " block_size=")+12;
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
2896
      ptr= int64_t10_to_str(share->block_size, ptr, 10);
2897
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2898
1 by brian
clean slate
2899
    if (share->transactional != HA_CHOICE_UNDEF)
2900
    {
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
2901
      ptr+= sprintf(ptr, " TRANSACTIONAL=%s",
2902
                    (share->transactional == HA_CHOICE_YES ? "1" : "0"));
1 by brian
clean slate
2903
    }
2904
    if (share->transactional != HA_CHOICE_UNDEF)
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
2905
      ptr+= sprintf(ptr, " transactional=%s",
2906
                    ha_choice_values[(uint) share->transactional]);
1 by brian
clean slate
2907
    table->field[19]->store(option_buff+1,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2908
                            (ptr == option_buff ? 0 :
1 by brian
clean slate
2909
                             (uint) (ptr-option_buff)-1), cs);
2910
2911
    tmp_buff= (share->table_charset ?
2912
               share->table_charset->name : "default");
2913
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
2914
2915
    if (share->comment.str)
2916
      table->field[20]->store(share->comment.str, share->comment.length, cs);
2917
2918
    if(file)
2919
    {
2920
      file->info(HA_STATUS_VARIABLE | HA_STATUS_TIME | HA_STATUS_AUTO |
2921
                 HA_STATUS_NO_LOCK);
2922
      enum row_type row_type = file->get_row_type();
2923
      switch (row_type) {
2924
      case ROW_TYPE_NOT_USED:
2925
      case ROW_TYPE_DEFAULT:
2926
        tmp_buff= ((share->db_options_in_use &
2927
                    HA_OPTION_COMPRESS_RECORD) ? "Compressed" :
2928
                   (share->db_options_in_use & HA_OPTION_PACK_RECORD) ?
2929
                   "Dynamic" : "Fixed");
2930
        break;
2931
      case ROW_TYPE_FIXED:
2932
        tmp_buff= "Fixed";
2933
        break;
2934
      case ROW_TYPE_DYNAMIC:
2935
        tmp_buff= "Dynamic";
2936
        break;
2937
      case ROW_TYPE_COMPRESSED:
2938
        tmp_buff= "Compressed";
2939
        break;
2940
      case ROW_TYPE_REDUNDANT:
2941
        tmp_buff= "Redundant";
2942
        break;
2943
      case ROW_TYPE_COMPACT:
2944
        tmp_buff= "Compact";
2945
        break;
2946
      case ROW_TYPE_PAGE:
2947
        tmp_buff= "Paged";
2948
        break;
2949
      }
2950
      table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
2951
      if (!tables->schema_table)
2952
      {
163 by Brian Aker
Merge Monty's code.
2953
        table->field[7]->store((int64_t) file->stats.records, true);
1 by brian
clean slate
2954
        table->field[7]->set_notnull();
2955
      }
163 by Brian Aker
Merge Monty's code.
2956
      table->field[8]->store((int64_t) file->stats.mean_rec_length, true);
2957
      table->field[9]->store((int64_t) file->stats.data_file_length, true);
1 by brian
clean slate
2958
      if (file->stats.max_data_file_length)
2959
      {
152 by Brian Aker
longlong replacement
2960
        table->field[10]->store((int64_t) file->stats.max_data_file_length,
163 by Brian Aker
Merge Monty's code.
2961
                                true);
1 by brian
clean slate
2962
      }
163 by Brian Aker
Merge Monty's code.
2963
      table->field[11]->store((int64_t) file->stats.index_file_length, true);
2964
      table->field[12]->store((int64_t) file->stats.delete_length, true);
1 by brian
clean slate
2965
      if (show_table->found_next_number_field)
2966
      {
152 by Brian Aker
longlong replacement
2967
        table->field[13]->store((int64_t) file->stats.auto_increment_value,
163 by Brian Aker
Merge Monty's code.
2968
                                true);
1 by brian
clean slate
2969
        table->field[13]->set_notnull();
2970
      }
2971
      if (file->stats.create_time)
2972
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
2973
        session->variables.time_zone->gmt_sec_to_TIME(&time,
726.1.1 by Toru Maesaka
Use time_t instead of my_time_t which should be safe (as in wide enough) on POSIX compliant systems.
2974
                                                  (time_t) file->stats.create_time);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
2975
        table->field[14]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
1 by brian
clean slate
2976
        table->field[14]->set_notnull();
2977
      }
2978
      if (file->stats.update_time)
2979
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
2980
        session->variables.time_zone->gmt_sec_to_TIME(&time,
726.1.1 by Toru Maesaka
Use time_t instead of my_time_t which should be safe (as in wide enough) on POSIX compliant systems.
2981
                                                  (time_t) file->stats.update_time);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
2982
        table->field[15]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
1 by brian
clean slate
2983
        table->field[15]->set_notnull();
2984
      }
2985
      if (file->stats.check_time)
2986
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
2987
        session->variables.time_zone->gmt_sec_to_TIME(&time,
726.1.1 by Toru Maesaka
Use time_t instead of my_time_t which should be safe (as in wide enough) on POSIX compliant systems.
2988
                                                  (time_t) file->stats.check_time);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
2989
        table->field[16]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
1 by brian
clean slate
2990
        table->field[16]->set_notnull();
2991
      }
2992
      if (file->ha_table_flags() & (ulong) HA_HAS_CHECKSUM)
2993
      {
163 by Brian Aker
Merge Monty's code.
2994
        table->field[18]->store((int64_t) file->checksum(), true);
1 by brian
clean slate
2995
        table->field[18]->set_notnull();
2996
      }
2997
    }
2998
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
2999
  return(schema_table_store_record(session, table));
1 by brian
clean slate
3000
}
3001
3002
3003
/**
3004
  @brief    Store field characteristics into appropriate I_S table columns
3005
3006
  @param[in]      table             I_S table
3007
  @param[in]      field             processed field
3008
  @param[in]      cs                I_S table charset
3009
  @param[in]      offset            offset from beginning of table
3010
                                    to DATE_TYPE column in I_S table
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3011
1 by brian
clean slate
3012
  @return         void
3013
*/
3014
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3015
void store_column_type(Table *table, Field *field, const CHARSET_INFO * const cs,
482 by Brian Aker
Remove uint.
3016
                       uint32_t offset)
1 by brian
clean slate
3017
{
3018
  bool is_blob;
3019
  int decimals, field_length;
3020
  const char *tmp_buff;
3021
  char column_type_buff[MAX_FIELD_WIDTH];
3022
  String column_type(column_type_buff, sizeof(column_type_buff), cs);
3023
3024
  field->sql_type(column_type);
3025
  /* DTD_IDENTIFIER column */
3026
  table->field[offset + 7]->store(column_type.ptr(), column_type.length(), cs);
3027
  table->field[offset + 7]->set_notnull();
3028
  tmp_buff= strchr(column_type.ptr(), '(');
3029
  /* DATA_TYPE column */
3030
  table->field[offset]->store(column_type.ptr(),
3031
                         (tmp_buff ? tmp_buff - column_type.ptr() :
3032
                          column_type.length()), cs);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3033
  is_blob= (field->type() == DRIZZLE_TYPE_BLOB);
1 by brian
clean slate
3034
  if (field->has_charset() || is_blob ||
238 by Brian Aker
Pass through sql_show to remove type to be removed.
3035
      field->real_type() == DRIZZLE_TYPE_VARCHAR)  // For varbinary type
1 by brian
clean slate
3036
  {
205 by Brian Aker
uint32 -> uin32_t
3037
    uint32_t octet_max_length= field->max_display_length();
3038
    if (is_blob && octet_max_length != (uint32_t) 4294967295U)
1 by brian
clean slate
3039
      octet_max_length /= field->charset()->mbmaxlen;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3040
    int64_t char_max_len= is_blob ?
152 by Brian Aker
longlong replacement
3041
      (int64_t) octet_max_length / field->charset()->mbminlen :
3042
      (int64_t) octet_max_length / field->charset()->mbmaxlen;
1 by brian
clean slate
3043
    /* CHARACTER_MAXIMUM_LENGTH column*/
163 by Brian Aker
Merge Monty's code.
3044
    table->field[offset + 1]->store(char_max_len, true);
1 by brian
clean slate
3045
    table->field[offset + 1]->set_notnull();
3046
    /* CHARACTER_OCTET_LENGTH column */
163 by Brian Aker
Merge Monty's code.
3047
    table->field[offset + 2]->store((int64_t) octet_max_length, true);
1 by brian
clean slate
3048
    table->field[offset + 2]->set_notnull();
3049
  }
3050
3051
  /*
3052
    Calculate field_length and decimals.
3053
    They are set to -1 if they should not be set (we should return NULL)
3054
  */
3055
3056
  decimals= field->decimals();
3057
  switch (field->type()) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3058
  case DRIZZLE_TYPE_NEWDECIMAL:
1 by brian
clean slate
3059
    field_length= ((Field_new_decimal*) field)->precision;
3060
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3061
  case DRIZZLE_TYPE_LONG:
3062
  case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
3063
    field_length= field->max_display_length() - 1;
3064
    break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3065
  case DRIZZLE_TYPE_DOUBLE:
1 by brian
clean slate
3066
    field_length= field->field_length;
3067
    if (decimals == NOT_FIXED_DEC)
3068
      decimals= -1;                           // return NULL
3069
    break;
3070
  default:
3071
    field_length= decimals= -1;
3072
    break;
3073
  }
3074
3075
  /* NUMERIC_PRECISION column */
3076
  if (field_length >= 0)
3077
  {
163 by Brian Aker
Merge Monty's code.
3078
    table->field[offset + 3]->store((int64_t) field_length, true);
1 by brian
clean slate
3079
    table->field[offset + 3]->set_notnull();
3080
  }
3081
  /* NUMERIC_SCALE column */
3082
  if (decimals >= 0)
3083
  {
163 by Brian Aker
Merge Monty's code.
3084
    table->field[offset + 4]->store((int64_t) decimals, true);
1 by brian
clean slate
3085
    table->field[offset + 4]->set_notnull();
3086
  }
3087
  if (field->has_charset())
3088
  {
3089
    /* CHARACTER_SET_NAME column*/
3090
    tmp_buff= field->charset()->csname;
3091
    table->field[offset + 5]->store(tmp_buff, strlen(tmp_buff), cs);
3092
    table->field[offset + 5]->set_notnull();
3093
    /* COLLATION_NAME column */
3094
    tmp_buff= field->charset()->name;
3095
    table->field[offset + 6]->store(tmp_buff, strlen(tmp_buff), cs);
3096
    table->field[offset + 6]->set_notnull();
3097
  }
3098
}
3099
3100
520.1.22 by Brian Aker
Second pass of thd cleanup
3101
static int get_schema_column_record(Session *session, TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3102
				    Table *table, bool res,
1 by brian
clean slate
3103
				    LEX_STRING *db_name,
3104
				    LEX_STRING *table_name)
3105
{
520.1.22 by Brian Aker
Second pass of thd cleanup
3106
  LEX *lex= session->lex;
461 by Monty Taylor
Removed NullS. bu-bye.
3107
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3108
  const CHARSET_INFO * const cs= system_charset_info;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3109
  Table *show_table;
1 by brian
clean slate
3110
  TABLE_SHARE *show_table_share;
3111
  Field **ptr, *field, *timestamp_field;
3112
  int count;
3113
3114
  if (res)
3115
  {
3116
    if (lex->sql_command != SQLCOM_SHOW_FIELDS)
3117
    {
3118
      /*
3119
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
3120
        rather than in SHOW COLUMNS
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3121
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
3122
      if (session->is_error())
3123
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3124
                     session->main_da.sql_errno(), session->main_da.message());
3125
      session->clear_error();
1 by brian
clean slate
3126
      res= 0;
3127
    }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3128
    return(res);
1 by brian
clean slate
3129
  }
3130
3131
  show_table= tables->table;
3132
  show_table_share= show_table->s;
3133
  count= 0;
3134
3135
  if (tables->schema_table)
3136
  {
3137
    ptr= show_table->field;
3138
    timestamp_field= show_table->timestamp_field;
3139
    show_table->use_all_columns();               // Required for default
3140
  }
3141
  else
3142
  {
3143
    ptr= show_table_share->field;
3144
    timestamp_field= show_table_share->timestamp_field;
3145
    /*
3146
      read_set may be inited in case of
3147
      temporary table
3148
    */
3149
    if (!show_table->read_set)
3150
    {
3151
      /* to satisfy 'field->val_str' ASSERTs */
481 by Brian Aker
Remove all of uchar.
3152
      unsigned char *bitmaps;
482 by Brian Aker
Remove uint.
3153
      uint32_t bitmap_size= show_table_share->column_bitmap_size;
520.1.22 by Brian Aker
Second pass of thd cleanup
3154
      if (!(bitmaps= (unsigned char*) alloc_root(session->mem_root, bitmap_size)))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3155
        return(0);
1 by brian
clean slate
3156
      bitmap_init(&show_table->def_read_set,
163 by Brian Aker
Merge Monty's code.
3157
                  (my_bitmap_map*) bitmaps, show_table_share->fields, false);
1 by brian
clean slate
3158
      bitmap_set_all(&show_table->def_read_set);
3159
      show_table->read_set= &show_table->def_read_set;
3160
    }
3161
    bitmap_set_all(show_table->read_set);
3162
  }
3163
3164
  for (; (field= *ptr) ; ptr++)
3165
  {
481 by Brian Aker
Remove all of uchar.
3166
    unsigned char *pos;
1 by brian
clean slate
3167
    char tmp[MAX_FIELD_WIDTH];
3168
    String type(tmp,sizeof(tmp), system_charset_info);
3169
    char *end;
3170
3171
    /* to satisfy 'field->val_str' ASSERTs */
3172
    field->table= show_table;
520.1.22 by Brian Aker
Second pass of thd cleanup
3173
    show_table->in_use= session;
1 by brian
clean slate
3174
3175
    if (wild && wild[0] &&
3176
        wild_case_compare(system_charset_info, field->field_name,wild))
3177
      continue;
3178
3179
    count++;
3180
    /* Get default row, with all NULL fields set to NULL */
3181
    restore_record(table, s->default_values);
3182
3183
    table->field[1]->store(db_name->str, db_name->length, cs);
3184
    table->field[2]->store(table_name->str, table_name->length, cs);
3185
    table->field[3]->store(field->field_name, strlen(field->field_name),
3186
                           cs);
163 by Brian Aker
Merge Monty's code.
3187
    table->field[4]->store((int64_t) count, true);
1 by brian
clean slate
3188
520.1.22 by Brian Aker
Second pass of thd cleanup
3189
    if (get_field_default_value(session, timestamp_field, field, &type, 0))
1 by brian
clean slate
3190
    {
3191
      table->field[5]->store(type.ptr(), type.length(), cs);
3192
      table->field[5]->set_notnull();
3193
    }
481 by Brian Aker
Remove all of uchar.
3194
    pos=(unsigned char*) ((field->flags & NOT_NULL_FLAG) ?  "NO" : "YES");
1 by brian
clean slate
3195
    table->field[6]->store((const char*) pos,
3196
                           strlen((const char*) pos), cs);
3197
    store_column_type(table, field, cs, 7);
3198
481 by Brian Aker
Remove all of uchar.
3199
    pos=(unsigned char*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
1 by brian
clean slate
3200
                 (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
3201
                 (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
3202
    table->field[15]->store((const char*) pos,
3203
                            strlen((const char*) pos), cs);
3204
3205
    end= tmp;
3206
    if (field->unireg_check == Field::NEXT_NUMBER)
3207
      table->field[16]->store(STRING_WITH_LEN("auto_increment"), cs);
3208
    if (timestamp_field == field &&
3209
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
3210
      table->field[16]->store(STRING_WITH_LEN("on update CURRENT_TIMESTAMP"),
3211
                              cs);
383.7.1 by Andrey Zhakov
Initial submit of code and tests
3212
    if (field->vcol_info)
3213
          table->field[16]->store(STRING_WITH_LEN("VIRTUAL"), cs);
1 by brian
clean slate
3214
    table->field[18]->store(field->comment.str, field->comment.length, cs);
3215
    {
3216
      enum column_format_type column_format= (enum column_format_type)
3217
        ((field->flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
481 by Brian Aker
Remove all of uchar.
3218
      pos=(unsigned char*)"Default";
1 by brian
clean slate
3219
      table->field[19]->store((const char*) pos,
3220
                              strlen((const char*) pos), cs);
481 by Brian Aker
Remove all of uchar.
3221
      pos=(unsigned char*)(column_format == COLUMN_FORMAT_TYPE_DEFAULT ? "Default" :
1 by brian
clean slate
3222
                   column_format == COLUMN_FORMAT_TYPE_FIXED ? "Fixed" :
3223
                                                             "Dynamic");
3224
      table->field[20]->store((const char*) pos,
3225
                              strlen((const char*) pos), cs);
3226
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
3227
    if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3228
      return(1);
1 by brian
clean slate
3229
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3230
  return(0);
1 by brian
clean slate
3231
}
3232
3233
3234
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3235
int fill_schema_charsets(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3236
{
3237
  CHARSET_INFO **cs;
520.1.22 by Brian Aker
Second pass of thd cleanup
3238
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3239
  Table *table= tables->table;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3240
  const CHARSET_INFO * const scs= system_charset_info;
1 by brian
clean slate
3241
3242
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
3243
  {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3244
    const CHARSET_INFO * const tmp_cs= cs[0];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3245
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) &&
1 by brian
clean slate
3246
        (tmp_cs->state & MY_CS_AVAILABLE) &&
3247
        !(tmp_cs->state & MY_CS_HIDDEN) &&
3248
        !(wild && wild[0] &&
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3249
          wild_case_compare(scs, tmp_cs->csname,wild)))
1 by brian
clean slate
3250
    {
3251
      const char *comment;
3252
      restore_record(table, s->default_values);
3253
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
3254
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
3255
      comment= tmp_cs->comment ? tmp_cs->comment : "";
3256
      table->field[2]->store(comment, strlen(comment), scs);
163 by Brian Aker
Merge Monty's code.
3257
      table->field[3]->store((int64_t) tmp_cs->mbmaxlen, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
3258
      if (schema_table_store_record(session, table))
1 by brian
clean slate
3259
        return 1;
3260
    }
3261
  }
3262
  return 0;
3263
}
3264
3265
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3266
int fill_schema_collation(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3267
{
3268
  CHARSET_INFO **cs;
520.1.22 by Brian Aker
Second pass of thd cleanup
3269
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3270
  Table *table= tables->table;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3271
  const CHARSET_INFO * const scs= system_charset_info;
1 by brian
clean slate
3272
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3273
  {
3274
    CHARSET_INFO **cl;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3275
    const CHARSET_INFO *tmp_cs= cs[0];
1 by brian
clean slate
3276
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) ||
3277
         (tmp_cs->state & MY_CS_HIDDEN) ||
3278
        !(tmp_cs->state & MY_CS_PRIMARY))
3279
      continue;
3280
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3281
    {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3282
      const CHARSET_INFO *tmp_cl= cl[0];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3283
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) ||
1 by brian
clean slate
3284
          !my_charset_same(tmp_cs, tmp_cl))
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3285
        continue;
1 by brian
clean slate
3286
      if (!(wild && wild[0] &&
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3287
          wild_case_compare(scs, tmp_cl->name,wild)))
1 by brian
clean slate
3288
      {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3289
        const char *tmp_buff;
3290
        restore_record(table, s->default_values);
3291
        table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
1 by brian
clean slate
3292
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
163 by Brian Aker
Merge Monty's code.
3293
        table->field[2]->store((int64_t) tmp_cl->number, true);
1 by brian
clean slate
3294
        tmp_buff= (tmp_cl->state & MY_CS_PRIMARY) ? "Yes" : "";
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3295
        table->field[3]->store(tmp_buff, strlen(tmp_buff), scs);
1 by brian
clean slate
3296
        tmp_buff= (tmp_cl->state & MY_CS_COMPILED)? "Yes" : "";
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3297
        table->field[4]->store(tmp_buff, strlen(tmp_buff), scs);
163 by Brian Aker
Merge Monty's code.
3298
        table->field[5]->store((int64_t) tmp_cl->strxfrm_multiply, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
3299
        if (schema_table_store_record(session, table))
1 by brian
clean slate
3300
          return 1;
3301
      }
3302
    }
3303
  }
3304
  return 0;
3305
}
3306
3307
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3308
int fill_schema_coll_charset_app(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3309
{
3310
  CHARSET_INFO **cs;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3311
  Table *table= tables->table;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3312
  const CHARSET_INFO * const scs= system_charset_info;
1 by brian
clean slate
3313
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3314
  {
3315
    CHARSET_INFO **cl;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3316
    const CHARSET_INFO *tmp_cs= cs[0];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3317
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) ||
1 by brian
clean slate
3318
        !(tmp_cs->state & MY_CS_PRIMARY))
3319
      continue;
3320
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3321
    {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3322
      const CHARSET_INFO *tmp_cl= cl[0];
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3323
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) ||
1 by brian
clean slate
3324
          !my_charset_same(tmp_cs,tmp_cl))
3325
	continue;
3326
      restore_record(table, s->default_values);
3327
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
3328
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
520.1.22 by Brian Aker
Second pass of thd cleanup
3329
      if (schema_table_store_record(session, table))
1 by brian
clean slate
3330
        return 1;
3331
    }
3332
  }
3333
  return 0;
3334
}
3335
3336
520.1.22 by Brian Aker
Second pass of thd cleanup
3337
static int get_schema_stat_record(Session *session, TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3338
				  Table *table, bool res,
1 by brian
clean slate
3339
				  LEX_STRING *db_name,
3340
				  LEX_STRING *table_name)
3341
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3342
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
3343
  if (res)
3344
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
3345
    if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
1 by brian
clean slate
3346
    {
3347
      /*
3348
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
3349
        rather than in SHOW KEYS
3350
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
3351
      if (session->is_error())
3352
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3353
                     session->main_da.sql_errno(), session->main_da.message());
3354
      session->clear_error();
1 by brian
clean slate
3355
      res= 0;
3356
    }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3357
    return(res);
1 by brian
clean slate
3358
  }
3359
  else
3360
  {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3361
    Table *show_table= tables->table;
1 by brian
clean slate
3362
    KEY *key_info=show_table->s->key_info;
3363
    if (show_table->file)
3364
      show_table->file->info(HA_STATUS_VARIABLE |
3365
                             HA_STATUS_NO_LOCK |
3366
                             HA_STATUS_TIME);
482 by Brian Aker
Remove uint.
3367
    for (uint32_t i=0 ; i < show_table->s->keys ; i++,key_info++)
1 by brian
clean slate
3368
    {
3369
      KEY_PART_INFO *key_part= key_info->key_part;
3370
      const char *str;
482 by Brian Aker
Remove uint.
3371
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
1 by brian
clean slate
3372
      {
3373
        restore_record(table, s->default_values);
3374
        table->field[1]->store(db_name->str, db_name->length, cs);
3375
        table->field[2]->store(table_name->str, table_name->length, cs);
152 by Brian Aker
longlong replacement
3376
        table->field[3]->store((int64_t) ((key_info->flags &
163 by Brian Aker
Merge Monty's code.
3377
                                            HA_NOSAME) ? 0 : 1), true);
1 by brian
clean slate
3378
        table->field[4]->store(db_name->str, db_name->length, cs);
3379
        table->field[5]->store(key_info->name, strlen(key_info->name), cs);
163 by Brian Aker
Merge Monty's code.
3380
        table->field[6]->store((int64_t) (j+1), true);
1 by brian
clean slate
3381
        str=(key_part->field ? key_part->field->field_name :
3382
             "?unknown field?");
3383
        table->field[7]->store(str, strlen(str), cs);
3384
        if (show_table->file)
3385
        {
3386
          if (show_table->file->index_flags(i, j, 0) & HA_READ_ORDER)
3387
          {
3388
            table->field[8]->store(((key_part->key_part_flag &
3389
                                     HA_REVERSE_SORT) ?
3390
                                    "D" : "A"), 1, cs);
3391
            table->field[8]->set_notnull();
3392
          }
3393
          KEY *key=show_table->key_info+i;
3394
          if (key->rec_per_key[j])
3395
          {
3396
            ha_rows records=(show_table->file->stats.records /
3397
                             key->rec_per_key[j]);
163 by Brian Aker
Merge Monty's code.
3398
            table->field[9]->store((int64_t) records, true);
1 by brian
clean slate
3399
            table->field[9]->set_notnull();
3400
          }
3401
          str= show_table->file->index_type(i);
3402
          table->field[13]->store(str, strlen(str), cs);
3403
        }
3404
        if ((key_part->field &&
3405
             key_part->length !=
3406
             show_table->s->field[key_part->fieldnr-1]->key_length()))
3407
        {
152 by Brian Aker
longlong replacement
3408
          table->field[10]->store((int64_t) key_part->length /
163 by Brian Aker
Merge Monty's code.
3409
                                  key_part->field->charset()->mbmaxlen, true);
1 by brian
clean slate
3410
          table->field[10]->set_notnull();
3411
        }
482 by Brian Aker
Remove uint.
3412
        uint32_t flags= key_part->field ? key_part->field->flags : 0;
1 by brian
clean slate
3413
        const char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
3414
        table->field[12]->store(pos, strlen(pos), cs);
3415
        if (!show_table->s->keys_in_use.is_set(i))
3416
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3417
        else
3418
          table->field[14]->store("", 0, cs);
3419
        table->field[14]->set_notnull();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3420
        assert(test(key_info->flags & HA_USES_COMMENT) ==
1 by brian
clean slate
3421
                   (key_info->comment.length > 0));
3422
        if (key_info->flags & HA_USES_COMMENT)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3423
          table->field[15]->store(key_info->comment.str,
1 by brian
clean slate
3424
                                  key_info->comment.length, cs);
520.1.22 by Brian Aker
Second pass of thd cleanup
3425
        if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3426
          return(1);
1 by brian
clean slate
3427
      }
3428
    }
3429
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3430
  return(res);
1 by brian
clean slate
3431
}
3432
3433
520.1.22 by Brian Aker
Second pass of thd cleanup
3434
bool store_constraints(Session *session, Table *table, LEX_STRING *db_name,
1 by brian
clean slate
3435
                       LEX_STRING *table_name, const char *key_name,
482 by Brian Aker
Remove uint.
3436
                       uint32_t key_len, const char *con_type, uint32_t con_len)
1 by brian
clean slate
3437
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3438
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
3439
  restore_record(table, s->default_values);
3440
  table->field[1]->store(db_name->str, db_name->length, cs);
3441
  table->field[2]->store(key_name, key_len, cs);
3442
  table->field[3]->store(db_name->str, db_name->length, cs);
3443
  table->field[4]->store(table_name->str, table_name->length, cs);
3444
  table->field[5]->store(con_type, con_len, cs);
520.1.22 by Brian Aker
Second pass of thd cleanup
3445
  return schema_table_store_record(session, table);
1 by brian
clean slate
3446
}
3447
3448
520.1.22 by Brian Aker
Second pass of thd cleanup
3449
static int get_schema_constraints_record(Session *session, TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3450
					 Table *table, bool res,
1 by brian
clean slate
3451
					 LEX_STRING *db_name,
3452
					 LEX_STRING *table_name)
3453
{
3454
  if (res)
3455
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
3456
    if (session->is_error())
3457
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3458
                   session->main_da.sql_errno(), session->main_da.message());
3459
    session->clear_error();
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3460
    return(0);
1 by brian
clean slate
3461
  }
3462
  else
3463
  {
3464
    List<FOREIGN_KEY_INFO> f_key_list;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3465
    Table *show_table= tables->table;
1 by brian
clean slate
3466
    KEY *key_info=show_table->key_info;
482 by Brian Aker
Remove uint.
3467
    uint32_t primary_key= show_table->s->primary_key;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3468
    show_table->file->info(HA_STATUS_VARIABLE |
1 by brian
clean slate
3469
                           HA_STATUS_NO_LOCK |
3470
                           HA_STATUS_TIME);
482 by Brian Aker
Remove uint.
3471
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
1 by brian
clean slate
3472
    {
3473
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3474
        continue;
3475
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
3476
      if (i == primary_key && is_primary_key(key_info))
1 by brian
clean slate
3477
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
3478
        if (store_constraints(session, table, db_name, table_name, key_info->name,
1 by brian
clean slate
3479
                              strlen(key_info->name),
3480
                              STRING_WITH_LEN("PRIMARY KEY")))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3481
          return(1);
1 by brian
clean slate
3482
      }
3483
      else if (key_info->flags & HA_NOSAME)
3484
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
3485
        if (store_constraints(session, table, db_name, table_name, key_info->name,
1 by brian
clean slate
3486
                              strlen(key_info->name),
3487
                              STRING_WITH_LEN("UNIQUE")))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3488
          return(1);
1 by brian
clean slate
3489
      }
3490
    }
3491
520.1.22 by Brian Aker
Second pass of thd cleanup
3492
    show_table->file->get_foreign_key_list(session, &f_key_list);
1 by brian
clean slate
3493
    FOREIGN_KEY_INFO *f_key_info;
3494
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3495
    while ((f_key_info=it++))
3496
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3497
      if (store_constraints(session, table, db_name, table_name,
1 by brian
clean slate
3498
                            f_key_info->forein_id->str,
3499
                            strlen(f_key_info->forein_id->str),
3500
                            "FOREIGN KEY", 11))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3501
        return(1);
1 by brian
clean slate
3502
    }
3503
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3504
  return(res);
1 by brian
clean slate
3505
}
3506
3507
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3508
void store_key_column_usage(Table *table, LEX_STRING *db_name,
1 by brian
clean slate
3509
                            LEX_STRING *table_name, const char *key_name,
482 by Brian Aker
Remove uint.
3510
                            uint32_t key_len, const char *con_type, uint32_t con_len,
152 by Brian Aker
longlong replacement
3511
                            int64_t idx)
1 by brian
clean slate
3512
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3513
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
3514
  table->field[1]->store(db_name->str, db_name->length, cs);
3515
  table->field[2]->store(key_name, key_len, cs);
3516
  table->field[4]->store(db_name->str, db_name->length, cs);
3517
  table->field[5]->store(table_name->str, table_name->length, cs);
3518
  table->field[6]->store(con_type, con_len, cs);
163 by Brian Aker
Merge Monty's code.
3519
  table->field[7]->store((int64_t) idx, true);
1 by brian
clean slate
3520
}
3521
3522
520.1.22 by Brian Aker
Second pass of thd cleanup
3523
static int get_schema_key_column_usage_record(Session *session,
327.2.4 by Brian Aker
Refactoring table.h
3524
					      TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3525
					      Table *table, bool res,
1 by brian
clean slate
3526
					      LEX_STRING *db_name,
3527
					      LEX_STRING *table_name)
3528
{
3529
  if (res)
3530
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
3531
    if (session->is_error())
3532
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3533
                   session->main_da.sql_errno(), session->main_da.message());
3534
    session->clear_error();
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3535
    return(0);
1 by brian
clean slate
3536
  }
3537
  else
3538
  {
3539
    List<FOREIGN_KEY_INFO> f_key_list;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3540
    Table *show_table= tables->table;
1 by brian
clean slate
3541
    KEY *key_info=show_table->key_info;
482 by Brian Aker
Remove uint.
3542
    uint32_t primary_key= show_table->s->primary_key;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3543
    show_table->file->info(HA_STATUS_VARIABLE |
1 by brian
clean slate
3544
                           HA_STATUS_NO_LOCK |
3545
                           HA_STATUS_TIME);
482 by Brian Aker
Remove uint.
3546
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
1 by brian
clean slate
3547
    {
3548
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3549
        continue;
482 by Brian Aker
Remove uint.
3550
      uint32_t f_idx= 0;
1 by brian
clean slate
3551
      KEY_PART_INFO *key_part= key_info->key_part;
482 by Brian Aker
Remove uint.
3552
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
1 by brian
clean slate
3553
      {
3554
        if (key_part->field)
3555
        {
3556
          f_idx++;
3557
          restore_record(table, s->default_values);
3558
          store_key_column_usage(table, db_name, table_name,
3559
                                 key_info->name,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3560
                                 strlen(key_info->name),
3561
                                 key_part->field->field_name,
1 by brian
clean slate
3562
                                 strlen(key_part->field->field_name),
152 by Brian Aker
longlong replacement
3563
                                 (int64_t) f_idx);
520.1.22 by Brian Aker
Second pass of thd cleanup
3564
          if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3565
            return(1);
1 by brian
clean slate
3566
        }
3567
      }
3568
    }
3569
520.1.22 by Brian Aker
Second pass of thd cleanup
3570
    show_table->file->get_foreign_key_list(session, &f_key_list);
1 by brian
clean slate
3571
    FOREIGN_KEY_INFO *f_key_info;
3572
    List_iterator_fast<FOREIGN_KEY_INFO> fkey_it(f_key_list);
3573
    while ((f_key_info= fkey_it++))
3574
    {
3575
      LEX_STRING *f_info;
3576
      LEX_STRING *r_info;
3577
      List_iterator_fast<LEX_STRING> it(f_key_info->foreign_fields),
3578
        it1(f_key_info->referenced_fields);
482 by Brian Aker
Remove uint.
3579
      uint32_t f_idx= 0;
1 by brian
clean slate
3580
      while ((f_info= it++))
3581
      {
3582
        r_info= it1++;
3583
        f_idx++;
3584
        restore_record(table, s->default_values);
3585
        store_key_column_usage(table, db_name, table_name,
3586
                               f_key_info->forein_id->str,
3587
                               f_key_info->forein_id->length,
3588
                               f_info->str, f_info->length,
152 by Brian Aker
longlong replacement
3589
                               (int64_t) f_idx);
163 by Brian Aker
Merge Monty's code.
3590
        table->field[8]->store((int64_t) f_idx, true);
1 by brian
clean slate
3591
        table->field[8]->set_notnull();
3592
        table->field[9]->store(f_key_info->referenced_db->str,
3593
                               f_key_info->referenced_db->length,
3594
                               system_charset_info);
3595
        table->field[9]->set_notnull();
3596
        table->field[10]->store(f_key_info->referenced_table->str,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3597
                                f_key_info->referenced_table->length,
1 by brian
clean slate
3598
                                system_charset_info);
3599
        table->field[10]->set_notnull();
3600
        table->field[11]->store(r_info->str, r_info->length,
3601
                                system_charset_info);
3602
        table->field[11]->set_notnull();
520.1.22 by Brian Aker
Second pass of thd cleanup
3603
        if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3604
          return(1);
1 by brian
clean slate
3605
      }
3606
    }
3607
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3608
  return(res);
1 by brian
clean slate
3609
}
3610
3611
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3612
int fill_open_tables(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3613
{
520.1.22 by Brian Aker
Second pass of thd cleanup
3614
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3615
  Table *table= tables->table;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3616
  const CHARSET_INFO * const cs= system_charset_info;
327.2.4 by Brian Aker
Refactoring table.h
3617
  OPEN_TableList *open_list;
520.1.22 by Brian Aker
Second pass of thd cleanup
3618
  if (!(open_list=list_open_tables(session,session->lex->select_lex.db, wild))
3619
            && session->is_fatal_error)
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3620
    return(1);
1 by brian
clean slate
3621
3622
  for (; open_list ; open_list=open_list->next)
3623
  {
3624
    restore_record(table, s->default_values);
3625
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
3626
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
163 by Brian Aker
Merge Monty's code.
3627
    table->field[2]->store((int64_t) open_list->in_use, true);
3628
    table->field[3]->store((int64_t) open_list->locked, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
3629
    if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3630
      return(1);
1 by brian
clean slate
3631
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3632
  return(0);
1 by brian
clean slate
3633
}
3634
3635
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3636
int fill_variables(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3637
{
3638
  int res= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
3639
  LEX *lex= session->lex;
461 by Monty Taylor
Removed NullS. bu-bye.
3640
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
1 by brian
clean slate
3641
  enum enum_schema_tables schema_table_idx=
3642
    get_schema_table_idx(tables->schema_table);
3643
  enum enum_var_type option_type= OPT_SESSION;
3644
  bool upper_case_names= (schema_table_idx != SCH_VARIABLES);
3645
  bool sorted_vars= (schema_table_idx == SCH_VARIABLES);
3646
3647
  if (lex->option_type == OPT_GLOBAL ||
3648
      schema_table_idx == SCH_GLOBAL_VARIABLES)
3649
    option_type= OPT_GLOBAL;
3650
658 by Brian Aker
Part removal of my_pthread.h
3651
  pthread_rwlock_rdlock(&LOCK_system_variables_hash);
520.1.22 by Brian Aker
Second pass of thd cleanup
3652
  res= show_status_array(session, wild, enumerate_sys_vars(session, sorted_vars),
1 by brian
clean slate
3653
                         option_type, NULL, "", tables->table, upper_case_names);
658 by Brian Aker
Part removal of my_pthread.h
3654
  pthread_rwlock_unlock(&LOCK_system_variables_hash);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3655
  return(res);
1 by brian
clean slate
3656
}
3657
3658
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3659
int fill_status(Session *session, TableList *tables, COND *)
1 by brian
clean slate
3660
{
520.1.22 by Brian Aker
Second pass of thd cleanup
3661
  LEX *lex= session->lex;
461 by Monty Taylor
Removed NullS. bu-bye.
3662
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
1 by brian
clean slate
3663
  int res= 0;
3664
  STATUS_VAR *tmp1, tmp;
3665
  enum enum_schema_tables schema_table_idx=
3666
    get_schema_table_idx(tables->schema_table);
3667
  enum enum_var_type option_type;
3668
  bool upper_case_names= (schema_table_idx != SCH_STATUS);
3669
3670
  if (schema_table_idx == SCH_STATUS)
3671
  {
3672
    option_type= lex->option_type;
3673
    if (option_type == OPT_GLOBAL)
3674
      tmp1= &tmp;
3675
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
3676
      tmp1= session->initial_status_var;
1 by brian
clean slate
3677
  }
3678
  else if (schema_table_idx == SCH_GLOBAL_STATUS)
3679
  {
3680
    option_type= OPT_GLOBAL;
3681
    tmp1= &tmp;
3682
  }
3683
  else
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3684
  {
1 by brian
clean slate
3685
    option_type= OPT_SESSION;
520.1.22 by Brian Aker
Second pass of thd cleanup
3686
    tmp1= &session->status_var;
1 by brian
clean slate
3687
  }
3688
3689
  pthread_mutex_lock(&LOCK_status);
3690
  if (option_type == OPT_GLOBAL)
3691
    calc_sum_of_all_status(&tmp);
520.1.22 by Brian Aker
Second pass of thd cleanup
3692
  res= show_status_array(session, wild,
1 by brian
clean slate
3693
                         (SHOW_VAR *)all_status_vars.buffer,
3694
                         option_type, tmp1, "", tables->table,
3695
                         upper_case_names);
3696
  pthread_mutex_unlock(&LOCK_status);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3697
  return(res);
1 by brian
clean slate
3698
}
3699
3700
3701
/*
3702
  Fill and store records into I_S.referential_constraints table
3703
3704
  SYNOPSIS
3705
    get_referential_constraints_record()
520.1.22 by Brian Aker
Second pass of thd cleanup
3706
    session                 thread handle
1 by brian
clean slate
3707
    tables              table list struct(processed table)
3708
    table               I_S table
3709
    res                 1 means the error during opening of the processed table
3710
                        0 means processed table is opened without error
3711
    base_name           db name
3712
    file_name           table name
3713
3714
  RETURN
3715
    0	ok
3716
    #   error
3717
*/
3718
3719
static int
520.1.22 by Brian Aker
Second pass of thd cleanup
3720
get_referential_constraints_record(Session *session, TableList *tables,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3721
                                   Table *table, bool res,
1 by brian
clean slate
3722
                                   LEX_STRING *db_name, LEX_STRING *table_name)
3723
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3724
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
3725
3726
  if (res)
3727
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
3728
    if (session->is_error())
3729
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3730
                   session->main_da.sql_errno(), session->main_da.message());
3731
    session->clear_error();
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3732
    return(0);
1 by brian
clean slate
3733
  }
3734
3735
  {
3736
    List<FOREIGN_KEY_INFO> f_key_list;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3737
    Table *show_table= tables->table;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3738
    show_table->file->info(HA_STATUS_VARIABLE |
1 by brian
clean slate
3739
                           HA_STATUS_NO_LOCK |
3740
                           HA_STATUS_TIME);
3741
520.1.22 by Brian Aker
Second pass of thd cleanup
3742
    show_table->file->get_foreign_key_list(session, &f_key_list);
1 by brian
clean slate
3743
    FOREIGN_KEY_INFO *f_key_info;
3744
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3745
    while ((f_key_info= it++))
3746
    {
3747
      restore_record(table, s->default_values);
3748
      table->field[1]->store(db_name->str, db_name->length, cs);
3749
      table->field[9]->store(table_name->str, table_name->length, cs);
3750
      table->field[2]->store(f_key_info->forein_id->str,
3751
                             f_key_info->forein_id->length, cs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3752
      table->field[4]->store(f_key_info->referenced_db->str,
1 by brian
clean slate
3753
                             f_key_info->referenced_db->length, cs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3754
      table->field[10]->store(f_key_info->referenced_table->str,
1 by brian
clean slate
3755
                             f_key_info->referenced_table->length, cs);
3756
      if (f_key_info->referenced_key_name)
3757
      {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3758
        table->field[5]->store(f_key_info->referenced_key_name->str,
1 by brian
clean slate
3759
                               f_key_info->referenced_key_name->length, cs);
3760
        table->field[5]->set_notnull();
3761
      }
3762
      else
3763
        table->field[5]->set_null();
3764
      table->field[6]->store(STRING_WITH_LEN("NONE"), cs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3765
      table->field[7]->store(f_key_info->update_method->str,
1 by brian
clean slate
3766
                             f_key_info->update_method->length, cs);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3767
      table->field[8]->store(f_key_info->delete_method->str,
1 by brian
clean slate
3768
                             f_key_info->delete_method->length, cs);
520.1.22 by Brian Aker
Second pass of thd cleanup
3769
      if (schema_table_store_record(session, table))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3770
        return(1);
1 by brian
clean slate
3771
    }
3772
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3773
  return(0);
1 by brian
clean slate
3774
}
3775
3776
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3777
struct schema_table_ref
1 by brian
clean slate
3778
{
3779
  const char *table_name;
3780
  ST_SCHEMA_TABLE *schema_table;
3781
};
3782
3783
3784
/*
3785
  Find schema_tables elment by name
3786
3787
  SYNOPSIS
3788
    find_schema_table_in_plugin()
520.1.22 by Brian Aker
Second pass of thd cleanup
3789
    session                 thread handler
1 by brian
clean slate
3790
    plugin              plugin
3791
    table_name          table name
3792
3793
  RETURN
3794
    0	table not found
3795
    1   found the schema table
3796
*/
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
3797
static bool find_schema_table_in_plugin(Session *, plugin_ref plugin,
3798
                                        void* p_table)
1 by brian
clean slate
3799
{
3800
  schema_table_ref *p_schema_table= (schema_table_ref *)p_table;
3801
  const char* table_name= p_schema_table->table_name;
3802
  ST_SCHEMA_TABLE *schema_table= plugin_data(plugin, ST_SCHEMA_TABLE *);
3803
3804
  if (!my_strcasecmp(system_charset_info,
3805
                     schema_table->table_name,
3806
                     table_name)) {
3807
    p_schema_table->schema_table= schema_table;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3808
    return(1);
1 by brian
clean slate
3809
  }
3810
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3811
  return(0);
1 by brian
clean slate
3812
}
3813
3814
3815
/*
3816
  Find schema_tables elment by name
3817
3818
  SYNOPSIS
3819
    find_schema_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
3820
    session                 thread handler
1 by brian
clean slate
3821
    table_name          table name
3822
3823
  RETURN
3824
    0	table not found
3825
    #   pointer to 'schema_tables' element
3826
*/
3827
520.1.22 by Brian Aker
Second pass of thd cleanup
3828
ST_SCHEMA_TABLE *find_schema_table(Session *session, const char* table_name)
1 by brian
clean slate
3829
{
3830
  schema_table_ref schema_table_a;
3831
  ST_SCHEMA_TABLE *schema_table= schema_tables;
3832
3833
  for (; schema_table->table_name; schema_table++)
3834
  {
3835
    if (!my_strcasecmp(system_charset_info,
3836
                       schema_table->table_name,
3837
                       table_name))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3838
      return(schema_table);
1 by brian
clean slate
3839
  }
3840
3841
  schema_table_a.table_name= table_name;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3842
  if (plugin_foreach(session, find_schema_table_in_plugin,
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
3843
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &schema_table_a))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3844
    return(schema_table_a.schema_table);
1 by brian
clean slate
3845
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3846
  return(NULL);
1 by brian
clean slate
3847
}
3848
3849
3850
ST_SCHEMA_TABLE *get_schema_table(enum enum_schema_tables schema_table_idx)
3851
{
3852
  return &schema_tables[schema_table_idx];
3853
}
3854
3855
3856
/**
3857
  Create information_schema table using schema_table data.
3858
3859
  @note
3860
3861
  @param
520.1.22 by Brian Aker
Second pass of thd cleanup
3862
    session	       	          thread handler
1 by brian
clean slate
3863
3864
  @param table_list Used to pass I_S table information(fields info, tables
3865
  parameters etc) and table name.
3866
3867
  @retval  \#             Pointer to created table
3868
  @retval  NULL           Can't create table
3869
*/
3870
520.1.22 by Brian Aker
Second pass of thd cleanup
3871
Table *create_schema_table(Session *session, TableList *table_list)
1 by brian
clean slate
3872
{
3873
  int field_count= 0;
3874
  Item *item;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3875
  Table *table;
1 by brian
clean slate
3876
  List<Item> field_list;
3877
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
3878
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
3879
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
3880
3881
  for (; fields_info->field_name; fields_info++)
3882
  {
3883
    switch (fields_info->field_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3884
    case DRIZZLE_TYPE_LONG:
3885
    case DRIZZLE_TYPE_LONGLONG:
1 by brian
clean slate
3886
      if (!(item= new Item_return_int(fields_info->field_name,
3887
                                      fields_info->field_length,
3888
                                      fields_info->field_type,
3889
                                      fields_info->value)))
3890
      {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3891
        return(0);
1 by brian
clean slate
3892
      }
3893
      item->unsigned_flag= (fields_info->field_flags & MY_I_S_UNSIGNED);
3894
      break;
575.5.1 by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code
3895
    case DRIZZLE_TYPE_DATE:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3896
    case DRIZZLE_TYPE_TIME:
3897
    case DRIZZLE_TYPE_TIMESTAMP:
3898
    case DRIZZLE_TYPE_DATETIME:
1 by brian
clean slate
3899
      if (!(item=new Item_return_date_time(fields_info->field_name,
3900
                                           fields_info->field_type)))
3901
      {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3902
        return(0);
1 by brian
clean slate
3903
      }
3904
      break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3905
    case DRIZZLE_TYPE_DOUBLE:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3906
      if ((item= new Item_float(fields_info->field_name, 0.0, NOT_FIXED_DEC,
1 by brian
clean slate
3907
                           fields_info->field_length)) == NULL)
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3908
        return(NULL);
1 by brian
clean slate
3909
      break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3910
    case DRIZZLE_TYPE_NEWDECIMAL:
152 by Brian Aker
longlong replacement
3911
      if (!(item= new Item_decimal((int64_t) fields_info->value, false)))
1 by brian
clean slate
3912
      {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3913
        return(0);
1 by brian
clean slate
3914
      }
3915
      item->unsigned_flag= (fields_info->field_flags & MY_I_S_UNSIGNED);
3916
      item->decimals= fields_info->field_length%10;
3917
      item->max_length= (fields_info->field_length/100)%100;
3918
      if (item->unsigned_flag == 0)
3919
        item->max_length+= 1;
3920
      if (item->decimals > 0)
3921
        item->max_length+= 1;
3922
      item->set_name(fields_info->field_name,
3923
                     strlen(fields_info->field_name), cs);
3924
      break;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
3925
    case DRIZZLE_TYPE_BLOB:
1 by brian
clean slate
3926
      if (!(item= new Item_blob(fields_info->field_name,
3927
                                fields_info->field_length)))
3928
      {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3929
        return(0);
1 by brian
clean slate
3930
      }
3931
      break;
3932
    default:
3933
      if (!(item= new Item_empty_string("", fields_info->field_length, cs)))
3934
      {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3935
        return(0);
1 by brian
clean slate
3936
      }
3937
      item->set_name(fields_info->field_name,
3938
                     strlen(fields_info->field_name), cs);
3939
      break;
3940
    }
3941
    field_list.push_back(item);
3942
    item->maybe_null= (fields_info->field_flags & MY_I_S_MAYBE_NULL);
3943
    field_count++;
3944
  }
3945
  TMP_TABLE_PARAM *tmp_table_param =
520.1.22 by Brian Aker
Second pass of thd cleanup
3946
    (TMP_TABLE_PARAM*) (session->alloc(sizeof(TMP_TABLE_PARAM)));
1 by brian
clean slate
3947
  tmp_table_param->init();
3948
  tmp_table_param->table_charset= cs;
3949
  tmp_table_param->field_count= field_count;
3950
  tmp_table_param->schema_table= 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
3951
  SELECT_LEX *select_lex= session->lex->current_select;
3952
  if (!(table= create_tmp_table(session, tmp_table_param,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3953
                                field_list, (order_st*) 0, 0, 0,
520.1.22 by Brian Aker
Second pass of thd cleanup
3954
                                (select_lex->options | session->options |
1 by brian
clean slate
3955
                                 TMP_TABLE_ALL_COLUMNS),
3956
                                HA_POS_ERROR, table_list->alias)))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3957
    return(0);
1 by brian
clean slate
3958
  my_bitmap_map* bitmaps=
520.1.22 by Brian Aker
Second pass of thd cleanup
3959
    (my_bitmap_map*) session->alloc(bitmap_buffer_size(field_count));
1 by brian
clean slate
3960
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
163 by Brian Aker
Merge Monty's code.
3961
              false);
1 by brian
clean slate
3962
  table->read_set= &table->def_read_set;
3963
  bitmap_clear_all(table->read_set);
3964
  table_list->schema_table_param= tmp_table_param;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
3965
  return(table);
1 by brian
clean slate
3966
}
3967
3968
3969
/*
3970
  For old SHOW compatibility. It is used when
3971
  old SHOW doesn't have generated column names
3972
  Make list of fields for SHOW
3973
3974
  SYNOPSIS
3975
    make_old_format()
520.1.22 by Brian Aker
Second pass of thd cleanup
3976
    session			thread handler
1 by brian
clean slate
3977
    schema_table        pointer to 'schema_tables' element
3978
3979
  RETURN
3980
   1	error
3981
   0	success
3982
*/
3983
520.1.22 by Brian Aker
Second pass of thd cleanup
3984
int make_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
1 by brian
clean slate
3985
{
3986
  ST_FIELD_INFO *field_info= schema_table->fields_info;
520.1.22 by Brian Aker
Second pass of thd cleanup
3987
  Name_resolution_context *context= &session->lex->select_lex.context;
1 by brian
clean slate
3988
  for (; field_info->field_name; field_info++)
3989
  {
3990
    if (field_info->old_name)
3991
    {
3992
      Item_field *field= new Item_field(context,
461 by Monty Taylor
Removed NullS. bu-bye.
3993
                                        NULL, NULL, field_info->field_name);
1 by brian
clean slate
3994
      if (field)
3995
      {
3996
        field->set_name(field_info->old_name,
3997
                        strlen(field_info->old_name),
3998
                        system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
3999
        if (add_item_to_list(session, field))
1 by brian
clean slate
4000
          return 1;
4001
      }
4002
    }
4003
  }
4004
  return 0;
4005
}
4006
4007
520.1.22 by Brian Aker
Second pass of thd cleanup
4008
int make_schemata_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
1 by brian
clean slate
4009
{
4010
  char tmp[128];
520.1.22 by Brian Aker
Second pass of thd cleanup
4011
  LEX *lex= session->lex;
1 by brian
clean slate
4012
  SELECT_LEX *sel= lex->current_select;
4013
  Name_resolution_context *context= &sel->context;
4014
4015
  if (!sel->item_list.elements)
4016
  {
4017
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
4018
    String buffer(tmp,sizeof(tmp), system_charset_info);
4019
    Item_field *field= new Item_field(context,
461 by Monty Taylor
Removed NullS. bu-bye.
4020
                                      NULL, NULL, field_info->field_name);
520.1.22 by Brian Aker
Second pass of thd cleanup
4021
    if (!field || add_item_to_list(session, field))
1 by brian
clean slate
4022
      return 1;
4023
    buffer.length(0);
4024
    buffer.append(field_info->old_name);
4025
    if (lex->wild && lex->wild->ptr())
4026
    {
4027
      buffer.append(STRING_WITH_LEN(" ("));
4028
      buffer.append(lex->wild->ptr());
4029
      buffer.append(')');
4030
    }
4031
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4032
  }
4033
  return 0;
4034
}
4035
4036
520.1.22 by Brian Aker
Second pass of thd cleanup
4037
int make_table_names_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
1 by brian
clean slate
4038
{
4039
  char tmp[128];
520.1.22 by Brian Aker
Second pass of thd cleanup
4040
  String buffer(tmp,sizeof(tmp), session->charset());
4041
  LEX *lex= session->lex;
1 by brian
clean slate
4042
  Name_resolution_context *context= &lex->select_lex.context;
4043
4044
  ST_FIELD_INFO *field_info= &schema_table->fields_info[2];
4045
  buffer.length(0);
4046
  buffer.append(field_info->old_name);
4047
  buffer.append(lex->select_lex.db);
4048
  if (lex->wild && lex->wild->ptr())
4049
  {
4050
    buffer.append(STRING_WITH_LEN(" ("));
4051
    buffer.append(lex->wild->ptr());
4052
    buffer.append(')');
4053
  }
4054
  Item_field *field= new Item_field(context,
461 by Monty Taylor
Removed NullS. bu-bye.
4055
                                    NULL, NULL, field_info->field_name);
520.1.22 by Brian Aker
Second pass of thd cleanup
4056
  if (add_item_to_list(session, field))
1 by brian
clean slate
4057
    return 1;
4058
  field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
4059
  if (session->lex->verbose)
1 by brian
clean slate
4060
  {
4061
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4062
    field_info= &schema_table->fields_info[3];
461 by Monty Taylor
Removed NullS. bu-bye.
4063
    field= new Item_field(context, NULL, NULL, field_info->field_name);
520.1.22 by Brian Aker
Second pass of thd cleanup
4064
    if (add_item_to_list(session, field))
1 by brian
clean slate
4065
      return 1;
4066
    field->set_name(field_info->old_name, strlen(field_info->old_name),
4067
                    system_charset_info);
4068
  }
4069
  return 0;
4070
}
4071
4072
520.1.22 by Brian Aker
Second pass of thd cleanup
4073
int make_columns_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
1 by brian
clean slate
4074
{
4075
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
4076
  int *field_num= fields_arr;
4077
  ST_FIELD_INFO *field_info;
520.1.22 by Brian Aker
Second pass of thd cleanup
4078
  Name_resolution_context *context= &session->lex->select_lex.context;
1 by brian
clean slate
4079
4080
  for (; *field_num >= 0; field_num++)
4081
  {
4082
    field_info= &schema_table->fields_info[*field_num];
520.1.22 by Brian Aker
Second pass of thd cleanup
4083
    if (!session->lex->verbose && (*field_num == 13 ||
1 by brian
clean slate
4084
                               *field_num == 17 ||
4085
                               *field_num == 18))
4086
      continue;
4087
    Item_field *field= new Item_field(context,
461 by Monty Taylor
Removed NullS. bu-bye.
4088
                                      NULL, NULL, field_info->field_name);
1 by brian
clean slate
4089
    if (field)
4090
    {
4091
      field->set_name(field_info->old_name,
4092
                      strlen(field_info->old_name),
4093
                      system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
4094
      if (add_item_to_list(session, field))
1 by brian
clean slate
4095
        return 1;
4096
    }
4097
  }
4098
  return 0;
4099
}
4100
4101
520.1.22 by Brian Aker
Second pass of thd cleanup
4102
int make_character_sets_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
1 by brian
clean slate
4103
{
4104
  int fields_arr[]= {0, 2, 1, 3, -1};
4105
  int *field_num= fields_arr;
4106
  ST_FIELD_INFO *field_info;
520.1.22 by Brian Aker
Second pass of thd cleanup
4107
  Name_resolution_context *context= &session->lex->select_lex.context;
1 by brian
clean slate
4108
4109
  for (; *field_num >= 0; field_num++)
4110
  {
4111
    field_info= &schema_table->fields_info[*field_num];
4112
    Item_field *field= new Item_field(context,
461 by Monty Taylor
Removed NullS. bu-bye.
4113
                                      NULL, NULL, field_info->field_name);
1 by brian
clean slate
4114
    if (field)
4115
    {
4116
      field->set_name(field_info->old_name,
4117
                      strlen(field_info->old_name),
4118
                      system_charset_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
4119
      if (add_item_to_list(session, field))
1 by brian
clean slate
4120
        return 1;
4121
    }
4122
  }
4123
  return 0;
4124
}
4125
4126
4127
/*
4128
  Create information_schema table
4129
4130
  SYNOPSIS
4131
  mysql_schema_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
4132
    session                thread handler
1 by brian
clean slate
4133
    lex                pointer to LEX
4134
    table_list         pointer to table_list
4135
4136
  RETURN
4137
    0	success
4138
    1   error
4139
*/
4140
769 by Brian Aker
Removing dead field translator code.
4141
int mysql_schema_table(Session *session, LEX *, TableList *table_list)
1 by brian
clean slate
4142
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
4143
  Table *table;
520.1.22 by Brian Aker
Second pass of thd cleanup
4144
  if (!(table= table_list->schema_table->create_table(session, table_list)))
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4145
    return(1);
1 by brian
clean slate
4146
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4147
  /*
4148
    This test is necessary to make
4149
    case insensitive file systems +
4150
    upper case table names(information schema tables) +
4151
    views
4152
    working correctly
4153
  */
4154
  if (table_list->schema_table_name)
4155
    table->alias_name_used= my_strcasecmp(table_alias_charset,
4156
                                          table_list->schema_table_name,
4157
                                          table_list->alias);
4158
  table_list->table_name= table->s->table_name.str;
4159
  table_list->table_name_length= table->s->table_name.length;
4160
  table_list->table= table;
520.1.22 by Brian Aker
Second pass of thd cleanup
4161
  table->next= session->derived_tables;
4162
  session->derived_tables= table;
1 by brian
clean slate
4163
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4164
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4165
  return(0);
1 by brian
clean slate
4166
}
4167
4168
4169
/*
4170
  Generate select from information_schema table
4171
4172
  SYNOPSIS
4173
    make_schema_select()
520.1.22 by Brian Aker
Second pass of thd cleanup
4174
    session                  thread handler
1 by brian
clean slate
4175
    sel                  pointer to SELECT_LEX
4176
    schema_table_idx     index of 'schema_tables' element
4177
4178
  RETURN
4179
    0	success
4180
    1   error
4181
*/
4182
520.1.22 by Brian Aker
Second pass of thd cleanup
4183
int make_schema_select(Session *session, SELECT_LEX *sel,
1 by brian
clean slate
4184
		       enum enum_schema_tables schema_table_idx)
4185
{
4186
  ST_SCHEMA_TABLE *schema_table= get_schema_table(schema_table_idx);
4187
  LEX_STRING db, table;
4188
  /*
4189
     We have to make non const db_name & table_name
4190
     because of lower_case_table_names
4191
  */
575.4.7 by Monty Taylor
More header cleanup.
4192
  session->make_lex_string(&db, INFORMATION_SCHEMA_NAME.c_str(),
4193
                       INFORMATION_SCHEMA_NAME.length(), 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
4194
  session->make_lex_string(&table, schema_table->table_name,
1 by brian
clean slate
4195
                       strlen(schema_table->table_name), 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
4196
  if (schema_table->old_format(session, schema_table) ||   /* Handle old syntax */
4197
      !sel->add_table_to_list(session, new Table_ident(session, db, table, 0),
1 by brian
clean slate
4198
                              0, 0, TL_READ))
4199
  {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4200
    return(1);
1 by brian
clean slate
4201
  }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4202
  return(0);
1 by brian
clean slate
4203
}
4204
4205
4206
/*
4207
  Fill temporary schema tables before SELECT
4208
4209
  SYNOPSIS
4210
    get_schema_tables_result()
4211
    join  join which use schema tables
4212
    executed_place place where I_S table processed
4213
4214
  RETURN
163 by Brian Aker
Merge Monty's code.
4215
    false success
4216
    true  error
1 by brian
clean slate
4217
*/
4218
4219
bool get_schema_tables_result(JOIN *join,
4220
                              enum enum_schema_table_state executed_place)
4221
{
4222
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
520.1.22 by Brian Aker
Second pass of thd cleanup
4223
  Session *session= join->session;
4224
  LEX *lex= session->lex;
1 by brian
clean slate
4225
  bool result= 0;
4226
520.1.22 by Brian Aker
Second pass of thd cleanup
4227
  session->no_warnings_for_error= 1;
1 by brian
clean slate
4228
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
4229
  {
4230
    if (!tab->table || !tab->table->pos_in_table_list)
4231
      break;
4232
327.2.4 by Brian Aker
Refactoring table.h
4233
    TableList *table_list= tab->table->pos_in_table_list;
1 by brian
clean slate
4234
    if (table_list->schema_table)
4235
    {
4236
      bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
4237
                          lex->current_select->master_unit()->item);
4238
4239
4240
      /* skip I_S optimizations specific to get_all_tables */
520.1.22 by Brian Aker
Second pass of thd cleanup
4241
      if (session->lex->describe &&
1 by brian
clean slate
4242
          (table_list->schema_table->fill_table != get_all_tables))
4243
        continue;
4244
4245
      /*
4246
        If schema table is already processed and
4247
        the statement is not a subselect then
4248
        we don't need to fill this table again.
4249
        If schema table is already processed and
4250
        schema_table_state != executed_place then
4251
        table is already processed and
4252
        we should skip second data processing.
4253
      */
4254
      if (table_list->schema_table_state &&
4255
          (!is_subselect || table_list->schema_table_state != executed_place))
4256
        continue;
4257
4258
      /*
4259
        if table is used in a subselect and
4260
        table has been processed earlier with the same
4261
        'executed_place' value then we should refresh the table.
4262
      */
4263
      if (table_list->schema_table_state && is_subselect)
4264
      {
4265
        table_list->table->file->extra(HA_EXTRA_NO_CACHE);
4266
        table_list->table->file->extra(HA_EXTRA_RESET_STATE);
4267
        table_list->table->file->ha_delete_all_rows();
4268
        free_io_cache(table_list->table);
4269
        filesort_free_buffers(table_list->table,1);
4270
        table_list->table->null_row= 0;
4271
      }
4272
      else
4273
        table_list->table->file->stats.records= 0;
4274
520.1.22 by Brian Aker
Second pass of thd cleanup
4275
      if (table_list->schema_table->fill_table(session, table_list,
1 by brian
clean slate
4276
                                               tab->select_cond))
4277
      {
4278
        result= 1;
4279
        join->error= 1;
4280
        tab->read_record.file= table_list->table->file;
4281
        table_list->schema_table_state= executed_place;
4282
        break;
4283
      }
4284
      tab->read_record.file= table_list->table->file;
4285
      table_list->schema_table_state= executed_place;
4286
    }
4287
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
4288
  session->no_warnings_for_error= 0;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4289
  return(result);
1 by brian
clean slate
4290
}
4291
4292
ST_FIELD_INFO schema_fields_info[]=
4293
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4294
  {"CATALOG_NAME", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4295
  {"SCHEMA_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Database",
4296
   SKIP_OPEN_TABLE},
4297
  {"DEFAULT_CHARACTER_SET_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4298
   SKIP_OPEN_TABLE},
4299
  {"DEFAULT_COLLATION_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4300
  {"SQL_PATH", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4301
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4302
};
4303
4304
4305
ST_FIELD_INFO tables_fields_info[]=
4306
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4307
  {"TABLE_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4308
  {"TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4309
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Name",
1 by brian
clean slate
4310
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4311
  {"TABLE_TYPE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
4312
  {"ENGINE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, "Engine", OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4313
  {"VERSION", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4314
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Version", OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4315
  {"ROW_FORMAT", 10, DRIZZLE_TYPE_VARCHAR, 0, 1, "Row_format", OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4316
  {"TABLE_ROWS", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4317
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Rows", OPEN_FULL_TABLE},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4318
  {"AVG_ROW_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4319
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Avg_row_length", OPEN_FULL_TABLE},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4320
  {"DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4321
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Data_length", OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4322
  {"MAX_DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4323
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Max_data_length", OPEN_FULL_TABLE},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4324
  {"INDEX_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4325
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Index_length", OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4326
  {"DATA_FREE", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4327
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Data_free", OPEN_FULL_TABLE},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4328
  {"AUTO_INCREMENT", MY_INT64_NUM_DECIMAL_DIGITS , DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4329
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Auto_increment", OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4330
  {"CREATE_TIME", 0, DRIZZLE_TYPE_DATETIME, 0, 1, "Create_time", OPEN_FULL_TABLE},
4331
  {"UPDATE_TIME", 0, DRIZZLE_TYPE_DATETIME, 0, 1, "Update_time", OPEN_FULL_TABLE},
4332
  {"CHECK_TIME", 0, DRIZZLE_TYPE_DATETIME, 0, 1, "Check_time", OPEN_FULL_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4333
  {"TABLE_COLLATION", 64, DRIZZLE_TYPE_VARCHAR, 0, 1, "Collation", OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4334
  {"CHECKSUM", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4335
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Checksum", OPEN_FULL_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4336
  {"CREATE_OPTIONS", 255, DRIZZLE_TYPE_VARCHAR, 0, 1, "Create_options",
1 by brian
clean slate
4337
   OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4338
  {"TABLE_COMMENT", TABLE_COMMENT_MAXLEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Comment", OPEN_FRM_ONLY},
4339
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4340
};
4341
4342
4343
ST_FIELD_INFO columns_fields_info[]=
4344
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4345
  {"TABLE_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FRM_ONLY},
4346
  {"TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
4347
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
4348
  {"COLUMN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Field",
1 by brian
clean slate
4349
   OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4350
  {"ORDINAL_POSITION", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
1 by brian
clean slate
4351
   MY_I_S_UNSIGNED, 0, OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4352
  {"COLUMN_DEFAULT", MAX_FIELD_VARCHARLENGTH, DRIZZLE_TYPE_VARCHAR, 0,
1 by brian
clean slate
4353
   1, "Default", OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4354
  {"IS_NULLABLE", 3, DRIZZLE_TYPE_VARCHAR, 0, 0, "Null", OPEN_FRM_ONLY},
4355
  {"DATA_TYPE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4356
  {"CHARACTER_MAXIMUM_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG,
4357
   0, (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), 0, OPEN_FRM_ONLY},
4358
  {"CHARACTER_OCTET_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS , DRIZZLE_TYPE_LONGLONG,
4359
   0, (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), 0, OPEN_FRM_ONLY},
4360
  {"NUMERIC_PRECISION", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG,
4361
   0, (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), 0, OPEN_FRM_ONLY},
4362
  {"NUMERIC_SCALE", MY_INT64_NUM_DECIMAL_DIGITS , DRIZZLE_TYPE_LONGLONG,
4363
   0, (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), 0, OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4364
  {"CHARACTER_SET_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FRM_ONLY},
4365
  {"COLLATION_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 1, "Collation", OPEN_FRM_ONLY},
4366
  {"COLUMN_TYPE", 65535, DRIZZLE_TYPE_VARCHAR, 0, 0, "Type", OPEN_FRM_ONLY},
4367
  {"COLUMN_KEY", 3, DRIZZLE_TYPE_VARCHAR, 0, 0, "Key", OPEN_FRM_ONLY},
4368
  {"EXTRA", 27, DRIZZLE_TYPE_VARCHAR, 0, 0, "Extra", OPEN_FRM_ONLY},
4369
  {"PRIVILEGES", 80, DRIZZLE_TYPE_VARCHAR, 0, 0, "Privileges", OPEN_FRM_ONLY},
4370
  {"COLUMN_COMMENT", COLUMN_COMMENT_MAXLEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Comment", OPEN_FRM_ONLY},
4371
  {"STORAGE", 8, DRIZZLE_TYPE_VARCHAR, 0, 0, "Storage", OPEN_FRM_ONLY},
4372
  {"FORMAT", 8, DRIZZLE_TYPE_VARCHAR, 0, 0, "Format", OPEN_FRM_ONLY},
4373
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4374
};
4375
4376
4377
ST_FIELD_INFO charsets_fields_info[]=
4378
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4379
  {"CHARACTER_SET_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, "Charset",
4380
   SKIP_OPEN_TABLE},
4381
  {"DEFAULT_COLLATE_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, "Default collation",
4382
   SKIP_OPEN_TABLE},
4383
  {"DESCRIPTION", 60, DRIZZLE_TYPE_VARCHAR, 0, 0, "Description",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4384
   SKIP_OPEN_TABLE},
4385
  {"MAXLEN", 3, DRIZZLE_TYPE_LONGLONG, 0, 0, "Maxlen", SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4386
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4387
};
4388
4389
4390
ST_FIELD_INFO collation_fields_info[]=
4391
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4392
  {"COLLATION_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, "Collation", SKIP_OPEN_TABLE},
4393
  {"CHARACTER_SET_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, "Charset",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4394
   SKIP_OPEN_TABLE},
4395
  {"ID", MY_INT32_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0, 0, "Id",
4396
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4397
  {"IS_DEFAULT", 3, DRIZZLE_TYPE_VARCHAR, 0, 0, "Default", SKIP_OPEN_TABLE},
4398
  {"IS_COMPILED", 3, DRIZZLE_TYPE_VARCHAR, 0, 0, "Compiled", SKIP_OPEN_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4399
  {"SORTLEN", 3, DRIZZLE_TYPE_LONGLONG, 0, 0, "Sortlen", SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4400
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4401
};
4402
4403
4404
4405
ST_FIELD_INFO coll_charset_app_fields_info[]=
4406
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4407
  {"COLLATION_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4408
  {"CHARACTER_SET_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4409
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4410
};
4411
4412
4413
ST_FIELD_INFO stat_fields_info[]=
4414
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4415
  {"TABLE_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FRM_ONLY},
4416
  {"TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
4417
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Table", OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4418
  {"NON_UNIQUE", 1, DRIZZLE_TYPE_LONGLONG, 0, 0, "Non_unique", OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4419
  {"INDEX_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FRM_ONLY},
4420
  {"INDEX_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Key_name",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4421
   OPEN_FRM_ONLY},
4422
  {"SEQ_IN_INDEX", 2, DRIZZLE_TYPE_LONGLONG, 0, 0, "Seq_in_index", OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4423
  {"COLUMN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Column_name",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4424
   OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4425
  {"COLLATION", 1, DRIZZLE_TYPE_VARCHAR, 0, 1, "Collation", OPEN_FRM_ONLY},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4426
  {"CARDINALITY", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0, 1,
1 by brian
clean slate
4427
   "Cardinality", OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4428
  {"SUB_PART", 3, DRIZZLE_TYPE_LONGLONG, 0, 1, "Sub_part", OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4429
  {"PACKED", 10, DRIZZLE_TYPE_VARCHAR, 0, 1, "Packed", OPEN_FRM_ONLY},
4430
  {"NULLABLE", 3, DRIZZLE_TYPE_VARCHAR, 0, 0, "Null", OPEN_FRM_ONLY},
4431
  {"INDEX_TYPE", 16, DRIZZLE_TYPE_VARCHAR, 0, 0, "Index_type", OPEN_FULL_TABLE},
4432
  {"COMMENT", 16, DRIZZLE_TYPE_VARCHAR, 0, 1, "Comment", OPEN_FRM_ONLY},
4433
  {"INDEX_COMMENT", INDEX_COMMENT_MAXLEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Index_Comment", OPEN_FRM_ONLY},
4434
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4435
};
4436
4437
4438
ST_FIELD_INFO table_constraints_fields_info[]=
4439
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4440
  {"CONSTRAINT_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FULL_TABLE},
4441
  {"CONSTRAINT_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4442
   OPEN_FULL_TABLE},
4443
  {"CONSTRAINT_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4444
   OPEN_FULL_TABLE},
4445
  {"TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4446
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4447
  {"CONSTRAINT_TYPE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4448
   OPEN_FULL_TABLE},
4449
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4450
};
4451
4452
4453
ST_FIELD_INFO key_column_usage_fields_info[]=
4454
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4455
  {"CONSTRAINT_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FULL_TABLE},
4456
  {"CONSTRAINT_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4457
   OPEN_FULL_TABLE},
4458
  {"CONSTRAINT_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4459
   OPEN_FULL_TABLE},
4460
  {"TABLE_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FULL_TABLE},
4461
  {"TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4462
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4463
  {"COLUMN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4464
  {"ORDINAL_POSITION", 10 ,DRIZZLE_TYPE_LONGLONG, 0, 0, 0, OPEN_FULL_TABLE},
4465
  {"POSITION_IN_UNIQUE_CONSTRAINT", 10 ,DRIZZLE_TYPE_LONGLONG, 0, 1, 0,
4466
   OPEN_FULL_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4467
  {"REFERENCED_TABLE_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0,
4468
   OPEN_FULL_TABLE},
4469
  {"REFERENCED_TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0,
4470
   OPEN_FULL_TABLE},
4471
  {"REFERENCED_COLUMN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0,
4472
   OPEN_FULL_TABLE},
4473
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4474
};
4475
4476
4477
ST_FIELD_INFO table_names_fields_info[]=
4478
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4479
  {"TABLE_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4480
  {"TABLE_SCHEMA",NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4481
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Tables_in_",
1 by brian
clean slate
4482
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4483
  {"TABLE_TYPE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Table_type",
1 by brian
clean slate
4484
   OPEN_FRM_ONLY},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4485
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4486
};
4487
4488
4489
ST_FIELD_INFO open_tables_fields_info[]=
4490
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4491
  {"Database", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Database",
1 by brian
clean slate
4492
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4493
  {"Table",NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Table", SKIP_OPEN_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4494
  {"In_use", 1, DRIZZLE_TYPE_LONGLONG, 0, 0, "In_use", SKIP_OPEN_TABLE},
4495
  {"Name_locked", 4, DRIZZLE_TYPE_LONGLONG, 0, 0, "Name_locked", SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4496
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4497
};
4498
4499
4500
ST_FIELD_INFO variables_fields_info[]=
4501
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4502
  {"VARIABLE_NAME", 64, DRIZZLE_TYPE_VARCHAR, 0, 0, "Variable_name",
1 by brian
clean slate
4503
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4504
  {"VARIABLE_VALUE", 16300, DRIZZLE_TYPE_VARCHAR, 0, 1, "Value", SKIP_OPEN_TABLE},
4505
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4506
};
4507
4508
4509
ST_FIELD_INFO processlist_fields_info[]=
4510
{
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4511
  {"ID", 4, DRIZZLE_TYPE_LONGLONG, 0, 0, "Id", SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4512
  {"USER", 16, DRIZZLE_TYPE_VARCHAR, 0, 0, "User", SKIP_OPEN_TABLE},
4513
  {"HOST", LIST_PROCESS_HOST_LEN,  DRIZZLE_TYPE_VARCHAR, 0, 0, "Host",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4514
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4515
  {"DB", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, "Db", SKIP_OPEN_TABLE},
4516
  {"COMMAND", 16, DRIZZLE_TYPE_VARCHAR, 0, 0, "Command", SKIP_OPEN_TABLE},
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4517
  {"TIME", 7, DRIZZLE_TYPE_LONGLONG, 0, 0, "Time", SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4518
  {"STATE", 64, DRIZZLE_TYPE_VARCHAR, 0, 1, "State", SKIP_OPEN_TABLE},
4519
  {"INFO", PROCESS_LIST_INFO_WIDTH, DRIZZLE_TYPE_VARCHAR, 0, 1, "Info",
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
4520
   SKIP_OPEN_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4521
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4522
};
4523
4524
4525
ST_FIELD_INFO plugin_fields_info[]=
4526
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4527
  {"PLUGIN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Name",
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4528
   SKIP_OPEN_TABLE},
4529
  {"PLUGIN_VERSION", 20, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4530
  {"PLUGIN_STATUS", 10, DRIZZLE_TYPE_VARCHAR, 0, 0, "Status", SKIP_OPEN_TABLE},
4531
  {"PLUGIN_TYPE", 80, DRIZZLE_TYPE_VARCHAR, 0, 0, "Type", SKIP_OPEN_TABLE},
4532
  {"PLUGIN_LIBRARY", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, "Library",
4533
   SKIP_OPEN_TABLE},
4534
  {"PLUGIN_AUTHOR", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4535
  {"PLUGIN_DESCRIPTION", 65535, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4536
  {"PLUGIN_LICENSE", 80, DRIZZLE_TYPE_VARCHAR, 0, 1, "License", SKIP_OPEN_TABLE},
4537
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4538
};
4539
4540
ST_FIELD_INFO referential_constraints_fields_info[]=
4541
{
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4542
  {"CONSTRAINT_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, OPEN_FULL_TABLE},
4543
  {"CONSTRAINT_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4544
   OPEN_FULL_TABLE},
4545
  {"CONSTRAINT_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4546
   OPEN_FULL_TABLE},
4547
  {"UNIQUE_CONSTRAINT_CATALOG", FN_REFLEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0,
4548
   OPEN_FULL_TABLE},
4549
  {"UNIQUE_CONSTRAINT_SCHEMA", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
4550
   OPEN_FULL_TABLE},
4551
  {"UNIQUE_CONSTRAINT_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0,
1 by brian
clean slate
4552
   MY_I_S_MAYBE_NULL, 0, OPEN_FULL_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4553
  {"MATCH_OPTION", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4554
  {"UPDATE_RULE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4555
  {"DELETE_RULE", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4556
  {"TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, OPEN_FULL_TABLE},
4557
  {"REFERENCED_TABLE_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, 0,
1 by brian
clean slate
4558
   OPEN_FULL_TABLE},
238 by Brian Aker
Pass through sql_show to remove type to be removed.
4559
  {0, 0, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE}
1 by brian
clean slate
4560
};
4561
4562
4563
/*
4564
  Description of ST_FIELD_INFO in table.h
4565
4566
  Make sure that the order of schema_tables and enum_schema_tables are the same.
4567
4568
*/
4569
4570
ST_SCHEMA_TABLE schema_tables[]=
4571
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4572
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table,
1 by brian
clean slate
4573
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0, 0},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4574
  {"COLLATIONS", collation_fields_info, create_schema_table,
1 by brian
clean slate
4575
   fill_schema_collation, make_old_format, 0, -1, -1, 0, 0},
4576
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
4577
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0, 0},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4578
  {"COLUMNS", columns_fields_info, create_schema_table,
1 by brian
clean slate
4579
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0,
357 by Brian Aker
flag cleanup
4580
   OPTIMIZE_I_S_TABLE},
1 by brian
clean slate
4581
  {"GLOBAL_STATUS", variables_fields_info, create_schema_table,
4582
   fill_status, make_old_format, 0, -1, -1, 0, 0},
4583
  {"GLOBAL_VARIABLES", variables_fields_info, create_schema_table,
4584
   fill_variables, make_old_format, 0, -1, -1, 0, 0},
4585
  {"KEY_COLUMN_USAGE", key_column_usage_fields_info, create_schema_table,
4586
   get_all_tables, 0, get_schema_key_column_usage_record, 4, 5, 0,
4587
   OPEN_TABLE_ONLY},
4588
  {"OPEN_TABLES", open_tables_fields_info, create_schema_table,
4589
   fill_open_tables, make_old_format, 0, -1, -1, 1, 0},
4590
  {"PLUGINS", plugin_fields_info, create_schema_table,
4591
   fill_plugins, make_old_format, 0, -1, -1, 0, 0},
4592
  {"PROCESSLIST", processlist_fields_info, create_schema_table,
4593
   fill_schema_processlist, make_old_format, 0, -1, -1, 0, 0},
4594
  {"REFERENTIAL_CONSTRAINTS", referential_constraints_fields_info,
4595
   create_schema_table, get_all_tables, 0, get_referential_constraints_record,
4596
   1, 9, 0, OPEN_TABLE_ONLY},
4597
  {"SCHEMATA", schema_fields_info, create_schema_table,
4598
   fill_schema_schemata, make_schemata_old_format, 0, 1, -1, 0, 0},
4599
  {"SESSION_STATUS", variables_fields_info, create_schema_table,
4600
   fill_status, make_old_format, 0, -1, -1, 0, 0},
4601
  {"SESSION_VARIABLES", variables_fields_info, create_schema_table,
4602
   fill_variables, make_old_format, 0, -1, -1, 0, 0},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4603
  {"STATISTICS", stat_fields_info, create_schema_table,
1 by brian
clean slate
4604
   get_all_tables, make_old_format, get_schema_stat_record, 1, 2, 0,
4605
   OPEN_TABLE_ONLY|OPTIMIZE_I_S_TABLE},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4606
  {"STATUS", variables_fields_info, create_schema_table, fill_status,
1 by brian
clean slate
4607
   make_old_format, 0, -1, -1, 1, 0},
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4608
  {"TABLES", tables_fields_info, create_schema_table,
1 by brian
clean slate
4609
   get_all_tables, make_old_format, get_schema_tables_record, 1, 2, 0,
4610
   OPTIMIZE_I_S_TABLE},
4611
  {"TABLE_CONSTRAINTS", table_constraints_fields_info, create_schema_table,
4612
   get_all_tables, 0, get_schema_constraints_record, 3, 4, 0, OPEN_TABLE_ONLY},
4613
  {"TABLE_NAMES", table_names_fields_info, create_schema_table,
4614
   get_all_tables, make_table_names_old_format, 0, 1, 2, 1, 0},
4615
  {"VARIABLES", variables_fields_info, create_schema_table, fill_variables,
4616
   make_old_format, 0, -1, -1, 1, 0},
4617
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
4618
};
4619
4620
4621
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
4622
template class List_iterator_fast<char>;
4623
template class List<char>;
4624
#endif
4625
4626
int initialize_schema_table(st_plugin_int *plugin)
4627
{
4628
  ST_SCHEMA_TABLE *schema_table;
4629
683 by Brian Aker
Removed casting from a few operations (passing this so Mark can fix the
4630
  if ((schema_table= new ST_SCHEMA_TABLE) == NULL)
4631
    return(1);
641.3.8 by Monty Taylor
Removed my_malloc from drizzled.
4632
  memset(schema_table, 0, sizeof(ST_SCHEMA_TABLE));
4633
1 by brian
clean slate
4634
  /* Historical Requirement */
4635
  plugin->data= schema_table; // shortcut for the future
4636
  if (plugin->plugin->init)
4637
  {
4638
    schema_table->create_table= create_schema_table;
4639
    schema_table->old_format= make_old_format;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4640
    schema_table->idx_field1= -1,
4641
    schema_table->idx_field2= -1;
1 by brian
clean slate
4642
4643
    /* Make the name available to the init() function. */
4644
    schema_table->table_name= plugin->name.str;
4645
4646
    if (plugin->plugin->init(schema_table))
4647
    {
755.2.1 by Mark Atwood
replace sql_print_error etc with errmsg_print
4648
      errmsg_printf(ERRMSG_LVL_ERROR,
4649
                    _("Plugin '%s' init function returned error."),
4650
                    plugin->name.str);
1 by brian
clean slate
4651
      goto err;
4652
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
4653
1 by brian
clean slate
4654
    /* Make sure the plugin name is not set inside the init() function. */
4655
    schema_table->table_name= plugin->name.str;
4656
  }
4657
683 by Brian Aker
Removed casting from a few operations (passing this so Mark can fix the
4658
  return 0;
1 by brian
clean slate
4659
err:
683 by Brian Aker
Removed casting from a few operations (passing this so Mark can fix the
4660
  delete schema_table;
4661
4662
  return 1;
1 by brian
clean slate
4663
}
4664
4665
int finalize_schema_table(st_plugin_int *plugin)
4666
{
4667
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
4668
4669
  if (schema_table && plugin->plugin->deinit)
683 by Brian Aker
Removed casting from a few operations (passing this so Mark can fix the
4670
    delete schema_table;
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
4671
4672
  return(0);
1 by brian
clean slate
4673
}