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