~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_show.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* 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 */
19
15
 
20
16
 
21
17
/* Function with list databases, tables or fields */
22
18
#include <drizzled/server_includes.h>
23
19
#include <drizzled/sql_select.h>
24
20
#include <drizzled/sql_show.h>
 
21
#include "repl_failsafe.h"
25
22
#include <mysys/my_dir.h>
26
 
#include <drizzled/gettext.h>
27
 
#include <drizzled/util/convert.h>
28
 
#include <drizzled/error.h>
29
 
#include <string>
30
 
#include <drizzled/tztime.h>
31
 
#include <drizzled/data_home.h>
 
23
#include <libdrizzle/gettext.h>
32
24
 
33
25
inline const char *
34
26
str_or_nil(const char *str)
39
31
/* Match the values of enum ha_choice */
40
32
static const char *ha_choice_values[] = {"", "0", "1"};
41
33
 
42
 
static void store_key_options(Session *session, String *packet, Table *table,
 
34
static void store_key_options(THD *thd, String *packet, Table *table,
43
35
                              KEY *key_info);
44
36
 
45
37
 
94
86
** List all table types supported
95
87
***************************************************************************/
96
88
 
97
 
static bool show_plugins(Session *session, plugin_ref plugin,
 
89
static bool show_plugins(THD *thd, plugin_ref plugin,
98
90
                            void *arg)
99
91
{
100
92
  Table *table= (Table*) arg;
176
168
  }
177
169
  table->field[7]->set_notnull();
178
170
 
179
 
  return schema_table_store_record(session, table);
 
171
  return schema_table_store_record(thd, table);
180
172
}
181
173
 
182
174
 
183
 
int fill_plugins(Session *session, TableList *tables, COND *)
 
175
int fill_plugins(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
184
176
{
185
177
  Table *table= tables->table;
186
178
 
187
 
  if (plugin_foreach_with_mask(session, show_plugins, DRIZZLE_ANY_PLUGIN,
 
179
  if (plugin_foreach_with_mask(thd, show_plugins, DRIZZLE_ANY_PLUGIN,
188
180
                               ~PLUGIN_IS_FREED, table))
189
181
    return(1);
190
182
 
197
189
 
198
190
  SYNOPSIS
199
191
    find_files()
200
 
    session                 thread handler
 
192
    thd                 thread handler
201
193
    files               put found files in this list
202
194
    db                  database name to set in TableList structure
203
195
    path                path to database
213
205
 
214
206
 
215
207
find_files_result
216
 
find_files(Session *session, List<LEX_STRING> *files, const char *db,
 
208
find_files(THD *thd, List<LEX_STRING> *files, const char *db,
217
209
           const char *path, const char *wild, bool dir)
218
210
{
219
 
  uint32_t i;
 
211
  uint i;
220
212
  char *ext;
221
213
  MY_DIR *dirp;
222
214
  FILEINFO *file;
223
215
  LEX_STRING *file_name= 0;
224
 
  uint32_t file_name_len;
 
216
  uint file_name_len;
225
217
  TableList table_list;
226
218
 
227
219
  if (wild && !wild[0])
271
263
      if (wild && wild_compare(uname, wild, 0))
272
264
        continue;
273
265
      if (!(file_name= 
274
 
            session->make_lex_string(file_name, uname, file_name_len, true)))
 
266
            thd->make_lex_string(file_name, uname, file_name_len, true)))
275
267
      {
276
268
        my_dirend(dirp);
277
269
        return(FIND_FILES_OOM);
297
289
      }
298
290
    }
299
291
    if (!(file_name= 
300
 
          session->make_lex_string(file_name, uname, file_name_len, true)) ||
 
292
          thd->make_lex_string(file_name, uname, file_name_len, true)) ||
301
293
        files->push_back(file_name))
302
294
    {
303
295
      my_dirend(dirp);
311
303
 
312
304
 
313
305
bool
314
 
mysqld_show_create(Session *session, TableList *table_list)
 
306
mysqld_show_create(THD *thd, TableList *table_list)
315
307
{
316
 
  Protocol *protocol= session->protocol;
 
308
  Protocol *protocol= thd->protocol;
317
309
  char buff[2048];
318
310
  String buffer(buff, sizeof(buff), system_charset_info);
319
311
 
320
312
  /* Only one table for now, but VIEW can involve several tables */
321
 
  if (open_normal_and_derived_tables(session, table_list, 0))
 
313
  if (open_normal_and_derived_tables(thd, table_list, 0))
322
314
  {
323
 
    if (session->is_error() && session->main_da.sql_errno() != ER_VIEW_INVALID)
 
315
    if (thd->is_error() && thd->main_da.sql_errno() != ER_VIEW_INVALID)
324
316
      return(true);
325
317
 
326
318
    /*
328
320
      issue a warning with 'warning' level status in 
329
321
      case of invalid view and last error is ER_VIEW_INVALID
330
322
    */
331
 
    drizzle_reset_errors(session, true);
332
 
    session->clear_error();
 
323
    drizzle_reset_errors(thd, true);
 
324
    thd->clear_error();
333
325
  }
334
326
 
335
327
  buffer.length(0);
336
328
 
337
 
  if (store_create_info(session, table_list, &buffer, NULL))
 
329
  if (store_create_info(thd, table_list, &buffer, NULL))
338
330
    return(true);
339
331
 
340
332
  List<Item> field_list;
342
334
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
343
335
    // 1024 is for not to confuse old clients
344
336
    field_list.push_back(new Item_empty_string("Create Table",
345
 
                                               cmax(buffer.length(),(uint32_t)1024)));
 
337
                                               max(buffer.length(),(uint32_t)1024)));
346
338
  }
347
339
 
348
340
  if (protocol->send_fields(&field_list,
362
354
  if (protocol->write())
363
355
    return(true);
364
356
 
365
 
  my_eof(session);
 
357
  my_eof(thd);
366
358
  return(false);
367
359
}
368
360
 
369
 
bool mysqld_show_create_db(Session *session, char *dbname,
 
361
bool mysqld_show_create_db(THD *thd, char *dbname,
370
362
                           HA_CREATE_INFO *create_info)
371
363
{
372
364
  char buff[2048];
373
365
  String buffer(buff, sizeof(buff), system_charset_info);
374
 
  Protocol *protocol=session->protocol;
 
366
  Protocol *protocol=thd->protocol;
375
367
 
376
 
  if (store_db_create_info(session, dbname, &buffer, create_info))
 
368
  if (store_db_create_info(thd, dbname, &buffer, create_info))
377
369
  {
378
370
    /* 
379
371
      This assumes that the only reason for which store_db_create_info()
397
389
 
398
390
  if (protocol->write())
399
391
    return(true);
400
 
  my_eof(session);
 
392
  my_eof(thd);
401
393
  return(false);
402
394
}
403
395
 
409
401
****************************************************************************/
410
402
 
411
403
void
412
 
mysqld_list_fields(Session *session, TableList *table_list, const char *wild)
 
404
mysqld_list_fields(THD *thd, TableList *table_list, const char *wild)
413
405
{
414
406
  Table *table;
415
407
 
416
 
  if (open_normal_and_derived_tables(session, table_list, 0))
 
408
  if (open_normal_and_derived_tables(thd, table_list, 0))
417
409
    return;
418
410
  table= table_list->table;
419
411
 
430
422
  }
431
423
  restore_record(table, s->default_values);              // Get empty record
432
424
  table->use_all_columns();
433
 
  if (session->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS))
 
425
  if (thd->protocol->send_fields(&field_list, Protocol::SEND_DEFAULTS))
434
426
    return;
435
 
  my_eof(session);
 
427
  my_eof(thd);
436
428
  return;
437
429
}
438
430
 
451
443
    0   No conflicting character
452
444
*/
453
445
 
454
 
static const char *require_quotes(const char *name, uint32_t name_length)
 
446
static const char *require_quotes(const char *name, uint name_length)
455
447
{
456
 
  uint32_t length;
 
448
  uint length;
457
449
  bool pure_digit= true;
458
450
  const char *end= name + name_length;
459
451
 
460
452
  for (; name < end ; name++)
461
453
  {
462
 
    unsigned char chr= (unsigned char) *name;
 
454
    uchar chr= (uchar) *name;
463
455
    length= my_mbcharlen(system_charset_info, chr);
464
456
    if (length == 1 && !system_charset_info->ident_map[chr])
465
457
      return name;
478
470
 
479
471
  SYNOPSIS
480
472
  append_identifier()
481
 
  session                   thread handler
 
473
  thd                   thread handler
482
474
  packet                target string
483
475
  name                  the identifier to be appended
484
476
  name_length           length of the appending identifier
485
477
*/
486
478
 
487
479
void
488
 
append_identifier(Session *session, String *packet, const char *name, uint32_t length)
 
480
append_identifier(THD *thd, String *packet, const char *name, uint length)
489
481
{
490
482
  const char *name_end;
491
483
  char quote_char;
492
 
  int q= get_quote_char_for_identifier(session, name, length);
 
484
  int q= get_quote_char_for_identifier(thd, name, length);
493
485
 
494
486
  if (q == EOF)
495
487
  {
502
494
   it's a keyword
503
495
  */
504
496
 
505
 
  packet->reserve(length*2 + 2);
 
497
  VOID(packet->reserve(length*2 + 2));
506
498
  quote_char= (char) q;
507
499
  packet->append(&quote_char, 1, system_charset_info);
508
500
 
509
501
  for (name_end= name+length ; name < name_end ; name+= length)
510
502
  {
511
 
    unsigned char chr= (unsigned char) *name;
 
503
    uchar chr= (uchar) *name;
512
504
    length= my_mbcharlen(system_charset_info, chr);
513
505
    /*
514
506
      my_mbcharlen can return 0 on a wrong multibyte
519
511
    */
520
512
    if (!length)
521
513
      length= 1;
522
 
    if (length == 1 && chr == (unsigned char) quote_char)
 
514
    if (length == 1 && chr == (uchar) quote_char)
523
515
      packet->append(&quote_char, 1, system_charset_info);
524
516
    packet->append(name, length, system_charset_info);
525
517
  }
532
524
 
533
525
  SYNOPSIS
534
526
    get_quote_char_for_identifier()
535
 
    session             Thread handler
 
527
    thd         Thread handler
536
528
    name        name to quote
537
529
    length      length of name
538
530
 
550
542
    #     Quote character
551
543
*/
552
544
 
553
 
int get_quote_char_for_identifier(Session *session, const char *name, uint32_t length)
 
545
int get_quote_char_for_identifier(THD *thd, const char *name, uint length)
554
546
{
555
547
  if (length &&
556
548
      !is_keyword(name,length) &&
557
549
      !require_quotes(name, length) &&
558
 
      !(session->options & OPTION_QUOTE_SHOW_CREATE))
 
550
      !(thd->options & OPTION_QUOTE_SHOW_CREATE))
559
551
    return EOF;
560
552
  return '`';
561
553
}
563
555
 
564
556
/* Append directory name (if exists) to CREATE INFO */
565
557
 
566
 
static void append_directory(Session *,
 
558
static void append_directory(THD *thd __attribute__((unused)),
567
559
                             String *packet, const char *dir_type,
568
 
                             const char *filename)
 
560
                             const char *filename)
569
561
{
570
562
  if (filename)
571
563
  {
572
 
    uint32_t length= dirname_length(filename);
 
564
    uint length= dirname_length(filename);
573
565
    packet->append(' ');
574
566
    packet->append(dir_type);
575
567
    packet->append(STRING_WITH_LEN(" DIRECTORY='"));
581
573
 
582
574
#define LIST_PROCESS_HOST_LEN 64
583
575
 
584
 
static bool get_field_default_value(Session *,
 
576
static bool get_field_default_value(THD *thd __attribute__((unused)),
585
577
                                    Field *timestamp_field,
586
578
                                    Field *field, String *def_value,
587
579
                                    bool quoted)
614
606
      if (type.length())
615
607
      {
616
608
        String def_val;
617
 
        uint32_t dummy_errors;
 
609
        uint dummy_errors;
618
610
        /* convert to system_charset_info == utf8 */
619
611
        def_val.copy(type.ptr(), type.length(), field->charset(),
620
612
                     system_charset_info, &dummy_errors);
640
632
 
641
633
  SYNOPSIS
642
634
    store_create_info()
643
 
    session               The thread
 
635
    thd               The thread
644
636
    table_list        A list containing one table to write statement
645
637
                      for.
646
638
    packet            Pointer to a string where statement will be
658
650
    0       OK
659
651
 */
660
652
 
661
 
int store_create_info(Session *session, TableList *table_list, String *packet,
 
653
int store_create_info(THD *thd, TableList *table_list, String *packet,
662
654
                      HA_CREATE_INFO *create_info_arg)
663
655
{
664
656
  List<Item> field_list;
665
 
  char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
 
657
  char tmp[MAX_FIELD_WIDTH], *for_str, buff[128], def_value_buf[MAX_FIELD_WIDTH];
666
658
  const char *alias;
667
 
  std::string buff;
668
659
  String type(tmp, sizeof(tmp), system_charset_info);
669
660
  String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
670
661
  Field **ptr,*field;
671
 
  uint32_t primary_key;
 
662
  uint primary_key;
672
663
  KEY *key_info;
673
664
  Table *table= table_list->table;
674
665
  handler *file= table->file;
697
688
      alias= share->table_name.str;
698
689
    }
699
690
  }
700
 
  append_identifier(session, packet, alias, strlen(alias));
 
691
  append_identifier(thd, packet, alias, strlen(alias));
701
692
  packet->append(STRING_WITH_LEN(" (\n"));
702
693
  /*
703
694
    We need this to get default values from the table
708
699
 
709
700
  for (ptr=table->field ; (field= *ptr); ptr++)
710
701
  {
711
 
    uint32_t flags = field->flags;
 
702
    uint flags = field->flags;
712
703
 
713
704
    if (ptr != table->field)
714
705
      packet->append(STRING_WITH_LEN(",\n"));
715
706
 
716
707
    packet->append(STRING_WITH_LEN("  "));
717
 
    append_identifier(session,packet,field->field_name, strlen(field->field_name));
 
708
    append_identifier(thd,packet,field->field_name, strlen(field->field_name));
718
709
    packet->append(' ');
719
710
    // check for surprises from the previous call to Field::sql_type()
720
711
    if (type.ptr() != tmp)
722
713
    else
723
714
      type.set_charset(system_charset_info);
724
715
 
725
 
    if (field->vcol_info)
726
 
    {
727
 
      packet->append(STRING_WITH_LEN("VIRTUAL "));
728
 
    }
729
 
 
730
716
    field->sql_type(type);
731
717
    packet->append(type.ptr(), type.length(), system_charset_info);
732
718
 
733
 
    if (field->vcol_info)
734
 
    {
735
 
      packet->append(STRING_WITH_LEN(" AS ("));
736
 
      packet->append(field->vcol_info->expr_str.str,
737
 
                     field->vcol_info->expr_str.length,
738
 
                     system_charset_info);
739
 
      packet->append(STRING_WITH_LEN(")"));
740
 
      if (field->is_stored)
741
 
        packet->append(STRING_WITH_LEN(" STORED"));
742
 
    }
743
 
    
744
719
    if (field->has_charset())
745
720
    {
746
721
      if (field->charset() != share->table_charset)
787
762
          packet->append(STRING_WITH_LEN(" DYNAMIC */"));
788
763
      }
789
764
    }
790
 
    if (!field->vcol_info &&
791
 
        get_field_default_value(session, table->timestamp_field,
 
765
    if (get_field_default_value(thd, table->timestamp_field,
792
766
                                field, &def_value, 1))
793
767
    {
794
768
      packet->append(STRING_WITH_LEN(" DEFAULT "));
815
789
  file->update_create_info(&create_info);
816
790
  primary_key= share->primary_key;
817
791
 
818
 
  for (uint32_t i=0 ; i < share->keys ; i++,key_info++)
 
792
  for (uint i=0 ; i < share->keys ; i++,key_info++)
819
793
  {
820
794
    KEY_PART_INFO *key_part= key_info->key_part;
821
795
    bool found_primary=0;
836
810
      packet->append(STRING_WITH_LEN("KEY "));
837
811
 
838
812
    if (!found_primary)
839
 
     append_identifier(session, packet, key_info->name, strlen(key_info->name));
 
813
     append_identifier(thd, packet, key_info->name, strlen(key_info->name));
840
814
 
841
815
    packet->append(STRING_WITH_LEN(" ("));
842
816
 
843
 
    for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
817
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
844
818
    {
845
819
      if (j)
846
820
        packet->append(',');
847
821
 
848
822
      if (key_part->field)
849
 
        append_identifier(session,packet,key_part->field->field_name,
 
823
        append_identifier(thd,packet,key_part->field->field_name,
850
824
                          strlen(key_part->field->field_name));
851
825
      if (key_part->field &&
852
826
          (key_part->length !=
853
827
           table->field[key_part->fieldnr-1]->key_length()))
854
828
      {
855
 
        buff= "(";
856
 
        buff= to_string(buff, (int32_t) key_part->length /
857
 
                              key_part->field->charset()->mbmaxlen);
858
 
        buff += ")";
859
 
        packet->append(buff.c_str(), buff.length());
 
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));
860
836
      }
861
837
    }
862
838
    packet->append(')');
863
 
    store_key_options(session, packet, table, key_info);
 
839
    store_key_options(thd, packet, table, key_info);
864
840
  }
865
841
 
866
842
  /*
906
882
 
907
883
    if (create_info.auto_increment_value > 1)
908
884
    {
 
885
      char *end;
909
886
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT="));
910
 
      buff= to_string(create_info.auto_increment_value);
911
 
      packet->append(buff.c_str(), buff.length());
 
887
      end= int64_t10_to_str(create_info.auto_increment_value, buff,10);
 
888
      packet->append(buff, (uint) (end - buff));
912
889
    }
913
890
 
914
891
    if (share->min_rows)
915
892
    {
 
893
      char *end;
916
894
      packet->append(STRING_WITH_LEN(" MIN_ROWS="));
917
 
      buff= to_string(share->min_rows);
918
 
      packet->append(buff.c_str(), buff.length());
 
895
      end= int64_t10_to_str(share->min_rows, buff, 10);
 
896
      packet->append(buff, (uint) (end- buff));
919
897
    }
920
898
 
921
899
    if (share->max_rows && !table_list->schema_table)
922
900
    {
 
901
      char *end;
923
902
      packet->append(STRING_WITH_LEN(" MAX_ROWS="));
924
 
      buff= to_string(share->max_rows);
925
 
      packet->append(buff.c_str(), buff.length());
 
903
      end= int64_t10_to_str(share->max_rows, buff, 10);
 
904
      packet->append(buff, (uint) (end - buff));
926
905
    }
927
906
 
928
907
    if (share->avg_row_length)
929
908
    {
 
909
      char *end;
930
910
      packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
931
 
      buff= to_string(share->avg_row_length);
932
 
      packet->append(buff.c_str(), buff.length());
 
911
      end= int64_t10_to_str(share->avg_row_length, buff,10);
 
912
      packet->append(buff, (uint) (end - buff));
933
913
    }
934
914
 
935
915
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
958
938
    }
959
939
    if (table->s->key_block_size)
960
940
    {
 
941
      char *end;
961
942
      packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
962
 
      buff= to_string(table->s->key_block_size);
963
 
      packet->append(buff.c_str(), buff.length());
 
943
      end= int64_t10_to_str(table->s->key_block_size, buff, 10);
 
944
      packet->append(buff, (uint) (end - buff));
964
945
    }
965
946
    if (share->block_size)
966
947
    {
 
948
      char *end;
967
949
      packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
968
 
      buff= to_string(share->block_size);
969
 
      packet->append(buff.c_str(), buff.length());
 
950
      end= int64_t10_to_str(share->block_size, buff,10);
 
951
      packet->append(buff, (uint) (end - buff));
970
952
    }
971
953
    table->file->append_create_info(packet);
972
954
    if (share->comment.length)
979
961
      packet->append(STRING_WITH_LEN(" CONNECTION="));
980
962
      append_unescaped(packet, share->connect_string.str, share->connect_string.length);
981
963
    }
982
 
    append_directory(session, packet, "DATA",  create_info.data_file_name);
983
 
    append_directory(session, packet, "INDEX", create_info.index_file_name);
 
964
    append_directory(thd, packet, "DATA",  create_info.data_file_name);
 
965
    append_directory(thd, packet, "INDEX", create_info.index_file_name);
984
966
  }
985
967
  table->restore_column_map(old_map);
986
968
  return(0);
999
981
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
1000
982
  in @c create_options are ignored.
1001
983
 
1002
 
  @param  session           The current thread instance.
 
984
  @param  thd           The current thread instance.
1003
985
  @param  dbname        The name of the database.
1004
986
  @param  buffer        A String instance where the statement is stored.
1005
987
  @param  create_info   If not NULL, the options member influences the resulting 
1008
990
  @returns true if errors are detected, false otherwise.
1009
991
*/
1010
992
 
1011
 
bool store_db_create_info(Session *session, const char *dbname, String *buffer,
 
993
bool store_db_create_info(THD *thd, const char *dbname, String *buffer,
1012
994
                          HA_CREATE_INFO *create_info)
1013
995
{
1014
996
  HA_CREATE_INFO create;
1015
 
  uint32_t create_options = create_info ? create_info->options : 0;
 
997
  uint create_options = create_info ? create_info->options : 0;
1016
998
 
1017
999
  if (!my_strcasecmp(system_charset_info, dbname,
1018
1000
                     INFORMATION_SCHEMA_NAME.str))
1025
1007
    if (check_db_dir_existence(dbname))
1026
1008
      return(true);
1027
1009
 
1028
 
    load_db_opt_by_name(session, dbname, &create);
 
1010
    load_db_opt_by_name(thd, dbname, &create);
1029
1011
  }
1030
1012
 
1031
1013
  buffer->length(0);
1036
1018
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
1037
1019
    buffer->append(STRING_WITH_LEN("/*!32312 IF NOT EXISTS*/ "));
1038
1020
 
1039
 
  append_identifier(session, buffer, dbname, strlen(dbname));
 
1021
  append_identifier(thd, buffer, dbname, strlen(dbname));
1040
1022
 
1041
1023
  if (create.default_table_charset)
1042
1024
  {
1054
1036
  return(false);
1055
1037
}
1056
1038
 
1057
 
static void store_key_options(Session *,
 
1039
static void store_key_options(THD *thd __attribute__((unused)),
1058
1040
                              String *packet, Table *table,
1059
1041
                              KEY *key_info)
1060
1042
{
1096
1078
  {
1097
1079
    return (void*) sql_alloc((uint) size);
1098
1080
  }
1099
 
  static void operator delete(void *, size_t)
 
1081
  static void operator delete(void *ptr __attribute__((unused)),
 
1082
                              size_t size __attribute__((unused)))
1100
1083
  { TRASH(ptr, size); }
1101
1084
 
1102
1085
  ulong thread_id;
1103
1086
  time_t start_time;
1104
 
  uint32_t   command;
 
1087
  uint   command;
1105
1088
  const char *user,*host,*db,*proc_info,*state_info;
1106
1089
  char *query;
1107
1090
};
1110
1093
template class I_List<thread_info>;
1111
1094
#endif
1112
1095
 
1113
 
void mysqld_list_processes(Session *session,const char *user, bool verbose)
 
1096
void mysqld_list_processes(THD *thd,const char *user, bool verbose)
1114
1097
{
1115
1098
  Item *field;
1116
1099
  List<Item> field_list;
1117
1100
  I_List<thread_info> thread_infos;
1118
 
  ulong max_query_length= (verbose ? session->variables.max_allowed_packet :
 
1101
  ulong max_query_length= (verbose ? thd->variables.max_allowed_packet :
1119
1102
                           PROCESS_LIST_WIDTH);
1120
 
  Protocol *protocol= session->protocol;
 
1103
  Protocol *protocol= thd->protocol;
1121
1104
 
1122
1105
  field_list.push_back(new Item_int("Id", 0, MY_INT32_NUM_DECIMAL_DIGITS));
1123
1106
  field_list.push_back(new Item_empty_string("User",16));
1134
1117
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
1135
1118
    return;
1136
1119
 
1137
 
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1138
 
  if (!session->killed)
 
1120
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
 
1121
  if (!thd->killed)
1139
1122
  {
1140
 
    I_List_iterator<Session> it(threads);
1141
 
    Session *tmp;
 
1123
    I_List_iterator<THD> it(threads);
 
1124
    THD *tmp;
1142
1125
    while ((tmp=it++))
1143
1126
    {
1144
1127
      Security_context *tmp_sctx= tmp->security_ctx;
1145
1128
      struct st_my_thread_var *mysys_var;
1146
1129
      if ((tmp->vio_ok() || tmp->system_thread) && (!user || (tmp_sctx->user && !strcmp(tmp_sctx->user, user))))
1147
1130
      {
1148
 
        thread_info *session_info= new thread_info;
 
1131
        thread_info *thd_info= new thread_info;
1149
1132
 
1150
 
        session_info->thread_id=tmp->thread_id;
1151
 
        session_info->user= session->strdup(tmp_sctx->user ? tmp_sctx->user :
 
1133
        thd_info->thread_id=tmp->thread_id;
 
1134
        thd_info->user= thd->strdup(tmp_sctx->user ? tmp_sctx->user :
1152
1135
                                    (tmp->system_thread ?
1153
1136
                                     "system user" : "unauthenticated user"));
1154
 
        session_info->host= session->strdup(tmp_sctx->ip);
1155
 
        if ((session_info->db=tmp->db))             // Safe test
1156
 
          session_info->db=session->strdup(session_info->db);
1157
 
        session_info->command=(int) tmp->command;
 
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;
1158
1141
        if ((mysys_var= tmp->mysys_var))
1159
1142
          pthread_mutex_lock(&mysys_var->mutex);
1160
 
        session_info->proc_info= (char*) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0);
1161
 
        session_info->state_info= (char*) (tmp->net.reading_or_writing ?
 
1143
        thd_info->proc_info= (char*) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0);
 
1144
        thd_info->state_info= (char*) (tmp->net.reading_or_writing ?
1162
1145
                                       (tmp->net.reading_or_writing == 2 ?
1163
1146
                                        "Writing to net" :
1164
 
                                        session_info->command == COM_SLEEP ? NULL :
 
1147
                                        thd_info->command == COM_SLEEP ? NullS :
1165
1148
                                        "Reading from net") :
1166
1149
                                       tmp->get_proc_info() ? tmp->get_proc_info() :
1167
1150
                                       tmp->mysys_var &&
1168
1151
                                       tmp->mysys_var->current_cond ?
1169
 
                                       "Waiting on cond" : NULL);
 
1152
                                       "Waiting on cond" : NullS);
1170
1153
        if (mysys_var)
1171
1154
          pthread_mutex_unlock(&mysys_var->mutex);
1172
1155
 
1173
 
        session_info->start_time= tmp->start_time;
1174
 
        session_info->query=0;
 
1156
        thd_info->start_time= tmp->start_time;
 
1157
        thd_info->query=0;
1175
1158
        if (tmp->query)
1176
1159
        {
1177
1160
          /* 
1179
1162
            the comment in sql_class.h why this prevents crashes in possible
1180
1163
            races with query_length
1181
1164
          */
1182
 
          uint32_t length= cmin((uint32_t)max_query_length, tmp->query_length);
1183
 
          session_info->query=(char*) session->strmake(tmp->query,length);
 
1165
          uint length= min((uint32_t)max_query_length, tmp->query_length);
 
1166
          thd_info->query=(char*) thd->strmake(tmp->query,length);
1184
1167
        }
1185
 
        thread_infos.append(session_info);
 
1168
        thread_infos.append(thd_info);
1186
1169
      }
1187
1170
    }
1188
1171
  }
1189
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
1172
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1190
1173
 
1191
 
  thread_info *session_info;
 
1174
  thread_info *thd_info;
1192
1175
  time_t now= my_time(0);
1193
 
  while ((session_info=thread_infos.get()))
 
1176
  while ((thd_info=thread_infos.get()))
1194
1177
  {
1195
1178
    protocol->prepare_for_resend();
1196
 
    protocol->store((uint64_t) session_info->thread_id);
1197
 
    protocol->store(session_info->user, system_charset_info);
1198
 
    protocol->store(session_info->host, system_charset_info);
1199
 
    protocol->store(session_info->db, system_charset_info);
1200
 
    if (session_info->proc_info)
1201
 
      protocol->store(session_info->proc_info, system_charset_info);
 
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);
1202
1185
    else
1203
 
      protocol->store(command_name[session_info->command].str, system_charset_info);
1204
 
    if (session_info->start_time)
1205
 
      protocol->store((uint32_t) (now - session_info->start_time));
 
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));
1206
1189
    else
1207
1190
      protocol->store_null();
1208
 
    protocol->store(session_info->state_info, system_charset_info);
1209
 
    protocol->store(session_info->query, system_charset_info);
 
1191
    protocol->store(thd_info->state_info, system_charset_info);
 
1192
    protocol->store(thd_info->query, system_charset_info);
1210
1193
    if (protocol->write())
1211
1194
      break; /* purecov: inspected */
1212
1195
  }
1213
 
  my_eof(session);
 
1196
  my_eof(thd);
1214
1197
  return;
1215
1198
}
1216
1199
 
1217
 
int fill_schema_processlist(Session* session, TableList* tables, COND*)
 
1200
int fill_schema_processlist(THD* thd, TableList* tables,
 
1201
                            COND* cond __attribute__((unused)))
1218
1202
{
1219
1203
  Table *table= tables->table;
1220
1204
  const CHARSET_INFO * const cs= system_charset_info;
1221
1205
  char *user;
1222
1206
  time_t now= my_time(0);
1223
1207
 
1224
 
  user= NULL;
1225
 
 
1226
 
  pthread_mutex_lock(&LOCK_thread_count);
1227
 
 
1228
 
  if (!session->killed)
 
1208
  user= NullS;
 
1209
 
 
1210
  VOID(pthread_mutex_lock(&LOCK_thread_count));
 
1211
 
 
1212
  if (!thd->killed)
1229
1213
  {
1230
 
    I_List_iterator<Session> it(threads);
1231
 
    Session* tmp;
 
1214
    I_List_iterator<THD> it(threads);
 
1215
    THD* tmp;
1232
1216
 
1233
1217
    while ((tmp= it++))
1234
1218
    {
1258
1242
      if ((mysys_var= tmp->mysys_var))
1259
1243
        pthread_mutex_lock(&mysys_var->mutex);
1260
1244
      /* COMMAND */
1261
 
      if ((val= (char *) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0)))
 
1245
      if ((val= (char *) (tmp->killed == THD::KILL_CONNECTION? "Killed" : 0)))
1262
1246
        table->field[4]->store(val, strlen(val), cs);
1263
1247
      else
1264
1248
        table->field[4]->store(command_name[tmp->command].str,
1270
1254
      val= (char*) (tmp->net.reading_or_writing ?
1271
1255
                    (tmp->net.reading_or_writing == 2 ?
1272
1256
                     "Writing to net" :
1273
 
                     tmp->command == COM_SLEEP ? NULL :
 
1257
                     tmp->command == COM_SLEEP ? NullS :
1274
1258
                     "Reading from net") :
1275
1259
                    tmp->get_proc_info() ? tmp->get_proc_info() :
1276
1260
                    tmp->mysys_var &&
1277
1261
                    tmp->mysys_var->current_cond ?
1278
 
                    "Waiting on cond" : NULL);
 
1262
                    "Waiting on cond" : NullS);
1279
1263
      if (val)
1280
1264
      {
1281
1265
        table->field[6]->store(val, strlen(val), cs);
1289
1273
      if (tmp->query)
1290
1274
      {
1291
1275
        table->field[7]->store(tmp->query,
1292
 
                               cmin((uint32_t)PROCESS_LIST_INFO_WIDTH,
 
1276
                               min((uint32_t)PROCESS_LIST_INFO_WIDTH,
1293
1277
                                   tmp->query_length), cs);
1294
1278
        table->field[7]->set_notnull();
1295
1279
      }
1296
1280
 
1297
 
      if (schema_table_store_record(session, table))
 
1281
      if (schema_table_store_record(thd, table))
1298
1282
      {
1299
 
        pthread_mutex_unlock(&LOCK_thread_count);
 
1283
        VOID(pthread_mutex_unlock(&LOCK_thread_count));
1300
1284
        return(1);
1301
1285
      }
1302
1286
    }
1303
1287
  }
1304
1288
 
1305
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
1289
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1306
1290
  return(0);
1307
1291
}
1308
1292
 
1323
1307
*/
1324
1308
static void shrink_var_array(DYNAMIC_ARRAY *array)
1325
1309
{
1326
 
  uint32_t a,b;
 
1310
  uint a,b;
1327
1311
  SHOW_VAR *all= dynamic_element(array, 0, SHOW_VAR *);
1328
1312
 
1329
1313
  for (a= b= 0; b < array->elements; b++)
1369
1353
    goto err;
1370
1354
  }
1371
1355
  while (list->name)
1372
 
    res|= insert_dynamic(&all_status_vars, (unsigned char*)list++);
1373
 
  res|= insert_dynamic(&all_status_vars, (unsigned char*)list); // appending NULL-element
 
1356
    res|= insert_dynamic(&all_status_vars, (uchar*)list++);
 
1357
  res|= insert_dynamic(&all_status_vars, (uchar*)list); // appending NULL-element
1374
1358
  all_status_vars.elements--; // but next insert_dynamic should overwite it
1375
1359
  if (status_vars_inited)
1376
1360
    sort_dynamic(&all_status_vars, show_var_cmp);
1464
1448
  else
1465
1449
  {
1466
1450
    SHOW_VAR *all= dynamic_element(&all_status_vars, 0, SHOW_VAR *);
1467
 
    uint32_t i;
 
1451
    uint i;
1468
1452
    for (; list->name; list++)
1469
1453
    {
1470
1454
      for (i= 0; i < all_status_vars.elements; i++)
1485
1469
    *buf= my_toupper(system_charset_info, *buf);
1486
1470
}
1487
1471
 
1488
 
static bool show_status_array(Session *session, const char *wild,
 
1472
static bool show_status_array(THD *thd, const char *wild,
1489
1473
                              SHOW_VAR *variables,
1490
1474
                              enum enum_var_type value_type,
1491
1475
                              struct system_status_var *status_var,
1504
1488
  null_lex_str.str= 0;                          // For sys_var->value_ptr()
1505
1489
  null_lex_str.length= 0;
1506
1490
 
1507
 
  prefix_end=my_stpncpy(name_buffer, prefix, sizeof(name_buffer)-1);
 
1491
  prefix_end=stpncpy(name_buffer, prefix, sizeof(name_buffer)-1);
1508
1492
  if (*prefix)
1509
1493
    *prefix_end++= '_';
1510
1494
  len=name_buffer + sizeof(name_buffer) - prefix_end;
1511
1495
 
1512
1496
  for (; variables->name; variables++)
1513
1497
  {
1514
 
    my_stpncpy(prefix_end, variables->name, len);
 
1498
    stpncpy(prefix_end, variables->name, len);
1515
1499
    name_buffer[sizeof(name_buffer)-1]=0;       /* Safety */
1516
1500
    if (ucase_names)
1517
1501
      make_upper(name_buffer);
1521
1505
      Repeat as necessary, if new var is again SHOW_FUNC
1522
1506
    */
1523
1507
    for (var=variables; var->type == SHOW_FUNC; var= &tmp)
1524
 
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(session, &tmp, buff);
 
1508
      ((mysql_show_var_func)((st_show_var_func_container *)var->value)->func)(thd, &tmp, buff);
1525
1509
 
1526
1510
    SHOW_TYPE show_type=var->type;
1527
1511
    if (show_type == SHOW_ARRAY)
1528
1512
    {
1529
 
      show_status_array(session, wild, (SHOW_VAR *) var->value, value_type,
 
1513
      show_status_array(thd, wild, (SHOW_VAR *) var->value, value_type,
1530
1514
                        status_var, name_buffer, table, ucase_names);
1531
1515
    }
1532
1516
    else
1542
1526
        if (show_type == SHOW_SYS)
1543
1527
        {
1544
1528
          show_type= ((sys_var*) value)->show_type();
1545
 
          value=     (char*) ((sys_var*) value)->value_ptr(session, value_type,
 
1529
          value=     (char*) ((sys_var*) value)->value_ptr(thd, value_type,
1546
1530
                                                           &null_lex_str);
1547
1531
        }
1548
1532
 
1576
1560
          end= int64_t10_to_str((int64_t) *(ha_rows*) value, buff, 10);
1577
1561
          break;
1578
1562
        case SHOW_BOOL:
1579
 
          end= my_stpcpy(buff, *(bool*) value ? "ON" : "OFF");
 
1563
          end= stpcpy(buff, *(bool*) value ? "ON" : "OFF");
1580
1564
          break;
1581
1565
        case SHOW_MY_BOOL:
1582
 
          end= my_stpcpy(buff, *(bool*) value ? "ON" : "OFF");
 
1566
          end= stpcpy(buff, *(bool*) value ? "ON" : "OFF");
1583
1567
          break;
1584
1568
        case SHOW_INT:
1585
1569
          end= int10_to_str((long) *(uint32_t*) value, buff, 10);
1628
1612
 
1629
1613
        pthread_mutex_unlock(&LOCK_global_system_variables);
1630
1614
 
1631
 
        if (schema_table_store_record(session, table))
 
1615
        if (schema_table_store_record(thd, table))
1632
1616
          return(true);
1633
1617
      }
1634
1618
    }
1644
1628
{
1645
1629
 
1646
1630
  /* Ensure that thread id not killed during loop */
1647
 
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1631
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
1648
1632
 
1649
 
  I_List_iterator<Session> it(threads);
1650
 
  Session *tmp;
 
1633
  I_List_iterator<THD> it(threads);
 
1634
  THD *tmp;
1651
1635
  
1652
1636
  /* Get global values as base */
1653
1637
  *to= global_status_var;
1656
1640
  while ((tmp= it++))
1657
1641
    add_to_status(to, &tmp->status_var);
1658
1642
  
1659
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
1643
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1660
1644
  return;
1661
1645
}
1662
1646
 
1677
1661
 
1678
1662
  SYNOPSIS
1679
1663
    schema_table_store_record()
1680
 
    session                   thread handler
 
1664
    thd                   thread handler
1681
1665
    table                 Information schema table to be updated
1682
1666
 
1683
1667
  RETURN
1685
1669
    1                     error
1686
1670
*/
1687
1671
 
1688
 
bool schema_table_store_record(Session *session, Table *table)
 
1672
bool schema_table_store_record(THD *thd, Table *table)
1689
1673
{
1690
1674
  int error;
1691
1675
  if ((error= table->file->ha_write_row(table->record[0])))
1692
1676
  {
1693
1677
    TMP_TABLE_PARAM *param= table->pos_in_table_list->schema_table_param;
1694
1678
 
1695
 
    if (create_myisam_from_heap(session, table, param->start_recinfo, 
 
1679
    if (create_myisam_from_heap(thd, table, param->start_recinfo, 
1696
1680
                                &param->recinfo, error, 0))
1697
1681
      return 1;
1698
1682
  }
1700
1684
}
1701
1685
 
1702
1686
 
1703
 
int make_table_list(Session *session, SELECT_LEX *sel,
 
1687
int make_table_list(THD *thd, SELECT_LEX *sel,
1704
1688
                    LEX_STRING *db_name, LEX_STRING *table_name)
1705
1689
{
1706
1690
  Table_ident *table_ident;
1707
 
  table_ident= new Table_ident(session, *db_name, *table_name, 1);
 
1691
  table_ident= new Table_ident(thd, *db_name, *table_name, 1);
1708
1692
  sel->init_query();
1709
 
  if (!sel->add_table_to_list(session, table_ident, 0, 0, TL_READ))
 
1693
  if (!sel->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
1710
1694
    return 1;
1711
1695
  return 0;
1712
1696
}
1720
1704
           fill appropriate lookup_field_vals struct field
1721
1705
           with this value.
1722
1706
 
1723
 
  @param[in]      session                   thread handler
 
1707
  @param[in]      thd                   thread handler
1724
1708
  @param[in]      item_func             part of WHERE condition
1725
1709
  @param[in]      table                 I_S table
1726
1710
  @param[in, out] lookup_field_vals     Struct which holds lookup values 
1730
1714
    1             error, there can be no matching records for the condition
1731
1715
*/
1732
1716
 
1733
 
bool get_lookup_value(Session *session, Item_func *item_func,
 
1717
bool get_lookup_value(THD *thd, Item_func *item_func,
1734
1718
                      TableList *table, 
1735
1719
                      LOOKUP_FIELD_VALUES *lookup_field_vals)
1736
1720
{
1775
1759
      return 1;
1776
1760
 
1777
1761
    /* Lookup value is database name */
1778
 
    if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1779
 
                               (unsigned char *) item_field->field_name,
 
1762
    if (!cs->coll->strnncollsp(cs, (uchar *) field_name1, strlen(field_name1),
 
1763
                               (uchar *) item_field->field_name,
1780
1764
                               strlen(item_field->field_name), 0))
1781
1765
    {
1782
 
      session->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
 
1766
      thd->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
1783
1767
                           tmp_str->length(), false);
1784
1768
    }
1785
1769
    /* Lookup value is table name */
1786
 
    else if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name2,
 
1770
    else if (!cs->coll->strnncollsp(cs, (uchar *) field_name2,
1787
1771
                                    strlen(field_name2),
1788
 
                                    (unsigned char *) item_field->field_name,
 
1772
                                    (uchar *) item_field->field_name,
1789
1773
                                    strlen(item_field->field_name), 0))
1790
1774
    {
1791
 
      session->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
 
1775
      thd->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
1792
1776
                           tmp_str->length(), false);
1793
1777
    }
1794
1778
  }
1803
1787
           from 'WHERE' condition if it's possible and 
1804
1788
           fill lookup_field_vals struct fields with these values.
1805
1789
 
1806
 
  @param[in]      session                   thread handler
 
1790
  @param[in]      thd                   thread handler
1807
1791
  @param[in]      cond                  WHERE condition
1808
1792
  @param[in]      table                 I_S table
1809
1793
  @param[in, out] lookup_field_vals     Struct which holds lookup values 
1813
1797
    1             error, there can be no matching records for the condition
1814
1798
*/
1815
1799
 
1816
 
bool calc_lookup_values_from_cond(Session *session, COND *cond, TableList *table,
 
1800
bool calc_lookup_values_from_cond(THD *thd, COND *cond, TableList *table,
1817
1801
                                  LOOKUP_FIELD_VALUES *lookup_field_vals)
1818
1802
{
1819
1803
  if (!cond)
1829
1813
      {
1830
1814
        if (item->type() == Item::FUNC_ITEM)
1831
1815
        {
1832
 
          if (get_lookup_value(session, (Item_func*)item, table, lookup_field_vals))
 
1816
          if (get_lookup_value(thd, (Item_func*)item, table, lookup_field_vals))
1833
1817
            return 1;
1834
1818
        }
1835
1819
        else
1836
1820
        {
1837
 
          if (calc_lookup_values_from_cond(session, item, table, lookup_field_vals))
 
1821
          if (calc_lookup_values_from_cond(thd, item, table, lookup_field_vals))
1838
1822
            return 1;
1839
1823
        }
1840
1824
      }
1842
1826
    return 0;
1843
1827
  }
1844
1828
  else if (cond->type() == Item::FUNC_ITEM &&
1845
 
           get_lookup_value(session, (Item_func*) cond, table, lookup_field_vals))
 
1829
           get_lookup_value(thd, (Item_func*) cond, table, lookup_field_vals))
1846
1830
    return 1;
1847
1831
  return 0;
1848
1832
}
1853
1837
  if (item->type() == Item::FUNC_ITEM)
1854
1838
  {
1855
1839
    Item_func *item_func= (Item_func*)item;
1856
 
    for (uint32_t i=0; i<item_func->argument_count(); i++)
 
1840
    for (uint i=0; i<item_func->argument_count(); i++)
1857
1841
    {
1858
1842
      if (!uses_only_table_name_fields(item_func->arguments()[i], table))
1859
1843
        return 0;
1870
1854
    const char *field_name2= schema_table->idx_field2 >= 0 ?
1871
1855
      field_info[schema_table->idx_field2].field_name : "";
1872
1856
    if (table->table != item_field->field->table ||
1873
 
        (cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1874
 
                               (unsigned char *) item_field->field_name,
 
1857
        (cs->coll->strnncollsp(cs, (uchar *) field_name1, strlen(field_name1),
 
1858
                               (uchar *) item_field->field_name,
1875
1859
                               strlen(item_field->field_name), 0) &&
1876
 
         cs->coll->strnncollsp(cs, (unsigned char *) field_name2, strlen(field_name2),
1877
 
                               (unsigned char *) item_field->field_name,
 
1860
         cs->coll->strnncollsp(cs, (uchar *) field_name2, strlen(field_name2),
 
1861
                               (uchar *) item_field->field_name,
1878
1862
                               strlen(item_field->field_name), 0)))
1879
1863
      return 0;
1880
1864
  }
1952
1936
           from LEX struct and fill lookup_field_vals struct field
1953
1937
           with these values.
1954
1938
 
1955
 
  @param[in]      session                   thread handler
 
1939
  @param[in]      thd                   thread handler
1956
1940
  @param[in]      cond                  WHERE condition
1957
1941
  @param[in]      tables                I_S table
1958
1942
  @param[in, out] lookup_field_values   Struct which holds lookup values 
1962
1946
    1             error, there can be no matching records for the condition
1963
1947
*/
1964
1948
 
1965
 
bool get_lookup_field_values(Session *session, COND *cond, TableList *tables,
 
1949
bool get_lookup_field_values(THD *thd, COND *cond, TableList *tables,
1966
1950
                             LOOKUP_FIELD_VALUES *lookup_field_values)
1967
1951
{
1968
 
  LEX *lex= session->lex;
1969
 
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
 
1952
  LEX *lex= thd->lex;
 
1953
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
1970
1954
  memset(lookup_field_values, 0, sizeof(LOOKUP_FIELD_VALUES));
1971
1955
  switch (lex->sql_command) {
1972
1956
  case SQLCOM_SHOW_DATABASES:
1993
1977
      The "default" is for queries over I_S.
1994
1978
      All previous cases handle SHOW commands.
1995
1979
    */
1996
 
    return calc_lookup_values_from_cond(session, cond, tables, lookup_field_values);
 
1980
    return calc_lookup_values_from_cond(thd, cond, tables, lookup_field_values);
1997
1981
  }
1998
1982
}
1999
1983
 
2009
1993
 
2010
1994
  SYNOPSIS
2011
1995
    make_db_list()
2012
 
    session                   thread handler
 
1996
    thd                   thread handler
2013
1997
    files                 list of db names
2014
1998
    wild                  wild string
2015
1999
    idx_field_vals        idx_field_vals->db_name contains db name or
2022
2006
    non-zero              error
2023
2007
*/
2024
2008
 
2025
 
int make_db_list(Session *session, List<LEX_STRING> *files,
 
2009
int make_db_list(THD *thd, List<LEX_STRING> *files,
2026
2010
                 LOOKUP_FIELD_VALUES *lookup_field_vals,
2027
2011
                 bool *with_i_schema)
2028
2012
{
2029
2013
  LEX_STRING *i_s_name_copy= 0;
2030
 
  i_s_name_copy= session->make_lex_string(i_s_name_copy,
 
2014
  i_s_name_copy= thd->make_lex_string(i_s_name_copy,
2031
2015
                                      INFORMATION_SCHEMA_NAME.str,
2032
2016
                                      INFORMATION_SCHEMA_NAME.length, true);
2033
2017
  *with_i_schema= 0;
2047
2031
      if (files->push_back(i_s_name_copy))
2048
2032
        return 1;
2049
2033
    }
2050
 
    return (find_files(session, files, NULL, mysql_data_home,
 
2034
    return (find_files(thd, files, NullS, mysql_data_home,
2051
2035
                       lookup_field_vals->db_value.str, 1) != FIND_FILES_OK);
2052
2036
  }
2053
2037
 
2078
2062
  if (files->push_back(i_s_name_copy))
2079
2063
    return 1;
2080
2064
  *with_i_schema= 1;
2081
 
  return (find_files(session, files, NULL,
2082
 
                     mysql_data_home, NULL, 1) != FIND_FILES_OK);
 
2065
  return (find_files(thd, files, NullS,
 
2066
                     mysql_data_home, NullS, 1) != FIND_FILES_OK);
2083
2067
}
2084
2068
 
2085
2069
 
2090
2074
};
2091
2075
 
2092
2076
 
2093
 
static bool add_schema_table(Session *session, plugin_ref plugin,
 
2077
static bool add_schema_table(THD *thd, plugin_ref plugin,
2094
2078
                                void* p_data)
2095
2079
{
2096
2080
  LEX_STRING *file_name= 0;
2114
2098
      return(0);
2115
2099
  }
2116
2100
 
2117
 
  if ((file_name= session->make_lex_string(file_name, schema_table->table_name,
 
2101
  if ((file_name= thd->make_lex_string(file_name, schema_table->table_name,
2118
2102
                                       strlen(schema_table->table_name),
2119
2103
                                       true)) &&
2120
2104
      !file_list->push_back(file_name))
2123
2107
}
2124
2108
 
2125
2109
 
2126
 
int schema_tables_add(Session *session, List<LEX_STRING> *files, const char *wild)
 
2110
int schema_tables_add(THD *thd, List<LEX_STRING> *files, const char *wild)
2127
2111
{
2128
2112
  LEX_STRING *file_name= 0;
2129
2113
  ST_SCHEMA_TABLE *tmp_schema_table= schema_tables;
2146
2130
        continue;
2147
2131
    }
2148
2132
    if ((file_name= 
2149
 
         session->make_lex_string(file_name, tmp_schema_table->table_name,
 
2133
         thd->make_lex_string(file_name, tmp_schema_table->table_name,
2150
2134
                              strlen(tmp_schema_table->table_name), true)) &&
2151
2135
        !files->push_back(file_name))
2152
2136
      continue;
2155
2139
 
2156
2140
  add_data.files= files;
2157
2141
  add_data.wild= wild;
2158
 
  if (plugin_foreach(session, add_schema_table,
 
2142
  if (plugin_foreach(thd, add_schema_table,
2159
2143
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &add_data))
2160
2144
    return(1);
2161
2145
 
2169
2153
  @details        The function creates the list of table names in
2170
2154
                  database
2171
2155
 
2172
 
  @param[in]      session                   thread handler
 
2156
  @param[in]      thd                   thread handler
2173
2157
  @param[in]      table_names           List of table names in database
2174
2158
  @param[in]      lex                   pointer to LEX struct
2175
2159
  @param[in]      lookup_field_vals     pointer to LOOKUP_FIELD_VALUE struct
2183
2167
*/
2184
2168
 
2185
2169
static int
2186
 
make_table_name_list(Session *session, List<LEX_STRING> *table_names, LEX *lex,
 
2170
make_table_name_list(THD *thd, List<LEX_STRING> *table_names, LEX *lex,
2187
2171
                     LOOKUP_FIELD_VALUES *lookup_field_vals,
2188
2172
                     bool with_i_schema, LEX_STRING *db_name)
2189
2173
{
2194
2178
  {
2195
2179
    if (with_i_schema)
2196
2180
    {
2197
 
      if (find_schema_table(session, lookup_field_vals->table_value.str))
 
2181
      if (find_schema_table(thd, lookup_field_vals->table_value.str))
2198
2182
      {
2199
2183
        if (table_names->push_back(&lookup_field_vals->table_value))
2200
2184
          return 1;
2213
2197
    to the list
2214
2198
  */
2215
2199
  if (with_i_schema)
2216
 
    return (schema_tables_add(session, table_names,
 
2200
    return (schema_tables_add(thd, table_names,
2217
2201
                              lookup_field_vals->table_value.str));
2218
2202
 
2219
 
  find_files_result res= find_files(session, table_names, db_name->str, path,
 
2203
  find_files_result res= find_files(thd, table_names, db_name->str, path,
2220
2204
                                    lookup_field_vals->table_value.str, 0);
2221
2205
  if (res != FIND_FILES_OK)
2222
2206
  {
2230
2214
    {
2231
2215
      if (lex->sql_command != SQLCOM_SELECT)
2232
2216
        return 1;
2233
 
      session->clear_error();
 
2217
      thd->clear_error();
2234
2218
      return 2;
2235
2219
    }
2236
2220
    return 1;
2242
2226
/**
2243
2227
  @brief          Fill I_S table for SHOW COLUMNS|INDEX commands
2244
2228
 
2245
 
  @param[in]      session                      thread handler
 
2229
  @param[in]      thd                      thread handler
2246
2230
  @param[in]      tables                   TableList for I_S table
2247
2231
  @param[in]      schema_table             pointer to I_S structure
2248
2232
  @param[in]      open_tables_state_backup pointer to Open_tables_state object
2256
2240
*/
2257
2241
 
2258
2242
static int 
2259
 
fill_schema_show_cols_or_idxs(Session *session, TableList *tables,
 
2243
fill_schema_show_cols_or_idxs(THD *thd, TableList *tables,
2260
2244
                              ST_SCHEMA_TABLE *schema_table,
2261
2245
                              Open_tables_state *open_tables_state_backup)
2262
2246
{
2263
 
  LEX *lex= session->lex;
 
2247
  LEX *lex= thd->lex;
2264
2248
  bool res;
2265
2249
  LEX_STRING tmp_lex_string, tmp_lex_string1, *db_name, *table_name;
2266
2250
  enum_sql_command save_sql_command= lex->sql_command;
2271
2255
 
2272
2256
  lex->all_selects_list= tables->schema_select_lex;
2273
2257
  /*
2274
 
    Restore session->temporary_tables to be able to process
 
2258
    Restore thd->temporary_tables to be able to process
2275
2259
    temporary tables(only for 'show index' & 'show columns').
2276
2260
    This should be changed when processing of temporary tables for
2277
2261
    I_S tables will be done.
2278
2262
  */
2279
 
  session->temporary_tables= open_tables_state_backup->temporary_tables;
 
2263
  thd->temporary_tables= open_tables_state_backup->temporary_tables;
2280
2264
  /*
2281
2265
    Let us set fake sql_command so views won't try to merge
2282
2266
    themselves into main statement. If we don't do this,
2284
2268
    SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()' 
2285
2269
  */
2286
2270
  lex->sql_command= SQLCOM_SHOW_FIELDS;
2287
 
  res= open_normal_and_derived_tables(session, show_table_list,
 
2271
  res= open_normal_and_derived_tables(thd, show_table_list,
2288
2272
                                      DRIZZLE_LOCK_IGNORE_FLUSH);
2289
2273
  lex->sql_command= save_sql_command;
2290
2274
  /*
2298
2282
    in this case(this part of code is used only for 
2299
2283
    'show columns' & 'show statistics' commands).
2300
2284
  */
2301
 
   table_name= session->make_lex_string(&tmp_lex_string1, show_table_list->alias,
 
2285
   table_name= thd->make_lex_string(&tmp_lex_string1, show_table_list->alias,
2302
2286
                                    strlen(show_table_list->alias), false);
2303
 
   db_name= session->make_lex_string(&tmp_lex_string, show_table_list->db,
 
2287
   db_name= thd->make_lex_string(&tmp_lex_string, show_table_list->db,
2304
2288
                                 show_table_list->db_length, false);
2305
2289
      
2306
2290
 
2307
 
   error= test(schema_table->process_table(session, show_table_list,
 
2291
   error= test(schema_table->process_table(thd, show_table_list,
2308
2292
                                           table, res, db_name,
2309
2293
                                           table_name));
2310
 
   session->temporary_tables= 0;
2311
 
   close_tables_for_reopen(session, &show_table_list);
 
2294
   thd->temporary_tables= 0;
 
2295
   close_tables_for_reopen(thd, &show_table_list);
2312
2296
   return(error);
2313
2297
}
2314
2298
 
2316
2300
/**
2317
2301
  @brief          Fill I_S table for SHOW Table NAMES commands
2318
2302
 
2319
 
  @param[in]      session                      thread handler
 
2303
  @param[in]      thd                      thread handler
2320
2304
  @param[in]      table                    Table struct for I_S table
2321
2305
  @param[in]      db_name                  database name
2322
2306
  @param[in]      table_name               table name
2327
2311
    @retval       1           error
2328
2312
*/
2329
2313
 
2330
 
static int fill_schema_table_names(Session *session, Table *table,
 
2314
static int fill_schema_table_names(THD *thd, Table *table,
2331
2315
                                   LEX_STRING *db_name, LEX_STRING *table_name,
2332
2316
                                   bool with_i_schema)
2333
2317
{
2342
2326
    char path[FN_REFLEN];
2343
2327
    (void) build_table_filename(path, sizeof(path), db_name->str, 
2344
2328
                                table_name->str, reg_ext, 0);
2345
 
    if (mysql_frm_type(session, path, &not_used)) 
 
2329
    if (mysql_frm_type(thd, path, &not_used)) 
2346
2330
    {
2347
2331
      table->field[3]->store(STRING_WITH_LEN("BASE Table"),
2348
2332
                             system_charset_info);
2353
2337
                             system_charset_info);
2354
2338
    }
2355
2339
 
2356
 
    if (session->is_error() && session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
 
2340
    if (thd->is_error() && thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2357
2341
    {
2358
 
      session->clear_error();
 
2342
      thd->clear_error();
2359
2343
      return 0;
2360
2344
    }
2361
2345
  }
2362
 
  if (schema_table_store_record(session, table))
 
2346
  if (schema_table_store_record(thd, table))
2363
2347
    return 1;
2364
2348
  return 0;
2365
2349
}
2381
2365
    @retval       SKIP_OPEN_TABLE | OPEN_FRM_ONLY | OPEN_FULL_TABLE
2382
2366
*/
2383
2367
 
2384
 
static uint32_t get_table_open_method(TableList *tables,
 
2368
static uint get_table_open_method(TableList *tables,
2385
2369
                                  ST_SCHEMA_TABLE *schema_table,
2386
 
                                  enum enum_schema_tables)
 
2370
                                  enum enum_schema_tables schema_table_idx __attribute__((unused)))
2387
2371
{
2388
2372
  /*
2389
2373
    determine which method will be used for table opening
2408
2392
/**
2409
2393
  @brief          Fill I_S table with data from FRM file only
2410
2394
 
2411
 
  @param[in]      session                      thread handler
 
2395
  @param[in]      thd                      thread handler
2412
2396
  @param[in]      table                    Table struct for I_S table
2413
2397
  @param[in]      schema_table             I_S table struct
2414
2398
  @param[in]      db_name                  database name
2422
2406
                              open_tables function for this table
2423
2407
*/
2424
2408
 
2425
 
static int fill_schema_table_from_frm(Session *session,TableList *tables,
 
2409
static int fill_schema_table_from_frm(THD *thd,TableList *tables,
2426
2410
                                      ST_SCHEMA_TABLE *schema_table,
2427
2411
                                      LEX_STRING *db_name,
2428
2412
                                      LEX_STRING *table_name,
2429
 
                                      enum enum_schema_tables)
 
2413
                                      enum enum_schema_tables schema_table_idx __attribute__((unused)))
2430
2414
{
2431
2415
  Table *table= tables->table;
2432
2416
  TABLE_SHARE *share;
2433
2417
  Table tbl;
2434
2418
  TableList table_list;
2435
 
  uint32_t res= 0;
 
2419
  uint res= 0;
2436
2420
  int error;
2437
2421
  char key[MAX_DBKEY_LENGTH];
2438
 
  uint32_t key_length;
 
2422
  uint key_length;
2439
2423
 
2440
2424
  memset(&table_list, 0, sizeof(TableList));
2441
2425
  memset(&tbl, 0, sizeof(Table));
2443
2427
  table_list.table_name= table_name->str;
2444
2428
  table_list.db= db_name->str;
2445
2429
 
2446
 
  key_length= create_table_def_key(session, key, &table_list, 0);
 
2430
  key_length= create_table_def_key(thd, key, &table_list, 0);
2447
2431
  pthread_mutex_lock(&LOCK_open);
2448
 
  share= get_table_share(session, &table_list, key,
 
2432
  share= get_table_share(thd, &table_list, key,
2449
2433
                         key_length, 0, &error);
2450
2434
  if (!share)
2451
2435
  {
2456
2440
  {
2457
2441
    tbl.s= share;
2458
2442
    table_list.table= &tbl;
2459
 
    res= schema_table->process_table(session, &table_list, table,
 
2443
    res= schema_table->process_table(thd, &table_list, table,
2460
2444
                                     res, db_name, table_name);
2461
2445
  }
2462
2446
 
2464
2448
 
2465
2449
err:
2466
2450
  pthread_mutex_unlock(&LOCK_open);
2467
 
  session->clear_error();
 
2451
  thd->clear_error();
2468
2452
  return res;
2469
2453
}
2470
2454
 
2480
2464
                  from frm files and storage engine are filled by the function
2481
2465
                  get_all_tables().
2482
2466
 
2483
 
  @param[in]      session                      thread handler
 
2467
  @param[in]      thd                      thread handler
2484
2468
  @param[in]      tables                   I_S table
2485
2469
  @param[in]      cond                     'WHERE' condition
2486
2470
 
2489
2473
    @retval       1                        error
2490
2474
*/
2491
2475
 
2492
 
int get_all_tables(Session *session, TableList *tables, COND *cond)
 
2476
int get_all_tables(THD *thd, TableList *tables, COND *cond)
2493
2477
{
2494
 
  LEX *lex= session->lex;
 
2478
  LEX *lex= thd->lex;
2495
2479
  Table *table= tables->table;
2496
2480
  SELECT_LEX *old_all_select_lex= lex->all_selects_list;
2497
2481
  enum_sql_command save_sql_command= lex->sql_command;
2505
2489
  List<LEX_STRING> db_names;
2506
2490
  List_iterator_fast<LEX_STRING> it(db_names);
2507
2491
  COND *partial_cond= 0;
2508
 
  uint32_t derived_tables= lex->derived_tables; 
 
2492
  uint derived_tables= lex->derived_tables; 
2509
2493
  int error= 1;
2510
2494
  Open_tables_state open_tables_state_backup;
2511
2495
  Query_tables_list query_tables_list_backup;
2512
 
  uint32_t table_open_method;
2513
 
  bool old_value= session->no_warnings_for_error;
 
2496
  uint table_open_method;
 
2497
  bool old_value= thd->no_warnings_for_error;
2514
2498
 
2515
2499
  lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
2516
2500
 
2519
2503
    tables open and locked, since we won't lock tables which we will
2520
2504
    open and will ignore possible name-locks for these tables.
2521
2505
  */
2522
 
  session->reset_n_backup_open_tables_state(&open_tables_state_backup);
 
2506
  thd->reset_n_backup_open_tables_state(&open_tables_state_backup);
2523
2507
 
2524
2508
  schema_table_idx= get_schema_table_idx(schema_table);
2525
2509
  tables->table_open_method= table_open_method=
2531
2515
  */
2532
2516
  if (lsel && lsel->table_list.first)
2533
2517
  {
2534
 
    error= fill_schema_show_cols_or_idxs(session, tables, schema_table,
 
2518
    error= fill_schema_show_cols_or_idxs(thd, tables, schema_table,
2535
2519
                                         &open_tables_state_backup);
2536
2520
    goto err;
2537
2521
  }
2538
2522
 
2539
 
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
 
2523
  if (get_lookup_field_values(thd, cond, tables, &lookup_field_vals))
2540
2524
  {
2541
2525
    error= 0;
2542
2526
    goto err;
2575
2559
    goto err;
2576
2560
  }
2577
2561
 
2578
 
  if (make_db_list(session, &db_names, &lookup_field_vals, &with_i_schema))
 
2562
  if (make_db_list(thd, &db_names, &lookup_field_vals, &with_i_schema))
2579
2563
    goto err;
2580
2564
  it.rewind(); /* To get access to new elements in basis list */
2581
2565
  while ((db_name= it++))
2582
2566
  {
2583
2567
    {
2584
 
      session->no_warnings_for_error= 1;
 
2568
      thd->no_warnings_for_error= 1;
2585
2569
      List<LEX_STRING> table_names;
2586
 
      int res= make_table_name_list(session, &table_names, lex,
 
2570
      int res= make_table_name_list(thd, &table_names, lex,
2587
2571
                                    &lookup_field_vals,
2588
2572
                                    with_i_schema, db_name);
2589
2573
      if (res == 2)   /* Not fatal error, continue */
2612
2596
              (!lookup_field_vals.table_value.length ||
2613
2597
               lookup_field_vals.wild_table_value))
2614
2598
          {
2615
 
            if (schema_table_store_record(session, table))
 
2599
            if (schema_table_store_record(thd, table))
2616
2600
              goto err;      /* Out of space in temporary table */
2617
2601
            continue;
2618
2602
          }
2620
2604
          /* SHOW Table NAMES command */
2621
2605
          if (schema_table_idx == SCH_TABLE_NAMES)
2622
2606
          {
2623
 
            if (fill_schema_table_names(session, tables->table, db_name,
 
2607
            if (fill_schema_table_names(thd, tables->table, db_name,
2624
2608
                                        table_name, with_i_schema))
2625
2609
              continue;
2626
2610
          }
2629
2613
            if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2630
2614
                !with_i_schema)
2631
2615
            {
2632
 
              if (!fill_schema_table_from_frm(session, tables, schema_table, db_name,
 
2616
              if (!fill_schema_table_from_frm(thd, tables, schema_table, db_name,
2633
2617
                                              table_name, schema_table_idx))
2634
2618
                continue;
2635
2619
            }
2640
2624
              Set the parent lex of 'sel' because it is needed by
2641
2625
              sel.init_query() which is called inside make_table_list.
2642
2626
            */
2643
 
            session->no_warnings_for_error= 1;
 
2627
            thd->no_warnings_for_error= 1;
2644
2628
            sel.parent_lex= lex;
2645
2629
            /* db_name can be changed in make_table_list() func */
2646
 
            if (!session->make_lex_string(&orig_db_name, db_name->str,
 
2630
            if (!thd->make_lex_string(&orig_db_name, db_name->str,
2647
2631
                                      db_name->length, false))
2648
2632
              goto err;
2649
 
            if (make_table_list(session, &sel, db_name, table_name))
 
2633
            if (make_table_list(thd, &sel, db_name, table_name))
2650
2634
              goto err;
2651
2635
            TableList *show_table_list= (TableList*) sel.table_list.first;
2652
2636
            lex->all_selects_list= &sel;
2654
2638
            lex->sql_command= SQLCOM_SHOW_FIELDS;
2655
2639
            show_table_list->i_s_requested_object=
2656
2640
              schema_table->i_s_requested_object;
2657
 
            res= open_normal_and_derived_tables(session, show_table_list,
 
2641
            res= open_normal_and_derived_tables(thd, show_table_list,
2658
2642
                                                DRIZZLE_LOCK_IGNORE_FLUSH);
2659
2643
            lex->sql_command= save_sql_command;
2660
2644
            /*
2661
2645
              XXX:  show_table_list has a flag i_is_requested,
2662
2646
              and when it's set, open_normal_and_derived_tables()
2663
2647
              can return an error without setting an error message
2664
 
              in Session, which is a hack. This is why we have to
2665
 
              check for res, then for session->is_error() only then
2666
 
              for session->main_da.sql_errno().
 
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().
2667
2651
            */
2668
 
            if (res && session->is_error() &&
2669
 
                session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
 
2652
            if (res && thd->is_error() &&
 
2653
                thd->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2670
2654
            {
2671
2655
              /*
2672
2656
                Hide error for not existing table.
2675
2659
                table does not exist.
2676
2660
              */
2677
2661
              res= 0;
2678
 
              session->clear_error();
 
2662
              thd->clear_error();
2679
2663
            }
2680
2664
            else
2681
2665
            {
2686
2670
                to use alias because alias contains original table name 
2687
2671
                in this case.
2688
2672
              */
2689
 
              session->make_lex_string(&tmp_lex_string, show_table_list->alias,
 
2673
              thd->make_lex_string(&tmp_lex_string, show_table_list->alias,
2690
2674
                                   strlen(show_table_list->alias), false);
2691
 
              res= schema_table->process_table(session, show_table_list, table,
 
2675
              res= schema_table->process_table(thd, show_table_list, table,
2692
2676
                                               res, &orig_db_name,
2693
2677
                                               &tmp_lex_string);
2694
 
              close_tables_for_reopen(session, &show_table_list);
 
2678
              close_tables_for_reopen(thd, &show_table_list);
2695
2679
            }
2696
2680
            assert(!lex->query_tables_own_last);
2697
2681
            if (res)
2709
2693
 
2710
2694
  error= 0;
2711
2695
err:
2712
 
  session->restore_backup_open_tables_state(&open_tables_state_backup);
 
2696
  thd->restore_backup_open_tables_state(&open_tables_state_backup);
2713
2697
  lex->restore_backup_query_tables_list(&query_tables_list_backup);
2714
2698
  lex->derived_tables= derived_tables;
2715
2699
  lex->all_selects_list= old_all_select_lex;
2716
2700
  lex->sql_command= save_sql_command;
2717
 
  session->no_warnings_for_error= old_value;
 
2701
  thd->no_warnings_for_error= old_value;
2718
2702
  return(error);
2719
2703
}
2720
2704
 
2721
2705
 
2722
 
bool store_schema_shemata(Session* session, Table *table, LEX_STRING *db_name,
 
2706
bool store_schema_shemata(THD* thd, Table *table, LEX_STRING *db_name,
2723
2707
                          const CHARSET_INFO * const cs)
2724
2708
{
2725
2709
  restore_record(table, s->default_values);
2726
2710
  table->field[1]->store(db_name->str, db_name->length, system_charset_info);
2727
2711
  table->field[2]->store(cs->csname, strlen(cs->csname), system_charset_info);
2728
2712
  table->field[3]->store(cs->name, strlen(cs->name), system_charset_info);
2729
 
  return schema_table_store_record(session, table);
 
2713
  return schema_table_store_record(thd, table);
2730
2714
}
2731
2715
 
2732
2716
 
2733
 
int fill_schema_schemata(Session *session, TableList *tables, COND *cond)
 
2717
int fill_schema_schemata(THD *thd, TableList *tables, COND *cond)
2734
2718
{
2735
2719
  /*
2736
2720
    TODO: fill_schema_shemata() is called when new client is connected.
2744
2728
  HA_CREATE_INFO create;
2745
2729
  Table *table= tables->table;
2746
2730
 
2747
 
  if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
 
2731
  if (get_lookup_field_values(thd, cond, tables, &lookup_field_vals))
2748
2732
    return(0);
2749
 
  if (make_db_list(session, &db_names, &lookup_field_vals,
 
2733
  if (make_db_list(thd, &db_names, &lookup_field_vals,
2750
2734
                   &with_i_schema))
2751
2735
    return(1);
2752
2736
 
2757
2741
     !with_i_schema)
2758
2742
  {
2759
2743
    char path[FN_REFLEN+16];
2760
 
    uint32_t path_len;
 
2744
    uint path_len;
2761
2745
    struct stat stat_info;
2762
2746
    if (!lookup_field_vals.db_value.str[0])
2763
2747
      return(0);
2773
2757
  {
2774
2758
    if (with_i_schema)       // information schema name is always first in list
2775
2759
    {
2776
 
      if (store_schema_shemata(session, table, db_name,
 
2760
      if (store_schema_shemata(thd, table, db_name,
2777
2761
                               system_charset_info))
2778
2762
        return(1);
2779
2763
      with_i_schema= 0;
2780
2764
      continue;
2781
2765
    }
2782
2766
    {
2783
 
      load_db_opt_by_name(session, db_name->str, &create);
2784
 
      if (store_schema_shemata(session, table, db_name,
 
2767
      load_db_opt_by_name(thd, db_name->str, &create);
 
2768
      if (store_schema_shemata(thd, table, db_name,
2785
2769
                               create.default_table_charset))
2786
2770
        return(1);
2787
2771
    }
2790
2774
}
2791
2775
 
2792
2776
 
2793
 
static int get_schema_tables_record(Session *session, TableList *tables,
 
2777
static int get_schema_tables_record(THD *thd, TableList *tables,
2794
2778
                                    Table *table, bool res,
2795
2779
                                    LEX_STRING *db_name,
2796
2780
                                    LEX_STRING *table_name)
2807
2791
    /*
2808
2792
      there was errors during opening tables
2809
2793
    */
2810
 
    const char *error= session->is_error() ? session->main_da.message() : "";
 
2794
    const char *error= thd->is_error() ? thd->main_da.message() : "";
2811
2795
    if (tables->schema_table)
2812
2796
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
2813
2797
    else
2814
2798
      table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
2815
2799
    table->field[20]->store(error, strlen(error), cs);
2816
 
    session->clear_error();
 
2800
    thd->clear_error();
2817
2801
  }
2818
2802
  else
2819
2803
  {
2842
2826
    ptr=option_buff;
2843
2827
    if (share->min_rows)
2844
2828
    {
2845
 
      ptr=my_stpcpy(ptr," min_rows=");
 
2829
      ptr=stpcpy(ptr," min_rows=");
2846
2830
      ptr=int64_t10_to_str(share->min_rows,ptr,10);
2847
2831
    }
2848
2832
    if (share->max_rows)
2849
2833
    {
2850
 
      ptr=my_stpcpy(ptr," max_rows=");
 
2834
      ptr=stpcpy(ptr," max_rows=");
2851
2835
      ptr=int64_t10_to_str(share->max_rows,ptr,10);
2852
2836
    }
2853
2837
    if (share->avg_row_length)
2854
2838
    {
2855
 
      ptr=my_stpcpy(ptr," avg_row_length=");
 
2839
      ptr=stpcpy(ptr," avg_row_length=");
2856
2840
      ptr=int64_t10_to_str(share->avg_row_length,ptr,10);
2857
2841
    }
2858
2842
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
2859
 
      ptr=my_stpcpy(ptr," pack_keys=1");
 
2843
      ptr=stpcpy(ptr," pack_keys=1");
2860
2844
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
2861
 
      ptr=my_stpcpy(ptr," pack_keys=0");
 
2845
      ptr=stpcpy(ptr," pack_keys=0");
2862
2846
    /* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */
2863
2847
    if (share->db_create_options & HA_OPTION_CHECKSUM)
2864
 
      ptr=my_stpcpy(ptr," checksum=1");
 
2848
      ptr=stpcpy(ptr," checksum=1");
2865
2849
    if (share->page_checksum != HA_CHOICE_UNDEF)
2866
2850
      ptr= strxmov(ptr, " page_checksum=",
2867
 
                   ha_choice_values[(uint) share->page_checksum], NULL);
 
2851
                   ha_choice_values[(uint) share->page_checksum], NullS);
2868
2852
    if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
2869
 
      ptr=my_stpcpy(ptr," delay_key_write=1");
 
2853
      ptr=stpcpy(ptr," delay_key_write=1");
2870
2854
    if (share->row_type != ROW_TYPE_DEFAULT)
2871
2855
      ptr=strxmov(ptr, " row_format=", 
2872
2856
                  ha_row_type[(uint) share->row_type],
2873
 
                  NULL);
 
2857
                  NullS);
2874
2858
    if (share->block_size)
2875
2859
    {
2876
 
      ptr= my_stpcpy(ptr, " block_size=");
 
2860
      ptr= stpcpy(ptr, " block_size=");
2877
2861
      ptr= int64_t10_to_str(share->block_size, ptr, 10);
2878
2862
    }
2879
2863
    
2881
2865
    {
2882
2866
      ptr= strxmov(ptr, " TRANSACTIONAL=",
2883
2867
                   (share->transactional == HA_CHOICE_YES ? "1" : "0"),
2884
 
                   NULL);
 
2868
                   NullS);
2885
2869
    }
2886
2870
    if (share->transactional != HA_CHOICE_UNDEF)
2887
2871
      ptr= strxmov(ptr, " transactional=",
2888
 
                   ha_choice_values[(uint) share->transactional], NULL);
 
2872
                   ha_choice_values[(uint) share->transactional], NullS);
2889
2873
    table->field[19]->store(option_buff+1,
2890
2874
                            (ptr == option_buff ? 0 : 
2891
2875
                             (uint) (ptr-option_buff)-1), cs);
2952
2936
      }
2953
2937
      if (file->stats.create_time)
2954
2938
      {
2955
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2939
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2956
2940
                                                  (my_time_t) file->stats.create_time);
2957
2941
        table->field[14]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2958
2942
        table->field[14]->set_notnull();
2959
2943
      }
2960
2944
      if (file->stats.update_time)
2961
2945
      {
2962
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2946
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2963
2947
                                                  (my_time_t) file->stats.update_time);
2964
2948
        table->field[15]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2965
2949
        table->field[15]->set_notnull();
2966
2950
      }
2967
2951
      if (file->stats.check_time)
2968
2952
      {
2969
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
 
2953
        thd->variables.time_zone->gmt_sec_to_TIME(&time,
2970
2954
                                                  (my_time_t) file->stats.check_time);
2971
2955
        table->field[16]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
2972
2956
        table->field[16]->set_notnull();
2978
2962
      }
2979
2963
    }
2980
2964
  }
2981
 
  return(schema_table_store_record(session, table));
 
2965
  return(schema_table_store_record(thd, table));
2982
2966
}
2983
2967
 
2984
2968
 
2995
2979
*/
2996
2980
 
2997
2981
void store_column_type(Table *table, Field *field, const CHARSET_INFO * const cs,
2998
 
                       uint32_t offset)
 
2982
                       uint offset)
2999
2983
{
3000
2984
  bool is_blob;
3001
2985
  int decimals, field_length;
3040
3024
  case DRIZZLE_TYPE_NEWDECIMAL:
3041
3025
    field_length= ((Field_new_decimal*) field)->precision;
3042
3026
    break;
 
3027
  case DRIZZLE_TYPE_TINY:
 
3028
  case DRIZZLE_TYPE_SHORT:
3043
3029
  case DRIZZLE_TYPE_LONG:
3044
3030
  case DRIZZLE_TYPE_LONGLONG:
3045
3031
    field_length= field->max_display_length() - 1;
3080
3066
}
3081
3067
 
3082
3068
 
3083
 
static int get_schema_column_record(Session *session, TableList *tables,
 
3069
static int get_schema_column_record(THD *thd, TableList *tables,
3084
3070
                                    Table *table, bool res,
3085
3071
                                    LEX_STRING *db_name,
3086
3072
                                    LEX_STRING *table_name)
3087
3073
{
3088
 
  LEX *lex= session->lex;
3089
 
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
 
3074
  LEX *lex= thd->lex;
 
3075
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
3090
3076
  const CHARSET_INFO * const cs= system_charset_info;
3091
3077
  Table *show_table;
3092
3078
  TABLE_SHARE *show_table_share;
3101
3087
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
3102
3088
        rather than in SHOW COLUMNS
3103
3089
      */ 
3104
 
      if (session->is_error())
3105
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3106
 
                     session->main_da.sql_errno(), session->main_da.message());
3107
 
      session->clear_error();
 
3090
      if (thd->is_error())
 
3091
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3092
                     thd->main_da.sql_errno(), thd->main_da.message());
 
3093
      thd->clear_error();
3108
3094
      res= 0;
3109
3095
    }
3110
3096
    return(res);
3131
3117
    if (!show_table->read_set)
3132
3118
    {
3133
3119
      /* to satisfy 'field->val_str' ASSERTs */
3134
 
      unsigned char *bitmaps;
3135
 
      uint32_t bitmap_size= show_table_share->column_bitmap_size;
3136
 
      if (!(bitmaps= (unsigned char*) alloc_root(session->mem_root, bitmap_size)))
 
3120
      uchar *bitmaps;
 
3121
      uint bitmap_size= show_table_share->column_bitmap_size;
 
3122
      if (!(bitmaps= (uchar*) alloc_root(thd->mem_root, bitmap_size)))
3137
3123
        return(0);
3138
3124
      bitmap_init(&show_table->def_read_set,
3139
3125
                  (my_bitmap_map*) bitmaps, show_table_share->fields, false);
3145
3131
 
3146
3132
  for (; (field= *ptr) ; ptr++)
3147
3133
  {
3148
 
    unsigned char *pos;
 
3134
    uchar *pos;
3149
3135
    char tmp[MAX_FIELD_WIDTH];
3150
3136
    String type(tmp,sizeof(tmp), system_charset_info);
3151
3137
    char *end;
3152
3138
 
3153
3139
    /* to satisfy 'field->val_str' ASSERTs */
3154
3140
    field->table= show_table;
3155
 
    show_table->in_use= session;
 
3141
    show_table->in_use= thd;
3156
3142
 
3157
3143
    if (wild && wild[0] &&
3158
3144
        wild_case_compare(system_charset_info, field->field_name,wild))
3168
3154
                           cs);
3169
3155
    table->field[4]->store((int64_t) count, true);
3170
3156
 
3171
 
    if (get_field_default_value(session, timestamp_field, field, &type, 0))
 
3157
    if (get_field_default_value(thd, timestamp_field, field, &type, 0))
3172
3158
    {
3173
3159
      table->field[5]->store(type.ptr(), type.length(), cs);
3174
3160
      table->field[5]->set_notnull();
3175
3161
    }
3176
 
    pos=(unsigned char*) ((field->flags & NOT_NULL_FLAG) ?  "NO" : "YES");
 
3162
    pos=(uchar*) ((field->flags & NOT_NULL_FLAG) ?  "NO" : "YES");
3177
3163
    table->field[6]->store((const char*) pos,
3178
3164
                           strlen((const char*) pos), cs);
3179
3165
    store_column_type(table, field, cs, 7);
3180
3166
 
3181
 
    pos=(unsigned char*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
 
3167
    pos=(uchar*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
3182
3168
                 (field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
3183
3169
                 (field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
3184
3170
    table->field[15]->store((const char*) pos,
3191
3177
        field->unireg_check != Field::TIMESTAMP_DN_FIELD)
3192
3178
      table->field[16]->store(STRING_WITH_LEN("on update CURRENT_TIMESTAMP"),
3193
3179
                              cs);
3194
 
    if (field->vcol_info)
3195
 
          table->field[16]->store(STRING_WITH_LEN("VIRTUAL"), cs);
 
3180
 
3196
3181
    table->field[18]->store(field->comment.str, field->comment.length, cs);
3197
3182
    {
3198
3183
      enum column_format_type column_format= (enum column_format_type)
3199
3184
        ((field->flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
3200
 
      pos=(unsigned char*)"Default";
 
3185
      pos=(uchar*)"Default";
3201
3186
      table->field[19]->store((const char*) pos,
3202
3187
                              strlen((const char*) pos), cs);
3203
 
      pos=(unsigned char*)(column_format == COLUMN_FORMAT_TYPE_DEFAULT ? "Default" :
 
3188
      pos=(uchar*)(column_format == COLUMN_FORMAT_TYPE_DEFAULT ? "Default" :
3204
3189
                   column_format == COLUMN_FORMAT_TYPE_FIXED ? "Fixed" :
3205
3190
                                                             "Dynamic");
3206
3191
      table->field[20]->store((const char*) pos,
3207
3192
                              strlen((const char*) pos), cs);
3208
3193
    }
3209
 
    if (schema_table_store_record(session, table))
 
3194
    if (schema_table_store_record(thd, table))
3210
3195
      return(1);
3211
3196
  }
3212
3197
  return(0);
3214
3199
 
3215
3200
 
3216
3201
 
3217
 
int fill_schema_charsets(Session *session, TableList *tables, COND *)
 
3202
int fill_schema_charsets(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3218
3203
{
3219
3204
  CHARSET_INFO **cs;
3220
 
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
 
3205
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
3221
3206
  Table *table= tables->table;
3222
3207
  const CHARSET_INFO * const scs= system_charset_info;
3223
3208
 
3237
3222
      comment= tmp_cs->comment ? tmp_cs->comment : "";
3238
3223
      table->field[2]->store(comment, strlen(comment), scs);
3239
3224
      table->field[3]->store((int64_t) tmp_cs->mbmaxlen, true);
3240
 
      if (schema_table_store_record(session, table))
 
3225
      if (schema_table_store_record(thd, table))
3241
3226
        return 1;
3242
3227
    }
3243
3228
  }
3245
3230
}
3246
3231
 
3247
3232
 
3248
 
int fill_schema_collation(Session *session, TableList *tables, COND *)
 
3233
int fill_schema_collation(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3249
3234
{
3250
3235
  CHARSET_INFO **cs;
3251
 
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
 
3236
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
3252
3237
  Table *table= tables->table;
3253
3238
  const CHARSET_INFO * const scs= system_charset_info;
3254
3239
  for (cs= all_charsets ; cs < all_charsets+255 ; cs++ )
3278
3263
        tmp_buff= (tmp_cl->state & MY_CS_COMPILED)? "Yes" : "";
3279
3264
        table->field[4]->store(tmp_buff, strlen(tmp_buff), scs);
3280
3265
        table->field[5]->store((int64_t) tmp_cl->strxfrm_multiply, true);
3281
 
        if (schema_table_store_record(session, table))
 
3266
        if (schema_table_store_record(thd, table))
3282
3267
          return 1;
3283
3268
      }
3284
3269
    }
3287
3272
}
3288
3273
 
3289
3274
 
3290
 
int fill_schema_coll_charset_app(Session *session, TableList *tables, COND *)
 
3275
int fill_schema_coll_charset_app(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3291
3276
{
3292
3277
  CHARSET_INFO **cs;
3293
3278
  Table *table= tables->table;
3308
3293
      restore_record(table, s->default_values);
3309
3294
      table->field[0]->store(tmp_cl->name, strlen(tmp_cl->name), scs);
3310
3295
      table->field[1]->store(tmp_cl->csname , strlen(tmp_cl->csname), scs);
3311
 
      if (schema_table_store_record(session, table))
 
3296
      if (schema_table_store_record(thd, table))
3312
3297
        return 1;
3313
3298
    }
3314
3299
  }
3316
3301
}
3317
3302
 
3318
3303
 
3319
 
static int get_schema_stat_record(Session *session, TableList *tables,
 
3304
static int get_schema_stat_record(THD *thd, TableList *tables,
3320
3305
                                  Table *table, bool res,
3321
3306
                                  LEX_STRING *db_name,
3322
3307
                                  LEX_STRING *table_name)
3324
3309
  const CHARSET_INFO * const cs= system_charset_info;
3325
3310
  if (res)
3326
3311
  {
3327
 
    if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
 
3312
    if (thd->lex->sql_command != SQLCOM_SHOW_KEYS)
3328
3313
    {
3329
3314
      /*
3330
3315
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
3331
3316
        rather than in SHOW KEYS
3332
3317
      */
3333
 
      if (session->is_error())
3334
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3335
 
                     session->main_da.sql_errno(), session->main_da.message());
3336
 
      session->clear_error();
 
3318
      if (thd->is_error())
 
3319
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3320
                     thd->main_da.sql_errno(), thd->main_da.message());
 
3321
      thd->clear_error();
3337
3322
      res= 0;
3338
3323
    }
3339
3324
    return(res);
3346
3331
      show_table->file->info(HA_STATUS_VARIABLE |
3347
3332
                             HA_STATUS_NO_LOCK |
3348
3333
                             HA_STATUS_TIME);
3349
 
    for (uint32_t i=0 ; i < show_table->s->keys ; i++,key_info++)
 
3334
    for (uint i=0 ; i < show_table->s->keys ; i++,key_info++)
3350
3335
    {
3351
3336
      KEY_PART_INFO *key_part= key_info->key_part;
3352
3337
      const char *str;
3353
 
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
3338
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
3354
3339
      {
3355
3340
        restore_record(table, s->default_values);
3356
3341
        table->field[1]->store(db_name->str, db_name->length, cs);
3391
3376
                                  key_part->field->charset()->mbmaxlen, true);
3392
3377
          table->field[10]->set_notnull();
3393
3378
        }
3394
 
        uint32_t flags= key_part->field ? key_part->field->flags : 0;
 
3379
        uint flags= key_part->field ? key_part->field->flags : 0;
3395
3380
        const char *pos=(char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
3396
3381
        table->field[12]->store(pos, strlen(pos), cs);
3397
3382
        if (!show_table->s->keys_in_use.is_set(i))
3404
3389
        if (key_info->flags & HA_USES_COMMENT)
3405
3390
          table->field[15]->store(key_info->comment.str, 
3406
3391
                                  key_info->comment.length, cs);
3407
 
        if (schema_table_store_record(session, table))
 
3392
        if (schema_table_store_record(thd, table))
3408
3393
          return(1);
3409
3394
      }
3410
3395
    }
3413
3398
}
3414
3399
 
3415
3400
 
3416
 
bool store_constraints(Session *session, Table *table, LEX_STRING *db_name,
 
3401
bool store_constraints(THD *thd, Table *table, LEX_STRING *db_name,
3417
3402
                       LEX_STRING *table_name, const char *key_name,
3418
 
                       uint32_t key_len, const char *con_type, uint32_t con_len)
 
3403
                       uint key_len, const char *con_type, uint con_len)
3419
3404
{
3420
3405
  const CHARSET_INFO * const cs= system_charset_info;
3421
3406
  restore_record(table, s->default_values);
3424
3409
  table->field[3]->store(db_name->str, db_name->length, cs);
3425
3410
  table->field[4]->store(table_name->str, table_name->length, cs);
3426
3411
  table->field[5]->store(con_type, con_len, cs);
3427
 
  return schema_table_store_record(session, table);
 
3412
  return schema_table_store_record(thd, table);
3428
3413
}
3429
3414
 
3430
3415
 
3431
 
static int get_schema_constraints_record(Session *session, TableList *tables,
 
3416
static int get_schema_constraints_record(THD *thd, TableList *tables,
3432
3417
                                         Table *table, bool res,
3433
3418
                                         LEX_STRING *db_name,
3434
3419
                                         LEX_STRING *table_name)
3435
3420
{
3436
3421
  if (res)
3437
3422
  {
3438
 
    if (session->is_error())
3439
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3440
 
                   session->main_da.sql_errno(), session->main_da.message());
3441
 
    session->clear_error();
 
3423
    if (thd->is_error())
 
3424
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3425
                   thd->main_da.sql_errno(), thd->main_da.message());
 
3426
    thd->clear_error();
3442
3427
    return(0);
3443
3428
  }
3444
3429
  else
3446
3431
    List<FOREIGN_KEY_INFO> f_key_list;
3447
3432
    Table *show_table= tables->table;
3448
3433
    KEY *key_info=show_table->key_info;
3449
 
    uint32_t primary_key= show_table->s->primary_key;
 
3434
    uint primary_key= show_table->s->primary_key;
3450
3435
    show_table->file->info(HA_STATUS_VARIABLE | 
3451
3436
                           HA_STATUS_NO_LOCK |
3452
3437
                           HA_STATUS_TIME);
3453
 
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
 
3438
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3454
3439
    {
3455
3440
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3456
3441
        continue;
3457
3442
 
3458
3443
      if (i == primary_key && !strcmp(key_info->name, primary_key_name))
3459
3444
      {
3460
 
        if (store_constraints(session, table, db_name, table_name, key_info->name,
 
3445
        if (store_constraints(thd, table, db_name, table_name, key_info->name,
3461
3446
                              strlen(key_info->name),
3462
3447
                              STRING_WITH_LEN("PRIMARY KEY")))
3463
3448
          return(1);
3464
3449
      }
3465
3450
      else if (key_info->flags & HA_NOSAME)
3466
3451
      {
3467
 
        if (store_constraints(session, table, db_name, table_name, key_info->name,
 
3452
        if (store_constraints(thd, table, db_name, table_name, key_info->name,
3468
3453
                              strlen(key_info->name),
3469
3454
                              STRING_WITH_LEN("UNIQUE")))
3470
3455
          return(1);
3471
3456
      }
3472
3457
    }
3473
3458
 
3474
 
    show_table->file->get_foreign_key_list(session, &f_key_list);
 
3459
    show_table->file->get_foreign_key_list(thd, &f_key_list);
3475
3460
    FOREIGN_KEY_INFO *f_key_info;
3476
3461
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3477
3462
    while ((f_key_info=it++))
3478
3463
    {
3479
 
      if (store_constraints(session, table, db_name, table_name, 
 
3464
      if (store_constraints(thd, table, db_name, table_name, 
3480
3465
                            f_key_info->forein_id->str,
3481
3466
                            strlen(f_key_info->forein_id->str),
3482
3467
                            "FOREIGN KEY", 11))
3489
3474
 
3490
3475
void store_key_column_usage(Table *table, LEX_STRING *db_name,
3491
3476
                            LEX_STRING *table_name, const char *key_name,
3492
 
                            uint32_t key_len, const char *con_type, uint32_t con_len,
 
3477
                            uint key_len, const char *con_type, uint con_len,
3493
3478
                            int64_t idx)
3494
3479
{
3495
3480
  const CHARSET_INFO * const cs= system_charset_info;
3502
3487
}
3503
3488
 
3504
3489
 
3505
 
static int get_schema_key_column_usage_record(Session *session,
 
3490
static int get_schema_key_column_usage_record(THD *thd,
3506
3491
                                              TableList *tables,
3507
3492
                                              Table *table, bool res,
3508
3493
                                              LEX_STRING *db_name,
3510
3495
{
3511
3496
  if (res)
3512
3497
  {
3513
 
    if (session->is_error())
3514
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3515
 
                   session->main_da.sql_errno(), session->main_da.message());
3516
 
    session->clear_error();
 
3498
    if (thd->is_error())
 
3499
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3500
                   thd->main_da.sql_errno(), thd->main_da.message());
 
3501
    thd->clear_error();
3517
3502
    return(0);
3518
3503
  }
3519
3504
  else
3521
3506
    List<FOREIGN_KEY_INFO> f_key_list;
3522
3507
    Table *show_table= tables->table;
3523
3508
    KEY *key_info=show_table->key_info;
3524
 
    uint32_t primary_key= show_table->s->primary_key;
 
3509
    uint primary_key= show_table->s->primary_key;
3525
3510
    show_table->file->info(HA_STATUS_VARIABLE | 
3526
3511
                           HA_STATUS_NO_LOCK |
3527
3512
                           HA_STATUS_TIME);
3528
 
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
 
3513
    for (uint i=0 ; i < show_table->s->keys ; i++, key_info++)
3529
3514
    {
3530
3515
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
3531
3516
        continue;
3532
 
      uint32_t f_idx= 0;
 
3517
      uint f_idx= 0;
3533
3518
      KEY_PART_INFO *key_part= key_info->key_part;
3534
 
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
3519
      for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
3535
3520
      {
3536
3521
        if (key_part->field)
3537
3522
        {
3543
3528
                                 key_part->field->field_name, 
3544
3529
                                 strlen(key_part->field->field_name),
3545
3530
                                 (int64_t) f_idx);
3546
 
          if (schema_table_store_record(session, table))
 
3531
          if (schema_table_store_record(thd, table))
3547
3532
            return(1);
3548
3533
        }
3549
3534
      }
3550
3535
    }
3551
3536
 
3552
 
    show_table->file->get_foreign_key_list(session, &f_key_list);
 
3537
    show_table->file->get_foreign_key_list(thd, &f_key_list);
3553
3538
    FOREIGN_KEY_INFO *f_key_info;
3554
3539
    List_iterator_fast<FOREIGN_KEY_INFO> fkey_it(f_key_list);
3555
3540
    while ((f_key_info= fkey_it++))
3558
3543
      LEX_STRING *r_info;
3559
3544
      List_iterator_fast<LEX_STRING> it(f_key_info->foreign_fields),
3560
3545
        it1(f_key_info->referenced_fields);
3561
 
      uint32_t f_idx= 0;
 
3546
      uint f_idx= 0;
3562
3547
      while ((f_info= it++))
3563
3548
      {
3564
3549
        r_info= it1++;
3582
3567
        table->field[11]->store(r_info->str, r_info->length,
3583
3568
                                system_charset_info);
3584
3569
        table->field[11]->set_notnull();
3585
 
        if (schema_table_store_record(session, table))
 
3570
        if (schema_table_store_record(thd, table))
3586
3571
          return(1);
3587
3572
      }
3588
3573
    }
3591
3576
}
3592
3577
 
3593
3578
 
3594
 
int fill_open_tables(Session *session, TableList *tables, COND *)
 
3579
int fill_open_tables(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3595
3580
{
3596
 
  const char *wild= session->lex->wild ? session->lex->wild->ptr() : NULL;
 
3581
  const char *wild= thd->lex->wild ? thd->lex->wild->ptr() : NullS;
3597
3582
  Table *table= tables->table;
3598
3583
  const CHARSET_INFO * const cs= system_charset_info;
3599
3584
  OPEN_TableList *open_list;
3600
 
  if (!(open_list=list_open_tables(session,session->lex->select_lex.db, wild))
3601
 
            && session->is_fatal_error)
 
3585
  if (!(open_list=list_open_tables(thd,thd->lex->select_lex.db, wild))
 
3586
            && thd->is_fatal_error)
3602
3587
    return(1);
3603
3588
 
3604
3589
  for (; open_list ; open_list=open_list->next)
3608
3593
    table->field[1]->store(open_list->table, strlen(open_list->table), cs);
3609
3594
    table->field[2]->store((int64_t) open_list->in_use, true);
3610
3595
    table->field[3]->store((int64_t) open_list->locked, true);
3611
 
    if (schema_table_store_record(session, table))
 
3596
    if (schema_table_store_record(thd, table))
3612
3597
      return(1);
3613
3598
  }
3614
3599
  return(0);
3615
3600
}
3616
3601
 
3617
3602
 
3618
 
int fill_variables(Session *session, TableList *tables, COND *)
 
3603
int fill_variables(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3619
3604
{
3620
3605
  int res= 0;
3621
 
  LEX *lex= session->lex;
3622
 
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
 
3606
  LEX *lex= thd->lex;
 
3607
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
3623
3608
  enum enum_schema_tables schema_table_idx=
3624
3609
    get_schema_table_idx(tables->schema_table);
3625
3610
  enum enum_var_type option_type= OPT_SESSION;
3631
3616
    option_type= OPT_GLOBAL;
3632
3617
 
3633
3618
  rw_rdlock(&LOCK_system_variables_hash);
3634
 
  res= show_status_array(session, wild, enumerate_sys_vars(session, sorted_vars),
 
3619
  res= show_status_array(thd, wild, enumerate_sys_vars(thd, sorted_vars),
3635
3620
                         option_type, NULL, "", tables->table, upper_case_names);
3636
3621
  rw_unlock(&LOCK_system_variables_hash);
3637
3622
  return(res);
3638
3623
}
3639
3624
 
3640
3625
 
3641
 
int fill_status(Session *session, TableList *tables, COND *)
 
3626
int fill_status(THD *thd, TableList *tables, COND *cond __attribute__((unused)))
3642
3627
{
3643
 
  LEX *lex= session->lex;
3644
 
  const char *wild= lex->wild ? lex->wild->ptr() : NULL;
 
3628
  LEX *lex= thd->lex;
 
3629
  const char *wild= lex->wild ? lex->wild->ptr() : NullS;
3645
3630
  int res= 0;
3646
3631
  STATUS_VAR *tmp1, tmp;
3647
3632
  enum enum_schema_tables schema_table_idx=
3655
3640
    if (option_type == OPT_GLOBAL)
3656
3641
      tmp1= &tmp;
3657
3642
    else
3658
 
      tmp1= session->initial_status_var;
 
3643
      tmp1= thd->initial_status_var;
3659
3644
  }
3660
3645
  else if (schema_table_idx == SCH_GLOBAL_STATUS)
3661
3646
  {
3665
3650
  else
3666
3651
  { 
3667
3652
    option_type= OPT_SESSION;
3668
 
    tmp1= &session->status_var;
 
3653
    tmp1= &thd->status_var;
3669
3654
  }
3670
3655
 
3671
3656
  pthread_mutex_lock(&LOCK_status);
3672
3657
  if (option_type == OPT_GLOBAL)
3673
3658
    calc_sum_of_all_status(&tmp);
3674
 
  res= show_status_array(session, wild,
 
3659
  res= show_status_array(thd, wild,
3675
3660
                         (SHOW_VAR *)all_status_vars.buffer,
3676
3661
                         option_type, tmp1, "", tables->table,
3677
3662
                         upper_case_names);
3685
3670
 
3686
3671
  SYNOPSIS
3687
3672
    get_referential_constraints_record()
3688
 
    session                 thread handle
 
3673
    thd                 thread handle
3689
3674
    tables              table list struct(processed table)
3690
3675
    table               I_S table
3691
3676
    res                 1 means the error during opening of the processed table
3699
3684
*/
3700
3685
 
3701
3686
static int
3702
 
get_referential_constraints_record(Session *session, TableList *tables,
 
3687
get_referential_constraints_record(THD *thd, TableList *tables,
3703
3688
                                   Table *table, bool res,
3704
3689
                                   LEX_STRING *db_name, LEX_STRING *table_name)
3705
3690
{
3707
3692
 
3708
3693
  if (res)
3709
3694
  {
3710
 
    if (session->is_error())
3711
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3712
 
                   session->main_da.sql_errno(), session->main_da.message());
3713
 
    session->clear_error();
 
3695
    if (thd->is_error())
 
3696
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3697
                   thd->main_da.sql_errno(), thd->main_da.message());
 
3698
    thd->clear_error();
3714
3699
    return(0);
3715
3700
  }
3716
3701
 
3721
3706
                           HA_STATUS_NO_LOCK |
3722
3707
                           HA_STATUS_TIME);
3723
3708
 
3724
 
    show_table->file->get_foreign_key_list(session, &f_key_list);
 
3709
    show_table->file->get_foreign_key_list(thd, &f_key_list);
3725
3710
    FOREIGN_KEY_INFO *f_key_info;
3726
3711
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
3727
3712
    while ((f_key_info= it++))
3748
3733
                             f_key_info->update_method->length, cs);
3749
3734
      table->field[8]->store(f_key_info->delete_method->str, 
3750
3735
                             f_key_info->delete_method->length, cs);
3751
 
      if (schema_table_store_record(session, table))
 
3736
      if (schema_table_store_record(thd, table))
3752
3737
        return(1);
3753
3738
    }
3754
3739
  }
3768
3753
 
3769
3754
  SYNOPSIS
3770
3755
    find_schema_table_in_plugin()
3771
 
    session                 thread handler
 
3756
    thd                 thread handler
3772
3757
    plugin              plugin
3773
3758
    table_name          table name
3774
3759
 
3776
3761
    0   table not found
3777
3762
    1   found the schema table
3778
3763
*/
3779
 
static bool find_schema_table_in_plugin(Session *, plugin_ref plugin,
3780
 
                                        void* p_table)
 
3764
static bool find_schema_table_in_plugin(THD *thd __attribute__((unused)),
 
3765
                                           plugin_ref plugin,
 
3766
                                           void* p_table)
3781
3767
{
3782
3768
  schema_table_ref *p_schema_table= (schema_table_ref *)p_table;
3783
3769
  const char* table_name= p_schema_table->table_name;
3799
3785
 
3800
3786
  SYNOPSIS
3801
3787
    find_schema_table()
3802
 
    session                 thread handler
 
3788
    thd                 thread handler
3803
3789
    table_name          table name
3804
3790
 
3805
3791
  RETURN
3807
3793
    #   pointer to 'schema_tables' element
3808
3794
*/
3809
3795
 
3810
 
ST_SCHEMA_TABLE *find_schema_table(Session *session, const char* table_name)
 
3796
ST_SCHEMA_TABLE *find_schema_table(THD *thd, const char* table_name)
3811
3797
{
3812
3798
  schema_table_ref schema_table_a;
3813
3799
  ST_SCHEMA_TABLE *schema_table= schema_tables;
3821
3807
  }
3822
3808
 
3823
3809
  schema_table_a.table_name= table_name;
3824
 
  if (plugin_foreach(session, find_schema_table_in_plugin, 
 
3810
  if (plugin_foreach(thd, find_schema_table_in_plugin, 
3825
3811
                     DRIZZLE_INFORMATION_SCHEMA_PLUGIN, &schema_table_a))
3826
3812
    return(schema_table_a.schema_table);
3827
3813
 
3841
3827
  @note
3842
3828
 
3843
3829
  @param
3844
 
    session                       thread handler
 
3830
    thd                   thread handler
3845
3831
 
3846
3832
  @param table_list Used to pass I_S table information(fields info, tables
3847
3833
  parameters etc) and table name.
3850
3836
  @retval  NULL           Can't create table
3851
3837
*/
3852
3838
 
3853
 
Table *create_schema_table(Session *session, TableList *table_list)
 
3839
Table *create_schema_table(THD *thd, TableList *table_list)
3854
3840
{
3855
3841
  int field_count= 0;
3856
3842
  Item *item;
3863
3849
  for (; fields_info->field_name; fields_info++)
3864
3850
  {
3865
3851
    switch (fields_info->field_type) {
 
3852
    case DRIZZLE_TYPE_TINY:
3866
3853
    case DRIZZLE_TYPE_LONG:
 
3854
    case DRIZZLE_TYPE_SHORT:
3867
3855
    case DRIZZLE_TYPE_LONGLONG:
3868
3856
      if (!(item= new Item_return_int(fields_info->field_name,
3869
3857
                                      fields_info->field_length,
3925
3913
    field_count++;
3926
3914
  }
3927
3915
  TMP_TABLE_PARAM *tmp_table_param =
3928
 
    (TMP_TABLE_PARAM*) (session->alloc(sizeof(TMP_TABLE_PARAM)));
 
3916
    (TMP_TABLE_PARAM*) (thd->alloc(sizeof(TMP_TABLE_PARAM)));
3929
3917
  tmp_table_param->init();
3930
3918
  tmp_table_param->table_charset= cs;
3931
3919
  tmp_table_param->field_count= field_count;
3932
3920
  tmp_table_param->schema_table= 1;
3933
 
  SELECT_LEX *select_lex= session->lex->current_select;
3934
 
  if (!(table= create_tmp_table(session, tmp_table_param,
 
3921
  SELECT_LEX *select_lex= thd->lex->current_select;
 
3922
  if (!(table= create_tmp_table(thd, tmp_table_param,
3935
3923
                                field_list, (order_st*) 0, 0, 0, 
3936
 
                                (select_lex->options | session->options |
 
3924
                                (select_lex->options | thd->options |
3937
3925
                                 TMP_TABLE_ALL_COLUMNS),
3938
3926
                                HA_POS_ERROR, table_list->alias)))
3939
3927
    return(0);
3940
3928
  my_bitmap_map* bitmaps=
3941
 
    (my_bitmap_map*) session->alloc(bitmap_buffer_size(field_count));
 
3929
    (my_bitmap_map*) thd->alloc(bitmap_buffer_size(field_count));
3942
3930
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
3943
3931
              false);
3944
3932
  table->read_set= &table->def_read_set;
3955
3943
 
3956
3944
  SYNOPSIS
3957
3945
    make_old_format()
3958
 
    session                     thread handler
 
3946
    thd                 thread handler
3959
3947
    schema_table        pointer to 'schema_tables' element
3960
3948
 
3961
3949
  RETURN
3963
3951
   0    success
3964
3952
*/
3965
3953
 
3966
 
int make_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
 
3954
int make_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
3967
3955
{
3968
3956
  ST_FIELD_INFO *field_info= schema_table->fields_info;
3969
 
  Name_resolution_context *context= &session->lex->select_lex.context;
 
3957
  Name_resolution_context *context= &thd->lex->select_lex.context;
3970
3958
  for (; field_info->field_name; field_info++)
3971
3959
  {
3972
3960
    if (field_info->old_name)
3973
3961
    {
3974
3962
      Item_field *field= new Item_field(context,
3975
 
                                        NULL, NULL, field_info->field_name);
 
3963
                                        NullS, NullS, field_info->field_name);
3976
3964
      if (field)
3977
3965
      {
3978
3966
        field->set_name(field_info->old_name,
3979
3967
                        strlen(field_info->old_name),
3980
3968
                        system_charset_info);
3981
 
        if (add_item_to_list(session, field))
 
3969
        if (add_item_to_list(thd, field))
3982
3970
          return 1;
3983
3971
      }
3984
3972
    }
3987
3975
}
3988
3976
 
3989
3977
 
3990
 
int make_schemata_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
 
3978
int make_schemata_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
3991
3979
{
3992
3980
  char tmp[128];
3993
 
  LEX *lex= session->lex;
 
3981
  LEX *lex= thd->lex;
3994
3982
  SELECT_LEX *sel= lex->current_select;
3995
3983
  Name_resolution_context *context= &sel->context;
3996
3984
 
3999
3987
    ST_FIELD_INFO *field_info= &schema_table->fields_info[1];
4000
3988
    String buffer(tmp,sizeof(tmp), system_charset_info);
4001
3989
    Item_field *field= new Item_field(context,
4002
 
                                      NULL, NULL, field_info->field_name);
4003
 
    if (!field || add_item_to_list(session, field))
 
3990
                                      NullS, NullS, field_info->field_name);
 
3991
    if (!field || add_item_to_list(thd, field))
4004
3992
      return 1;
4005
3993
    buffer.length(0);
4006
3994
    buffer.append(field_info->old_name);
4016
4004
}
4017
4005
 
4018
4006
 
4019
 
int make_table_names_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
 
4007
int make_table_names_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
4020
4008
{
4021
4009
  char tmp[128];
4022
 
  String buffer(tmp,sizeof(tmp), session->charset());
4023
 
  LEX *lex= session->lex;
 
4010
  String buffer(tmp,sizeof(tmp), thd->charset());
 
4011
  LEX *lex= thd->lex;
4024
4012
  Name_resolution_context *context= &lex->select_lex.context;
4025
4013
 
4026
4014
  ST_FIELD_INFO *field_info= &schema_table->fields_info[2];
4034
4022
    buffer.append(')');
4035
4023
  }
4036
4024
  Item_field *field= new Item_field(context,
4037
 
                                    NULL, NULL, field_info->field_name);
4038
 
  if (add_item_to_list(session, field))
 
4025
                                    NullS, NullS, field_info->field_name);
 
4026
  if (add_item_to_list(thd, field))
4039
4027
    return 1;
4040
4028
  field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4041
 
  if (session->lex->verbose)
 
4029
  if (thd->lex->verbose)
4042
4030
  {
4043
4031
    field->set_name(buffer.ptr(), buffer.length(), system_charset_info);
4044
4032
    field_info= &schema_table->fields_info[3];
4045
 
    field= new Item_field(context, NULL, NULL, field_info->field_name);
4046
 
    if (add_item_to_list(session, field))
 
4033
    field= new Item_field(context, NullS, NullS, field_info->field_name);
 
4034
    if (add_item_to_list(thd, field))
4047
4035
      return 1;
4048
4036
    field->set_name(field_info->old_name, strlen(field_info->old_name),
4049
4037
                    system_charset_info);
4052
4040
}
4053
4041
 
4054
4042
 
4055
 
int make_columns_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
 
4043
int make_columns_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
4056
4044
{
4057
4045
  int fields_arr[]= {3, 14, 13, 6, 15, 5, 16, 17, 18, -1};
4058
4046
  int *field_num= fields_arr;
4059
4047
  ST_FIELD_INFO *field_info;
4060
 
  Name_resolution_context *context= &session->lex->select_lex.context;
 
4048
  Name_resolution_context *context= &thd->lex->select_lex.context;
4061
4049
 
4062
4050
  for (; *field_num >= 0; field_num++)
4063
4051
  {
4064
4052
    field_info= &schema_table->fields_info[*field_num];
4065
 
    if (!session->lex->verbose && (*field_num == 13 ||
 
4053
    if (!thd->lex->verbose && (*field_num == 13 ||
4066
4054
                               *field_num == 17 ||
4067
4055
                               *field_num == 18))
4068
4056
      continue;
4069
4057
    Item_field *field= new Item_field(context,
4070
 
                                      NULL, NULL, field_info->field_name);
 
4058
                                      NullS, NullS, field_info->field_name);
4071
4059
    if (field)
4072
4060
    {
4073
4061
      field->set_name(field_info->old_name,
4074
4062
                      strlen(field_info->old_name),
4075
4063
                      system_charset_info);
4076
 
      if (add_item_to_list(session, field))
 
4064
      if (add_item_to_list(thd, field))
4077
4065
        return 1;
4078
4066
    }
4079
4067
  }
4081
4069
}
4082
4070
 
4083
4071
 
4084
 
int make_character_sets_old_format(Session *session, ST_SCHEMA_TABLE *schema_table)
 
4072
int make_character_sets_old_format(THD *thd, ST_SCHEMA_TABLE *schema_table)
4085
4073
{
4086
4074
  int fields_arr[]= {0, 2, 1, 3, -1};
4087
4075
  int *field_num= fields_arr;
4088
4076
  ST_FIELD_INFO *field_info;
4089
 
  Name_resolution_context *context= &session->lex->select_lex.context;
 
4077
  Name_resolution_context *context= &thd->lex->select_lex.context;
4090
4078
 
4091
4079
  for (; *field_num >= 0; field_num++)
4092
4080
  {
4093
4081
    field_info= &schema_table->fields_info[*field_num];
4094
4082
    Item_field *field= new Item_field(context,
4095
 
                                      NULL, NULL, field_info->field_name);
 
4083
                                      NullS, NullS, field_info->field_name);
4096
4084
    if (field)
4097
4085
    {
4098
4086
      field->set_name(field_info->old_name,
4099
4087
                      strlen(field_info->old_name),
4100
4088
                      system_charset_info);
4101
 
      if (add_item_to_list(session, field))
 
4089
      if (add_item_to_list(thd, field))
4102
4090
        return 1;
4103
4091
    }
4104
4092
  }
4111
4099
 
4112
4100
  SYNOPSIS
4113
4101
  mysql_schema_table()
4114
 
    session                thread handler
 
4102
    thd                thread handler
4115
4103
    lex                pointer to LEX
4116
4104
    table_list         pointer to table_list
4117
4105
 
4120
4108
    1   error
4121
4109
*/
4122
4110
 
4123
 
int mysql_schema_table(Session *session, LEX *lex, TableList *table_list)
 
4111
int mysql_schema_table(THD *thd, LEX *lex, TableList *table_list)
4124
4112
{
4125
4113
  Table *table;
4126
 
  if (!(table= table_list->schema_table->create_table(session, table_list)))
 
4114
  if (!(table= table_list->schema_table->create_table(thd, table_list)))
4127
4115
    return(1);
4128
4116
  table->s->tmp_table= SYSTEM_TMP_TABLE;
4129
4117
  /*
4140
4128
  table_list->table_name= table->s->table_name.str;
4141
4129
  table_list->table_name_length= table->s->table_name.length;
4142
4130
  table_list->table= table;
4143
 
  table->next= session->derived_tables;
4144
 
  session->derived_tables= table;
 
4131
  table->next= thd->derived_tables;
 
4132
  thd->derived_tables= table;
4145
4133
  table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
4146
4134
 
4147
4135
  if (table_list->schema_table_reformed) // show command
4156
4144
      for (transl= table_list->field_translation; transl < end; transl++)
4157
4145
      {
4158
4146
        if (!transl->item->fixed &&
4159
 
            transl->item->fix_fields(session, &transl->item))
 
4147
            transl->item->fix_fields(thd, &transl->item))
4160
4148
          return(1);
4161
4149
      }
4162
4150
      return(0);
4163
4151
    }
4164
4152
    List_iterator_fast<Item> it(sel->item_list);
4165
4153
    if (!(transl=
4166
 
          (Field_translator*)(session->alloc(sel->item_list.elements *
 
4154
          (Field_translator*)(thd->stmt_arena->
 
4155
                              alloc(sel->item_list.elements *
4167
4156
                                    sizeof(Field_translator)))))
4168
4157
    {
4169
4158
      return(1);
4172
4161
    {
4173
4162
      transl->item= item;
4174
4163
      transl->name= item->name;
4175
 
      if (!item->fixed && item->fix_fields(session, &transl->item))
 
4164
      if (!item->fixed && item->fix_fields(thd, &transl->item))
4176
4165
      {
4177
4166
        return(1);
4178
4167
      }
4190
4179
 
4191
4180
  SYNOPSIS
4192
4181
    make_schema_select()
4193
 
    session                  thread handler
 
4182
    thd                  thread handler
4194
4183
    sel                  pointer to SELECT_LEX
4195
4184
    schema_table_idx     index of 'schema_tables' element
4196
4185
 
4199
4188
    1   error
4200
4189
*/
4201
4190
 
4202
 
int make_schema_select(Session *session, SELECT_LEX *sel,
 
4191
int make_schema_select(THD *thd, SELECT_LEX *sel,
4203
4192
                       enum enum_schema_tables schema_table_idx)
4204
4193
{
4205
4194
  ST_SCHEMA_TABLE *schema_table= get_schema_table(schema_table_idx);
4208
4197
     We have to make non const db_name & table_name
4209
4198
     because of lower_case_table_names
4210
4199
  */
4211
 
  session->make_lex_string(&db, INFORMATION_SCHEMA_NAME.str,
 
4200
  thd->make_lex_string(&db, INFORMATION_SCHEMA_NAME.str,
4212
4201
                       INFORMATION_SCHEMA_NAME.length, 0);
4213
 
  session->make_lex_string(&table, schema_table->table_name,
 
4202
  thd->make_lex_string(&table, schema_table->table_name,
4214
4203
                       strlen(schema_table->table_name), 0);
4215
 
  if (schema_table->old_format(session, schema_table) ||   /* Handle old syntax */
4216
 
      !sel->add_table_to_list(session, new Table_ident(session, db, table, 0),
 
4204
  if (schema_table->old_format(thd, schema_table) ||   /* Handle old syntax */
 
4205
      !sel->add_table_to_list(thd, new Table_ident(thd, db, table, 0),
4217
4206
                              0, 0, TL_READ))
4218
4207
  {
4219
4208
    return(1);
4239
4228
                              enum enum_schema_table_state executed_place)
4240
4229
{
4241
4230
  JOIN_TAB *tmp_join_tab= join->join_tab+join->tables;
4242
 
  Session *session= join->session;
4243
 
  LEX *lex= session->lex;
 
4231
  THD *thd= join->thd;
 
4232
  LEX *lex= thd->lex;
4244
4233
  bool result= 0;
4245
4234
 
4246
 
  session->no_warnings_for_error= 1;
 
4235
  thd->no_warnings_for_error= 1;
4247
4236
  for (JOIN_TAB *tab= join->join_tab; tab < tmp_join_tab; tab++)
4248
4237
  {
4249
4238
    if (!tab->table || !tab->table->pos_in_table_list)
4257
4246
 
4258
4247
 
4259
4248
      /* skip I_S optimizations specific to get_all_tables */
4260
 
      if (session->lex->describe &&
 
4249
      if (thd->lex->describe &&
4261
4250
          (table_list->schema_table->fill_table != get_all_tables))
4262
4251
        continue;
4263
4252
 
4291
4280
      else
4292
4281
        table_list->table->file->stats.records= 0;
4293
4282
 
4294
 
      if (table_list->schema_table->fill_table(session, table_list,
 
4283
      if (table_list->schema_table->fill_table(thd, table_list,
4295
4284
                                               tab->select_cond))
4296
4285
      {
4297
4286
        result= 1;
4304
4293
      table_list->schema_table_state= executed_place;
4305
4294
    }
4306
4295
  }
4307
 
  session->no_warnings_for_error= 0;
 
4296
  thd->no_warnings_for_error= 0;
4308
4297
  return(result);
4309
4298
}
4310
4299
 
4674
4663
 
4675
4664
  return(0);
4676
4665
err:
4677
 
  free(schema_table);
 
4666
  my_free(schema_table, MYF(0));
4678
4667
  return(1);
4679
4668
}
4680
4669
 
4683
4672
  ST_SCHEMA_TABLE *schema_table= (ST_SCHEMA_TABLE *)plugin->data;
4684
4673
 
4685
4674
  if (schema_table && plugin->plugin->deinit)
4686
 
    free(schema_table);
 
4675
    my_free(schema_table, MYF(0));
4687
4676
 
4688
4677
  return(0);
4689
4678
}