~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Monty Taylor
  • Date: 2009-06-08 13:39:05 UTC
  • mto: This revision was merged to the branch mainline in revision 1060.
  • Revision ID: mordred@inaugust.com-20090608133905-3hogrrafmfg6e5hs
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
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
 */
15
19
 
16
20
 
17
21
/* Function with list databases, tables or fields */
18
22
#include <drizzled/server_includes.h>
19
23
#include <drizzled/sql_select.h>
20
 
#include <drizzled/sql_show.h>
21
 
#include "repl_failsafe.h"
 
24
#include <drizzled/show.h>
22
25
#include <mysys/my_dir.h>
23
 
#include <libdrizzle/gettext.h>
 
26
#include <drizzled/gettext.h>
 
27
#include <drizzled/util/convert.h>
 
28
#include <drizzled/error.h>
 
29
#include <drizzled/tztime.h>
 
30
#include <drizzled/data_home.h>
 
31
#include <drizzled/item/blob.h>
 
32
#include <drizzled/item/cmpfunc.h>
 
33
#include <drizzled/item/return_int.h>
 
34
#include <drizzled/item/empty_string.h>
 
35
#include <drizzled/item/return_date_time.h>
 
36
#include <drizzled/sql_base.h>
 
37
#include <drizzled/db.h>
 
38
#include <drizzled/field/timestamp.h>
 
39
#include <drizzled/field/decimal.h>
 
40
#include <drizzled/lock.h>
 
41
#include <drizzled/item/return_date_time.h>
 
42
#include <drizzled/item/empty_string.h>
 
43
#include "drizzled/plugin_registry.h"
 
44
 
 
45
#include <string>
 
46
#include <iostream>
 
47
#include <sstream>
 
48
#include <vector>
 
49
#include <algorithm>
 
50
 
 
51
using namespace std;
 
52
 
 
53
extern "C"
 
54
int show_var_cmp(const void *var1, const void *var2);
24
55
 
25
56
inline const char *
26
57
str_or_nil(const char *str)
31
62
/* Match the values of enum ha_choice */
32
63
static const char *ha_choice_values[] = {"", "0", "1"};
33
64
 
34
 
static void store_key_options(THD *thd, String *packet, Table *table,
35
 
                              KEY *key_info);
36
 
 
 
65
static void store_key_options(String *packet, Table *table, KEY *key_info);
 
66
 
 
67
static vector<InfoSchemaTable *> all_schema_tables;
 
68
 
 
69
Table *create_schema_table(Session *session, TableList *table_list);
 
70
int make_old_format(Session *session, InfoSchemaTable *schema_table);
 
71
 
 
72
void add_infoschema_table(InfoSchemaTable *schema_table)
 
73
{
 
74
  if (schema_table->create_table == NULL)
 
75
    schema_table->create_table= create_schema_table;
 
76
  if (schema_table->old_format == NULL)
 
77
    schema_table->old_format= make_old_format;
 
78
  if (schema_table->idx_field1 == 0)
 
79
    schema_table->idx_field1= -1;
 
80
  if (schema_table->idx_field2)
 
81
   schema_table->idx_field2= -1;
 
82
 
 
83
  all_schema_tables.push_back(schema_table);
 
84
}
 
85
 
 
86
void remove_infoschema_table(InfoSchemaTable *table)
 
87
{
 
88
  all_schema_tables.erase(remove_if(all_schema_tables.begin(),
 
89
                                    all_schema_tables.end(),
 
90
                                    bind2nd(equal_to<InfoSchemaTable *>(),
 
91
                                            table)),
 
92
                          all_schema_tables.end());
 
93
}
37
94
 
38
95
 
39
96
int wild_case_compare(const CHARSET_INFO * const cs, const char *str,const char *wildstr)
86
143
** List all table types supported
87
144
***************************************************************************/
88
145
 
89
 
static bool show_plugins(THD *thd, plugin_ref plugin,
90
 
                            void *arg)
 
146
class ShowPlugins : public unary_function<st_plugin_int *, bool>
91
147
{
92
 
  Table *table= (Table*) arg;
93
 
  struct st_mysql_plugin *plug= plugin_decl(plugin);
94
 
  struct st_plugin_dl *plugin_dl= plugin_dlib(plugin);
95
 
  const CHARSET_INFO * const cs= system_charset_info;
96
 
 
97
 
  restore_record(table, s->default_values);
98
 
 
99
 
  table->field[0]->store(plugin_name(plugin)->str,
100
 
                         plugin_name(plugin)->length, cs);
101
 
 
102
 
  if (plug->version)
103
 
  {
104
 
    table->field[1]->store(plug->version, strlen(plug->version), cs);
105
 
    table->field[1]->set_notnull();
106
 
  }
107
 
  else
108
 
    table->field[1]->set_null();
109
 
 
110
 
  switch (plugin_state(plugin)) {
111
 
  /* case PLUGIN_IS_FREED: does not happen */
112
 
  case PLUGIN_IS_DELETED:
113
 
    table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
114
 
    break;
115
 
  case PLUGIN_IS_UNINITIALIZED:
116
 
    table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
117
 
    break;
118
 
  case PLUGIN_IS_READY:
119
 
    table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
120
 
    break;
121
 
  default:
122
 
    assert(0);
123
 
  }
124
 
 
125
 
  table->field[3]->store(plugin_type_names[plug->type].str,
126
 
                         plugin_type_names[plug->type].length,
127
 
                         cs);
128
 
 
129
 
  if (plugin_dl)
130
 
  {
131
 
    table->field[4]->store(plugin_dl->dl.str, plugin_dl->dl.length, cs);
132
 
    table->field[4]->set_notnull();
133
 
  }
134
 
  else
135
 
  {
136
 
    table->field[4]->set_null();
137
 
  }
138
 
 
139
 
  if (plug->author)
140
 
  {
141
 
    table->field[5]->store(plug->author, strlen(plug->author), cs);
 
148
  Session *session;
 
149
  Table *table;
 
150
public:
 
151
  ShowPlugins(Session *session_arg, Table *table_arg)
 
152
    : session(session_arg), table(table_arg) {}
 
153
 
 
154
  result_type operator() (argument_type plugin)
 
155
  {
 
156
    struct drizzled_plugin_manifest *plug= plugin_decl(plugin);
 
157
    const CHARSET_INFO * const cs= system_charset_info;
 
158
  
 
159
    table->restoreRecordAsDefault();
 
160
  
 
161
    table->field[0]->store(plugin_name(plugin)->str,
 
162
                           plugin_name(plugin)->length, cs);
 
163
  
 
164
    if (plug->version)
 
165
    {
 
166
      table->field[1]->store(plug->version, strlen(plug->version), cs);
 
167
      table->field[1]->set_notnull();
 
168
    }
 
169
    else
 
170
      table->field[1]->set_null();
 
171
  
 
172
    if (plugin->isInited)
 
173
      table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
 
174
    else
 
175
      table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
 
176
  
 
177
    if (plug->author)
 
178
    {
 
179
      table->field[3]->store(plug->author, strlen(plug->author), cs);
 
180
      table->field[3]->set_notnull();
 
181
    }
 
182
    else
 
183
      table->field[3]->set_null();
 
184
  
 
185
    if (plug->descr)
 
186
    {
 
187
      table->field[4]->store(plug->descr, strlen(plug->descr), cs);
 
188
      table->field[4]->set_notnull();
 
189
    }
 
190
    else
 
191
      table->field[4]->set_null();
 
192
  
 
193
    switch (plug->license) {
 
194
    case PLUGIN_LICENSE_GPL:
 
195
      table->field[5]->store(PLUGIN_LICENSE_GPL_STRING,
 
196
                             strlen(PLUGIN_LICENSE_GPL_STRING), cs);
 
197
      break;
 
198
    case PLUGIN_LICENSE_BSD:
 
199
      table->field[5]->store(PLUGIN_LICENSE_BSD_STRING,
 
200
                             strlen(PLUGIN_LICENSE_BSD_STRING), cs);
 
201
      break;
 
202
    case PLUGIN_LICENSE_LGPL:
 
203
      table->field[5]->store(PLUGIN_LICENSE_LGPL_STRING,
 
204
                             strlen(PLUGIN_LICENSE_LGPL_STRING), cs);
 
205
      break;
 
206
    default:
 
207
      table->field[5]->store(PLUGIN_LICENSE_PROPRIETARY_STRING,
 
208
                             strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
 
209
      break;
 
210
    }
142
211
    table->field[5]->set_notnull();
143
 
  }
144
 
  else
145
 
    table->field[5]->set_null();
146
 
 
147
 
  if (plug->descr)
148
 
  {
149
 
    table->field[6]->store(plug->descr, strlen(plug->descr), cs);
150
 
    table->field[6]->set_notnull();
151
 
  }
152
 
  else
153
 
    table->field[6]->set_null();
154
 
 
155
 
  switch (plug->license) {
156
 
  case PLUGIN_LICENSE_GPL:
157
 
    table->field[7]->store(PLUGIN_LICENSE_GPL_STRING, 
158
 
                           strlen(PLUGIN_LICENSE_GPL_STRING), cs);
159
 
    break;
160
 
  case PLUGIN_LICENSE_BSD:
161
 
    table->field[7]->store(PLUGIN_LICENSE_BSD_STRING, 
162
 
                           strlen(PLUGIN_LICENSE_BSD_STRING), cs);
163
 
    break;
164
 
  default:
165
 
    table->field[7]->store(PLUGIN_LICENSE_PROPRIETARY_STRING, 
166
 
                           strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
167
 
    break;
168
 
  }
169
 
  table->field[7]->set_notnull();
170
 
 
171
 
  return schema_table_store_record(thd, table);
172
 
}
173
 
 
174
 
 
175
 
int fill_plugins(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
212
  
 
213
    return schema_table_store_record(session, table);
 
214
  }
 
215
};
 
216
 
 
217
 
 
218
int fill_plugins(Session *session, TableList *tables, COND *)
176
219
{
177
220
  Table *table= tables->table;
178
221
 
179
 
  if (plugin_foreach_with_mask(thd, show_plugins, DRIZZLE_ANY_PLUGIN,
180
 
                               ~PLUGIN_IS_FREED, table))
181
 
    return(1);
182
 
 
 
222
  PluginRegistry &registry= PluginRegistry::getPluginRegistry();
 
223
  vector<st_plugin_int *> plugins= registry.get_list(true);
 
224
  vector<st_plugin_int *>::iterator iter=
 
225
    find_if(plugins.begin(), plugins.end(), ShowPlugins(session, table));
 
226
  if (iter != plugins.end())
 
227
    return 1;
183
228
  return(0);
184
229
}
185
230
 
189
234
 
190
235
  SYNOPSIS
191
236
    find_files()
192
 
    thd                 thread handler
 
237
    session                 thread handler
193
238
    files               put found files in this list
194
239
    db                  database name to set in TableList structure
195
240
    path                path to database
205
250
 
206
251
 
207
252
find_files_result
208
 
find_files(THD *thd, List<LEX_STRING> *files, const char *db,
 
253
find_files(Session *session, List<LEX_STRING> *files, const char *db,
209
254
           const char *path, const char *wild, bool dir)
210
255
{
211
256
  uint32_t i;
219
264
  if (wild && !wild[0])
220
265
    wild=0;
221
266
 
222
 
  memset(&table_list, 0, sizeof(table_list));
223
 
 
224
267
  if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0))))
225
268
  {
226
269
    if (my_errno == ENOENT)
230
273
    return(FIND_FILES_DIR);
231
274
  }
232
275
 
233
 
  for (i=0 ; i < (uint) dirp->number_off_files  ; i++)
 
276
  for (i=0 ; i < (uint32_t) dirp->number_off_files  ; i++)
234
277
  {
235
278
    char uname[NAME_LEN + 1];                   /* Unencoded name */
236
279
    file=dirp->dir_entry+i;
237
280
    if (dir)
238
281
    {                                           /* Return databases */
239
 
      if ((file->name[0] == '.' && 
 
282
      if ((file->name[0] == '.' &&
240
283
          ((file->name[1] == '.' && file->name[2] == '\0') ||
241
284
            file->name[1] == '\0')))
242
285
        continue;                               /* . or .. */
262
305
      file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
263
306
      if (wild && wild_compare(uname, wild, 0))
264
307
        continue;
265
 
      if (!(file_name= 
266
 
            thd->make_lex_string(file_name, uname, file_name_len, true)))
 
308
      if (!(file_name=
 
309
            session->make_lex_string(file_name, uname, file_name_len, true)))
267
310
      {
268
311
        my_dirend(dirp);
269
312
        return(FIND_FILES_OOM);
271
314
    }
272
315
    else
273
316
    {
274
 
        // Return only .frm files which aren't temp files.
275
 
      if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),reg_ext) ||
276
 
          is_prefix(file->name, tmp_file_prefix))
 
317
      // Return only .frm files which aren't temp files.
 
318
      if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),".dfe") ||
 
319
          is_prefix(file->name, TMP_FILE_PREFIX))
277
320
        continue;
278
321
      *ext=0;
279
322
      file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
280
323
      if (wild)
281
324
      {
282
 
        if (lower_case_table_names)
283
 
        {
284
 
          if (wild_case_compare(files_charset_info, uname, wild))
285
 
            continue;
286
 
        }
287
 
        else if (wild_compare(uname, wild, 0))
288
 
          continue;
 
325
        if (wild_case_compare(files_charset_info, uname, wild))
 
326
          continue;
289
327
      }
290
328
    }
291
 
    if (!(file_name= 
292
 
          thd->make_lex_string(file_name, uname, file_name_len, true)) ||
 
329
    if (!(file_name=
 
330
          session->make_lex_string(file_name, uname, file_name_len, true)) ||
293
331
        files->push_back(file_name))
294
332
    {
295
333
      my_dirend(dirp);
302
340
}
303
341
 
304
342
 
305
 
bool
306
 
mysqld_show_create(THD *thd, TableList *table_list)
 
343
bool drizzled_show_create(Session *session, TableList *table_list)
307
344
{
308
 
  Protocol *protocol= thd->protocol;
 
345
  Protocol *protocol= session->protocol;
309
346
  char buff[2048];
310
347
  String buffer(buff, sizeof(buff), system_charset_info);
311
348
 
312
349
  /* Only one table for now, but VIEW can involve several tables */
313
 
  if (open_normal_and_derived_tables(thd, table_list, 0))
 
350
  if (open_normal_and_derived_tables(session, table_list, 0))
314
351
  {
315
 
    if (thd->is_error() && thd->main_da.sql_errno() != ER_VIEW_INVALID)
316
 
      return(true);
 
352
    if (session->is_error())
 
353
      return true;
317
354
 
318
355
    /*
319
356
      Clear all messages with 'error' level status and
320
 
      issue a warning with 'warning' level status in 
 
357
      issue a warning with 'warning' level status in
321
358
      case of invalid view and last error is ER_VIEW_INVALID
322
359
    */
323
 
    drizzle_reset_errors(thd, true);
324
 
    thd->clear_error();
 
360
    drizzle_reset_errors(session, true);
 
361
    session->clear_error();
325
362
  }
326
363
 
327
364
  buffer.length(0);
328
365
 
329
 
  if (store_create_info(thd, table_list, &buffer, NULL))
330
 
    return(true);
 
366
  if (store_create_info(table_list, &buffer, NULL))
 
367
    return true;
331
368
 
332
369
  List<Item> field_list;
333
370
  {
337
374
                                               cmax(buffer.length(),(uint32_t)1024)));
338
375
  }
339
376
 
340
 
  if (protocol->send_fields(&field_list,
341
 
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
342
 
    return(true);
343
 
  protocol->prepare_for_resend();
 
377
  if (protocol->sendFields(&field_list,
 
378
                           Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
 
379
  {
 
380
    return true;
 
381
  }
 
382
  protocol->prepareForResend();
344
383
  {
345
384
    if (table_list->schema_table)
346
 
      protocol->store(table_list->schema_table->table_name,
347
 
                      system_charset_info);
 
385
      protocol->store(table_list->schema_table->table_name);
348
386
    else
349
 
      protocol->store(table_list->table->alias, system_charset_info);
 
387
      protocol->store(table_list->table->alias);
350
388
  }
351
389
 
352
 
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
 
390
  protocol->store(buffer.ptr(), buffer.length());
353
391
 
354
392
  if (protocol->write())
355
 
    return(true);
 
393
    return true;
356
394
 
357
 
  my_eof(thd);
358
 
  return(false);
 
395
  session->my_eof();
 
396
  return false;
359
397
}
360
398
 
361
 
bool mysqld_show_create_db(THD *thd, char *dbname,
 
399
bool mysqld_show_create_db(Session *session, char *dbname,
362
400
                           HA_CREATE_INFO *create_info)
363
401
{
364
402
  char buff[2048];
365
403
  String buffer(buff, sizeof(buff), system_charset_info);
366
 
  Protocol *protocol=thd->protocol;
 
404
  Protocol *protocol=session->protocol;
367
405
 
368
 
  if (store_db_create_info(thd, dbname, &buffer, create_info))
 
406
  if (store_db_create_info(dbname, &buffer, create_info))
369
407
  {
370
 
    /* 
 
408
    /*
371
409
      This assumes that the only reason for which store_db_create_info()
372
410
      can fail is incorrect database name (which is the case now).
373
411
    */
374
412
    my_error(ER_BAD_DB_ERROR, MYF(0), dbname);
375
 
    return(true);    
 
413
    return true;
376
414
  }
377
415
 
378
416
  List<Item> field_list;
379
417
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
380
418
  field_list.push_back(new Item_empty_string("Create Database",1024));
381
419
 
382
 
  if (protocol->send_fields(&field_list,
383
 
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
384
 
    return(true);
 
420
  if (protocol->sendFields(&field_list,
 
421
                           Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
 
422
    return true;
385
423
 
386
 
  protocol->prepare_for_resend();
387
 
  protocol->store(dbname, strlen(dbname), system_charset_info);
388
 
  protocol->store(buffer.ptr(), buffer.length(), buffer.charset());
 
424
  protocol->prepareForResend();
 
425
  protocol->store(dbname, strlen(dbname));
 
426
  protocol->store(buffer.ptr(), buffer.length());
389
427
 
390
428
  if (protocol->write())
391
 
    return(true);
392
 
  my_eof(thd);
393
 
  return(false);
 
429
    return true;
 
430
  session->my_eof();
 
431
  return false;
394
432
}
395
433
 
396
434
 
401
439
****************************************************************************/
402
440
 
403
441
void
404
 
mysqld_list_fields(THD *thd, TableList *table_list, const char *wild)
 
442
mysqld_list_fields(Session *session, TableList *table_list, const char *wild)
405
443
{
406
444
  Table *table;
407
445
 
408
 
  if (open_normal_and_derived_tables(thd, table_list, 0))
 
446
  if (open_normal_and_derived_tables(session, table_list, 0))
409
447
    return;
410
448
  table= table_list->table;
411
449
 
414
452
  Field **ptr,*field;
415
453
  for (ptr=table->field ; (field= *ptr); ptr++)
416
454
  {
417
 
    if (!wild || !wild[0] || 
 
455
    if (!wild || !wild[0] ||
418
456
        !wild_case_compare(system_charset_info, field->field_name,wild))
419
457
    {
420
458
      field_list.push_back(new Item_field(field));
421
459
    }
422
460
  }
423
 
  restore_record(table, s->default_values);              // Get empty record
 
461
  table->restoreRecordAsDefault();              // Get empty record
424
462
  table->use_all_columns();
425
 
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS))
426
 
    return;
427
 
  my_eof(thd);
428
 
  return;
429
 
}
430
 
 
431
 
 
432
 
/*
433
 
  Go through all character combinations and ensure that sql_lex.cc can
434
 
  parse it as an identifier.
435
 
 
436
 
  SYNOPSIS
437
 
  require_quotes()
438
 
  name                  attribute name
439
 
  name_length           length of name
440
 
 
441
 
  RETURN
442
 
    #   Pointer to conflicting character
443
 
    0   No conflicting character
444
 
*/
445
 
 
446
 
static const char *require_quotes(const char *name, uint32_t name_length)
447
 
{
448
 
  uint32_t length;
449
 
  bool pure_digit= true;
450
 
  const char *end= name + name_length;
451
 
 
452
 
  for (; name < end ; name++)
453
 
  {
454
 
    unsigned char chr= (unsigned char) *name;
455
 
    length= my_mbcharlen(system_charset_info, chr);
456
 
    if (length == 1 && !system_charset_info->ident_map[chr])
457
 
      return name;
458
 
    if (length == 1 && (chr < '0' || chr > '9'))
459
 
      pure_digit= false;
460
 
  }
461
 
  if (pure_digit)
462
 
    return name;
463
 
  return 0;
464
 
}
465
 
 
466
 
 
467
 
/*
468
 
  Quote the given identifier if needed and append it to the target string.
469
 
  If the given identifier is empty, it will be quoted.
470
 
 
471
 
  SYNOPSIS
472
 
  append_identifier()
473
 
  thd                   thread handler
474
 
  packet                target string
475
 
  name                  the identifier to be appended
476
 
  name_length           length of the appending identifier
477
 
*/
478
 
 
479
 
void
480
 
append_identifier(THD *thd, String *packet, const char *name, uint32_t length)
481
 
{
482
 
  const char *name_end;
483
 
  char quote_char;
484
 
  int q= get_quote_char_for_identifier(thd, name, length);
485
 
 
486
 
  if (q == EOF)
487
 
  {
488
 
    packet->append(name, length, packet->charset());
489
 
    return;
490
 
  }
491
 
 
492
 
  /*
493
 
    The identifier must be quoted as it includes a quote character or
494
 
   it's a keyword
495
 
  */
496
 
 
497
 
  packet->reserve(length*2 + 2);
498
 
  quote_char= (char) q;
499
 
  packet->append(&quote_char, 1, system_charset_info);
500
 
 
501
 
  for (name_end= name+length ; name < name_end ; name+= length)
502
 
  {
503
 
    unsigned char chr= (unsigned char) *name;
504
 
    length= my_mbcharlen(system_charset_info, chr);
505
 
    /*
506
 
      my_mbcharlen can return 0 on a wrong multibyte
507
 
      sequence. It is possible when upgrading from 4.0,
508
 
      and identifier contains some accented characters.
509
 
      The manual says it does not work. So we'll just
510
 
      change length to 1 not to hang in the endless loop.
511
 
    */
512
 
    if (!length)
513
 
      length= 1;
514
 
    if (length == 1 && chr == (unsigned char) quote_char)
515
 
      packet->append(&quote_char, 1, system_charset_info);
516
 
    packet->append(name, length, system_charset_info);
517
 
  }
518
 
  packet->append(&quote_char, 1, system_charset_info);
 
463
  if (session->protocol->sendFields(&field_list, Protocol::SEND_DEFAULTS))
 
464
    return;
 
465
  session->my_eof();
519
466
}
520
467
 
521
468
 
524
471
 
525
472
  SYNOPSIS
526
473
    get_quote_char_for_identifier()
527
 
    thd         Thread handler
528
 
    name        name to quote
529
 
    length      length of name
530
474
 
531
475
  IMPLEMENTATION
532
476
    Force quoting in the following cases:
542
486
    #     Quote character
543
487
*/
544
488
 
545
 
int get_quote_char_for_identifier(THD *thd, const char *name, uint32_t length)
 
489
int get_quote_char_for_identifier()
546
490
{
547
 
  if (length &&
548
 
      !is_keyword(name,length) &&
549
 
      !require_quotes(name, length) &&
550
 
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
551
 
    return EOF;
552
491
  return '`';
553
492
}
554
493
 
555
494
 
556
495
/* Append directory name (if exists) to CREATE INFO */
557
496
 
558
 
static void append_directory(THD *thd __attribute__((unused)),
559
 
                             String *packet, const char *dir_type,
560
 
                             const char *filename)
 
497
static void append_directory(String *packet, const char *dir_type,
 
498
                             const char *filename)
561
499
{
562
500
  if (filename)
563
501
  {
573
511
 
574
512
#define LIST_PROCESS_HOST_LEN 64
575
513
 
576
 
static bool get_field_default_value(THD *thd __attribute__((unused)),
577
 
                                    Field *timestamp_field,
 
514
static bool get_field_default_value(Field *timestamp_field,
578
515
                                    Field *field, String *def_value,
579
516
                                    bool quoted)
580
517
{
581
518
  bool has_default;
582
519
  bool has_now_default;
583
520
 
584
 
  /* 
 
521
  /*
585
522
     We are using CURRENT_TIMESTAMP instead of NOW because it is
586
523
     more standard
587
524
  */
588
525
  has_now_default= (timestamp_field == field &&
589
526
                    field->unireg_check != Field::TIMESTAMP_UN_FIELD);
590
 
    
 
527
 
591
528
  has_default= (field->type() != DRIZZLE_TYPE_BLOB &&
592
529
                !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
593
 
                field->unireg_check != Field::NEXT_NUMBER
594
 
                  && has_now_default);
 
530
                field->unireg_check != Field::NEXT_NUMBER);
595
531
 
596
532
  def_value->length(0);
597
533
  if (has_default)
621
557
    else if (field->maybe_null() && quoted)
622
558
      def_value->append(STRING_WITH_LEN("NULL"));    // Null as default
623
559
    else
624
 
      return 0;
625
 
 
 
560
      return false;
626
561
  }
627
562
  return has_default;
628
563
}
632
567
 
633
568
  SYNOPSIS
634
569
    store_create_info()
635
 
    thd               The thread
636
570
    table_list        A list containing one table to write statement
637
571
                      for.
638
572
    packet            Pointer to a string where statement will be
641
575
                      to tailor the format of the statement.  Can be
642
576
                      NULL, in which case only SQL_MODE is considered
643
577
                      when building the statement.
644
 
  
 
578
 
645
579
  NOTE
646
580
    Currently always return 0, but might return error code in the
647
581
    future.
648
 
    
 
582
 
649
583
  RETURN
650
584
    0       OK
651
585
 */
652
586
 
653
 
int store_create_info(THD *thd, TableList *table_list, String *packet,
654
 
                      HA_CREATE_INFO *create_info_arg)
 
587
int store_create_info(TableList *table_list, String *packet, HA_CREATE_INFO *create_info_arg)
655
588
{
656
589
  List<Item> field_list;
657
 
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], def_value_buf[MAX_FIELD_WIDTH];
 
590
  char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
658
591
  const char *alias;
 
592
  string buff;
659
593
  String type(tmp, sizeof(tmp), system_charset_info);
660
594
  String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
661
595
  Field **ptr,*field;
663
597
  KEY *key_info;
664
598
  Table *table= table_list->table;
665
599
  handler *file= table->file;
666
 
  TABLE_SHARE *share= table->s;
 
600
  TableShare *share= table->s;
667
601
  HA_CREATE_INFO create_info;
668
602
  bool show_table_options= false;
669
603
  my_bitmap_map *old_map;
670
604
 
671
 
  restore_record(table, s->default_values); // Get empty record
 
605
  table->restoreRecordAsDefault(); // Get empty record
672
606
 
673
607
  if (share->tmp_table)
674
608
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
680
614
  if (table_list->schema_table)
681
615
    alias= table_list->schema_table->table_name;
682
616
  else
683
 
  {
684
 
    if (lower_case_table_names == 2)
685
 
      alias= table->alias;
686
 
    else
687
 
    {
688
 
      alias= share->table_name.str;
689
 
    }
690
 
  }
691
 
  append_identifier(thd, packet, alias, strlen(alias));
 
617
    alias= share->table_name.str;
 
618
 
 
619
  packet->append_identifier(alias, strlen(alias));
692
620
  packet->append(STRING_WITH_LEN(" (\n"));
693
621
  /*
694
622
    We need this to get default values from the table
705
633
      packet->append(STRING_WITH_LEN(",\n"));
706
634
 
707
635
    packet->append(STRING_WITH_LEN("  "));
708
 
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
 
636
    packet->append_identifier(field->field_name, strlen(field->field_name));
709
637
    packet->append(' ');
710
638
    // check for surprises from the previous call to Field::sql_type()
711
639
    if (type.ptr() != tmp)
720
648
    {
721
649
      if (field->charset() != share->table_charset)
722
650
      {
723
 
        packet->append(STRING_WITH_LEN(" CHARACTER SET "));
724
 
        packet->append(field->charset()->csname);
 
651
        packet->append(STRING_WITH_LEN(" CHARACTER SET "));
 
652
        packet->append(field->charset()->csname);
725
653
      }
726
 
      /* 
727
 
        For string types dump collation name only if 
728
 
        collation is not primary for the given charset
 
654
 
 
655
      /*
 
656
        For string types dump collation name only if
 
657
        collation is not primary for the given charset
729
658
      */
730
659
      if (!(field->charset()->state & MY_CS_PRIMARY))
731
660
      {
732
 
        packet->append(STRING_WITH_LEN(" COLLATE "));
733
 
        packet->append(field->charset()->name);
 
661
        packet->append(STRING_WITH_LEN(" COLLATE "));
 
662
        packet->append(field->charset()->name);
734
663
      }
735
664
    }
736
665
 
754
683
      if (column_format)
755
684
      {
756
685
        packet->append(STRING_WITH_LEN(" /*!"));
757
 
        packet->append(STRING_WITH_LEN(DRIZZLE_VERSION_TABLESPACE_IN_FRM_STR));
758
686
        packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
759
687
        if (column_format == COLUMN_FORMAT_TYPE_FIXED)
760
688
          packet->append(STRING_WITH_LEN(" FIXED */"));
762
690
          packet->append(STRING_WITH_LEN(" DYNAMIC */"));
763
691
      }
764
692
    }
765
 
    if (get_field_default_value(thd, table->timestamp_field,
766
 
                                field, &def_value, 1))
 
693
    if (get_field_default_value(table->timestamp_field, field, &def_value, 1))
767
694
    {
768
695
      packet->append(STRING_WITH_LEN(" DEFAULT "));
769
696
      packet->append(def_value.ptr(), def_value.length(), system_charset_info);
795
722
    bool found_primary=0;
796
723
    packet->append(STRING_WITH_LEN(",\n  "));
797
724
 
798
 
    if (i == primary_key && !strcmp(key_info->name, primary_key_name))
 
725
    if (i == primary_key && is_primary_key(key_info))
799
726
    {
800
727
      found_primary=1;
801
728
      /*
810
737
      packet->append(STRING_WITH_LEN("KEY "));
811
738
 
812
739
    if (!found_primary)
813
 
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
 
740
     packet->append_identifier(key_info->name, strlen(key_info->name));
814
741
 
815
742
    packet->append(STRING_WITH_LEN(" ("));
816
743
 
820
747
        packet->append(',');
821
748
 
822
749
      if (key_part->field)
823
 
        append_identifier(thd,packet,key_part->field->field_name,
824
 
                          strlen(key_part->field->field_name));
 
750
        packet->append_identifier(key_part->field->field_name,
 
751
                                  strlen(key_part->field->field_name));
825
752
      if (key_part->field &&
826
753
          (key_part->length !=
827
754
           table->field[key_part->fieldnr-1]->key_length()))
828
755
      {
829
 
        char *end;
830
 
        buff[0] = '(';
831
 
        end= int10_to_str((long) key_part->length /
832
 
                          key_part->field->charset()->mbmaxlen,
833
 
                          buff + 1,10);
834
 
        *end++ = ')';
835
 
        packet->append(buff,(uint) (end-buff));
 
756
        buff.assign("(");
 
757
        buff.append(to_string((int32_t) key_part->length /
 
758
                              key_part->field->charset()->mbmaxlen));
 
759
        buff.append(")");
 
760
        packet->append(buff.c_str(), buff.length());
836
761
      }
837
762
    }
838
763
    packet->append(')');
839
 
    store_key_options(thd, packet, table, key_info);
 
764
    store_key_options(packet, table, key_info);
840
765
  }
841
766
 
842
767
  /*
866
791
        (create_info_arg->used_fields & HA_CREATE_USED_ENGINE))
867
792
    {
868
793
      packet->append(STRING_WITH_LEN(" ENGINE="));
869
 
      packet->append(file->table_type());
 
794
      packet->append(file->engine->getName().c_str());
870
795
    }
871
796
 
872
797
    /*
882
807
 
883
808
    if (create_info.auto_increment_value > 1)
884
809
    {
885
 
      char *end;
886
810
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT="));
887
 
      end= int64_t10_to_str(create_info.auto_increment_value, buff,10);
888
 
      packet->append(buff, (uint) (end - buff));
 
811
      buff= to_string(create_info.auto_increment_value);
 
812
      packet->append(buff.c_str(), buff.length());
889
813
    }
890
814
 
891
815
    if (share->min_rows)
892
816
    {
893
 
      char *end;
894
817
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
895
 
      end= int64_t10_to_str(share->min_rows, buff, 10);
896
 
      packet->append(buff, (uint) (end- buff));
 
818
      buff= to_string(share->min_rows);
 
819
      packet->append(buff.c_str(), buff.length());
897
820
    }
898
821
 
899
822
    if (share->max_rows && !table_list->schema_table)
900
823
    {
901
 
      char *end;
902
824
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
903
 
      end= int64_t10_to_str(share->max_rows, buff, 10);
904
 
      packet->append(buff, (uint) (end - buff));
 
825
      buff= to_string(share->max_rows);
 
826
      packet->append(buff.c_str(), buff.length());
905
827
    }
906
828
 
907
829
    if (share->avg_row_length)
908
830
    {
909
 
      char *end;
910
831
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
911
 
      end= int64_t10_to_str(share->avg_row_length, buff,10);
912
 
      packet->append(buff, (uint) (end - buff));
 
832
      buff= to_string(share->avg_row_length);
 
833
      packet->append(buff.c_str(), buff.length());
913
834
    }
914
835
 
915
836
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
922
843
    if (share->page_checksum != HA_CHOICE_UNDEF)
923
844
    {
924
845
      packet->append(STRING_WITH_LEN(" PAGE_CHECKSUM="));
925
 
      packet->append(ha_choice_values[(uint) share->page_checksum], 1);
 
846
      packet->append(ha_choice_values[(uint32_t) share->page_checksum], 1);
926
847
    }
927
848
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
928
849
      packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
929
850
    if (create_info.row_type != ROW_TYPE_DEFAULT)
930
851
    {
931
852
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
932
 
      packet->append(ha_row_type[(uint) create_info.row_type]);
933
 
    }
934
 
    if (share->transactional != HA_CHOICE_UNDEF)
935
 
    {
936
 
      packet->append(STRING_WITH_LEN(" TRANSACTIONAL="));
937
 
      packet->append(ha_choice_values[(uint) share->transactional], 1);
 
853
      packet->append(ha_row_type[(uint32_t) create_info.row_type]);
938
854
    }
939
855
    if (table->s->key_block_size)
940
856
    {
941
 
      char *end;
942
857
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
943
 
      end= int64_t10_to_str(table->s->key_block_size, buff, 10);
944
 
      packet->append(buff, (uint) (end - buff));
 
858
      buff= to_string(table->s->key_block_size);
 
859
      packet->append(buff.c_str(), buff.length());
945
860
    }
946
861
    if (share->block_size)
947
862
    {
948
 
      char *end;
949
863
      packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
950
 
      end= int64_t10_to_str(share->block_size, buff,10);
951
 
      packet->append(buff, (uint) (end - buff));
 
864
      buff= to_string(share->block_size);
 
865
      packet->append(buff.c_str(), buff.length());
952
866
    }
953
867
    table->file->append_create_info(packet);
954
868
    if (share->comment.length)
961
875
      packet->append(STRING_WITH_LEN(" CONNECTION="));
962
876
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
963
877
    }
964
 
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
965
 
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
 
878
    append_directory(packet, "DATA",  create_info.data_file_name);
 
879
    append_directory(packet, "INDEX", create_info.index_file_name);
966
880
  }
967
881
  table->restore_column_map(old_map);
968
882
  return(0);
981
895
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
982
896
  in @c create_options are ignored.
983
897
 
984
 
  @param  thd           The current thread instance.
 
898
  @param  session           The current thread instance.
985
899
  @param  dbname        The name of the database.
986
900
  @param  buffer        A String instance where the statement is stored.
987
 
  @param  create_info   If not NULL, the options member influences the resulting 
 
901
  @param  create_info   If not NULL, the options member influences the resulting
988
902
                        CRATE statement.
989
903
 
990
904
  @returns true if errors are detected, false otherwise.
991
905
*/
992
906
 
993
 
bool store_db_create_info(THD *thd, const char *dbname, String *buffer,
994
 
                          HA_CREATE_INFO *create_info)
 
907
bool store_db_create_info(const char *dbname, String *buffer, HA_CREATE_INFO *create_info)
995
908
{
996
909
  HA_CREATE_INFO create;
997
910
  uint32_t create_options = create_info ? create_info->options : 0;
998
911
 
999
912
  if (!my_strcasecmp(system_charset_info, dbname,
1000
 
                     INFORMATION_SCHEMA_NAME.str))
 
913
                     INFORMATION_SCHEMA_NAME.c_str()))
1001
914
  {
1002
 
    dbname= INFORMATION_SCHEMA_NAME.str;
 
915
    dbname= INFORMATION_SCHEMA_NAME.c_str();
1003
916
    create.default_table_charset= system_charset_info;
1004
917
  }
1005
918
  else
1006
919
  {
1007
920
    if (check_db_dir_existence(dbname))
1008
 
      return(true);
 
921
      return true;
1009
922
 
1010
 
    load_db_opt_by_name(thd, dbname, &create);
 
923
    load_db_opt_by_name(dbname, &create);
1011
924
  }
1012
925
 
1013
926
  buffer->length(0);
1016
929
  buffer->append(STRING_WITH_LEN("CREATE DATABASE "));
1017
930
 
1018
931
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
1019
 
    buffer->append(STRING_WITH_LEN("/*!32312 IF NOT EXISTS*/ "));
1020
 
 
1021
 
  append_identifier(thd, buffer, dbname, strlen(dbname));
1022
 
 
1023
 
  if (create.default_table_charset)
1024
 
  {
1025
 
    buffer->append(STRING_WITH_LEN(" /*!40100"));
1026
 
    buffer->append(STRING_WITH_LEN(" DEFAULT CHARACTER SET "));
1027
 
    buffer->append(create.default_table_charset->csname);
1028
 
    if (!(create.default_table_charset->state & MY_CS_PRIMARY))
1029
 
    {
1030
 
      buffer->append(STRING_WITH_LEN(" COLLATE "));
1031
 
      buffer->append(create.default_table_charset->name);
1032
 
    }
1033
 
    buffer->append(STRING_WITH_LEN(" */"));
1034
 
  }
1035
 
 
1036
 
  return(false);
 
932
    buffer->append(STRING_WITH_LEN("IF NOT EXISTS "));
 
933
 
 
934
  buffer->append_identifier(dbname, strlen(dbname));
 
935
 
 
936
  return false;
1037
937
}
1038
938
 
1039
 
static void store_key_options(THD *thd __attribute__((unused)),
1040
 
                              String *packet, Table *table,
1041
 
                              KEY *key_info)
 
939
static void store_key_options(String *packet, Table *table, KEY *key_info)
1042
940
{
1043
941
  char *end, buff[32];
1044
942
 
1053
951
  {
1054
952
    packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
1055
953
    end= int64_t10_to_str(key_info->block_size, buff, 10);
1056
 
    packet->append(buff, (uint) (end - buff));
 
954
    packet->append(buff, (uint32_t) (end - buff));
1057
955
  }
1058
956
 
1059
 
  assert(test(key_info->flags & HA_USES_COMMENT) == 
 
957
  assert(test(key_info->flags & HA_USES_COMMENT) ==
1060
958
              (key_info->comment.length > 0));
1061
959
  if (key_info->flags & HA_USES_COMMENT)
1062
960
  {
1063
961
    packet->append(STRING_WITH_LEN(" COMMENT "));
1064
 
    append_unescaped(packet, key_info->comment.str, 
 
962
    append_unescaped(packet, key_info->comment.str,
1065
963
                     key_info->comment.length);
1066
964
  }
1067
965
}
1076
974
public:
1077
975
  static void *operator new(size_t size)
1078
976
  {
1079
 
    return (void*) sql_alloc((uint) size);
 
977
    return (void*) sql_alloc((uint32_t) size);
1080
978
  }
1081
 
  static void operator delete(void *ptr __attribute__((unused)),
1082
 
                              size_t size __attribute__((unused)))
 
979
  static void operator delete(void *, size_t)
1083
980
  { TRASH(ptr, size); }
1084
981
 
1085
 
  ulong thread_id;
 
982
  my_thread_id thread_id;
1086
983
  time_t start_time;
1087
984
  uint32_t   command;
1088
985
  const char *user,*host,*db,*proc_info,*state_info;
1093
990
template class I_List<thread_info>;
1094
991
#endif
1095
992
 
1096
 
void mysqld_list_processes(THD *thd,const char *user, bool verbose)
 
993
void mysqld_list_processes(Session *session,const char *user, bool)
1097
994
{
1098
995
  Item *field;
1099
996
  List<Item> field_list;
1100
997
  I_List<thread_info> thread_infos;
1101
 
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
1102
 
                           PROCESS_LIST_WIDTH);
1103
 
  Protocol *protocol= thd->protocol;
 
998
  Protocol *protocol= session->protocol;
1104
999
 
1105
1000
  field_list.push_back(new Item_int("Id", 0, MY_INT32_NUM_DECIMAL_DIGITS));
1106
1001
  field_list.push_back(new Item_empty_string("User",16));
1107
1002
  field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
1108
1003
  field_list.push_back(field=new Item_empty_string("db",NAME_CHAR_LEN));
1109
 
  field->maybe_null=1;
 
1004
  field->maybe_null= true;
1110
1005
  field_list.push_back(new Item_empty_string("Command",16));
1111
1006
  field_list.push_back(new Item_return_int("Time",7, DRIZZLE_TYPE_LONG));
1112
1007
  field_list.push_back(field=new Item_empty_string("State",30));
1113
 
  field->maybe_null=1;
1114
 
  field_list.push_back(field=new Item_empty_string("Info",max_query_length));
1115
 
  field->maybe_null=1;
1116
 
  if (protocol->send_fields(&field_list,
1117
 
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
 
1008
  field->maybe_null= true;
 
1009
  field_list.push_back(field=new Item_empty_string("Info", PROCESS_LIST_WIDTH));
 
1010
  field->maybe_null= true;
 
1011
  if (protocol->sendFields(&field_list,
 
1012
                           Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
1118
1013
    return;
1119
1014
 
1120
1015
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1121
 
  if (!thd->killed)
 
1016
  if (!session->killed)
1122
1017
  {
1123
 
    I_List_iterator<THD> it(threads);
1124
 
    THD *tmp;
1125
 
    while ((tmp=it++))
 
1018
    Session *tmp;
 
1019
    for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
1126
1020
    {
1127
 
      Security_context *tmp_sctx= tmp->security_ctx;
 
1021
      tmp= *it;
 
1022
      Security_context *tmp_sctx= &tmp->security_ctx;
1128
1023
      struct st_my_thread_var *mysys_var;
1129
 
      if ((tmp->vio_ok() || tmp->system_thread) && (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
 
1024
      if (tmp->protocol->isConnected() && (!user || (tmp_sctx->user.c_str() && !strcmp(tmp_sctx->user.c_str(), user))))
1130
1025
      {
1131
 
        thread_info *thd_info= new thread_info;
 
1026
        thread_info *session_info= new thread_info;
1132
1027
 
1133
 
        thd_info->thread_id=tmp->thread_id;
1134
 
        thd_info->user= thd->strdup(tmp_sctx->user ? tmp_sctx->user :
1135
 
                                    (tmp->system_thread ?
1136
 
                                     "system user" : "unauthenticated user"));
1137
 
        thd_info->host= thd->strdup(tmp_sctx->ip);
1138
 
        if ((thd_info->db=tmp->db))             // Safe test
1139
 
          thd_info->db=thd->strdup(thd_info->db);
1140
 
        thd_info->command=(int) tmp->command;
 
1028
        session_info->thread_id=tmp->thread_id;
 
1029
        session_info->user= session->strdup(tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() : "unauthenticated user");
 
1030
        session_info->host= session->strdup(tmp_sctx->ip.c_str());
 
1031
        if ((session_info->db=tmp->db))             // Safe test
 
1032
          session_info->db=session->strdup(session_info->db);
 
1033
        session_info->command=(int) tmp->command;
1141
1034
        if ((mysys_var= tmp->mysys_var))
1142
1035
          pthread_mutex_lock(&mysys_var->mutex);
1143
 
        thd_info->proc_info= (char*) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0);
1144
 
        thd_info->state_info= (char*) (tmp->net.reading_or_writing ?
1145
 
                                       (tmp->net.reading_or_writing == 2 ?
1146
 
                                        "Writing to net" :
1147
 
                                        thd_info->command == COM_SLEEP ? NULL :
1148
 
                                        "Reading from net") :
 
1036
 
 
1037
        if (tmp->killed == Session::KILL_CONNECTION)
 
1038
          session_info->proc_info= (char*) "Killed";
 
1039
        else
 
1040
          session_info->proc_info= command_name[session_info->command].str;
 
1041
 
 
1042
        session_info->state_info= (char*) (tmp->protocol->isWriting() ?
 
1043
                                           "Writing to net" :
 
1044
                                           tmp->protocol->isReading() ?
 
1045
                                           (session_info->command == COM_SLEEP ?
 
1046
                                            NULL : "Reading from net") :
1149
1047
                                       tmp->get_proc_info() ? tmp->get_proc_info() :
1150
1048
                                       tmp->mysys_var &&
1151
1049
                                       tmp->mysys_var->current_cond ?
1153
1051
        if (mysys_var)
1154
1052
          pthread_mutex_unlock(&mysys_var->mutex);
1155
1053
 
1156
 
        thd_info->start_time= tmp->start_time;
1157
 
        thd_info->query=0;
1158
 
        if (tmp->query)
1159
 
        {
1160
 
          /* 
1161
 
            query_length is always set to 0 when we set query = NULL; see
1162
 
            the comment in sql_class.h why this prevents crashes in possible
1163
 
            races with query_length
1164
 
          */
1165
 
          uint32_t length= cmin((uint32_t)max_query_length, tmp->query_length);
1166
 
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1167
 
        }
1168
 
        thread_infos.append(thd_info);
 
1054
        session_info->start_time= tmp->start_time;
 
1055
        session_info->query= NULL;
 
1056
        if (tmp->process_list_info[0])
 
1057
          session_info->query= session->strdup(tmp->process_list_info);
 
1058
        thread_infos.append(session_info);
1169
1059
      }
1170
1060
    }
1171
1061
  }
1172
1062
  pthread_mutex_unlock(&LOCK_thread_count);
1173
1063
 
1174
 
  thread_info *thd_info;
1175
 
  time_t now= my_time(0);
1176
 
  while ((thd_info=thread_infos.get()))
 
1064
  thread_info *session_info;
 
1065
  time_t now= time(NULL);
 
1066
  while ((session_info=thread_infos.get()))
1177
1067
  {
1178
 
    protocol->prepare_for_resend();
1179
 
    protocol->store((uint64_t) thd_info->thread_id);
1180
 
    protocol->store(thd_info->user, system_charset_info);
1181
 
    protocol->store(thd_info->host, system_charset_info);
1182
 
    protocol->store(thd_info->db, system_charset_info);
1183
 
    if (thd_info->proc_info)
1184
 
      protocol->store(thd_info->proc_info, system_charset_info);
1185
 
    else
1186
 
      protocol->store(command_name[thd_info->command].str, system_charset_info);
1187
 
    if (thd_info->start_time)
1188
 
      protocol->store((uint32_t) (now - thd_info->start_time));
1189
 
    else
1190
 
      protocol->store_null();
1191
 
    protocol->store(thd_info->state_info, system_charset_info);
1192
 
    protocol->store(thd_info->query, system_charset_info);
 
1068
    protocol->prepareForResend();
 
1069
    protocol->store((uint64_t) session_info->thread_id);
 
1070
    protocol->store(session_info->user);
 
1071
    protocol->store(session_info->host);
 
1072
    protocol->store(session_info->db);
 
1073
    protocol->store(session_info->proc_info);
 
1074
 
 
1075
    if (session_info->start_time)
 
1076
      protocol->store((uint32_t) (now - session_info->start_time));
 
1077
    else
 
1078
      protocol->store();
 
1079
 
 
1080
    protocol->store(session_info->state_info);
 
1081
    protocol->store(session_info->query);
 
1082
 
1193
1083
    if (protocol->write())
1194
1084
      break; /* purecov: inspected */
1195
1085
  }
1196
 
  my_eof(thd);
 
1086
  session->my_eof();
1197
1087
  return;
1198
1088
}
1199
1089
 
1200
 
int fill_schema_processlist(THD* thd, TableList* tables,
1201
 
                            COND* cond __attribute__((unused)))
 
1090
int fill_schema_processlist(Session* session, TableList* tables, COND*)
1202
1091
{
1203
1092
  Table *table= tables->table;
1204
1093
  const CHARSET_INFO * const cs= system_charset_info;
1205
1094
  char *user;
1206
 
  time_t now= my_time(0);
 
1095
  time_t now= time(NULL);
 
1096
  size_t length;
 
1097
 
 
1098
  if (now == (time_t)-1)
 
1099
    return 1;
1207
1100
 
1208
1101
  user= NULL;
1209
1102
 
1210
1103
  pthread_mutex_lock(&LOCK_thread_count);
1211
1104
 
1212
 
  if (!thd->killed)
 
1105
  if (!session->killed)
1213
1106
  {
1214
 
    I_List_iterator<THD> it(threads);
1215
 
    THD* tmp;
 
1107
    Session* tmp;
1216
1108
 
1217
 
    while ((tmp= it++))
 
1109
    for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
1218
1110
    {
1219
 
      Security_context *tmp_sctx= tmp->security_ctx;
 
1111
      tmp= *it;
 
1112
      Security_context *tmp_sctx= &tmp->security_ctx;
1220
1113
      struct st_my_thread_var *mysys_var;
1221
1114
      const char *val;
1222
1115
 
1223
 
      if ((!tmp->vio_ok() && !tmp->system_thread))
 
1116
      if (! tmp->protocol->isConnected())
1224
1117
        continue;
1225
1118
 
1226
 
      restore_record(table, s->default_values);
 
1119
      table->restoreRecordAsDefault();
1227
1120
      /* ID */
1228
1121
      table->field[0]->store((int64_t) tmp->thread_id, true);
1229
1122
      /* USER */
1230
 
      val= tmp_sctx->user ? tmp_sctx->user :
1231
 
            (tmp->system_thread ? "system user" : "unauthenticated user");
 
1123
      val= tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() : "unauthenticated user";
1232
1124
      table->field[1]->store(val, strlen(val), cs);
1233
1125
      /* HOST */
1234
 
      table->field[2]->store(tmp_sctx->ip, strlen(tmp_sctx->ip), cs);
 
1126
      table->field[2]->store(tmp_sctx->ip.c_str(), strlen(tmp_sctx->ip.c_str()), cs);
1235
1127
      /* DB */
1236
1128
      if (tmp->db)
1237
1129
      {
1242
1134
      if ((mysys_var= tmp->mysys_var))
1243
1135
        pthread_mutex_lock(&mysys_var->mutex);
1244
1136
      /* COMMAND */
1245
 
      if ((val= (char *) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0)))
 
1137
      if ((val= (char *) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0)))
1246
1138
        table->field[4]->store(val, strlen(val), cs);
1247
1139
      else
1248
1140
        table->field[4]->store(command_name[tmp->command].str,
1251
1143
      table->field[5]->store((uint32_t)(tmp->start_time ?
1252
1144
                                      now - tmp->start_time : 0), true);
1253
1145
      /* STATE */
1254
 
      val= (char*) (tmp->net.reading_or_writing ?
1255
 
                    (tmp->net.reading_or_writing == 2 ?
1256
 
                     "Writing to net" :
1257
 
                     tmp->command == COM_SLEEP ? NULL :
1258
 
                     "Reading from net") :
 
1146
      val= (char*) (tmp->protocol->isWriting() ?
 
1147
                    "Writing to net" :
 
1148
                    tmp->protocol->isReading() ?
 
1149
                    (tmp->command == COM_SLEEP ?
 
1150
                     NULL : "Reading from net") :
1259
1151
                    tmp->get_proc_info() ? tmp->get_proc_info() :
1260
1152
                    tmp->mysys_var &&
1261
1153
                    tmp->mysys_var->current_cond ?
1269
1161
      if (mysys_var)
1270
1162
        pthread_mutex_unlock(&mysys_var->mutex);
1271
1163
 
1272
 
      /* INFO */
1273
 
      if (tmp->query)
 
1164
      length= strlen(tmp->process_list_info);
 
1165
 
 
1166
      if (length)
1274
1167
      {
1275
 
        table->field[7]->store(tmp->query,
1276
 
                               cmin((uint32_t)PROCESS_LIST_INFO_WIDTH,
1277
 
                                   tmp->query_length), cs);
 
1168
        table->field[7]->store(tmp->process_list_info, length, cs);
1278
1169
        table->field[7]->set_notnull();
1279
1170
      }
1280
1171
 
1281
 
      if (schema_table_store_record(thd, table))
 
1172
      if (schema_table_store_record(session, table))
1282
1173
      {
1283
1174
        pthread_mutex_unlock(&LOCK_thread_count);
1284
1175
        return(1);
1294
1185
  Status functions
1295
1186
*****************************************************************************/
1296
1187
 
1297
 
static DYNAMIC_ARRAY all_status_vars;
 
1188
static vector<SHOW_VAR *> all_status_vars;
1298
1189
static bool status_vars_inited= 0;
1299
 
static int show_var_cmp(const void *var1, const void *var2)
 
1190
int show_var_cmp(const void *var1, const void *var2)
1300
1191
{
1301
1192
  return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
1302
1193
}
1303
1194
 
1304
 
/*
1305
 
  deletes all the SHOW_UNDEF elements from the array and calls
1306
 
  delete_dynamic() if it's completely empty.
1307
 
*/
1308
 
static void shrink_var_array(DYNAMIC_ARRAY *array)
 
1195
class show_var_cmp_functor
1309
1196
{
1310
 
  uint32_t a,b;
1311
 
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);
 
1197
  public:
 
1198
  show_var_cmp_functor() { }
 
1199
  inline bool operator()(const SHOW_VAR *var1, const SHOW_VAR *var2) const
 
1200
  {
 
1201
    int val= strcmp(var1->name, var2->name);
 
1202
    return (val < 0);
 
1203
  }
 
1204
};
1312
1205
 
1313
 
  for (a= b= 0; b < array->elements; b++)
1314
 
    if (all[b].type != SHOW_UNDEF)
1315
 
      all[a++]= all[b];
1316
 
  if (a)
 
1206
class show_var_remove_if
 
1207
{
 
1208
  public:
 
1209
  show_var_remove_if() { }
 
1210
  inline bool operator()(const SHOW_VAR *curr) const
1317
1211
  {
1318
 
    memset(all+a, 0, sizeof(SHOW_VAR)); // writing NULL-element to the end
1319
 
    array->elements= a;
 
1212
    return (curr->type == SHOW_UNDEF);
1320
1213
  }
1321
 
  else // array is completely empty - delete it
1322
 
    delete_dynamic(array);
1323
 
}
 
1214
};
1324
1215
 
1325
1216
/*
1326
1217
  Adds an array of SHOW_VAR entries to the output of SHOW STATUS
1338
1229
    As a special optimization, if add_status_vars() is called before
1339
1230
    init_status_vars(), it assumes "startup mode" - neither concurrent access
1340
1231
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
1341
 
 
1342
 
    The last entry of the all_status_vars[] should always be {0,0,SHOW_UNDEF}
1343
1232
*/
1344
1233
int add_status_vars(SHOW_VAR *list)
1345
1234
{
1346
1235
  int res= 0;
1347
1236
  if (status_vars_inited)
1348
1237
    pthread_mutex_lock(&LOCK_status);
1349
 
  if (!all_status_vars.buffer && // array is not allocated yet - do it now
1350
 
      my_init_dynamic_array(&all_status_vars, sizeof(SHOW_VAR), 200, 20))
1351
 
  {
1352
 
    res= 1;
1353
 
    goto err;
1354
 
  }
1355
1238
  while (list->name)
1356
 
    res|= insert_dynamic(&all_status_vars, (unsigned char*)list++);
1357
 
  res|= insert_dynamic(&all_status_vars, (unsigned char*)list); // appending NULL-element
1358
 
  all_status_vars.elements--; // but next insert_dynamic should overwite it
 
1239
    all_status_vars.insert(all_status_vars.begin(), list++);
1359
1240
  if (status_vars_inited)
1360
 
    sort_dynamic(&all_status_vars, show_var_cmp);
1361
 
err:
 
1241
    sort(all_status_vars.begin(), all_status_vars.end(),
 
1242
         show_var_cmp_functor());
1362
1243
  if (status_vars_inited)
1363
1244
    pthread_mutex_unlock(&LOCK_status);
1364
1245
  return res;
1374
1255
*/
1375
1256
void init_status_vars()
1376
1257
{
1377
 
  status_vars_inited=1;
1378
 
  sort_dynamic(&all_status_vars, show_var_cmp);
 
1258
  status_vars_inited= 1;
 
1259
  sort(all_status_vars.begin(), all_status_vars.end(),
 
1260
       show_var_cmp_functor());
1379
1261
}
1380
1262
 
1381
1263
void reset_status_vars()
1382
1264
{
1383
 
  SHOW_VAR *ptr= (SHOW_VAR*) all_status_vars.buffer;
1384
 
  SHOW_VAR *last= ptr + all_status_vars.elements;
1385
 
  for (; ptr < last; ptr++)
 
1265
  vector<SHOW_VAR *>::iterator p= all_status_vars.begin();
 
1266
  while (p != all_status_vars.end())
1386
1267
  {
1387
1268
    /* Note that SHOW_LONG_NOFLUSH variables are not reset */
1388
 
    if (ptr->type == SHOW_LONG)
1389
 
      *(ulong*) ptr->value= 0;
1390
 
  }  
 
1269
    if ((*p)->type == SHOW_LONG)
 
1270
      (*p)->value= 0;
 
1271
    ++p;
 
1272
  }
1391
1273
}
1392
1274
 
1393
1275
/*
1396
1278
  DESCRIPTION
1397
1279
    This function is not strictly required if all add_to_status/
1398
1280
    remove_status_vars are properly paired, but it's a safety measure that
1399
 
    deletes everything from the all_status_vars[] even if some
 
1281
    deletes everything from the all_status_vars vector even if some
1400
1282
    remove_status_vars were forgotten
1401
1283
*/
1402
1284
void free_status_vars()
1403
1285
{
1404
 
  delete_dynamic(&all_status_vars);
 
1286
  all_status_vars.clear();
1405
1287
}
1406
1288
 
1407
1289
/*
1423
1305
  if (status_vars_inited)
1424
1306
  {
1425
1307
    pthread_mutex_lock(&LOCK_status);
1426
 
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1427
 
    int a= 0, b= all_status_vars.elements, c= (a+b)/2;
 
1308
    SHOW_VAR *all= all_status_vars.front();
 
1309
    int a= 0, b= all_status_vars.size(), c= (a+b)/2;
1428
1310
 
1429
1311
    for (; list->name; list++)
1430
1312
    {
1431
1313
      int res= 0;
1432
 
      for (a= 0, b= all_status_vars.elements; b-a > 1; c= (a+b)/2)
 
1314
      for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
1433
1315
      {
1434
1316
        res= show_var_cmp(list, all+c);
1435
1317
        if (res < 0)
1442
1324
      if (res == 0)
1443
1325
        all[c].type= SHOW_UNDEF;
1444
1326
    }
1445
 
    shrink_var_array(&all_status_vars);
 
1327
    /* removes all the SHOW_UNDEF elements from the vector */
 
1328
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
1329
                            all_status_vars.end(),show_var_remove_if()),
 
1330
                            all_status_vars.end());
1446
1331
    pthread_mutex_unlock(&LOCK_status);
1447
1332
  }
1448
1333
  else
1449
1334
  {
1450
 
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
 
1335
    SHOW_VAR *all= all_status_vars.front();
1451
1336
    uint32_t i;
1452
1337
    for (; list->name; list++)
1453
1338
    {
1454
 
      for (i= 0; i < all_status_vars.elements; i++)
 
1339
      for (i= 0; i < all_status_vars.size(); i++)
1455
1340
      {
1456
1341
        if (show_var_cmp(list, all+i))
1457
1342
          continue;
1459
1344
        break;
1460
1345
      }
1461
1346
    }
1462
 
    shrink_var_array(&all_status_vars);
 
1347
    /* removes all the SHOW_UNDEF elements from the vector */
 
1348
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
1349
                            all_status_vars.end(),show_var_remove_if()),
 
1350
                            all_status_vars.end());
1463
1351
  }
1464
1352
}
1465
1353
 
1469
1357
    *buf= my_toupper(system_charset_info, *buf);
1470
1358
}
1471
1359
 
1472
 
static bool show_status_array(THD *thd, const char *wild,
 
1360
static bool show_status_array(Session *session, const char *wild,
1473
1361
                              SHOW_VAR *variables,
1474
1362
                              enum enum_var_type value_type,
1475
1363
                              struct system_status_var *status_var,
1476
1364
                              const char *prefix, Table *table,
1477
1365
                              bool ucase_names)
1478
1366
{
1479
 
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, long);
 
1367
  MY_ALIGNED_BYTE_ARRAY(buff_data, SHOW_VAR_FUNC_BUFF_SIZE, int64_t);
1480
1368
  char * const buff= (char *) &buff_data;
1481
1369
  char *prefix_end;
1482
1370
  /* the variable name should not be longer than 64 characters */
1483
1371
  char name_buffer[64];
1484
1372
  int len;
1485
 
  LEX_STRING null_lex_str;
1486
1373
  SHOW_VAR tmp, *var;
1487
1374
 
1488
 
  null_lex_str.str= 0;                          // For sys_var->value_ptr()
1489
 
  null_lex_str.length= 0;
 
1375
  prefix_end= strncpy(name_buffer, prefix, sizeof(name_buffer)-1);
 
1376
  prefix_end+= strlen(prefix);
1490
1377
 
1491
 
  prefix_end=my_stpncpy(name_buffer, prefix, sizeof(name_buffer)-1);
1492
1378
  if (*prefix)
1493
1379
    *prefix_end++= '_';
1494
1380
  len=name_buffer + sizeof(name_buffer) - prefix_end;
1495
1381
 
1496
1382
  for (; variables->name; variables++)
1497
1383
  {
1498
 
    my_stpncpy(prefix_end, variables->name, len);
 
1384
    strncpy(prefix_end, variables->name, len);
1499
1385
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
1500
1386
    if (ucase_names)
1501
1387
      make_upper(name_buffer);
1505
1391
      Repeat as necessary, if new var is again SHOW_FUNC
1506
1392
    */
1507
1393
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
1508
 
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(thd, &tmp, buff);
 
1394
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(&tmp, buff);
1509
1395
 
1510
1396
    SHOW_TYPE show_type=var->type;
1511
1397
    if (show_type == SHOW_ARRAY)
1512
1398
    {
1513
 
      show_status_array(thd, wild, (SHOW_VAR *) var->value, value_type,
 
1399
      show_status_array(session, wild, (SHOW_VAR *) var->value, value_type,
1514
1400
                        status_var, name_buffer, table, ucase_names);
1515
1401
    }
1516
1402
    else
1520
1406
      {
1521
1407
        char *value=var->value;
1522
1408
        const char *pos, *end;                  // We assign a lot of const's
1523
 
 
1524
1409
        pthread_mutex_lock(&LOCK_global_system_variables);
1525
1410
 
1526
1411
        if (show_type == SHOW_SYS)
1527
1412
        {
1528
1413
          show_type= ((sys_var*) value)->show_type();
1529
 
          value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type,
1530
 
                                                           &null_lex_str);
 
1414
          value= (char*) ((sys_var*) value)->value_ptr(session, value_type,
 
1415
                                                       &null_lex_str);
1531
1416
        }
1532
1417
 
1533
1418
        pos= end= buff;
1547
1432
          value= ((char *) status_var + (ulong) value);
1548
1433
          /* fall through */
1549
1434
        case SHOW_LONG:
1550
 
        case SHOW_LONG_NOFLUSH: // the difference lies in refresh_status()
1551
1435
          end= int10_to_str(*(long*) value, buff, 10);
1552
1436
          break;
1553
1437
        case SHOW_LONGLONG_STATUS:
1556
1440
        case SHOW_LONGLONG:
1557
1441
          end= int64_t10_to_str(*(int64_t*) value, buff, 10);
1558
1442
          break;
 
1443
        case SHOW_SIZE:
 
1444
          {
 
1445
            stringstream ss (stringstream::in);
 
1446
            ss << *(size_t*) value;
 
1447
 
 
1448
            string str= ss.str();
 
1449
            strncpy(buff, str.c_str(), str.length());
 
1450
            end= buff+ str.length();
 
1451
          }
 
1452
          break;
1559
1453
        case SHOW_HA_ROWS:
1560
1454
          end= int64_t10_to_str((int64_t) *(ha_rows*) value, buff, 10);
1561
1455
          break;
1562
1456
        case SHOW_BOOL:
1563
 
          end= my_stpcpy(buff, *(bool*) value ? "ON" : "OFF");
 
1457
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
1564
1458
          break;
1565
1459
        case SHOW_MY_BOOL:
1566
 
          end= my_stpcpy(buff, *(bool*) value ? "ON" : "OFF");
 
1460
          end+= sprintf(buff,"%s", *(bool*) value ? "ON" : "OFF");
1567
1461
          break;
1568
1462
        case SHOW_INT:
 
1463
        case SHOW_INT_NOFLUSH: // the difference lies in refresh_status()
1569
1464
          end= int10_to_str((long) *(uint32_t*) value, buff, 10);
1570
1465
          break;
1571
1466
        case SHOW_HAVE:
1572
1467
        {
1573
 
          SHOW_COMP_OPTION tmp= *(SHOW_COMP_OPTION*) value;
1574
 
          pos= show_comp_option_name[(int) tmp];
 
1468
          SHOW_COMP_OPTION tmp_option= *(SHOW_COMP_OPTION *)value;
 
1469
          pos= show_comp_option_name[(int) tmp_option];
1575
1470
          end= strchr(pos, '\0');
1576
1471
          break;
1577
1472
        }
1604
1499
          assert(0);
1605
1500
          break;
1606
1501
        }
1607
 
        restore_record(table, s->default_values);
 
1502
        table->restoreRecordAsDefault();
1608
1503
        table->field[0]->store(name_buffer, strlen(name_buffer),
1609
1504
                               system_charset_info);
1610
1505
        table->field[1]->store(pos, (uint32_t) (end - pos), system_charset_info);
1612
1507
 
1613
1508
        pthread_mutex_unlock(&LOCK_global_system_variables);
1614
1509
 
1615
 
        if (schema_table_store_record(thd, table))
1616
 
          return(true);
 
1510
        if (schema_table_store_record(session, table))
 
1511
          return true;
1617
1512
      }
1618
1513
    }
1619
1514
  }
1620
1515
 
1621
 
  return(false);
 
1516
  return false;
1622
1517
}
1623
1518
 
1624
1519
 
1626
1521
 
1627
1522
void calc_sum_of_all_status(STATUS_VAR *to)
1628
1523
{
1629
 
 
1630
1524
  /* Ensure that thread id not killed during loop */
1631
1525
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1632
1526
 
1633
 
  I_List_iterator<THD> it(threads);
1634
 
  THD *tmp;
1635
 
  
1636
1527
  /* Get global values as base */
1637
1528
  *to= global_status_var;
1638
 
  
 
1529
 
1639
1530
  /* Add to this status from existing threads */
1640
 
  while ((tmp= it++))
1641
 
    add_to_status(to, &tmp->status_var);
1642
 
  
 
1531
  for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
 
1532
  {
 
1533
    add_to_status(to, &((*it)->status_var));
 
1534
  }
 
1535
 
1643
1536
  pthread_mutex_unlock(&LOCK_thread_count);
1644
1537
  return;
1645
1538
}
1646
1539
 
1647
1540
 
1648
1541
/* This is only used internally, but we need it here as a forward reference */
1649
 
extern ST_SCHEMA_TABLE schema_tables[];
 
1542
extern InfoSchemaTable schema_tables[];
1650
1543
 
1651
1544
typedef struct st_lookup_field_values
1652
1545
{
1661
1554
 
1662
1555
  SYNOPSIS
1663
1556
    schema_table_store_record()
1664
 
    thd                   thread handler
 
1557
    session                   thread handler
1665
1558
    table                 Information schema table to be updated
1666
1559
 
1667
1560
  RETURN
1669
1562
    1                     error
1670
1563
*/
1671
1564
 
1672
 
bool schema_table_store_record(THD *thd, Table *table)
 
1565
bool schema_table_store_record(Session *session, Table *table)
1673
1566
{
1674
1567
  int error;
1675
1568
  if ((error= table->file->ha_write_row(table->record[0])))
1676
1569
  {
1677
 
    TMP_TABLE_PARAM *param= table->pos_in_table_list->schema_table_param;
 
1570
    Tmp_Table_Param *param= table->pos_in_table_list->schema_table_param;
1678
1571
 
1679
 
    if (create_myisam_from_heap(thd, table, param->start_recinfo, 
 
1572
    if (create_myisam_from_heap(session, table, param->start_recinfo,
1680
1573
                                &param->recinfo, error, 0))
1681
 
      return 1;
 
1574
      return true;
1682
1575
  }
1683
 
  return 0;
 
1576
  return false;
1684
1577
}
1685
1578
 
1686
1579
 
1687
 
int make_table_list(THD *thd, SELECT_LEX *sel,
 
1580
int make_table_list(Session *session, Select_Lex *sel,
1688
1581
                    LEX_STRING *db_name, LEX_STRING *table_name)
1689
1582
{
1690
1583
  Table_ident *table_ident;
1691
 
  table_ident= new Table_ident(thd, *db_name, *table_name, 1);
 
1584
  table_ident= new Table_ident(session, *db_name, *table_name, 1);
1692
1585
  sel->init_query();
1693
 
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
 
1586
  if (!sel->add_table_to_list(session, table_ident, 0, 0, TL_READ))
1694
1587
    return 1;
1695
1588
  return 0;
1696
1589
}
1697
1590
 
1698
1591
 
1699
1592
/**
1700
 
  @brief    Get lookup value from the part of 'WHERE' condition 
 
1593
  @brief    Get lookup value from the part of 'WHERE' condition
1701
1594
 
1702
 
  @details This function gets lookup value from 
1703
 
           the part of 'WHERE' condition if it's possible and 
 
1595
  @details This function gets lookup value from
 
1596
           the part of 'WHERE' condition if it's possible and
1704
1597
           fill appropriate lookup_field_vals struct field
1705
1598
           with this value.
1706
1599
 
1707
 
  @param[in]      thd                   thread handler
 
1600
  @param[in]      session                   thread handler
1708
1601
  @param[in]      item_func             part of WHERE condition
1709
1602
  @param[in]      table                 I_S table
1710
 
  @param[in, out] lookup_field_vals     Struct which holds lookup values 
 
1603
  @param[in, out] lookup_field_vals     Struct which holds lookup values
1711
1604
 
1712
1605
  @return
1713
1606
    0             success
1714
1607
    1             error, there can be no matching records for the condition
1715
1608
*/
1716
1609
 
1717
 
bool get_lookup_value(THD *thd, Item_func *item_func,
1718
 
                      TableList *table, 
 
1610
bool get_lookup_value(Session *session, Item_func *item_func,
 
1611
                      TableList *table,
1719
1612
                      LOOKUP_FIELD_VALUES *lookup_field_vals)
1720
1613
{
1721
 
  ST_SCHEMA_TABLE *schema_table= table->schema_table;
 
1614
  InfoSchemaTable *schema_table= table->schema_table;
1722
1615
  ST_FIELD_INFO *field_info= schema_table->fields_info;
1723
1616
  const char *field_name1= schema_table->idx_field1 >= 0 ?
1724
1617
    field_info[schema_table->idx_field1].field_name : "";
1763
1656
                               (unsigned char *) item_field->field_name,
1764
1657
                               strlen(item_field->field_name), 0))
1765
1658
    {
1766
 
      thd->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
 
1659
      session->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
1767
1660
                           tmp_str->length(), false);
1768
1661
    }
1769
1662
    /* Lookup value is table name */
1772
1665
                                    (unsigned char *) item_field->field_name,
1773
1666
                                    strlen(item_field->field_name), 0))
1774
1667
    {
1775
 
      thd->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
 
1668
      session->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
1776
1669
                           tmp_str->length(), false);
1777
1670
    }
1778
1671
  }
1781
1674
 
1782
1675
 
1783
1676
/**
1784
 
  @brief    Calculates lookup values from 'WHERE' condition 
 
1677
  @brief    Calculates lookup values from 'WHERE' condition
1785
1678
 
1786
1679
  @details This function calculates lookup value(database name, table name)
1787
 
           from 'WHERE' condition if it's possible and 
 
1680
           from 'WHERE' condition if it's possible and
1788
1681
           fill lookup_field_vals struct fields with these values.
1789
1682
 
1790
 
  @param[in]      thd                   thread handler
 
1683
  @param[in]      session                   thread handler
1791
1684
  @param[in]      cond                  WHERE condition
1792
1685
  @param[in]      table                 I_S table
1793
 
  @param[in, out] lookup_field_vals     Struct which holds lookup values 
 
1686
  @param[in, out] lookup_field_vals     Struct which holds lookup values
1794
1687
 
1795
1688
  @return
1796
1689
    0             success
1797
1690
    1             error, there can be no matching records for the condition
1798
1691
*/
1799
1692
 
1800
 
bool calc_lookup_values_from_cond(THD *thd, COND *cond, TableList *table,
 
1693
bool calc_lookup_values_from_cond(Session *session, COND *cond, TableList *table,
1801
1694
                                  LOOKUP_FIELD_VALUES *lookup_field_vals)
1802
1695
{
1803
1696
  if (!cond)
1813
1706
      {
1814
1707
        if (item->type() == Item::FUNC_ITEM)
1815
1708
        {
1816
 
          if (get_lookup_value(thd, (Item_func*)item, table, lookup_field_vals))
 
1709
          if (get_lookup_value(session, (Item_func*)item, table, lookup_field_vals))
1817
1710
            return 1;
1818
1711
        }
1819
1712
        else
1820
1713
        {
1821
 
          if (calc_lookup_values_from_cond(thd, item, table, lookup_field_vals))
 
1714
          if (calc_lookup_values_from_cond(session, item, table, lookup_field_vals))
1822
1715
            return 1;
1823
1716
        }
1824
1717
      }
1826
1719
    return 0;
1827
1720
  }
1828
1721
  else if (cond->type() == Item::FUNC_ITEM &&
1829
 
           get_lookup_value(thd, (Item_func*) cond, table, lookup_field_vals))
 
1722
           get_lookup_value(session, (Item_func*) cond, table, lookup_field_vals))
1830
1723
    return 1;
1831
1724
  return 0;
1832
1725
}
1847
1740
  {
1848
1741
    Item_field *item_field= (Item_field*)item;
1849
1742
    const CHARSET_INFO * const cs= system_charset_info;
1850
 
    ST_SCHEMA_TABLE *schema_table= table->schema_table;
 
1743
    InfoSchemaTable *schema_table= table->schema_table;
1851
1744
    ST_FIELD_INFO *field_info= schema_table->fields_info;
1852
1745
    const char *field_name1= schema_table->idx_field1 >= 0 ?
1853
1746
      field_info[schema_table->idx_field1].field_name : "";
1936
1829
           from LEX struct and fill lookup_field_vals struct field
1937
1830
           with these values.
1938
1831
 
1939
 
  @param[in]      thd                   thread handler
 
1832
  @param[in]      session                   thread handler
1940
1833
  @param[in]      cond                  WHERE condition
1941
1834
  @param[in]      tables                I_S table
1942
 
  @param[in, out] lookup_field_values   Struct which holds lookup values 
 
1835
  @param[in, out] lookup_field_values   Struct which holds lookup values
1943
1836
 
1944
1837
  @return
1945
1838
    0             success
1946
1839
    1             error, there can be no matching records for the condition
1947
1840
*/
1948
1841
 
1949
 
bool get_lookup_field_values(THD *thd, COND *cond, TableList *tables,
 
1842
bool get_lookup_field_values(Session *session, COND *cond, TableList *tables,
1950
1843
                             LOOKUP_FIELD_VALUES *lookup_field_values)
1951
1844
{
1952
 
  LEX *lex= thd->lex;
 
1845
  LEX *lex= session->lex;
1953
1846
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
1954
1847
  memset(lookup_field_values, 0, sizeof(LOOKUP_FIELD_VALUES));
1955
1848
  switch (lex->sql_command) {
1977
1870
      The "default" is for queries over I_S.
1978
1871
      All previous cases handle SHOW commands.
1979
1872
    */
1980
 
    return calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values);
 
1873
    return calc_lookup_values_from_cond(session, cond, tables, lookup_field_values);
1981
1874
  }
1982
1875
}
1983
1876
 
1984
1877
 
1985
 
enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table)
 
1878
enum enum_schema_tables get_schema_table_idx(InfoSchemaTable *schema_table)
1986
1879
{
1987
1880
  return (enum enum_schema_tables) (schema_table - &schema_tables[0]);
1988
1881
}
1993
1886
 
1994
1887
  SYNOPSIS
1995
1888
    make_db_list()
1996
 
    thd                   thread handler
 
1889
    session                   thread handler
1997
1890
    files                 list of db names
1998
1891
    wild                  wild string
1999
1892
    idx_field_vals        idx_field_vals->db_name contains db name or
2000
1893
                          wild string
2001
1894
    with_i_schema         returns 1 if we added 'IS' name to list
2002
 
                          otherwise returns 0 
 
1895
                          otherwise returns 0
2003
1896
 
2004
1897
  RETURN
2005
1898
    zero                  success
2006
1899
    non-zero              error
2007
1900
*/
2008
1901
 
2009
 
int make_db_list(THD *thd, List<LEX_STRING> *files,
 
1902
int make_db_list(Session *session, List<LEX_STRING> *files,
2010
1903
                 LOOKUP_FIELD_VALUES *lookup_field_vals,
2011
1904
                 bool *with_i_schema)
2012
1905
{
2013
1906
  LEX_STRING *i_s_name_copy= 0;
2014
 
  i_s_name_copy= thd->make_lex_string(i_s_name_copy,
2015
 
                                      INFORMATION_SCHEMA_NAME.str,
2016
 
                                      INFORMATION_SCHEMA_NAME.length, true);
 
1907
  i_s_name_copy= session->make_lex_string(i_s_name_copy,
 
1908
                                      INFORMATION_SCHEMA_NAME.c_str(),
 
1909
                                      INFORMATION_SCHEMA_NAME.length(), true);
2017
1910
  *with_i_schema= 0;
2018
1911
  if (lookup_field_vals->wild_db_value)
2019
1912
  {
2023
1916
      LIKE clause (see also get_index_field_values() function)
2024
1917
    */
2025
1918
    if (!lookup_field_vals->db_value.str ||
2026
 
        !wild_case_compare(system_charset_info, 
2027
 
                           INFORMATION_SCHEMA_NAME.str,
 
1919
        !wild_case_compare(system_charset_info,
 
1920
                           INFORMATION_SCHEMA_NAME.c_str(),
2028
1921
                           lookup_field_vals->db_value.str))
2029
1922
    {
2030
1923
      *with_i_schema= 1;
2031
1924
      if (files->push_back(i_s_name_copy))
2032
1925
        return 1;
2033
1926
    }
2034
 
    return (find_files(thd, files, NULL, mysql_data_home,
 
1927
    return (find_files(session, files, NULL, drizzle_data_home,
2035
1928
                       lookup_field_vals->db_value.str, 1) != FIND_FILES_OK);
2036
1929
  }
2037
1930
 
2042
1935
  */
2043
1936
  if (lookup_field_vals->db_value.str)
2044
1937
  {
2045
 
    if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.str,
 
1938
    if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.c_str(),
2046
1939
                       lookup_field_vals->db_value.str))
2047
1940
    {
2048
1941
      *with_i_schema= 1;
2062
1955
  if (files->push_back(i_s_name_copy))
2063
1956
    return 1;
2064
1957
  *with_i_schema= 1;
2065
 
  return (find_files(thd, files, NULL,
2066
 
                     mysql_data_home, NULL, 1) != FIND_FILES_OK);
 
1958
  return (find_files(session, files, NULL,
 
1959
                     drizzle_data_home, NULL, 1) != FIND_FILES_OK);
2067
1960
}
2068
1961
 
2069
1962
 
2070
 
struct st_add_schema_table 
 
1963
struct st_add_schema_table
2071
1964
{
2072
1965
  List<LEX_STRING> *files;
2073
1966
  const char *wild;
2074
1967
};
2075
1968
 
2076
1969
 
2077
 
static bool add_schema_table(THD *thd, plugin_ref plugin,
2078
 
                                void* p_data)
 
1970
class AddSchemaTable : public unary_function<InfoSchemaTable *, bool>
2079
1971
{
2080
 
  LEX_STRING *file_name= 0;
2081
 
  st_add_schema_table *data= (st_add_schema_table *)p_data;
2082
 
  List<LEX_STRING> *file_list= data->files;
2083
 
  const char *wild= data->wild;
2084
 
  ST_SCHEMA_TABLE *schema_table= plugin_data(plugin, ST_SCHEMA_TABLE *);
2085
 
 
2086
 
  if (schema_table->hidden)
2087
 
      return(0);
2088
 
  if (wild)
 
1972
  Session *session;
 
1973
  st_add_schema_table *data;
 
1974
public:
 
1975
  AddSchemaTable(Session *session_arg, st_add_schema_table *data_arg)
 
1976
    : session(session_arg), data(data_arg) {}
 
1977
  result_type operator() (argument_type schema_table)
2089
1978
  {
2090
 
    if (lower_case_table_names)
 
1979
    LEX_STRING *file_name= 0;
 
1980
    List<LEX_STRING> *file_list= data->files;
 
1981
    const char *wild= data->wild;
 
1982
  
 
1983
    if (schema_table->hidden)
 
1984
        return(0);
 
1985
    if (wild)
2091
1986
    {
2092
1987
      if (wild_case_compare(files_charset_info,
2093
1988
                            schema_table->table_name,
2094
1989
                            wild))
2095
1990
        return(0);
2096
1991
    }
2097
 
    else if (wild_compare(schema_table->table_name, wild, 0))
 
1992
  
 
1993
    if ((file_name= session->make_lex_string(file_name, schema_table->table_name,
 
1994
                                         strlen(schema_table->table_name),
 
1995
                                         true)) &&
 
1996
        !file_list->push_back(file_name))
2098
1997
      return(0);
 
1998
    return(1);
2099
1999
  }
2100
 
 
2101
 
  if ((file_name= thd->make_lex_string(file_name, schema_table->table_name,
2102
 
                                       strlen(schema_table->table_name),
2103
 
                                       true)) &&
2104
 
      !file_list->push_back(file_name))
2105
 
    return(0);
2106
 
  return(1);
2107
 
}
2108
 
 
2109
 
 
2110
 
int schema_tables_add(THD *thd, List<LEX_STRING> *files, const char *wild)
 
2000
};
 
2001
 
 
2002
 
 
2003
int schema_tables_add(Session *session, List<LEX_STRING> *files, const char *wild)
2111
2004
{
2112
2005
  LEX_STRING *file_name= 0;
2113
 
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
 
2006
  InfoSchemaTable *tmp_schema_table= schema_tables;
2114
2007
  st_add_schema_table add_data;
2115
2008
 
2116
2009
  for (; tmp_schema_table->table_name; tmp_schema_table++)
2119
2012
      continue;
2120
2013
    if (wild)
2121
2014
    {
2122
 
      if (lower_case_table_names)
2123
 
      {
2124
 
        if (wild_case_compare(files_charset_info,
2125
 
                              tmp_schema_table->table_name,
2126
 
                              wild))
2127
 
          continue;
2128
 
      }
2129
 
      else if (wild_compare(tmp_schema_table->table_name, wild, 0))
 
2015
      if (wild_case_compare(files_charset_info,
 
2016
                            tmp_schema_table->table_name,
 
2017
                            wild))
2130
2018
        continue;
2131
2019
    }
2132
 
    if ((file_name= 
2133
 
         thd->make_lex_string(file_name, tmp_schema_table->table_name,
 
2020
    if ((file_name=
 
2021
         session->make_lex_string(file_name, tmp_schema_table->table_name,
2134
2022
                              strlen(tmp_schema_table->table_name), true)) &&
2135
2023
        !files->push_back(file_name))
2136
2024
      continue;
2139
2027
 
2140
2028
  add_data.files= files;
2141
2029
  add_data.wild= wild;
2142
 
  if (plugin_foreach(thd, add_schema_table,
2143
 
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &add_data))
2144
 
    return(1);
2145
 
 
2146
 
  return(0);
 
2030
  vector<InfoSchemaTable *>::iterator iter=
 
2031
    find_if(all_schema_tables.begin(), all_schema_tables.end(),
 
2032
           AddSchemaTable(session, &add_data));
 
2033
  if (iter != all_schema_tables.end())
 
2034
    return 1;
 
2035
  return 0 ;
2147
2036
}
2148
2037
 
2149
2038
 
2153
2042
  @details        The function creates the list of table names in
2154
2043
                  database
2155
2044
 
2156
 
  @param[in]      thd                   thread handler
 
2045
  @param[in]      session                   thread handler
2157
2046
  @param[in]      table_names           List of table names in database
2158
2047
  @param[in]      lex                   pointer to LEX struct
2159
2048
  @param[in]      lookup_field_vals     pointer to LOOKUP_FIELD_VALUE struct
2167
2056
*/
2168
2057
 
2169
2058
static int
2170
 
make_table_name_list(THD *thd, List<LEX_STRING> *table_names, LEX *lex,
 
2059
make_table_name_list(Session *session, List<LEX_STRING> *table_names, LEX *lex,
2171
2060
                     LOOKUP_FIELD_VALUES *lookup_field_vals,
2172
2061
                     bool with_i_schema, LEX_STRING *db_name)
2173
2062
{
2174
2063
  char path[FN_REFLEN];
2175
 
  build_table_filename(path, sizeof(path), db_name->str, "", "", 0);
 
2064
  build_table_filename(path, sizeof(path), db_name->str, "", false);
2176
2065
  if (!lookup_field_vals->wild_table_value &&
2177
2066
      lookup_field_vals->table_value.str)
2178
2067
  {
2179
2068
    if (with_i_schema)
2180
2069
    {
2181
 
      if (find_schema_table(thd, lookup_field_vals->table_value.str))
 
2070
      if (find_schema_table(lookup_field_vals->table_value.str))
2182
2071
      {
2183
2072
        if (table_names->push_back(&lookup_field_vals->table_value))
2184
2073
          return 1;
2185
2074
      }
2186
2075
    }
2187
2076
    else
2188
 
    {    
 
2077
    {
2189
2078
      if (table_names->push_back(&lookup_field_vals->table_value))
2190
2079
        return 1;
2191
2080
    }
2197
2086
    to the list
2198
2087
  */
2199
2088
  if (with_i_schema)
2200
 
    return (schema_tables_add(thd, table_names,
 
2089
    return (schema_tables_add(session, table_names,
2201
2090
                              lookup_field_vals->table_value.str));
2202
2091
 
2203
 
  find_files_result res= find_files(thd, table_names, db_name->str, path,
 
2092
  find_files_result res= find_files(session, table_names, db_name->str, path,
2204
2093
                                    lookup_field_vals->table_value.str, 0);
2205
2094
  if (res != FIND_FILES_OK)
2206
2095
  {
2214
2103
    {
2215
2104
      if (lex->sql_command != SQLCOM_SELECT)
2216
2105
        return 1;
2217
 
      thd->clear_error();
 
2106
      session->clear_error();
2218
2107
      return 2;
2219
2108
    }
2220
2109
    return 1;
2226
2115
/**
2227
2116
  @brief          Fill I_S table for SHOW COLUMNS|INDEX commands
2228
2117
 
2229
 
  @param[in]      thd                      thread handler
 
2118
  @param[in]      session                      thread handler
2230
2119
  @param[in]      tables                   TableList for I_S table
2231
2120
  @param[in]      schema_table             pointer to I_S structure
2232
2121
  @param[in]      open_tables_state_backup pointer to Open_tables_state object
2239
2128
    @retval       1           error
2240
2129
*/
2241
2130
 
2242
 
static int 
2243
 
fill_schema_show_cols_or_idxs(THD *thd, TableList *tables,
2244
 
                              ST_SCHEMA_TABLE *schema_table,
 
2131
static int
 
2132
fill_schema_show_cols_or_idxs(Session *session, TableList *tables,
 
2133
                              InfoSchemaTable *schema_table,
2245
2134
                              Open_tables_state *open_tables_state_backup)
2246
2135
{
2247
 
  LEX *lex= thd->lex;
 
2136
  LEX *lex= session->lex;
2248
2137
  bool res;
2249
2138
  LEX_STRING tmp_lex_string, tmp_lex_string1, *db_name, *table_name;
2250
2139
  enum_sql_command save_sql_command= lex->sql_command;
2255
2144
 
2256
2145
  lex->all_selects_list= tables->schema_select_lex;
2257
2146
  /*
2258
 
    Restore thd->temporary_tables to be able to process
 
2147
    Restore session->temporary_tables to be able to process
2259
2148
    temporary tables(only for 'show index' & 'show columns').
2260
2149
    This should be changed when processing of temporary tables for
2261
2150
    I_S tables will be done.
2262
2151
  */
2263
 
  thd->temporary_tables= open_tables_state_backup->temporary_tables;
 
2152
  session->temporary_tables= open_tables_state_backup->temporary_tables;
2264
2153
  /*
2265
2154
    Let us set fake sql_command so views won't try to merge
2266
2155
    themselves into main statement. If we don't do this,
2267
2156
    SELECT * from information_schema.xxxx will cause problems.
2268
 
    SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()' 
 
2157
    SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()'
2269
2158
  */
2270
2159
  lex->sql_command= SQLCOM_SHOW_FIELDS;
2271
 
  res= open_normal_and_derived_tables(thd, show_table_list,
 
2160
  res= open_normal_and_derived_tables(session, show_table_list,
2272
2161
                                      DRIZZLE_LOCK_IGNORE_FLUSH);
2273
2162
  lex->sql_command= save_sql_command;
2274
2163
  /*
2275
2164
    get_all_tables() returns 1 on failure and 0 on success thus
2276
2165
    return only these and not the result code of ::process_table()
2277
2166
 
2278
 
    We should use show_table_list->alias instead of 
 
2167
    We should use show_table_list->alias instead of
2279
2168
    show_table_list->table_name because table_name
2280
2169
    could be changed during opening of I_S tables. It's safe
2281
 
    to use alias because alias contains original table name 
2282
 
    in this case(this part of code is used only for 
 
2170
    to use alias because alias contains original table name
 
2171
    in this case(this part of code is used only for
2283
2172
    'show columns' & 'show statistics' commands).
2284
2173
  */
2285
 
   table_name= thd->make_lex_string(&tmp_lex_string1, show_table_list->alias,
 
2174
   table_name= session->make_lex_string(&tmp_lex_string1, show_table_list->alias,
2286
2175
                                    strlen(show_table_list->alias), false);
2287
 
   db_name= thd->make_lex_string(&tmp_lex_string, show_table_list->db,
 
2176
   db_name= session->make_lex_string(&tmp_lex_string, show_table_list->db,
2288
2177
                                 show_table_list->db_length, false);
2289
 
      
2290
 
 
2291
 
   error= test(schema_table->process_table(thd, show_table_list,
 
2178
 
 
2179
 
 
2180
   error= test(schema_table->process_table(session, show_table_list,
2292
2181
                                           table, res, db_name,
2293
2182
                                           table_name));
2294
 
   thd->temporary_tables= 0;
2295
 
   close_tables_for_reopen(thd, &show_table_list);
 
2183
   session->temporary_tables= 0;
 
2184
   session->close_tables_for_reopen(&show_table_list);
 
2185
 
2296
2186
   return(error);
2297
2187
}
2298
2188
 
2300
2190
/**
2301
2191
  @brief          Fill I_S table for SHOW Table NAMES commands
2302
2192
 
2303
 
  @param[in]      thd                      thread handler
 
2193
  @param[in]      session                      thread handler
2304
2194
  @param[in]      table                    Table struct for I_S table
2305
2195
  @param[in]      db_name                  database name
2306
2196
  @param[in]      table_name               table name
2311
2201
    @retval       1           error
2312
2202
*/
2313
2203
 
2314
 
static int fill_schema_table_names(THD *thd, Table *table,
 
2204
static int fill_schema_table_names(Session *session, Table *table,
2315
2205
                                   LEX_STRING *db_name, LEX_STRING *table_name,
2316
2206
                                   bool with_i_schema)
2317
2207
{
2322
2212
  }
2323
2213
  else
2324
2214
  {
2325
 
    enum legacy_db_type not_used;
2326
2215
    char path[FN_REFLEN];
2327
 
    (void) build_table_filename(path, sizeof(path), db_name->str, 
2328
 
                                table_name->str, reg_ext, 0);
2329
 
    if (mysql_frm_type(thd, path, &not_used)) 
2330
 
    {
 
2216
    (void) build_table_filename(path, sizeof(path), db_name->str,
 
2217
                                table_name->str, false);
 
2218
 
2331
2219
      table->field[3]->store(STRING_WITH_LEN("BASE Table"),
2332
2220
                             system_charset_info);
2333
 
    }
2334
 
    else
2335
 
    {
2336
 
      table->field[3]->store(STRING_WITH_LEN("ERROR"),
2337
 
                             system_charset_info);
2338
 
    }
2339
2221
 
2340
 
    if (thd->is_error() && thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
 
2222
    if (session->is_error() && session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2341
2223
    {
2342
 
      thd->clear_error();
 
2224
      session->clear_error();
2343
2225
      return 0;
2344
2226
    }
2345
2227
  }
2346
 
  if (schema_table_store_record(thd, table))
 
2228
  if (schema_table_store_record(session, table))
2347
2229
    return 1;
2348
2230
  return 0;
2349
2231
}
2366
2248
*/
2367
2249
 
2368
2250
static uint32_t get_table_open_method(TableList *tables,
2369
 
                                  ST_SCHEMA_TABLE *schema_table,
2370
 
                                  enum enum_schema_tables schema_table_idx __attribute__((unused)))
 
2251
                                      InfoSchemaTable *schema_table,
 
2252
                                      enum enum_schema_tables)
2371
2253
{
2372
2254
  /*
2373
2255
    determine which method will be used for table opening
2378
2260
    int table_open_method= 0, field_indx= 0;
2379
2261
    for (ptr=tables->table->field; (field= *ptr) ; ptr++)
2380
2262
    {
2381
 
      if (bitmap_is_set(tables->table->read_set, field->field_index))
 
2263
      if (field->isReadSet())
2382
2264
        table_open_method|= schema_table->fields_info[field_indx].open_method;
2383
2265
      field_indx++;
2384
2266
    }
2385
2267
    return table_open_method;
2386
2268
  }
2387
2269
  /* I_S tables which use get_all_tables but can not be optimized */
2388
 
  return (uint) OPEN_FULL_TABLE;
 
2270
  return (uint32_t) OPEN_FULL_TABLE;
2389
2271
}
2390
2272
 
2391
2273
 
2392
2274
/**
2393
2275
  @brief          Fill I_S table with data from FRM file only
2394
2276
 
2395
 
  @param[in]      thd                      thread handler
 
2277
  @param[in]      session                      thread handler
2396
2278
  @param[in]      table                    Table struct for I_S table
2397
2279
  @param[in]      schema_table             I_S table struct
2398
2280
  @param[in]      db_name                  database name
2406
2288
                              open_tables function for this table
2407
2289
*/
2408
2290
 
2409
 
static int fill_schema_table_from_frm(THD *thd,TableList *tables,
2410
 
                                      ST_SCHEMA_TABLE *schema_table,
 
2291
static int fill_schema_table_from_frm(Session *session,TableList *tables,
 
2292
                                      InfoSchemaTable *schema_table,
2411
2293
                                      LEX_STRING *db_name,
2412
2294
                                      LEX_STRING *table_name,
2413
 
                                      enum enum_schema_tables schema_table_idx __attribute__((unused)))
 
2295
                                      enum enum_schema_tables)
2414
2296
{
2415
2297
  Table *table= tables->table;
2416
 
  TABLE_SHARE *share;
 
2298
  TableShare *share;
2417
2299
  Table tbl;
2418
2300
  TableList table_list;
2419
2301
  uint32_t res= 0;
2421
2303
  char key[MAX_DBKEY_LENGTH];
2422
2304
  uint32_t key_length;
2423
2305
 
2424
 
  memset(&table_list, 0, sizeof(TableList));
2425
2306
  memset(&tbl, 0, sizeof(Table));
2426
2307
 
2427
2308
  table_list.table_name= table_name->str;
2428
2309
  table_list.db= db_name->str;
2429
2310
 
2430
 
  key_length= create_table_def_key(thd, key, &table_list, 0);
2431
 
  pthread_mutex_lock(&LOCK_open);
2432
 
  share= get_table_share(thd, &table_list, key,
 
2311
  key_length= create_table_def_key(key, &table_list);
 
2312
  pthread_mutex_lock(&LOCK_open); /* Locking to get table share when filling schema table from FRM */
 
2313
  share= get_table_share(session, &table_list, key,
2433
2314
                         key_length, 0, &error);
2434
2315
  if (!share)
2435
2316
  {
2440
2321
  {
2441
2322
    tbl.s= share;
2442
2323
    table_list.table= &tbl;
2443
 
    res= schema_table->process_table(thd, &table_list, table,
 
2324
    res= schema_table->process_table(session, &table_list, table,
2444
2325
                                     res, db_name, table_name);
2445
2326
  }
2446
2327
 
2447
 
  release_table_share(share, RELEASE_NORMAL);
 
2328
  release_table_share(share);
2448
2329
 
2449
2330
err:
2450
2331
  pthread_mutex_unlock(&LOCK_open);
2451
 
  thd->clear_error();
 
2332
  session->clear_error();
2452
2333
  return res;
2453
2334
}
2454
2335
 
2464
2345
                  from frm files and storage engine are filled by the function
2465
2346
                  get_all_tables().
2466
2347
 
2467
 
  @param[in]      thd                      thread handler
 
2348
  @param[in]      session                      thread handler
2468
2349
  @param[in]      tables                   I_S table
2469
2350
  @param[in]      cond                     'WHERE' condition
2470
2351
 
2473
2354
    @retval       1                        error
2474
2355
*/
2475
2356
 
2476
 
int get_all_tables(THD *thd, TableList *tables, COND *cond)
 
2357
int get_all_tables(Session *session, TableList *tables, COND *cond)
2477
2358
{
2478
 
  LEX *lex= thd->lex;
 
2359
  LEX *lex= session->lex;
2479
2360
  Table *table= tables->table;
2480
 
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
 
2361
  Select_Lex *old_all_select_lex= lex->all_selects_list;
2481
2362
  enum_sql_command save_sql_command= lex->sql_command;
2482
 
  SELECT_LEX *lsel= tables->schema_select_lex;
2483
 
  ST_SCHEMA_TABLE *schema_table= tables->schema_table;
2484
 
  SELECT_LEX sel;
 
2363
  Select_Lex *lsel= tables->schema_select_lex;
 
2364
  InfoSchemaTable *schema_table= tables->schema_table;
 
2365
  Select_Lex sel;
2485
2366
  LOOKUP_FIELD_VALUES lookup_field_vals;
2486
2367
  LEX_STRING *db_name, *table_name;
2487
2368
  bool with_i_schema;
2489
2370
  List<LEX_STRING> db_names;
2490
2371
  List_iterator_fast<LEX_STRING> it(db_names);
2491
2372
  COND *partial_cond= 0;
2492
 
  uint32_t derived_tables= lex->derived_tables; 
 
2373
  uint32_t derived_tables= lex->derived_tables;
2493
2374
  int error= 1;
2494
2375
  Open_tables_state open_tables_state_backup;
2495
2376
  Query_tables_list query_tables_list_backup;
2496
2377
  uint32_t table_open_method;
2497
 
  bool old_value= thd->no_warnings_for_error;
2498
 
 
2499
 
  lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
 
2378
  bool old_value= session->no_warnings_for_error;
2500
2379
 
2501
2380
  /*
2502
2381
    We should not introduce deadlocks even if we already have some
2503
2382
    tables open and locked, since we won't lock tables which we will
2504
2383
    open and will ignore possible name-locks for these tables.
2505
2384
  */
2506
 
  thd->reset_n_backup_open_tables_state(&open_tables_state_backup);
 
2385
  session->reset_n_backup_open_tables_state(&open_tables_state_backup);
2507
2386
 
2508
2387
  schema_table_idx= get_schema_table_idx(schema_table);
2509
2388
  tables->table_open_method= table_open_method=
2510
2389
    get_table_open_method(tables, schema_table, schema_table_idx);
2511
 
  /* 
 
2390
  /*
2512
2391
    this branch processes SHOW FIELDS, SHOW INDEXES commands.
2513
2392
    see sql_parse.cc, prepare_schema_table() function where
2514
2393
    this values are initialized
2515
2394
  */
2516
2395
  if (lsel && lsel->table_list.first)
2517
2396
  {
2518
 
    error= fill_schema_show_cols_or_idxs(thd, tables, schema_table,
 
2397
    error= fill_schema_show_cols_or_idxs(session, tables, schema_table,
2519
2398
                                         &open_tables_state_backup);
2520
2399
    goto err;
2521
2400
  }
2522
2401
 
2523
 
  if (get_lookup_field_values(thd, cond, tables, &lookup_field_vals))
 
2402
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
2524
2403
  {
2525
2404
    error= 0;
2526
2405
    goto err;
2528
2407
 
2529
2408
  if (!lookup_field_vals.wild_db_value && !lookup_field_vals.wild_table_value)
2530
2409
  {
2531
 
    /* 
 
2410
    /*
2532
2411
      if lookup value is empty string then
2533
2412
      it's impossible table name or db name
2534
2413
    */
2544
2423
      !lookup_field_vals.wild_db_value)
2545
2424
    tables->has_db_lookup_value= true;
2546
2425
  if (lookup_field_vals.table_value.length &&
2547
 
      !lookup_field_vals.wild_table_value) 
 
2426
      !lookup_field_vals.wild_table_value)
2548
2427
    tables->has_table_lookup_value= true;
2549
2428
 
2550
2429
  if (tables->has_db_lookup_value && tables->has_table_lookup_value)
2559
2438
    goto err;
2560
2439
  }
2561
2440
 
2562
 
  if (make_db_list(thd, &db_names, &lookup_field_vals, &with_i_schema))
 
2441
  if (make_db_list(session, &db_names, &lookup_field_vals, &with_i_schema))
2563
2442
    goto err;
2564
2443
  it.rewind(); /* To get access to new elements in basis list */
2565
2444
  while ((db_name= it++))
2566
2445
  {
2567
2446
    {
2568
 
      thd->no_warnings_for_error= 1;
 
2447
      session->no_warnings_for_error= 1;
2569
2448
      List<LEX_STRING> table_names;
2570
 
      int res= make_table_name_list(thd, &table_names, lex,
 
2449
      int res= make_table_name_list(session, &table_names, lex,
2571
2450
                                    &lookup_field_vals,
2572
2451
                                    with_i_schema, db_name);
2573
2452
      if (res == 2)   /* Not fatal error, continue */
2578
2457
      List_iterator_fast<LEX_STRING> it_files(table_names);
2579
2458
      while ((table_name= it_files++))
2580
2459
      {
2581
 
        restore_record(table, s->default_values);
 
2460
        table->restoreRecordAsDefault();
2582
2461
        table->field[schema_table->idx_field1]->
2583
2462
          store(db_name->str, db_name->length, system_charset_info);
2584
2463
        table->field[schema_table->idx_field2]->
2588
2467
        {
2589
2468
          /*
2590
2469
            If table is I_S.tables and open_table_method is 0 (eg SKIP_OPEN)
2591
 
            we can skip table opening and we don't have lookup value for 
 
2470
            we can skip table opening and we don't have lookup value for
2592
2471
            table name or lookup value is wild string(table name list is
2593
2472
            already created by make_table_name_list() function).
2594
2473
          */
2596
2475
              (!lookup_field_vals.table_value.length ||
2597
2476
               lookup_field_vals.wild_table_value))
2598
2477
          {
2599
 
            if (schema_table_store_record(thd, table))
 
2478
            if (schema_table_store_record(session, table))
2600
2479
              goto err;      /* Out of space in temporary table */
2601
2480
            continue;
2602
2481
          }
2604
2483
          /* SHOW Table NAMES command */
2605
2484
          if (schema_table_idx == SCH_TABLE_NAMES)
2606
2485
          {
2607
 
            if (fill_schema_table_names(thd, tables->table, db_name,
 
2486
            if (fill_schema_table_names(session, tables->table, db_name,
2608
2487
                                        table_name, with_i_schema))
2609
2488
              continue;
2610
2489
          }
2613
2492
            if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2614
2493
                !with_i_schema)
2615
2494
            {
2616
 
              if (!fill_schema_table_from_frm(thd, tables, schema_table, db_name,
 
2495
              if (!fill_schema_table_from_frm(session, tables, schema_table, db_name,
2617
2496
                                              table_name, schema_table_idx))
2618
2497
                continue;
2619
2498
            }
2620
2499
 
2621
 
            int res;
2622
2500
            LEX_STRING tmp_lex_string, orig_db_name;
2623
2501
            /*
2624
2502
              Set the parent lex of 'sel' because it is needed by
2625
2503
              sel.init_query() which is called inside make_table_list.
2626
2504
            */
2627
 
            thd->no_warnings_for_error= 1;
 
2505
            session->no_warnings_for_error= 1;
2628
2506
            sel.parent_lex= lex;
2629
2507
            /* db_name can be changed in make_table_list() func */
2630
 
            if (!thd->make_lex_string(&orig_db_name, db_name->str,
2631
 
                                      db_name->length, false))
 
2508
            if (!session->make_lex_string(&orig_db_name, db_name->str,
 
2509
                                          db_name->length, false))
2632
2510
              goto err;
2633
 
            if (make_table_list(thd, &sel, db_name, table_name))
 
2511
            if (make_table_list(session, &sel, db_name, table_name))
2634
2512
              goto err;
2635
2513
            TableList *show_table_list= (TableList*) sel.table_list.first;
2636
2514
            lex->all_selects_list= &sel;
2638
2516
            lex->sql_command= SQLCOM_SHOW_FIELDS;
2639
2517
            show_table_list->i_s_requested_object=
2640
2518
              schema_table->i_s_requested_object;
2641
 
            res= open_normal_and_derived_tables(thd, show_table_list,
 
2519
            res= open_normal_and_derived_tables(session, show_table_list,
2642
2520
                                                DRIZZLE_LOCK_IGNORE_FLUSH);
2643
2521
            lex->sql_command= save_sql_command;
2644
2522
            /*
2645
 
              XXX:  show_table_list has a flag i_is_requested,
2646
 
              and when it's set, open_normal_and_derived_tables()
2647
 
              can return an error without setting an error message
2648
 
              in THD, which is a hack. This is why we have to
2649
 
              check for res, then for thd->is_error() only then
2650
 
              for thd->main_da.sql_errno().
 
2523
XXX:  show_table_list has a flag i_is_requested,
 
2524
and when it's set, open_normal_and_derived_tables()
 
2525
can return an error without setting an error message
 
2526
in Session, which is a hack. This is why we have to
 
2527
check for res, then for session->is_error() only then
 
2528
for session->main_da.sql_errno().
2651
2529
            */
2652
 
            if (res && thd->is_error() &&
2653
 
                thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
 
2530
            if (res && session->is_error() &&
 
2531
                session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2654
2532
            {
2655
2533
              /*
2656
2534
                Hide error for not existing table.
2659
2537
                table does not exist.
2660
2538
              */
2661
2539
              res= 0;
2662
 
              thd->clear_error();
 
2540
              session->clear_error();
2663
2541
            }
2664
2542
            else
2665
2543
            {
2666
2544
              /*
2667
 
                We should use show_table_list->alias instead of 
 
2545
                We should use show_table_list->alias instead of
2668
2546
                show_table_list->table_name because table_name
2669
2547
                could be changed during opening of I_S tables. It's safe
2670
 
                to use alias because alias contains original table name 
 
2548
                to use alias because alias contains original table name
2671
2549
                in this case.
2672
2550
              */
2673
 
              thd->make_lex_string(&tmp_lex_string, show_table_list->alias,
2674
 
                                   strlen(show_table_list->alias), false);
2675
 
              res= schema_table->process_table(thd, show_table_list, table,
 
2551
              session->make_lex_string(&tmp_lex_string, show_table_list->alias,
 
2552
                                       strlen(show_table_list->alias), false);
 
2553
              res= schema_table->process_table(session, show_table_list, table,
2676
2554
                                               res, &orig_db_name,
2677
2555
                                               &tmp_lex_string);
2678
 
              close_tables_for_reopen(thd, &show_table_list);
 
2556
              session->close_tables_for_reopen(&show_table_list);
2679
2557
            }
2680
2558
            assert(!lex->query_tables_own_last);
2681
2559
            if (res)
2693
2571
 
2694
2572
  error= 0;
2695
2573
err:
2696
 
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2697
 
  lex->restore_backup_query_tables_list(&query_tables_list_backup);
 
2574
  session->restore_backup_open_tables_state(&open_tables_state_backup);
2698
2575
  lex->derived_tables= derived_tables;
2699
2576
  lex->all_selects_list= old_all_select_lex;
2700
2577
  lex->sql_command= save_sql_command;
2701
 
  thd->no_warnings_for_error= old_value;
 
2578
  session->no_warnings_for_error= old_value;
2702
2579
  return(error);
2703
2580
}
2704
2581
 
2705
2582
 
2706
 
bool store_schema_shemata(THD* thd, Table *table, LEX_STRING *db_name,
 
2583
bool store_schema_shemata(Session* session, Table *table, LEX_STRING *db_name,
2707
2584
                          const CHARSET_INFO * const cs)
2708
2585
{
2709
 
  restore_record(table, s->default_values);
 
2586
  table->restoreRecordAsDefault();
2710
2587
  table->field[1]->store(db_name->str, db_name->length, system_charset_info);
2711
2588
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
2712
2589
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2713
 
  return schema_table_store_record(thd, table);
 
2590
  return schema_table_store_record(session, table);
2714
2591
}
2715
2592
 
2716
2593
 
2717
 
int fill_schema_schemata(THD *thd, TableList *tables, COND *cond)
 
2594
int fill_schema_schemata(Session *session, TableList *tables, COND *cond)
2718
2595
{
2719
2596
  /*
2720
2597
    TODO: fill_schema_shemata() is called when new client is connected.
2725
2602
  List<LEX_STRING> db_names;
2726
2603
  LEX_STRING *db_name;
2727
2604
  bool with_i_schema;
2728
 
  HA_CREATE_INFO create;
2729
2605
  Table *table= tables->table;
2730
2606
 
2731
 
  if (get_lookup_field_values(thd, cond, tables, &lookup_field_vals))
 
2607
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
2732
2608
    return(0);
2733
 
  if (make_db_list(thd, &db_names, &lookup_field_vals,
 
2609
  if (make_db_list(session, &db_names, &lookup_field_vals,
2734
2610
                   &with_i_schema))
2735
2611
    return(1);
2736
2612
 
2746
2622
    if (!lookup_field_vals.db_value.str[0])
2747
2623
      return(0);
2748
2624
    path_len= build_table_filename(path, sizeof(path),
2749
 
                                   lookup_field_vals.db_value.str, "", "", 0);
 
2625
                                   lookup_field_vals.db_value.str, "", false);
2750
2626
    path[path_len-1]= 0;
2751
2627
    if (stat(path,&stat_info))
2752
2628
      return(0);
2757
2633
  {
2758
2634
    if (with_i_schema)       // information schema name is always first in list
2759
2635
    {
2760
 
      if (store_schema_shemata(thd, table, db_name,
 
2636
      if (store_schema_shemata(session, table, db_name,
2761
2637
                               system_charset_info))
2762
2638
        return(1);
2763
2639
      with_i_schema= 0;
2764
2640
      continue;
2765
2641
    }
2766
2642
    {
2767
 
      load_db_opt_by_name(thd, db_name->str, &create);
2768
 
      if (store_schema_shemata(thd, table, db_name,
 
2643
      HA_CREATE_INFO create;
 
2644
      load_db_opt_by_name(db_name->str, &create);
 
2645
 
 
2646
      if (store_schema_shemata(session, table, db_name,
2769
2647
                               create.default_table_charset))
2770
2648
        return(1);
2771
2649
    }
2774
2652
}
2775
2653
 
2776
2654
 
2777
 
static int get_schema_tables_record(THD *thd, TableList *tables,
 
2655
static int get_schema_tables_record(Session *session, TableList *tables,
2778
2656
                                    Table *table, bool res,
2779
2657
                                    LEX_STRING *db_name,
2780
2658
                                    LEX_STRING *table_name)
2783
2661
  DRIZZLE_TIME time;
2784
2662
  const CHARSET_INFO * const cs= system_charset_info;
2785
2663
 
2786
 
  restore_record(table, s->default_values);
 
2664
  table->restoreRecordAsDefault();
2787
2665
  table->field[1]->store(db_name->str, db_name->length, cs);
2788
2666
  table->field[2]->store(table_name->str, table_name->length, cs);
2789
2667
  if (res)
2791
2669
    /*
2792
2670
      there was errors during opening tables
2793
2671
    */
2794
 
    const char *error= thd->is_error() ? thd->main_da.message() : "";
 
2672
    const char *error= session->is_error() ? session->main_da.message() : "";
2795
2673
    if (tables->schema_table)
2796
2674
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2797
2675
    else
2798
2676
      table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
2799
2677
    table->field[20]->store(error, strlen(error), cs);
2800
 
    thd->clear_error();
 
2678
    session->clear_error();
2801
2679
  }
2802
2680
  else
2803
2681
  {
2804
2682
    char option_buff[400],*ptr;
2805
2683
    Table *show_table= tables->table;
2806
 
    TABLE_SHARE *share= show_table->s;
 
2684
    TableShare *share= show_table->s;
2807
2685
    handler *file= show_table->file;
2808
 
    handlerton *tmp_db_type= share->db_type();
 
2686
    StorageEngine *tmp_db_type= share->db_type();
2809
2687
    if (share->tmp_table == SYSTEM_TMP_TABLE)
2810
2688
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2811
2689
    else if (share->tmp_table)
2819
2697
        continue;
2820
2698
      table->field[i]->set_notnull();
2821
2699
    }
2822
 
    tmp_buff= (char *) ha_resolve_storage_engine_name(tmp_db_type);
2823
 
    table->field[4]->store(tmp_buff, strlen(tmp_buff), cs);
2824
 
    table->field[5]->store((int64_t) share->frm_version, true);
 
2700
    string engine_name= ha_resolve_storage_engine_name(tmp_db_type);
 
2701
    table->field[4]->store(engine_name.c_str(), engine_name.size(), cs);
 
2702
    table->field[5]->store((int64_t) 0, true);
2825
2703
 
2826
2704
    ptr=option_buff;
2827
2705
    if (share->min_rows)
2828
2706
    {
2829
 
      ptr=my_stpcpy(ptr," min_rows=");
2830
 
      ptr=int64_t10_to_str(share->min_rows,ptr,10);
 
2707
      ptr= strcpy(ptr," min_rows=")+10;
 
2708
      ptr= int64_t10_to_str(share->min_rows,ptr,10);
2831
2709
    }
2832
2710
    if (share->max_rows)
2833
2711
    {
2834
 
      ptr=my_stpcpy(ptr," max_rows=");
2835
 
      ptr=int64_t10_to_str(share->max_rows,ptr,10);
 
2712
      ptr= strcpy(ptr," max_rows=")+10;
 
2713
      ptr= int64_t10_to_str(share->max_rows,ptr,10);
2836
2714
    }
2837
2715
    if (share->avg_row_length)
2838
2716
    {
2839
 
      ptr=my_stpcpy(ptr," avg_row_length=");
2840
 
      ptr=int64_t10_to_str(share->avg_row_length,ptr,10);
 
2717
      ptr= strcpy(ptr," avg_row_length=")+16;
 
2718
      ptr= int64_t10_to_str(share->avg_row_length,ptr,10);
2841
2719
    }
2842
2720
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2843
 
      ptr=my_stpcpy(ptr," pack_keys=1");
 
2721
      ptr= strcpy(ptr," pack_keys=1")+12;
2844
2722
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2845
 
      ptr=my_stpcpy(ptr," pack_keys=0");
 
2723
      ptr= strcpy(ptr," pack_keys=0")+12;
2846
2724
    /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */
2847
2725
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2848
 
      ptr=my_stpcpy(ptr," checksum=1");
 
2726
      ptr= strcpy(ptr," checksum=1")+11;
2849
2727
    if (share->page_checksum != HA_CHOICE_UNDEF)
2850
 
      ptr= strxmov(ptr, " page_checksum=",
2851
 
                   ha_choice_values[(uint) share->page_checksum], NULL);
 
2728
      ptr+= sprintf(ptr, " page_checksum=%s",
 
2729
                    ha_choice_values[(uint32_t) share->page_checksum]);
2852
2730
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2853
 
      ptr=my_stpcpy(ptr," delay_key_write=1");
 
2731
      ptr= strcpy(ptr," delay_key_write=1")+18;
2854
2732
    if (share->row_type != ROW_TYPE_DEFAULT)
2855
 
      ptr=strxmov(ptr, " row_format=", 
2856
 
                  ha_row_type[(uint) share->row_type],
2857
 
                  NULL);
 
2733
      ptr+= sprintf(ptr, " row_format=%s", ha_row_type[(uint32_t)share->row_type]);
2858
2734
    if (share->block_size)
2859
2735
    {
2860
 
      ptr= my_stpcpy(ptr, " block_size=");
 
2736
      ptr= strcpy(ptr, " block_size=")+12;
2861
2737
      ptr= int64_t10_to_str(share->block_size, ptr, 10);
2862
2738
    }
2863
 
    
2864
 
    if (share->transactional != HA_CHOICE_UNDEF)
2865
 
    {
2866
 
      ptr= strxmov(ptr, " TRANSACTIONAL=",
2867
 
                   (share->transactional == HA_CHOICE_YES ? "1" : "0"),
2868
 
                   NULL);
2869
 
    }
2870
 
    if (share->transactional != HA_CHOICE_UNDEF)
2871
 
      ptr= strxmov(ptr, " transactional=",
2872
 
                   ha_choice_values[(uint) share->transactional], NULL);
 
2739
 
2873
2740
    table->field[19]->store(option_buff+1,
2874
 
                            (ptr == option_buff ? 0 : 
2875
 
                             (uint) (ptr-option_buff)-1), cs);
 
2741
                            (ptr == option_buff ? 0 :
 
2742
                             (uint32_t) (ptr-option_buff)-1), cs);
2876
2743
 
2877
2744
    tmp_buff= (share->table_charset ?
2878
2745
               share->table_charset->name : "default");
2936
2803
      }
2937
2804
      if (file->stats.create_time)
2938
2805
      {
2939
 
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2940
 
                                                  (my_time_t) file->stats.create_time);
 
2806
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2807
                                                  (time_t) file->stats.create_time);
2941
2808
        table->field[14]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2942
2809
        table->field[14]->set_notnull();
2943
2810
      }
2944
2811
      if (file->stats.update_time)
2945
2812
      {
2946
 
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2947
 
                                                  (my_time_t) file->stats.update_time);
 
2813
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2814
                                                  (time_t) file->stats.update_time);
2948
2815
        table->field[15]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2949
2816
        table->field[15]->set_notnull();
2950
2817
      }
2951
2818
      if (file->stats.check_time)
2952
2819
      {
2953
 
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2954
 
                                                  (my_time_t) file->stats.check_time);
 
2820
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2821
                                                  (time_t) file->stats.check_time);
2955
2822
        table->field[16]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2956
2823
        table->field[16]->set_notnull();
2957
2824
      }
2962
2829
      }
2963
2830
    }
2964
2831
  }
2965
 
  return(schema_table_store_record(thd, table));
 
2832
  return(schema_table_store_record(session, table));
2966
2833
}
2967
2834
 
2968
2835
 
2974
2841
  @param[in]      cs                I_S table charset
2975
2842
  @param[in]      offset            offset from beginning of table
2976
2843
                                    to DATE_TYPE column in I_S table
2977
 
                                    
 
2844
 
2978
2845
  @return         void
2979
2846
*/
2980
2847
 
3003
2870
    uint32_t octet_max_length= field->max_display_length();
3004
2871
    if (is_blob && octet_max_length != (uint32_t) 4294967295U)
3005
2872
      octet_max_length /= field->charset()->mbmaxlen;
3006
 
    int64_t char_max_len= is_blob ? 
 
2873
    int64_t char_max_len= is_blob ?
3007
2874
      (int64_t) octet_max_length / field->charset()->mbminlen :
3008
2875
      (int64_t) octet_max_length / field->charset()->mbmaxlen;
3009
2876
    /* CHARACTER_MAXIMUM_LENGTH column*/
3024
2891
  case DRIZZLE_TYPE_NEWDECIMAL:
3025
2892
    field_length= ((Field_new_decimal*) field)->precision;
3026
2893
    break;
3027
 
  case DRIZZLE_TYPE_TINY:
3028
2894
  case DRIZZLE_TYPE_LONG:
3029
2895
  case DRIZZLE_TYPE_LONGLONG:
3030
2896
    field_length= field->max_display_length() - 1;
3065
2931
}
3066
2932
 
3067
2933
 
3068
 
static int get_schema_column_record(THD *thd, TableList *tables,
 
2934
static int get_schema_column_record(Session *session, TableList *tables,
3069
2935
                                    Table *table, bool res,
3070
2936
                                    LEX_STRING *db_name,
3071
2937
                                    LEX_STRING *table_name)
3072
2938
{
3073
 
  LEX *lex= thd->lex;
 
2939
  LEX *lex= session->lex;
3074
2940
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
3075
2941
  const CHARSET_INFO * const cs= system_charset_info;
3076
2942
  Table *show_table;
3077
 
  TABLE_SHARE *show_table_share;
 
2943
  TableShare *show_table_share;
3078
2944
  Field **ptr, *field, *timestamp_field;
3079
2945
  int count;
3080
2946
 
3085
2951
      /*
3086
2952
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
3087
2953
        rather than in SHOW COLUMNS
3088
 
      */ 
3089
 
      if (thd->is_error())
3090
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3091
 
                     thd->main_da.sql_errno(), thd->main_da.message());
3092
 
      thd->clear_error();
 
2954
      */
 
2955
      if (session->is_error())
 
2956
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2957
                     session->main_da.sql_errno(), session->main_da.message());
 
2958
      session->clear_error();
3093
2959
      res= 0;
3094
2960
    }
3095
2961
    return(res);
3118
2984
      /* to satisfy 'field->val_str' ASSERTs */
3119
2985
      unsigned char *bitmaps;
3120
2986
      uint32_t bitmap_size= show_table_share->column_bitmap_size;
3121
 
      if (!(bitmaps= (unsigned char*) alloc_root(thd->mem_root, bitmap_size)))
 
2987
      if (!(bitmaps= (unsigned char*) alloc_root(session->mem_root, bitmap_size)))
3122
2988
        return(0);
3123
2989
      bitmap_init(&show_table->def_read_set,
3124
 
                  (my_bitmap_map*) bitmaps, show_table_share->fields, false);
 
2990
                  (my_bitmap_map*) bitmaps, show_table_share->fields);
3125
2991
      bitmap_set_all(&show_table->def_read_set);
3126
2992
      show_table->read_set= &show_table->def_read_set;
3127
2993
    }
3128
 
    bitmap_set_all(show_table->read_set);
 
2994
    show_table->setReadSet();
3129
2995
  }
3130
2996
 
3131
2997
  for (; (field= *ptr) ; ptr++)
3137
3003
 
3138
3004
    /* to satisfy 'field->val_str' ASSERTs */
3139
3005
    field->table= show_table;
3140
 
    show_table->in_use= thd;
 
3006
    show_table->in_use= session;
3141
3007
 
3142
3008
    if (wild && wild[0] &&
3143
3009
        wild_case_compare(system_charset_info, field->field_name,wild))
3145
3011
 
3146
3012
    count++;
3147
3013
    /* Get default row, with all NULL fields set to NULL */
3148
 
    restore_record(table, s->default_values);
 
3014
    table->restoreRecordAsDefault();
3149
3015
 
3150
3016
    table->field[1]->store(db_name->str, db_name->length, cs);
3151
3017
    table->field[2]->store(table_name->str, table_name->length, cs);
3153
3019
                           cs);
3154
3020
    table->field[4]->store((int64_t) count, true);
3155
3021
 
3156
 
    if (get_field_default_value(thd, timestamp_field, field, &type, 0))
 
3022
    if (get_field_default_value(timestamp_field, field, &type, 0))
3157
3023
    {
3158
3024
      table->field[5]->store(type.ptr(), type.length(), cs);
3159
3025
      table->field[5]->set_notnull();
3176
3042
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
3177
3043
      table->field[16]->store(STRING_WITH_LEN("on update CURRENT_TIMESTAMP"),
3178
3044
                              cs);
3179
 
 
3180
3045
    table->field[18]->store(field->comment.str, field->comment.length, cs);
3181
3046
    {
3182
3047
      enum column_format_type column_format= (enum column_format_type)
3190
3055
      table->field[20]->store((const char*) pos,
3191
3056
                              strlen((const char*) pos), cs);
3192
3057
    }
3193
 
    if (schema_table_store_record(thd, table))
 
3058
    if (schema_table_store_record(session, table))
3194
3059
      return(1);
3195
3060
  }
3196
3061
  return(0);
3198
3063
 
3199
3064
 
3200
3065
 
3201
 
int fill_schema_charsets(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3066
int fill_schema_charsets(Session *session, TableList *tables, COND *)
3202
3067
{
3203
3068
  CHARSET_INFO **cs;
3204
 
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NULL;
 
3069
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
3205
3070
  Table *table= tables->table;
3206
3071
  const CHARSET_INFO * const scs= system_charset_info;
3207
3072
 
3208
3073
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++)
3209
3074
  {
3210
3075
    const CHARSET_INFO * const tmp_cs= cs[0];
3211
 
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) && 
 
3076
    if (tmp_cs && (tmp_cs->state & MY_CS_PRIMARY) &&
3212
3077
        (tmp_cs->state & MY_CS_AVAILABLE) &&
3213
3078
        !(tmp_cs->state & MY_CS_HIDDEN) &&
3214
3079
        !(wild && wild[0] &&
3215
3080
          wild_case_compare(scs, tmp_cs->csname,wild)))
3216
3081
    {
3217
3082
      const char *comment;
3218
 
      restore_record(table, s->default_values);
 
3083
      table->restoreRecordAsDefault();
3219
3084
      table->field[0]->store(tmp_cs->csname, strlen(tmp_cs->csname), scs);
3220
3085
      table->field[1]->store(tmp_cs->name, strlen(tmp_cs->name), scs);
3221
3086
      comment= tmp_cs->comment ? tmp_cs->comment : "";
3222
3087
      table->field[2]->store(comment, strlen(comment), scs);
3223
3088
      table->field[3]->store((int64_t) tmp_cs->mbmaxlen, true);
3224
 
      if (schema_table_store_record(thd, table))
 
3089
      if (schema_table_store_record(session, table))
3225
3090
        return 1;
3226
3091
    }
3227
3092
  }
3229
3094
}
3230
3095
 
3231
3096
 
3232
 
int fill_schema_collation(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3097
int fill_schema_collation(Session *session, TableList *tables, COND *)
3233
3098
{
3234
3099
  CHARSET_INFO **cs;
3235
 
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NULL;
 
3100
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
3236
3101
  Table *table= tables->table;
3237
3102
  const CHARSET_INFO * const scs= system_charset_info;
3238
3103
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3246
3111
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3247
3112
    {
3248
3113
      const CHARSET_INFO *tmp_cl= cl[0];
3249
 
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
 
3114
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) ||
3250
3115
          !my_charset_same(tmp_cs, tmp_cl))
3251
3116
        continue;
3252
3117
      if (!(wild && wild[0] &&
3253
3118
          wild_case_compare(scs, tmp_cl->name,wild)))
3254
3119
      {
3255
3120
        const char *tmp_buff;
3256
 
        restore_record(table, s->default_values);
 
3121
        table->restoreRecordAsDefault();
3257
3122
        table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
3258
3123
        table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3259
3124
        table->field[2]->store((int64_t) tmp_cl->number, true);
3262
3127
        tmp_buff= (tmp_cl->state & MY_CS_COMPILED)? "Yes" : "";
3263
3128
        table->field[4]->store(tmp_buff, strlen(tmp_buff), scs);
3264
3129
        table->field[5]->store((int64_t) tmp_cl->strxfrm_multiply, true);
3265
 
        if (schema_table_store_record(thd, table))
 
3130
        if (schema_table_store_record(session, table))
3266
3131
          return 1;
3267
3132
      }
3268
3133
    }
3271
3136
}
3272
3137
 
3273
3138
 
3274
 
int fill_schema_coll_charset_app(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3139
int fill_schema_coll_charset_app(Session *session, TableList *tables, COND *)
3275
3140
{
3276
3141
  CHARSET_INFO **cs;
3277
3142
  Table *table= tables->table;
3280
3145
  {
3281
3146
    CHARSET_INFO **cl;
3282
3147
    const CHARSET_INFO *tmp_cs= cs[0];
3283
 
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) || 
 
3148
    if (!tmp_cs || !(tmp_cs->state & MY_CS_AVAILABLE) ||
3284
3149
        !(tmp_cs->state & MY_CS_PRIMARY))
3285
3150
      continue;
3286
3151
    for (cl= all_charsets; cl < all_charsets+255 ;cl ++)
3287
3152
    {
3288
3153
      const CHARSET_INFO *tmp_cl= cl[0];
3289
 
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) || 
 
3154
      if (!tmp_cl || !(tmp_cl->state & MY_CS_AVAILABLE) ||
3290
3155
          !my_charset_same(tmp_cs,tmp_cl))
3291
3156
        continue;
3292
 
      restore_record(table, s->default_values);
 
3157
      table->restoreRecordAsDefault();
3293
3158
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
3294
3159
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3295
 
      if (schema_table_store_record(thd, table))
 
3160
      if (schema_table_store_record(session, table))
3296
3161
        return 1;
3297
3162
    }
3298
3163
  }
3300
3165
}
3301
3166
 
3302
3167
 
3303
 
static int get_schema_stat_record(THD *thd, TableList *tables,
 
3168
static int get_schema_stat_record(Session *session, TableList *tables,
3304
3169
                                  Table *table, bool res,
3305
3170
                                  LEX_STRING *db_name,
3306
3171
                                  LEX_STRING *table_name)
3308
3173
  const CHARSET_INFO * const cs= system_charset_info;
3309
3174
  if (res)
3310
3175
  {
3311
 
    if (thd->lex->sql_command != SQLCOM_SHOW_KEYS)
 
3176
    if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
3312
3177
    {
3313
3178
      /*
3314
3179
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
3315
3180
        rather than in SHOW KEYS
3316
3181
      */
3317
 
      if (thd->is_error())
3318
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3319
 
                     thd->main_da.sql_errno(), thd->main_da.message());
3320
 
      thd->clear_error();
 
3182
      if (session->is_error())
 
3183
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3184
                     session->main_da.sql_errno(), session->main_da.message());
 
3185
      session->clear_error();
3321
3186
      res= 0;
3322
3187
    }
3323
3188
    return(res);
3336
3201
      const char *str;
3337
3202
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
3338
3203
      {
3339
 
        restore_record(table, s->default_values);
 
3204
        table->restoreRecordAsDefault();
3340
3205
        table->field[1]->store(db_name->str, db_name->length, cs);
3341
3206
        table->field[2]->store(table_name->str, table_name->length, cs);
3342
3207
        table->field[3]->store((int64_t) ((key_info->flags &
3378
3243
        uint32_t flags= key_part->field ? key_part->field->flags : 0;
3379
3244
        const char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
3380
3245
        table->field[12]->store(pos, strlen(pos), cs);
3381
 
        if (!show_table->s->keys_in_use.is_set(i))
 
3246
        if (!show_table->s->keys_in_use.test(i))
3382
3247
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
3383
3248
        else
3384
3249
          table->field[14]->store("", 0, cs);
3385
3250
        table->field[14]->set_notnull();
3386
 
        assert(test(key_info->flags & HA_USES_COMMENT) == 
 
3251
        assert(test(key_info->flags & HA_USES_COMMENT) ==
3387
3252
                   (key_info->comment.length > 0));
3388
3253
        if (key_info->flags & HA_USES_COMMENT)
3389
 
          table->field[15]->store(key_info->comment.str, 
 
3254
          table->field[15]->store(key_info->comment.str,
3390
3255
                                  key_info->comment.length, cs);
3391
 
        if (schema_table_store_record(thd, table))
 
3256
        if (schema_table_store_record(session, table))
3392
3257
          return(1);
3393
3258
      }
3394
3259
    }
3397
3262
}
3398
3263
 
3399
3264
 
3400
 
bool store_constraints(THD *thd, Table *table, LEX_STRING *db_name,
 
3265
bool store_constraints(Session *session, Table *table, LEX_STRING *db_name,
3401
3266
                       LEX_STRING *table_name, const char *key_name,
3402
3267
                       uint32_t key_len, const char *con_type, uint32_t con_len)
3403
3268
{
3404
3269
  const CHARSET_INFO * const cs= system_charset_info;
3405
 
  restore_record(table, s->default_values);
 
3270
  table->restoreRecordAsDefault();
3406
3271
  table->field[1]->store(db_name->str, db_name->length, cs);
3407
3272
  table->field[2]->store(key_name, key_len, cs);
3408
3273
  table->field[3]->store(db_name->str, db_name->length, cs);
3409
3274
  table->field[4]->store(table_name->str, table_name->length, cs);
3410
3275
  table->field[5]->store(con_type, con_len, cs);
3411
 
  return schema_table_store_record(thd, table);
 
3276
  return schema_table_store_record(session, table);
3412
3277
}
3413
3278
 
3414
3279
 
3415
 
static int get_schema_constraints_record(THD *thd, TableList *tables,
 
3280
static int get_schema_constraints_record(Session *session, TableList *tables,
3416
3281
                                         Table *table, bool res,
3417
3282
                                         LEX_STRING *db_name,
3418
3283
                                         LEX_STRING *table_name)
3419
3284
{
3420
3285
  if (res)
3421
3286
  {
3422
 
    if (thd->is_error())
3423
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3424
 
                   thd->main_da.sql_errno(), thd->main_da.message());
3425
 
    thd->clear_error();
 
3287
    if (session->is_error())
 
3288
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3289
                   session->main_da.sql_errno(), session->main_da.message());
 
3290
    session->clear_error();
3426
3291
    return(0);
3427
3292
  }
3428
3293
  else
3431
3296
    Table *show_table= tables->table;
3432
3297
    KEY *key_info=show_table->key_info;
3433
3298
    uint32_t primary_key= show_table->s->primary_key;
3434
 
    show_table->file->info(HA_STATUS_VARIABLE | 
 
3299
    show_table->file->info(HA_STATUS_VARIABLE |
3435
3300
                           HA_STATUS_NO_LOCK |
3436
3301
                           HA_STATUS_TIME);
3437
3302
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
3439
3304
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3440
3305
        continue;
3441
3306
 
3442
 
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
 
3307
      if (i == primary_key && is_primary_key(key_info))
3443
3308
      {
3444
 
        if (store_constraints(thd, table, db_name, table_name, key_info->name,
 
3309
        if (store_constraints(session, table, db_name, table_name, key_info->name,
3445
3310
                              strlen(key_info->name),
3446
3311
                              STRING_WITH_LEN("PRIMARY KEY")))
3447
3312
          return(1);
3448
3313
      }
3449
3314
      else if (key_info->flags & HA_NOSAME)
3450
3315
      {
3451
 
        if (store_constraints(thd, table, db_name, table_name, key_info->name,
 
3316
        if (store_constraints(session, table, db_name, table_name, key_info->name,
3452
3317
                              strlen(key_info->name),
3453
3318
                              STRING_WITH_LEN("UNIQUE")))
3454
3319
          return(1);
3455
3320
      }
3456
3321
    }
3457
3322
 
3458
 
    show_table->file->get_foreign_key_list(thd, &f_key_list);
 
3323
    show_table->file->get_foreign_key_list(session, &f_key_list);
3459
3324
    FOREIGN_KEY_INFO *f_key_info;
3460
3325
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3461
3326
    while ((f_key_info=it++))
3462
3327
    {
3463
 
      if (store_constraints(thd, table, db_name, table_name, 
 
3328
      if (store_constraints(session, table, db_name, table_name,
3464
3329
                            f_key_info->forein_id->str,
3465
3330
                            strlen(f_key_info->forein_id->str),
3466
3331
                            "FOREIGN KEY", 11))
3486
3351
}
3487
3352
 
3488
3353
 
3489
 
static int get_schema_key_column_usage_record(THD *thd,
 
3354
static int get_schema_key_column_usage_record(Session *session,
3490
3355
                                              TableList *tables,
3491
3356
                                              Table *table, bool res,
3492
3357
                                              LEX_STRING *db_name,
3494
3359
{
3495
3360
  if (res)
3496
3361
  {
3497
 
    if (thd->is_error())
3498
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3499
 
                   thd->main_da.sql_errno(), thd->main_da.message());
3500
 
    thd->clear_error();
 
3362
    if (session->is_error())
 
3363
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3364
                   session->main_da.sql_errno(), session->main_da.message());
 
3365
    session->clear_error();
3501
3366
    return(0);
3502
3367
  }
3503
3368
  else
3506
3371
    Table *show_table= tables->table;
3507
3372
    KEY *key_info=show_table->key_info;
3508
3373
    uint32_t primary_key= show_table->s->primary_key;
3509
 
    show_table->file->info(HA_STATUS_VARIABLE | 
 
3374
    show_table->file->info(HA_STATUS_VARIABLE |
3510
3375
                           HA_STATUS_NO_LOCK |
3511
3376
                           HA_STATUS_TIME);
3512
3377
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
3520
3385
        if (key_part->field)
3521
3386
        {
3522
3387
          f_idx++;
3523
 
          restore_record(table, s->default_values);
 
3388
          table->restoreRecordAsDefault();
3524
3389
          store_key_column_usage(table, db_name, table_name,
3525
3390
                                 key_info->name,
3526
 
                                 strlen(key_info->name), 
3527
 
                                 key_part->field->field_name, 
 
3391
                                 strlen(key_info->name),
 
3392
                                 key_part->field->field_name,
3528
3393
                                 strlen(key_part->field->field_name),
3529
3394
                                 (int64_t) f_idx);
3530
 
          if (schema_table_store_record(thd, table))
 
3395
          if (schema_table_store_record(session, table))
3531
3396
            return(1);
3532
3397
        }
3533
3398
      }
3534
3399
    }
3535
3400
 
3536
 
    show_table->file->get_foreign_key_list(thd, &f_key_list);
 
3401
    show_table->file->get_foreign_key_list(session, &f_key_list);
3537
3402
    FOREIGN_KEY_INFO *f_key_info;
3538
3403
    List_iterator_fast<FOREIGN_KEY_INFO> fkey_it(f_key_list);
3539
3404
    while ((f_key_info= fkey_it++))
3547
3412
      {
3548
3413
        r_info= it1++;
3549
3414
        f_idx++;
3550
 
        restore_record(table, s->default_values);
 
3415
        table->restoreRecordAsDefault();
3551
3416
        store_key_column_usage(table, db_name, table_name,
3552
3417
                               f_key_info->forein_id->str,
3553
3418
                               f_key_info->forein_id->length,
3560
3425
                               system_charset_info);
3561
3426
        table->field[9]->set_notnull();
3562
3427
        table->field[10]->store(f_key_info->referenced_table->str,
3563
 
                                f_key_info->referenced_table->length, 
 
3428
                                f_key_info->referenced_table->length,
3564
3429
                                system_charset_info);
3565
3430
        table->field[10]->set_notnull();
3566
3431
        table->field[11]->store(r_info->str, r_info->length,
3567
3432
                                system_charset_info);
3568
3433
        table->field[11]->set_notnull();
3569
 
        if (schema_table_store_record(thd, table))
 
3434
        if (schema_table_store_record(session, table))
3570
3435
          return(1);
3571
3436
      }
3572
3437
    }
3575
3440
}
3576
3441
 
3577
3442
 
3578
 
int fill_open_tables(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3443
int fill_open_tables(Session *session, TableList *tables, COND *)
3579
3444
{
3580
 
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NULL;
 
3445
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
3581
3446
  Table *table= tables->table;
3582
3447
  const CHARSET_INFO * const cs= system_charset_info;
3583
3448
  OPEN_TableList *open_list;
3584
 
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
3585
 
            && thd->is_fatal_error)
 
3449
  if (!(open_list=list_open_tables(session->lex->select_lex.db, wild))
 
3450
            && session->is_fatal_error)
3586
3451
    return(1);
3587
3452
 
3588
3453
  for (; open_list ; open_list=open_list->next)
3589
3454
  {
3590
 
    restore_record(table, s->default_values);
 
3455
    table->restoreRecordAsDefault();
3591
3456
    table->field[0]->store(open_list->db, strlen(open_list->db), cs);
3592
3457
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
3593
3458
    table->field[2]->store((int64_t) open_list->in_use, true);
3594
3459
    table->field[3]->store((int64_t) open_list->locked, true);
3595
 
    if (schema_table_store_record(thd, table))
 
3460
    if (schema_table_store_record(session, table))
3596
3461
      return(1);
3597
3462
  }
3598
3463
  return(0);
3599
3464
}
3600
3465
 
3601
3466
 
3602
 
int fill_variables(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3467
int fill_variables(Session *session, TableList *tables, COND *)
3603
3468
{
3604
3469
  int res= 0;
3605
 
  LEX *lex= thd->lex;
 
3470
  LEX *lex= session->lex;
3606
3471
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
3607
3472
  enum enum_schema_tables schema_table_idx=
3608
3473
    get_schema_table_idx(tables->schema_table);
3614
3479
      schema_table_idx == SCH_GLOBAL_VARIABLES)
3615
3480
    option_type= OPT_GLOBAL;
3616
3481
 
3617
 
  rw_rdlock(&LOCK_system_variables_hash);
3618
 
  res= show_status_array(thd, wild, enumerate_sys_vars(thd, sorted_vars),
 
3482
  pthread_rwlock_rdlock(&LOCK_system_variables_hash);
 
3483
  res= show_status_array(session, wild, enumerate_sys_vars(session, sorted_vars),
3619
3484
                         option_type, NULL, "", tables->table, upper_case_names);
3620
 
  rw_unlock(&LOCK_system_variables_hash);
 
3485
  pthread_rwlock_unlock(&LOCK_system_variables_hash);
3621
3486
  return(res);
3622
3487
}
3623
3488
 
3624
3489
 
3625
 
int fill_status(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
 
3490
int fill_status(Session *session, TableList *tables, COND *)
3626
3491
{
3627
 
  LEX *lex= thd->lex;
 
3492
  LEX *lex= session->lex;
3628
3493
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
3629
3494
  int res= 0;
3630
3495
  STATUS_VAR *tmp1, tmp;
3639
3504
    if (option_type == OPT_GLOBAL)
3640
3505
      tmp1= &tmp;
3641
3506
    else
3642
 
      tmp1= thd->initial_status_var;
 
3507
      tmp1= session->initial_status_var;
3643
3508
  }
3644
3509
  else if (schema_table_idx == SCH_GLOBAL_STATUS)
3645
3510
  {
3647
3512
    tmp1= &tmp;
3648
3513
  }
3649
3514
  else
3650
 
  { 
 
3515
  {
3651
3516
    option_type= OPT_SESSION;
3652
 
    tmp1= &thd->status_var;
 
3517
    tmp1= &session->status_var;
3653
3518
  }
3654
3519
 
3655
3520
  pthread_mutex_lock(&LOCK_status);
3656
3521
  if (option_type == OPT_GLOBAL)
3657
3522
    calc_sum_of_all_status(&tmp);
3658
 
  res= show_status_array(thd, wild,
3659
 
                         (SHOW_VAR *)all_status_vars.buffer,
 
3523
  res= show_status_array(session, wild,
 
3524
                         (SHOW_VAR *) all_status_vars.front(),
3660
3525
                         option_type, tmp1, "", tables->table,
3661
3526
                         upper_case_names);
3662
3527
  pthread_mutex_unlock(&LOCK_status);
3669
3534
 
3670
3535
  SYNOPSIS
3671
3536
    get_referential_constraints_record()
3672
 
    thd                 thread handle
 
3537
    session                 thread handle
3673
3538
    tables              table list struct(processed table)
3674
3539
    table               I_S table
3675
3540
    res                 1 means the error during opening of the processed table
3683
3548
*/
3684
3549
 
3685
3550
static int
3686
 
get_referential_constraints_record(THD *thd, TableList *tables,
 
3551
get_referential_constraints_record(Session *session, TableList *tables,
3687
3552
                                   Table *table, bool res,
3688
3553
                                   LEX_STRING *db_name, LEX_STRING *table_name)
3689
3554
{
3691
3556
 
3692
3557
  if (res)
3693
3558
  {
3694
 
    if (thd->is_error())
3695
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3696
 
                   thd->main_da.sql_errno(), thd->main_da.message());
3697
 
    thd->clear_error();
 
3559
    if (session->is_error())
 
3560
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3561
                   session->main_da.sql_errno(), session->main_da.message());
 
3562
    session->clear_error();
3698
3563
    return(0);
3699
3564
  }
3700
3565
 
3701
3566
  {
3702
3567
    List<FOREIGN_KEY_INFO> f_key_list;
3703
3568
    Table *show_table= tables->table;
3704
 
    show_table->file->info(HA_STATUS_VARIABLE | 
 
3569
    show_table->file->info(HA_STATUS_VARIABLE |
3705
3570
                           HA_STATUS_NO_LOCK |
3706
3571
                           HA_STATUS_TIME);
3707
3572
 
3708
 
    show_table->file->get_foreign_key_list(thd, &f_key_list);
 
3573
    show_table->file->get_foreign_key_list(session, &f_key_list);
3709
3574
    FOREIGN_KEY_INFO *f_key_info;
3710
3575
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3711
3576
    while ((f_key_info= it++))
3712
3577
    {
3713
 
      restore_record(table, s->default_values);
 
3578
      table->restoreRecordAsDefault();
3714
3579
      table->field[1]->store(db_name->str, db_name->length, cs);
3715
3580
      table->field[9]->store(table_name->str, table_name->length, cs);
3716
3581
      table->field[2]->store(f_key_info->forein_id->str,
3717
3582
                             f_key_info->forein_id->length, cs);
3718
 
      table->field[4]->store(f_key_info->referenced_db->str, 
 
3583
      table->field[4]->store(f_key_info->referenced_db->str,
3719
3584
                             f_key_info->referenced_db->length, cs);
3720
 
      table->field[10]->store(f_key_info->referenced_table->str, 
 
3585
      table->field[10]->store(f_key_info->referenced_table->str,
3721
3586
                             f_key_info->referenced_table->length, cs);
3722
3587
      if (f_key_info->referenced_key_name)
3723
3588
      {
3724
 
        table->field[5]->store(f_key_info->referenced_key_name->str, 
 
3589
        table->field[5]->store(f_key_info->referenced_key_name->str,
3725
3590
                               f_key_info->referenced_key_name->length, cs);
3726
3591
        table->field[5]->set_notnull();
3727
3592
      }
3728
3593
      else
3729
3594
        table->field[5]->set_null();
3730
3595
      table->field[6]->store(STRING_WITH_LEN("NONE"), cs);
3731
 
      table->field[7]->store(f_key_info->update_method->str, 
 
3596
      table->field[7]->store(f_key_info->update_method->str,
3732
3597
                             f_key_info->update_method->length, cs);
3733
 
      table->field[8]->store(f_key_info->delete_method->str, 
 
3598
      table->field[8]->store(f_key_info->delete_method->str,
3734
3599
                             f_key_info->delete_method->length, cs);
3735
 
      if (schema_table_store_record(thd, table))
 
3600
      if (schema_table_store_record(session, table))
3736
3601
        return(1);
3737
3602
    }
3738
3603
  }
3740
3605
}
3741
3606
 
3742
3607
 
3743
 
struct schema_table_ref 
 
3608
class FindSchemaTableByName : public unary_function<InfoSchemaTable *, bool>
3744
3609
{
3745
3610
  const char *table_name;
3746
 
  ST_SCHEMA_TABLE *schema_table;
 
3611
public:
 
3612
  FindSchemaTableByName(const char *table_name_arg)
 
3613
    : table_name(table_name_arg) {}
 
3614
  result_type operator() (argument_type schema_table)
 
3615
  {
 
3616
    return !my_strcasecmp(system_charset_info,
 
3617
                          schema_table->table_name,
 
3618
                          table_name);
 
3619
  }
3747
3620
};
3748
3621
 
3749
3622
 
3751
3624
  Find schema_tables elment by name
3752
3625
 
3753
3626
  SYNOPSIS
3754
 
    find_schema_table_in_plugin()
3755
 
    thd                 thread handler
3756
 
    plugin              plugin
3757
 
    table_name          table name
3758
 
 
3759
 
  RETURN
3760
 
    0   table not found
3761
 
    1   found the schema table
3762
 
*/
3763
 
static bool find_schema_table_in_plugin(THD *thd __attribute__((unused)),
3764
 
                                           plugin_ref plugin,
3765
 
                                           void* p_table)
3766
 
{
3767
 
  schema_table_ref *p_schema_table= (schema_table_ref *)p_table;
3768
 
  const char* table_name= p_schema_table->table_name;
3769
 
  ST_SCHEMA_TABLE *schema_table= plugin_data(plugin, ST_SCHEMA_TABLE *);
3770
 
 
3771
 
  if (!my_strcasecmp(system_charset_info,
3772
 
                     schema_table->table_name,
3773
 
                     table_name)) {
3774
 
    p_schema_table->schema_table= schema_table;
3775
 
    return(1);
3776
 
  }
3777
 
 
3778
 
  return(0);
3779
 
}
3780
 
 
3781
 
 
3782
 
/*
3783
 
  Find schema_tables elment by name
3784
 
 
3785
 
  SYNOPSIS
3786
3627
    find_schema_table()
3787
 
    thd                 thread handler
3788
3628
    table_name          table name
3789
3629
 
3790
3630
  RETURN
3792
3632
    #   pointer to 'schema_tables' element
3793
3633
*/
3794
3634
 
3795
 
ST_SCHEMA_TABLE *find_schema_table(THD *thd, const char* table_name)
 
3635
InfoSchemaTable *find_schema_table(const char* table_name)
3796
3636
{
3797
 
  schema_table_ref schema_table_a;
3798
 
  ST_SCHEMA_TABLE *schema_table= schema_tables;
 
3637
  InfoSchemaTable *schema_table= schema_tables;
3799
3638
 
3800
3639
  for (; schema_table->table_name; schema_table++)
3801
3640
  {
3805
3644
      return(schema_table);
3806
3645
  }
3807
3646
 
3808
 
  schema_table_a.table_name= table_name;
3809
 
  if (plugin_foreach(thd, find_schema_table_in_plugin, 
3810
 
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &schema_table_a))
3811
 
    return(schema_table_a.schema_table);
3812
 
 
3813
 
  return(NULL);
 
3647
  vector<InfoSchemaTable *>::iterator iter= 
 
3648
    find_if(all_schema_tables.begin(), all_schema_tables.end(),
 
3649
            FindSchemaTableByName(table_name));
 
3650
  if (iter != all_schema_tables.end())
 
3651
    return *iter;
 
3652
  return NULL;
3814
3653
}
3815
3654
 
3816
3655
 
3817
 
ST_SCHEMA_TABLE *get_schema_table(enum enum_schema_tables schema_table_idx)
 
3656
InfoSchemaTable *get_schema_table(enum enum_schema_tables schema_table_idx)
3818
3657
{
3819
3658
  return &schema_tables[schema_table_idx];
3820
3659
}
3826
3665
  @note
3827
3666
 
3828
3667
  @param
3829
 
    thd                   thread handler
 
3668
    session                       thread handler
3830
3669
 
3831
3670
  @param table_list Used to pass I_S table information(fields info, tables
3832
3671
  parameters etc) and table name.
3835
3674
  @retval  NULL           Can't create table
3836
3675
*/
3837
3676
 
3838
 
Table *create_schema_table(THD *thd, TableList *table_list)
 
3677
Table *create_schema_table(Session *session, TableList *table_list)
3839
3678
{
3840
3679
  int field_count= 0;
3841
3680
  Item *item;
3842
3681
  Table *table;
3843
3682
  List<Item> field_list;
3844
 
  ST_SCHEMA_TABLE *schema_table= table_list->schema_table;
 
3683
  InfoSchemaTable *schema_table= table_list->schema_table;
3845
3684
  ST_FIELD_INFO *fields_info= schema_table->fields_info;
3846
3685
  const CHARSET_INFO * const cs= system_charset_info;
3847
3686
 
3848
3687
  for (; fields_info->field_name; fields_info++)
3849
3688
  {
3850
3689
    switch (fields_info->field_type) {
3851
 
    case DRIZZLE_TYPE_TINY:
3852
3690
    case DRIZZLE_TYPE_LONG:
3853
3691
    case DRIZZLE_TYPE_LONGLONG:
3854
3692
      if (!(item= new Item_return_int(fields_info->field_name,
3860
3698
      }
3861
3699
      item->unsigned_flag= (fields_info->field_flags & MY_I_S_UNSIGNED);
3862
3700
      break;
3863
 
    case DRIZZLE_TYPE_NEWDATE:
3864
 
    case DRIZZLE_TYPE_TIME:
 
3701
    case DRIZZLE_TYPE_DATE:
3865
3702
    case DRIZZLE_TYPE_TIMESTAMP:
3866
3703
    case DRIZZLE_TYPE_DATETIME:
3867
3704
      if (!(item=new Item_return_date_time(fields_info->field_name,
3871
3708
      }
3872
3709
      break;
3873
3710
    case DRIZZLE_TYPE_DOUBLE:
3874
 
      if ((item= new Item_float(fields_info->field_name, 0.0, NOT_FIXED_DEC, 
 
3711
      if ((item= new Item_float(fields_info->field_name, 0.0, NOT_FIXED_DEC,
3875
3712
                           fields_info->field_length)) == NULL)
3876
 
        return(NULL);
 
3713
        return NULL;
3877
3714
      break;
3878
3715
    case DRIZZLE_TYPE_NEWDECIMAL:
3879
3716
      if (!(item= new Item_decimal((int64_t) fields_info->value, false)))
3910
3747
    item->maybe_null= (fields_info->field_flags & MY_I_S_MAYBE_NULL);
3911
3748
    field_count++;
3912
3749
  }
3913
 
  TMP_TABLE_PARAM *tmp_table_param =
3914
 
    (TMP_TABLE_PARAM*) (thd->alloc(sizeof(TMP_TABLE_PARAM)));
 
3750
  Tmp_Table_Param *tmp_table_param =
 
3751
    (Tmp_Table_Param*) (session->alloc(sizeof(Tmp_Table_Param)));
3915
3752
  tmp_table_param->init();
3916
3753
  tmp_table_param->table_charset= cs;
3917
3754
  tmp_table_param->field_count= field_count;
3918
3755
  tmp_table_param->schema_table= 1;
3919
 
  SELECT_LEX *select_lex= thd->lex->current_select;
3920
 
  if (!(table= create_tmp_table(thd, tmp_table_param,
3921
 
                                field_list, (order_st*) 0, 0, 0, 
3922
 
                                (select_lex->options | thd->options |
 
3756
  Select_Lex *select_lex= session->lex->current_select;
 
3757
  if (!(table= create_tmp_table(session, tmp_table_param,
 
3758
                                field_list, (order_st*) 0, 0, 0,
 
3759
                                (select_lex->options | session->options |
3923
3760
                                 TMP_TABLE_ALL_COLUMNS),
3924
3761
                                HA_POS_ERROR, table_list->alias)))
3925
3762
    return(0);
3926
3763
  my_bitmap_map* bitmaps=
3927
 
    (my_bitmap_map*) thd->alloc(bitmap_buffer_size(field_count));
3928
 
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
3929
 
              false);
 
3764
    (my_bitmap_map*) session->alloc(bitmap_buffer_size(field_count));
 
3765
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count);
3930
3766
  table->read_set= &table->def_read_set;
3931
3767
  bitmap_clear_all(table->read_set);
3932
3768
  table_list->schema_table_param= tmp_table_param;
3941
3777
 
3942
3778
  SYNOPSIS
3943
3779
    make_old_format()
3944
 
    thd                 thread handler
 
3780
    session                     thread handler
3945
3781
    schema_table        pointer to 'schema_tables' element
3946
3782
 
3947
3783
  RETURN
3949
3785
   0    success
3950
3786
*/
3951
3787
 
3952
 
int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
 
3788
int make_old_format(Session *session, InfoSchemaTable *schema_table)
3953
3789
{
3954
3790
  ST_FIELD_INFO *field_info= schema_table->fields_info;
3955
 
  Name_resolution_context *context= &thd->lex->select_lex.context;
 
3791
  Name_resolution_context *context= &session->lex->select_lex.context;
3956
3792
  for (; field_info->field_name; field_info++)
3957
3793
  {
3958
3794
    if (field_info->old_name)
3964
3800
        field->set_name(field_info->old_name,
3965
3801
                        strlen(field_info->old_name),
3966
3802
                        system_charset_info);
3967
 
        if (add_item_to_list(thd, field))
 
3803
        if (session->add_item_to_list(field))
3968
3804
          return 1;
3969
3805
      }
3970
3806
    }
3973
3809
}
3974
3810
 
3975
3811
 
3976
 
int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
 
3812
int make_schemata_old_format(Session *session, InfoSchemaTable *schema_table)
3977
3813
{
3978
3814
  char tmp[128];
3979
 
  LEX *lex= thd->lex;
3980
 
  SELECT_LEX *sel= lex->current_select;
 
3815
  LEX *lex= session->lex;
 
3816
  Select_Lex *sel= lex->current_select;
3981
3817
  Name_resolution_context *context= &sel->context;
3982
3818
 
3983
3819
  if (!sel->item_list.elements)
3986
3822
    String buffer(tmp,sizeof(tmp), system_charset_info);
3987
3823
    Item_field *field= new Item_field(context,
3988
3824
                                      NULL, NULL, field_info->field_name);
3989
 
    if (!field || add_item_to_list(thd, field))
 
3825
    if (!field || session->add_item_to_list(field))
3990
3826
      return 1;
3991
3827
    buffer.length(0);
3992
3828
    buffer.append(field_info->old_name);
4002
3838
}
4003
3839
 
4004
3840
 
4005
 
int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
 
3841
int make_table_names_old_format(Session *session, InfoSchemaTable *schema_table)
4006
3842
{
4007
3843
  char tmp[128];
4008
 
  String buffer(tmp,sizeof(tmp), thd->charset());
4009
 
  LEX *lex= thd->lex;
 
3844
  String buffer(tmp,sizeof(tmp), session->charset());
 
3845
  LEX *lex= session->lex;
4010
3846
  Name_resolution_context *context= &lex->select_lex.context;
4011
3847
 
4012
3848
  ST_FIELD_INFO *field_info= &schema_table->fields_info[2];
4021
3857
  }
4022
3858
  Item_field *field= new Item_field(context,
4023
3859
                                    NULL, NULL, field_info->field_name);
4024
 
  if (add_item_to_list(thd, field))
 
3860
  if (session->add_item_to_list(field))
4025
3861
    return 1;
4026
3862
  field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4027
 
  if (thd->lex->verbose)
 
3863
  if (session->lex->verbose)
4028
3864
  {
4029
3865
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4030
3866
    field_info= &schema_table->fields_info[3];
4031
3867
    field= new Item_field(context, NULL, NULL, field_info->field_name);
4032
 
    if (add_item_to_list(thd, field))
 
3868
    if (session->add_item_to_list(field))
4033
3869
      return 1;
4034
3870
    field->set_name(field_info->old_name, strlen(field_info->old_name),
4035
3871
                    system_charset_info);
4038
3874
}
4039
3875
 
4040
3876
 
4041
 
int make_columns_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
 
3877
int make_columns_old_format(Session *session, InfoSchemaTable *schema_table)
4042
3878
{
4043
3879
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
4044
3880
  int *field_num= fields_arr;
4045
3881
  ST_FIELD_INFO *field_info;
4046
 
  Name_resolution_context *context= &thd->lex->select_lex.context;
 
3882
  Name_resolution_context *context= &session->lex->select_lex.context;
4047
3883
 
4048
3884
  for (; *field_num >= 0; field_num++)
4049
3885
  {
4050
3886
    field_info= &schema_table->fields_info[*field_num];
4051
 
    if (!thd->lex->verbose && (*field_num == 13 ||
 
3887
    if (!session->lex->verbose && (*field_num == 13 ||
4052
3888
                               *field_num == 17 ||
4053
3889
                               *field_num == 18))
4054
3890
      continue;
4059
3895
      field->set_name(field_info->old_name,
4060
3896
                      strlen(field_info->old_name),
4061
3897
                      system_charset_info);
4062
 
      if (add_item_to_list(thd, field))
 
3898
      if (session->add_item_to_list(field))
4063
3899
        return 1;
4064
3900
    }
4065
3901
  }
4067
3903
}
4068
3904
 
4069
3905
 
4070
 
int make_character_sets_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
 
3906
int make_character_sets_old_format(Session *session, InfoSchemaTable *schema_table)
4071
3907
{
4072
3908
  int fields_arr[]= {0, 2, 1, 3, -1};
4073
3909
  int *field_num= fields_arr;
4074
3910
  ST_FIELD_INFO *field_info;
4075
 
  Name_resolution_context *context= &thd->lex->select_lex.context;
 
3911
  Name_resolution_context *context= &session->lex->select_lex.context;
4076
3912
 
4077
3913
  for (; *field_num >= 0; field_num++)
4078
3914
  {
4084
3920
      field->set_name(field_info->old_name,
4085
3921
                      strlen(field_info->old_name),
4086
3922
                      system_charset_info);
4087
 
      if (add_item_to_list(thd, field))
 
3923
      if (session->add_item_to_list(field))
4088
3924
        return 1;
4089
3925
    }
4090
3926
  }
4097
3933
 
4098
3934
  SYNOPSIS
4099
3935
  mysql_schema_table()
4100
 
    thd                thread handler
 
3936
    session                thread handler
4101
3937
    lex                pointer to LEX
4102
3938
    table_list         pointer to table_list
4103
3939
 
4104
3940
  RETURN
4105
 
    0   success
4106
 
    1   error
 
3941
    true on error
4107
3942
*/
4108
3943
 
4109
 
int mysql_schema_table(THD *thd, LEX *lex, TableList *table_list)
 
3944
bool mysql_schema_table(Session *session, LEX *, TableList *table_list)
4110
3945
{
4111
3946
  Table *table;
4112
 
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4113
 
    return(1);
 
3947
  if (!(table= table_list->schema_table->create_table(session, table_list)))
 
3948
    return true;
4114
3949
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4115
3950
  /*
4116
3951
    This test is necessary to make
4126
3961
  table_list->table_name= table->s->table_name.str;
4127
3962
  table_list->table_name_length= table->s->table_name.length;
4128
3963
  table_list->table= table;
4129
 
  table->next= thd->derived_tables;
4130
 
  thd->derived_tables= table;
 
3964
  table->next= session->derived_tables;
 
3965
  session->derived_tables= table;
4131
3966
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4132
3967
 
4133
 
  if (table_list->schema_table_reformed) // show command
4134
 
  {
4135
 
    SELECT_LEX *sel= lex->current_select;
4136
 
    Item *item;
4137
 
    Field_translator *transl, *org_transl;
4138
 
 
4139
 
    if (table_list->field_translation)
4140
 
    {
4141
 
      Field_translator *end= table_list->field_translation_end;
4142
 
      for (transl= table_list->field_translation; transl < end; transl++)
4143
 
      {
4144
 
        if (!transl->item->fixed &&
4145
 
            transl->item->fix_fields(thd, &transl->item))
4146
 
          return(1);
4147
 
      }
4148
 
      return(0);
4149
 
    }
4150
 
    List_iterator_fast<Item> it(sel->item_list);
4151
 
    if (!(transl=
4152
 
          (Field_translator*)(thd->alloc(sel->item_list.elements *
4153
 
                                    sizeof(Field_translator)))))
4154
 
    {
4155
 
      return(1);
4156
 
    }
4157
 
    for (org_transl= transl; (item= it++); transl++)
4158
 
    {
4159
 
      transl->item= item;
4160
 
      transl->name= item->name;
4161
 
      if (!item->fixed && item->fix_fields(thd, &transl->item))
4162
 
      {
4163
 
        return(1);
4164
 
      }
4165
 
    }
4166
 
    table_list->field_translation= org_transl;
4167
 
    table_list->field_translation_end= transl;
4168
 
  }
4169
 
 
4170
 
  return(0);
 
3968
  return false;
4171
3969
}
4172
3970
 
4173
3971
 
4176
3974
 
4177
3975
  SYNOPSIS
4178
3976
    make_schema_select()
4179
 
    thd                  thread handler
4180
 
    sel                  pointer to SELECT_LEX
 
3977
    session                  thread handler
 
3978
    sel                  pointer to Select_Lex
4181
3979
    schema_table_idx     index of 'schema_tables' element
4182
3980
 
4183
3981
  RETURN
4184
 
    0   success
4185
 
    1   error
 
3982
    true on error
4186
3983
*/
4187
3984
 
4188
 
int make_schema_select(THD *thd, SELECT_LEX *sel,
4189
 
                       enum enum_schema_tables schema_table_idx)
 
3985
bool make_schema_select(Session *session, Select_Lex *sel,
 
3986
                        enum enum_schema_tables schema_table_idx)
4190
3987
{
4191
 
  ST_SCHEMA_TABLE *schema_table= get_schema_table(schema_table_idx);
 
3988
  InfoSchemaTable *schema_table= get_schema_table(schema_table_idx);
4192
3989
  LEX_STRING db, table;
4193
3990
  /*
4194
3991
     We have to make non const db_name & table_name
4195
3992
     because of lower_case_table_names
4196
3993
  */
4197
 
  thd->make_lex_string(&db, INFORMATION_SCHEMA_NAME.str,
4198
 
                       INFORMATION_SCHEMA_NAME.length, 0);
4199
 
  thd->make_lex_string(&table, schema_table->table_name,
 
3994
  session->make_lex_string(&db, INFORMATION_SCHEMA_NAME.c_str(),
 
3995
                       INFORMATION_SCHEMA_NAME.length(), 0);
 
3996
  session->make_lex_string(&table, schema_table->table_name,
4200
3997
                       strlen(schema_table->table_name), 0);
4201
 
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
4202
 
      !sel->add_table_to_list(thd, new Table_ident(thd, db, table, 0),
 
3998
  if (schema_table->old_format(session, schema_table) ||   /* Handle old syntax */
 
3999
      !sel->add_table_to_list(session, new Table_ident(session, db, table, 0),
4203
4000
                              0, 0, TL_READ))
4204
4001
  {
4205
 
    return(1);
 
4002
    return true;
4206
4003
  }
4207
 
  return(0);
 
4004
  return false;
4208
4005
}
4209
4006
 
4210
4007
 
4225
4022
                              enum enum_schema_table_state executed_place)
4226
4023
{
4227
4024
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
4228
 
  THD *thd= join->thd;
4229
 
  LEX *lex= thd->lex;
 
4025
  Session *session= join->session;
 
4026
  LEX *lex= session->lex;
4230
4027
  bool result= 0;
4231
4028
 
4232
 
  thd->no_warnings_for_error= 1;
 
4029
  session->no_warnings_for_error= 1;
4233
4030
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
4234
4031
  {
4235
4032
    if (!tab->table || !tab->table->pos_in_table_list)
4243
4040
 
4244
4041
 
4245
4042
      /* skip I_S optimizations specific to get_all_tables */
4246
 
      if (thd->lex->describe &&
 
4043
      if (session->lex->describe &&
4247
4044
          (table_list->schema_table->fill_table != get_all_tables))
4248
4045
        continue;
4249
4046
 
4277
4074
      else
4278
4075
        table_list->table->file->stats.records= 0;
4279
4076
 
4280
 
      if (table_list->schema_table->fill_table(thd, table_list,
 
4077
      if (table_list->schema_table->fill_table(session, table_list,
4281
4078
                                               tab->select_cond))
4282
4079
      {
4283
4080
        result= 1;
4290
4087
      table_list->schema_table_state= executed_place;
4291
4088
    }
4292
4089
  }
4293
 
  thd->no_warnings_for_error= 0;
 
4090
  session->no_warnings_for_error= 0;
4294
4091
  return(result);
4295
4092
}
4296
4093
 
4320
4117
  {"ROW_FORMAT", 10, DRIZZLE_TYPE_VARCHAR, 0, 1, "Row_format", OPEN_FULL_TABLE},
4321
4118
  {"TABLE_ROWS", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4322
4119
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Rows", OPEN_FULL_TABLE},
4323
 
  {"AVG_ROW_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0, 
 
4120
  {"AVG_ROW_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4324
4121
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Avg_row_length", OPEN_FULL_TABLE},
4325
 
  {"DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0, 
 
4122
  {"DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4326
4123
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Data_length", OPEN_FULL_TABLE},
4327
4124
  {"MAX_DATA_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4328
4125
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Max_data_length", OPEN_FULL_TABLE},
4329
 
  {"INDEX_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0, 
 
4126
  {"INDEX_LENGTH", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4330
4127
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Index_length", OPEN_FULL_TABLE},
4331
4128
  {"DATA_FREE", MY_INT64_NUM_DECIMAL_DIGITS, DRIZZLE_TYPE_LONGLONG, 0,
4332
4129
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Data_free", OPEN_FULL_TABLE},
4333
 
  {"AUTO_INCREMENT", MY_INT64_NUM_DECIMAL_DIGITS , DRIZZLE_TYPE_LONGLONG, 0, 
 
4130
  {"AUTO_INCREMENT", MY_INT64_NUM_DECIMAL_DIGITS , DRIZZLE_TYPE_LONGLONG, 0,
4334
4131
   (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED), "Auto_increment", OPEN_FULL_TABLE},
4335
4132
  {"CREATE_TIME", 0, DRIZZLE_TYPE_DATETIME, 0, 1, "Create_time", OPEN_FULL_TABLE},
4336
4133
  {"UPDATE_TIME", 0, DRIZZLE_TYPE_DATETIME, 0, 1, "Update_time", OPEN_FULL_TABLE},
4529
4326
 
4530
4327
ST_FIELD_INFO plugin_fields_info[]=
4531
4328
{
4532
 
  {"PLUGIN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Name", 
 
4329
  {"PLUGIN_NAME", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 0, "Name",
4533
4330
   SKIP_OPEN_TABLE},
4534
4331
  {"PLUGIN_VERSION", 20, DRIZZLE_TYPE_VARCHAR, 0, 0, 0, SKIP_OPEN_TABLE},
4535
4332
  {"PLUGIN_STATUS", 10, DRIZZLE_TYPE_VARCHAR, 0, 0, "Status", SKIP_OPEN_TABLE},
4536
 
  {"PLUGIN_TYPE", 80, DRIZZLE_TYPE_VARCHAR, 0, 0, "Type", SKIP_OPEN_TABLE},
4537
 
  {"PLUGIN_LIBRARY", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, "Library",
4538
 
   SKIP_OPEN_TABLE},
4539
4333
  {"PLUGIN_AUTHOR", NAME_CHAR_LEN, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4540
4334
  {"PLUGIN_DESCRIPTION", 65535, DRIZZLE_TYPE_VARCHAR, 0, 1, 0, SKIP_OPEN_TABLE},
4541
4335
  {"PLUGIN_LICENSE", 80, DRIZZLE_TYPE_VARCHAR, 0, 1, "License", SKIP_OPEN_TABLE},
4572
4366
 
4573
4367
*/
4574
4368
 
4575
 
ST_SCHEMA_TABLE schema_tables[]=
 
4369
InfoSchemaTable schema_tables[]=
4576
4370
{
4577
 
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table, 
 
4371
  {"CHARACTER_SETS", charsets_fields_info, create_schema_table,
4578
4372
   fill_schema_charsets, make_character_sets_old_format, 0, -1, -1, 0, 0},
4579
 
  {"COLLATIONS", collation_fields_info, create_schema_table, 
 
4373
  {"COLLATIONS", collation_fields_info, create_schema_table,
4580
4374
   fill_schema_collation, make_old_format, 0, -1, -1, 0, 0},
4581
4375
  {"COLLATION_CHARACTER_SET_APPLICABILITY", coll_charset_app_fields_info,
4582
4376
   create_schema_table, fill_schema_coll_charset_app, 0, 0, -1, -1, 0, 0},
4583
 
  {"COLUMNS", columns_fields_info, create_schema_table, 
 
4377
  {"COLUMNS", columns_fields_info, create_schema_table,
4584
4378
   get_all_tables, make_columns_old_format, get_schema_column_record, 1, 2, 0,
4585
4379
   OPTIMIZE_I_S_TABLE},
4586
4380
  {"GLOBAL_STATUS", variables_fields_info, create_schema_table,
4605
4399
   fill_status, make_old_format, 0, -1, -1, 0, 0},
4606
4400
  {"SESSION_VARIABLES", variables_fields_info, create_schema_table,
4607
4401
   fill_variables, make_old_format, 0, -1, -1, 0, 0},
4608
 
  {"STATISTICS", stat_fields_info, create_schema_table, 
 
4402
  {"STATISTICS", stat_fields_info, create_schema_table,
4609
4403
   get_all_tables, make_old_format, get_schema_stat_record, 1, 2, 0,
4610
4404
   OPEN_TABLE_ONLY|OPTIMIZE_I_S_TABLE},
4611
 
  {"STATUS", variables_fields_info, create_schema_table, fill_status, 
 
4405
  {"STATUS", variables_fields_info, create_schema_table, fill_status,
4612
4406
   make_old_format, 0, -1, -1, 1, 0},
4613
 
  {"TABLES", tables_fields_info, create_schema_table, 
 
4407
  {"TABLES", tables_fields_info, create_schema_table,
4614
4408
   get_all_tables, make_old_format, get_schema_tables_record, 1, 2, 0,
4615
4409
   OPTIMIZE_I_S_TABLE},
4616
4410
  {"TABLE_CONSTRAINTS", table_constraints_fields_info, create_schema_table,
4622
4416
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
4623
4417
};
4624
4418
 
4625
 
 
4626
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
4627
 
template class List_iterator_fast<char>;
4628
 
template class List<char>;
4629
 
#endif
4630
 
 
4631
 
int initialize_schema_table(st_plugin_int *plugin)
4632
 
{
4633
 
  ST_SCHEMA_TABLE *schema_table;
4634
 
 
4635
 
  if (!(schema_table= (ST_SCHEMA_TABLE *)my_malloc(sizeof(ST_SCHEMA_TABLE),
4636
 
                                MYF(MY_WME | MY_ZEROFILL))))
4637
 
      return(1);
4638
 
  /* Historical Requirement */
4639
 
  plugin->data= schema_table; // shortcut for the future
4640
 
  if (plugin->plugin->init)
4641
 
  {
4642
 
    schema_table->create_table= create_schema_table;
4643
 
    schema_table->old_format= make_old_format;
4644
 
    schema_table->idx_field1= -1, 
4645
 
    schema_table->idx_field2= -1; 
4646
 
 
4647
 
    /* Make the name available to the init() function. */
4648
 
    schema_table->table_name= plugin->name.str;
4649
 
 
4650
 
    if (plugin->plugin->init(schema_table))
4651
 
    {
4652
 
      sql_print_error(_("Plugin '%s' init function returned error."),
4653
 
                      plugin->name.str);
4654
 
      goto err;
4655
 
    }
4656
 
    
4657
 
    /* Make sure the plugin name is not set inside the init() function. */
4658
 
    schema_table->table_name= plugin->name.str;
4659
 
  }
4660
 
 
4661
 
  return(0);
4662
 
err:
4663
 
  free(schema_table);
4664
 
  return(1);
4665
 
}
4666
 
 
4667
 
int finalize_schema_table(st_plugin_int *plugin)
4668
 
{
4669
 
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
4670
 
 
4671
 
  if (schema_table && plugin->plugin->deinit)
4672
 
    free(schema_table);
4673
 
 
4674
 
  return(0);
4675
 
}