~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_base.cc

Removed/replaced DBUG symbols and standardized TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
 
17
17
/* Basic functions needed by many modules */
18
 
#include <drizzled/server_includes.h>
19
 
#include <drizzled/sql_select.h>
20
 
#include <mysys/my_dir.h>
21
 
#include <drizzled/drizzled_error_messages.h>
22
 
#include <libdrizzle/gettext.h>
 
18
 
 
19
#include "mysql_priv.h"
 
20
#include "sql_select.h"
 
21
#include <m_ctype.h>
 
22
#include <my_dir.h>
 
23
#include <hash.h>
23
24
 
24
25
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
25
26
 
27
28
  @defgroup Data_Dictionary Data Dictionary
28
29
  @{
29
30
*/
30
 
Table *unused_tables;                           /* Used by mysql_test */
 
31
TABLE *unused_tables;                           /* Used by mysql_test */
31
32
HASH open_cache;                                /* Used by mysql_test */
32
33
static HASH table_def_cache;
33
34
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
34
35
static pthread_mutex_t LOCK_table_share;
35
36
static bool table_def_inited= 0;
36
37
 
37
 
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
38
 
                             const char *alias,
39
 
                             char *cache_key, uint32_t cache_key_length);
40
 
static void free_cache_entry(Table *entry);
41
 
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
 
38
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
 
39
                             const char *alias,
 
40
                             char *cache_key, uint cache_key_length,
 
41
                             MEM_ROOT *mem_root, uint flags);
 
42
static void free_cache_entry(TABLE *entry);
 
43
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
42
44
                                 bool send_refresh);
43
45
 
44
46
 
45
 
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
46
 
                                  bool not_used __attribute__((unused)))
 
47
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
 
48
                                 my_bool not_used __attribute__((unused)))
47
49
{
48
 
  Table *entry=(Table*) record;
 
50
  TABLE *entry=(TABLE*) record;
49
51
  *length= entry->s->table_cache_key.length;
50
 
  return (unsigned char*) entry->s->table_cache_key.str;
 
52
  return (uchar*) entry->s->table_cache_key.str;
51
53
}
52
54
 
53
55
 
55
57
{
56
58
  return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
57
59
                   0, 0, table_cache_key,
58
 
                   (hash_free_key) free_cache_entry, 0);
 
60
                   (hash_free_key) free_cache_entry, 0) != 0;
59
61
}
60
62
 
61
63
void table_cache_free(void)
69
71
  return;
70
72
}
71
73
 
72
 
uint32_t cached_open_tables(void)
 
74
uint cached_open_tables(void)
73
75
{
74
76
  return open_cache.records;
75
77
}
99
101
    Length of key
100
102
*/
101
103
 
102
 
uint32_t create_table_def_key(THD *thd, char *key, TableList *table_list,
 
104
uint create_table_def_key(THD *thd, char *key, TABLE_LIST *table_list,
103
105
                          bool tmp_table)
104
106
{
105
 
  uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
 
107
  uint key_length= (uint) (strmov(strmov(key, table_list->db)+1,
106
108
                                  table_list->table_name)-key)+1;
107
109
  if (tmp_table)
108
110
  {
119
121
  Functions to handle table definition cach (TABLE_SHARE)
120
122
*****************************************************************************/
121
123
 
122
 
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
123
 
                                bool not_used __attribute__((unused)))
 
124
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
 
125
                               my_bool not_used __attribute__((unused)))
124
126
{
125
127
  TABLE_SHARE *entry=(TABLE_SHARE*) record;
126
128
  *length= entry->table_cache_key.length;
127
 
  return (unsigned char*) entry->table_cache_key.str;
 
129
  return (uchar*) entry->table_cache_key.str;
128
130
}
129
131
 
130
132
 
152
154
 
153
155
  return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
154
156
                   0, 0, table_def_key,
155
 
                   (hash_free_key) table_def_free_entry, 0);
 
157
                   (hash_free_key) table_def_free_entry, 0) != 0;
156
158
}
157
159
 
158
160
 
168
170
}
169
171
 
170
172
 
171
 
uint32_t cached_table_definitions(void)
 
173
uint cached_table_definitions(void)
172
174
{
173
175
  return table_def_cache.records;
174
176
}
199
201
   #  Share for table
200
202
*/
201
203
 
202
 
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
203
 
                             uint32_t key_length, uint32_t db_flags, int *error)
 
204
TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key,
 
205
                             uint key_length, uint db_flags, int *error)
204
206
{
205
207
  TABLE_SHARE *share;
206
208
 
207
209
  *error= 0;
208
210
 
209
211
  /* Read table definition from cache */
210
 
  if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
 
212
  if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
211
213
                                         key_length)))
212
214
    goto found;
213
215
 
237
239
   */
238
240
  assign_new_table_id(share);
239
241
 
240
 
  if (my_hash_insert(&table_def_cache, (unsigned char*) share))
 
242
  if (my_hash_insert(&table_def_cache, (uchar*) share))
241
243
  {
242
244
    free_table_share(share);
243
245
    return(0);                          // return error
245
247
  if (open_table_def(thd, share, db_flags))
246
248
  {
247
249
    *error= share->error;
248
 
    (void) hash_delete(&table_def_cache, (unsigned char*) share);
 
250
    (void) hash_delete(&table_def_cache, (uchar*) share);
249
251
    return(0);
250
252
  }
251
253
  share->ref_count++;                           // Mark in use
288
290
         oldest_unused_share->next)
289
291
  {
290
292
    pthread_mutex_lock(&oldest_unused_share->mutex);
291
 
    hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
 
293
    VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
292
294
  }
293
295
 
294
296
  return(share);
302
304
*/
303
305
 
304
306
static TABLE_SHARE
305
 
*get_table_share_with_create(THD *thd, TableList *table_list,
306
 
                             char *key, uint32_t key_length,
307
 
                             uint32_t db_flags, int *error)
 
307
*get_table_share_with_create(THD *thd, TABLE_LIST *table_list,
 
308
                             char *key, uint key_length,
 
309
                             uint db_flags, int *error)
308
310
{
309
311
  TABLE_SHARE *share;
310
312
  int tmp;
327
329
    Finally, if share is still NULL, it's a real error and we need
328
330
    to abort.
329
331
 
330
 
    @todo Rework alternative ways to deal with ER_NO_SUCH Table.
 
332
    @todo Rework alternative ways to deal with ER_NO_SUCH TABLE.
331
333
  */
332
334
  if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
333
335
 
336
338
  /* Table didn't exist. Check if some engine can provide it */
337
339
  if ((tmp= ha_create_table_from_engine(thd, table_list->db,
338
340
                                        table_list->table_name)) < 0)
 
341
  {
 
342
    /*
 
343
      No such table in any engine.
 
344
      Hide "Table doesn't exist" errors if the table belongs to a view.
 
345
      The check for thd->is_error() is necessary to not push an
 
346
      unwanted error in case of pre-locking, which silences
 
347
      "no such table" errors.
 
348
      @todo Rework the alternative ways to deal with ER_NO_SUCH TABLE.
 
349
    */
 
350
    if (thd->is_error() && table_list->belong_to_view)
 
351
    {
 
352
      thd->clear_error();
 
353
      my_error(ER_VIEW_INVALID, MYF(0), "", "");
 
354
    }
339
355
    return(0);
340
 
 
 
356
  }
341
357
  if (tmp)
342
358
  {
343
359
    /* Give right error message */
349
365
    return(0);
350
366
  }
351
367
  /* Table existed in engine. Let's open it */
352
 
  drizzle_reset_errors(thd, 1);                   // Clear warnings
 
368
  mysql_reset_errors(thd, 1);                   // Clear warnings
353
369
  thd->clear_error();                           // Clear error message
354
370
  return(get_table_share(thd, table_list, key, key_length,
355
371
                              db_flags, error));
378
394
*/
379
395
 
380
396
void release_table_share(TABLE_SHARE *share,
381
 
                         enum release_type type __attribute__((unused)))
 
397
                         enum release_type type __attribute__((__unused__)))
382
398
{
383
399
  bool to_be_deleted= 0;
384
400
 
406
422
 
407
423
  if (to_be_deleted)
408
424
  {
409
 
    hash_delete(&table_def_cache, (unsigned char*) share);
 
425
    hash_delete(&table_def_cache, (uchar*) share);
410
426
    return;
411
427
  }
412
428
  pthread_mutex_unlock(&share->mutex);
430
446
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
431
447
{
432
448
  char key[NAME_LEN*2+2];
433
 
  TableList table_list;
434
 
  uint32_t key_length;
 
449
  TABLE_LIST table_list;
 
450
  uint key_length;
435
451
  safe_mutex_assert_owner(&LOCK_open);
436
452
 
437
453
  table_list.db= (char*) db;
438
454
  table_list.table_name= (char*) table_name;
439
455
  key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
440
 
  return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
 
456
  return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
441
457
}  
442
458
 
443
459
 
459
475
*/
460
476
 
461
477
 
462
 
void close_handle_and_leave_table_as_lock(Table *table)
 
478
void close_handle_and_leave_table_as_lock(TABLE *table)
463
479
{
464
480
  TABLE_SHARE *share, *old_share= table->s;
465
481
  char *key_buff;
477
493
                       &key_buff, old_share->table_cache_key.length,
478
494
                       NULL))
479
495
  {
480
 
    memset(share, 0, sizeof(*share));
 
496
    bzero((char*) share, sizeof(*share));
481
497
    share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
482
498
                               old_share->table_cache_key.length);
483
499
    share->tmp_table= INTERNAL_TMP_TABLE;       // for intern_close_table()
512
528
    #           Pointer to list of names of open tables.
513
529
*/
514
530
 
515
 
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
 
531
OPEN_TABLE_LIST *list_open_tables(THD *thd __attribute__((__unused__)),
516
532
                                  const char *db, const char *wild)
517
533
{
518
534
  int result = 0;
519
 
  OPEN_TableList **start_list, *open_list;
520
 
  TableList table_list;
 
535
  OPEN_TABLE_LIST **start_list, *open_list;
 
536
  TABLE_LIST table_list;
521
537
 
522
 
  pthread_mutex_lock(&LOCK_open);
523
 
  memset(&table_list, 0, sizeof(table_list));
 
538
  VOID(pthread_mutex_lock(&LOCK_open));
 
539
  bzero((char*) &table_list,sizeof(table_list));
524
540
  start_list= &open_list;
525
541
  open_list=0;
526
542
 
527
 
  for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
 
543
  for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
528
544
  {
529
 
    OPEN_TableList *table;
530
 
    Table *entry=(Table*) hash_element(&open_cache,idx);
 
545
    OPEN_TABLE_LIST *table;
 
546
    TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
531
547
    TABLE_SHARE *share= entry->s;
532
548
 
533
549
    if (db && my_strcasecmp(system_charset_info, db, share->db.str))
554
570
    }
555
571
    if (table)
556
572
      continue;
557
 
    if (!(*start_list = (OPEN_TableList *)
 
573
    if (!(*start_list = (OPEN_TABLE_LIST *)
558
574
          sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
559
575
    {
560
576
      open_list=0;                              // Out of memory
561
577
      break;
562
578
    }
563
 
    my_stpcpy((*start_list)->table=
564
 
           my_stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
 
579
    strmov((*start_list)->table=
 
580
           strmov(((*start_list)->db= (char*) ((*start_list)+1)),
565
581
                  share->db.str)+1,
566
582
           share->table_name.str);
567
583
    (*start_list)->in_use= entry->in_use ? 1 : 0;
569
585
    start_list= &(*start_list)->next;
570
586
    *start_list=0;
571
587
  }
572
 
  pthread_mutex_unlock(&LOCK_open);
 
588
  VOID(pthread_mutex_unlock(&LOCK_open));
573
589
  return(open_list);
574
590
}
575
591
 
578
594
 ****************************************************************************/
579
595
 
580
596
 
581
 
void intern_close_table(Table *table)
 
597
void intern_close_table(TABLE *table)
582
598
{                                               // Free all structures
583
599
  free_io_cache(table);
584
600
  if (table->file)                              // Not true if name lock
585
 
    closefrm(table, 1);                 // close file
 
601
    VOID(closefrm(table, 1));                   // close file
586
602
  return;
587
603
}
588
604
 
597
613
    We need to have a lock on LOCK_open when calling this
598
614
*/
599
615
 
600
 
static void free_cache_entry(Table *table)
 
616
static void free_cache_entry(TABLE *table)
601
617
{
602
618
  intern_close_table(table);
603
619
  if (!table->in_use)
611
627
        unused_tables=0;
612
628
    }
613
629
  }
614
 
  free((unsigned char*) table);
 
630
  my_free((uchar*) table,MYF(0));
615
631
  return;
616
632
}
617
633
 
618
634
/* Free resources allocated by filesort() and read_record() */
619
635
 
620
 
void free_io_cache(Table *table)
 
636
void free_io_cache(TABLE *table)
621
637
{
622
638
  if (table->sort.io_cache)
623
639
  {
624
640
    close_cached_file(table->sort.io_cache);
625
 
    free((unsigned char*) table->sort.io_cache);
 
641
    my_free((uchar*) table->sort.io_cache,MYF(0));
626
642
    table->sort.io_cache=0;
627
643
  }
628
644
  return;
644
660
          and tables must be NULL.
645
661
*/
646
662
 
647
 
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
 
663
bool close_cached_tables(THD *thd, TABLE_LIST *tables, bool have_lock,
648
664
                         bool wait_for_refresh, bool wait_for_placeholders)
649
665
{
650
666
  bool result=0;
651
667
  assert(thd || (!wait_for_refresh && !tables));
652
668
 
653
669
  if (!have_lock)
654
 
    pthread_mutex_lock(&LOCK_open);
 
670
    VOID(pthread_mutex_lock(&LOCK_open));
655
671
  if (!tables)
656
672
  {
657
673
    refresh_version++;                          // Force close of open tables
658
674
    while (unused_tables)
659
675
    {
660
676
#ifdef EXTRA_DEBUG
661
 
      if (hash_delete(&open_cache,(unsigned char*) unused_tables))
 
677
      if (hash_delete(&open_cache,(uchar*) unused_tables))
662
678
        printf("Warning: Couldn't delete open table from hash\n");
663
679
#else
664
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
 
680
      VOID(hash_delete(&open_cache,(uchar*) unused_tables));
665
681
#endif
666
682
    }
667
683
    /* Free table shares */
668
684
    while (oldest_unused_share->next)
669
685
    {
670
686
      pthread_mutex_lock(&oldest_unused_share->mutex);
671
 
      hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
 
687
      VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
672
688
    }
673
689
    if (wait_for_refresh)
674
690
    {
709
725
        after the call to close_old_data_files() i.e. after removal of
710
726
        current thread locks.
711
727
      */
712
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
728
      for (uint idx=0 ; idx < open_cache.records ; idx++)
713
729
      {
714
 
        Table *table=(Table*) hash_element(&open_cache,idx);
 
730
        TABLE *table=(TABLE*) hash_element(&open_cache,idx);
715
731
        if (table->in_use)
716
732
          table->in_use->some_tables_deleted= 1;
717
733
      }
720
736
  else
721
737
  {
722
738
    bool found=0;
723
 
    for (TableList *table= tables; table; table= table->next_local)
 
739
    for (TABLE_LIST *table= tables; table; table= table->next_local)
724
740
    {
725
741
      if (remove_table_from_cache(thd, table->db, table->table_name,
726
742
                                  RTFC_OWNED_BY_THD_FLAG))
748
764
    while (found && ! thd->killed)
749
765
    {
750
766
      found=0;
751
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
767
      for (uint idx=0 ; idx < open_cache.records ; idx++)
752
768
      {
753
 
        Table *table=(Table*) hash_element(&open_cache,idx);
 
769
        TABLE *table=(TABLE*) hash_element(&open_cache,idx);
754
770
        /* Avoid a self-deadlock. */
755
771
        if (table->in_use == thd)
756
772
          continue;
757
773
        /*
758
774
          Note that we wait here only for tables which are actually open, and
759
 
          not for placeholders with Table::open_placeholder set. Waiting for
 
775
          not for placeholders with TABLE::open_placeholder set. Waiting for
760
776
          latter will cause deadlock in the following scenario, for example:
761
777
 
762
778
          conn1: lock table t1 write;
786
802
    result=reopen_tables(thd,1,1);
787
803
    thd->in_lock_tables=0;
788
804
    /* Set version for table */
789
 
    for (Table *table=thd->open_tables; table ; table= table->next)
 
805
    for (TABLE *table=thd->open_tables; table ; table= table->next)
790
806
    {
791
807
      /*
792
808
        Preserve the version (0) of write locked tables so that a impending
797
813
    }
798
814
  }
799
815
  if (!have_lock)
800
 
    pthread_mutex_unlock(&LOCK_open);
 
816
    VOID(pthread_mutex_unlock(&LOCK_open));
801
817
  if (wait_for_refresh)
802
818
  {
803
819
    pthread_mutex_lock(&thd->mysys_var->mutex);
818
834
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
819
835
                                    LEX_STRING *connection, bool have_lock)
820
836
{
821
 
  uint32_t idx;
822
 
  TableList tmp, *tables= NULL;
 
837
  uint idx;
 
838
  TABLE_LIST tmp, *tables= NULL;
823
839
  bool result= false;
824
840
  assert(thd);
825
841
 
826
 
  memset(&tmp, 0, sizeof(TableList));
 
842
  bzero(&tmp, sizeof(TABLE_LIST));
827
843
 
828
844
  if (!have_lock)
829
 
    pthread_mutex_lock(&LOCK_open);
 
845
    VOID(pthread_mutex_lock(&LOCK_open));
830
846
 
831
847
  for (idx= 0; idx < table_def_cache.records; idx++)
832
848
  {
851
867
    tmp.table_name= share->table_name.str;
852
868
    tmp.next_local= tables;
853
869
 
854
 
    tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp, 
855
 
                                       sizeof(TableList));
 
870
    tables= (TABLE_LIST *) memdup_root(thd->mem_root, (char*)&tmp, 
 
871
                                       sizeof(TABLE_LIST));
856
872
  }
857
873
 
858
874
  if (tables)
859
875
    result= close_cached_tables(thd, tables, true, false, false);
860
876
 
861
877
  if (!have_lock)
862
 
    pthread_mutex_unlock(&LOCK_open);
 
878
    VOID(pthread_mutex_unlock(&LOCK_open));
863
879
 
864
880
  if (if_wait_for_refresh)
865
881
  {
866
882
    pthread_mutex_lock(&thd->mysys_var->mutex);
867
883
    thd->mysys_var->current_mutex= 0;
868
884
    thd->mysys_var->current_cond= 0;
869
 
    thd->set_proc_info(0);
 
885
    thd->proc_info=0;
870
886
    pthread_mutex_unlock(&thd->mysys_var->mutex);
871
887
  }
872
888
 
886
902
 
887
903
static void mark_temp_tables_as_free_for_reuse(THD *thd)
888
904
{
889
 
  for (Table *table= thd->temporary_tables ; table ; table= table->next)
 
905
  for (TABLE *table= thd->temporary_tables ; table ; table= table->next)
890
906
  {
891
907
    if ((table->query_id == thd->query_id) && ! table->open_by_handler)
892
908
    {
920
936
    set to query_id of original query.
921
937
*/
922
938
 
923
 
static void mark_used_tables_as_free_for_reuse(THD *thd, Table *table)
 
939
static void mark_used_tables_as_free_for_reuse(THD *thd, TABLE *table)
924
940
{
925
941
  for (; table ; table= table->next)
926
942
  {
947
963
 
948
964
  safe_mutex_assert_not_owner(&LOCK_open);
949
965
 
950
 
  pthread_mutex_lock(&LOCK_open);
 
966
  VOID(pthread_mutex_lock(&LOCK_open));
951
967
 
952
968
  while (thd->open_tables)
953
969
    found_old_table|= close_thread_table(thd, &thd->open_tables);
955
971
 
956
972
  /* Free tables to hold down open files */
957
973
  while (open_cache.records > table_cache_size && unused_tables)
958
 
    hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
 
974
    VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
959
975
  if (found_old_table)
960
976
  {
961
977
    /* Tell threads waiting for refresh that something has happened */
962
978
    broadcast_refresh();
963
979
  }
964
980
 
965
 
  pthread_mutex_unlock(&LOCK_open);
 
981
  VOID(pthread_mutex_unlock(&LOCK_open));
966
982
}
967
983
 
968
984
 
986
1002
 
987
1003
void close_thread_tables(THD *thd)
988
1004
{
989
 
  Table *table;
 
1005
  TABLE *table;
990
1006
 
991
1007
  /*
992
1008
    We are assuming here that thd->derived_tables contains ONLY derived
1001
1017
  */
1002
1018
  if (thd->derived_tables)
1003
1019
  {
1004
 
    Table *next;
 
1020
    TABLE *next;
1005
1021
    /*
1006
1022
      Close all derived tables generated in queries like
1007
1023
      SELECT * FROM (SELECT * FROM t1)
1009
1025
    for (table= thd->derived_tables ; table ; table= next)
1010
1026
    {
1011
1027
      next= table->next;
1012
 
      table->free_tmp_table(thd);
 
1028
      free_tmp_table(thd, table);
1013
1029
    }
1014
1030
    thd->derived_tables= 0;
1015
1031
  }
1077
1093
 
1078
1094
/* move one table to free list */
1079
1095
 
1080
 
bool close_thread_table(THD *thd, Table **table_ptr)
 
1096
bool close_thread_table(THD *thd, TABLE **table_ptr)
1081
1097
{
1082
1098
  bool found_old_table= 0;
1083
 
  Table *table= *table_ptr;
 
1099
  TABLE *table= *table_ptr;
1084
1100
 
1085
1101
  assert(table->key_read == 0);
1086
1102
  assert(!table->file || table->file->inited == handler::NONE);
1090
1106
  if (table->needs_reopen_or_name_lock() ||
1091
1107
      thd->version != refresh_version || !table->db_stat)
1092
1108
  {
1093
 
    hash_delete(&open_cache,(unsigned char*) table);
 
1109
    VOID(hash_delete(&open_cache,(uchar*) table));
1094
1110
    found_old_table=1;
1095
1111
  }
1096
1112
  else
1097
1113
  {
1098
1114
    /*
1099
 
      Open placeholders have Table::db_stat set to 0, so they should be
 
1115
      Open placeholders have TABLE::db_stat set to 0, so they should be
1100
1116
      handled by the first alternative.
1101
1117
    */
1102
1118
    assert(!table->open_placeholder);
1119
1135
 
1120
1136
 
1121
1137
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1122
 
static inline uint32_t  tmpkeyval(THD *thd __attribute__((unused)),
1123
 
                              Table *table)
 
1138
static inline uint  tmpkeyval(THD *thd __attribute__((__unused__)),
 
1139
                              TABLE *table)
1124
1140
{
1125
1141
  return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1126
1142
}
1128
1144
 
1129
1145
/*
1130
1146
  Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1131
 
  creates one DROP TEMPORARY Table binlog event for each pseudo-thread 
 
1147
  creates one DROP TEMPORARY TABLE binlog event for each pseudo-thread 
1132
1148
*/
1133
1149
 
1134
1150
void close_temporary_tables(THD *thd)
1135
1151
{
1136
 
  Table *table;
1137
 
  Table *next= NULL;
1138
 
  Table *prev_table;
 
1152
  TABLE *table;
 
1153
  TABLE *next= NULL;
 
1154
  TABLE *prev_table;
1139
1155
  /* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1140
1156
  bool was_quote_show= true;
1141
1157
 
1144
1160
 
1145
1161
  if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1146
1162
  {
1147
 
    Table *tmp_next;
 
1163
    TABLE *tmp_next;
1148
1164
    for (table= thd->temporary_tables; table; table= tmp_next)
1149
1165
    {
1150
1166
      tmp_next= table->next;
1155
1171
  }
1156
1172
 
1157
1173
  /* Better add "if exists", in case a RESET MASTER has been done */
1158
 
  const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1159
 
  uint32_t stub_len= sizeof(stub) - 1;
 
1174
  const char stub[]= "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS ";
 
1175
  uint stub_len= sizeof(stub) - 1;
1160
1176
  char buf[256];
1161
1177
  String s_query= String(buf, sizeof(buf), system_charset_info);
1162
1178
  bool found_user_tables= false;
1172
1188
       table;
1173
1189
       prev_table= table, table= table->next)
1174
1190
  {
1175
 
    Table *prev_sorted /* same as for prev_table */, *sorted;
 
1191
    TABLE *prev_sorted /* same as for prev_table */, *sorted;
1176
1192
    if (is_user_table(table))
1177
1193
    {
1178
1194
      if (!found_user_tables)
1238
1254
        close_temporary(table, 1, 1);
1239
1255
      }
1240
1256
      thd->clear_error();
1241
 
      const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
 
1257
      CHARSET_INFO *cs_save= thd->variables.character_set_client;
1242
1258
      thd->variables.character_set_client= system_charset_info;
1243
1259
      Query_log_event qinfo(thd, s_query.ptr(),
1244
1260
                            s_query.length() - 1 /* to remove trailing ',' */,
1288
1304
    #           Pointer to found table.
1289
1305
*/
1290
1306
 
1291
 
TableList *find_table_in_list(TableList *table,
1292
 
                               TableList *TableList::*link,
 
1307
TABLE_LIST *find_table_in_list(TABLE_LIST *table,
 
1308
                               TABLE_LIST *TABLE_LIST::*link,
1293
1309
                               const char *db_name,
1294
1310
                               const char *table_name)
1295
1311
{
1328
1344
    Also SELECT::exclude_from_table_unique_test used to exclude from check
1329
1345
    tables of main SELECT of multi-delete and multi-update
1330
1346
 
1331
 
    We also skip tables with TableList::prelocking_placeholder set,
 
1347
    We also skip tables with TABLE_LIST::prelocking_placeholder set,
1332
1348
    because we want to allow SELECTs from them, and their modification
1333
1349
    will rise the error anyway.
1334
1350
 
1340
1356
    0 if table is unique
1341
1357
*/
1342
1358
 
1343
 
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
 
1359
TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1344
1360
                         bool check_alias)
1345
1361
{
1346
 
  TableList *res;
 
1362
  TABLE_LIST *res;
1347
1363
  const char *d_name, *t_name, *t_alias;
1348
1364
 
1349
1365
  /*
1350
1366
    If this function called for query which update table (INSERT/UPDATE/...)
1351
 
    then we have in table->table pointer to Table object which we are
1352
 
    updating even if it is VIEW so we need TableList of this Table object
 
1367
    then we have in table->table pointer to TABLE object which we are
 
1368
    updating even if it is VIEW so we need TABLE_LIST of this TABLE object
1353
1369
    to get right names (even if lower_case_table_names used).
1354
1370
 
1355
1371
    If this function called for CREATE command that we have not opened table
1356
 
    (table->table equal to 0) and right names is in current TableList
 
1372
    (table->table equal to 0) and right names is in current TABLE_LIST
1357
1373
    object.
1358
1374
  */
1359
1375
  if (table->table)
1363
1379
      return(0);
1364
1380
    table= table->find_underlying_table(table->table);
1365
1381
    /*
1366
 
      as far as we have table->table we have to find real TableList of
 
1382
      as far as we have table->table we have to find real TABLE_LIST of
1367
1383
      it in underlying tables
1368
1384
    */
1369
1385
    assert(table);
1407
1423
    here we hide view underlying tables if we have them
1408
1424
*/
1409
1425
 
1410
 
void update_non_unique_table_error(TableList *update,
1411
 
                                   const char *operation __attribute__((unused)),
1412
 
                                   TableList *duplicate __attribute__((unused)))
 
1426
void update_non_unique_table_error(TABLE_LIST *update,
 
1427
                                   const char *operation __attribute__((__unused__)),
 
1428
                                   TABLE_LIST *duplicate __attribute__((__unused__)))
1413
1429
{
1414
1430
  my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1415
1431
}
1416
1432
 
1417
1433
 
1418
 
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
 
1434
TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name)
1419
1435
{
1420
 
  TableList table_list;
 
1436
  TABLE_LIST table_list;
1421
1437
 
1422
1438
  table_list.db= (char*) db;
1423
1439
  table_list.table_name= (char*) table_name;
1425
1441
}
1426
1442
 
1427
1443
 
1428
 
Table *find_temporary_table(THD *thd, TableList *table_list)
 
1444
TABLE *find_temporary_table(THD *thd, TABLE_LIST *table_list)
1429
1445
{
1430
1446
  char  key[MAX_DBKEY_LENGTH];
1431
1447
  uint  key_length;
1432
 
  Table *table;
 
1448
  TABLE *table;
1433
1449
 
1434
1450
  key_length= create_table_def_key(thd, key, table_list, 1);
1435
1451
  for (table=thd->temporary_tables ; table ; table= table->next)
1456
1472
 
1457
1473
  This function is used to drop user temporary tables, as well as
1458
1474
  internal tables created in CREATE TEMPORARY TABLE ... SELECT
1459
 
  or ALTER Table. Even though part of the work done by this function
 
1475
  or ALTER TABLE. Even though part of the work done by this function
1460
1476
  is redundant when the table is internal, as long as we
1461
1477
  link both internal and user temporary tables into the same
1462
1478
  thd->temporary_tables list, it's impossible to tell here whether
1468
1484
  @retval -1  the table is in use by a outer query
1469
1485
*/
1470
1486
 
1471
 
int drop_temporary_table(THD *thd, TableList *table_list)
 
1487
int drop_temporary_table(THD *thd, TABLE_LIST *table_list)
1472
1488
{
1473
 
  Table *table;
 
1489
  TABLE *table;
1474
1490
 
1475
1491
  if (!(table= find_temporary_table(thd, table_list)))
1476
1492
    return(1);
1495
1511
  unlink from thd->temporary tables and close temporary table
1496
1512
*/
1497
1513
 
1498
 
void close_temporary_table(THD *thd, Table *table,
 
1514
void close_temporary_table(THD *thd, TABLE *table,
1499
1515
                           bool free_share, bool delete_table)
1500
1516
{
1501
1517
  if (table->prev)
1536
1552
    If this is needed, use close_temporary_table()
1537
1553
*/
1538
1554
 
1539
 
void close_temporary(Table *table, bool free_share, bool delete_table)
 
1555
void close_temporary(TABLE *table, bool free_share, bool delete_table)
1540
1556
{
1541
1557
  handlerton *table_type= table->s->db_type();
1542
1558
 
1552
1568
  if (free_share)
1553
1569
  {
1554
1570
    free_table_share(table->s);
1555
 
    free((char*) table);
 
1571
    my_free((char*) table,MYF(0));
1556
1572
  }
1557
1573
  return;
1558
1574
}
1559
1575
 
1560
1576
 
1561
1577
/*
1562
 
  Used by ALTER Table when the table is a temporary one. It changes something
 
1578
  Used by ALTER TABLE when the table is a temporary one. It changes something
1563
1579
  only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1564
1580
  name).
1565
1581
  Prepares a table cache key, which is the concatenation of db, table_name and
1566
1582
  thd->slave_proxy_id, separated by '\0'.
1567
1583
*/
1568
1584
 
1569
 
bool rename_temporary_table(THD* thd, Table *table, const char *db,
 
1585
bool rename_temporary_table(THD* thd, TABLE *table, const char *db,
1570
1586
                            const char *table_name)
1571
1587
{
1572
1588
  char *key;
1573
 
  uint32_t key_length;
 
1589
  uint key_length;
1574
1590
  TABLE_SHARE *share= table->s;
1575
 
  TableList table_list;
 
1591
  TABLE_LIST table_list;
1576
1592
 
1577
1593
  if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1578
1594
    return(1);                          /* purecov: inspected */
1587
1603
 
1588
1604
        /* move table first in unused links */
1589
1605
 
1590
 
static void relink_unused(Table *table)
 
1606
static void relink_unused(TABLE *table)
1591
1607
{
1592
1608
  if (table != unused_tables)
1593
1609
  {
1617
1633
          not locked (for example already unlocked).
1618
1634
*/
1619
1635
 
1620
 
void unlink_open_table(THD *thd, Table *find, bool unlock)
 
1636
void unlink_open_table(THD *thd, TABLE *find, bool unlock)
1621
1637
{
1622
1638
  char key[MAX_DBKEY_LENGTH];
1623
 
  uint32_t key_length= find->s->table_cache_key.length;
1624
 
  Table *list, **prev;
 
1639
  uint key_length= find->s->table_cache_key.length;
 
1640
  TABLE *list, **prev;
1625
1641
 
1626
1642
  safe_mutex_assert_owner(&LOCK_open);
1627
1643
 
1646
1662
      /* Remove table from open_tables list. */
1647
1663
      *prev= list->next;
1648
1664
      /* Close table. */
1649
 
      hash_delete(&open_cache,(unsigned char*) list); // Close table
 
1665
      VOID(hash_delete(&open_cache,(uchar*) list)); // Close table
1650
1666
    }
1651
1667
    else
1652
1668
    {
1665
1681
    Auxiliary routine which closes and drops open table.
1666
1682
 
1667
1683
    @param  thd         Thread handle
1668
 
    @param  table       Table object for table to be dropped
 
1684
    @param  table       TABLE object for table to be dropped
1669
1685
    @param  db_name     Name of database for this table
1670
1686
    @param  table_name  Name of this table
1671
1687
 
1680
1696
          table that was locked with LOCK TABLES.
1681
1697
*/
1682
1698
 
1683
 
void drop_open_table(THD *thd, Table *table, const char *db_name,
 
1699
void drop_open_table(THD *thd, TABLE *table, const char *db_name,
1684
1700
                     const char *table_name)
1685
1701
{
1686
1702
  if (table->s->tmp_table)
1688
1704
  else
1689
1705
  {
1690
1706
    handlerton *table_type= table->s->db_type();
1691
 
    pthread_mutex_lock(&LOCK_open);
 
1707
    VOID(pthread_mutex_lock(&LOCK_open));
1692
1708
    /*
1693
1709
      unlink_open_table() also tells threads waiting for refresh or close
1694
1710
      that something has happened.
1695
1711
    */
1696
1712
    unlink_open_table(thd, table, false);
1697
1713
    quick_rm_table(table_type, db_name, table_name, 0);
1698
 
    pthread_mutex_unlock(&LOCK_open);
 
1714
    VOID(pthread_mutex_unlock(&LOCK_open));
1699
1715
  }
1700
1716
}
1701
1717
 
1717
1733
  const char *proc_info;
1718
1734
  thd->mysys_var->current_mutex= mutex;
1719
1735
  thd->mysys_var->current_cond= cond;
1720
 
  proc_info=thd->get_proc_info();
 
1736
  proc_info=thd->proc_info;
1721
1737
  thd_proc_info(thd, "Waiting for table");
1722
1738
  if (!thd->killed)
1723
1739
    (void) pthread_cond_wait(cond, mutex);
1753
1769
  @return false on success, true otherwise.
1754
1770
*/
1755
1771
 
1756
 
bool name_lock_locked_table(THD *thd, TableList *tables)
 
1772
bool name_lock_locked_table(THD *thd, TABLE_LIST *tables)
1757
1773
{
1758
1774
  /* Under LOCK TABLES we must only accept write locked tables. */
1759
1775
  tables->table= find_locked_table(thd, tables->db, tables->table_name);
1782
1798
  SYNOPSIS
1783
1799
    reopen_name_locked_table()
1784
1800
      thd         Thread handle
1785
 
      table_list  TableList object for table to be open, TableList::table
1786
 
                  member should point to Table object which was used for
 
1801
      table_list  TABLE_LIST object for table to be open, TABLE_LIST::table
 
1802
                  member should point to TABLE object which was used for
1787
1803
                  name-locking.
1788
 
      link_in     true  - if Table object for table to be opened should be
 
1804
      link_in     true  - if TABLE object for table to be opened should be
1789
1805
                          linked into THD::open_tables list.
1790
1806
                  false - placeholder used for name-locking is already in
1791
 
                          this list so we only need to preserve Table::next
 
1807
                          this list so we only need to preserve TABLE::next
1792
1808
                          pointer.
1793
1809
 
1794
1810
  NOTE
1799
1815
    true  - Error
1800
1816
*/
1801
1817
 
1802
 
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
 
1818
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in)
1803
1819
{
1804
 
  Table *table= table_list->table;
 
1820
  TABLE *table= table_list->table;
1805
1821
  TABLE_SHARE *share;
1806
1822
  char *table_name= table_list->table_name;
1807
 
  Table orig_table;
 
1823
  TABLE orig_table;
1808
1824
 
1809
1825
  safe_mutex_assert_owner(&LOCK_open);
1810
1826
 
1815
1831
 
1816
1832
  if (open_unireg_entry(thd, table, table_list, table_name,
1817
1833
                        table->s->table_cache_key.str,
1818
 
                        table->s->table_cache_key.length))
 
1834
                        table->s->table_cache_key.length, thd->mem_root, 0))
1819
1835
  {
1820
1836
    intern_close_table(table);
1821
1837
    /*
1848
1864
  else
1849
1865
  {
1850
1866
    /*
1851
 
      Table object should be already in THD::open_tables list so we just
1852
 
      need to set Table::next correctly.
 
1867
      TABLE object should be already in THD::open_tables list so we just
 
1868
      need to set TABLE::next correctly.
1853
1869
    */
1854
1870
    table->next= orig_table.next;
1855
1871
  }
1857
1873
  table->tablenr=thd->current_tablenr++;
1858
1874
  table->used_fields=0;
1859
1875
  table->const_table=0;
1860
 
  table->null_row= false;
1861
 
  table->maybe_null= false;
1862
 
  table->force_index= false;
 
1876
  table->null_row= table->maybe_null= table->force_index= 0;
1863
1877
  table->status=STATUS_NO_RECORD;
1864
 
  return false;
 
1878
  return(false);
1865
1879
}
1866
1880
 
1867
1881
 
1874
1888
    @param key         Table cache key for name to be locked
1875
1889
    @param key_length  Table cache key length
1876
1890
 
1877
 
    @return Pointer to Table object used for name locking or 0 in
 
1891
    @return Pointer to TABLE object used for name locking or 0 in
1878
1892
            case of failure.
1879
1893
*/
1880
1894
 
1881
 
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1882
 
                                      uint32_t key_length)
 
1895
TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
 
1896
                                      uint key_length)
1883
1897
{
1884
 
  Table *table;
 
1898
  TABLE *table;
1885
1899
  TABLE_SHARE *share;
1886
1900
  char *key_buff;
1887
1901
 
1905
1919
  table->in_use= thd;
1906
1920
  table->locked_by_name=1;
1907
1921
 
1908
 
  if (my_hash_insert(&open_cache, (unsigned char*)table))
 
1922
  if (my_hash_insert(&open_cache, (uchar*)table))
1909
1923
  {
1910
 
    free((unsigned char*) table);
 
1924
    my_free((uchar*) table, MYF(0));
1911
1925
    return(NULL);
1912
1926
  }
1913
1927
 
1925
1939
    @param[out] table       Out parameter which is either:
1926
1940
                            - set to NULL if table cache contains record for
1927
1941
                              the table or
1928
 
                            - set to point to the Table instance used for
 
1942
                            - set to point to the TABLE instance used for
1929
1943
                              name-locking.
1930
1944
 
1931
1945
    @note This function takes into account all records for table in table
1938
1952
*/
1939
1953
 
1940
1954
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1941
 
                                   const char *table_name, Table **table)
 
1955
                                   const char *table_name, TABLE **table)
1942
1956
{
1943
1957
  char key[MAX_DBKEY_LENGTH];
1944
 
  uint32_t key_length;
1945
 
 
1946
 
  key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1947
 
  pthread_mutex_lock(&LOCK_open);
1948
 
 
1949
 
  if (hash_search(&open_cache, (unsigned char *)key, key_length))
 
1958
  uint key_length;
 
1959
 
 
1960
  key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1;
 
1961
  VOID(pthread_mutex_lock(&LOCK_open));
 
1962
 
 
1963
  if (hash_search(&open_cache, (uchar *)key, key_length))
1950
1964
  {
1951
 
    pthread_mutex_unlock(&LOCK_open);
 
1965
    VOID(pthread_mutex_unlock(&LOCK_open));
1952
1966
    *table= 0;
1953
1967
    return(false);
1954
1968
  }
1955
1969
  if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1956
1970
  {
1957
 
    pthread_mutex_unlock(&LOCK_open);
 
1971
    VOID(pthread_mutex_unlock(&LOCK_open));
1958
1972
    return(true);
1959
1973
  }
1960
1974
  (*table)->open_placeholder= 1;
1961
1975
  (*table)->next= thd->open_tables;
1962
1976
  thd->open_tables= *table;
1963
 
  pthread_mutex_unlock(&LOCK_open);
 
1977
  VOID(pthread_mutex_unlock(&LOCK_open));
1964
1978
  return(false);
1965
1979
}
1966
1980
 
1986
2000
    @retval  false  No error. 'exists' out parameter set accordingly.
1987
2001
*/
1988
2002
 
1989
 
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
 
2003
bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists)
1990
2004
{
1991
2005
  char path[FN_REFLEN];
1992
2006
  int rc;
2040
2054
                        If this is a NULL pointer, then the table is not
2041
2055
                        put in the thread-open-list.
2042
2056
    flags               Bitmap of flags to modify how open works:
2043
 
                          DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
 
2057
                          MYSQL_LOCK_IGNORE_FLUSH - Open table even if
2044
2058
                          someone has done a flush or namelock on it.
2045
2059
                          No version number checking is done.
2046
 
                          DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
 
2060
                          MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary
2047
2061
                          table not the base table or view.
2048
2062
 
2049
2063
  IMPLEMENTATION
2052
2066
    If table list element for the table to be opened has "create" flag
2053
2067
    set and table does not exist, this function will automatically insert
2054
2068
    a placeholder for exclusive name lock into the open tables cache and
2055
 
    will return the Table instance that corresponds to this placeholder.
 
2069
    will return the TABLE instance that corresponds to this placeholder.
2056
2070
 
2057
2071
  RETURN
2058
2072
    NULL  Open failed.  If refresh is set then one should close
2059
2073
          all other tables and retry the open.
2060
 
    #     Success. Pointer to Table object for open table.
 
2074
    #     Success. Pointer to TABLE object for open table.
2061
2075
*/
2062
2076
 
2063
2077
 
2064
 
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint32_t flags)
 
2078
TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
 
2079
                  bool *refresh, uint flags)
2065
2080
{
2066
 
  register Table *table;
 
2081
  register TABLE *table;
2067
2082
  char key[MAX_DBKEY_LENGTH];
2068
2083
  unsigned int key_length;
2069
2084
  char *alias= table_list->alias;
2077
2092
    *refresh=0;
2078
2093
 
2079
2094
  /* an open table operation needs a lot of the stack space */
2080
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
 
2095
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2081
2096
    return(0);
2082
2097
 
2083
2098
  if (thd->killed)
2104
2119
        /*
2105
2120
          We're trying to use the same temporary table twice in a query.
2106
2121
          Right now we don't support this because a temporary table
2107
 
          is always represented by only one Table object in THD, and
 
2122
          is always represented by only one TABLE object in THD, and
2108
2123
          it can not be cloned. Emit an error for an unsupported behaviour.
2109
2124
        */
2110
2125
        if (table->query_id)
2119
2134
    }
2120
2135
  }
2121
2136
 
2122
 
  if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
 
2137
  if (flags & MYSQL_OPEN_TEMPORARY_ONLY)
2123
2138
  {
2124
2139
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2125
2140
    return(0);
2134
2149
  */
2135
2150
  if (thd->locked_tables)
2136
2151
  {                                             // Using table locks
2137
 
    Table *best_table= 0;
 
2152
    TABLE *best_table= 0;
2138
2153
    int best_distance= INT_MIN;
2139
2154
    bool check_if_used= false;
2140
2155
    for (table=thd->open_tables; table ; table=table->next)
2156
2171
          return(0);
2157
2172
        }
2158
2173
        /*
2159
 
          When looking for a usable Table, ignore MERGE children, as they
 
2174
          When looking for a usable TABLE, ignore MERGE children, as they
2160
2175
          belong to their parent and cannot be used explicitly.
2161
2176
        */
2162
2177
        if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2216
2231
    this is the normal use case.
2217
2232
    Now we should:
2218
2233
    - try to find the table in the table cache.
2219
 
    - if one of the discovered Table instances is name-locked
 
2234
    - if one of the discovered TABLE instances is name-locked
2220
2235
      (table->s->version == 0) or some thread has started FLUSH TABLES
2221
2236
      (refresh_version > table->s->version), back off -- we have to wait
2222
2237
      until no one holds a name lock on the table.
2223
 
    - if there is no such Table in the name cache, read the table definition
 
2238
    - if there is no such TABLE in the name cache, read the table definition
2224
2239
    and insert it into the cache.
2225
2240
    We perform all of the above under LOCK_open which currently protects
2226
2241
    the open cache (also known as table cache) and table definitions stored
2227
2242
    on disk.
2228
2243
  */
2229
2244
 
2230
 
  pthread_mutex_lock(&LOCK_open);
 
2245
  VOID(pthread_mutex_lock(&LOCK_open));
2231
2246
 
2232
2247
  /*
2233
2248
    If it's the first table from a list of tables used in a query,
2240
2255
  if (!thd->open_tables)
2241
2256
    thd->version=refresh_version;
2242
2257
  else if ((thd->version != refresh_version) &&
2243
 
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
 
2258
           ! (flags & MYSQL_LOCK_IGNORE_FLUSH))
2244
2259
  {
2245
2260
    /* Someone did a refresh while thread was opening tables */
2246
2261
    if (refresh)
2247
2262
      *refresh=1;
2248
 
    pthread_mutex_unlock(&LOCK_open);
 
2263
    VOID(pthread_mutex_unlock(&LOCK_open));
2249
2264
    return(0);
2250
2265
  }
2251
2266
 
2261
2276
 
2262
2277
  /*
2263
2278
    Actually try to find the table in the open_cache.
2264
 
    The cache may contain several "Table" instances for the same
 
2279
    The cache may contain several "TABLE" instances for the same
2265
2280
    physical table. The instances that are currently "in use" by
2266
2281
    some thread have their "in_use" member != NULL.
2267
2282
    There is no good reason for having more than one entry in the
2269
2284
    an implicit "pending locks queue" - see
2270
2285
    wait_for_locked_table_names for details.
2271
2286
  */
2272
 
  for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
 
2287
  for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
2273
2288
                                  &state);
2274
2289
       table && table->in_use ;
2275
 
       table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
 
2290
       table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
2276
2291
                                 &state))
2277
2292
  {
2278
2293
    /*
2282
2297
      (re-)opened and added to the cache.
2283
2298
      If since then we did (or just started) FLUSH TABLES
2284
2299
      statement, refresh_version has been increased.
2285
 
      For "name-locked" Table instances, table->s->version is set
 
2300
      For "name-locked" TABLE instances, table->s->version is set
2286
2301
      to 0 (see lock_table_name for details).
2287
2302
      In case there is a pending FLUSH TABLES or a name lock, we
2288
2303
      need to back off and re-start opening tables.
2295
2310
    */
2296
2311
    if (table->needs_reopen_or_name_lock())
2297
2312
    {
2298
 
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
 
2313
      if (flags & MYSQL_LOCK_IGNORE_FLUSH)
2299
2314
      {
2300
2315
        /* Force close at once after usage */
2301
2316
        thd->version= table->s->version;
2305
2320
      /* Avoid self-deadlocks by detecting self-dependencies. */
2306
2321
      if (table->open_placeholder && table->in_use == thd)
2307
2322
      {
2308
 
        pthread_mutex_unlock(&LOCK_open);
 
2323
        VOID(pthread_mutex_unlock(&LOCK_open));
2309
2324
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2310
2325
        return(0);
2311
2326
      }
2346
2361
      }
2347
2362
      else
2348
2363
      {
2349
 
        pthread_mutex_unlock(&LOCK_open);
 
2364
        VOID(pthread_mutex_unlock(&LOCK_open));
2350
2365
      }
2351
2366
      /*
2352
2367
        There is a refresh in progress for this table.
2372
2387
  }
2373
2388
  else
2374
2389
  {
2375
 
    /* Insert a new Table instance into the open cache */
 
2390
    /* Insert a new TABLE instance into the open cache */
2376
2391
    int error;
2377
2392
    /* Free cache if too big */
2378
2393
    while (open_cache.records > table_cache_size && unused_tables)
2379
 
      hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
 
2394
      VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2380
2395
 
2381
2396
    if (table_list->create)
2382
2397
    {
2384
2399
 
2385
2400
      if (check_if_table_exists(thd, table_list, &exists))
2386
2401
      {
2387
 
        pthread_mutex_unlock(&LOCK_open);
 
2402
        VOID(pthread_mutex_unlock(&LOCK_open));
2388
2403
        return(NULL);
2389
2404
      }
2390
2405
 
2395
2410
        */
2396
2411
        if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2397
2412
        {
2398
 
          pthread_mutex_unlock(&LOCK_open);
 
2413
          VOID(pthread_mutex_unlock(&LOCK_open));
2399
2414
          return(NULL);
2400
2415
        }
2401
2416
        /*
2406
2421
        table->open_placeholder= 1;
2407
2422
        table->next= thd->open_tables;
2408
2423
        thd->open_tables= table;
2409
 
        pthread_mutex_unlock(&LOCK_open);
 
2424
        VOID(pthread_mutex_unlock(&LOCK_open));
2410
2425
        return(table);
2411
2426
      }
2412
2427
      /* Table exists. Let us try to open it. */
2413
2428
    }
2414
2429
 
2415
2430
    /* make a new table */
2416
 
    if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
 
2431
    if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
2417
2432
    {
2418
 
      pthread_mutex_unlock(&LOCK_open);
 
2433
      VOID(pthread_mutex_unlock(&LOCK_open));
2419
2434
      return(NULL);
2420
2435
    }
2421
2436
 
2422
 
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
 
2437
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
 
2438
                             mem_root, (flags & OPEN_VIEW_NO_PARSE));
2423
2439
    /* Combine the follow two */
2424
2440
    if (error > 0)
2425
2441
    {
2426
 
      free((unsigned char*)table);
2427
 
      pthread_mutex_unlock(&LOCK_open);
 
2442
      my_free((uchar*)table, MYF(0));
 
2443
      VOID(pthread_mutex_unlock(&LOCK_open));
2428
2444
      return(NULL);
2429
2445
    }
2430
2446
    if (error < 0)
2431
2447
    {
2432
 
      free((unsigned char*)table);
2433
 
      pthread_mutex_unlock(&LOCK_open);
 
2448
      my_free((uchar*)table, MYF(0));
 
2449
      VOID(pthread_mutex_unlock(&LOCK_open));
2434
2450
      return(0); // VIEW
2435
2451
    }
2436
 
    my_hash_insert(&open_cache,(unsigned char*) table);
 
2452
    VOID(my_hash_insert(&open_cache,(uchar*) table));
2437
2453
  }
2438
2454
 
2439
 
  pthread_mutex_unlock(&LOCK_open);
 
2455
  VOID(pthread_mutex_unlock(&LOCK_open));
2440
2456
  if (refresh)
2441
2457
  {
2442
2458
    table->next=thd->open_tables;               /* Link into simple list */
2453
2469
  /* Fix alias if table name changes */
2454
2470
  if (strcmp(table->alias, alias))
2455
2471
  {
2456
 
    uint32_t length=(uint) strlen(alias)+1;
 
2472
    uint length=(uint) strlen(alias)+1;
2457
2473
    table->alias= (char*) my_realloc((char*) table->alias, length,
2458
2474
                                     MYF(MY_WME));
2459
 
    memcpy((void*) table->alias, alias, length);
 
2475
    memcpy((char*) table->alias, alias, length);
2460
2476
  }
2461
2477
  /* These variables are also set in reopen_table() */
2462
2478
  table->tablenr=thd->current_tablenr++;
2463
2479
  table->used_fields=0;
2464
2480
  table->const_table=0;
2465
 
  table->null_row= false;
2466
 
  table->maybe_null= false;
2467
 
  table->force_index= false;
 
2481
  table->null_row= table->maybe_null= table->force_index= 0;
2468
2482
  table->status=STATUS_NO_RECORD;
2469
2483
  table->insert_values= 0;
 
2484
  table->fulltext_searched= 0;
 
2485
  table->file->ft_handler= 0;
2470
2486
  /* Catch wrong handling of the auto_increment_field_not_null. */
2471
2487
  assert(!table->auto_increment_field_not_null);
2472
2488
  table->auto_increment_field_not_null= false;
2479
2495
}
2480
2496
 
2481
2497
 
2482
 
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
 
2498
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name)
2483
2499
{
2484
2500
  char  key[MAX_DBKEY_LENGTH];
2485
 
  uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
 
2501
  uint key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
2486
2502
 
2487
 
  for (Table *table=thd->open_tables; table ; table=table->next)
 
2503
  for (TABLE *table=thd->open_tables; table ; table=table->next)
2488
2504
  {
2489
2505
    if (table->s->table_cache_key.length == key_length &&
2490
2506
        !memcmp(table->s->table_cache_key.str, key, key_length))
2510
2526
   1  error. The old table object is not changed.
2511
2527
*/
2512
2528
 
2513
 
bool reopen_table(Table *table)
 
2529
bool reopen_table(TABLE *table)
2514
2530
{
2515
 
  Table tmp;
 
2531
  TABLE tmp;
2516
2532
  bool error= 1;
2517
2533
  Field **field;
2518
 
  uint32_t key,part;
2519
 
  TableList table_list;
 
2534
  uint key,part;
 
2535
  TABLE_LIST table_list;
2520
2536
  THD *thd= table->in_use;
2521
2537
 
2522
2538
  assert(table->s->ref_count == 0);
2524
2540
 
2525
2541
#ifdef EXTRA_DEBUG
2526
2542
  if (table->db_stat)
2527
 
    sql_print_error(_("Table %s had a open data handler in reopen_table"),
 
2543
    sql_print_error("Table %s had a open data handler in reopen_table",
2528
2544
                    table->alias);
2529
2545
#endif
2530
 
  memset(&table_list, 0, sizeof(TableList));
 
2546
  bzero((char*) &table_list, sizeof(TABLE_LIST));
2531
2547
  table_list.db=         table->s->db.str;
2532
2548
  table_list.table_name= table->s->table_name.str;
2533
2549
  table_list.table=      table;
2538
2554
  if (open_unireg_entry(thd, &tmp, &table_list,
2539
2555
                        table->alias,
2540
2556
                        table->s->table_cache_key.str,
2541
 
                        table->s->table_cache_key.length))
 
2557
                        table->s->table_cache_key.length,
 
2558
                        thd->mem_root, 0))
2542
2559
    goto end;
2543
2560
 
2544
2561
  /* This list copies variables set by open_table */
2560
2577
  tmp.prev=             table->prev;
2561
2578
 
2562
2579
  if (table->file)
2563
 
    closefrm(table, 1);         // close file, free everything
 
2580
    VOID(closefrm(table, 1));           // close file, free everything
2564
2581
 
2565
2582
  *table= tmp;
2566
2583
  table->default_column_bitmaps();
2602
2619
    @note This function assumes that if we are not under LOCK TABLES,
2603
2620
          then there is only one table open and locked. This means that
2604
2621
          the function probably has to be adjusted before it can be used
2605
 
          anywhere outside ALTER Table.
 
2622
          anywhere outside ALTER TABLE.
2606
2623
 
2607
2624
    @note Must not use TABLE_SHARE::table_name/db of the table being closed,
2608
2625
          the strings are used in a loop even after the share may be freed.
2611
2628
void close_data_files_and_morph_locks(THD *thd, const char *db,
2612
2629
                                      const char *table_name)
2613
2630
{
2614
 
  Table *table;
 
2631
  TABLE *table;
2615
2632
 
2616
2633
  safe_mutex_assert_owner(&LOCK_open);
2617
2634
 
2627
2644
 
2628
2645
  /*
2629
2646
    Note that open table list may contain a name-lock placeholder
2630
 
    for target table name if we process ALTER Table ... RENAME.
 
2647
    for target table name if we process ALTER TABLE ... RENAME.
2631
2648
    So loop below makes sense even if we are not under LOCK TABLES.
2632
2649
  */
2633
2650
  for (table=thd->open_tables; table ; table=table->next)
2657
2674
 
2658
2675
    @note Since this function can't properly handle prelocking and
2659
2676
          create placeholders it should be used in very special
2660
 
          situations like FLUSH TABLES or ALTER Table. In general
 
2677
          situations like FLUSH TABLES or ALTER TABLE. In general
2661
2678
          case one should just repeat open_tables()/lock_tables()
2662
2679
          combination when one needs tables to be reopened (for
2663
2680
          example see open_and_lock_tables()).
2669
2686
 
2670
2687
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2671
2688
{
2672
 
  Table *table,*next,**prev;
2673
 
  Table **tables,**tables_ptr;                  // For locks
 
2689
  TABLE *table,*next,**prev;
 
2690
  TABLE **tables,**tables_ptr;                  // For locks
2674
2691
  bool error=0, not_used;
2675
 
  const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2676
 
                    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2677
 
                    DRIZZLE_LOCK_IGNORE_FLUSH;
 
2692
  const uint flags= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN |
 
2693
                    MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK |
 
2694
                    MYSQL_LOCK_IGNORE_FLUSH;
2678
2695
 
2679
2696
  if (!thd->open_tables)
2680
2697
    return(0);
2686
2703
      The ptr is checked later
2687
2704
      Do not handle locks of MERGE children.
2688
2705
    */
2689
 
    uint32_t opens=0;
 
2706
    uint opens=0;
2690
2707
    for (table= thd->open_tables; table ; table=table->next)
2691
2708
      opens++;
2692
 
    tables= (Table**) my_alloca(sizeof(Table*)*opens);
 
2709
    tables= (TABLE**) my_alloca(sizeof(TABLE*)*opens);
2693
2710
  }
2694
2711
  else
2695
2712
    tables= &thd->open_tables;
2698
2715
  prev= &thd->open_tables;
2699
2716
  for (table=thd->open_tables; table ; table=next)
2700
2717
  {
2701
 
    uint32_t db_stat=table->db_stat;
 
2718
    uint db_stat=table->db_stat;
2702
2719
    next=table->next;
2703
2720
    if (!tables || (!db_stat && reopen_table(table)))
2704
2721
    {
2705
2722
      my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2706
 
      hash_delete(&open_cache,(unsigned char*) table);
 
2723
      VOID(hash_delete(&open_cache,(uchar*) table));
2707
2724
      error=1;
2708
2725
    }
2709
2726
    else
2723
2740
  *prev=0;
2724
2741
  if (tables != tables_ptr)                     // Should we get back old locks
2725
2742
  {
2726
 
    DRIZZLE_LOCK *lock;
 
2743
    MYSQL_LOCK *lock;
2727
2744
    /*
2728
2745
      We should always get these locks. Anyway, we must not go into
2729
2746
      wait_for_tables() as it tries to acquire LOCK_open, which is
2748
2765
  }
2749
2766
  if (get_locks && tables)
2750
2767
  {
2751
 
    my_afree((unsigned char*) tables);
 
2768
    my_afree((uchar*) tables);
2752
2769
  }
2753
2770
  broadcast_refresh();
2754
2771
  return(error);
2756
2773
 
2757
2774
 
2758
2775
/**
2759
 
    Close handlers for tables in list, but leave the Table structure
 
2776
    Close handlers for tables in list, but leave the TABLE structure
2760
2777
    intact so that we can re-open these quickly.
2761
2778
 
2762
2779
    @param thd           Thread context
2763
 
    @param table         Head of the list of Table objects
 
2780
    @param table         Head of the list of TABLE objects
2764
2781
    @param morph_locks   true  - remove locks which we have on tables being closed
2765
2782
                                 but ensure that no DML or DDL will sneak in before
2766
2783
                                 we will re-open the table (i.e. temporarily morph
2769
2786
    @param send_refresh  Should we awake waiters even if we didn't close any tables?
2770
2787
*/
2771
2788
 
2772
 
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
 
2789
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
2773
2790
                                 bool send_refresh)
2774
2791
{
2775
2792
  bool found= send_refresh;
2786
2803
      {
2787
2804
        if (morph_locks)
2788
2805
        {
2789
 
          Table *ulcktbl= table;
 
2806
          TABLE *ulcktbl= table;
2790
2807
          if (ulcktbl->lock_count)
2791
2808
          {
2792
2809
            /*
2812
2829
          }
2813
2830
          /*
2814
2831
            We want to protect the table from concurrent DDL operations
2815
 
            (like RENAME Table) until we will re-open and re-lock it.
 
2832
            (like RENAME TABLE) until we will re-open and re-lock it.
2816
2833
          */
2817
2834
          table->open_placeholder= 1;
2818
2835
        }
2824
2841
          We come here only in close-for-back-off scenario. So we have to
2825
2842
          "close" create placeholder here to avoid deadlocks (for example,
2826
2843
          in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
2827
 
          and RENAME Table t2 TO t1). In close-for-re-open scenario we will
 
2844
          and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will
2828
2845
          probably want to let it stay.
2829
2846
 
2830
2847
          Note "***": We must not enter this branch if the placeholder
2847
2864
  if the table is closed
2848
2865
*/
2849
2866
 
2850
 
bool table_is_used(Table *table, bool wait_for_name_lock)
 
2867
bool table_is_used(TABLE *table, bool wait_for_name_lock)
2851
2868
{
2852
2869
  do
2853
2870
  {
2854
2871
    char *key= table->s->table_cache_key.str;
2855
 
    uint32_t key_length= table->s->table_cache_key.length;
 
2872
    uint key_length= table->s->table_cache_key.length;
2856
2873
 
2857
2874
    HASH_SEARCH_STATE state;
2858
 
    for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
 
2875
    for (TABLE *search= (TABLE*) hash_first(&open_cache, (uchar*) key,
2859
2876
                                             key_length, &state);
2860
2877
         search ;
2861
 
         search= (Table*) hash_next(&open_cache, (unsigned char*) key,
 
2878
         search= (TABLE*) hash_next(&open_cache, (uchar*) key,
2862
2879
                                    key_length, &state))
2863
2880
    {
2864
2881
      if (search->in_use == table->in_use)
2923
2940
  INFORMATION
2924
2941
    This is only called on drop tables
2925
2942
 
2926
 
    The Table object for the dropped table is unlocked but still kept around
 
2943
    The TABLE object for the dropped table is unlocked but still kept around
2927
2944
    as a name lock, which means that the table will be available for other
2928
2945
    thread as soon as we call unlock_table_names().
2929
2946
    If there is multiple copies of the table locked, all copies except
2935
2952
*/
2936
2953
 
2937
2954
 
2938
 
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
 
2955
TABLE *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2939
2956
{
2940
 
  Table *table,*next,**prev, *found= 0;
 
2957
  TABLE *table,*next,**prev, *found= 0;
2941
2958
  prev= &thd->open_tables;
2942
2959
 
2943
2960
  /*
2968
2985
      else
2969
2986
      {
2970
2987
        /* We already have a name lock, remove copy */
2971
 
        hash_delete(&open_cache,(unsigned char*) table);
 
2988
        VOID(hash_delete(&open_cache,(uchar*) table));
2972
2989
      }
2973
2990
    }
2974
2991
    else
2982
2999
    broadcast_refresh();
2983
3000
  if (thd->locked_tables && thd->locked_tables->table_count == 0)
2984
3001
  {
2985
 
    free((unsigned char*) thd->locked_tables);
 
3002
    my_free((uchar*) thd->locked_tables,MYF(0));
2986
3003
    thd->locked_tables=0;
2987
3004
  }
2988
3005
  return(found);
2990
3007
 
2991
3008
 
2992
3009
/*
2993
 
  If we have the table open, which only happens when a LOCK Table has been
 
3010
  If we have the table open, which only happens when a LOCK TABLE has been
2994
3011
  done on the table, change the lock type to a lock that will abort all
2995
3012
  other threads trying to get the lock.
2996
3013
*/
2997
3014
 
2998
3015
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
2999
3016
{
3000
 
  Table *table;
 
3017
  TABLE *table;
3001
3018
  for (table= thd->open_tables; table ; table= table->next)
3002
3019
  {
3003
3020
    if (!strcmp(table->s->table_name.str, table_name) &&
3038
3055
    share opens have been executed while one table was open all the
3039
3056
    time).
3040
3057
 
3041
 
    share->table_map_id is not UINT32_MAX.
 
3058
    share->table_map_id is not ~0UL.
3042
3059
 */
3043
3060
void assign_new_table_id(TABLE_SHARE *share)
3044
3061
{
3045
 
  static uint32_t last_table_id= UINT32_MAX;
 
3062
  static ulong last_table_id= ~0UL;
3046
3063
 
3047
3064
  /* Preconditions */
3048
3065
  assert(share != NULL);
3053
3070
    There is one reserved number that cannot be used.  Remember to
3054
3071
    change this when 6-byte global table id's are introduced.
3055
3072
  */
3056
 
  if (unlikely(tid == UINT32_MAX))
 
3073
  if (unlikely(tid == ~0UL))
3057
3074
    tid= ++last_table_id;
3058
3075
  share->table_map_id= tid;
3059
3076
 
3060
3077
  /* Post conditions */
3061
 
  assert(share->table_map_id != UINT32_MAX);
 
3078
  assert(share->table_map_id != ~0UL);
3062
3079
 
3063
3080
  return;
3064
3081
}
3070
3087
    open_unireg_entry()
3071
3088
    thd                 Thread handle
3072
3089
    entry               Store open table definition here
3073
 
    table_list          TableList with db, table_name
 
3090
    table_list          TABLE_LIST with db, table_name & belong_to_view
3074
3091
    alias               Alias name
3075
3092
    cache_key           Key for share_cache
3076
3093
    cache_key_length    length of cache_key
 
3094
    mem_root            temporary mem_root for parsing
 
3095
    flags               the OPEN_VIEW_NO_PARSE flag to be passed to
 
3096
                        openfrm()/open_new_frm()
3077
3097
 
3078
3098
  NOTES
3079
3099
   Extra argument for open is taken from thd->open_options
3084
3104
    #   Error
3085
3105
*/
3086
3106
 
3087
 
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
 
3107
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
3088
3108
                             const char *alias,
3089
 
                             char *cache_key, uint32_t cache_key_length)
 
3109
                             char *cache_key, uint cache_key_length,
 
3110
                             MEM_ROOT *mem_root __attribute__((__unused__)),
 
3111
                             uint flags __attribute__((__unused__)))
3090
3112
{
3091
3113
  int error;
3092
3114
  TABLE_SHARE *share;
3093
 
  uint32_t discover_retry_count= 0;
 
3115
  uint discover_retry_count= 0;
3094
3116
 
3095
3117
  safe_mutex_assert_owner(&LOCK_open);
3096
3118
retry:
3097
3119
  if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3098
3120
                                           cache_key_length, 
 
3121
                                           OPEN_VIEW |
3099
3122
                                           table_list->i_s_requested_object,
3100
3123
                                           &error)))
3101
3124
    return(1);
3105
3128
                                               HA_OPEN_RNDFILE |
3106
3129
                                               HA_GET_INDEX |
3107
3130
                                               HA_TRY_READ_ONLY),
3108
 
                                       (EXTRA_RECORD),
 
3131
                                       (READ_KEYINFO | COMPUTE_TYPES |
 
3132
                                        EXTRA_RECORD),
3109
3133
                                       thd->open_options, entry, OTM_OPEN)))
3110
3134
  {
3111
3135
    if (error == 7)                             // Table def changed
3141
3165
      release_table_share(share, RELEASE_WAIT_FOR_DROP);
3142
3166
      if (!thd->killed)
3143
3167
      {
3144
 
        drizzle_reset_errors(thd, 1);         // Clear warnings
 
3168
        mysql_reset_errors(thd, 1);         // Clear warnings
3145
3169
        thd->clear_error();                 // Clear error message
3146
3170
        goto retry;
3147
3171
      }
3167
3191
                               (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3168
3192
                                       HA_GET_INDEX |
3169
3193
                                       HA_TRY_READ_ONLY),
3170
 
                               EXTRA_RECORD,
 
3194
                               READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD,
3171
3195
                               ha_open_options | HA_OPEN_FOR_REPAIR,
3172
3196
                               entry, OTM_OPEN) || ! entry->file ||
3173
3197
        (entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3175
3199
       /* Give right error message */
3176
3200
       thd->clear_error();
3177
3201
       my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3178
 
       sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
 
3202
       sql_print_error("Couldn't repair table: %s.%s", share->db.str,
3179
3203
                       share->table_name.str);
3180
3204
       if (entry->file)
3181
3205
        closefrm(entry, 0);
3201
3225
    if (mysql_bin_log.is_open())
3202
3226
    {
3203
3227
      char *query, *end;
3204
 
      uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
 
3228
      uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3205
3229
      if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3206
3230
      {
3207
3231
        /* this DELETE FROM is needed even with row-based binlogging */
3208
 
        end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3209
 
                      share->db.str,"`.`",share->table_name.str,"`", NULL);
 
3232
        end = strxmov(strmov(query, "DELETE FROM `"),
 
3233
                      share->db.str,"`.`",share->table_name.str,"`", NullS);
3210
3234
        thd->binlog_query(THD::STMT_QUERY_TYPE,
3211
3235
                          query, (ulong)(end-query), false, false);
3212
 
        free(query);
 
3236
        my_free(query, MYF(0));
3213
3237
      }
3214
3238
      else
3215
3239
      {
3218
3242
          DBA on top of warning the client (which will automatically be done
3219
3243
          because of MYF(MY_WME) in my_malloc() above).
3220
3244
        */
3221
 
        sql_print_error(_("When opening HEAP table, could not allocate memory "
3222
 
                          "to write 'DELETE FROM `%s`.`%s`' to the binary log"),
 
3245
        sql_print_error("When opening HEAP table, could not allocate memory "
 
3246
                        "to write 'DELETE FROM `%s`.`%s`' to the binary log",
3223
3247
                        table_list->db, table_list->table_name);
3224
3248
        closefrm(entry, 0);
3225
3249
        goto err;
3243
3267
    start - list of tables in/out
3244
3268
    counter - number of opened tables will be return using this parameter
3245
3269
    flags   - bitmap of flags to modify how the tables will be open:
3246
 
              DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
3270
              MYSQL_LOCK_IGNORE_FLUSH - open table even if someone has
3247
3271
              done a flush or namelock on it.
3248
3272
 
3249
3273
  NOTE
3262
3286
    -1 - error
3263
3287
*/
3264
3288
 
3265
 
int open_tables(THD *thd, TableList **start, uint32_t *counter, uint32_t flags)
 
3289
int open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags)
3266
3290
{
3267
 
  TableList *tables= NULL;
 
3291
  TABLE_LIST *tables= NULL;
3268
3292
  bool refresh;
3269
3293
  int result=0;
3270
3294
  MEM_ROOT new_frm_mem;
3301
3325
      continue;
3302
3326
    }
3303
3327
    /*
3304
 
      If this TableList object is a placeholder for an information_schema
 
3328
      If this TABLE_LIST object is a placeholder for an information_schema
3305
3329
      table, create a temporary table to represent the information_schema
3306
3330
      table in the query. Do not fill it yet - will be filled during
3307
3331
      execution.
3319
3343
      not opened yet. Try to open the table.
3320
3344
    */
3321
3345
    if (!tables->table)
3322
 
      tables->table= open_table(thd, tables, &refresh, flags);
 
3346
      tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
3323
3347
 
3324
3348
    if (!tables->table)
3325
3349
    {
3367
3391
  {
3368
3392
    /*
3369
3393
      Some functions determine success as (tables->table != NULL).
3370
 
      tables->table is in thd->open_tables.
 
3394
      tables->table is in thd->open_tables. It won't go lost. If the
 
3395
      error happens on a MERGE child, clear the parents TABLE reference.
3371
3396
    */
 
3397
    if (tables->parent_l)
 
3398
      tables->parent_l->table= NULL;
3372
3399
    tables->table= NULL;
3373
3400
  }
3374
3401
  return(result);
3389
3416
  1     error
3390
3417
*/
3391
3418
 
3392
 
static bool check_lock_and_start_stmt(THD *thd, Table *table,
 
3419
static bool check_lock_and_start_stmt(THD *thd, TABLE *table,
3393
3420
                                      thr_lock_type lock_type)
3394
3421
{
3395
3422
  int error;
3439
3466
    and locking issues because it does not call lock_tables().
3440
3467
*/
3441
3468
 
3442
 
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
 
3469
TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
3443
3470
                                thr_lock_type lock_type)
3444
3471
{
3445
 
  TableList *save_next_global;
 
3472
  TABLE_LIST *save_next_global;
3446
3473
 
3447
3474
  /* Remember old 'next' pointer. */
3448
3475
  save_next_global= table_l->next_global;
3451
3478
 
3452
3479
  /* Set requested lock type. */
3453
3480
  table_l->lock_type= lock_type;
 
3481
  /* Allow to open real tables only. */
 
3482
  table_l->required_type= FRMTYPE_TABLE;
3454
3483
 
3455
3484
  /* Open the table. */
3456
3485
  if (simple_open_n_lock_tables(thd, table_l))
3487
3516
      table_list->table         table
3488
3517
*/
3489
3518
 
3490
 
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3491
 
                   uint32_t lock_flags)
 
3519
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type,
 
3520
                   uint lock_flags)
3492
3521
{
3493
 
  Table *table;
 
3522
  TABLE *table;
3494
3523
  bool refresh;
3495
3524
 
3496
3525
  thd_proc_info(thd, "Opening table");
3497
3526
  thd->current_tablenr= 0;
3498
 
  while (!(table= open_table(thd, table_list, &refresh, 0)) &&
 
3527
  /* open_ltable can be used only for BASIC TABLEs */
 
3528
  table_list->required_type= FRMTYPE_TABLE;
 
3529
  while (!(table= open_table(thd, table_list, thd->mem_root, &refresh, 0)) &&
3499
3530
         refresh)
3500
3531
    ;
3501
3532
 
3547
3578
    the third argument set appropriately.
3548
3579
*/
3549
3580
 
3550
 
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
 
3581
int open_and_lock_tables_derived(THD *thd, TABLE_LIST *tables, bool derived)
3551
3582
{
3552
 
  uint32_t counter;
 
3583
  uint counter;
3553
3584
  bool need_reopen;
3554
3585
 
3555
3586
  for ( ; ; ) 
3580
3611
    thd         - thread handler
3581
3612
    tables      - list of tables for open
3582
3613
    flags       - bitmap of flags to modify how the tables will be open:
3583
 
                  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
3614
                  MYSQL_LOCK_IGNORE_FLUSH - open table even if someone has
3584
3615
                  done a flush or namelock on it.
3585
3616
 
3586
3617
  RETURN
3592
3623
    data from the tables.
3593
3624
*/
3594
3625
 
3595
 
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint32_t flags)
 
3626
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags)
3596
3627
{
3597
 
  uint32_t counter;
 
3628
  uint counter;
3598
3629
  assert(!thd->fill_derived_tables());
3599
3630
  if (open_tables(thd, &tables, &counter, flags) ||
3600
3631
      mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3640
3671
   @param tables Tables involved in the query
3641
3672
 */
3642
3673
 
3643
 
int decide_logging_format(THD *thd, TableList *tables)
 
3674
int decide_logging_format(THD *thd, TABLE_LIST *tables)
3644
3675
{
3645
3676
  if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3646
3677
  {
3647
3678
    handler::Table_flags flags_some_set= handler::Table_flags();
3648
3679
    handler::Table_flags flags_all_set= ~handler::Table_flags();
3649
 
    bool multi_engine= false;
 
3680
    my_bool multi_engine= false;
3650
3681
    void* prev_ht= NULL;
3651
 
    for (TableList *table= tables; table; table= table->next_global)
 
3682
    for (TABLE_LIST *table= tables; table; table= table->next_global)
3652
3683
    {
3653
3684
      if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3654
3685
      {
3655
 
        uint64_t const flags= table->table->file->ha_table_flags();
 
3686
        ulonglong const flags= table->table->file->ha_table_flags();
3656
3687
        if (prev_ht && prev_ht != table->table->file->ht)
3657
3688
          multi_engine= true;
3658
3689
        prev_ht= table->table->file->ht;
3684
3715
                " but not allowed by this combination of engines");
3685
3716
    }
3686
3717
 
 
3718
    /*
 
3719
      If more than one engine is involved in the statement and at
 
3720
      least one is doing it's own logging (is *self-logging*), the
 
3721
      statement cannot be logged atomically, so we generate an error
 
3722
      rather than allowing the binlog to become corrupt.
 
3723
     */
 
3724
    if (multi_engine &&
 
3725
        (flags_some_set & HA_HAS_OWN_BINLOGGING))
 
3726
    {
 
3727
      error= ER_BINLOG_LOGGING_IMPOSSIBLE;
 
3728
      my_error(error, MYF(0),
 
3729
               "Statement cannot be written atomically since more"
 
3730
               " than one engine involved and at least one engine"
 
3731
               " is self-logging");
 
3732
    }
 
3733
 
3687
3734
    if (error)
3688
3735
      return -1;
3689
3736
 
3737
3784
   -1   Error
3738
3785
*/
3739
3786
 
3740
 
int lock_tables(THD *thd, TableList *tables, uint32_t count, bool *need_reopen)
 
3787
int lock_tables(THD *thd, TABLE_LIST *tables, uint count, bool *need_reopen)
3741
3788
{
3742
 
  TableList *table;
 
3789
  TABLE_LIST *table;
3743
3790
 
3744
3791
  /*
3745
3792
    We can't meet statement requiring prelocking if we already
3753
3800
  if (!thd->locked_tables)
3754
3801
  {
3755
3802
    assert(thd->lock == 0);     // You must lock everything at once
3756
 
    Table **start,**ptr;
3757
 
    uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
 
3803
    TABLE **start,**ptr;
 
3804
    uint lock_flag= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN;
3758
3805
 
3759
 
    if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
 
3806
    if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
3760
3807
      return(-1);
3761
3808
    for (table= tables; table; table= table->next_global)
3762
3809
    {
3772
3819
  }
3773
3820
  else
3774
3821
  {
3775
 
    TableList *first_not_own= thd->lex->first_not_own_table();
 
3822
    TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
3776
3823
    /*
3777
3824
      When open_and_lock_tables() is called for a single table out of
3778
3825
      a table list, the 'next_global' chain is temporarily broken. We
3808
3855
 
3809
3856
*/
3810
3857
 
3811
 
void close_tables_for_reopen(THD *thd, TableList **tables)
 
3858
void close_tables_for_reopen(THD *thd, TABLE_LIST **tables)
3812
3859
{
3813
3860
  /*
3814
3861
    If table list consists only from tables from prelocking set, table list
3817
3864
  if (thd->lex->first_not_own_table() == *tables)
3818
3865
    *tables= 0;
3819
3866
  thd->lex->chop_off_not_own_tables();
3820
 
  for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
 
3867
  for (TABLE_LIST *tmp= *tables; tmp; tmp= tmp->next_global)
3821
3868
    tmp->table= 0;
3822
3869
  close_thread_tables(thd);
3823
3870
}
3840
3887
 
3841
3888
 RETURN
3842
3889
   0  Error
3843
 
   #  Table object
 
3890
   #  TABLE object
3844
3891
*/
3845
3892
 
3846
 
Table *open_temporary_table(THD *thd, const char *path, const char *db,
 
3893
TABLE *open_temporary_table(THD *thd, const char *path, const char *db,
3847
3894
                            const char *table_name, bool link_in_list,
3848
3895
                            open_table_mode open_mode)
3849
3896
{
3850
 
  Table *tmp_table;
 
3897
  TABLE *tmp_table;
3851
3898
  TABLE_SHARE *share;
3852
3899
  char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3853
 
  uint32_t key_length;
3854
 
  TableList table_list;
 
3900
  uint key_length;
 
3901
  TABLE_LIST table_list;
3855
3902
 
3856
3903
  table_list.db=         (char*) db;
3857
3904
  table_list.table_name= (char*) table_name;
3858
3905
  /* Create the cache_key for temporary tables */
3859
3906
  key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3860
3907
 
3861
 
  if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
 
3908
  if (!(tmp_table= (TABLE*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3862
3909
                                      strlen(path)+1 + key_length,
3863
3910
                                      MYF(MY_WME))))
3864
3911
    return(0);                          /* purecov: inspected */
3865
3912
 
3866
3913
  share= (TABLE_SHARE*) (tmp_table+1);
3867
3914
  tmp_path= (char*) (share+1);
3868
 
  saved_cache_key= my_stpcpy(tmp_path, path)+1;
 
3915
  saved_cache_key= strmov(tmp_path, path)+1;
3869
3916
  memcpy(saved_cache_key, cache_key, key_length);
3870
3917
 
3871
3918
  init_tmp_table_share(thd, share, saved_cache_key, key_length,
3872
 
                       strchr(saved_cache_key, '\0')+1, tmp_path);
 
3919
                       strend(saved_cache_key)+1, tmp_path);
3873
3920
 
3874
3921
  if (open_table_def(thd, share, 0) ||
3875
3922
      open_table_from_share(thd, share, table_name,
3877
3924
                            (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3878
3925
                                    HA_GET_INDEX),
3879
3926
                            (open_mode == OTM_ALTER) ?
3880
 
                              (EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3881
 
                            : (EXTRA_RECORD),
 
3927
                              (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD |
 
3928
                               OPEN_FRM_FILE_ONLY)
 
3929
                            : (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD),
3882
3930
                            ha_open_options,
3883
3931
                            tmp_table, open_mode))
3884
3932
  {
3885
3933
    /* No need to lock share->mutex as this is not needed for tmp tables */
3886
3934
    free_table_share(share);
3887
 
    free((char*) tmp_table);
 
3935
    my_free((char*) tmp_table,MYF(0));
3888
3936
    return(0);
3889
3937
  }
3890
3938
 
3923
3971
  handler *file;
3924
3972
  char *ext;
3925
3973
 
3926
 
  my_stpcpy(ext= strchr(path, '\0'), reg_ext);
 
3974
  strmov(ext= strend(path), reg_ext);
3927
3975
  if (my_delete(path,MYF(0)))
3928
3976
    error=1; /* purecov: inspected */
3929
3977
  *ext= 0;                              // remove extension
3931
3979
  if (!frm_only && file && file->ha_delete_table(path))
3932
3980
  {
3933
3981
    error=1;
3934
 
    sql_print_warning(_("Could not remove temporary table: '%s', error: %d"),
 
3982
    sql_print_warning("Could not remove temporary table: '%s', error: %d",
3935
3983
                      path, my_errno);
3936
3984
  }
3937
3985
  delete file;
3954
4002
 
3955
4003
#define WRONG_GRANT (Field*) -1
3956
4004
 
3957
 
static void update_field_dependencies(THD *thd, Field *field, Table *table)
 
4005
static void update_field_dependencies(THD *thd, Field *field, TABLE *table)
3958
4006
{
3959
4007
  if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3960
4008
  {
3996
4044
 
3997
4045
 
3998
4046
/*
 
4047
  Find a field by name in a view that uses merge algorithm.
 
4048
 
 
4049
  SYNOPSIS
 
4050
    find_field_in_view()
 
4051
    thd                         thread handler
 
4052
    table_list                  view to search for 'name'
 
4053
    name                        name of field
 
4054
    length                      length of name
 
4055
    item_name                   name of item if it will be created (VIEW)
 
4056
    ref                         expression substituted in VIEW should be passed
 
4057
                                using this reference (return view_ref_found)
 
4058
    register_tree_change        true if ref is not stack variable and we
 
4059
                                need register changes in item tree
 
4060
 
 
4061
  RETURN
 
4062
    0                   field is not found
 
4063
    view_ref_found      found value in VIEW (real result is in *ref)
 
4064
    #                   pointer to field - only for schema table fields
 
4065
*/
 
4066
 
 
4067
static Field *
 
4068
find_field_in_view(THD *thd, TABLE_LIST *table_list,
 
4069
                   const char *name, uint length __attribute__((__unused__)),
 
4070
                   const char *item_name __attribute__((__unused__)),
 
4071
                   Item **ref,
 
4072
                   bool register_tree_change __attribute__((__unused__)))
 
4073
{
 
4074
  Field_iterator_view field_it;
 
4075
  field_it.set(table_list);
 
4076
  
 
4077
  assert(table_list->schema_table_reformed ||
 
4078
              (ref != 0 && (0) != 0));
 
4079
  for (; !field_it.end_of_fields(); field_it.next())
 
4080
  {
 
4081
    if (!my_strcasecmp(system_charset_info, field_it.name(), name))
 
4082
    {
 
4083
      /*
 
4084
        create_item() may, or may not create a new Item, depending on
 
4085
        the column reference. See create_view_field() for details.
 
4086
      */
 
4087
      Item *item= field_it.create_item(thd);
 
4088
      
 
4089
      if (!item)
 
4090
        return(0);
 
4091
      /*
 
4092
       *ref != NULL means that *ref contains the item that we need to
 
4093
       replace. If the item was aliased by the user, set the alias to
 
4094
       the replacing item.
 
4095
       We need to set alias on both ref itself and on ref real item.
 
4096
      */
 
4097
      if (*ref && !(*ref)->is_autogenerated_name)
 
4098
      {
 
4099
        item->set_name((*ref)->name, (*ref)->name_length,
 
4100
                       system_charset_info);
 
4101
        item->real_item()->set_name((*ref)->name, (*ref)->name_length,
 
4102
                       system_charset_info);
 
4103
      }
 
4104
      if (register_tree_change)
 
4105
        thd->change_item_tree(ref, item);
 
4106
      else
 
4107
        *ref= item;
 
4108
      return((Field*) view_ref_found);
 
4109
    }
 
4110
  }
 
4111
  return(0);
 
4112
}
 
4113
 
 
4114
 
 
4115
/*
3999
4116
  Find field by name in a NATURAL/USING join table reference.
4000
4117
 
4001
4118
  SYNOPSIS
4025
4142
*/
4026
4143
 
4027
4144
static Field *
4028
 
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4029
 
                           uint32_t length __attribute__((unused)),
4030
 
                           Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4031
 
                           TableList **actual_table)
 
4145
find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
 
4146
                           uint length __attribute__((__unused__)),
 
4147
                           Item **ref, bool register_tree_change,
 
4148
                           TABLE_LIST **actual_table)
4032
4149
{
4033
4150
  List_iterator_fast<Natural_join_column>
4034
4151
    field_it(*(table_ref->join_columns));
4053
4170
  }
4054
4171
  if (!nj_col)
4055
4172
    return(NULL);
 
4173
 
 
4174
  if (nj_col->view_field)
 
4175
  {
 
4176
    Item *item;
 
4177
    /*
 
4178
      create_item() may, or may not create a new Item, depending on the
 
4179
      column reference. See create_view_field() for details.
 
4180
    */
 
4181
    item= nj_col->create_item(thd);
 
4182
    /*
 
4183
     *ref != NULL means that *ref contains the item that we need to
 
4184
     replace. If the item was aliased by the user, set the alias to
 
4185
     the replacing item.
 
4186
     We need to set alias on both ref itself and on ref real item.
 
4187
     */
 
4188
    if (*ref && !(*ref)->is_autogenerated_name)
 
4189
    {
 
4190
      item->set_name((*ref)->name, (*ref)->name_length,
 
4191
                     system_charset_info);
 
4192
      item->real_item()->set_name((*ref)->name, (*ref)->name_length,
 
4193
                                  system_charset_info);
 
4194
    }
 
4195
 
 
4196
    if (!item)
 
4197
      return(NULL);
 
4198
    assert(nj_col->table_field == NULL);
 
4199
    if (nj_col->table_ref->schema_table_reformed)
 
4200
    {
 
4201
      /*
 
4202
        Translation table items are always Item_fields and fixed
 
4203
        already('mysql_schema_table' function). So we can return
 
4204
        ->field. It is used only for 'show & where' commands.
 
4205
      */
 
4206
      return(((Item_field*) (nj_col->view_field->item))->field);
 
4207
    }
 
4208
    if (register_tree_change)
 
4209
      thd->change_item_tree(ref, item);
 
4210
    else
 
4211
      *ref= item;
 
4212
    found_field= (Field*) view_ref_found;
 
4213
  }
 
4214
  else
4056
4215
  {
4057
4216
    /* This is a base table. */
 
4217
    assert(nj_col->view_field == NULL);
4058
4218
    assert(nj_col->table_ref->table == nj_col->table_field->table);
4059
4219
    found_field= nj_col->table_field;
4060
4220
    update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4085
4245
*/
4086
4246
 
4087
4247
Field *
4088
 
find_field_in_table(THD *thd, Table *table, const char *name, uint32_t length,
4089
 
                    bool allow_rowid, uint32_t *cached_field_index_ptr)
 
4248
find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
 
4249
                    bool allow_rowid, uint *cached_field_index_ptr)
4090
4250
{
4091
4251
  Field **field_ptr, *field;
4092
 
  uint32_t cached_field_index= *cached_field_index_ptr;
 
4252
  uint cached_field_index= *cached_field_index_ptr;
4093
4253
 
4094
4254
  /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4095
4255
  if (cached_field_index < table->s->fields &&
4098
4258
    field_ptr= table->field + cached_field_index;
4099
4259
  else if (table->s->name_hash.records)
4100
4260
  {
4101
 
    field_ptr= (Field**) hash_search(&table->s->name_hash, (unsigned char*) name,
 
4261
    field_ptr= (Field**) hash_search(&table->s->name_hash, (uchar*) name,
4102
4262
                                     length);
4103
4263
    if (field_ptr)
4104
4264
    {
4181
4341
*/
4182
4342
 
4183
4343
Field *
4184
 
find_field_in_table_ref(THD *thd, TableList *table_list,
4185
 
                        const char *name, uint32_t length,
 
4344
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
 
4345
                        const char *name, uint length,
4186
4346
                        const char *item_name, const char *db_name,
4187
4347
                        const char *table_name, Item **ref,
4188
4348
                        bool check_privileges, bool allow_rowid,
4189
 
                        uint32_t *cached_field_index_ptr,
4190
 
                        bool register_tree_change, TableList **actual_table)
 
4349
                        uint *cached_field_index_ptr,
 
4350
                        bool register_tree_change, TABLE_LIST **actual_table)
4191
4351
{
4192
 
  Field *fld= NULL;
 
4352
  Field *fld;
4193
4353
 
4194
4354
  assert(table_list->alias);
4195
4355
  assert(name);
4230
4390
 
4231
4391
  if (table_list->field_translation)
4232
4392
  {
 
4393
    /* 'table_list' is a view or an information schema table. */
 
4394
    if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
 
4395
                                 register_tree_change)))
 
4396
      *actual_table= table_list;
4233
4397
  }
4234
4398
  else if (!table_list->nested_join)
4235
4399
  {
4251
4415
    */
4252
4416
    if (table_name && table_name[0])
4253
4417
    {
4254
 
      List_iterator<TableList> it(table_list->nested_join->join_list);
4255
 
      TableList *table;
 
4418
      List_iterator<TABLE_LIST> it(table_list->nested_join->join_list);
 
4419
      TABLE_LIST *table;
4256
4420
      while ((table= it++))
4257
4421
      {
4258
4422
        if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
4292
4456
          else
4293
4457
          {
4294
4458
            if (thd->mark_used_columns == MARK_COLUMNS_READ)
4295
 
              it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
 
4459
              it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4296
4460
          }
4297
4461
        }
4298
4462
        else
4299
4463
          field_to_set= fld;
4300
4464
        if (field_to_set)
4301
4465
        {
4302
 
          Table *table= field_to_set->table;
 
4466
          TABLE *table= field_to_set->table;
4303
4467
          if (thd->mark_used_columns == MARK_COLUMNS_READ)
4304
4468
            bitmap_set_bit(table->read_set, field_to_set->field_index);
4305
4469
          else
4326
4490
    #                   pointer to field
4327
4491
*/
4328
4492
 
4329
 
Field *find_field_in_table_sef(Table *table, const char *name)
 
4493
Field *find_field_in_table_sef(TABLE *table, const char *name)
4330
4494
{
4331
4495
  Field **field_ptr;
4332
4496
  if (table->s->name_hash.records)
4333
4497
  {
4334
 
    field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
 
4498
    field_ptr= (Field**)hash_search(&table->s->name_hash,(uchar*) name,
4335
4499
                                    strlen(name));
4336
4500
    if (field_ptr)
4337
4501
    {
4393
4557
 
4394
4558
Field *
4395
4559
find_field_in_tables(THD *thd, Item_ident *item,
4396
 
                     TableList *first_table, TableList *last_table,
 
4560
                     TABLE_LIST *first_table, TABLE_LIST *last_table,
4397
4561
                     Item **ref, find_item_error_report_type report_error,
4398
4562
                     bool check_privileges, bool register_tree_change)
4399
4563
{
4401
4565
  const char *db= item->db_name;
4402
4566
  const char *table_name= item->table_name;
4403
4567
  const char *name= item->field_name;
4404
 
  uint32_t length=(uint) strlen(name);
 
4568
  uint length=(uint) strlen(name);
4405
4569
  char name_buff[NAME_LEN+1];
4406
 
  TableList *cur_table= first_table;
4407
 
  TableList *actual_table;
 
4570
  TABLE_LIST *cur_table= first_table;
 
4571
  TABLE_LIST *actual_table;
4408
4572
  bool allow_rowid;
4409
4573
 
4410
4574
  if (!table_name || !table_name[0])
4419
4583
  {
4420
4584
    /*
4421
4585
      This shortcut is used by prepared statements. We assume that
4422
 
      TableList *first_table is not changed during query execution (which
 
4586
      TABLE_LIST *first_table is not changed during query execution (which
4423
4587
      is true for all queries except RENAME but luckily RENAME doesn't
4424
4588
      use fields...) so we can rely on reusing pointer to its member.
4425
4589
      With this optimization we also miss case when addition of one more
4426
4590
      field makes some prepared query ambiguous and so erroneous, but we
4427
4591
      accept this trade off.
4428
4592
    */
4429
 
    TableList *table_ref= item->cached_table;
 
4593
    TABLE_LIST *table_ref= item->cached_table;
4430
4594
    /*
4431
4595
      The condition (table_ref->view == NULL) ensures that we will call
4432
4596
      find_field_in_table even in the case of information schema tables
4450
4614
        Only views fields should be marked as dependent, not an underlying
4451
4615
        fields.
4452
4616
      */
 
4617
      if (!table_ref->belong_to_view)
4453
4618
      {
4454
4619
        SELECT_LEX *current_sel= thd->lex->current_select;
4455
4620
        SELECT_LEX *last_select= table_ref->select_lex;
4561
4726
    char buff[NAME_LEN*2+1];
4562
4727
    if (db && db[0])
4563
4728
    {
4564
 
      strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
 
4729
      strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
4565
4730
      table_name=buff;
4566
4731
    }
4567
4732
    my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4616
4781
 
4617
4782
 
4618
4783
Item **
4619
 
find_item_in_list(Item *find, List<Item> &items, uint32_t *counter,
 
4784
find_item_in_list(Item *find, List<Item> &items, uint *counter,
4620
4785
                  find_item_error_report_type report_error,
4621
4786
                  enum_resolution_type *resolution)
4622
4787
{
4631
4796
    (and not an item that happens to have a name).
4632
4797
  */
4633
4798
  bool is_ref_by_name= 0;
4634
 
  uint32_t unaliased_counter= 0;
 
4799
  uint unaliased_counter= 0;
4635
4800
 
4636
4801
  *resolution= NOT_RESOLVED;
4637
4802
 
4644
4809
    db_name=    ((Item_ident*) find)->db_name;
4645
4810
  }
4646
4811
 
4647
 
  for (uint32_t i= 0; (item=li++); i++)
 
4812
  for (uint i= 0; (item=li++); i++)
4648
4813
  {
4649
4814
    if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
4650
4815
    {
4886
5051
*/
4887
5052
 
4888
5053
static bool
4889
 
set_new_item_local_context(THD *thd, Item_ident *item, TableList *table_ref)
 
5054
set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref)
4890
5055
{
4891
5056
  Name_resolution_context *context;
4892
5057
  if (!(context= new (thd->mem_root) Name_resolution_context))
4931
5096
*/
4932
5097
 
4933
5098
static bool
4934
 
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4935
 
                    List<String> *using_fields, uint32_t *found_using_fields)
 
5099
mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
 
5100
                    List<String> *using_fields, uint *found_using_fields)
4936
5101
{
4937
5102
  Field_iterator_table_ref it_1, it_2;
4938
5103
  Natural_join_column *nj_col_1, *nj_col_2;
4942
5107
    Leaf table references to which new natural join columns are added
4943
5108
    if the leaves are != NULL.
4944
5109
  */
4945
 
  TableList *leaf_1= (table_ref_1->nested_join &&
 
5110
  TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
4946
5111
                       !table_ref_1->is_natural_join) ?
4947
5112
                      NULL : table_ref_1;
4948
 
  TableList *leaf_2= (table_ref_2->nested_join &&
 
5113
  TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
4949
5114
                       !table_ref_2->is_natural_join) ?
4950
5115
                      NULL : table_ref_2;
4951
5116
 
5032
5197
        goto err;                               // out of memory
5033
5198
 
5034
5199
      /*
 
5200
        The following assert checks that the two created items are of
 
5201
        type Item_ident.
 
5202
      */
 
5203
      assert(!thd->lex->current_select->no_wrap_view_item);
 
5204
      /*
5035
5205
        In the case of no_wrap_view_item == 0, the created items must be
5036
5206
        of sub-classes of Item_ident.
5037
5207
      */
5072
5242
 
5073
5243
      if (field_1)
5074
5244
      {
5075
 
        Table *table_1= nj_col_1->table_ref->table;
 
5245
        TABLE *table_1= nj_col_1->table_ref->table;
5076
5246
        /* Mark field_1 used for table cache. */
5077
5247
        bitmap_set_bit(table_1->read_set, field_1->field_index);
5078
5248
        table_1->covering_keys.intersect(field_1->part_of_key);
5080
5250
      }
5081
5251
      if (field_2)
5082
5252
      {
5083
 
        Table *table_2= nj_col_2->table_ref->table;
 
5253
        TABLE *table_2= nj_col_2->table_ref->table;
5084
5254
        /* Mark field_2 used for table cache. */
5085
5255
        bitmap_set_bit(table_2->read_set, field_2->field_index);
5086
5256
        table_2->covering_keys.intersect(field_2->part_of_key);
5145
5315
*/
5146
5316
 
5147
5317
static bool
5148
 
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5149
 
                                 TableList *natural_using_join,
5150
 
                                 TableList *table_ref_1,
5151
 
                                 TableList *table_ref_2,
 
5318
store_natural_using_join_columns(THD *thd __attribute__((__unused__)),
 
5319
                                 TABLE_LIST *natural_using_join,
 
5320
                                 TABLE_LIST *table_ref_1,
 
5321
                                 TABLE_LIST *table_ref_2,
5152
5322
                                 List<String> *using_fields,
5153
 
                                 uint32_t found_using_fields)
 
5323
                                 uint found_using_fields)
5154
5324
{
5155
5325
  Field_iterator_table_ref it_1, it_2;
5156
5326
  Natural_join_column *nj_col_1, *nj_col_2;
5248
5418
  DESCRIPTION
5249
5419
    The procedure performs a post-order traversal of a nested join tree
5250
5420
    and materializes the row types of NATURAL/USING joins in a
5251
 
    bottom-up manner until it reaches the TableList elements that
 
5421
    bottom-up manner until it reaches the TABLE_LIST elements that
5252
5422
    represent the top-most NATURAL/USING joins. The procedure should be
5253
5423
    applied to each element of SELECT_LEX::top_join_list (i.e. to each
5254
5424
    top-level element of the FROM clause).
5264
5434
*/
5265
5435
 
5266
5436
static bool
5267
 
store_top_level_join_columns(THD *thd, TableList *table_ref,
5268
 
                             TableList *left_neighbor,
5269
 
                             TableList *right_neighbor)
 
5437
store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref,
 
5438
                             TABLE_LIST *left_neighbor,
 
5439
                             TABLE_LIST *right_neighbor)
5270
5440
{
5271
5441
  bool result= true;
5272
5442
 
5273
5443
  /* Call the procedure recursively for each nested table reference. */
5274
5444
  if (table_ref->nested_join)
5275
5445
  {
5276
 
    List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
5277
 
    TableList *same_level_left_neighbor= nested_it++;
5278
 
    TableList *same_level_right_neighbor= NULL;
 
5446
    List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list);
 
5447
    TABLE_LIST *same_level_left_neighbor= nested_it++;
 
5448
    TABLE_LIST *same_level_right_neighbor= NULL;
5279
5449
    /* Left/right-most neighbors, possibly at higher levels in the join tree. */
5280
 
    TableList *real_left_neighbor, *real_right_neighbor;
 
5450
    TABLE_LIST *real_left_neighbor, *real_right_neighbor;
5281
5451
 
5282
5452
    while (same_level_left_neighbor)
5283
5453
    {
5284
 
      TableList *cur_table_ref= same_level_left_neighbor;
 
5454
      TABLE_LIST *cur_table_ref= same_level_left_neighbor;
5285
5455
      same_level_left_neighbor= nested_it++;
5286
5456
      /*
5287
5457
        The order of RIGHT JOIN operands is reversed in 'join list' to
5299
5469
      {
5300
5470
        /* This can happen only for JOIN ... ON. */
5301
5471
        assert(table_ref->nested_join->join_list.elements == 2);
5302
 
        std::swap(same_level_left_neighbor, cur_table_ref);
 
5472
        swap_variables(TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
5303
5473
      }
5304
5474
 
5305
5475
      /*
5327
5497
  {
5328
5498
    assert(table_ref->nested_join &&
5329
5499
                table_ref->nested_join->join_list.elements == 2);
5330
 
    List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
 
5500
    List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
5331
5501
    /*
5332
5502
      Notice that the order of join operands depends on whether table_ref
5333
5503
      represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
5334
5504
      in inverted order.
5335
5505
     */
5336
 
    TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5337
 
    TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
 
5506
    TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
 
5507
    TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */
5338
5508
    List<String> *using_fields= table_ref->join_using_fields;
5339
 
    uint32_t found_using_fields;
 
5509
    uint found_using_fields;
5340
5510
 
5341
5511
    /*
5342
5512
      The two join operands were interchanged in the parser, change the order
5343
5513
      back for 'mark_common_columns'.
5344
5514
    */
5345
5515
    if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5346
 
      std::swap(table_ref_1, table_ref_2);
 
5516
      swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5347
5517
    if (mark_common_columns(thd, table_ref_1, table_ref_2,
5348
5518
                            using_fields, &found_using_fields))
5349
5519
      goto err;
5354
5524
      same as of an equivalent LEFT JOIN.
5355
5525
    */
5356
5526
    if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5357
 
      std::swap(table_ref_1, table_ref_2);
 
5527
      swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5358
5528
    if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5359
5529
                                         table_ref_2, using_fields,
5360
5530
                                         found_using_fields))
5371
5541
    /* Add a true condition to outer joins that have no common columns. */
5372
5542
    if (table_ref_2->outer_join &&
5373
5543
        !table_ref_1->on_expr && !table_ref_2->on_expr)
5374
 
      table_ref_2->on_expr= new Item_int((int64_t) 1,1);   /* Always true. */
 
5544
      table_ref_2->on_expr= new Item_int((longlong) 1,1);   /* Always true. */
5375
5545
 
5376
5546
    /* Change this table reference to become a leaf for name resolution. */
5377
5547
    if (left_neighbor)
5378
5548
    {
5379
 
      TableList *last_leaf_on_the_left;
 
5549
      TABLE_LIST *last_leaf_on_the_left;
5380
5550
      last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
5381
5551
      last_leaf_on_the_left->next_name_resolution_table= table_ref;
5382
5552
    }
5383
5553
    if (right_neighbor)
5384
5554
    {
5385
 
      TableList *first_leaf_on_the_right;
 
5555
      TABLE_LIST *first_leaf_on_the_right;
5386
5556
      first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
5387
5557
      table_ref->next_name_resolution_table= first_leaf_on_the_right;
5388
5558
    }
5421
5591
    false  OK
5422
5592
*/
5423
5593
static bool setup_natural_join_row_types(THD *thd,
5424
 
                                         List<TableList> *from_clause,
 
5594
                                         List<TABLE_LIST> *from_clause,
5425
5595
                                         Name_resolution_context *context)
5426
5596
{
5427
5597
  thd->where= "from clause";
5428
5598
  if (from_clause->elements == 0)
5429
5599
    return false; /* We come here in the case of UNIONs. */
5430
5600
 
5431
 
  List_iterator_fast<TableList> table_ref_it(*from_clause);
5432
 
  TableList *table_ref; /* Current table reference. */
 
5601
  List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause);
 
5602
  TABLE_LIST *table_ref; /* Current table reference. */
5433
5603
  /* Table reference to the left of the current. */
5434
 
  TableList *left_neighbor;
 
5604
  TABLE_LIST *left_neighbor;
5435
5605
  /* Table reference to the right of the current. */
5436
 
  TableList *right_neighbor= NULL;
 
5606
  TABLE_LIST *right_neighbor= NULL;
5437
5607
 
5438
5608
  /* Note that tables in the list are in reversed order */
5439
5609
  for (left_neighbor= table_ref_it++; left_neighbor ; )
5440
5610
  {
5441
5611
    table_ref= left_neighbor;
5442
5612
    left_neighbor= table_ref_it++;
5443
 
    if (store_top_level_join_columns(thd, table_ref,
5444
 
                                     left_neighbor, right_neighbor))
5445
 
      return true;
5446
 
    if (left_neighbor)
 
5613
    /* For stored procedures do not redo work if already done. */
 
5614
    if (context->select_lex->first_execution)
5447
5615
    {
5448
 
      TableList *first_leaf_on_the_right;
5449
 
      first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5450
 
      left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
 
5616
      if (store_top_level_join_columns(thd, table_ref,
 
5617
                                       left_neighbor, right_neighbor))
 
5618
        return true;
 
5619
      if (left_neighbor)
 
5620
      {
 
5621
        TABLE_LIST *first_leaf_on_the_right;
 
5622
        first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
 
5623
        left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
 
5624
      }
5451
5625
    }
5452
5626
    right_neighbor= table_ref;
5453
5627
  }
5471
5645
****************************************************************************/
5472
5646
 
5473
5647
int setup_wild(THD *thd,
5474
 
               TableList *tables __attribute__((unused)),
 
5648
               TABLE_LIST *tables __attribute__((__unused__)),
5475
5649
               List<Item> &fields,
5476
5650
               List<Item> *sum_func_list,
5477
 
               uint32_t wild_num)
 
5651
               uint wild_num)
5478
5652
{
5479
5653
  if (!wild_num)
5480
5654
    return(0);
5490
5664
        ((Item_field*) item)->field_name[0] == '*' &&
5491
5665
        !((Item_field*) item)->field)
5492
5666
    {
5493
 
      uint32_t elem= fields.elements;
 
5667
      uint elem= fields.elements;
5494
5668
      bool any_privileges= ((Item_field *) item)->any_privileges;
5495
5669
      Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
5496
5670
      if (subsel &&
5501
5675
 
5502
5676
          Item_int do not need fix_fields() because it is basic constant.
5503
5677
        */
5504
 
        it.replace(new Item_int("Not_used", (int64_t) 1,
 
5678
        it.replace(new Item_int("Not_used", (longlong) 1,
5505
5679
                                MY_INT64_NUM_DECIMAL_DIGITS));
5506
5680
      }
5507
5681
      else if (insert_fields(thd, ((Item_field*) item)->context,
5562
5736
    ref_pointer_array
5563
5737
  */
5564
5738
  if (ref_pointer_array)
5565
 
    memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
 
5739
    bzero(ref_pointer_array, sizeof(Item *) * fields.elements);
5566
5740
 
5567
5741
  Item **ref= ref_pointer_array;
5568
5742
  thd->lex->current_select->cur_pos_in_select_list= 0;
5603
5777
  RETURN pointer on pointer to next_leaf of last element
5604
5778
*/
5605
5779
 
5606
 
TableList **make_leaves_list(TableList **list, TableList *tables)
 
5780
TABLE_LIST **make_leaves_list(TABLE_LIST **list, TABLE_LIST *tables)
5607
5781
{
5608
 
  for (TableList *table= tables; table; table= table->next_local)
 
5782
  for (TABLE_LIST *table= tables; table; table= table->next_local)
5609
5783
  {
5610
5784
    {
5611
5785
      *list= table;
5644
5818
*/
5645
5819
 
5646
5820
bool setup_tables(THD *thd, Name_resolution_context *context,
5647
 
                  List<TableList> *from_clause, TableList *tables,
5648
 
                  TableList **leaves, bool select_insert)
 
5821
                  List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
 
5822
                  TABLE_LIST **leaves, bool select_insert)
5649
5823
{
5650
 
  uint32_t tablenr= 0;
 
5824
  uint tablenr= 0;
5651
5825
 
5652
5826
  assert ((select_insert && !tables->next_name_resolution_table) || !tables || 
5653
5827
               (context->table_list && context->first_name_resolution_table));
5655
5829
    this is used for INSERT ... SELECT.
5656
5830
    For select we setup tables except first (and its underlying tables)
5657
5831
  */
5658
 
  TableList *first_select_table= (select_insert ?
 
5832
  TABLE_LIST *first_select_table= (select_insert ?
5659
5833
                                   tables->next_local:
5660
5834
                                   0);
5661
5835
  if (!(*leaves))
5662
5836
    make_leaves_list(leaves, tables);
5663
5837
 
5664
 
  TableList *table_list;
 
5838
  TABLE_LIST *table_list;
5665
5839
  for (table_list= *leaves;
5666
5840
       table_list;
5667
5841
       table_list= table_list->next_leaf, tablenr++)
5668
5842
  {
5669
 
    Table *table= table_list->table;
 
5843
    TABLE *table= table_list->table;
5670
5844
    table->pos_in_table_list= table_list;
5671
5845
    if (first_select_table &&
5672
5846
        table_list->top_table() == first_select_table)
5718
5892
*/
5719
5893
bool setup_tables_and_check_access(THD *thd, 
5720
5894
                                   Name_resolution_context *context,
5721
 
                                   List<TableList> *from_clause,
5722
 
                                   TableList *tables,
5723
 
                                   TableList **leaves,
 
5895
                                   List<TABLE_LIST> *from_clause,
 
5896
                                   TABLE_LIST *tables,
 
5897
                                   TABLE_LIST **leaves,
5724
5898
                                   bool select_insert)
5725
5899
{
5726
 
  TableList *leaves_tmp= NULL;
 
5900
  TABLE_LIST *leaves_tmp= NULL;
5727
5901
  bool first_table= true;
5728
5902
 
5729
5903
  if (setup_tables(thd, context, from_clause, tables,
5735
5909
 
5736
5910
  for (; leaves_tmp; leaves_tmp= leaves_tmp->next_leaf)
5737
5911
  {
 
5912
    if (leaves_tmp->belong_to_view)
 
5913
    {
 
5914
      return true;
 
5915
    }
5738
5916
    first_table= 0;
5739
5917
  }
5740
5918
  return false;
5755
5933
     1  error
5756
5934
*/
5757
5935
 
5758
 
bool get_key_map_from_key_list(key_map *map, Table *table,
 
5936
bool get_key_map_from_key_list(key_map *map, TABLE *table,
5759
5937
                               List<String> *index_list)
5760
5938
{
5761
5939
  List_iterator_fast<String> it(*index_list);
5762
5940
  String *name;
5763
 
  uint32_t pos;
 
5941
  uint pos;
5764
5942
 
5765
5943
  map->clear_all();
5766
5944
  while ((name=it++))
5802
5980
bool
5803
5981
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5804
5982
              const char *table_name, List_iterator<Item> *it,
5805
 
              bool any_privileges __attribute__((unused)))
 
5983
              bool any_privileges __attribute__((__unused__)))
5806
5984
{
5807
5985
  Field_iterator_table_ref field_iterator;
5808
5986
  bool found;
5827
6005
    else treat natural joins as leaves and do not iterate over their underlying
5828
6006
    tables.
5829
6007
  */
5830
 
  for (TableList *tables= (table_name ? context->table_list :
 
6008
  for (TABLE_LIST *tables= (table_name ? context->table_list :
5831
6009
                            context->first_name_resolution_table);
5832
6010
       tables;
5833
6011
       tables= (table_name ? tables->next_local :
5835
6013
       )
5836
6014
  {
5837
6015
    Field *field;
5838
 
    Table *table= tables->table;
 
6016
    TABLE *table= tables->table;
5839
6017
 
5840
6018
    assert(tables->is_leaf_for_name_resolution());
5841
6019
 
5884
6062
        }
5885
6063
        if (tables->is_natural_join)
5886
6064
        {
5887
 
          Table *field_table;
 
6065
          TABLE *field_table;
5888
6066
          /*
5889
6067
            In this case we are sure that the column ref will not be created
5890
6068
            because it was already created and stored with the natural join.
5951
6129
    false if all is OK
5952
6130
*/
5953
6131
 
5954
 
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
5955
 
                TableList *leaves,
 
6132
int setup_conds(THD *thd, TABLE_LIST *tables __attribute__((__unused__)),
 
6133
                TABLE_LIST *leaves,
5956
6134
                COND **conds)
5957
6135
{
5958
6136
  SELECT_LEX *select_lex= thd->lex->current_select;
5959
 
  TableList *table= NULL;       // For HP compilers
 
6137
  TABLE_LIST *table= NULL;      // For HP compilers
5960
6138
  void *save_thd_marker= thd->thd_marker;
5961
6139
  /*
5962
6140
    it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
5990
6168
  */
5991
6169
  for (table= leaves; table; table= table->next_leaf)
5992
6170
  {
5993
 
    TableList *embedded; /* The table at the current level of nesting. */
5994
 
    TableList *embedding= table; /* The parent nested table reference. */
 
6171
    TABLE_LIST *embedded; /* The table at the current level of nesting. */
 
6172
    TABLE_LIST *embedding= table; /* The parent nested table reference. */
5995
6173
    do
5996
6174
    {
5997
6175
      embedded= embedding;
6054
6232
  List_iterator_fast<Item> f(fields),v(values);
6055
6233
  Item *value, *fld;
6056
6234
  Item_field *field;
6057
 
  Table *table= 0;
 
6235
  TABLE *table= 0;
6058
6236
 
6059
6237
  /*
6060
6238
    Reset the table->auto_increment_field_not_null as it is valid for
6124
6302
 
6125
6303
bool
6126
6304
fill_record(THD *thd, Field **ptr, List<Item> &values,
6127
 
            bool ignore_errors __attribute__((unused)))
 
6305
            bool ignore_errors __attribute__((__unused__)))
6128
6306
{
6129
6307
  List_iterator_fast<Item> v(values);
6130
6308
  Item *value;
6131
 
  Table *table= 0;
 
6309
  TABLE *table= 0;
6132
6310
 
6133
6311
  Field *field;
6134
6312
  /*
6162
6340
}
6163
6341
 
6164
6342
 
6165
 
bool mysql_rm_tmp_tables(void)
 
6343
my_bool mysql_rm_tmp_tables(void)
6166
6344
{
6167
 
  uint32_t i, idx;
 
6345
  uint i, idx;
6168
6346
  char  filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6169
6347
  MY_DIR *dirp;
6170
6348
  FILEINFO *file;
6194
6372
                                   (file->name[1] == '.' &&  !file->name[2])))
6195
6373
        continue;
6196
6374
 
6197
 
      if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
 
6375
      if (!bcmp((uchar*) file->name, (uchar*) tmp_file_prefix,
 
6376
                tmp_file_prefix_length))
6198
6377
      {
6199
6378
        char *ext= fn_ext(file->name);
6200
 
        uint32_t ext_len= strlen(ext);
6201
 
        uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
 
6379
        uint ext_len= strlen(ext);
 
6380
        uint filePath_len= snprintf(filePath, sizeof(filePath),
6202
6381
                                    "%s%c%s", tmpdir, FN_LIBCHAR,
6203
6382
                                    file->name);
6204
 
        if (!memcmp(reg_ext, ext, ext_len))
 
6383
        if (!bcmp((uchar*) reg_ext, (uchar*) ext, ext_len))
6205
6384
        {
6206
6385
          handler *handler_file= 0;
6207
6386
          /* We should cut file extention before deleting of table */
6222
6401
          So we hide error messages which happnes during deleting of these
6223
6402
          files(MYF(0)).
6224
6403
        */
6225
 
        my_delete(filePath, MYF(0)); 
 
6404
        VOID(my_delete(filePath, MYF(0))); 
6226
6405
      }
6227
6406
    }
6228
6407
    my_dirend(dirp);
6253
6432
 
6254
6433
void remove_db_from_cache(const char *db)
6255
6434
{
6256
 
  for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
6435
  for (uint idx=0 ; idx < open_cache.records ; idx++)
6257
6436
  {
6258
 
    Table *table=(Table*) hash_element(&open_cache,idx);
 
6437
    TABLE *table=(TABLE*) hash_element(&open_cache,idx);
6259
6438
    if (!strcmp(table->s->db.str, db))
6260
6439
    {
6261
6440
      table->s->version= 0L;                    /* Free when thread is ready */
6264
6443
    }
6265
6444
  }
6266
6445
  while (unused_tables && !unused_tables->s->version)
6267
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6446
    VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6268
6447
}
6269
6448
 
6270
6449
 
6280
6459
{
6281
6460
  (void) pthread_mutex_lock(&LOCK_open);
6282
6461
  while (unused_tables)
6283
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6462
    hash_delete(&open_cache,(uchar*) unused_tables);
6284
6463
  (void) pthread_mutex_unlock(&LOCK_open);
6285
6464
}
6286
6465
 
6301
6480
*/
6302
6481
 
6303
6482
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6304
 
                             uint32_t flags)
 
6483
                             uint flags)
6305
6484
{
6306
6485
  char key[MAX_DBKEY_LENGTH];
6307
 
  uint32_t key_length;
6308
 
  Table *table;
 
6486
  uint key_length;
 
6487
  TABLE *table;
6309
6488
  TABLE_SHARE *share;
6310
6489
  bool result= 0, signalled= 0;
6311
6490
 
6312
 
  key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
 
6491
  key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
6313
6492
  for (;;)
6314
6493
  {
6315
6494
    HASH_SEARCH_STATE state;
6316
6495
    result= signalled= 0;
6317
6496
 
6318
 
    for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
 
6497
    for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
6319
6498
                                    &state);
6320
6499
         table;
6321
 
         table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
 
6500
         table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
6322
6501
                                   &state))
6323
6502
    {
6324
6503
      THD *in_use;
6340
6519
        {
6341
6520
          result=1;
6342
6521
        }
 
6522
        /* Kill delayed insert threads */
 
6523
        if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) &&
 
6524
            ! in_use->killed)
 
6525
        {
 
6526
          in_use->killed= THD::KILL_CONNECTION;
 
6527
          pthread_mutex_lock(&in_use->mysys_var->mutex);
 
6528
          if (in_use->mysys_var->current_cond)
 
6529
          {
 
6530
            pthread_mutex_lock(in_use->mysys_var->current_mutex);
 
6531
            signalled= 1;
 
6532
            pthread_cond_broadcast(in_use->mysys_var->current_cond);
 
6533
            pthread_mutex_unlock(in_use->mysys_var->current_mutex);
 
6534
          }
 
6535
          pthread_mutex_unlock(&in_use->mysys_var->mutex);
 
6536
        }
6343
6537
        /*
6344
6538
          Now we must abort all tables locks used by this thread
6345
6539
          as the thread may be waiting to get a lock for another table.
6349
6543
          open_tables list. Aborting the MERGE lock after a child was
6350
6544
          closed and before the parent is closed would be fatal.
6351
6545
        */
6352
 
        for (Table *thd_table= in_use->open_tables;
 
6546
        for (TABLE *thd_table= in_use->open_tables;
6353
6547
             thd_table ;
6354
6548
             thd_table= thd_table->next)
6355
6549
        {
6362
6556
        result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6363
6557
    }
6364
6558
    while (unused_tables && !unused_tables->s->version)
6365
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6559
      VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6366
6560
 
6367
6561
    /* Remove table from table definition cache if it's not in use */
6368
 
    if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
 
6562
    if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6369
6563
                                           key_length)))
6370
6564
    {
6371
6565
      share->version= 0;                          // Mark for delete
6372
6566
      if (share->ref_count == 0)
6373
6567
      {
6374
6568
        pthread_mutex_lock(&share->mutex);
6375
 
        hash_delete(&table_def_cache, (unsigned char*) share);
 
6569
        VOID(hash_delete(&table_def_cache, (uchar*) share));
6376
6570
      }
6377
6571
    }
6378
6572
 
6395
6589
            It can happen that another thread has opened the
6396
6590
            table but has not yet locked any table at all. Since
6397
6591
            it can be locked waiting for a table that our thread
6398
 
            has done LOCK Table x WRITE on previously, we need to
 
6592
            has done LOCK TABLE x WRITE on previously, we need to
6399
6593
            ensure that the thread actually hears our signal
6400
6594
            before we go to sleep. Thus we wait for a short time
6401
6595
            and then we retry another loop in the
6418
6612
{
6419
6613
  return a->length == b->length && !strncmp(a->str, b->str, a->length);
6420
6614
}
 
6615
 
 
6616
 
 
6617
/*
 
6618
  Open and lock system tables for read.
 
6619
 
 
6620
  SYNOPSIS
 
6621
    open_system_tables_for_read()
 
6622
      thd         Thread context.
 
6623
      table_list  List of tables to open.
 
6624
      backup      Pointer to Open_tables_state instance where
 
6625
                  information about currently open tables will be
 
6626
                  saved, and from which will be restored when we will
 
6627
                  end work with system tables.
 
6628
 
 
6629
  NOTES
 
6630
    Thanks to restrictions which we put on opening and locking of
 
6631
    system tables for writing, we can open and lock them for reading
 
6632
    even when we already have some other tables open and locked.  One
 
6633
    must call close_system_tables() to close systems tables opened
 
6634
    with this call.
 
6635
 
 
6636
  RETURN
 
6637
    false   Success
 
6638
    true    Error
 
6639
*/
 
6640
 
 
6641
bool
 
6642
open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
 
6643
                            Open_tables_state *backup)
 
6644
{
 
6645
  thd->reset_n_backup_open_tables_state(backup);
 
6646
 
 
6647
  uint count= 0;
 
6648
  bool not_used;
 
6649
  for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
 
6650
  {
 
6651
    TABLE *table= open_table(thd, tables, thd->mem_root, &not_used,
 
6652
                             MYSQL_LOCK_IGNORE_FLUSH);
 
6653
    if (!table)
 
6654
      goto error;
 
6655
 
 
6656
    assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
6657
 
 
6658
    table->use_all_columns();
 
6659
    table->reginfo.lock_type= tables->lock_type;
 
6660
    tables->table= table;
 
6661
    count++;
 
6662
  }
 
6663
 
 
6664
  {
 
6665
    TABLE **list= (TABLE**) thd->alloc(sizeof(TABLE*) * count);
 
6666
    TABLE **ptr= list;
 
6667
    for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
 
6668
      *(ptr++)= tables->table;
 
6669
 
 
6670
    thd->lock= mysql_lock_tables(thd, list, count,
 
6671
                                 MYSQL_LOCK_IGNORE_FLUSH, &not_used);
 
6672
  }
 
6673
  if (thd->lock)
 
6674
    return(false);
 
6675
 
 
6676
error:
 
6677
  close_system_tables(thd, backup);
 
6678
 
 
6679
  return(true);
 
6680
}
 
6681
 
 
6682
 
 
6683
/*
 
6684
  Close system tables, opened with open_system_tables_for_read().
 
6685
 
 
6686
  SYNOPSIS
 
6687
    close_system_tables()
 
6688
      thd     Thread context
 
6689
      backup  Pointer to Open_tables_state instance which holds
 
6690
              information about tables which were open before we
 
6691
              decided to access system tables.
 
6692
*/
 
6693
 
 
6694
void
 
6695
close_system_tables(THD *thd, Open_tables_state *backup)
 
6696
{
 
6697
  close_thread_tables(thd);
 
6698
  thd->restore_backup_open_tables_state(backup);
 
6699
}
 
6700
 
 
6701
 
 
6702
/*
 
6703
  Open and lock one system table for update.
 
6704
 
 
6705
  SYNOPSIS
 
6706
    open_system_table_for_update()
 
6707
      thd        Thread context.
 
6708
      one_table  Table to open.
 
6709
 
 
6710
  NOTES
 
6711
    Table opened with this call should closed using close_thread_tables().
 
6712
 
 
6713
  RETURN
 
6714
    0   Error
 
6715
    #   Pointer to TABLE object of system table
 
6716
*/
 
6717
 
 
6718
TABLE *
 
6719
open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
 
6720
{
 
6721
  TABLE *table= open_ltable(thd, one_table, one_table->lock_type, 0);
 
6722
  if (table)
 
6723
  {
 
6724
    assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
6725
    table->use_all_columns();
 
6726
  }
 
6727
 
 
6728
  return(table);
 
6729
}
 
6730
 
6421
6731
/**
6422
6732
  @} (end of group Data_Dictionary)
6423
6733
*/