~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_base.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 22:08:52 UTC
  • mto: This revision was merged to the branch mainline in revision 77.
  • Revision ID: monty@inaugust.com-20080705220852-cqd9t6tfkhvlcf73
Removed HAVE_LONG_LONG, as this is now assumed.

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>
23
18
 
24
 
#if TIME_WITH_SYS_TIME
25
 
# include <sys/time.h>
26
 
# include <time.h>
27
 
#else
28
 
# if HAVE_SYS_TIME_H
29
 
#  include <sys/time.h>
30
 
# else
31
 
#  include <time.h>
32
 
# endif
33
 
#endif
 
19
#include "mysql_priv.h"
 
20
#include "sql_select.h"
 
21
#include <m_ctype.h>
 
22
#include <my_dir.h>
 
23
#include <hash.h>
34
24
 
35
25
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
36
26
 
37
27
/**
38
 
  return true if the table was created explicitly.
39
 
*/
40
 
inline bool is_user_table(Table * table)
41
 
{
42
 
  const char *name= table->s->table_name.str;
43
 
  return strncmp(name, tmp_file_prefix, tmp_file_prefix_length);
44
 
}
45
 
 
46
 
 
47
 
/**
48
28
  @defgroup Data_Dictionary Data Dictionary
49
29
  @{
50
30
*/
51
 
Table *unused_tables;                           /* Used by mysql_test */
 
31
TABLE *unused_tables;                           /* Used by mysql_test */
52
32
HASH open_cache;                                /* Used by mysql_test */
53
33
static HASH table_def_cache;
54
34
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
55
35
static pthread_mutex_t LOCK_table_share;
56
36
static bool table_def_inited= 0;
57
37
 
58
 
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
59
 
                             const char *alias,
60
 
                             char *cache_key, uint32_t cache_key_length);
61
 
static void free_cache_entry(Table *entry);
62
 
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,
63
44
                                 bool send_refresh);
64
45
 
65
46
 
66
 
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
67
 
                                  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)))
68
49
{
69
 
  Table *entry=(Table*) record;
 
50
  TABLE *entry=(TABLE*) record;
70
51
  *length= entry->s->table_cache_key.length;
71
 
  return (unsigned char*) entry->s->table_cache_key.str;
 
52
  return (uchar*) entry->s->table_cache_key.str;
72
53
}
73
54
 
74
55
 
76
57
{
77
58
  return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
78
59
                   0, 0, table_cache_key,
79
 
                   (hash_free_key) free_cache_entry, 0);
 
60
                   (hash_free_key) free_cache_entry, 0) != 0;
80
61
}
81
62
 
82
63
void table_cache_free(void)
83
64
{
 
65
  DBUG_ENTER("table_cache_free");
84
66
  if (table_def_inited)
85
67
  {
86
68
    close_cached_tables(NULL, NULL, false, false, false);
87
69
    if (!open_cache.records)                    // Safety first
88
70
      hash_free(&open_cache);
89
71
  }
90
 
  return;
 
72
  DBUG_VOID_RETURN;
91
73
}
92
74
 
93
 
uint32_t cached_open_tables(void)
 
75
uint cached_open_tables(void)
94
76
{
95
77
  return open_cache.records;
96
78
}
97
79
 
 
80
 
 
81
#ifdef EXTRA_DEBUG
 
82
static void check_unused(void)
 
83
{
 
84
  uint count= 0, open_files= 0, idx= 0;
 
85
  TABLE *cur_link,*start_link;
 
86
 
 
87
  if ((start_link=cur_link=unused_tables))
 
88
  {
 
89
    do
 
90
    {
 
91
      if (cur_link != cur_link->next->prev || cur_link != cur_link->prev->next)
 
92
      {
 
93
        DBUG_PRINT("error",("Unused_links aren't linked properly")); /* purecov: inspected */
 
94
        return; /* purecov: inspected */
 
95
      }
 
96
    } while (count++ < open_cache.records &&
 
97
             (cur_link=cur_link->next) != start_link);
 
98
    if (cur_link != start_link)
 
99
    {
 
100
      DBUG_PRINT("error",("Unused_links aren't connected")); /* purecov: inspected */
 
101
    }
 
102
  }
 
103
  for (idx=0 ; idx < open_cache.records ; idx++)
 
104
  {
 
105
    TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
 
106
    if (!entry->in_use)
 
107
      count--;
 
108
    if (entry->file)
 
109
      open_files++;
 
110
  }
 
111
  if (count != 0)
 
112
  {
 
113
    DBUG_PRINT("error",("Unused_links doesn't match open_cache: diff: %d", /* purecov: inspected */
 
114
                        count)); /* purecov: inspected */
 
115
  }
 
116
 
 
117
#ifdef NOT_SAFE_FOR_REPAIR
 
118
  /*
 
119
    check that open cache and table definition cache has same number of
 
120
    aktive tables
 
121
  */
 
122
  count= 0;
 
123
  for (idx=0 ; idx < table_def_cache.records ; idx++)
 
124
  {
 
125
    TABLE_SHARE *entry= (TABLE_SHARE*) hash_element(&table_def_cache,idx);
 
126
    count+= entry->ref_count;
 
127
  }
 
128
  if (count != open_files)
 
129
  {
 
130
    DBUG_PRINT("error", ("table_def ref_count: %u  open_cache: %u",
 
131
                         count, open_files));
 
132
    DBUG_ASSERT(count == open_files);
 
133
  }
 
134
#endif
 
135
}
 
136
#else
 
137
#define check_unused()
 
138
#endif
 
139
 
 
140
 
98
141
/*
99
142
  Create a table cache key
100
143
 
120
163
    Length of key
121
164
*/
122
165
 
123
 
uint32_t create_table_def_key(THD *thd, char *key, TableList *table_list,
 
166
uint create_table_def_key(THD *thd, char *key, TABLE_LIST *table_list,
124
167
                          bool tmp_table)
125
168
{
126
 
  uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
 
169
  uint key_length= (uint) (strmov(strmov(key, table_list->db)+1,
127
170
                                  table_list->table_name)-key)+1;
128
171
  if (tmp_table)
129
172
  {
140
183
  Functions to handle table definition cach (TABLE_SHARE)
141
184
*****************************************************************************/
142
185
 
143
 
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
144
 
                                bool not_used __attribute__((unused)))
 
186
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
 
187
                               my_bool not_used __attribute__((unused)))
145
188
{
146
189
  TABLE_SHARE *entry=(TABLE_SHARE*) record;
147
190
  *length= entry->table_cache_key.length;
148
 
  return (unsigned char*) entry->table_cache_key.str;
 
191
  return (uchar*) entry->table_cache_key.str;
149
192
}
150
193
 
151
194
 
152
195
static void table_def_free_entry(TABLE_SHARE *share)
153
196
{
 
197
  DBUG_ENTER("table_def_free_entry");
154
198
  if (share->prev)
155
199
  {
156
200
    /* remove from old_unused_share list */
160
204
    pthread_mutex_unlock(&LOCK_table_share);
161
205
  }
162
206
  free_table_share(share);
163
 
  return;
 
207
  DBUG_VOID_RETURN;
164
208
}
165
209
 
166
210
 
173
217
 
174
218
  return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
175
219
                   0, 0, table_def_key,
176
 
                   (hash_free_key) table_def_free_entry, 0);
 
220
                   (hash_free_key) table_def_free_entry, 0) != 0;
177
221
}
178
222
 
179
223
 
180
224
void table_def_free(void)
181
225
{
 
226
  DBUG_ENTER("table_def_free");
182
227
  if (table_def_inited)
183
228
  {
184
229
    table_def_inited= 0;
185
230
    pthread_mutex_destroy(&LOCK_table_share);
186
231
    hash_free(&table_def_cache);
187
232
  }
188
 
  return;
 
233
  DBUG_VOID_RETURN;
189
234
}
190
235
 
191
236
 
192
 
uint32_t cached_table_definitions(void)
 
237
uint cached_table_definitions(void)
193
238
{
194
239
  return table_def_cache.records;
195
240
}
220
265
   #  Share for table
221
266
*/
222
267
 
223
 
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
224
 
                             uint32_t key_length, uint32_t db_flags, int *error)
 
268
TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key,
 
269
                             uint key_length, uint db_flags, int *error)
225
270
{
226
271
  TABLE_SHARE *share;
 
272
  DBUG_ENTER("get_table_share");
227
273
 
228
274
  *error= 0;
229
275
 
230
276
  /* Read table definition from cache */
231
 
  if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
 
277
  if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
232
278
                                         key_length)))
233
279
    goto found;
234
280
 
235
281
  if (!(share= alloc_table_share(table_list, key, key_length)))
236
282
  {
237
 
    return(0);
 
283
    DBUG_RETURN(0);
238
284
  }
239
285
 
240
286
  /*
258
304
   */
259
305
  assign_new_table_id(share);
260
306
 
261
 
  if (my_hash_insert(&table_def_cache, (unsigned char*) share))
 
307
  if (my_hash_insert(&table_def_cache, (uchar*) share))
262
308
  {
263
309
    free_table_share(share);
264
 
    return(0);                          // return error
 
310
    DBUG_RETURN(0);                             // return error
265
311
  }
266
312
  if (open_table_def(thd, share, db_flags))
267
313
  {
268
314
    *error= share->error;
269
 
    (void) hash_delete(&table_def_cache, (unsigned char*) share);
270
 
    return(0);
 
315
    (void) hash_delete(&table_def_cache, (uchar*) share);
 
316
    DBUG_RETURN(0);
271
317
  }
272
318
  share->ref_count++;                           // Mark in use
 
319
  DBUG_PRINT("exit", ("share: 0x%lx  ref_count: %u",
 
320
                      (ulong) share, share->ref_count));
273
321
  (void) pthread_mutex_unlock(&share->mutex);
274
 
  return(share);
 
322
  DBUG_RETURN(share);
275
323
 
276
324
found:
277
325
  /* 
286
334
    /* Table definition contained an error */
287
335
    open_table_error(share, share->error, share->open_errno, share->errarg);
288
336
    (void) pthread_mutex_unlock(&share->mutex);
289
 
    return(0);
 
337
    DBUG_RETURN(0);
290
338
  }
291
339
 
292
340
  if (!share->ref_count++ && share->prev)
295
343
      Share was not used before and it was in the old_unused_share list
296
344
      Unlink share from this list
297
345
    */
 
346
    DBUG_PRINT("info", ("Unlinking from not used list"));
298
347
    pthread_mutex_lock(&LOCK_table_share);
299
348
    *share->prev= share->next;
300
349
    share->next->prev= share->prev;
309
358
         oldest_unused_share->next)
310
359
  {
311
360
    pthread_mutex_lock(&oldest_unused_share->mutex);
312
 
    hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
 
361
    VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
313
362
  }
314
363
 
315
 
  return(share);
 
364
  DBUG_PRINT("exit", ("share: 0x%lx  ref_count: %u",
 
365
                      (ulong) share, share->ref_count));
 
366
  DBUG_RETURN(share);
316
367
}
317
368
 
318
369
 
323
374
*/
324
375
 
325
376
static TABLE_SHARE
326
 
*get_table_share_with_create(THD *thd, TableList *table_list,
327
 
                             char *key, uint32_t key_length,
328
 
                             uint32_t db_flags, int *error)
 
377
*get_table_share_with_create(THD *thd, TABLE_LIST *table_list,
 
378
                             char *key, uint key_length,
 
379
                             uint db_flags, int *error)
329
380
{
330
381
  TABLE_SHARE *share;
331
382
  int tmp;
 
383
  DBUG_ENTER("get_table_share_with_create");
332
384
 
333
385
  share= get_table_share(thd, table_list, key, key_length, db_flags, error);
334
386
  /*
348
400
    Finally, if share is still NULL, it's a real error and we need
349
401
    to abort.
350
402
 
351
 
    @todo Rework alternative ways to deal with ER_NO_SUCH Table.
 
403
    @todo Rework alternative ways to deal with ER_NO_SUCH TABLE.
352
404
  */
353
405
  if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
354
406
 
355
 
    return(share);
 
407
    DBUG_RETURN(share);
356
408
 
357
409
  /* Table didn't exist. Check if some engine can provide it */
358
410
  if ((tmp= ha_create_table_from_engine(thd, table_list->db,
359
411
                                        table_list->table_name)) < 0)
360
 
    return(0);
361
 
 
 
412
  {
 
413
    /*
 
414
      No such table in any engine.
 
415
      Hide "Table doesn't exist" errors if the table belongs to a view.
 
416
      The check for thd->is_error() is necessary to not push an
 
417
      unwanted error in case of pre-locking, which silences
 
418
      "no such table" errors.
 
419
      @todo Rework the alternative ways to deal with ER_NO_SUCH TABLE.
 
420
    */
 
421
    if (thd->is_error() && table_list->belong_to_view)
 
422
    {
 
423
      thd->clear_error();
 
424
      my_error(ER_VIEW_INVALID, MYF(0), "", "");
 
425
    }
 
426
    DBUG_RETURN(0);
 
427
  }
362
428
  if (tmp)
363
429
  {
364
430
    /* Give right error message */
365
431
    thd->clear_error();
 
432
    DBUG_PRINT("error", ("Discovery of %s/%s failed", table_list->db,
 
433
                         table_list->table_name));
366
434
    my_printf_error(ER_UNKNOWN_ERROR,
367
435
                    "Failed to open '%-.64s', error while "
368
436
                    "unpacking from engine",
369
437
                    MYF(0), table_list->table_name);
370
 
    return(0);
 
438
    DBUG_RETURN(0);
371
439
  }
372
440
  /* Table existed in engine. Let's open it */
373
 
  drizzle_reset_errors(thd, 1);                   // Clear warnings
 
441
  mysql_reset_errors(thd, 1);                   // Clear warnings
374
442
  thd->clear_error();                           // Clear error message
375
 
  return(get_table_share(thd, table_list, key, key_length,
 
443
  DBUG_RETURN(get_table_share(thd, table_list, key, key_length,
376
444
                              db_flags, error));
377
445
}
378
446
 
398
466
     that the table is deleted or the thread is killed.
399
467
*/
400
468
 
401
 
void release_table_share(TABLE_SHARE *share,
402
 
                         enum release_type type __attribute__((unused)))
 
469
void release_table_share(TABLE_SHARE *share, enum release_type type)
403
470
{
404
471
  bool to_be_deleted= 0;
 
472
  DBUG_ENTER("release_table_share");
 
473
  DBUG_PRINT("enter",
 
474
             ("share: 0x%lx  table: %s.%s  ref_count: %u  version: %lu",
 
475
              (ulong) share, share->db.str, share->table_name.str,
 
476
              share->ref_count, share->version));
405
477
 
406
478
  safe_mutex_assert_owner(&LOCK_open);
407
479
 
413
485
    else
414
486
    {
415
487
      /* Link share last in used_table_share list */
416
 
      assert(share->next == 0);
 
488
      DBUG_PRINT("info",("moving share to unused list"));
 
489
 
 
490
      DBUG_ASSERT(share->next == 0);
417
491
      pthread_mutex_lock(&LOCK_table_share);
418
492
      share->prev= end_of_unused_share.prev;
419
493
      *end_of_unused_share.prev= share;
427
501
 
428
502
  if (to_be_deleted)
429
503
  {
430
 
    hash_delete(&table_def_cache, (unsigned char*) share);
431
 
    return;
 
504
    DBUG_PRINT("info", ("Deleting share"));
 
505
    hash_delete(&table_def_cache, (uchar*) share);
 
506
    DBUG_VOID_RETURN;
432
507
  }
433
508
  pthread_mutex_unlock(&share->mutex);
434
 
  return;
 
509
  DBUG_VOID_RETURN;
435
510
}
436
511
 
437
512
 
451
526
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
452
527
{
453
528
  char key[NAME_LEN*2+2];
454
 
  TableList table_list;
455
 
  uint32_t key_length;
 
529
  TABLE_LIST table_list;
 
530
  uint key_length;
456
531
  safe_mutex_assert_owner(&LOCK_open);
457
532
 
458
533
  table_list.db= (char*) db;
459
534
  table_list.table_name= (char*) table_name;
460
535
  key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
461
 
  return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
 
536
  return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
462
537
}  
463
538
 
464
539
 
480
555
*/
481
556
 
482
557
 
483
 
void close_handle_and_leave_table_as_lock(Table *table)
 
558
void close_handle_and_leave_table_as_lock(TABLE *table)
484
559
{
485
560
  TABLE_SHARE *share, *old_share= table->s;
486
561
  char *key_buff;
487
562
  MEM_ROOT *mem_root= &table->mem_root;
 
563
  DBUG_ENTER("close_handle_and_leave_table_as_lock");
488
564
 
489
 
  assert(table->db_stat);
 
565
  DBUG_ASSERT(table->db_stat);
490
566
 
491
567
  /*
492
568
    Make a local copy of the table share and free the current one.
498
574
                       &key_buff, old_share->table_cache_key.length,
499
575
                       NULL))
500
576
  {
501
 
    memset(share, 0, sizeof(*share));
 
577
    bzero((char*) share, sizeof(*share));
502
578
    share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
503
579
                               old_share->table_cache_key.length);
504
580
    share->tmp_table= INTERNAL_TMP_TABLE;       // for intern_close_table()
510
586
  table->s= share;
511
587
  table->file->change_table_ptr(table, table->s);
512
588
 
513
 
  return;
 
589
  DBUG_VOID_RETURN;
514
590
}
515
591
 
516
592
 
533
609
    #           Pointer to list of names of open tables.
534
610
*/
535
611
 
536
 
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
537
 
                                  const char *db, const char *wild)
 
612
OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild)
538
613
{
539
614
  int result = 0;
540
 
  OPEN_TableList **start_list, *open_list;
541
 
  TableList table_list;
 
615
  OPEN_TABLE_LIST **start_list, *open_list;
 
616
  TABLE_LIST table_list;
 
617
  DBUG_ENTER("list_open_tables");
542
618
 
543
 
  pthread_mutex_lock(&LOCK_open);
544
 
  memset(&table_list, 0, sizeof(table_list));
 
619
  VOID(pthread_mutex_lock(&LOCK_open));
 
620
  bzero((char*) &table_list,sizeof(table_list));
545
621
  start_list= &open_list;
546
622
  open_list=0;
547
623
 
548
 
  for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
 
624
  for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
549
625
  {
550
 
    OPEN_TableList *table;
551
 
    Table *entry=(Table*) hash_element(&open_cache,idx);
 
626
    OPEN_TABLE_LIST *table;
 
627
    TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
552
628
    TABLE_SHARE *share= entry->s;
553
629
 
554
630
    if (db && my_strcasecmp(system_charset_info, db, share->db.str))
575
651
    }
576
652
    if (table)
577
653
      continue;
578
 
    if (!(*start_list = (OPEN_TableList *)
 
654
    if (!(*start_list = (OPEN_TABLE_LIST *)
579
655
          sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
580
656
    {
581
657
      open_list=0;                              // Out of memory
582
658
      break;
583
659
    }
584
 
    my_stpcpy((*start_list)->table=
585
 
           my_stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
 
660
    strmov((*start_list)->table=
 
661
           strmov(((*start_list)->db= (char*) ((*start_list)+1)),
586
662
                  share->db.str)+1,
587
663
           share->table_name.str);
588
664
    (*start_list)->in_use= entry->in_use ? 1 : 0;
590
666
    start_list= &(*start_list)->next;
591
667
    *start_list=0;
592
668
  }
593
 
  pthread_mutex_unlock(&LOCK_open);
594
 
  return(open_list);
 
669
  VOID(pthread_mutex_unlock(&LOCK_open));
 
670
  DBUG_RETURN(open_list);
595
671
}
596
672
 
597
673
/*****************************************************************************
599
675
 ****************************************************************************/
600
676
 
601
677
 
602
 
void intern_close_table(Table *table)
 
678
void intern_close_table(TABLE *table)
603
679
{                                               // Free all structures
 
680
  DBUG_ENTER("intern_close_table");
 
681
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx",
 
682
                        table->s ? table->s->db.str : "?",
 
683
                        table->s ? table->s->table_name.str : "?",
 
684
                        (long) table));
 
685
 
604
686
  free_io_cache(table);
605
687
  if (table->file)                              // Not true if name lock
606
 
    closefrm(table, 1);                 // close file
607
 
  return;
 
688
    VOID(closefrm(table, 1));                   // close file
 
689
  DBUG_VOID_RETURN;
608
690
}
609
691
 
610
692
/*
618
700
    We need to have a lock on LOCK_open when calling this
619
701
*/
620
702
 
621
 
static void free_cache_entry(Table *table)
 
703
static void free_cache_entry(TABLE *table)
622
704
{
 
705
  DBUG_ENTER("free_cache_entry");
 
706
 
623
707
  intern_close_table(table);
624
708
  if (!table->in_use)
625
709
  {
631
715
      if (table == unused_tables)
632
716
        unused_tables=0;
633
717
    }
 
718
    check_unused();                             // consisty check
634
719
  }
635
 
  free((unsigned char*) table);
636
 
  return;
 
720
  my_free((uchar*) table,MYF(0));
 
721
  DBUG_VOID_RETURN;
637
722
}
638
723
 
639
724
/* Free resources allocated by filesort() and read_record() */
640
725
 
641
 
void free_io_cache(Table *table)
 
726
void free_io_cache(TABLE *table)
642
727
{
 
728
  DBUG_ENTER("free_io_cache");
643
729
  if (table->sort.io_cache)
644
730
  {
645
731
    close_cached_file(table->sort.io_cache);
646
 
    free((unsigned char*) table->sort.io_cache);
 
732
    my_free((uchar*) table->sort.io_cache,MYF(0));
647
733
    table->sort.io_cache=0;
648
734
  }
649
 
  return;
 
735
  DBUG_VOID_RETURN;
650
736
}
651
737
 
652
738
 
665
751
          and tables must be NULL.
666
752
*/
667
753
 
668
 
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
 
754
bool close_cached_tables(THD *thd, TABLE_LIST *tables, bool have_lock,
669
755
                         bool wait_for_refresh, bool wait_for_placeholders)
670
756
{
671
757
  bool result=0;
672
 
  assert(thd || (!wait_for_refresh && !tables));
 
758
  DBUG_ENTER("close_cached_tables");
 
759
  DBUG_ASSERT(thd || (!wait_for_refresh && !tables));
673
760
 
674
761
  if (!have_lock)
675
 
    pthread_mutex_lock(&LOCK_open);
 
762
    VOID(pthread_mutex_lock(&LOCK_open));
676
763
  if (!tables)
677
764
  {
678
765
    refresh_version++;                          // Force close of open tables
679
766
    while (unused_tables)
680
767
    {
681
768
#ifdef EXTRA_DEBUG
682
 
      if (hash_delete(&open_cache,(unsigned char*) unused_tables))
 
769
      if (hash_delete(&open_cache,(uchar*) unused_tables))
683
770
        printf("Warning: Couldn't delete open table from hash\n");
684
771
#else
685
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
 
772
      VOID(hash_delete(&open_cache,(uchar*) unused_tables));
686
773
#endif
687
774
    }
688
775
    /* Free table shares */
689
776
    while (oldest_unused_share->next)
690
777
    {
691
778
      pthread_mutex_lock(&oldest_unused_share->mutex);
692
 
      hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
 
779
      VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
693
780
    }
 
781
    DBUG_PRINT("tcache", ("incremented global refresh_version to: %lu",
 
782
                          refresh_version));
694
783
    if (wait_for_refresh)
695
784
    {
696
785
      /*
730
819
        after the call to close_old_data_files() i.e. after removal of
731
820
        current thread locks.
732
821
      */
733
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
822
      for (uint idx=0 ; idx < open_cache.records ; idx++)
734
823
      {
735
 
        Table *table=(Table*) hash_element(&open_cache,idx);
 
824
        TABLE *table=(TABLE*) hash_element(&open_cache,idx);
736
825
        if (table->in_use)
737
826
          table->in_use->some_tables_deleted= 1;
738
827
      }
741
830
  else
742
831
  {
743
832
    bool found=0;
744
 
    for (TableList *table= tables; table; table= table->next_local)
 
833
    for (TABLE_LIST *table= tables; table; table= table->next_local)
745
834
    {
746
835
      if (remove_table_from_cache(thd, table->db, table->table_name,
747
836
                                  RTFC_OWNED_BY_THD_FLAG))
759
848
    */
760
849
    thd->mysys_var->current_mutex= &LOCK_open;
761
850
    thd->mysys_var->current_cond= &COND_refresh;
762
 
    thd->set_proc_info("Flushing tables");
 
851
    thd_proc_info(thd, "Flushing tables");
763
852
 
764
853
    close_old_data_files(thd,thd->open_tables,1,1);
765
854
    mysql_ha_flush(thd);
766
855
 
767
856
    bool found=1;
768
857
    /* Wait until all threads has closed all the tables we had locked */
 
858
    DBUG_PRINT("info",
 
859
               ("Waiting for other threads to close their open tables"));
769
860
    while (found && ! thd->killed)
770
861
    {
771
862
      found=0;
772
 
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
863
      for (uint idx=0 ; idx < open_cache.records ; idx++)
773
864
      {
774
 
        Table *table=(Table*) hash_element(&open_cache,idx);
 
865
        TABLE *table=(TABLE*) hash_element(&open_cache,idx);
775
866
        /* Avoid a self-deadlock. */
776
867
        if (table->in_use == thd)
777
868
          continue;
778
869
        /*
779
870
          Note that we wait here only for tables which are actually open, and
780
 
          not for placeholders with Table::open_placeholder set. Waiting for
 
871
          not for placeholders with TABLE::open_placeholder set. Waiting for
781
872
          latter will cause deadlock in the following scenario, for example:
782
873
 
783
874
          conn1: lock table t1 write;
793
884
            (table->open_placeholder && wait_for_placeholders)))
794
885
        {
795
886
          found=1;
 
887
          DBUG_PRINT("signal", ("Waiting for COND_refresh"));
796
888
          pthread_cond_wait(&COND_refresh,&LOCK_open);
797
889
          break;
798
890
        }
807
899
    result=reopen_tables(thd,1,1);
808
900
    thd->in_lock_tables=0;
809
901
    /* Set version for table */
810
 
    for (Table *table=thd->open_tables; table ; table= table->next)
 
902
    for (TABLE *table=thd->open_tables; table ; table= table->next)
811
903
    {
812
904
      /*
813
905
        Preserve the version (0) of write locked tables so that a impending
818
910
    }
819
911
  }
820
912
  if (!have_lock)
821
 
    pthread_mutex_unlock(&LOCK_open);
 
913
    VOID(pthread_mutex_unlock(&LOCK_open));
822
914
  if (wait_for_refresh)
823
915
  {
824
916
    pthread_mutex_lock(&thd->mysys_var->mutex);
825
917
    thd->mysys_var->current_mutex= 0;
826
918
    thd->mysys_var->current_cond= 0;
827
 
    thd->set_proc_info(0);
 
919
    thd_proc_info(thd, 0);
828
920
    pthread_mutex_unlock(&thd->mysys_var->mutex);
829
921
  }
830
 
  return(result);
 
922
  DBUG_RETURN(result);
831
923
}
832
924
 
833
925
 
839
931
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
840
932
                                    LEX_STRING *connection, bool have_lock)
841
933
{
842
 
  uint32_t idx;
843
 
  TableList tmp, *tables= NULL;
 
934
  uint idx;
 
935
  TABLE_LIST tmp, *tables= NULL;
844
936
  bool result= false;
845
 
  assert(thd);
 
937
  DBUG_ENTER("close_cached_connections");
 
938
  DBUG_ASSERT(thd);
846
939
 
847
 
  memset(&tmp, 0, sizeof(TableList));
 
940
  bzero(&tmp, sizeof(TABLE_LIST));
848
941
 
849
942
  if (!have_lock)
850
 
    pthread_mutex_lock(&LOCK_open);
 
943
    VOID(pthread_mutex_lock(&LOCK_open));
851
944
 
852
945
  for (idx= 0; idx < table_def_cache.records; idx++)
853
946
  {
872
965
    tmp.table_name= share->table_name.str;
873
966
    tmp.next_local= tables;
874
967
 
875
 
    tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp, 
876
 
                                       sizeof(TableList));
 
968
    tables= (TABLE_LIST *) memdup_root(thd->mem_root, (char*)&tmp, 
 
969
                                       sizeof(TABLE_LIST));
877
970
  }
878
971
 
879
972
  if (tables)
880
973
    result= close_cached_tables(thd, tables, true, false, false);
881
974
 
882
975
  if (!have_lock)
883
 
    pthread_mutex_unlock(&LOCK_open);
 
976
    VOID(pthread_mutex_unlock(&LOCK_open));
884
977
 
885
978
  if (if_wait_for_refresh)
886
979
  {
887
980
    pthread_mutex_lock(&thd->mysys_var->mutex);
888
981
    thd->mysys_var->current_mutex= 0;
889
982
    thd->mysys_var->current_cond= 0;
890
 
    thd->set_proc_info(0);
 
983
    thd->proc_info=0;
891
984
    pthread_mutex_unlock(&thd->mysys_var->mutex);
892
985
  }
893
986
 
894
 
  return(result);
 
987
  DBUG_RETURN(result);
895
988
}
896
989
 
897
990
 
907
1000
 
908
1001
static void mark_temp_tables_as_free_for_reuse(THD *thd)
909
1002
{
910
 
  for (Table *table= thd->temporary_tables ; table ; table= table->next)
 
1003
  for (TABLE *table= thd->temporary_tables ; table ; table= table->next)
911
1004
  {
912
1005
    if ((table->query_id == thd->query_id) && ! table->open_by_handler)
913
1006
    {
941
1034
    set to query_id of original query.
942
1035
*/
943
1036
 
944
 
static void mark_used_tables_as_free_for_reuse(THD *thd, Table *table)
 
1037
static void mark_used_tables_as_free_for_reuse(THD *thd, TABLE *table)
945
1038
{
946
1039
  for (; table ; table= table->next)
947
1040
  {
968
1061
 
969
1062
  safe_mutex_assert_not_owner(&LOCK_open);
970
1063
 
971
 
  pthread_mutex_lock(&LOCK_open);
 
1064
  VOID(pthread_mutex_lock(&LOCK_open));
 
1065
 
 
1066
  DBUG_PRINT("info", ("thd->open_tables: 0x%lx", (long) thd->open_tables));
972
1067
 
973
1068
  while (thd->open_tables)
974
1069
    found_old_table|= close_thread_table(thd, &thd->open_tables);
976
1071
 
977
1072
  /* Free tables to hold down open files */
978
1073
  while (open_cache.records > table_cache_size && unused_tables)
979
 
    hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
 
1074
    VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
 
1075
  check_unused();
980
1076
  if (found_old_table)
981
1077
  {
982
1078
    /* Tell threads waiting for refresh that something has happened */
983
1079
    broadcast_refresh();
984
1080
  }
985
1081
 
986
 
  pthread_mutex_unlock(&LOCK_open);
 
1082
  VOID(pthread_mutex_unlock(&LOCK_open));
987
1083
}
988
1084
 
989
1085
 
1007
1103
 
1008
1104
void close_thread_tables(THD *thd)
1009
1105
{
1010
 
  Table *table;
 
1106
  TABLE *table;
 
1107
  DBUG_ENTER("close_thread_tables");
1011
1108
 
1012
1109
  /*
1013
1110
    We are assuming here that thd->derived_tables contains ONLY derived
1022
1119
  */
1023
1120
  if (thd->derived_tables)
1024
1121
  {
1025
 
    Table *next;
 
1122
    TABLE *next;
1026
1123
    /*
1027
1124
      Close all derived tables generated in queries like
1028
1125
      SELECT * FROM (SELECT * FROM t1)
1030
1127
    for (table= thd->derived_tables ; table ; table= next)
1031
1128
    {
1032
1129
      next= table->next;
1033
 
      table->free_tmp_table(thd);
 
1130
      free_tmp_table(thd, table);
1034
1131
    }
1035
1132
    thd->derived_tables= 0;
1036
1133
  }
1064
1161
    /*
1065
1162
      We are under simple LOCK TABLES so should not do anything else.
1066
1163
    */
1067
 
    return;
 
1164
    DBUG_VOID_RETURN;
1068
1165
  }
1069
1166
 
1070
1167
  if (thd->lock)
1092
1189
  if (thd->open_tables)
1093
1190
    close_open_tables(thd);
1094
1191
 
1095
 
  return;
 
1192
  DBUG_VOID_RETURN;
1096
1193
}
1097
1194
 
1098
1195
 
1099
1196
/* move one table to free list */
1100
1197
 
1101
 
bool close_thread_table(THD *thd, Table **table_ptr)
 
1198
bool close_thread_table(THD *thd, TABLE **table_ptr)
1102
1199
{
1103
1200
  bool found_old_table= 0;
1104
 
  Table *table= *table_ptr;
1105
 
 
1106
 
  assert(table->key_read == 0);
1107
 
  assert(!table->file || table->file->inited == handler::NONE);
 
1201
  TABLE *table= *table_ptr;
 
1202
  DBUG_ENTER("close_thread_table");
 
1203
  DBUG_ASSERT(table->key_read == 0);
 
1204
  DBUG_ASSERT(!table->file || table->file->inited == handler::NONE);
 
1205
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
 
1206
                        table->s->table_name.str, (long) table));
1108
1207
 
1109
1208
  *table_ptr=table->next;
1110
1209
 
1111
1210
  if (table->needs_reopen_or_name_lock() ||
1112
1211
      thd->version != refresh_version || !table->db_stat)
1113
1212
  {
1114
 
    hash_delete(&open_cache,(unsigned char*) table);
 
1213
    VOID(hash_delete(&open_cache,(uchar*) table));
1115
1214
    found_old_table=1;
1116
1215
  }
1117
1216
  else
1118
1217
  {
1119
1218
    /*
1120
 
      Open placeholders have Table::db_stat set to 0, so they should be
 
1219
      Open placeholders have TABLE::db_stat set to 0, so they should be
1121
1220
      handled by the first alternative.
1122
1221
    */
1123
 
    assert(!table->open_placeholder);
 
1222
    DBUG_ASSERT(!table->open_placeholder);
1124
1223
 
1125
1224
    /* Free memory and reset for next loop */
1126
1225
    table->file->ha_reset();
1135
1234
    else
1136
1235
      unused_tables=table->next=table->prev=table;
1137
1236
  }
1138
 
  return(found_old_table);
 
1237
  DBUG_RETURN(found_old_table);
1139
1238
}
1140
1239
 
1141
1240
 
1142
1241
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1143
 
static inline uint32_t  tmpkeyval(THD *thd __attribute__((unused)),
1144
 
                              Table *table)
 
1242
static inline uint  tmpkeyval(THD *thd, TABLE *table)
1145
1243
{
1146
1244
  return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1147
1245
}
1149
1247
 
1150
1248
/*
1151
1249
  Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1152
 
  creates one DROP TEMPORARY Table binlog event for each pseudo-thread 
 
1250
  creates one DROP TEMPORARY TABLE binlog event for each pseudo-thread 
1153
1251
*/
1154
1252
 
1155
1253
void close_temporary_tables(THD *thd)
1156
1254
{
1157
 
  Table *table;
1158
 
  Table *next= NULL;
1159
 
  Table *prev_table;
 
1255
  TABLE *table;
 
1256
  TABLE *next= NULL;
 
1257
  TABLE *prev_table;
1160
1258
  /* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1161
1259
  bool was_quote_show= true;
1162
1260
 
1165
1263
 
1166
1264
  if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1167
1265
  {
1168
 
    Table *tmp_next;
 
1266
    TABLE *tmp_next;
1169
1267
    for (table= thd->temporary_tables; table; table= tmp_next)
1170
1268
    {
1171
1269
      tmp_next= table->next;
1176
1274
  }
1177
1275
 
1178
1276
  /* Better add "if exists", in case a RESET MASTER has been done */
1179
 
  const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1180
 
  uint32_t stub_len= sizeof(stub) - 1;
 
1277
  const char stub[]= "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS ";
 
1278
  uint stub_len= sizeof(stub) - 1;
1181
1279
  char buf[256];
1182
1280
  String s_query= String(buf, sizeof(buf), system_charset_info);
1183
1281
  bool found_user_tables= false;
1193
1291
       table;
1194
1292
       prev_table= table, table= table->next)
1195
1293
  {
1196
 
    Table *prev_sorted /* same as for prev_table */, *sorted;
 
1294
    TABLE *prev_sorted /* same as for prev_table */, *sorted;
1197
1295
    if (is_user_table(table))
1198
1296
    {
1199
1297
      if (!found_user_tables)
1259
1357
        close_temporary(table, 1, 1);
1260
1358
      }
1261
1359
      thd->clear_error();
1262
 
      const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
 
1360
      CHARSET_INFO *cs_save= thd->variables.character_set_client;
1263
1361
      thd->variables.character_set_client= system_charset_info;
1264
1362
      Query_log_event qinfo(thd, s_query.ptr(),
1265
1363
                            s_query.length() - 1 /* to remove trailing ',' */,
1309
1407
    #           Pointer to found table.
1310
1408
*/
1311
1409
 
1312
 
TableList *find_table_in_list(TableList *table,
1313
 
                               TableList *TableList::*link,
 
1410
TABLE_LIST *find_table_in_list(TABLE_LIST *table,
 
1411
                               TABLE_LIST *TABLE_LIST::*link,
1314
1412
                               const char *db_name,
1315
1413
                               const char *table_name)
1316
1414
{
1349
1447
    Also SELECT::exclude_from_table_unique_test used to exclude from check
1350
1448
    tables of main SELECT of multi-delete and multi-update
1351
1449
 
1352
 
    We also skip tables with TableList::prelocking_placeholder set,
 
1450
    We also skip tables with TABLE_LIST::prelocking_placeholder set,
1353
1451
    because we want to allow SELECTs from them, and their modification
1354
1452
    will rise the error anyway.
1355
1453
 
1361
1459
    0 if table is unique
1362
1460
*/
1363
1461
 
1364
 
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
 
1462
TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1365
1463
                         bool check_alias)
1366
1464
{
1367
 
  TableList *res;
 
1465
  TABLE_LIST *res;
1368
1466
  const char *d_name, *t_name, *t_alias;
 
1467
  DBUG_ENTER("unique_table");
 
1468
  DBUG_PRINT("enter", ("table alias: %s", table->alias));
1369
1469
 
1370
1470
  /*
1371
1471
    If this function called for query which update table (INSERT/UPDATE/...)
1372
 
    then we have in table->table pointer to Table object which we are
1373
 
    updating even if it is VIEW so we need TableList of this Table object
 
1472
    then we have in table->table pointer to TABLE object which we are
 
1473
    updating even if it is VIEW so we need TABLE_LIST of this TABLE object
1374
1474
    to get right names (even if lower_case_table_names used).
1375
1475
 
1376
1476
    If this function called for CREATE command that we have not opened table
1377
 
    (table->table equal to 0) and right names is in current TableList
 
1477
    (table->table equal to 0) and right names is in current TABLE_LIST
1378
1478
    object.
1379
1479
  */
1380
1480
  if (table->table)
1381
1481
  {
1382
1482
    /* temporary table is always unique */
1383
1483
    if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
1384
 
      return(0);
 
1484
      DBUG_RETURN(0);
1385
1485
    table= table->find_underlying_table(table->table);
1386
1486
    /*
1387
 
      as far as we have table->table we have to find real TableList of
 
1487
      as far as we have table->table we have to find real TABLE_LIST of
1388
1488
      it in underlying tables
1389
1489
    */
1390
 
    assert(table);
 
1490
    DBUG_ASSERT(table);
1391
1491
  }
1392
1492
  d_name= table->db;
1393
1493
  t_name= table->table_name;
1394
1494
  t_alias= table->alias;
1395
1495
 
 
1496
  DBUG_PRINT("info", ("real table: %s.%s", d_name, t_name));
1396
1497
  for (;;)
1397
1498
  {
1398
1499
    if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1409
1510
      (exclude_from_table_unique_test) or prelocking placeholder.
1410
1511
    */
1411
1512
    table_list= res->next_global;
 
1513
    DBUG_PRINT("info",
 
1514
               ("found same copy of table or table which we should skip"));
1412
1515
  }
1413
 
  return(res);
 
1516
  DBUG_RETURN(res);
1414
1517
}
1415
1518
 
1416
1519
 
1428
1531
    here we hide view underlying tables if we have them
1429
1532
*/
1430
1533
 
1431
 
void update_non_unique_table_error(TableList *update,
1432
 
                                   const char *operation __attribute__((unused)),
1433
 
                                   TableList *duplicate __attribute__((unused)))
 
1534
void update_non_unique_table_error(TABLE_LIST *update,
 
1535
                                   const char *operation,
 
1536
                                   TABLE_LIST *duplicate)
1434
1537
{
1435
1538
  my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1436
1539
}
1437
1540
 
1438
1541
 
1439
 
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
 
1542
TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name)
1440
1543
{
1441
 
  TableList table_list;
 
1544
  TABLE_LIST table_list;
1442
1545
 
1443
1546
  table_list.db= (char*) db;
1444
1547
  table_list.table_name= (char*) table_name;
1446
1549
}
1447
1550
 
1448
1551
 
1449
 
Table *find_temporary_table(THD *thd, TableList *table_list)
 
1552
TABLE *find_temporary_table(THD *thd, TABLE_LIST *table_list)
1450
1553
{
1451
1554
  char  key[MAX_DBKEY_LENGTH];
1452
1555
  uint  key_length;
1453
 
  Table *table;
 
1556
  TABLE *table;
 
1557
  DBUG_ENTER("find_temporary_table");
 
1558
  DBUG_PRINT("enter", ("table: '%s'.'%s'",
 
1559
                       table_list->db, table_list->table_name));
1454
1560
 
1455
1561
  key_length= create_table_def_key(thd, key, table_list, 1);
1456
1562
  for (table=thd->temporary_tables ; table ; table= table->next)
1457
1563
  {
1458
1564
    if (table->s->table_cache_key.length == key_length &&
1459
1565
        !memcmp(table->s->table_cache_key.str, key, key_length))
1460
 
      return(table);
 
1566
    {
 
1567
      DBUG_PRINT("info",
 
1568
                 ("Found table. server_id: %u  pseudo_thread_id: %lu",
 
1569
                  (uint) thd->server_id,
 
1570
                  (ulong) thd->variables.pseudo_thread_id));
 
1571
      DBUG_RETURN(table);
 
1572
    }
1461
1573
  }
1462
 
  return(0);                               // Not a temporary table
 
1574
  DBUG_RETURN(0);                               // Not a temporary table
1463
1575
}
1464
1576
 
1465
1577
 
1477
1589
 
1478
1590
  This function is used to drop user temporary tables, as well as
1479
1591
  internal tables created in CREATE TEMPORARY TABLE ... SELECT
1480
 
  or ALTER Table. Even though part of the work done by this function
 
1592
  or ALTER TABLE. Even though part of the work done by this function
1481
1593
  is redundant when the table is internal, as long as we
1482
1594
  link both internal and user temporary tables into the same
1483
1595
  thd->temporary_tables list, it's impossible to tell here whether
1489
1601
  @retval -1  the table is in use by a outer query
1490
1602
*/
1491
1603
 
1492
 
int drop_temporary_table(THD *thd, TableList *table_list)
 
1604
int drop_temporary_table(THD *thd, TABLE_LIST *table_list)
1493
1605
{
1494
 
  Table *table;
 
1606
  TABLE *table;
 
1607
  DBUG_ENTER("drop_temporary_table");
 
1608
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
 
1609
                          table_list->db, table_list->table_name));
1495
1610
 
1496
1611
  if (!(table= find_temporary_table(thd, table_list)))
1497
 
    return(1);
 
1612
    DBUG_RETURN(1);
1498
1613
 
1499
1614
  /* Table might be in use by some outer statement. */
1500
1615
  if (table->query_id && table->query_id != thd->query_id)
1501
1616
  {
1502
1617
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1503
 
    return(-1);
 
1618
    DBUG_RETURN(-1);
1504
1619
  }
1505
1620
 
1506
1621
  /*
1509
1624
  */
1510
1625
  mysql_lock_remove(thd, thd->locked_tables, table, false);
1511
1626
  close_temporary_table(thd, table, 1, 1);
1512
 
  return(0);
 
1627
  DBUG_RETURN(0);
1513
1628
}
1514
1629
 
1515
1630
/*
1516
1631
  unlink from thd->temporary tables and close temporary table
1517
1632
*/
1518
1633
 
1519
 
void close_temporary_table(THD *thd, Table *table,
 
1634
void close_temporary_table(THD *thd, TABLE *table,
1520
1635
                           bool free_share, bool delete_table)
1521
1636
{
 
1637
  DBUG_ENTER("close_temporary_table");
 
1638
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s' 0x%lx  alias: '%s'",
 
1639
                          table->s->db.str, table->s->table_name.str,
 
1640
                          (long) table, table->alias));
 
1641
 
1522
1642
  if (table->prev)
1523
1643
  {
1524
1644
    table->prev->next= table->next;
1528
1648
  else
1529
1649
  {
1530
1650
    /* removing the item from the list */
1531
 
    assert(table == thd->temporary_tables);
 
1651
    DBUG_ASSERT(table == thd->temporary_tables);
1532
1652
    /*
1533
1653
      slave must reset its temporary list pointer to zero to exclude
1534
1654
      passing non-zero value to end_slave via rli->save_temporary_tables
1541
1661
  if (thd->slave_thread)
1542
1662
  {
1543
1663
    /* natural invariant of temporary_tables */
1544
 
    assert(slave_open_temp_tables || !thd->temporary_tables);
 
1664
    DBUG_ASSERT(slave_open_temp_tables || !thd->temporary_tables);
1545
1665
    slave_open_temp_tables--;
1546
1666
  }
1547
1667
  close_temporary(table, free_share, delete_table);
1548
 
  return;
 
1668
  DBUG_VOID_RETURN;
1549
1669
}
1550
1670
 
1551
1671
 
1557
1677
    If this is needed, use close_temporary_table()
1558
1678
*/
1559
1679
 
1560
 
void close_temporary(Table *table, bool free_share, bool delete_table)
 
1680
void close_temporary(TABLE *table, bool free_share, bool delete_table)
1561
1681
{
1562
1682
  handlerton *table_type= table->s->db_type();
 
1683
  DBUG_ENTER("close_temporary");
 
1684
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
 
1685
                          table->s->db.str, table->s->table_name.str));
1563
1686
 
1564
1687
  free_io_cache(table);
1565
1688
  closefrm(table, 0);
1573
1696
  if (free_share)
1574
1697
  {
1575
1698
    free_table_share(table->s);
1576
 
    free((char*) table);
 
1699
    my_free((char*) table,MYF(0));
1577
1700
  }
1578
 
  return;
 
1701
  DBUG_VOID_RETURN;
1579
1702
}
1580
1703
 
1581
1704
 
1582
1705
/*
1583
 
  Used by ALTER Table when the table is a temporary one. It changes something
 
1706
  Used by ALTER TABLE when the table is a temporary one. It changes something
1584
1707
  only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1585
1708
  name).
1586
1709
  Prepares a table cache key, which is the concatenation of db, table_name and
1587
1710
  thd->slave_proxy_id, separated by '\0'.
1588
1711
*/
1589
1712
 
1590
 
bool rename_temporary_table(THD* thd, Table *table, const char *db,
 
1713
bool rename_temporary_table(THD* thd, TABLE *table, const char *db,
1591
1714
                            const char *table_name)
1592
1715
{
1593
1716
  char *key;
1594
 
  uint32_t key_length;
 
1717
  uint key_length;
1595
1718
  TABLE_SHARE *share= table->s;
1596
 
  TableList table_list;
 
1719
  TABLE_LIST table_list;
 
1720
  DBUG_ENTER("rename_temporary_table");
1597
1721
 
1598
1722
  if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1599
 
    return(1);                          /* purecov: inspected */
 
1723
    DBUG_RETURN(1);                             /* purecov: inspected */
1600
1724
 
1601
1725
  table_list.db= (char*) db;
1602
1726
  table_list.table_name= (char*) table_name;
1603
1727
  key_length= create_table_def_key(thd, key, &table_list, 1);
1604
1728
  share->set_table_cache_key(key, key_length);
1605
 
  return(0);
 
1729
  DBUG_RETURN(0);
1606
1730
}
1607
1731
 
1608
1732
 
1609
1733
        /* move table first in unused links */
1610
1734
 
1611
 
static void relink_unused(Table *table)
 
1735
static void relink_unused(TABLE *table)
1612
1736
{
1613
1737
  if (table != unused_tables)
1614
1738
  {
1619
1743
    unused_tables->prev->next=table;
1620
1744
    unused_tables->prev=table;
1621
1745
    unused_tables=table;
 
1746
    check_unused();
1622
1747
  }
1623
1748
}
1624
1749
 
1638
1763
          not locked (for example already unlocked).
1639
1764
*/
1640
1765
 
1641
 
void unlink_open_table(THD *thd, Table *find, bool unlock)
 
1766
void unlink_open_table(THD *thd, TABLE *find, bool unlock)
1642
1767
{
1643
1768
  char key[MAX_DBKEY_LENGTH];
1644
 
  uint32_t key_length= find->s->table_cache_key.length;
1645
 
  Table *list, **prev;
 
1769
  uint key_length= find->s->table_cache_key.length;
 
1770
  TABLE *list, **prev;
 
1771
  DBUG_ENTER("unlink_open_table");
1646
1772
 
1647
1773
  safe_mutex_assert_owner(&LOCK_open);
1648
1774
 
1667
1793
      /* Remove table from open_tables list. */
1668
1794
      *prev= list->next;
1669
1795
      /* Close table. */
1670
 
      hash_delete(&open_cache,(unsigned char*) list); // Close table
 
1796
      VOID(hash_delete(&open_cache,(uchar*) list)); // Close table
1671
1797
    }
1672
1798
    else
1673
1799
    {
1678
1804
 
1679
1805
  // Notify any 'refresh' threads
1680
1806
  broadcast_refresh();
1681
 
  return;
 
1807
  DBUG_VOID_RETURN;
1682
1808
}
1683
1809
 
1684
1810
 
1686
1812
    Auxiliary routine which closes and drops open table.
1687
1813
 
1688
1814
    @param  thd         Thread handle
1689
 
    @param  table       Table object for table to be dropped
 
1815
    @param  table       TABLE object for table to be dropped
1690
1816
    @param  db_name     Name of database for this table
1691
1817
    @param  table_name  Name of this table
1692
1818
 
1701
1827
          table that was locked with LOCK TABLES.
1702
1828
*/
1703
1829
 
1704
 
void drop_open_table(THD *thd, Table *table, const char *db_name,
 
1830
void drop_open_table(THD *thd, TABLE *table, const char *db_name,
1705
1831
                     const char *table_name)
1706
1832
{
1707
1833
  if (table->s->tmp_table)
1709
1835
  else
1710
1836
  {
1711
1837
    handlerton *table_type= table->s->db_type();
1712
 
    pthread_mutex_lock(&LOCK_open);
 
1838
    VOID(pthread_mutex_lock(&LOCK_open));
1713
1839
    /*
1714
1840
      unlink_open_table() also tells threads waiting for refresh or close
1715
1841
      that something has happened.
1716
1842
    */
1717
1843
    unlink_open_table(thd, table, false);
1718
1844
    quick_rm_table(table_type, db_name, table_name, 0);
1719
 
    pthread_mutex_unlock(&LOCK_open);
 
1845
    VOID(pthread_mutex_unlock(&LOCK_open));
1720
1846
  }
1721
1847
}
1722
1848
 
1738
1864
  const char *proc_info;
1739
1865
  thd->mysys_var->current_mutex= mutex;
1740
1866
  thd->mysys_var->current_cond= cond;
1741
 
  proc_info=thd->get_proc_info();
1742
 
  thd->set_proc_info("Waiting for table");
 
1867
  proc_info=thd->proc_info;
 
1868
  thd_proc_info(thd, "Waiting for table");
 
1869
  DBUG_ENTER("wait_for_condition");
1743
1870
  if (!thd->killed)
1744
1871
    (void) pthread_cond_wait(cond, mutex);
1745
1872
 
1758
1885
  pthread_mutex_lock(&thd->mysys_var->mutex);
1759
1886
  thd->mysys_var->current_mutex= 0;
1760
1887
  thd->mysys_var->current_cond= 0;
1761
 
  thd->set_proc_info(proc_info);
 
1888
  thd_proc_info(thd, proc_info);
1762
1889
  pthread_mutex_unlock(&thd->mysys_var->mutex);
1763
 
  return;
 
1890
  DBUG_VOID_RETURN;
1764
1891
}
1765
1892
 
1766
1893
 
1774
1901
  @return false on success, true otherwise.
1775
1902
*/
1776
1903
 
1777
 
bool name_lock_locked_table(THD *thd, TableList *tables)
 
1904
bool name_lock_locked_table(THD *thd, TABLE_LIST *tables)
1778
1905
{
 
1906
  DBUG_ENTER("name_lock_locked_table");
 
1907
 
1779
1908
  /* Under LOCK TABLES we must only accept write locked tables. */
1780
1909
  tables->table= find_locked_table(thd, tables->db, tables->table_name);
1781
1910
 
1790
1919
      other statement will open this table.
1791
1920
    */
1792
1921
    wait_while_table_is_used(thd, tables->table, HA_EXTRA_FORCE_REOPEN);
1793
 
    return(false);
 
1922
    DBUG_RETURN(false);
1794
1923
  }
1795
1924
 
1796
 
  return(true);
 
1925
  DBUG_RETURN(true);
1797
1926
}
1798
1927
 
1799
1928
 
1803
1932
  SYNOPSIS
1804
1933
    reopen_name_locked_table()
1805
1934
      thd         Thread handle
1806
 
      table_list  TableList object for table to be open, TableList::table
1807
 
                  member should point to Table object which was used for
 
1935
      table_list  TABLE_LIST object for table to be open, TABLE_LIST::table
 
1936
                  member should point to TABLE object which was used for
1808
1937
                  name-locking.
1809
 
      link_in     true  - if Table object for table to be opened should be
 
1938
      link_in     true  - if TABLE object for table to be opened should be
1810
1939
                          linked into THD::open_tables list.
1811
1940
                  false - placeholder used for name-locking is already in
1812
 
                          this list so we only need to preserve Table::next
 
1941
                          this list so we only need to preserve TABLE::next
1813
1942
                          pointer.
1814
1943
 
1815
1944
  NOTE
1820
1949
    true  - Error
1821
1950
*/
1822
1951
 
1823
 
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
 
1952
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in)
1824
1953
{
1825
 
  Table *table= table_list->table;
 
1954
  TABLE *table= table_list->table;
1826
1955
  TABLE_SHARE *share;
1827
1956
  char *table_name= table_list->table_name;
1828
 
  Table orig_table;
 
1957
  TABLE orig_table;
 
1958
  DBUG_ENTER("reopen_name_locked_table");
1829
1959
 
1830
1960
  safe_mutex_assert_owner(&LOCK_open);
1831
1961
 
1832
1962
  if (thd->killed || !table)
1833
 
    return(true);
 
1963
    DBUG_RETURN(true);
1834
1964
 
1835
1965
  orig_table= *table;
1836
1966
 
1837
1967
  if (open_unireg_entry(thd, table, table_list, table_name,
1838
1968
                        table->s->table_cache_key.str,
1839
 
                        table->s->table_cache_key.length))
 
1969
                        table->s->table_cache_key.length, thd->mem_root, 0))
1840
1970
  {
1841
1971
    intern_close_table(table);
1842
1972
    /*
1846
1976
      object to its original state.
1847
1977
    */
1848
1978
    *table= orig_table;
1849
 
    return(true);
 
1979
    DBUG_RETURN(true);
1850
1980
  }
1851
1981
 
1852
1982
  share= table->s;
1860
1990
  */
1861
1991
  share->version=0;
1862
1992
  table->in_use = thd;
 
1993
  check_unused();
1863
1994
 
1864
1995
  if (link_in)
1865
1996
  {
1869
2000
  else
1870
2001
  {
1871
2002
    /*
1872
 
      Table object should be already in THD::open_tables list so we just
1873
 
      need to set Table::next correctly.
 
2003
      TABLE object should be already in THD::open_tables list so we just
 
2004
      need to set TABLE::next correctly.
1874
2005
    */
1875
2006
    table->next= orig_table.next;
1876
2007
  }
1878
2009
  table->tablenr=thd->current_tablenr++;
1879
2010
  table->used_fields=0;
1880
2011
  table->const_table=0;
1881
 
  table->null_row= false;
1882
 
  table->maybe_null= false;
1883
 
  table->force_index= false;
 
2012
  table->null_row= table->maybe_null= table->force_index= 0;
1884
2013
  table->status=STATUS_NO_RECORD;
1885
 
  return false;
 
2014
  DBUG_RETURN(false);
1886
2015
}
1887
2016
 
1888
2017
 
1895
2024
    @param key         Table cache key for name to be locked
1896
2025
    @param key_length  Table cache key length
1897
2026
 
1898
 
    @return Pointer to Table object used for name locking or 0 in
 
2027
    @return Pointer to TABLE object used for name locking or 0 in
1899
2028
            case of failure.
1900
2029
*/
1901
2030
 
1902
 
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1903
 
                                      uint32_t key_length)
 
2031
TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
 
2032
                                      uint key_length)
1904
2033
{
1905
 
  Table *table;
 
2034
  TABLE *table;
1906
2035
  TABLE_SHARE *share;
1907
2036
  char *key_buff;
 
2037
  DBUG_ENTER("table_cache_insert_placeholder");
1908
2038
 
1909
2039
  safe_mutex_assert_owner(&LOCK_open);
1910
2040
 
1918
2048
                       &share, sizeof(*share),
1919
2049
                       &key_buff, key_length,
1920
2050
                       NULL))
1921
 
    return(NULL);
 
2051
    DBUG_RETURN(NULL);
1922
2052
 
1923
2053
  table->s= share;
1924
2054
  share->set_table_cache_key(key_buff, key, key_length);
1926
2056
  table->in_use= thd;
1927
2057
  table->locked_by_name=1;
1928
2058
 
1929
 
  if (my_hash_insert(&open_cache, (unsigned char*)table))
 
2059
  if (my_hash_insert(&open_cache, (uchar*)table))
1930
2060
  {
1931
 
    free((unsigned char*) table);
1932
 
    return(NULL);
 
2061
    my_free((uchar*) table, MYF(0));
 
2062
    DBUG_RETURN(NULL);
1933
2063
  }
1934
2064
 
1935
 
  return(table);
 
2065
  DBUG_RETURN(table);
1936
2066
}
1937
2067
 
1938
2068
 
1946
2076
    @param[out] table       Out parameter which is either:
1947
2077
                            - set to NULL if table cache contains record for
1948
2078
                              the table or
1949
 
                            - set to point to the Table instance used for
 
2079
                            - set to point to the TABLE instance used for
1950
2080
                              name-locking.
1951
2081
 
1952
2082
    @note This function takes into account all records for table in table
1959
2089
*/
1960
2090
 
1961
2091
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1962
 
                                   const char *table_name, Table **table)
 
2092
                                   const char *table_name, TABLE **table)
1963
2093
{
1964
2094
  char key[MAX_DBKEY_LENGTH];
1965
 
  uint32_t key_length;
1966
 
 
1967
 
  key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1968
 
  pthread_mutex_lock(&LOCK_open);
1969
 
 
1970
 
  if (hash_search(&open_cache, (unsigned char *)key, key_length))
 
2095
  uint key_length;
 
2096
  DBUG_ENTER("lock_table_name_if_not_cached");
 
2097
 
 
2098
  key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1;
 
2099
  VOID(pthread_mutex_lock(&LOCK_open));
 
2100
 
 
2101
  if (hash_search(&open_cache, (uchar *)key, key_length))
1971
2102
  {
1972
 
    pthread_mutex_unlock(&LOCK_open);
 
2103
    VOID(pthread_mutex_unlock(&LOCK_open));
 
2104
    DBUG_PRINT("info", ("Table is cached, name-lock is not obtained"));
1973
2105
    *table= 0;
1974
 
    return(false);
 
2106
    DBUG_RETURN(false);
1975
2107
  }
1976
2108
  if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1977
2109
  {
1978
 
    pthread_mutex_unlock(&LOCK_open);
1979
 
    return(true);
 
2110
    VOID(pthread_mutex_unlock(&LOCK_open));
 
2111
    DBUG_RETURN(true);
1980
2112
  }
1981
2113
  (*table)->open_placeholder= 1;
1982
2114
  (*table)->next= thd->open_tables;
1983
2115
  thd->open_tables= *table;
1984
 
  pthread_mutex_unlock(&LOCK_open);
1985
 
  return(false);
 
2116
  VOID(pthread_mutex_unlock(&LOCK_open));
 
2117
  DBUG_RETURN(false);
1986
2118
}
1987
2119
 
1988
2120
 
2007
2139
    @retval  false  No error. 'exists' out parameter set accordingly.
2008
2140
*/
2009
2141
 
2010
 
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
 
2142
bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists)
2011
2143
{
2012
2144
  char path[FN_REFLEN];
2013
2145
  int rc;
 
2146
  DBUG_ENTER("check_if_table_exists");
2014
2147
 
2015
2148
  safe_mutex_assert_owner(&LOCK_open);
2016
2149
 
2017
2150
  *exists= true;
2018
2151
 
2019
2152
  if (get_cached_table_share(table->db, table->table_name))
2020
 
    return(false);
 
2153
    DBUG_RETURN(false);
2021
2154
 
2022
2155
  build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2023
2156
                       reg_ext, 0);
2024
2157
 
2025
2158
  if (!access(path, F_OK))
2026
 
    return(false);
 
2159
    DBUG_RETURN(false);
2027
2160
 
2028
2161
  /* .FRM file doesn't exist. Check if some engine can provide it. */
2029
2162
 
2033
2166
  {
2034
2167
    /* Table does not exists in engines as well. */
2035
2168
    *exists= false;
2036
 
    return(false);
 
2169
    DBUG_RETURN(false);
2037
2170
  }
2038
2171
  else if (!rc)
2039
2172
  {
2040
2173
    /* Table exists in some engine and .FRM for it was created. */
2041
 
    return(false);
 
2174
    DBUG_RETURN(false);
2042
2175
  }
2043
2176
  else /* (rc > 0) */
2044
2177
  {
2045
2178
    my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while "
2046
2179
                    "unpacking from engine", MYF(0), table->table_name);
2047
 
    return(true);
 
2180
    DBUG_RETURN(true);
2048
2181
  }
2049
2182
}
2050
2183
 
2061
2194
                        If this is a NULL pointer, then the table is not
2062
2195
                        put in the thread-open-list.
2063
2196
    flags               Bitmap of flags to modify how open works:
2064
 
                          DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
 
2197
                          MYSQL_LOCK_IGNORE_FLUSH - Open table even if
2065
2198
                          someone has done a flush or namelock on it.
2066
2199
                          No version number checking is done.
2067
 
                          DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
 
2200
                          MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary
2068
2201
                          table not the base table or view.
2069
2202
 
2070
2203
  IMPLEMENTATION
2073
2206
    If table list element for the table to be opened has "create" flag
2074
2207
    set and table does not exist, this function will automatically insert
2075
2208
    a placeholder for exclusive name lock into the open tables cache and
2076
 
    will return the Table instance that corresponds to this placeholder.
 
2209
    will return the TABLE instance that corresponds to this placeholder.
2077
2210
 
2078
2211
  RETURN
2079
2212
    NULL  Open failed.  If refresh is set then one should close
2080
2213
          all other tables and retry the open.
2081
 
    #     Success. Pointer to Table object for open table.
 
2214
    #     Success. Pointer to TABLE object for open table.
2082
2215
*/
2083
2216
 
2084
2217
 
2085
 
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint32_t flags)
 
2218
TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
 
2219
                  bool *refresh, uint flags)
2086
2220
{
2087
 
  register Table *table;
 
2221
  register TABLE *table;
2088
2222
  char key[MAX_DBKEY_LENGTH];
2089
2223
  unsigned int key_length;
2090
2224
  char *alias= table_list->alias;
2091
2225
  HASH_SEARCH_STATE state;
 
2226
  DBUG_ENTER("open_table");
2092
2227
 
2093
2228
  /* Parsing of partitioning information from .frm needs thd->lex set up. */
2094
 
  assert(thd->lex->is_lex_started);
 
2229
  DBUG_ASSERT(thd->lex->is_lex_started);
2095
2230
 
2096
2231
  /* find a unused table in the open table cache */
2097
2232
  if (refresh)
2098
2233
    *refresh=0;
2099
2234
 
2100
2235
  /* an open table operation needs a lot of the stack space */
2101
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2102
 
    return(0);
 
2236
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
 
2237
    DBUG_RETURN(0);
2103
2238
 
2104
2239
  if (thd->killed)
2105
 
    return(0);
 
2240
    DBUG_RETURN(0);
2106
2241
 
2107
2242
  key_length= (create_table_def_key(thd, key, table_list, 1) -
2108
2243
               TMP_TABLE_KEY_EXTRA);
2125
2260
        /*
2126
2261
          We're trying to use the same temporary table twice in a query.
2127
2262
          Right now we don't support this because a temporary table
2128
 
          is always represented by only one Table object in THD, and
 
2263
          is always represented by only one TABLE object in THD, and
2129
2264
          it can not be cloned. Emit an error for an unsupported behaviour.
2130
2265
        */
2131
2266
        if (table->query_id)
2132
2267
        {
 
2268
          DBUG_PRINT("error",
 
2269
                     ("query_id: %lu  server_id: %u  pseudo_thread_id: %lu",
 
2270
                      (ulong) table->query_id, (uint) thd->server_id,
 
2271
                      (ulong) thd->variables.pseudo_thread_id));
2133
2272
          my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2134
 
          return(0);
 
2273
          DBUG_RETURN(0);
2135
2274
        }
2136
2275
        table->query_id= thd->query_id;
2137
2276
        thd->thread_specific_used= true;
 
2277
        DBUG_PRINT("info",("Using temporary table"));
2138
2278
        goto reset;
2139
2279
      }
2140
2280
    }
2141
2281
  }
2142
2282
 
2143
 
  if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
 
2283
  if (flags & MYSQL_OPEN_TEMPORARY_ONLY)
2144
2284
  {
2145
2285
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2146
 
    return(0);
 
2286
    DBUG_RETURN(0);
2147
2287
  }
2148
2288
 
2149
2289
  /*
2155
2295
  */
2156
2296
  if (thd->locked_tables)
2157
2297
  {                                             // Using table locks
2158
 
    Table *best_table= 0;
 
2298
    TABLE *best_table= 0;
2159
2299
    int best_distance= INT_MIN;
2160
2300
    bool check_if_used= false;
2161
2301
    for (table=thd->open_tables; table ; table=table->next)
2174
2314
          */
2175
2315
          my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
2176
2316
                   table->s->table_name.str);
2177
 
          return(0);
 
2317
          DBUG_RETURN(0);
2178
2318
        }
2179
2319
        /*
2180
 
          When looking for a usable Table, ignore MERGE children, as they
 
2320
          When looking for a usable TABLE, ignore MERGE children, as they
2181
2321
          belong to their parent and cannot be used explicitly.
2182
2322
        */
2183
2323
        if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2219
2359
    {
2220
2360
      table= best_table;
2221
2361
      table->query_id= thd->query_id;
 
2362
      DBUG_PRINT("info",("Using locked table"));
2222
2363
      goto reset;
2223
2364
    }
2224
2365
    /*
2229
2370
      locked tables list was created.
2230
2371
    */
2231
2372
    my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
2232
 
    return(0);
 
2373
    DBUG_RETURN(0);
2233
2374
  }
2234
2375
 
2235
2376
  /*
2237
2378
    this is the normal use case.
2238
2379
    Now we should:
2239
2380
    - try to find the table in the table cache.
2240
 
    - if one of the discovered Table instances is name-locked
 
2381
    - if one of the discovered TABLE instances is name-locked
2241
2382
      (table->s->version == 0) or some thread has started FLUSH TABLES
2242
2383
      (refresh_version > table->s->version), back off -- we have to wait
2243
2384
      until no one holds a name lock on the table.
2244
 
    - if there is no such Table in the name cache, read the table definition
 
2385
    - if there is no such TABLE in the name cache, read the table definition
2245
2386
    and insert it into the cache.
2246
2387
    We perform all of the above under LOCK_open which currently protects
2247
2388
    the open cache (also known as table cache) and table definitions stored
2248
2389
    on disk.
2249
2390
  */
2250
2391
 
2251
 
  pthread_mutex_lock(&LOCK_open);
 
2392
  VOID(pthread_mutex_lock(&LOCK_open));
2252
2393
 
2253
2394
  /*
2254
2395
    If it's the first table from a list of tables used in a query,
2261
2402
  if (!thd->open_tables)
2262
2403
    thd->version=refresh_version;
2263
2404
  else if ((thd->version != refresh_version) &&
2264
 
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
 
2405
           ! (flags & MYSQL_LOCK_IGNORE_FLUSH))
2265
2406
  {
2266
2407
    /* Someone did a refresh while thread was opening tables */
2267
2408
    if (refresh)
2268
2409
      *refresh=1;
2269
 
    pthread_mutex_unlock(&LOCK_open);
2270
 
    return(0);
 
2410
    VOID(pthread_mutex_unlock(&LOCK_open));
 
2411
    DBUG_RETURN(0);
2271
2412
  }
2272
2413
 
2273
2414
  /*
2282
2423
 
2283
2424
  /*
2284
2425
    Actually try to find the table in the open_cache.
2285
 
    The cache may contain several "Table" instances for the same
 
2426
    The cache may contain several "TABLE" instances for the same
2286
2427
    physical table. The instances that are currently "in use" by
2287
2428
    some thread have their "in_use" member != NULL.
2288
2429
    There is no good reason for having more than one entry in the
2290
2431
    an implicit "pending locks queue" - see
2291
2432
    wait_for_locked_table_names for details.
2292
2433
  */
2293
 
  for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
 
2434
  for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
2294
2435
                                  &state);
2295
2436
       table && table->in_use ;
2296
 
       table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
 
2437
       table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
2297
2438
                                 &state))
2298
2439
  {
 
2440
    DBUG_PRINT("tcache", ("in_use table: '%s'.'%s' 0x%lx", table->s->db.str,
 
2441
                          table->s->table_name.str, (long) table));
2299
2442
    /*
2300
2443
      Here we flush tables marked for flush.
2301
2444
      Normally, table->s->version contains the value of
2303
2446
      (re-)opened and added to the cache.
2304
2447
      If since then we did (or just started) FLUSH TABLES
2305
2448
      statement, refresh_version has been increased.
2306
 
      For "name-locked" Table instances, table->s->version is set
 
2449
      For "name-locked" TABLE instances, table->s->version is set
2307
2450
      to 0 (see lock_table_name for details).
2308
2451
      In case there is a pending FLUSH TABLES or a name lock, we
2309
2452
      need to back off and re-start opening tables.
2316
2459
    */
2317
2460
    if (table->needs_reopen_or_name_lock())
2318
2461
    {
2319
 
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
 
2462
      DBUG_PRINT("note",
 
2463
                 ("Found table '%s.%s' with different refresh version",
 
2464
                  table_list->db, table_list->table_name));
 
2465
 
 
2466
      if (flags & MYSQL_LOCK_IGNORE_FLUSH)
2320
2467
      {
2321
2468
        /* Force close at once after usage */
2322
2469
        thd->version= table->s->version;
2326
2473
      /* Avoid self-deadlocks by detecting self-dependencies. */
2327
2474
      if (table->open_placeholder && table->in_use == thd)
2328
2475
      {
2329
 
        pthread_mutex_unlock(&LOCK_open);
 
2476
        VOID(pthread_mutex_unlock(&LOCK_open));
2330
2477
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2331
 
        return(0);
 
2478
        DBUG_RETURN(0);
2332
2479
      }
2333
2480
 
2334
2481
      /*
2367
2514
      }
2368
2515
      else
2369
2516
      {
2370
 
        pthread_mutex_unlock(&LOCK_open);
 
2517
        VOID(pthread_mutex_unlock(&LOCK_open));
2371
2518
      }
2372
2519
      /*
2373
2520
        There is a refresh in progress for this table.
2375
2522
      */
2376
2523
      if (refresh)
2377
2524
        *refresh=1;
2378
 
      return(0);
 
2525
      DBUG_RETURN(0);
2379
2526
    }
2380
2527
  }
2381
2528
  if (table)
2382
2529
  {
 
2530
    DBUG_PRINT("tcache", ("unused table: '%s'.'%s' 0x%lx", table->s->db.str,
 
2531
                          table->s->table_name.str, (long) table));
2383
2532
    /* Unlink the table from "unused_tables" list. */
2384
2533
    if (table == unused_tables)
2385
2534
    {                                           // First unused
2393
2542
  }
2394
2543
  else
2395
2544
  {
2396
 
    /* Insert a new Table instance into the open cache */
 
2545
    /* Insert a new TABLE instance into the open cache */
2397
2546
    int error;
 
2547
    DBUG_PRINT("tcache", ("opening new table"));
2398
2548
    /* Free cache if too big */
2399
2549
    while (open_cache.records > table_cache_size && unused_tables)
2400
 
      hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
 
2550
      VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2401
2551
 
2402
2552
    if (table_list->create)
2403
2553
    {
2405
2555
 
2406
2556
      if (check_if_table_exists(thd, table_list, &exists))
2407
2557
      {
2408
 
        pthread_mutex_unlock(&LOCK_open);
2409
 
        return(NULL);
 
2558
        VOID(pthread_mutex_unlock(&LOCK_open));
 
2559
        DBUG_RETURN(NULL);
2410
2560
      }
2411
2561
 
2412
2562
      if (!exists)
2416
2566
        */
2417
2567
        if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2418
2568
        {
2419
 
          pthread_mutex_unlock(&LOCK_open);
2420
 
          return(NULL);
 
2569
          VOID(pthread_mutex_unlock(&LOCK_open));
 
2570
          DBUG_RETURN(NULL);
2421
2571
        }
2422
2572
        /*
2423
2573
          Link placeholder to the open tables list so it will be automatically
2427
2577
        table->open_placeholder= 1;
2428
2578
        table->next= thd->open_tables;
2429
2579
        thd->open_tables= table;
2430
 
        pthread_mutex_unlock(&LOCK_open);
2431
 
        return(table);
 
2580
        VOID(pthread_mutex_unlock(&LOCK_open));
 
2581
        DBUG_RETURN(table);
2432
2582
      }
2433
2583
      /* Table exists. Let us try to open it. */
2434
2584
    }
2435
2585
 
2436
2586
    /* make a new table */
2437
 
    if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
 
2587
    if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
2438
2588
    {
2439
 
      pthread_mutex_unlock(&LOCK_open);
2440
 
      return(NULL);
 
2589
      VOID(pthread_mutex_unlock(&LOCK_open));
 
2590
      DBUG_RETURN(NULL);
2441
2591
    }
2442
2592
 
2443
 
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
 
2593
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
 
2594
                             mem_root, (flags & OPEN_VIEW_NO_PARSE));
2444
2595
    /* Combine the follow two */
2445
2596
    if (error > 0)
2446
2597
    {
2447
 
      free((unsigned char*)table);
2448
 
      pthread_mutex_unlock(&LOCK_open);
2449
 
      return(NULL);
 
2598
      my_free((uchar*)table, MYF(0));
 
2599
      VOID(pthread_mutex_unlock(&LOCK_open));
 
2600
      DBUG_RETURN(NULL);
2450
2601
    }
2451
2602
    if (error < 0)
2452
2603
    {
2453
 
      free((unsigned char*)table);
2454
 
      pthread_mutex_unlock(&LOCK_open);
2455
 
      return(0); // VIEW
 
2604
      my_free((uchar*)table, MYF(0));
 
2605
      VOID(pthread_mutex_unlock(&LOCK_open));
 
2606
      DBUG_RETURN(0); // VIEW
2456
2607
    }
2457
 
    my_hash_insert(&open_cache,(unsigned char*) table);
 
2608
    DBUG_PRINT("info", ("inserting table '%s'.'%s' 0x%lx into the cache",
 
2609
                        table->s->db.str, table->s->table_name.str,
 
2610
                        (long) table));
 
2611
    VOID(my_hash_insert(&open_cache,(uchar*) table));
2458
2612
  }
2459
2613
 
2460
 
  pthread_mutex_unlock(&LOCK_open);
 
2614
  check_unused();                               // Debugging call
 
2615
 
 
2616
  VOID(pthread_mutex_unlock(&LOCK_open));
2461
2617
  if (refresh)
2462
2618
  {
2463
2619
    table->next=thd->open_tables;               /* Link into simple list */
2466
2622
  table->reginfo.lock_type=TL_READ;             /* Assume read */
2467
2623
 
2468
2624
 reset:
2469
 
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
 
2625
  DBUG_ASSERT(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2470
2626
 
2471
2627
  if (thd->lex->need_correct_ident())
2472
2628
    table->alias_name_used= my_strcasecmp(table_alias_charset,
2474
2630
  /* Fix alias if table name changes */
2475
2631
  if (strcmp(table->alias, alias))
2476
2632
  {
2477
 
    uint32_t length=(uint) strlen(alias)+1;
 
2633
    uint length=(uint) strlen(alias)+1;
2478
2634
    table->alias= (char*) my_realloc((char*) table->alias, length,
2479
2635
                                     MYF(MY_WME));
2480
 
    memcpy((void*) table->alias, alias, length);
 
2636
    memcpy((char*) table->alias, alias, length);
2481
2637
  }
2482
2638
  /* These variables are also set in reopen_table() */
2483
2639
  table->tablenr=thd->current_tablenr++;
2484
2640
  table->used_fields=0;
2485
2641
  table->const_table=0;
2486
 
  table->null_row= false;
2487
 
  table->maybe_null= false;
2488
 
  table->force_index= false;
 
2642
  table->null_row= table->maybe_null= table->force_index= 0;
2489
2643
  table->status=STATUS_NO_RECORD;
2490
2644
  table->insert_values= 0;
 
2645
  table->fulltext_searched= 0;
 
2646
  table->file->ft_handler= 0;
2491
2647
  /* Catch wrong handling of the auto_increment_field_not_null. */
2492
 
  assert(!table->auto_increment_field_not_null);
 
2648
  DBUG_ASSERT(!table->auto_increment_field_not_null);
2493
2649
  table->auto_increment_field_not_null= false;
2494
2650
  if (table->timestamp_field)
2495
2651
    table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
2496
2652
  table->pos_in_table_list= table_list;
2497
2653
  table->clear_column_bitmaps();
2498
 
  assert(table->key_read == 0);
2499
 
  return(table);
 
2654
  DBUG_ASSERT(table->key_read == 0);
 
2655
  DBUG_RETURN(table);
2500
2656
}
2501
2657
 
2502
2658
 
2503
 
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
 
2659
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name)
2504
2660
{
2505
2661
  char  key[MAX_DBKEY_LENGTH];
2506
 
  uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
 
2662
  uint key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
2507
2663
 
2508
 
  for (Table *table=thd->open_tables; table ; table=table->next)
 
2664
  for (TABLE *table=thd->open_tables; table ; table=table->next)
2509
2665
  {
2510
2666
    if (table->s->table_cache_key.length == key_length &&
2511
2667
        !memcmp(table->s->table_cache_key.str, key, key_length))
2531
2687
   1  error. The old table object is not changed.
2532
2688
*/
2533
2689
 
2534
 
bool reopen_table(Table *table)
 
2690
bool reopen_table(TABLE *table)
2535
2691
{
2536
 
  Table tmp;
 
2692
  TABLE tmp;
2537
2693
  bool error= 1;
2538
2694
  Field **field;
2539
 
  uint32_t key,part;
2540
 
  TableList table_list;
 
2695
  uint key,part;
 
2696
  TABLE_LIST table_list;
2541
2697
  THD *thd= table->in_use;
 
2698
  DBUG_ENTER("reopen_table");
 
2699
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
 
2700
                        table->s->table_name.str, (long) table));
2542
2701
 
2543
 
  assert(table->s->ref_count == 0);
2544
 
  assert(!table->sort.io_cache);
 
2702
  DBUG_ASSERT(table->s->ref_count == 0);
 
2703
  DBUG_ASSERT(!table->sort.io_cache);
2545
2704
 
2546
2705
#ifdef EXTRA_DEBUG
2547
2706
  if (table->db_stat)
2548
 
    sql_print_error(_("Table %s had a open data handler in reopen_table"),
 
2707
    sql_print_error("Table %s had a open data handler in reopen_table",
2549
2708
                    table->alias);
2550
2709
#endif
2551
 
  memset(&table_list, 0, sizeof(TableList));
 
2710
  bzero((char*) &table_list, sizeof(TABLE_LIST));
2552
2711
  table_list.db=         table->s->db.str;
2553
2712
  table_list.table_name= table->s->table_name.str;
2554
2713
  table_list.table=      table;
2555
2714
 
2556
2715
  if (wait_for_locked_table_names(thd, &table_list))
2557
 
    return(1);                             // Thread was killed
 
2716
    DBUG_RETURN(1);                             // Thread was killed
2558
2717
 
2559
2718
  if (open_unireg_entry(thd, &tmp, &table_list,
2560
2719
                        table->alias,
2561
2720
                        table->s->table_cache_key.str,
2562
 
                        table->s->table_cache_key.length))
 
2721
                        table->s->table_cache_key.length,
 
2722
                        thd->mem_root, 0))
2563
2723
    goto end;
2564
2724
 
2565
2725
  /* This list copies variables set by open_table */
2581
2741
  tmp.prev=             table->prev;
2582
2742
 
2583
2743
  if (table->file)
2584
 
    closefrm(table, 1);         // close file, free everything
 
2744
    VOID(closefrm(table, 1));           // close file, free everything
2585
2745
 
2586
2746
  *table= tmp;
2587
2747
  table->default_column_bitmaps();
2588
2748
  table->file->change_table_ptr(table, table->s);
2589
2749
 
2590
 
  assert(table->alias != 0);
 
2750
  DBUG_ASSERT(table->alias != 0);
2591
2751
  for (field=table->field ; *field ; field++)
2592
2752
  {
2593
2753
    (*field)->table= (*field)->orig_table= table;
2608
2768
  error=0;
2609
2769
 
2610
2770
 end:
2611
 
  return(error);
 
2771
  DBUG_RETURN(error);
2612
2772
}
2613
2773
 
2614
2774
 
2623
2783
    @note This function assumes that if we are not under LOCK TABLES,
2624
2784
          then there is only one table open and locked. This means that
2625
2785
          the function probably has to be adjusted before it can be used
2626
 
          anywhere outside ALTER Table.
 
2786
          anywhere outside ALTER TABLE.
2627
2787
 
2628
2788
    @note Must not use TABLE_SHARE::table_name/db of the table being closed,
2629
2789
          the strings are used in a loop even after the share may be freed.
2632
2792
void close_data_files_and_morph_locks(THD *thd, const char *db,
2633
2793
                                      const char *table_name)
2634
2794
{
2635
 
  Table *table;
 
2795
  TABLE *table;
 
2796
  DBUG_ENTER("close_data_files_and_morph_locks");
2636
2797
 
2637
2798
  safe_mutex_assert_owner(&LOCK_open);
2638
2799
 
2648
2809
 
2649
2810
  /*
2650
2811
    Note that open table list may contain a name-lock placeholder
2651
 
    for target table name if we process ALTER Table ... RENAME.
 
2812
    for target table name if we process ALTER TABLE ... RENAME.
2652
2813
    So loop below makes sense even if we are not under LOCK TABLES.
2653
2814
  */
2654
2815
  for (table=thd->open_tables; table ; table=table->next)
2664
2825
      close_handle_and_leave_table_as_lock(table);
2665
2826
    }
2666
2827
  }
2667
 
  return;
 
2828
  DBUG_VOID_RETURN;
2668
2829
}
2669
2830
 
2670
2831
 
2678
2839
 
2679
2840
    @note Since this function can't properly handle prelocking and
2680
2841
          create placeholders it should be used in very special
2681
 
          situations like FLUSH TABLES or ALTER Table. In general
 
2842
          situations like FLUSH TABLES or ALTER TABLE. In general
2682
2843
          case one should just repeat open_tables()/lock_tables()
2683
2844
          combination when one needs tables to be reopened (for
2684
2845
          example see open_and_lock_tables()).
2690
2851
 
2691
2852
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2692
2853
{
2693
 
  Table *table,*next,**prev;
2694
 
  Table **tables,**tables_ptr;                  // For locks
 
2854
  TABLE *table,*next,**prev;
 
2855
  TABLE **tables,**tables_ptr;                  // For locks
2695
2856
  bool error=0, not_used;
2696
 
  const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2697
 
                    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2698
 
                    DRIZZLE_LOCK_IGNORE_FLUSH;
 
2857
  const uint flags= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN |
 
2858
                    MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK |
 
2859
                    MYSQL_LOCK_IGNORE_FLUSH;
 
2860
 
 
2861
  DBUG_ENTER("reopen_tables");
2699
2862
 
2700
2863
  if (!thd->open_tables)
2701
 
    return(0);
 
2864
    DBUG_RETURN(0);
2702
2865
 
2703
2866
  safe_mutex_assert_owner(&LOCK_open);
2704
2867
  if (get_locks)
2707
2870
      The ptr is checked later
2708
2871
      Do not handle locks of MERGE children.
2709
2872
    */
2710
 
    uint32_t opens=0;
 
2873
    uint opens=0;
2711
2874
    for (table= thd->open_tables; table ; table=table->next)
2712
2875
      opens++;
2713
 
    tables= (Table**) my_alloca(sizeof(Table*)*opens);
 
2876
    DBUG_PRINT("tcache", ("open tables to lock: %u", opens));
 
2877
    tables= (TABLE**) my_alloca(sizeof(TABLE*)*opens);
2714
2878
  }
2715
2879
  else
2716
2880
    tables= &thd->open_tables;
2719
2883
  prev= &thd->open_tables;
2720
2884
  for (table=thd->open_tables; table ; table=next)
2721
2885
  {
2722
 
    uint32_t db_stat=table->db_stat;
 
2886
    uint db_stat=table->db_stat;
2723
2887
    next=table->next;
2724
2888
    if (!tables || (!db_stat && reopen_table(table)))
2725
2889
    {
2726
2890
      my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2727
 
      hash_delete(&open_cache,(unsigned char*) table);
 
2891
      VOID(hash_delete(&open_cache,(uchar*) table));
2728
2892
      error=1;
2729
2893
    }
2730
2894
    else
2742
2906
    }
2743
2907
  }
2744
2908
  *prev=0;
 
2909
  DBUG_PRINT("tcache", ("open tables to lock: %u",
 
2910
                        (uint) (tables_ptr - tables)));
2745
2911
  if (tables != tables_ptr)                     // Should we get back old locks
2746
2912
  {
2747
 
    DRIZZLE_LOCK *lock;
 
2913
    MYSQL_LOCK *lock;
2748
2914
    /*
2749
2915
      We should always get these locks. Anyway, we must not go into
2750
2916
      wait_for_tables() as it tries to acquire LOCK_open, which is
2769
2935
  }
2770
2936
  if (get_locks && tables)
2771
2937
  {
2772
 
    my_afree((unsigned char*) tables);
 
2938
    my_afree((uchar*) tables);
2773
2939
  }
2774
2940
  broadcast_refresh();
2775
 
  return(error);
 
2941
  DBUG_RETURN(error);
2776
2942
}
2777
2943
 
2778
2944
 
2779
2945
/**
2780
 
    Close handlers for tables in list, but leave the Table structure
 
2946
    Close handlers for tables in list, but leave the TABLE structure
2781
2947
    intact so that we can re-open these quickly.
2782
2948
 
2783
2949
    @param thd           Thread context
2784
 
    @param table         Head of the list of Table objects
 
2950
    @param table         Head of the list of TABLE objects
2785
2951
    @param morph_locks   true  - remove locks which we have on tables being closed
2786
2952
                                 but ensure that no DML or DDL will sneak in before
2787
2953
                                 we will re-open the table (i.e. temporarily morph
2790
2956
    @param send_refresh  Should we awake waiters even if we didn't close any tables?
2791
2957
*/
2792
2958
 
2793
 
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
 
2959
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
2794
2960
                                 bool send_refresh)
2795
2961
{
2796
2962
  bool found= send_refresh;
 
2963
  DBUG_ENTER("close_old_data_files");
2797
2964
 
2798
2965
  for (; table ; table=table->next)
2799
2966
  {
 
2967
    DBUG_PRINT("tcache", ("checking table: '%s'.'%s' 0x%lx",
 
2968
                          table->s->db.str, table->s->table_name.str,
 
2969
                          (long) table));
 
2970
    DBUG_PRINT("tcache", ("needs refresh: %d  is open: %u",
 
2971
                          table->needs_reopen_or_name_lock(), table->db_stat));
2800
2972
    /*
2801
2973
      Reopen marked for flush.
2802
2974
    */
2807
2979
      {
2808
2980
        if (morph_locks)
2809
2981
        {
2810
 
          Table *ulcktbl= table;
 
2982
          TABLE *ulcktbl= table;
2811
2983
          if (ulcktbl->lock_count)
2812
2984
          {
2813
2985
            /*
2833
3005
          }
2834
3006
          /*
2835
3007
            We want to protect the table from concurrent DDL operations
2836
 
            (like RENAME Table) until we will re-open and re-lock it.
 
3008
            (like RENAME TABLE) until we will re-open and re-lock it.
2837
3009
          */
2838
3010
          table->open_placeholder= 1;
2839
3011
        }
2845
3017
          We come here only in close-for-back-off scenario. So we have to
2846
3018
          "close" create placeholder here to avoid deadlocks (for example,
2847
3019
          in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
2848
 
          and RENAME Table t2 TO t1). In close-for-re-open scenario we will
 
3020
          and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will
2849
3021
          probably want to let it stay.
2850
3022
 
2851
3023
          Note "***": We must not enter this branch if the placeholder
2858
3030
  }
2859
3031
  if (found)
2860
3032
    broadcast_refresh();
2861
 
  return;
 
3033
  DBUG_VOID_RETURN;
2862
3034
}
2863
3035
 
2864
3036
 
2868
3040
  if the table is closed
2869
3041
*/
2870
3042
 
2871
 
bool table_is_used(Table *table, bool wait_for_name_lock)
 
3043
bool table_is_used(TABLE *table, bool wait_for_name_lock)
2872
3044
{
 
3045
  DBUG_ENTER("table_is_used");
2873
3046
  do
2874
3047
  {
2875
3048
    char *key= table->s->table_cache_key.str;
2876
 
    uint32_t key_length= table->s->table_cache_key.length;
 
3049
    uint key_length= table->s->table_cache_key.length;
2877
3050
 
 
3051
    DBUG_PRINT("loop", ("table_name: %s", table->alias));
2878
3052
    HASH_SEARCH_STATE state;
2879
 
    for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
 
3053
    for (TABLE *search= (TABLE*) hash_first(&open_cache, (uchar*) key,
2880
3054
                                             key_length, &state);
2881
3055
         search ;
2882
 
         search= (Table*) hash_next(&open_cache, (unsigned char*) key,
 
3056
         search= (TABLE*) hash_next(&open_cache, (uchar*) key,
2883
3057
                                    key_length, &state))
2884
3058
    {
 
3059
      DBUG_PRINT("info", ("share: 0x%lx  "
 
3060
                          "open_placeholder: %d  locked_by_name: %d "
 
3061
                          "db_stat: %u  version: %lu",
 
3062
                          (ulong) search->s,
 
3063
                          search->open_placeholder, search->locked_by_name,
 
3064
                          search->db_stat,
 
3065
                          search->s->version));
2885
3066
      if (search->in_use == table->in_use)
2886
3067
        continue;                               // Name locked by this thread
2887
3068
      /*
2893
3074
      */
2894
3075
      if ( (search->locked_by_name && wait_for_name_lock) ||
2895
3076
           (search->is_name_opened() && search->needs_reopen_or_name_lock()))
2896
 
        return(1);
 
3077
        DBUG_RETURN(1);
2897
3078
    }
2898
3079
  } while ((table=table->next));
2899
 
  return(0);
 
3080
  DBUG_RETURN(0);
2900
3081
}
2901
3082
 
2902
3083
 
2905
3086
bool wait_for_tables(THD *thd)
2906
3087
{
2907
3088
  bool result;
 
3089
  DBUG_ENTER("wait_for_tables");
2908
3090
 
2909
 
  thd->set_proc_info("Waiting for tables");
 
3091
  thd_proc_info(thd, "Waiting for tables");
2910
3092
  pthread_mutex_lock(&LOCK_open);
2911
3093
  while (!thd->killed)
2912
3094
  {
2922
3104
  else
2923
3105
  {
2924
3106
    /* Now we can open all tables without any interference */
2925
 
    thd->set_proc_info("Reopen tables");
 
3107
    thd_proc_info(thd, "Reopen tables");
2926
3108
    thd->version= refresh_version;
2927
3109
    result=reopen_tables(thd,0,0);
2928
3110
  }
2929
3111
  pthread_mutex_unlock(&LOCK_open);
2930
 
  thd->set_proc_info(0);
2931
 
  return(result);
 
3112
  thd_proc_info(thd, 0);
 
3113
  DBUG_RETURN(result);
2932
3114
}
2933
3115
 
2934
3116
 
2944
3126
  INFORMATION
2945
3127
    This is only called on drop tables
2946
3128
 
2947
 
    The Table object for the dropped table is unlocked but still kept around
 
3129
    The TABLE object for the dropped table is unlocked but still kept around
2948
3130
    as a name lock, which means that the table will be available for other
2949
3131
    thread as soon as we call unlock_table_names().
2950
3132
    If there is multiple copies of the table locked, all copies except
2956
3138
*/
2957
3139
 
2958
3140
 
2959
 
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
 
3141
TABLE *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2960
3142
{
2961
 
  Table *table,*next,**prev, *found= 0;
 
3143
  TABLE *table,*next,**prev, *found= 0;
2962
3144
  prev= &thd->open_tables;
 
3145
  DBUG_ENTER("drop_locked_tables");
2963
3146
 
2964
3147
  /*
2965
3148
    Note that we need to hold LOCK_open while changing the
2989
3172
      else
2990
3173
      {
2991
3174
        /* We already have a name lock, remove copy */
2992
 
        hash_delete(&open_cache,(unsigned char*) table);
 
3175
        VOID(hash_delete(&open_cache,(uchar*) table));
2993
3176
      }
2994
3177
    }
2995
3178
    else
3003
3186
    broadcast_refresh();
3004
3187
  if (thd->locked_tables && thd->locked_tables->table_count == 0)
3005
3188
  {
3006
 
    free((unsigned char*) thd->locked_tables);
 
3189
    my_free((uchar*) thd->locked_tables,MYF(0));
3007
3190
    thd->locked_tables=0;
3008
3191
  }
3009
 
  return(found);
 
3192
  DBUG_RETURN(found);
3010
3193
}
3011
3194
 
3012
3195
 
3013
3196
/*
3014
 
  If we have the table open, which only happens when a LOCK Table has been
 
3197
  If we have the table open, which only happens when a LOCK TABLE has been
3015
3198
  done on the table, change the lock type to a lock that will abort all
3016
3199
  other threads trying to get the lock.
3017
3200
*/
3018
3201
 
3019
3202
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
3020
3203
{
3021
 
  Table *table;
 
3204
  TABLE *table;
3022
3205
  for (table= thd->open_tables; table ; table= table->next)
3023
3206
  {
3024
3207
    if (!strcmp(table->s->table_name.str, table_name) &&
3059
3242
    share opens have been executed while one table was open all the
3060
3243
    time).
3061
3244
 
3062
 
    share->table_map_id is not UINT32_MAX.
 
3245
    share->table_map_id is not ~0UL.
3063
3246
 */
3064
3247
void assign_new_table_id(TABLE_SHARE *share)
3065
3248
{
3066
 
  static uint32_t last_table_id= UINT32_MAX;
 
3249
  static ulong last_table_id= ~0UL;
 
3250
 
 
3251
  DBUG_ENTER("assign_new_table_id");
3067
3252
 
3068
3253
  /* Preconditions */
3069
 
  assert(share != NULL);
 
3254
  DBUG_ASSERT(share != NULL);
3070
3255
  safe_mutex_assert_owner(&LOCK_open);
3071
3256
 
3072
3257
  ulong tid= ++last_table_id;                   /* get next id */
3074
3259
    There is one reserved number that cannot be used.  Remember to
3075
3260
    change this when 6-byte global table id's are introduced.
3076
3261
  */
3077
 
  if (unlikely(tid == UINT32_MAX))
 
3262
  if (unlikely(tid == ~0UL))
3078
3263
    tid= ++last_table_id;
3079
3264
  share->table_map_id= tid;
 
3265
  DBUG_PRINT("info", ("table_id=%lu", tid));
3080
3266
 
3081
3267
  /* Post conditions */
3082
 
  assert(share->table_map_id != UINT32_MAX);
 
3268
  DBUG_ASSERT(share->table_map_id != ~0UL);
3083
3269
 
3084
 
  return;
 
3270
  DBUG_VOID_RETURN;
3085
3271
}
3086
3272
 
3087
3273
/*
3091
3277
    open_unireg_entry()
3092
3278
    thd                 Thread handle
3093
3279
    entry               Store open table definition here
3094
 
    table_list          TableList with db, table_name
 
3280
    table_list          TABLE_LIST with db, table_name & belong_to_view
3095
3281
    alias               Alias name
3096
3282
    cache_key           Key for share_cache
3097
3283
    cache_key_length    length of cache_key
 
3284
    mem_root            temporary mem_root for parsing
 
3285
    flags               the OPEN_VIEW_NO_PARSE flag to be passed to
 
3286
                        openfrm()/open_new_frm()
3098
3287
 
3099
3288
  NOTES
3100
3289
   Extra argument for open is taken from thd->open_options
3105
3294
    #   Error
3106
3295
*/
3107
3296
 
3108
 
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
 
3297
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
3109
3298
                             const char *alias,
3110
 
                             char *cache_key, uint32_t cache_key_length)
 
3299
                             char *cache_key, uint cache_key_length,
 
3300
                             MEM_ROOT *mem_root, uint flags)
3111
3301
{
3112
3302
  int error;
3113
3303
  TABLE_SHARE *share;
3114
 
  uint32_t discover_retry_count= 0;
 
3304
  uint discover_retry_count= 0;
 
3305
  DBUG_ENTER("open_unireg_entry");
3115
3306
 
3116
3307
  safe_mutex_assert_owner(&LOCK_open);
3117
3308
retry:
3118
3309
  if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3119
3310
                                           cache_key_length, 
 
3311
                                           OPEN_VIEW |
3120
3312
                                           table_list->i_s_requested_object,
3121
3313
                                           &error)))
3122
 
    return(1);
 
3314
    DBUG_RETURN(1);
3123
3315
 
3124
3316
  while ((error= open_table_from_share(thd, share, alias,
3125
3317
                                       (uint) (HA_OPEN_KEYFILE |
3126
3318
                                               HA_OPEN_RNDFILE |
3127
3319
                                               HA_GET_INDEX |
3128
3320
                                               HA_TRY_READ_ONLY),
3129
 
                                       (EXTRA_RECORD),
 
3321
                                       (READ_KEYINFO | COMPUTE_TYPES |
 
3322
                                        EXTRA_RECORD),
3130
3323
                                       thd->open_options, entry, OTM_OPEN)))
3131
3324
  {
3132
3325
    if (error == 7)                             // Table def changed
3162
3355
      release_table_share(share, RELEASE_WAIT_FOR_DROP);
3163
3356
      if (!thd->killed)
3164
3357
      {
3165
 
        drizzle_reset_errors(thd, 1);         // Clear warnings
 
3358
        mysql_reset_errors(thd, 1);         // Clear warnings
3166
3359
        thd->clear_error();                 // Clear error message
3167
3360
        goto retry;
3168
3361
      }
3169
 
      return(1);
 
3362
      DBUG_RETURN(1);
3170
3363
    }
3171
3364
    if (!entry->s || !entry->s->crashed)
3172
3365
      goto err;
3188
3381
                               (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3189
3382
                                       HA_GET_INDEX |
3190
3383
                                       HA_TRY_READ_ONLY),
3191
 
                               EXTRA_RECORD,
 
3384
                               READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD,
3192
3385
                               ha_open_options | HA_OPEN_FOR_REPAIR,
3193
3386
                               entry, OTM_OPEN) || ! entry->file ||
3194
3387
        (entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3196
3389
       /* Give right error message */
3197
3390
       thd->clear_error();
3198
3391
       my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3199
 
       sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
 
3392
       sql_print_error("Couldn't repair table: %s.%s", share->db.str,
3200
3393
                       share->table_name.str);
3201
3394
       if (entry->file)
3202
3395
        closefrm(entry, 0);
3222
3415
    if (mysql_bin_log.is_open())
3223
3416
    {
3224
3417
      char *query, *end;
3225
 
      uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
 
3418
      uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3226
3419
      if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3227
3420
      {
3228
3421
        /* this DELETE FROM is needed even with row-based binlogging */
3229
 
        end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3230
 
                      share->db.str,"`.`",share->table_name.str,"`", NULL);
 
3422
        end = strxmov(strmov(query, "DELETE FROM `"),
 
3423
                      share->db.str,"`.`",share->table_name.str,"`", NullS);
3231
3424
        thd->binlog_query(THD::STMT_QUERY_TYPE,
3232
3425
                          query, (ulong)(end-query), false, false);
3233
 
        free(query);
 
3426
        my_free(query, MYF(0));
3234
3427
      }
3235
3428
      else
3236
3429
      {
3239
3432
          DBA on top of warning the client (which will automatically be done
3240
3433
          because of MYF(MY_WME) in my_malloc() above).
3241
3434
        */
3242
 
        sql_print_error(_("When opening HEAP table, could not allocate memory "
3243
 
                          "to write 'DELETE FROM `%s`.`%s`' to the binary log"),
 
3435
        sql_print_error("When opening HEAP table, could not allocate memory "
 
3436
                        "to write 'DELETE FROM `%s`.`%s`' to the binary log",
3244
3437
                        table_list->db, table_list->table_name);
3245
3438
        closefrm(entry, 0);
3246
3439
        goto err;
3247
3440
      }
3248
3441
    }
3249
3442
  }
3250
 
  return(0);
 
3443
  DBUG_RETURN(0);
3251
3444
 
3252
3445
err:
3253
3446
  release_table_share(share, RELEASE_NORMAL);
3254
 
  return(1);
 
3447
  DBUG_RETURN(1);
3255
3448
}
3256
3449
 
3257
3450
 
3264
3457
    start - list of tables in/out
3265
3458
    counter - number of opened tables will be return using this parameter
3266
3459
    flags   - bitmap of flags to modify how the tables will be open:
3267
 
              DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
3460
              MYSQL_LOCK_IGNORE_FLUSH - open table even if someone has
3268
3461
              done a flush or namelock on it.
3269
3462
 
3270
3463
  NOTE
3283
3476
    -1 - error
3284
3477
*/
3285
3478
 
3286
 
int open_tables(THD *thd, TableList **start, uint32_t *counter, uint32_t flags)
 
3479
int open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags)
3287
3480
{
3288
 
  TableList *tables= NULL;
 
3481
  TABLE_LIST *tables= NULL;
3289
3482
  bool refresh;
3290
3483
  int result=0;
3291
3484
  MEM_ROOT new_frm_mem;
3292
3485
  /* Also used for indicating that prelocking is need */
3293
3486
  bool safe_to_ignore_table;
3294
3487
 
 
3488
  DBUG_ENTER("open_tables");
3295
3489
  /*
3296
3490
    temporary mem_root for new .frm parsing.
3297
3491
    TODO: variables for size
3301
3495
  thd->current_tablenr= 0;
3302
3496
 restart:
3303
3497
  *counter= 0;
3304
 
  thd->set_proc_info("Opening tables");
 
3498
  thd_proc_info(thd, "Opening tables");
3305
3499
 
3306
3500
  /*
3307
3501
    For every table in the list of tables to open, try to find or open
3309
3503
  */
3310
3504
  for (tables= *start; tables ;tables= tables->next_global)
3311
3505
  {
 
3506
    DBUG_PRINT("tcache", ("opening table: '%s'.'%s'  item: 0x%lx",
 
3507
                          tables->db, tables->table_name, (long) tables));
 
3508
 
3312
3509
    safe_to_ignore_table= false;
3313
3510
 
3314
3511
    /*
3322
3519
      continue;
3323
3520
    }
3324
3521
    /*
3325
 
      If this TableList object is a placeholder for an information_schema
 
3522
      If this TABLE_LIST object is a placeholder for an information_schema
3326
3523
      table, create a temporary table to represent the information_schema
3327
3524
      table in the query. Do not fill it yet - will be filled during
3328
3525
      execution.
3331
3528
    {
3332
3529
      if (!mysql_schema_table(thd, thd->lex, tables))
3333
3530
        continue;
3334
 
      return(-1);
 
3531
      DBUG_RETURN(-1);
3335
3532
    }
3336
3533
    (*counter)++;
3337
3534
 
3340
3537
      not opened yet. Try to open the table.
3341
3538
    */
3342
3539
    if (!tables->table)
3343
 
      tables->table= open_table(thd, tables, &refresh, flags);
 
3540
    {
 
3541
      tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
 
3542
    }
 
3543
    else
 
3544
      DBUG_PRINT("tcache", ("referenced table: '%s'.'%s' 0x%lx",
 
3545
                            tables->db, tables->table_name,
 
3546
                            (long) tables->table));
3344
3547
 
3345
3548
    if (!tables->table)
3346
3549
    {
3367
3570
      }
3368
3571
 
3369
3572
      if (safe_to_ignore_table)
 
3573
      {
 
3574
        DBUG_PRINT("info", ("open_table: ignoring table '%s'.'%s'",
 
3575
                            tables->db, tables->alias));
3370
3576
        continue;
 
3577
      }
3371
3578
 
3372
3579
      result= -1;                               // Fatal error
3373
3580
      break;
3381
3588
    }
3382
3589
  }
3383
3590
 
3384
 
  thd->set_proc_info(0);
 
3591
  thd_proc_info(thd, 0);
3385
3592
  free_root(&new_frm_mem, MYF(0));              // Free pre-alloced block
3386
3593
 
3387
3594
  if (result && tables)
3388
3595
  {
3389
3596
    /*
3390
3597
      Some functions determine success as (tables->table != NULL).
3391
 
      tables->table is in thd->open_tables.
 
3598
      tables->table is in thd->open_tables. It won't go lost. If the
 
3599
      error happens on a MERGE child, clear the parents TABLE reference.
3392
3600
    */
 
3601
    if (tables->parent_l)
 
3602
      tables->parent_l->table= NULL;
3393
3603
    tables->table= NULL;
3394
3604
  }
3395
 
  return(result);
 
3605
  DBUG_PRINT("tcache", ("returning: %d", result));
 
3606
  DBUG_RETURN(result);
3396
3607
}
3397
3608
 
3398
3609
 
3410
3621
  1     error
3411
3622
*/
3412
3623
 
3413
 
static bool check_lock_and_start_stmt(THD *thd, Table *table,
 
3624
static bool check_lock_and_start_stmt(THD *thd, TABLE *table,
3414
3625
                                      thr_lock_type lock_type)
3415
3626
{
3416
3627
  int error;
 
3628
  DBUG_ENTER("check_lock_and_start_stmt");
3417
3629
 
3418
3630
  if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3419
3631
      (int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3420
3632
  {
3421
3633
    my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3422
 
    return(1);
 
3634
    DBUG_RETURN(1);
3423
3635
  }
3424
3636
  if ((error=table->file->start_stmt(thd, lock_type)))
3425
3637
  {
3426
3638
    table->file->print_error(error,MYF(0));
3427
 
    return(1);
 
3639
    DBUG_RETURN(1);
3428
3640
  }
3429
 
  return(0);
 
3641
  DBUG_RETURN(0);
3430
3642
}
3431
3643
 
3432
3644
 
3460
3672
    and locking issues because it does not call lock_tables().
3461
3673
*/
3462
3674
 
3463
 
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
 
3675
TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
3464
3676
                                thr_lock_type lock_type)
3465
3677
{
3466
 
  TableList *save_next_global;
 
3678
  TABLE_LIST *save_next_global;
 
3679
  DBUG_ENTER("open_n_lock_single_table");
3467
3680
 
3468
3681
  /* Remember old 'next' pointer. */
3469
3682
  save_next_global= table_l->next_global;
3472
3685
 
3473
3686
  /* Set requested lock type. */
3474
3687
  table_l->lock_type= lock_type;
 
3688
  /* Allow to open real tables only. */
 
3689
  table_l->required_type= FRMTYPE_TABLE;
3475
3690
 
3476
3691
  /* Open the table. */
3477
3692
  if (simple_open_n_lock_tables(thd, table_l))
3480
3695
  /* Restore list. */
3481
3696
  table_l->next_global= save_next_global;
3482
3697
 
3483
 
  return(table_l->table);
 
3698
  DBUG_RETURN(table_l->table);
3484
3699
}
3485
3700
 
3486
3701
 
3508
3723
      table_list->table         table
3509
3724
*/
3510
3725
 
3511
 
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3512
 
                   uint32_t lock_flags)
 
3726
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type,
 
3727
                   uint lock_flags)
3513
3728
{
3514
 
  Table *table;
 
3729
  TABLE *table;
3515
3730
  bool refresh;
 
3731
  DBUG_ENTER("open_ltable");
3516
3732
 
3517
 
  thd->set_proc_info("Opening table");
 
3733
  thd_proc_info(thd, "Opening table");
3518
3734
  thd->current_tablenr= 0;
3519
 
  while (!(table= open_table(thd, table_list, &refresh, 0)) &&
 
3735
  /* open_ltable can be used only for BASIC TABLEs */
 
3736
  table_list->required_type= FRMTYPE_TABLE;
 
3737
  while (!(table= open_table(thd, table_list, thd->mem_root, &refresh, 0)) &&
3520
3738
         refresh)
3521
3739
    ;
3522
3740
 
3531
3749
    }
3532
3750
    else
3533
3751
    {
3534
 
      assert(thd->lock == 0);   // You must lock everything at once
 
3752
      DBUG_ASSERT(thd->lock == 0);      // You must lock everything at once
3535
3753
      if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3536
3754
        if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
3537
3755
                                            lock_flags, &refresh)))
3539
3757
    }
3540
3758
  }
3541
3759
 
3542
 
  thd->set_proc_info(0);
3543
 
  return(table);
 
3760
  thd_proc_info(thd, 0);
 
3761
  DBUG_RETURN(table);
3544
3762
}
3545
3763
 
3546
3764
 
3568
3786
    the third argument set appropriately.
3569
3787
*/
3570
3788
 
3571
 
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
 
3789
int open_and_lock_tables_derived(THD *thd, TABLE_LIST *tables, bool derived)
3572
3790
{
3573
 
  uint32_t counter;
 
3791
  uint counter;
3574
3792
  bool need_reopen;
 
3793
  DBUG_ENTER("open_and_lock_tables_derived");
 
3794
  DBUG_PRINT("enter", ("derived handling: %d", derived));
3575
3795
 
3576
3796
  for ( ; ; ) 
3577
3797
  {
3578
3798
    if (open_tables(thd, &tables, &counter, 0))
3579
 
      return(-1);
 
3799
      DBUG_RETURN(-1);
 
3800
 
 
3801
    DBUG_EXECUTE_IF("sleep_open_and_lock_after_open", {
 
3802
      const char *old_proc_info= thd->proc_info;
 
3803
      thd->proc_info= "DBUG sleep";
 
3804
      my_sleep(6000000);
 
3805
      thd->proc_info= old_proc_info;});
3580
3806
 
3581
3807
    if (!lock_tables(thd, tables, counter, &need_reopen))
3582
3808
      break;
3583
3809
    if (!need_reopen)
3584
 
      return(-1);
 
3810
      DBUG_RETURN(-1);
3585
3811
    close_tables_for_reopen(thd, &tables);
3586
3812
  }
3587
3813
  if (derived &&
3588
3814
      (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3589
3815
       (thd->fill_derived_tables() &&
3590
3816
        mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3591
 
    return(true); /* purecov: inspected */
3592
 
  return(0);
 
3817
    DBUG_RETURN(true); /* purecov: inspected */
 
3818
  DBUG_RETURN(0);
3593
3819
}
3594
3820
 
3595
3821
 
3601
3827
    thd         - thread handler
3602
3828
    tables      - list of tables for open
3603
3829
    flags       - bitmap of flags to modify how the tables will be open:
3604
 
                  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
3830
                  MYSQL_LOCK_IGNORE_FLUSH - open table even if someone has
3605
3831
                  done a flush or namelock on it.
3606
3832
 
3607
3833
  RETURN
3613
3839
    data from the tables.
3614
3840
*/
3615
3841
 
3616
 
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint32_t flags)
 
3842
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags)
3617
3843
{
3618
 
  uint32_t counter;
3619
 
  assert(!thd->fill_derived_tables());
 
3844
  uint counter;
 
3845
  DBUG_ENTER("open_normal_and_derived_tables");
 
3846
  DBUG_ASSERT(!thd->fill_derived_tables());
3620
3847
  if (open_tables(thd, &tables, &counter, flags) ||
3621
3848
      mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3622
 
    return(true); /* purecov: inspected */
3623
 
  return(0);
 
3849
    DBUG_RETURN(true); /* purecov: inspected */
 
3850
  DBUG_RETURN(0);
3624
3851
}
3625
3852
 
3626
3853
 
3661
3888
   @param tables Tables involved in the query
3662
3889
 */
3663
3890
 
3664
 
int decide_logging_format(THD *thd, TableList *tables)
 
3891
int decide_logging_format(THD *thd, TABLE_LIST *tables)
3665
3892
{
3666
3893
  if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3667
3894
  {
3668
3895
    handler::Table_flags flags_some_set= handler::Table_flags();
3669
3896
    handler::Table_flags flags_all_set= ~handler::Table_flags();
3670
 
    bool multi_engine= false;
 
3897
    my_bool multi_engine= false;
3671
3898
    void* prev_ht= NULL;
3672
 
    for (TableList *table= tables; table; table= table->next_global)
 
3899
    for (TABLE_LIST *table= tables; table; table= table->next_global)
3673
3900
    {
3674
3901
      if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3675
3902
      {
3676
 
        uint64_t const flags= table->table->file->ha_table_flags();
 
3903
        ulonglong const flags= table->table->file->ha_table_flags();
 
3904
        DBUG_PRINT("info", ("table: %s; ha_table_flags: %s%s",
 
3905
                            table->table_name,
 
3906
                            FLAGSTR(flags, HA_BINLOG_STMT_CAPABLE),
 
3907
                            FLAGSTR(flags, HA_BINLOG_ROW_CAPABLE)));
3677
3908
        if (prev_ht && prev_ht != table->table->file->ht)
3678
3909
          multi_engine= true;
3679
3910
        prev_ht= table->table->file->ht;
3682
3913
      }
3683
3914
    }
3684
3915
 
 
3916
    DBUG_PRINT("info", ("flags_all_set: %s%s",
 
3917
                        FLAGSTR(flags_all_set, HA_BINLOG_STMT_CAPABLE),
 
3918
                        FLAGSTR(flags_all_set, HA_BINLOG_ROW_CAPABLE)));
 
3919
    DBUG_PRINT("info", ("flags_some_set: %s%s",
 
3920
                        FLAGSTR(flags_some_set, HA_BINLOG_STMT_CAPABLE),
 
3921
                        FLAGSTR(flags_some_set, HA_BINLOG_ROW_CAPABLE)));
 
3922
    DBUG_PRINT("info", ("thd->variables.binlog_format: %ld",
 
3923
                        thd->variables.binlog_format));
 
3924
    DBUG_PRINT("info", ("multi_engine: %s",
 
3925
                        multi_engine ? "true" : "false"));
 
3926
 
3685
3927
    int error= 0;
3686
3928
    if (flags_all_set == 0)
3687
3929
    {
3705
3947
                " but not allowed by this combination of engines");
3706
3948
    }
3707
3949
 
 
3950
    /*
 
3951
      If more than one engine is involved in the statement and at
 
3952
      least one is doing it's own logging (is *self-logging*), the
 
3953
      statement cannot be logged atomically, so we generate an error
 
3954
      rather than allowing the binlog to become corrupt.
 
3955
     */
 
3956
    if (multi_engine &&
 
3957
        (flags_some_set & HA_HAS_OWN_BINLOGGING))
 
3958
    {
 
3959
      error= ER_BINLOG_LOGGING_IMPOSSIBLE;
 
3960
      my_error(error, MYF(0),
 
3961
               "Statement cannot be written atomically since more"
 
3962
               " than one engine involved and at least one engine"
 
3963
               " is self-logging");
 
3964
    }
 
3965
 
 
3966
    DBUG_PRINT("info", ("error: %d", error));
 
3967
 
3708
3968
    if (error)
3709
3969
      return -1;
3710
3970
 
3758
4018
   -1   Error
3759
4019
*/
3760
4020
 
3761
 
int lock_tables(THD *thd, TableList *tables, uint32_t count, bool *need_reopen)
 
4021
int lock_tables(THD *thd, TABLE_LIST *tables, uint count, bool *need_reopen)
3762
4022
{
3763
 
  TableList *table;
 
4023
  TABLE_LIST *table;
3764
4024
 
 
4025
  DBUG_ENTER("lock_tables");
3765
4026
  /*
3766
4027
    We can't meet statement requiring prelocking if we already
3767
4028
    in prelocked mode.
3769
4030
  *need_reopen= false;
3770
4031
 
3771
4032
  if (!tables)
3772
 
    return(decide_logging_format(thd, tables));
 
4033
    DBUG_RETURN(decide_logging_format(thd, tables));
3773
4034
 
3774
4035
  if (!thd->locked_tables)
3775
4036
  {
3776
 
    assert(thd->lock == 0);     // You must lock everything at once
3777
 
    Table **start,**ptr;
3778
 
    uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
 
4037
    DBUG_ASSERT(thd->lock == 0);        // You must lock everything at once
 
4038
    TABLE **start,**ptr;
 
4039
    uint lock_flag= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN;
3779
4040
 
3780
 
    if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
3781
 
      return(-1);
 
4041
    if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
 
4042
      DBUG_RETURN(-1);
3782
4043
    for (table= tables; table; table= table->next_global)
3783
4044
    {
3784
4045
      if (!table->placeholder())
3788
4049
    if (!(thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
3789
4050
                                       lock_flag, need_reopen)))
3790
4051
    {
3791
 
      return(-1);
 
4052
      DBUG_RETURN(-1);
3792
4053
    }
3793
4054
  }
3794
4055
  else
3795
4056
  {
3796
 
    TableList *first_not_own= thd->lex->first_not_own_table();
 
4057
    TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
3797
4058
    /*
3798
4059
      When open_and_lock_tables() is called for a single table out of
3799
4060
      a table list, the 'next_global' chain is temporarily broken. We
3809
4070
      if (!table->placeholder() &&
3810
4071
          check_lock_and_start_stmt(thd, table->table, table->lock_type))
3811
4072
      {
3812
 
        return(-1);
 
4073
        DBUG_RETURN(-1);
3813
4074
      }
3814
4075
    }
3815
4076
  }
3816
4077
 
3817
 
  return(decide_logging_format(thd, tables));
 
4078
  DBUG_RETURN(decide_logging_format(thd, tables));
3818
4079
}
3819
4080
 
3820
4081
 
3829
4090
 
3830
4091
*/
3831
4092
 
3832
 
void close_tables_for_reopen(THD *thd, TableList **tables)
 
4093
void close_tables_for_reopen(THD *thd, TABLE_LIST **tables)
3833
4094
{
3834
4095
  /*
3835
4096
    If table list consists only from tables from prelocking set, table list
3838
4099
  if (thd->lex->first_not_own_table() == *tables)
3839
4100
    *tables= 0;
3840
4101
  thd->lex->chop_off_not_own_tables();
3841
 
  for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
 
4102
  for (TABLE_LIST *tmp= *tables; tmp; tmp= tmp->next_global)
3842
4103
    tmp->table= 0;
3843
4104
  close_thread_tables(thd);
3844
4105
}
3861
4122
 
3862
4123
 RETURN
3863
4124
   0  Error
3864
 
   #  Table object
 
4125
   #  TABLE object
3865
4126
*/
3866
4127
 
3867
 
Table *open_temporary_table(THD *thd, const char *path, const char *db,
 
4128
TABLE *open_temporary_table(THD *thd, const char *path, const char *db,
3868
4129
                            const char *table_name, bool link_in_list,
3869
4130
                            open_table_mode open_mode)
3870
4131
{
3871
 
  Table *tmp_table;
 
4132
  TABLE *tmp_table;
3872
4133
  TABLE_SHARE *share;
3873
4134
  char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3874
 
  uint32_t key_length;
3875
 
  TableList table_list;
 
4135
  uint key_length;
 
4136
  TABLE_LIST table_list;
 
4137
  DBUG_ENTER("open_temporary_table");
 
4138
  DBUG_PRINT("enter",
 
4139
             ("table: '%s'.'%s'  path: '%s'  server_id: %u  "
 
4140
              "pseudo_thread_id: %lu",
 
4141
              db, table_name, path,
 
4142
              (uint) thd->server_id, (ulong) thd->variables.pseudo_thread_id));
3876
4143
 
3877
4144
  table_list.db=         (char*) db;
3878
4145
  table_list.table_name= (char*) table_name;
3879
4146
  /* Create the cache_key for temporary tables */
3880
4147
  key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3881
4148
 
3882
 
  if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
 
4149
  if (!(tmp_table= (TABLE*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3883
4150
                                      strlen(path)+1 + key_length,
3884
4151
                                      MYF(MY_WME))))
3885
 
    return(0);                          /* purecov: inspected */
 
4152
    DBUG_RETURN(0);                             /* purecov: inspected */
3886
4153
 
3887
4154
  share= (TABLE_SHARE*) (tmp_table+1);
3888
4155
  tmp_path= (char*) (share+1);
3889
 
  saved_cache_key= my_stpcpy(tmp_path, path)+1;
 
4156
  saved_cache_key= strmov(tmp_path, path)+1;
3890
4157
  memcpy(saved_cache_key, cache_key, key_length);
3891
4158
 
3892
4159
  init_tmp_table_share(thd, share, saved_cache_key, key_length,
3893
 
                       strchr(saved_cache_key, '\0')+1, tmp_path);
 
4160
                       strend(saved_cache_key)+1, tmp_path);
3894
4161
 
3895
4162
  if (open_table_def(thd, share, 0) ||
3896
4163
      open_table_from_share(thd, share, table_name,
3898
4165
                            (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3899
4166
                                    HA_GET_INDEX),
3900
4167
                            (open_mode == OTM_ALTER) ?
3901
 
                              (EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3902
 
                            : (EXTRA_RECORD),
 
4168
                              (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD |
 
4169
                               OPEN_FRM_FILE_ONLY)
 
4170
                            : (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD),
3903
4171
                            ha_open_options,
3904
4172
                            tmp_table, open_mode))
3905
4173
  {
3906
4174
    /* No need to lock share->mutex as this is not needed for tmp tables */
3907
4175
    free_table_share(share);
3908
 
    free((char*) tmp_table);
3909
 
    return(0);
 
4176
    my_free((char*) tmp_table,MYF(0));
 
4177
    DBUG_RETURN(0);
3910
4178
  }
3911
4179
 
3912
4180
  tmp_table->reginfo.lock_type= TL_WRITE;        // Simulate locked
3934
4202
      slave_open_temp_tables++;
3935
4203
  }
3936
4204
  tmp_table->pos_in_table_list= 0;
3937
 
  return(tmp_table);
 
4205
  DBUG_PRINT("tmptable", ("opened table: '%s'.'%s' 0x%lx", tmp_table->s->db.str,
 
4206
                          tmp_table->s->table_name.str, (long) tmp_table));
 
4207
  DBUG_RETURN(tmp_table);
3938
4208
}
3939
4209
 
3940
4210
 
3943
4213
  bool error=0;
3944
4214
  handler *file;
3945
4215
  char *ext;
 
4216
  DBUG_ENTER("rm_temporary_table");
3946
4217
 
3947
 
  my_stpcpy(ext= strchr(path, '\0'), reg_ext);
 
4218
  strmov(ext= strend(path), reg_ext);
3948
4219
  if (my_delete(path,MYF(0)))
3949
4220
    error=1; /* purecov: inspected */
3950
4221
  *ext= 0;                              // remove extension
3952
4223
  if (!frm_only && file && file->ha_delete_table(path))
3953
4224
  {
3954
4225
    error=1;
3955
 
    sql_print_warning(_("Could not remove temporary table: '%s', error: %d"),
 
4226
    sql_print_warning("Could not remove temporary table: '%s', error: %d",
3956
4227
                      path, my_errno);
3957
4228
  }
3958
4229
  delete file;
3959
 
  return(error);
 
4230
  DBUG_RETURN(error);
3960
4231
}
3961
4232
 
3962
4233
 
3975
4246
 
3976
4247
#define WRONG_GRANT (Field*) -1
3977
4248
 
3978
 
static void update_field_dependencies(THD *thd, Field *field, Table *table)
 
4249
static void update_field_dependencies(THD *thd, Field *field, TABLE *table)
3979
4250
{
 
4251
  DBUG_ENTER("update_field_dependencies");
3980
4252
  if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3981
4253
  {
3982
4254
    MY_BITMAP *current_bitmap, *other_bitmap;
4003
4275
    if (bitmap_fast_test_and_set(current_bitmap, field->field_index))
4004
4276
    {
4005
4277
      if (thd->mark_used_columns == MARK_COLUMNS_WRITE)
 
4278
      {
 
4279
        DBUG_PRINT("warning", ("Found duplicated field"));
4006
4280
        thd->dup_field= field;
4007
 
      return;
 
4281
      }
 
4282
      else
 
4283
      {
 
4284
        DBUG_PRINT("note", ("Field found before"));
 
4285
      }
 
4286
      DBUG_VOID_RETURN;
4008
4287
    }
4009
4288
    if (table->get_fields_in_item_tree)
4010
4289
      field->flags|= GET_FIXED_FIELDS_FLAG;
4012
4291
  }
4013
4292
  else if (table->get_fields_in_item_tree)
4014
4293
    field->flags|= GET_FIXED_FIELDS_FLAG;
4015
 
  return;
 
4294
  DBUG_VOID_RETURN;
 
4295
}
 
4296
 
 
4297
 
 
4298
/*
 
4299
  Find a field by name in a view that uses merge algorithm.
 
4300
 
 
4301
  SYNOPSIS
 
4302
    find_field_in_view()
 
4303
    thd                         thread handler
 
4304
    table_list                  view to search for 'name'
 
4305
    name                        name of field
 
4306
    length                      length of name
 
4307
    item_name                   name of item if it will be created (VIEW)
 
4308
    ref                         expression substituted in VIEW should be passed
 
4309
                                using this reference (return view_ref_found)
 
4310
    register_tree_change        true if ref is not stack variable and we
 
4311
                                need register changes in item tree
 
4312
 
 
4313
  RETURN
 
4314
    0                   field is not found
 
4315
    view_ref_found      found value in VIEW (real result is in *ref)
 
4316
    #                   pointer to field - only for schema table fields
 
4317
*/
 
4318
 
 
4319
static Field *
 
4320
find_field_in_view(THD *thd, TABLE_LIST *table_list,
 
4321
                   const char *name, uint length,
 
4322
                   const char *item_name, Item **ref,
 
4323
                   bool register_tree_change)
 
4324
{
 
4325
  DBUG_ENTER("find_field_in_view");
 
4326
  DBUG_PRINT("enter",
 
4327
             ("view: '%s', field name: '%s', item name: '%s', ref 0x%lx",
 
4328
              table_list->alias, name, item_name, (ulong) ref));
 
4329
  Field_iterator_view field_it;
 
4330
  field_it.set(table_list);
 
4331
  
 
4332
  DBUG_ASSERT(table_list->schema_table_reformed ||
 
4333
              (ref != 0 && (0) != 0));
 
4334
  for (; !field_it.end_of_fields(); field_it.next())
 
4335
  {
 
4336
    if (!my_strcasecmp(system_charset_info, field_it.name(), name))
 
4337
    {
 
4338
      /*
 
4339
        create_item() may, or may not create a new Item, depending on
 
4340
        the column reference. See create_view_field() for details.
 
4341
      */
 
4342
      Item *item= field_it.create_item(thd);
 
4343
      
 
4344
      if (!item)
 
4345
        DBUG_RETURN(0);
 
4346
      /*
 
4347
       *ref != NULL means that *ref contains the item that we need to
 
4348
       replace. If the item was aliased by the user, set the alias to
 
4349
       the replacing item.
 
4350
       We need to set alias on both ref itself and on ref real item.
 
4351
      */
 
4352
      if (*ref && !(*ref)->is_autogenerated_name)
 
4353
      {
 
4354
        item->set_name((*ref)->name, (*ref)->name_length,
 
4355
                       system_charset_info);
 
4356
        item->real_item()->set_name((*ref)->name, (*ref)->name_length,
 
4357
                       system_charset_info);
 
4358
      }
 
4359
      if (register_tree_change)
 
4360
        thd->change_item_tree(ref, item);
 
4361
      else
 
4362
        *ref= item;
 
4363
      DBUG_RETURN((Field*) view_ref_found);
 
4364
    }
 
4365
  }
 
4366
  DBUG_RETURN(0);
4016
4367
}
4017
4368
 
4018
4369
 
4046
4397
*/
4047
4398
 
4048
4399
static Field *
4049
 
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4050
 
                           uint32_t length __attribute__((unused)),
4051
 
                           Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4052
 
                           TableList **actual_table)
 
4400
find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
 
4401
                           uint length, Item **ref, bool register_tree_change,
 
4402
                           TABLE_LIST **actual_table)
4053
4403
{
4054
4404
  List_iterator_fast<Natural_join_column>
4055
4405
    field_it(*(table_ref->join_columns));
4056
4406
  Natural_join_column *nj_col, *curr_nj_col;
4057
4407
  Field *found_field;
4058
 
 
4059
 
  assert(table_ref->is_natural_join && table_ref->join_columns);
4060
 
  assert(*actual_table == NULL);
 
4408
  DBUG_ENTER("find_field_in_natural_join");
 
4409
  DBUG_PRINT("enter", ("field name: '%s', ref 0x%lx",
 
4410
                       name, (ulong) ref));
 
4411
  DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns);
 
4412
  DBUG_ASSERT(*actual_table == NULL);
4061
4413
 
4062
4414
  for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; 
4063
4415
       curr_nj_col= field_it++)
4067
4419
      if (nj_col)
4068
4420
      {
4069
4421
        my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
4070
 
        return(NULL);
 
4422
        DBUG_RETURN(NULL);
4071
4423
      }
4072
4424
      nj_col= curr_nj_col;
4073
4425
    }
4074
4426
  }
4075
4427
  if (!nj_col)
4076
 
    return(NULL);
 
4428
    DBUG_RETURN(NULL);
 
4429
 
 
4430
  if (nj_col->view_field)
 
4431
  {
 
4432
    Item *item;
 
4433
    /*
 
4434
      create_item() may, or may not create a new Item, depending on the
 
4435
      column reference. See create_view_field() for details.
 
4436
    */
 
4437
    item= nj_col->create_item(thd);
 
4438
    /*
 
4439
     *ref != NULL means that *ref contains the item that we need to
 
4440
     replace. If the item was aliased by the user, set the alias to
 
4441
     the replacing item.
 
4442
     We need to set alias on both ref itself and on ref real item.
 
4443
     */
 
4444
    if (*ref && !(*ref)->is_autogenerated_name)
 
4445
    {
 
4446
      item->set_name((*ref)->name, (*ref)->name_length,
 
4447
                     system_charset_info);
 
4448
      item->real_item()->set_name((*ref)->name, (*ref)->name_length,
 
4449
                                  system_charset_info);
 
4450
    }
 
4451
 
 
4452
    if (!item)
 
4453
      DBUG_RETURN(NULL);
 
4454
    DBUG_ASSERT(nj_col->table_field == NULL);
 
4455
    if (nj_col->table_ref->schema_table_reformed)
 
4456
    {
 
4457
      /*
 
4458
        Translation table items are always Item_fields and fixed
 
4459
        already('mysql_schema_table' function). So we can return
 
4460
        ->field. It is used only for 'show & where' commands.
 
4461
      */
 
4462
      DBUG_RETURN(((Item_field*) (nj_col->view_field->item))->field);
 
4463
    }
 
4464
    if (register_tree_change)
 
4465
      thd->change_item_tree(ref, item);
 
4466
    else
 
4467
      *ref= item;
 
4468
    found_field= (Field*) view_ref_found;
 
4469
  }
 
4470
  else
4077
4471
  {
4078
4472
    /* This is a base table. */
4079
 
    assert(nj_col->table_ref->table == nj_col->table_field->table);
 
4473
    DBUG_ASSERT(nj_col->view_field == NULL);
 
4474
    DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->table);
4080
4475
    found_field= nj_col->table_field;
4081
4476
    update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4082
4477
  }
4083
4478
 
4084
4479
  *actual_table= nj_col->table_ref;
4085
4480
  
4086
 
  return(found_field);
 
4481
  DBUG_RETURN(found_field);
4087
4482
}
4088
4483
 
4089
4484
 
4106
4501
*/
4107
4502
 
4108
4503
Field *
4109
 
find_field_in_table(THD *thd, Table *table, const char *name, uint32_t length,
4110
 
                    bool allow_rowid, uint32_t *cached_field_index_ptr)
 
4504
find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
 
4505
                    bool allow_rowid, uint *cached_field_index_ptr)
4111
4506
{
4112
4507
  Field **field_ptr, *field;
4113
 
  uint32_t cached_field_index= *cached_field_index_ptr;
 
4508
  uint cached_field_index= *cached_field_index_ptr;
 
4509
  DBUG_ENTER("find_field_in_table");
 
4510
  DBUG_PRINT("enter", ("table: '%s', field name: '%s'", table->alias, name));
4114
4511
 
4115
4512
  /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4116
4513
  if (cached_field_index < table->s->fields &&
4119
4516
    field_ptr= table->field + cached_field_index;
4120
4517
  else if (table->s->name_hash.records)
4121
4518
  {
4122
 
    field_ptr= (Field**) hash_search(&table->s->name_hash, (unsigned char*) name,
 
4519
    field_ptr= (Field**) hash_search(&table->s->name_hash, (uchar*) name,
4123
4520
                                     length);
4124
4521
    if (field_ptr)
4125
4522
    {
4133
4530
  else
4134
4531
  {
4135
4532
    if (!(field_ptr= table->field))
4136
 
      return((Field *)0);
 
4533
      DBUG_RETURN((Field *)0);
4137
4534
    for (; *field_ptr; ++field_ptr)
4138
4535
      if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
4139
4536
        break;
4141
4538
 
4142
4539
  if (field_ptr && *field_ptr)
4143
4540
  {
4144
 
    if ((*field_ptr)->vcol_info)
4145
 
    {
4146
 
      if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4147
 
      {
4148
 
        Item *vcol_item= (*field_ptr)->vcol_info->expr_item;
4149
 
        assert(vcol_item);
4150
 
        vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4151
 
        /* 
4152
 
          Set the virtual field for write here if 
4153
 
          1) this procedure is called for a read-only operation (SELECT), and
4154
 
          2) the virtual column is not phycically stored in the table
4155
 
        */
4156
 
        if ((thd->mark_used_columns != MARK_COLUMNS_WRITE) && 
4157
 
            (not (*field_ptr)->is_stored))
4158
 
          bitmap_set_bit((*field_ptr)->table->write_set, (*field_ptr)->field_index);
4159
 
      }
4160
 
    }
4161
4541
    *cached_field_index_ptr= field_ptr - table->field;
4162
4542
    field= *field_ptr;
4163
4543
  }
4166
4546
    if (!allow_rowid ||
4167
4547
        my_strcasecmp(system_charset_info, name, "_rowid") ||
4168
4548
        table->s->rowid_field_offset == 0)
4169
 
      return((Field*) 0);
 
4549
      DBUG_RETURN((Field*) 0);
4170
4550
    field= table->field[table->s->rowid_field_offset-1];
4171
4551
  }
4172
4552
 
4173
4553
  update_field_dependencies(thd, field, table);
4174
4554
 
4175
 
  return(field);
 
4555
  DBUG_RETURN(field);
4176
4556
}
4177
4557
 
4178
4558
 
4219
4599
*/
4220
4600
 
4221
4601
Field *
4222
 
find_field_in_table_ref(THD *thd, TableList *table_list,
4223
 
                        const char *name, uint32_t length,
 
4602
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
 
4603
                        const char *name, uint length,
4224
4604
                        const char *item_name, const char *db_name,
4225
4605
                        const char *table_name, Item **ref,
4226
4606
                        bool check_privileges, bool allow_rowid,
4227
 
                        uint32_t *cached_field_index_ptr,
4228
 
                        bool register_tree_change, TableList **actual_table)
 
4607
                        uint *cached_field_index_ptr,
 
4608
                        bool register_tree_change, TABLE_LIST **actual_table)
4229
4609
{
4230
 
  Field *fld= NULL;
4231
 
 
4232
 
  assert(table_list->alias);
4233
 
  assert(name);
4234
 
  assert(item_name);
 
4610
  Field *fld;
 
4611
  DBUG_ENTER("find_field_in_table_ref");
 
4612
  DBUG_ASSERT(table_list->alias);
 
4613
  DBUG_ASSERT(name);
 
4614
  DBUG_ASSERT(item_name);
 
4615
  DBUG_PRINT("enter",
 
4616
             ("table: '%s'  field name: '%s'  item name: '%s'  ref 0x%lx",
 
4617
              table_list->alias, name, item_name, (ulong) ref));
4235
4618
 
4236
4619
  /*
4237
4620
    Check that the table and database that qualify the current field name
4262
4645
      (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
4263
4646
       (db_name && db_name[0] && table_list->db && table_list->db[0] &&
4264
4647
        strcmp(db_name, table_list->db))))
4265
 
    return(0);
 
4648
    DBUG_RETURN(0);
4266
4649
 
4267
4650
  *actual_table= NULL;
4268
4651
 
4269
4652
  if (table_list->field_translation)
4270
4653
  {
 
4654
    /* 'table_list' is a view or an information schema table. */
 
4655
    if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
 
4656
                                 register_tree_change)))
 
4657
      *actual_table= table_list;
4271
4658
  }
4272
4659
  else if (!table_list->nested_join)
4273
4660
  {
4274
4661
    /* 'table_list' is a stored table. */
4275
 
    assert(table_list->table);
 
4662
    DBUG_ASSERT(table_list->table);
4276
4663
    if ((fld= find_field_in_table(thd, table_list->table, name, length,
4277
4664
                                  allow_rowid,
4278
4665
                                  cached_field_index_ptr)))
4289
4676
    */
4290
4677
    if (table_name && table_name[0])
4291
4678
    {
4292
 
      List_iterator<TableList> it(table_list->nested_join->join_list);
4293
 
      TableList *table;
 
4679
      List_iterator<TABLE_LIST> it(table_list->nested_join->join_list);
 
4680
      TABLE_LIST *table;
4294
4681
      while ((table= it++))
4295
4682
      {
4296
4683
        if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
4298
4685
                                          check_privileges, allow_rowid,
4299
4686
                                          cached_field_index_ptr,
4300
4687
                                          register_tree_change, actual_table)))
4301
 
          return(fld);
 
4688
          DBUG_RETURN(fld);
4302
4689
      }
4303
 
      return(0);
 
4690
      DBUG_RETURN(0);
4304
4691
    }
4305
4692
    /*
4306
4693
      Non-qualified field, search directly in the result columns of the
4330
4717
          else
4331
4718
          {
4332
4719
            if (thd->mark_used_columns == MARK_COLUMNS_READ)
4333
 
              it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
 
4720
              it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4334
4721
          }
4335
4722
        }
4336
4723
        else
4337
4724
          field_to_set= fld;
4338
4725
        if (field_to_set)
4339
4726
        {
4340
 
          Table *table= field_to_set->table;
 
4727
          TABLE *table= field_to_set->table;
4341
4728
          if (thd->mark_used_columns == MARK_COLUMNS_READ)
4342
4729
            bitmap_set_bit(table->read_set, field_to_set->field_index);
4343
4730
          else
4345
4732
        }
4346
4733
      }
4347
4734
  }
4348
 
  return(fld);
 
4735
  DBUG_RETURN(fld);
4349
4736
}
4350
4737
 
4351
4738
 
4364
4751
    #                   pointer to field
4365
4752
*/
4366
4753
 
4367
 
Field *find_field_in_table_sef(Table *table, const char *name)
 
4754
Field *find_field_in_table_sef(TABLE *table, const char *name)
4368
4755
{
4369
4756
  Field **field_ptr;
4370
4757
  if (table->s->name_hash.records)
4371
4758
  {
4372
 
    field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
 
4759
    field_ptr= (Field**)hash_search(&table->s->name_hash,(uchar*) name,
4373
4760
                                    strlen(name));
4374
4761
    if (field_ptr)
4375
4762
    {
4431
4818
 
4432
4819
Field *
4433
4820
find_field_in_tables(THD *thd, Item_ident *item,
4434
 
                     TableList *first_table, TableList *last_table,
 
4821
                     TABLE_LIST *first_table, TABLE_LIST *last_table,
4435
4822
                     Item **ref, find_item_error_report_type report_error,
4436
4823
                     bool check_privileges, bool register_tree_change)
4437
4824
{
4439
4826
  const char *db= item->db_name;
4440
4827
  const char *table_name= item->table_name;
4441
4828
  const char *name= item->field_name;
4442
 
  uint32_t length=(uint) strlen(name);
 
4829
  uint length=(uint) strlen(name);
4443
4830
  char name_buff[NAME_LEN+1];
4444
 
  TableList *cur_table= first_table;
4445
 
  TableList *actual_table;
 
4831
  TABLE_LIST *cur_table= first_table;
 
4832
  TABLE_LIST *actual_table;
4446
4833
  bool allow_rowid;
4447
4834
 
4448
4835
  if (!table_name || !table_name[0])
4457
4844
  {
4458
4845
    /*
4459
4846
      This shortcut is used by prepared statements. We assume that
4460
 
      TableList *first_table is not changed during query execution (which
 
4847
      TABLE_LIST *first_table is not changed during query execution (which
4461
4848
      is true for all queries except RENAME but luckily RENAME doesn't
4462
4849
      use fields...) so we can rely on reusing pointer to its member.
4463
4850
      With this optimization we also miss case when addition of one more
4464
4851
      field makes some prepared query ambiguous and so erroneous, but we
4465
4852
      accept this trade off.
4466
4853
    */
4467
 
    TableList *table_ref= item->cached_table;
 
4854
    TABLE_LIST *table_ref= item->cached_table;
4468
4855
    /*
4469
4856
      The condition (table_ref->view == NULL) ensures that we will call
4470
4857
      find_field_in_table even in the case of information schema tables
4488
4875
        Only views fields should be marked as dependent, not an underlying
4489
4876
        fields.
4490
4877
      */
 
4878
      if (!table_ref->belong_to_view)
4491
4879
      {
4492
4880
        SELECT_LEX *current_sel= thd->lex->current_select;
4493
4881
        SELECT_LEX *last_select= table_ref->select_lex;
4562
4950
      item->cached_table= (!actual_table->cacheable_table || found) ?
4563
4951
                          0 : actual_table;
4564
4952
 
4565
 
      assert(thd->where);
 
4953
      DBUG_ASSERT(thd->where);
4566
4954
      /*
4567
4955
        If we found a fully qualified field we return it directly as it can't
4568
4956
        have duplicates.
4599
4987
    char buff[NAME_LEN*2+1];
4600
4988
    if (db && db[0])
4601
4989
    {
4602
 
      strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
 
4990
      strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
4603
4991
      table_name=buff;
4604
4992
    }
4605
4993
    my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4654
5042
 
4655
5043
 
4656
5044
Item **
4657
 
find_item_in_list(Item *find, List<Item> &items, uint32_t *counter,
 
5045
find_item_in_list(Item *find, List<Item> &items, uint *counter,
4658
5046
                  find_item_error_report_type report_error,
4659
5047
                  enum_resolution_type *resolution)
4660
5048
{
4669
5057
    (and not an item that happens to have a name).
4670
5058
  */
4671
5059
  bool is_ref_by_name= 0;
4672
 
  uint32_t unaliased_counter= 0;
 
5060
  uint unaliased_counter= 0;
4673
5061
 
4674
5062
  *resolution= NOT_RESOLVED;
4675
5063
 
4682
5070
    db_name=    ((Item_ident*) find)->db_name;
4683
5071
  }
4684
5072
 
4685
 
  for (uint32_t i= 0; (item=li++); i++)
 
5073
  for (uint i= 0; (item=li++); i++)
4686
5074
  {
4687
5075
    if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
4688
5076
    {
4924
5312
*/
4925
5313
 
4926
5314
static bool
4927
 
set_new_item_local_context(THD *thd, Item_ident *item, TableList *table_ref)
 
5315
set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref)
4928
5316
{
4929
5317
  Name_resolution_context *context;
4930
5318
  if (!(context= new (thd->mem_root) Name_resolution_context))
4969
5357
*/
4970
5358
 
4971
5359
static bool
4972
 
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4973
 
                    List<String> *using_fields, uint32_t *found_using_fields)
 
5360
mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
 
5361
                    List<String> *using_fields, uint *found_using_fields)
4974
5362
{
4975
5363
  Field_iterator_table_ref it_1, it_2;
4976
5364
  Natural_join_column *nj_col_1, *nj_col_2;
4980
5368
    Leaf table references to which new natural join columns are added
4981
5369
    if the leaves are != NULL.
4982
5370
  */
4983
 
  TableList *leaf_1= (table_ref_1->nested_join &&
 
5371
  TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
4984
5372
                       !table_ref_1->is_natural_join) ?
4985
5373
                      NULL : table_ref_1;
4986
 
  TableList *leaf_2= (table_ref_2->nested_join &&
 
5374
  TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
4987
5375
                       !table_ref_2->is_natural_join) ?
4988
5376
                      NULL : table_ref_2;
4989
5377
 
 
5378
  DBUG_ENTER("mark_common_columns");
 
5379
  DBUG_PRINT("info", ("operand_1: %s  operand_2: %s",
 
5380
                      table_ref_1->alias, table_ref_2->alias));
 
5381
 
4990
5382
  *found_using_fields= 0;
4991
5383
 
4992
5384
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
5000
5392
    field_name_1= nj_col_1->name();
5001
5393
    is_using_column_1= using_fields && 
5002
5394
      test_if_string_in_list(field_name_1, using_fields);
 
5395
    DBUG_PRINT ("info", ("field_name_1=%s.%s", 
 
5396
                         nj_col_1->table_name() ? nj_col_1->table_name() : "", 
 
5397
                         field_name_1));
5003
5398
 
5004
5399
    /*
5005
5400
      Find a field with the same name in table_ref_2.
5016
5411
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
5017
5412
        goto err;
5018
5413
      cur_field_name_2= cur_nj_col_2->name();
 
5414
      DBUG_PRINT ("info", ("cur_field_name_2=%s.%s", 
 
5415
                           cur_nj_col_2->table_name() ? 
 
5416
                             cur_nj_col_2->table_name() : "", 
 
5417
                           cur_field_name_2));
5019
5418
 
5020
5419
      /*
5021
5420
        Compare the two columns and check for duplicate common fields.
5030
5429
      */
5031
5430
      if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2))
5032
5431
      {
 
5432
        DBUG_PRINT ("info", ("match c1.is_common=%d", nj_col_1->is_common));
5033
5433
        if (cur_nj_col_2->is_common ||
5034
5434
            (found && (!using_fields || is_using_column_1)))
5035
5435
        {
5070
5470
        goto err;                               // out of memory
5071
5471
 
5072
5472
      /*
 
5473
        The following assert checks that the two created items are of
 
5474
        type Item_ident.
 
5475
      */
 
5476
      DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item);
 
5477
      /*
5073
5478
        In the case of no_wrap_view_item == 0, the created items must be
5074
5479
        of sub-classes of Item_ident.
5075
5480
      */
5076
 
      assert(item_1->type() == Item::FIELD_ITEM ||
 
5481
      DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM ||
5077
5482
                  item_1->type() == Item::REF_ITEM);
5078
 
      assert(item_2->type() == Item::FIELD_ITEM ||
 
5483
      DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM ||
5079
5484
                  item_2->type() == Item::REF_ITEM);
5080
5485
 
5081
5486
      /*
5107
5512
                  eq_cond);
5108
5513
 
5109
5514
      nj_col_1->is_common= nj_col_2->is_common= true;
 
5515
      DBUG_PRINT ("info", ("%s.%s and %s.%s are common", 
 
5516
                           nj_col_1->table_name() ? 
 
5517
                             nj_col_1->table_name() : "", 
 
5518
                           nj_col_1->name(),
 
5519
                           nj_col_2->table_name() ? 
 
5520
                             nj_col_2->table_name() : "", 
 
5521
                           nj_col_2->name()));
5110
5522
 
5111
5523
      if (field_1)
5112
5524
      {
5113
 
        Table *table_1= nj_col_1->table_ref->table;
 
5525
        TABLE *table_1= nj_col_1->table_ref->table;
5114
5526
        /* Mark field_1 used for table cache. */
5115
5527
        bitmap_set_bit(table_1->read_set, field_1->field_index);
5116
5528
        table_1->covering_keys.intersect(field_1->part_of_key);
5118
5530
      }
5119
5531
      if (field_2)
5120
5532
      {
5121
 
        Table *table_2= nj_col_2->table_ref->table;
 
5533
        TABLE *table_2= nj_col_2->table_ref->table;
5122
5534
        /* Mark field_2 used for table cache. */
5123
5535
        bitmap_set_bit(table_2->read_set, field_2->field_index);
5124
5536
        table_2->covering_keys.intersect(field_2->part_of_key);
5142
5554
  result= false;
5143
5555
 
5144
5556
err:
5145
 
  return(result);
 
5557
  DBUG_RETURN(result);
5146
5558
}
5147
5559
 
5148
5560
 
5183
5595
*/
5184
5596
 
5185
5597
static bool
5186
 
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5187
 
                                 TableList *natural_using_join,
5188
 
                                 TableList *table_ref_1,
5189
 
                                 TableList *table_ref_2,
 
5598
store_natural_using_join_columns(THD *thd, TABLE_LIST *natural_using_join,
 
5599
                                 TABLE_LIST *table_ref_1,
 
5600
                                 TABLE_LIST *table_ref_2,
5190
5601
                                 List<String> *using_fields,
5191
 
                                 uint32_t found_using_fields)
 
5602
                                 uint found_using_fields)
5192
5603
{
5193
5604
  Field_iterator_table_ref it_1, it_2;
5194
5605
  Natural_join_column *nj_col_1, *nj_col_2;
5195
5606
  bool result= true;
5196
5607
  List<Natural_join_column> *non_join_columns;
 
5608
  DBUG_ENTER("store_natural_using_join_columns");
5197
5609
 
5198
 
  assert(!natural_using_join->join_columns);
 
5610
  DBUG_ASSERT(!natural_using_join->join_columns);
5199
5611
 
5200
5612
  if (!(non_join_columns= new List<Natural_join_column>) ||
5201
5613
      !(natural_using_join->join_columns= new List<Natural_join_column>))
5267
5679
  result= false;
5268
5680
 
5269
5681
err:
5270
 
  return(result);
 
5682
  DBUG_RETURN(result);
5271
5683
}
5272
5684
 
5273
5685
 
5286
5698
  DESCRIPTION
5287
5699
    The procedure performs a post-order traversal of a nested join tree
5288
5700
    and materializes the row types of NATURAL/USING joins in a
5289
 
    bottom-up manner until it reaches the TableList elements that
 
5701
    bottom-up manner until it reaches the TABLE_LIST elements that
5290
5702
    represent the top-most NATURAL/USING joins. The procedure should be
5291
5703
    applied to each element of SELECT_LEX::top_join_list (i.e. to each
5292
5704
    top-level element of the FROM clause).
5302
5714
*/
5303
5715
 
5304
5716
static bool
5305
 
store_top_level_join_columns(THD *thd, TableList *table_ref,
5306
 
                             TableList *left_neighbor,
5307
 
                             TableList *right_neighbor)
 
5717
store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref,
 
5718
                             TABLE_LIST *left_neighbor,
 
5719
                             TABLE_LIST *right_neighbor)
5308
5720
{
5309
5721
  bool result= true;
5310
5722
 
 
5723
  DBUG_ENTER("store_top_level_join_columns");
 
5724
 
5311
5725
  /* Call the procedure recursively for each nested table reference. */
5312
5726
  if (table_ref->nested_join)
5313
5727
  {
5314
 
    List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
5315
 
    TableList *same_level_left_neighbor= nested_it++;
5316
 
    TableList *same_level_right_neighbor= NULL;
 
5728
    List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list);
 
5729
    TABLE_LIST *same_level_left_neighbor= nested_it++;
 
5730
    TABLE_LIST *same_level_right_neighbor= NULL;
5317
5731
    /* Left/right-most neighbors, possibly at higher levels in the join tree. */
5318
 
    TableList *real_left_neighbor, *real_right_neighbor;
 
5732
    TABLE_LIST *real_left_neighbor, *real_right_neighbor;
5319
5733
 
5320
5734
    while (same_level_left_neighbor)
5321
5735
    {
5322
 
      TableList *cur_table_ref= same_level_left_neighbor;
 
5736
      TABLE_LIST *cur_table_ref= same_level_left_neighbor;
5323
5737
      same_level_left_neighbor= nested_it++;
5324
5738
      /*
5325
5739
        The order of RIGHT JOIN operands is reversed in 'join list' to
5336
5750
          cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
5337
5751
      {
5338
5752
        /* This can happen only for JOIN ... ON. */
5339
 
        assert(table_ref->nested_join->join_list.elements == 2);
5340
 
        std::swap(same_level_left_neighbor, cur_table_ref);
 
5753
        DBUG_ASSERT(table_ref->nested_join->join_list.elements == 2);
 
5754
        swap_variables(TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
5341
5755
      }
5342
5756
 
5343
5757
      /*
5363
5777
  */
5364
5778
  if (table_ref->is_natural_join)
5365
5779
  {
5366
 
    assert(table_ref->nested_join &&
 
5780
    DBUG_ASSERT(table_ref->nested_join &&
5367
5781
                table_ref->nested_join->join_list.elements == 2);
5368
 
    List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
 
5782
    List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
5369
5783
    /*
5370
5784
      Notice that the order of join operands depends on whether table_ref
5371
5785
      represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
5372
5786
      in inverted order.
5373
5787
     */
5374
 
    TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5375
 
    TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
 
5788
    TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
 
5789
    TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */
5376
5790
    List<String> *using_fields= table_ref->join_using_fields;
5377
 
    uint32_t found_using_fields;
 
5791
    uint found_using_fields;
5378
5792
 
5379
5793
    /*
5380
5794
      The two join operands were interchanged in the parser, change the order
5381
5795
      back for 'mark_common_columns'.
5382
5796
    */
5383
5797
    if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5384
 
      std::swap(table_ref_1, table_ref_2);
 
5798
      swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5385
5799
    if (mark_common_columns(thd, table_ref_1, table_ref_2,
5386
5800
                            using_fields, &found_using_fields))
5387
5801
      goto err;
5392
5806
      same as of an equivalent LEFT JOIN.
5393
5807
    */
5394
5808
    if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5395
 
      std::swap(table_ref_1, table_ref_2);
 
5809
      swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5396
5810
    if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5397
5811
                                         table_ref_2, using_fields,
5398
5812
                                         found_using_fields))
5409
5823
    /* Add a true condition to outer joins that have no common columns. */
5410
5824
    if (table_ref_2->outer_join &&
5411
5825
        !table_ref_1->on_expr && !table_ref_2->on_expr)
5412
 
      table_ref_2->on_expr= new Item_int((int64_t) 1,1);   /* Always true. */
 
5826
      table_ref_2->on_expr= new Item_int((longlong) 1,1);   /* Always true. */
5413
5827
 
5414
5828
    /* Change this table reference to become a leaf for name resolution. */
5415
5829
    if (left_neighbor)
5416
5830
    {
5417
 
      TableList *last_leaf_on_the_left;
 
5831
      TABLE_LIST *last_leaf_on_the_left;
5418
5832
      last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
5419
5833
      last_leaf_on_the_left->next_name_resolution_table= table_ref;
5420
5834
    }
5421
5835
    if (right_neighbor)
5422
5836
    {
5423
 
      TableList *first_leaf_on_the_right;
 
5837
      TABLE_LIST *first_leaf_on_the_right;
5424
5838
      first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
5425
5839
      table_ref->next_name_resolution_table= first_leaf_on_the_right;
5426
5840
    }
5430
5844
  result= false; /* All is OK. */
5431
5845
 
5432
5846
err:
5433
 
  return(result);
 
5847
  DBUG_RETURN(result);
5434
5848
}
5435
5849
 
5436
5850
 
5459
5873
    false  OK
5460
5874
*/
5461
5875
static bool setup_natural_join_row_types(THD *thd,
5462
 
                                         List<TableList> *from_clause,
 
5876
                                         List<TABLE_LIST> *from_clause,
5463
5877
                                         Name_resolution_context *context)
5464
5878
{
5465
5879
  thd->where= "from clause";
5466
5880
  if (from_clause->elements == 0)
5467
5881
    return false; /* We come here in the case of UNIONs. */
5468
5882
 
5469
 
  List_iterator_fast<TableList> table_ref_it(*from_clause);
5470
 
  TableList *table_ref; /* Current table reference. */
 
5883
  List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause);
 
5884
  TABLE_LIST *table_ref; /* Current table reference. */
5471
5885
  /* Table reference to the left of the current. */
5472
 
  TableList *left_neighbor;
 
5886
  TABLE_LIST *left_neighbor;
5473
5887
  /* Table reference to the right of the current. */
5474
 
  TableList *right_neighbor= NULL;
 
5888
  TABLE_LIST *right_neighbor= NULL;
5475
5889
 
5476
5890
  /* Note that tables in the list are in reversed order */
5477
5891
  for (left_neighbor= table_ref_it++; left_neighbor ; )
5478
5892
  {
5479
5893
    table_ref= left_neighbor;
5480
5894
    left_neighbor= table_ref_it++;
5481
 
    if (store_top_level_join_columns(thd, table_ref,
5482
 
                                     left_neighbor, right_neighbor))
5483
 
      return true;
5484
 
    if (left_neighbor)
 
5895
    /* For stored procedures do not redo work if already done. */
 
5896
    if (context->select_lex->first_execution)
5485
5897
    {
5486
 
      TableList *first_leaf_on_the_right;
5487
 
      first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5488
 
      left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
 
5898
      if (store_top_level_join_columns(thd, table_ref,
 
5899
                                       left_neighbor, right_neighbor))
 
5900
        return true;
 
5901
      if (left_neighbor)
 
5902
      {
 
5903
        TABLE_LIST *first_leaf_on_the_right;
 
5904
        first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
 
5905
        left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
 
5906
      }
5489
5907
    }
5490
5908
    right_neighbor= table_ref;
5491
5909
  }
5496
5914
    right_neighbor points to the left-most top-level table reference in the
5497
5915
    FROM clause.
5498
5916
  */
5499
 
  assert(right_neighbor);
 
5917
  DBUG_ASSERT(right_neighbor);
5500
5918
  context->first_name_resolution_table=
5501
5919
    right_neighbor->first_leaf_for_name_resolution();
5502
5920
 
5508
5926
** Expand all '*' in given fields
5509
5927
****************************************************************************/
5510
5928
 
5511
 
int setup_wild(THD *thd,
5512
 
               TableList *tables __attribute__((unused)),
5513
 
               List<Item> &fields,
5514
 
               List<Item> *sum_func_list,
5515
 
               uint32_t wild_num)
 
5929
int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
 
5930
               List<Item> *sum_func_list,
 
5931
               uint wild_num)
5516
5932
{
5517
5933
  if (!wild_num)
5518
5934
    return(0);
5519
5935
 
5520
5936
  Item *item;
5521
5937
  List_iterator<Item> it(fields);
 
5938
  DBUG_ENTER("setup_wild");
5522
5939
 
5523
5940
  thd->lex->current_select->cur_pos_in_select_list= 0;
5524
5941
  while (wild_num && (item= it++))
5528
5945
        ((Item_field*) item)->field_name[0] == '*' &&
5529
5946
        !((Item_field*) item)->field)
5530
5947
    {
5531
 
      uint32_t elem= fields.elements;
 
5948
      uint elem= fields.elements;
5532
5949
      bool any_privileges= ((Item_field *) item)->any_privileges;
5533
5950
      Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
5534
5951
      if (subsel &&
5539
5956
 
5540
5957
          Item_int do not need fix_fields() because it is basic constant.
5541
5958
        */
5542
 
        it.replace(new Item_int("Not_used", (int64_t) 1,
 
5959
        it.replace(new Item_int("Not_used", (longlong) 1,
5543
5960
                                MY_INT64_NUM_DECIMAL_DIGITS));
5544
5961
      }
5545
5962
      else if (insert_fields(thd, ((Item_field*) item)->context,
5547
5964
                             ((Item_field*) item)->table_name, &it,
5548
5965
                             any_privileges))
5549
5966
      {
5550
 
        return(-1);
 
5967
        DBUG_RETURN(-1);
5551
5968
      }
5552
5969
      if (sum_func_list)
5553
5970
      {
5564
5981
      thd->lex->current_select->cur_pos_in_select_list++;
5565
5982
  }
5566
5983
  thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5567
 
  return(0);
 
5984
  DBUG_RETURN(0);
5568
5985
}
5569
5986
 
5570
5987
/****************************************************************************
5580
5997
  nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
5581
5998
  List_iterator<Item> it(fields);
5582
5999
  bool save_is_item_list_lookup;
 
6000
  DBUG_ENTER("setup_fields");
5583
6001
 
5584
6002
  thd->mark_used_columns= mark_used_columns;
 
6003
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
5585
6004
  if (allow_sum_func)
5586
6005
    thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5587
6006
  thd->where= THD::DEFAULT_WHERE;
5600
6019
    ref_pointer_array
5601
6020
  */
5602
6021
  if (ref_pointer_array)
5603
 
    memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
 
6022
    bzero(ref_pointer_array, sizeof(Item *) * fields.elements);
5604
6023
 
5605
6024
  Item **ref= ref_pointer_array;
5606
6025
  thd->lex->current_select->cur_pos_in_select_list= 0;
5611
6030
      thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5612
6031
      thd->lex->allow_sum_func= save_allow_sum_func;
5613
6032
      thd->mark_used_columns= save_mark_used_columns;
5614
 
      return(true); /* purecov: inspected */
 
6033
      DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
 
6034
      DBUG_RETURN(true); /* purecov: inspected */
5615
6035
    }
5616
6036
    if (ref)
5617
6037
      *(ref++)= item;
5626
6046
 
5627
6047
  thd->lex->allow_sum_func= save_allow_sum_func;
5628
6048
  thd->mark_used_columns= save_mark_used_columns;
5629
 
  return(test(thd->is_error()));
 
6049
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
 
6050
  DBUG_RETURN(test(thd->is_error()));
5630
6051
}
5631
6052
 
5632
6053
 
5641
6062
  RETURN pointer on pointer to next_leaf of last element
5642
6063
*/
5643
6064
 
5644
 
TableList **make_leaves_list(TableList **list, TableList *tables)
 
6065
TABLE_LIST **make_leaves_list(TABLE_LIST **list, TABLE_LIST *tables)
5645
6066
{
5646
 
  for (TableList *table= tables; table; table= table->next_local)
 
6067
  for (TABLE_LIST *table= tables; table; table= table->next_local)
5647
6068
  {
5648
6069
    {
5649
6070
      *list= table;
5682
6103
*/
5683
6104
 
5684
6105
bool setup_tables(THD *thd, Name_resolution_context *context,
5685
 
                  List<TableList> *from_clause, TableList *tables,
5686
 
                  TableList **leaves, bool select_insert)
 
6106
                  List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
 
6107
                  TABLE_LIST **leaves, bool select_insert)
5687
6108
{
5688
 
  uint32_t tablenr= 0;
 
6109
  uint tablenr= 0;
 
6110
  DBUG_ENTER("setup_tables");
5689
6111
 
5690
 
  assert ((select_insert && !tables->next_name_resolution_table) || !tables || 
 
6112
  DBUG_ASSERT ((select_insert && !tables->next_name_resolution_table) || !tables || 
5691
6113
               (context->table_list && context->first_name_resolution_table));
5692
6114
  /*
5693
6115
    this is used for INSERT ... SELECT.
5694
6116
    For select we setup tables except first (and its underlying tables)
5695
6117
  */
5696
 
  TableList *first_select_table= (select_insert ?
 
6118
  TABLE_LIST *first_select_table= (select_insert ?
5697
6119
                                   tables->next_local:
5698
6120
                                   0);
5699
6121
  if (!(*leaves))
5700
6122
    make_leaves_list(leaves, tables);
5701
6123
 
5702
 
  TableList *table_list;
 
6124
  TABLE_LIST *table_list;
5703
6125
  for (table_list= *leaves;
5704
6126
       table_list;
5705
6127
       table_list= table_list->next_leaf, tablenr++)
5706
6128
  {
5707
 
    Table *table= table_list->table;
 
6129
    TABLE *table= table_list->table;
5708
6130
    table->pos_in_table_list= table_list;
5709
6131
    if (first_select_table &&
5710
6132
        table_list->top_table() == first_select_table)
5715
6137
    }
5716
6138
    setup_table_map(table, table_list, tablenr);
5717
6139
    if (table_list->process_index_hints(table))
5718
 
      return(1);
 
6140
      DBUG_RETURN(1);
5719
6141
  }
5720
6142
  if (tablenr > MAX_TABLES)
5721
6143
  {
5722
6144
    my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
5723
 
    return(1);
 
6145
    DBUG_RETURN(1);
5724
6146
  }
5725
6147
 
5726
6148
  /* Precompute and store the row types of NATURAL/USING joins. */
5727
6149
  if (setup_natural_join_row_types(thd, from_clause, context))
5728
 
    return(1);
 
6150
    DBUG_RETURN(1);
5729
6151
 
5730
 
  return(0);
 
6152
  DBUG_RETURN(0);
5731
6153
}
5732
6154
 
5733
6155
 
5756
6178
*/
5757
6179
bool setup_tables_and_check_access(THD *thd, 
5758
6180
                                   Name_resolution_context *context,
5759
 
                                   List<TableList> *from_clause,
5760
 
                                   TableList *tables,
5761
 
                                   TableList **leaves,
 
6181
                                   List<TABLE_LIST> *from_clause,
 
6182
                                   TABLE_LIST *tables,
 
6183
                                   TABLE_LIST **leaves,
5762
6184
                                   bool select_insert)
5763
6185
{
5764
 
  TableList *leaves_tmp= NULL;
 
6186
  TABLE_LIST *leaves_tmp= NULL;
5765
6187
  bool first_table= true;
5766
6188
 
5767
6189
  if (setup_tables(thd, context, from_clause, tables,
5773
6195
 
5774
6196
  for (; leaves_tmp; leaves_tmp= leaves_tmp->next_leaf)
5775
6197
  {
 
6198
    if (leaves_tmp->belong_to_view)
 
6199
    {
 
6200
      return true;
 
6201
    }
5776
6202
    first_table= 0;
5777
6203
  }
5778
6204
  return false;
5793
6219
     1  error
5794
6220
*/
5795
6221
 
5796
 
bool get_key_map_from_key_list(key_map *map, Table *table,
 
6222
bool get_key_map_from_key_list(key_map *map, TABLE *table,
5797
6223
                               List<String> *index_list)
5798
6224
{
5799
6225
  List_iterator_fast<String> it(*index_list);
5800
6226
  String *name;
5801
 
  uint32_t pos;
 
6227
  uint pos;
5802
6228
 
5803
6229
  map->clear_all();
5804
6230
  while ((name=it++))
5839
6265
 
5840
6266
bool
5841
6267
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5842
 
              const char *table_name, List_iterator<Item> *it,
5843
 
              bool any_privileges __attribute__((unused)))
 
6268
              const char *table_name, List_iterator<Item> *it,
 
6269
              bool any_privileges)
5844
6270
{
5845
6271
  Field_iterator_table_ref field_iterator;
5846
6272
  bool found;
5847
6273
  char name_buff[NAME_LEN+1];
 
6274
  DBUG_ENTER("insert_fields");
5848
6275
 
5849
6276
  if (db_name && lower_case_table_names)
5850
6277
  {
5865
6292
    else treat natural joins as leaves and do not iterate over their underlying
5866
6293
    tables.
5867
6294
  */
5868
 
  for (TableList *tables= (table_name ? context->table_list :
 
6295
  for (TABLE_LIST *tables= (table_name ? context->table_list :
5869
6296
                            context->first_name_resolution_table);
5870
6297
       tables;
5871
6298
       tables= (table_name ? tables->next_local :
5873
6300
       )
5874
6301
  {
5875
6302
    Field *field;
5876
 
    Table *table= tables->table;
 
6303
    TABLE *table= tables->table;
5877
6304
 
5878
 
    assert(tables->is_leaf_for_name_resolution());
 
6305
    DBUG_ASSERT(tables->is_leaf_for_name_resolution());
5879
6306
 
5880
6307
    if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) || 
5881
6308
        (db_name && strcmp(tables->db,db_name)))
5901
6328
      Item *item;
5902
6329
 
5903
6330
      if (!(item= field_iterator.create_item(thd)))
5904
 
        return(true);
 
6331
        DBUG_RETURN(true);
5905
6332
 
5906
6333
      if (!found)
5907
6334
      {
5915
6342
      {
5916
6343
        /* Mark fields as used to allow storage engine to optimze access */
5917
6344
        bitmap_set_bit(field->table->read_set, field->field_index);
5918
 
        /*
5919
 
          Mark virtual fields for write and others that the virtual fields
5920
 
          depend on for read.
5921
 
        */
5922
 
        if (field->vcol_info)
5923
 
        {
5924
 
          Item *vcol_item= field->vcol_info->expr_item;
5925
 
          assert(vcol_item);
5926
 
          vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
5927
 
          bitmap_set_bit(field->table->write_set, field->field_index);
5928
 
        }
5929
6345
        if (table)
5930
6346
        {
5931
6347
          table->covering_keys.intersect(field->part_of_key);
5933
6349
        }
5934
6350
        if (tables->is_natural_join)
5935
6351
        {
5936
 
          Table *field_table;
 
6352
          TABLE *field_table;
5937
6353
          /*
5938
6354
            In this case we are sure that the column ref will not be created
5939
6355
            because it was already created and stored with the natural join.
5940
6356
          */
5941
6357
          Natural_join_column *nj_col;
5942
6358
          if (!(nj_col= field_iterator.get_natural_column_ref()))
5943
 
            return(true);
5944
 
          assert(nj_col->table_field);
 
6359
            DBUG_RETURN(true);
 
6360
          DBUG_ASSERT(nj_col->table_field);
5945
6361
          field_table= nj_col->table_ref->table;
5946
6362
          if (field_table)
5947
6363
          {
5966
6382
      table->used_fields= table->s->fields;
5967
6383
  }
5968
6384
  if (found)
5969
 
    return(false);
 
6385
    DBUG_RETURN(false);
5970
6386
 
5971
6387
  /*
5972
6388
    TODO: in the case when we skipped all columns because there was a
5978
6394
  else
5979
6395
    my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
5980
6396
 
5981
 
  return(true);
 
6397
  DBUG_RETURN(true);
5982
6398
}
5983
6399
 
5984
6400
 
6000
6416
    false if all is OK
6001
6417
*/
6002
6418
 
6003
 
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
6004
 
                TableList *leaves,
 
6419
int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves,
6005
6420
                COND **conds)
6006
6421
{
6007
6422
  SELECT_LEX *select_lex= thd->lex->current_select;
6008
 
  TableList *table= NULL;       // For HP compilers
 
6423
  TABLE_LIST *table= NULL;      // For HP compilers
6009
6424
  void *save_thd_marker= thd->thd_marker;
6010
6425
  /*
6011
6426
    it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
6017
6432
  */
6018
6433
  bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
6019
6434
  select_lex->is_item_list_lookup= 0;
 
6435
  DBUG_ENTER("setup_conds");
6020
6436
 
6021
6437
  thd->mark_used_columns= MARK_COLUMNS_READ;
 
6438
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6022
6439
  select_lex->cond_count= 0;
6023
6440
  select_lex->between_count= 0;
6024
6441
  select_lex->max_equal_elems= 0;
6039
6456
  */
6040
6457
  for (table= leaves; table; table= table->next_leaf)
6041
6458
  {
6042
 
    TableList *embedded; /* The table at the current level of nesting. */
6043
 
    TableList *embedding= table; /* The parent nested table reference. */
 
6459
    TABLE_LIST *embedded; /* The table at the current level of nesting. */
 
6460
    TABLE_LIST *embedding= table; /* The parent nested table reference. */
6044
6461
    do
6045
6462
    {
6046
6463
      embedded= embedding;
6063
6480
  thd->thd_marker= save_thd_marker;
6064
6481
 
6065
6482
  thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6066
 
  return(test(thd->is_error()));
 
6483
  DBUG_RETURN(test(thd->is_error()));
6067
6484
 
6068
6485
err_no_arena:
6069
6486
  select_lex->is_item_list_lookup= save_is_item_list_lookup;
6070
 
  return(1);
 
6487
  DBUG_RETURN(1);
6071
6488
}
6072
6489
 
6073
6490
 
6103
6520
  List_iterator_fast<Item> f(fields),v(values);
6104
6521
  Item *value, *fld;
6105
6522
  Item_field *field;
6106
 
  Table *table= 0;
6107
 
  List<Table> tbl_list;
6108
 
  bool abort_on_warning_saved= thd->abort_on_warning;
6109
 
  tbl_list.empty();
 
6523
  TABLE *table= 0;
 
6524
  DBUG_ENTER("fill_record");
6110
6525
 
6111
6526
  /*
6112
6527
    Reset the table->auto_increment_field_not_null as it is valid for
6140
6555
    table= rfield->table;
6141
6556
    if (rfield == table->next_number_field)
6142
6557
      table->auto_increment_field_not_null= true;
6143
 
    if (rfield->vcol_info && 
6144
 
        value->type() != Item::DEFAULT_VALUE_ITEM && 
6145
 
        value->type() != Item::NULL_ITEM &&
6146
 
        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6147
 
    {
6148
 
      thd->abort_on_warning= false;
6149
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6150
 
                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6151
 
                          ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6152
 
                          rfield->field_name, table->s->table_name.str);
6153
 
      thd->abort_on_warning= abort_on_warning_saved;
6154
 
    }
6155
6558
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
6156
6559
    {
6157
6560
      my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
6158
6561
      goto err;
6159
6562
    }
6160
 
    tbl_list.push_back(table);
6161
 
  }
6162
 
  /* Update virtual fields*/
6163
 
  thd->abort_on_warning= false;
6164
 
  if (tbl_list.head())
6165
 
  {
6166
 
    List_iterator_fast<Table> t(tbl_list);
6167
 
    Table *prev_table= 0;
6168
 
    while ((table= t++))
6169
 
    {
6170
 
      /*
6171
 
        Do simple optimization to prevent unnecessary re-generating 
6172
 
        values for virtual fields
6173
 
      */
6174
 
      if (table != prev_table)
6175
 
      {
6176
 
        prev_table= table;
6177
 
        if (table->vfield)
6178
 
        {
6179
 
          if (update_virtual_fields_marked_for_write(table, false))
6180
 
            goto err;
6181
 
        }
6182
 
      }
6183
 
    }
6184
 
  }
6185
 
  thd->abort_on_warning= abort_on_warning_saved;
6186
 
  return(thd->is_error());
 
6563
  }
 
6564
  DBUG_RETURN(thd->is_error());
6187
6565
err:
6188
 
  thd->abort_on_warning= abort_on_warning_saved;
6189
6566
  if (table)
6190
6567
    table->auto_increment_field_not_null= false;
6191
 
  return(true);
 
6568
  DBUG_RETURN(true);
6192
6569
}
6193
6570
 
6194
6571
 
6213
6590
*/
6214
6591
 
6215
6592
bool
6216
 
fill_record(THD *thd, Field **ptr, List<Item> &values,
6217
 
            bool ignore_errors __attribute__((unused)))
 
6593
fill_record(THD *thd, Field **ptr, List<Item> &values, bool ignore_errors)
6218
6594
{
6219
6595
  List_iterator_fast<Item> v(values);
6220
6596
  Item *value;
6221
 
  Table *table= 0;
 
6597
  TABLE *table= 0;
 
6598
  DBUG_ENTER("fill_record");
 
6599
 
6222
6600
  Field *field;
6223
 
  List<Table> tbl_list;
6224
 
  bool abort_on_warning_saved= thd->abort_on_warning;
6225
 
  
6226
 
  tbl_list.empty();
6227
6601
  /*
6228
6602
    Reset the table->auto_increment_field_not_null as it is valid for
6229
6603
    only one row.
6243
6617
    table= field->table;
6244
6618
    if (field == table->next_number_field)
6245
6619
      table->auto_increment_field_not_null= true;
6246
 
    if (field->vcol_info && 
6247
 
        value->type() != Item::DEFAULT_VALUE_ITEM && 
6248
 
        value->type() != Item::NULL_ITEM &&
6249
 
        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6250
 
    {
6251
 
      thd->abort_on_warning= false;
6252
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6253
 
                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6254
 
                          ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6255
 
                          field->field_name, table->s->table_name.str);
6256
 
      thd->abort_on_warning= abort_on_warning_saved;
6257
 
    }
6258
6620
    if (value->save_in_field(field, 0) < 0)
6259
6621
      goto err;
6260
 
    tbl_list.push_back(table);
6261
 
  }
6262
 
  /* Update virtual fields*/
6263
 
  thd->abort_on_warning= false;
6264
 
  if (tbl_list.head())
6265
 
  {
6266
 
    List_iterator_fast<Table> t(tbl_list);
6267
 
    Table *prev_table= 0;
6268
 
    while ((table= t++))
6269
 
    {
6270
 
      /*
6271
 
        Do simple optimization to prevent unnecessary re-generating 
6272
 
        values for virtual fields
6273
 
      */
6274
 
      if (table != prev_table)
6275
 
      {
6276
 
        prev_table= table;
6277
 
        if (table->vfield)
6278
 
        {
6279
 
          if (update_virtual_fields_marked_for_write(table, false))
6280
 
          {
6281
 
            goto err;
6282
 
          }
6283
 
        }
6284
 
      }
6285
 
    }
6286
 
  }
6287
 
  thd->abort_on_warning= abort_on_warning_saved;
6288
 
  return(thd->is_error());
 
6622
  }
 
6623
  DBUG_RETURN(thd->is_error());
6289
6624
 
6290
6625
err:
6291
 
  thd->abort_on_warning= abort_on_warning_saved;
6292
6626
  if (table)
6293
6627
    table->auto_increment_field_not_null= false;
6294
 
  return(true);
 
6628
  DBUG_RETURN(true);
6295
6629
}
6296
6630
 
6297
6631
 
6298
 
bool mysql_rm_tmp_tables(void)
 
6632
my_bool mysql_rm_tmp_tables(void)
6299
6633
{
6300
 
  uint32_t i, idx;
 
6634
  uint i, idx;
6301
6635
  char  filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6302
6636
  MY_DIR *dirp;
6303
6637
  FILEINFO *file;
6304
6638
  TABLE_SHARE share;
6305
6639
  THD *thd;
 
6640
  DBUG_ENTER("mysql_rm_tmp_tables");
6306
6641
 
6307
6642
  if (!(thd= new THD))
6308
 
    return(1);
 
6643
    DBUG_RETURN(1);
6309
6644
  thd->thread_stack= (char*) &thd;
6310
6645
  thd->store_globals();
6311
6646
 
6327
6662
                                   (file->name[1] == '.' &&  !file->name[2])))
6328
6663
        continue;
6329
6664
 
6330
 
      if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
 
6665
      if (!bcmp((uchar*) file->name, (uchar*) tmp_file_prefix,
 
6666
                tmp_file_prefix_length))
6331
6667
      {
6332
6668
        char *ext= fn_ext(file->name);
6333
 
        uint32_t ext_len= strlen(ext);
6334
 
        uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
6335
 
                                    "%s%c%s", tmpdir, FN_LIBCHAR,
6336
 
                                    file->name);
6337
 
        if (!memcmp(reg_ext, ext, ext_len))
 
6669
        uint ext_len= strlen(ext);
 
6670
        uint filePath_len= my_snprintf(filePath, sizeof(filePath),
 
6671
                                       "%s%c%s", tmpdir, FN_LIBCHAR,
 
6672
                                       file->name);
 
6673
        if (!bcmp((uchar*) reg_ext, (uchar*) ext, ext_len))
6338
6674
        {
6339
6675
          handler *handler_file= 0;
6340
6676
          /* We should cut file extention before deleting of table */
6355
6691
          So we hide error messages which happnes during deleting of these
6356
6692
          files(MYF(0)).
6357
6693
        */
6358
 
        my_delete(filePath, MYF(0)); 
 
6694
        VOID(my_delete(filePath, MYF(0))); 
6359
6695
      }
6360
6696
    }
6361
6697
    my_dirend(dirp);
6362
6698
  }
6363
6699
  delete thd;
6364
6700
  my_pthread_setspecific_ptr(THR_THD,  0);
6365
 
  return(0);
 
6701
  DBUG_RETURN(0);
6366
6702
}
6367
6703
 
6368
6704
 
6386
6722
 
6387
6723
void remove_db_from_cache(const char *db)
6388
6724
{
6389
 
  for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
 
6725
  for (uint idx=0 ; idx < open_cache.records ; idx++)
6390
6726
  {
6391
 
    Table *table=(Table*) hash_element(&open_cache,idx);
 
6727
    TABLE *table=(TABLE*) hash_element(&open_cache,idx);
6392
6728
    if (!strcmp(table->s->db.str, db))
6393
6729
    {
6394
6730
      table->s->version= 0L;                    /* Free when thread is ready */
6397
6733
    }
6398
6734
  }
6399
6735
  while (unused_tables && !unused_tables->s->version)
6400
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6736
    VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6401
6737
}
6402
6738
 
6403
6739
 
6413
6749
{
6414
6750
  (void) pthread_mutex_lock(&LOCK_open);
6415
6751
  while (unused_tables)
6416
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6752
    hash_delete(&open_cache,(uchar*) unused_tables);
6417
6753
  (void) pthread_mutex_unlock(&LOCK_open);
6418
6754
}
6419
6755
 
6434
6770
*/
6435
6771
 
6436
6772
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6437
 
                             uint32_t flags)
 
6773
                             uint flags)
6438
6774
{
6439
6775
  char key[MAX_DBKEY_LENGTH];
6440
 
  uint32_t key_length;
6441
 
  Table *table;
 
6776
  uint key_length;
 
6777
  TABLE *table;
6442
6778
  TABLE_SHARE *share;
6443
6779
  bool result= 0, signalled= 0;
 
6780
  DBUG_ENTER("remove_table_from_cache");
 
6781
  DBUG_PRINT("enter", ("table: '%s'.'%s'  flags: %u", db, table_name, flags));
6444
6782
 
6445
 
  key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
 
6783
  key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
6446
6784
  for (;;)
6447
6785
  {
6448
6786
    HASH_SEARCH_STATE state;
6449
6787
    result= signalled= 0;
6450
6788
 
6451
 
    for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
 
6789
    for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
6452
6790
                                    &state);
6453
6791
         table;
6454
 
         table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
 
6792
         table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
6455
6793
                                   &state))
6456
6794
    {
6457
6795
      THD *in_use;
 
6796
      DBUG_PRINT("tcache", ("found table: '%s'.'%s' 0x%lx", table->s->db.str,
 
6797
                            table->s->table_name.str, (long) table));
6458
6798
 
6459
6799
      table->s->version=0L;             /* Free when thread is ready */
6460
6800
      if (!(in_use=table->in_use))
6461
6801
      {
 
6802
        DBUG_PRINT("info",("Table was not in use"));
6462
6803
        relink_unused(table);
6463
6804
      }
6464
6805
      else if (in_use != thd)
6465
6806
      {
 
6807
        DBUG_PRINT("info", ("Table was in use by other thread"));
6466
6808
        /*
6467
6809
          Mark that table is going to be deleted from cache. This will
6468
6810
          force threads that are in mysql_lock_tables() (but not yet
6471
6813
        in_use->some_tables_deleted= 1;
6472
6814
        if (table->is_name_opened())
6473
6815
        {
 
6816
          DBUG_PRINT("info", ("Found another active instance of the table"));
6474
6817
          result=1;
6475
6818
        }
 
6819
        /* Kill delayed insert threads */
 
6820
        if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) &&
 
6821
            ! in_use->killed)
 
6822
        {
 
6823
          in_use->killed= THD::KILL_CONNECTION;
 
6824
          pthread_mutex_lock(&in_use->mysys_var->mutex);
 
6825
          if (in_use->mysys_var->current_cond)
 
6826
          {
 
6827
            pthread_mutex_lock(in_use->mysys_var->current_mutex);
 
6828
            signalled= 1;
 
6829
            pthread_cond_broadcast(in_use->mysys_var->current_cond);
 
6830
            pthread_mutex_unlock(in_use->mysys_var->current_mutex);
 
6831
          }
 
6832
          pthread_mutex_unlock(&in_use->mysys_var->mutex);
 
6833
        }
6476
6834
        /*
6477
6835
          Now we must abort all tables locks used by this thread
6478
6836
          as the thread may be waiting to get a lock for another table.
6482
6840
          open_tables list. Aborting the MERGE lock after a child was
6483
6841
          closed and before the parent is closed would be fatal.
6484
6842
        */
6485
 
        for (Table *thd_table= in_use->open_tables;
 
6843
        for (TABLE *thd_table= in_use->open_tables;
6486
6844
             thd_table ;
6487
6845
             thd_table= thd_table->next)
6488
6846
        {
6492
6850
        }
6493
6851
      }
6494
6852
      else
 
6853
      {
 
6854
        DBUG_PRINT("info", ("Table was in use by current thread. db_stat: %u",
 
6855
                            table->db_stat));
6495
6856
        result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
 
6857
      }
6496
6858
    }
6497
6859
    while (unused_tables && !unused_tables->s->version)
6498
 
      hash_delete(&open_cache,(unsigned char*) unused_tables);
 
6860
      VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6499
6861
 
 
6862
    DBUG_PRINT("info", ("Removing table from table_def_cache"));
6500
6863
    /* Remove table from table definition cache if it's not in use */
6501
 
    if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
 
6864
    if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6502
6865
                                           key_length)))
6503
6866
    {
 
6867
      DBUG_PRINT("info", ("share version: %lu  ref_count: %u",
 
6868
                          share->version, share->ref_count));
6504
6869
      share->version= 0;                          // Mark for delete
6505
6870
      if (share->ref_count == 0)
6506
6871
      {
6507
6872
        pthread_mutex_lock(&share->mutex);
6508
 
        hash_delete(&table_def_cache, (unsigned char*) share);
 
6873
        VOID(hash_delete(&table_def_cache, (uchar*) share));
6509
6874
      }
6510
6875
    }
6511
6876
 
6516
6881
        reopen their tables
6517
6882
      */
6518
6883
      broadcast_refresh();
 
6884
      DBUG_PRINT("info", ("Waiting for refresh signal"));
6519
6885
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !thd->killed)
6520
6886
      {
6521
6887
        dropping_tables++;
6528
6894
            It can happen that another thread has opened the
6529
6895
            table but has not yet locked any table at all. Since
6530
6896
            it can be locked waiting for a table that our thread
6531
 
            has done LOCK Table x WRITE on previously, we need to
 
6897
            has done LOCK TABLE x WRITE on previously, we need to
6532
6898
            ensure that the thread actually hears our signal
6533
6899
            before we go to sleep. Thus we wait for a short time
6534
6900
            and then we retry another loop in the
6543
6909
    }
6544
6910
    break;
6545
6911
  }
6546
 
  return(result);
 
6912
  DBUG_RETURN(result);
6547
6913
}
6548
6914
 
6549
6915
 
6551
6917
{
6552
6918
  return a->length == b->length && !strncmp(a->str, b->str, a->length);
6553
6919
}
 
6920
 
 
6921
 
 
6922
/*
 
6923
  Open and lock system tables for read.
 
6924
 
 
6925
  SYNOPSIS
 
6926
    open_system_tables_for_read()
 
6927
      thd         Thread context.
 
6928
      table_list  List of tables to open.
 
6929
      backup      Pointer to Open_tables_state instance where
 
6930
                  information about currently open tables will be
 
6931
                  saved, and from which will be restored when we will
 
6932
                  end work with system tables.
 
6933
 
 
6934
  NOTES
 
6935
    Thanks to restrictions which we put on opening and locking of
 
6936
    system tables for writing, we can open and lock them for reading
 
6937
    even when we already have some other tables open and locked.  One
 
6938
    must call close_system_tables() to close systems tables opened
 
6939
    with this call.
 
6940
 
 
6941
  RETURN
 
6942
    false   Success
 
6943
    true    Error
 
6944
*/
 
6945
 
 
6946
bool
 
6947
open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
 
6948
                            Open_tables_state *backup)
 
6949
{
 
6950
  DBUG_ENTER("open_system_tables_for_read");
 
6951
 
 
6952
  thd->reset_n_backup_open_tables_state(backup);
 
6953
 
 
6954
  uint count= 0;
 
6955
  bool not_used;
 
6956
  for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
 
6957
  {
 
6958
    TABLE *table= open_table(thd, tables, thd->mem_root, &not_used,
 
6959
                             MYSQL_LOCK_IGNORE_FLUSH);
 
6960
    if (!table)
 
6961
      goto error;
 
6962
 
 
6963
    DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
6964
 
 
6965
    table->use_all_columns();
 
6966
    table->reginfo.lock_type= tables->lock_type;
 
6967
    tables->table= table;
 
6968
    count++;
 
6969
  }
 
6970
 
 
6971
  {
 
6972
    TABLE **list= (TABLE**) thd->alloc(sizeof(TABLE*) * count);
 
6973
    TABLE **ptr= list;
 
6974
    for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
 
6975
      *(ptr++)= tables->table;
 
6976
 
 
6977
    thd->lock= mysql_lock_tables(thd, list, count,
 
6978
                                 MYSQL_LOCK_IGNORE_FLUSH, &not_used);
 
6979
  }
 
6980
  if (thd->lock)
 
6981
    DBUG_RETURN(false);
 
6982
 
 
6983
error:
 
6984
  close_system_tables(thd, backup);
 
6985
 
 
6986
  DBUG_RETURN(true);
 
6987
}
 
6988
 
 
6989
 
 
6990
/*
 
6991
  Close system tables, opened with open_system_tables_for_read().
 
6992
 
 
6993
  SYNOPSIS
 
6994
    close_system_tables()
 
6995
      thd     Thread context
 
6996
      backup  Pointer to Open_tables_state instance which holds
 
6997
              information about tables which were open before we
 
6998
              decided to access system tables.
 
6999
*/
 
7000
 
 
7001
void
 
7002
close_system_tables(THD *thd, Open_tables_state *backup)
 
7003
{
 
7004
  close_thread_tables(thd);
 
7005
  thd->restore_backup_open_tables_state(backup);
 
7006
}
 
7007
 
 
7008
 
 
7009
/*
 
7010
  Open and lock one system table for update.
 
7011
 
 
7012
  SYNOPSIS
 
7013
    open_system_table_for_update()
 
7014
      thd        Thread context.
 
7015
      one_table  Table to open.
 
7016
 
 
7017
  NOTES
 
7018
    Table opened with this call should closed using close_thread_tables().
 
7019
 
 
7020
  RETURN
 
7021
    0   Error
 
7022
    #   Pointer to TABLE object of system table
 
7023
*/
 
7024
 
 
7025
TABLE *
 
7026
open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
 
7027
{
 
7028
  DBUG_ENTER("open_system_table_for_update");
 
7029
 
 
7030
  TABLE *table= open_ltable(thd, one_table, one_table->lock_type, 0);
 
7031
  if (table)
 
7032
  {
 
7033
    DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
7034
    table->use_all_columns();
 
7035
  }
 
7036
 
 
7037
  DBUG_RETURN(table);
 
7038
}
 
7039
 
6554
7040
/**
6555
7041
  @} (end of group Data_Dictionary)
6556
7042
*/