~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/table.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
/* Structs that defines the TABLE */
 
18
 
 
19
class Item;                             /* Needed by ORDER */
 
20
class Item_subselect;
 
21
class Item_field;
 
22
class GRANT_TABLE;
 
23
class st_select_lex_unit;
 
24
class st_select_lex;
 
25
class partition_info;
 
26
class COND_EQUAL;
 
27
class Security_context;
 
28
 
 
29
/*************************************************************************/
 
30
 
 
31
/**
 
32
 View_creation_ctx -- creation context of view objects.
 
33
*/
 
34
 
 
35
class View_creation_ctx : public Default_object_creation_ctx,
 
36
                          public Sql_alloc
 
37
{
 
38
public:
 
39
  static View_creation_ctx *create(THD *thd);
 
40
 
 
41
  static View_creation_ctx *create(THD *thd,
 
42
                                   TABLE_LIST *view);
 
43
 
 
44
private:
 
45
  View_creation_ctx(THD *thd)
 
46
    : Default_object_creation_ctx(thd)
 
47
  { }
 
48
};
 
49
 
 
50
/*************************************************************************/
 
51
 
 
52
/* Order clause list element */
 
53
 
 
54
typedef struct st_order {
 
55
  struct st_order *next;
 
56
  Item   **item;                        /* Point at item in select fields */
 
57
  Item   *item_ptr;                     /* Storage for initial item */
 
58
  Item   **item_copy;                   /* For SPs; the original item ptr */
 
59
  int    counter;                       /* position in SELECT list, correct
 
60
                                           only if counter_used is true*/
 
61
  bool   asc;                           /* true if ascending */
 
62
  bool   free_me;                       /* true if item isn't shared  */
 
63
  bool   in_field_list;                 /* true if in select field list */
 
64
  bool   counter_used;                  /* parameter was counter of columns */
 
65
  Field  *field;                        /* If tmp-table group */
 
66
  char   *buff;                         /* If tmp-table group */
 
67
  table_map used, depend_map;
 
68
} ORDER;
 
69
 
 
70
/**
 
71
   @brief The current state of the privilege checking process for the current
 
72
   user, SQL statement and SQL object.
 
73
 
 
74
   @details The privilege checking process is divided into phases depending on
 
75
   the level of the privilege to be checked and the type of object to be
 
76
   accessed. Due to the mentioned scattering of privilege checking
 
77
   functionality, it is necessary to keep track of the state of the
 
78
   process. This information is stored in privilege, want_privilege, and
 
79
   orig_want_privilege.
 
80
 
 
81
   A GRANT_INFO also serves as a cache of the privilege hash tables. Relevant
 
82
   members are grant_table and version.
 
83
 */
 
84
typedef struct st_grant_info
 
85
{
 
86
  /**
 
87
     @brief A copy of the privilege information regarding the current host,
 
88
     database, object and user.
 
89
 
 
90
     @details The version of this copy is found in GRANT_INFO::version.
 
91
   */
 
92
  GRANT_TABLE *grant_table;
 
93
  /**
 
94
     @brief Used for cache invalidation when caching privilege information.
 
95
 
 
96
     @details The privilege information is stored on disk, with dedicated
 
97
     caches residing in memory: table-level and column-level privileges,
 
98
     respectively, have their own dedicated caches.
 
99
 
 
100
     The GRANT_INFO works as a level 1 cache with this member updated to the
 
101
     current value of the global variable @c grant_version (@c static variable
 
102
     in sql_acl.cc). It is updated Whenever the GRANT_INFO is refreshed from
 
103
     the level 2 cache. The level 2 cache is the @c column_priv_hash structure
 
104
     (@c static variable in sql_acl.cc)
 
105
 
 
106
     @see grant_version
 
107
   */
 
108
  uint version;
 
109
  /**
 
110
     @brief The set of privileges that the current user has fulfilled for a
 
111
     certain host, database, and object.
 
112
     
 
113
     @details This field is continually updated throughout the access checking
 
114
     process. In each step the "wanted privilege" is checked against the
 
115
     fulfilled privileges. When/if the intersection of these sets is empty,
 
116
     access is granted.
 
117
 
 
118
     The set is implemented as a bitmap, with the bits defined in sql_acl.h.
 
119
   */
 
120
  ulong privilege;
 
121
  /**
 
122
     @brief the set of privileges that the current user needs to fulfil in
 
123
     order to carry out the requested operation.
 
124
   */
 
125
  ulong want_privilege;
 
126
  /**
 
127
    Stores the requested access acl of top level tables list. Is used to
 
128
    check access rights to the underlying tables of a view.
 
129
  */
 
130
  ulong orig_want_privilege;
 
131
} GRANT_INFO;
 
132
 
 
133
enum tmp_table_type
 
134
{
 
135
  NO_TMP_TABLE, NON_TRANSACTIONAL_TMP_TABLE, TRANSACTIONAL_TMP_TABLE,
 
136
  INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE
 
137
};
 
138
 
 
139
/** Event on which trigger is invoked. */
 
140
enum trg_event_type
 
141
{
 
142
  TRG_EVENT_INSERT= 0,
 
143
  TRG_EVENT_UPDATE= 1,
 
144
  TRG_EVENT_DELETE= 2,
 
145
  TRG_EVENT_MAX
 
146
};
 
147
 
 
148
enum frm_type_enum
 
149
{
 
150
  FRMTYPE_ERROR= 0,
 
151
  FRMTYPE_TABLE,
 
152
  FRMTYPE_VIEW
 
153
};
 
154
 
 
155
enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP };
 
156
 
 
157
typedef struct st_filesort_info
 
158
{
 
159
  IO_CACHE *io_cache;           /* If sorted through filesort */
 
160
  uchar     **sort_keys;        /* Buffer for sorting keys */
 
161
  uchar     *buffpek;           /* Buffer for buffpek structures */
 
162
  uint      buffpek_len;        /* Max number of buffpeks in the buffer */
 
163
  uchar     *addon_buf;         /* Pointer to a buffer if sorted with fields */
 
164
  size_t    addon_length;       /* Length of the buffer */
 
165
  struct st_sort_addon_field *addon_field;     /* Pointer to the fields info */
 
166
  void    (*unpack)(struct st_sort_addon_field *, uchar *); /* To unpack back */
 
167
  uchar     *record_pointers;    /* If sorted in memory */
 
168
  ha_rows   found_records;      /* How many records in sort */
 
169
} FILESORT_INFO;
 
170
 
 
171
 
 
172
/*
 
173
  Values in this enum are used to indicate how a tables TIMESTAMP field
 
174
  should be treated. It can be set to the current timestamp on insert or
 
175
  update or both.
 
176
  WARNING: The values are used for bit operations. If you change the
 
177
  enum, you must keep the bitwise relation of the values. For example:
 
178
  (int) TIMESTAMP_AUTO_SET_ON_BOTH must be equal to
 
179
  (int) TIMESTAMP_AUTO_SET_ON_INSERT | (int) TIMESTAMP_AUTO_SET_ON_UPDATE.
 
180
  We use an enum here so that the debugger can display the value names.
 
181
*/
 
182
enum timestamp_auto_set_type
 
183
{
 
184
  TIMESTAMP_NO_AUTO_SET= 0, TIMESTAMP_AUTO_SET_ON_INSERT= 1,
 
185
  TIMESTAMP_AUTO_SET_ON_UPDATE= 2, TIMESTAMP_AUTO_SET_ON_BOTH= 3
 
186
};
 
187
#define clear_timestamp_auto_bits(_target_, _bits_) \
 
188
  (_target_)= (enum timestamp_auto_set_type)((int)(_target_) & ~(int)(_bits_))
 
189
 
 
190
class Field_timestamp;
 
191
class Field_blob;
 
192
class Table_triggers_list;
 
193
 
 
194
/**
 
195
  Category of table found in the table share.
 
196
*/
 
197
enum enum_table_category
 
198
{
 
199
  /**
 
200
    Unknown value.
 
201
  */
 
202
  TABLE_UNKNOWN_CATEGORY=0,
 
203
 
 
204
  /**
 
205
    Temporary table.
 
206
    The table is visible only in the session.
 
207
    Therefore,
 
208
    - FLUSH TABLES WITH READ LOCK
 
209
    - SET GLOBAL READ_ONLY = ON
 
210
    do not apply to this table.
 
211
    Note that LOCK TABLE t FOR READ/WRITE
 
212
    can be used on temporary tables.
 
213
    Temporary tables are not part of the table cache.
 
214
  */
 
215
  TABLE_CATEGORY_TEMPORARY=1,
 
216
 
 
217
  /**
 
218
    User table.
 
219
    These tables do honor:
 
220
    - LOCK TABLE t FOR READ/WRITE
 
221
    - FLUSH TABLES WITH READ LOCK
 
222
    - SET GLOBAL READ_ONLY = ON
 
223
    User tables are cached in the table cache.
 
224
  */
 
225
  TABLE_CATEGORY_USER=2,
 
226
 
 
227
  /**
 
228
    System table, maintained by the server.
 
229
    These tables do honor:
 
230
    - LOCK TABLE t FOR READ/WRITE
 
231
    - FLUSH TABLES WITH READ LOCK
 
232
    - SET GLOBAL READ_ONLY = ON
 
233
    Typically, writes to system tables are performed by
 
234
    the server implementation, not explicitly be a user.
 
235
    System tables are cached in the table cache.
 
236
  */
 
237
  TABLE_CATEGORY_SYSTEM=3,
 
238
 
 
239
  /**
 
240
    Information schema tables.
 
241
    These tables are an interface provided by the system
 
242
    to inspect the system metadata.
 
243
    These tables do *not* honor:
 
244
    - LOCK TABLE t FOR READ/WRITE
 
245
    - FLUSH TABLES WITH READ LOCK
 
246
    - SET GLOBAL READ_ONLY = ON
 
247
    as there is no point in locking explicitely
 
248
    an INFORMATION_SCHEMA table.
 
249
    Nothing is directly written to information schema tables.
 
250
    Note that this value is not used currently,
 
251
    since information schema tables are not shared,
 
252
    but implemented as session specific temporary tables.
 
253
  */
 
254
  /*
 
255
    TODO: Fixing the performance issues of I_S will lead
 
256
    to I_S tables in the table cache, which should use
 
257
    this table type.
 
258
  */
 
259
  TABLE_CATEGORY_INFORMATION=4,
 
260
 
 
261
  /**
 
262
    Performance schema tables.
 
263
    These tables are an interface provided by the system
 
264
    to inspect the system performance data.
 
265
    These tables do *not* honor:
 
266
    - LOCK TABLE t FOR READ/WRITE
 
267
    - FLUSH TABLES WITH READ LOCK
 
268
    - SET GLOBAL READ_ONLY = ON
 
269
    as there is no point in locking explicitely
 
270
    a PERFORMANCE_SCHEMA table.
 
271
    An example of PERFORMANCE_SCHEMA tables are:
 
272
    - mysql.slow_log
 
273
    - mysql.general_log,
 
274
    which *are* updated even when there is either
 
275
    a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
 
276
    User queries do not write directly to these tables
 
277
    (there are exceptions for log tables).
 
278
    The server implementation perform writes.
 
279
    Performance tables are cached in the table cache.
 
280
  */
 
281
  TABLE_CATEGORY_PERFORMANCE=5
 
282
};
 
283
typedef enum enum_table_category TABLE_CATEGORY;
 
284
 
 
285
TABLE_CATEGORY get_table_category(const LEX_STRING *db,
 
286
                                  const LEX_STRING *name);
 
287
 
 
288
 
 
289
typedef struct st_table_field_type
 
290
{
 
291
  LEX_STRING name;
 
292
  LEX_STRING type;
 
293
  LEX_STRING cset;
 
294
} TABLE_FIELD_TYPE;
 
295
 
 
296
 
 
297
typedef struct st_table_field_def
 
298
{
 
299
  uint count;
 
300
  const TABLE_FIELD_TYPE *field;
 
301
} TABLE_FIELD_DEF;
 
302
 
 
303
 
 
304
class Table_check_intact
 
305
{
 
306
protected:
 
307
  virtual void report_error(uint code, const char *fmt, ...)= 0;
 
308
 
 
309
public:
 
310
  Table_check_intact() {}
 
311
  virtual ~Table_check_intact() {}
 
312
 
 
313
  /** Checks whether a table is intact. */
 
314
  bool check(TABLE *table, const TABLE_FIELD_DEF *table_def);
 
315
};
 
316
 
 
317
 
 
318
/*
 
319
  This structure is shared between different table objects. There is one
 
320
  instance of table share per one table in the database.
 
321
*/
 
322
 
 
323
typedef struct st_table_share
 
324
{
 
325
  st_table_share() {}                    /* Remove gcc warning */
 
326
 
 
327
  /** Category of this table. */
 
328
  TABLE_CATEGORY table_category;
 
329
 
 
330
  /* hash of field names (contains pointers to elements of field array) */
 
331
  HASH  name_hash;                      /* hash of field names */
 
332
  MEM_ROOT mem_root;
 
333
  TYPELIB keynames;                     /* Pointers to keynames */
 
334
  TYPELIB fieldnames;                   /* Pointer to fieldnames */
 
335
  TYPELIB *intervals;                   /* pointer to interval info */
 
336
  pthread_mutex_t mutex;                /* For locking the share  */
 
337
  pthread_cond_t cond;                  /* To signal that share is ready */
 
338
  struct st_table_share *next,          /* Link to unused shares */
 
339
    **prev;
 
340
#ifdef NOT_YET
 
341
  struct st_table *open_tables;         /* link to open tables */
 
342
#endif
 
343
 
 
344
  /* The following is copied to each TABLE on OPEN */
 
345
  Field **field;
 
346
  Field **found_next_number_field;
 
347
  Field *timestamp_field;               /* Used only during open */
 
348
  KEY  *key_info;                       /* data of keys in database */
 
349
  uint  *blob_field;                    /* Index to blobs in Field arrray*/
 
350
 
 
351
  uchar *default_values;                /* row with default values */
 
352
  LEX_STRING comment;                   /* Comment about table */
 
353
  CHARSET_INFO *table_charset;          /* Default charset of string fields */
 
354
 
 
355
  MY_BITMAP all_set;
 
356
  /*
 
357
    Key which is used for looking-up table in table cache and in the list
 
358
    of thread's temporary tables. Has the form of:
 
359
      "database_name\0table_name\0" + optional part for temporary tables.
 
360
 
 
361
    Note that all three 'table_cache_key', 'db' and 'table_name' members
 
362
    must be set (and be non-zero) for tables in table cache. They also
 
363
    should correspond to each other.
 
364
    To ensure this one can use set_table_cache() methods.
 
365
  */
 
366
  LEX_STRING table_cache_key;
 
367
  LEX_STRING db;                        /* Pointer to db */
 
368
  LEX_STRING table_name;                /* Table name (for open) */
 
369
  LEX_STRING path;                      /* Path to .frm file (from datadir) */
 
370
  LEX_STRING normalized_path;           /* unpack_filename(path) */
 
371
  LEX_STRING connect_string;
 
372
 
 
373
  /* 
 
374
     Set of keys in use, implemented as a Bitmap.
 
375
     Excludes keys disabled by ALTER TABLE ... DISABLE KEYS.
 
376
  */
 
377
  key_map keys_in_use;
 
378
  key_map keys_for_keyread;
 
379
  ha_rows min_rows, max_rows;           /* create information */
 
380
  ulong   avg_row_length;               /* create information */
 
381
  ulong   raid_chunksize;
 
382
  ulong   version, mysql_version;
 
383
  ulong   timestamp_offset;             /* Set to offset+1 of record */
 
384
  ulong   reclength;                    /* Recordlength */
 
385
 
 
386
  plugin_ref db_plugin;                 /* storage engine plugin */
 
387
  inline handlerton *db_type() const    /* table_type for handler */
 
388
  { 
 
389
    // DBUG_ASSERT(db_plugin);
 
390
    return db_plugin ? plugin_data(db_plugin, handlerton*) : NULL;
 
391
  }
 
392
  enum row_type row_type;               /* How rows are stored */
 
393
  enum tmp_table_type tmp_table;
 
394
  enum enum_ha_unused unused1;
 
395
  enum enum_ha_unused unused2;
 
396
 
 
397
  uint ref_count;                       /* How many TABLE objects uses this */
 
398
  uint open_count;                      /* Number of tables in open list */
 
399
  uint blob_ptr_size;                   /* 4 or 8 */
 
400
  uint key_block_size;                  /* create key_block_size, if used */
 
401
  uint null_bytes, last_null_bit_pos;
 
402
  uint fields;                          /* Number of fields */
 
403
  uint rec_buff_length;                 /* Size of table->record[] buffer */
 
404
  uint keys, key_parts;
 
405
  uint max_key_length, max_unique_length, total_key_length;
 
406
  uint uniques;                         /* Number of UNIQUE index */
 
407
  uint null_fields;                     /* number of null fields */
 
408
  uint blob_fields;                     /* number of blob fields */
 
409
  uint timestamp_field_offset;          /* Field number for timestamp field */
 
410
  uint varchar_fields;                  /* number of varchar fields */
 
411
  uint db_create_options;               /* Create options from database */
 
412
  uint db_options_in_use;               /* Options in use */
 
413
  uint db_record_offset;                /* if HA_REC_IN_SEQ */
 
414
  uint raid_type, raid_chunks;
 
415
  uint rowid_field_offset;              /* Field_nr +1 to rowid field */
 
416
  /* Index of auto-updated TIMESTAMP field in field array */
 
417
  uint primary_key;
 
418
  uint next_number_index;               /* autoincrement key number */
 
419
  uint next_number_key_offset;          /* autoinc keypart offset in a key */
 
420
  uint next_number_keypart;             /* autoinc keypart number in a key */
 
421
  uint error, open_errno, errarg;       /* error from open_table_def() */
 
422
  uint column_bitmap_size;
 
423
  uchar frm_version;
 
424
  bool null_field_first;
 
425
  bool system;                          /* Set if system table (one record) */
 
426
  bool crypted;                         /* If .frm file is crypted */
 
427
  bool db_low_byte_first;               /* Portable row format */
 
428
  bool crashed;
 
429
  bool is_view;
 
430
  bool name_lock, replace_with_name_lock;
 
431
  bool waiting_on_cond;                 /* Protection against free */
 
432
  ulong table_map_id;                   /* for row-based replication */
 
433
  ulonglong table_map_version;
 
434
 
 
435
  /*
 
436
    Cache for row-based replication table share checks that does not
 
437
    need to be repeated. Possible values are: -1 when cache value is
 
438
    not calculated yet, 0 when table *shall not* be replicated, 1 when
 
439
    table *may* be replicated.
 
440
  */
 
441
  int cached_row_logging_check;
 
442
 
 
443
#ifdef WITH_PARTITION_STORAGE_ENGINE
 
444
  /** @todo: Move into *ha_data for partitioning */
 
445
  bool auto_partitioned;
 
446
  const char *partition_info;
 
447
  uint  partition_info_len;
 
448
  uint  partition_info_buffer_size;
 
449
  const char *part_state;
 
450
  uint part_state_len;
 
451
  handlerton *default_part_db_type;
 
452
#endif
 
453
 
 
454
  /**
 
455
    Cache the checked structure of this table.
 
456
 
 
457
    The pointer data is used to describe the structure that
 
458
    a instance of the table must have. Each element of the
 
459
    array specifies a field that must exist on the table.
 
460
 
 
461
    The pointer is cached in order to perform the check only
 
462
    once -- when the table is loaded from the disk.
 
463
  */
 
464
  const TABLE_FIELD_DEF *table_field_def_cache;
 
465
 
 
466
  /** place to store storage engine specific data */
 
467
  void *ha_data;
 
468
 
 
469
 
 
470
  /*
 
471
    Set share's table cache key and update its db and table name appropriately.
 
472
 
 
473
    SYNOPSIS
 
474
      set_table_cache_key()
 
475
        key_buff    Buffer with already built table cache key to be
 
476
                    referenced from share.
 
477
        key_length  Key length.
 
478
 
 
479
    NOTES
 
480
      Since 'key_buff' buffer will be referenced from share it should has same
 
481
      life-time as share itself.
 
482
      This method automatically ensures that TABLE_SHARE::table_name/db have
 
483
      appropriate values by using table cache key as their source.
 
484
  */
 
485
 
 
486
  void set_table_cache_key(char *key_buff, uint key_length)
 
487
  {
 
488
    table_cache_key.str= key_buff;
 
489
    table_cache_key.length= key_length;
 
490
    /*
 
491
      Let us use the fact that the key is "db/0/table_name/0" + optional
 
492
      part for temporary tables.
 
493
    */
 
494
    db.str=            table_cache_key.str;
 
495
    db.length=         strlen(db.str);
 
496
    table_name.str=    db.str + db.length + 1;
 
497
    table_name.length= strlen(table_name.str);
 
498
  }
 
499
 
 
500
 
 
501
  /*
 
502
    Set share's table cache key and update its db and table name appropriately.
 
503
 
 
504
    SYNOPSIS
 
505
      set_table_cache_key()
 
506
        key_buff    Buffer to be used as storage for table cache key
 
507
                    (should be at least key_length bytes).
 
508
        key         Value for table cache key.
 
509
        key_length  Key length.
 
510
 
 
511
    NOTE
 
512
      Since 'key_buff' buffer will be used as storage for table cache key
 
513
      it should has same life-time as share itself.
 
514
  */
 
515
 
 
516
  void set_table_cache_key(char *key_buff, const char *key, uint key_length)
 
517
  {
 
518
    memcpy(key_buff, key, key_length);
 
519
    set_table_cache_key(key_buff, key_length);
 
520
  }
 
521
 
 
522
  inline bool honor_global_locks()
 
523
  {
 
524
    return ((table_category == TABLE_CATEGORY_USER)
 
525
            || (table_category == TABLE_CATEGORY_SYSTEM));
 
526
  }
 
527
 
 
528
  inline bool require_write_privileges()
 
529
  {
 
530
    return (table_category == TABLE_CATEGORY_PERFORMANCE);
 
531
  }
 
532
 
 
533
  inline ulong get_table_def_version()
 
534
  {
 
535
    return table_map_id;
 
536
  }
 
537
 
 
538
  /**
 
539
    Convert unrelated members of TABLE_SHARE to one enum
 
540
    representing its type.
 
541
 
 
542
    @todo perhaps we need to have a member instead of a function.
 
543
  */
 
544
  enum enum_table_ref_type get_table_ref_type() const
 
545
  {
 
546
    if (is_view)
 
547
      return TABLE_REF_VIEW;
 
548
    switch (tmp_table) {
 
549
    case NO_TMP_TABLE:
 
550
      return TABLE_REF_BASE_TABLE;
 
551
    case SYSTEM_TMP_TABLE:
 
552
      return TABLE_REF_I_S_TABLE;
 
553
    default:
 
554
      return TABLE_REF_TMP_TABLE;
 
555
    }
 
556
  }
 
557
  /**
 
558
    Return a table metadata version.
 
559
     * for base tables, we return table_map_id.
 
560
       It is assigned from a global counter incremented for each
 
561
       new table loaded into the table definition cache (TDC).
 
562
     * for temporary tables it's table_map_id again. But for
 
563
       temporary tables table_map_id is assigned from
 
564
       thd->query_id. The latter is assigned from a thread local
 
565
       counter incremented for every new SQL statement. Since
 
566
       temporary tables are thread-local, each temporary table
 
567
       gets a unique id.
 
568
     * for everything else (views, information schema tables),
 
569
       the version id is zero.
 
570
 
 
571
   This choice of version id is a large compromise
 
572
   to have a working prepared statement validation in 5.1. In
 
573
   future version ids will be persistent, as described in WL#4180.
 
574
 
 
575
   Let's try to explain why and how this limited solution allows
 
576
   to validate prepared statements.
 
577
 
 
578
   Firstly, sets (in mathematical sense) of version numbers
 
579
   never intersect for different table types. Therefore,
 
580
   version id of a temporary table is never compared with
 
581
   a version id of a view, and vice versa.
 
582
 
 
583
   Secondly, for base tables, we know that each DDL flushes the
 
584
   respective share from the TDC. This ensures that whenever
 
585
   a table is altered or dropped and recreated, it gets a new
 
586
   version id.
 
587
   Unfortunately, since elements of the TDC are also flushed on
 
588
   LRU basis, this choice of version ids leads to false positives.
 
589
   E.g. when the TDC size is too small, we may have a SELECT
 
590
   * FROM INFORMATION_SCHEMA.TABLES flush all its elements, which
 
591
   in turn will lead to a validation error and a subsequent
 
592
   reprepare of all prepared statements.  This is
 
593
   considered acceptable, since as long as prepared statements are
 
594
   automatically reprepared, spurious invalidation is only
 
595
   a performance hit. Besides, no better simple solution exists.
 
596
 
 
597
   For temporary tables, using thd->query_id ensures that if
 
598
   a temporary table was altered or recreated, a new version id is
 
599
   assigned. This suits validation needs very well and will perhaps
 
600
   never change.
 
601
 
 
602
   Metadata of information schema tables never changes.
 
603
   Thus we can safely assume 0 for a good enough version id.
 
604
 
 
605
   Views are a special and tricky case. A view is always inlined
 
606
   into the parse tree of a prepared statement at prepare.
 
607
   Thus, when we execute a prepared statement, the parse tree
 
608
   will not get modified even if the view is replaced with another
 
609
   view.  Therefore, we can safely choose 0 for version id of
 
610
   views and effectively never invalidate a prepared statement
 
611
   when a view definition is altered. Note, that this leads to
 
612
   wrong binary log in statement-based replication, since we log
 
613
   prepared statement execution in form Query_log_events
 
614
   containing conventional statements. But since there is no
 
615
   metadata locking for views, the very same problem exists for
 
616
   conventional statements alone, as reported in Bug#25144. The only
 
617
   difference between prepared and conventional execution is,
 
618
   effectively, that for prepared statements the race condition
 
619
   window is much wider.
 
620
   In 6.0 we plan to support view metadata locking (WL#3726) and
 
621
   extend table definition cache to cache views (WL#4298).
 
622
   When this is done, views will be handled in the same fashion
 
623
   as the base tables.
 
624
 
 
625
   Finally, by taking into account table type, we always
 
626
   track that a change has taken place when a view is replaced
 
627
   with a base table, a base table is replaced with a temporary
 
628
   table and so on.
 
629
 
 
630
   @sa TABLE_LIST::is_table_ref_id_equal()
 
631
  */
 
632
  ulong get_table_ref_version() const
 
633
  {
 
634
    return (tmp_table == SYSTEM_TMP_TABLE || is_view) ? 0 : table_map_id;
 
635
  }
 
636
 
 
637
} TABLE_SHARE;
 
638
 
 
639
 
 
640
extern ulong refresh_version;
 
641
 
 
642
/* Information for one open table */
 
643
enum index_hint_type
 
644
{
 
645
  INDEX_HINT_IGNORE,
 
646
  INDEX_HINT_USE,
 
647
  INDEX_HINT_FORCE
 
648
};
 
649
 
 
650
struct st_table {
 
651
  st_table() {}                               /* Remove gcc warning */
 
652
 
 
653
  TABLE_SHARE   *s;
 
654
  handler       *file;
 
655
#ifdef NOT_YET
 
656
  struct st_table *used_next, **used_prev;      /* Link to used tables */
 
657
  struct st_table *open_next, **open_prev;      /* Link to open tables */
 
658
#endif
 
659
  struct st_table *next, *prev;
 
660
 
 
661
  /* For the below MERGE related members see top comment in ha_myisammrg.cc */
 
662
  struct st_table *parent;          /* Set in MERGE child.  Ptr to parent */
 
663
  TABLE_LIST      *child_l;         /* Set in MERGE parent. List of children */
 
664
  TABLE_LIST      **child_last_l;   /* Set in MERGE parent. End of list */
 
665
 
 
666
  THD   *in_use;                        /* Which thread uses this */
 
667
  Field **field;                        /* Pointer to fields */
 
668
 
 
669
  uchar *record[2];                     /* Pointer to records */
 
670
  uchar *write_row_record;              /* Used as optimisation in
 
671
                                           THD::write_row */
 
672
  uchar *insert_values;                  /* used by INSERT ... UPDATE */
 
673
  /* 
 
674
    Map of keys that can be used to retrieve all data from this table 
 
675
    needed by the query without reading the row.
 
676
  */
 
677
  key_map covering_keys;
 
678
  key_map quick_keys, merge_keys;
 
679
  /*
 
680
    A set of keys that can be used in the query that references this
 
681
    table.
 
682
 
 
683
    All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be 
 
684
    subtracted from this set upon instantiation. Thus for any TABLE t it holds
 
685
    that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we 
 
686
    must not introduce any new keys here (see setup_tables).
 
687
 
 
688
    The set is implemented as a bitmap.
 
689
  */
 
690
  key_map keys_in_use_for_query;
 
691
  /* Map of keys that can be used to calculate GROUP BY without sorting */
 
692
  key_map keys_in_use_for_group_by;
 
693
  /* Map of keys that can be used to calculate ORDER BY without sorting */
 
694
  key_map keys_in_use_for_order_by;
 
695
  KEY  *key_info;                       /* data of keys in database */
 
696
 
 
697
  Field *next_number_field;             /* Set if next_number is activated */
 
698
  Field *found_next_number_field;       /* Set on open */
 
699
  Field_timestamp *timestamp_field;
 
700
 
 
701
  /* Table's triggers, 0 if there are no of them */
 
702
  Table_triggers_list *triggers;
 
703
  TABLE_LIST *pos_in_table_list;/* Element referring to this table */
 
704
  ORDER         *group;
 
705
  const char    *alias;                   /* alias or table name */
 
706
  uchar         *null_flags;
 
707
  my_bitmap_map *bitmap_init_value;
 
708
  MY_BITMAP     def_read_set, def_write_set, tmp_set; /* containers */
 
709
  MY_BITMAP     *read_set, *write_set;          /* Active column sets */
 
710
  /*
 
711
   The ID of the query that opened and is using this table. Has different
 
712
   meanings depending on the table type.
 
713
 
 
714
   Temporary tables:
 
715
 
 
716
   table->query_id is set to thd->query_id for the duration of a statement
 
717
   and is reset to 0 once it is closed by the same statement. A non-zero
 
718
   table->query_id means that a statement is using the table even if it's
 
719
   not the current statement (table is in use by some outer statement).
 
720
 
 
721
   Non-temporary tables:
 
722
 
 
723
   Under pre-locked or LOCK TABLES mode: query_id is set to thd->query_id
 
724
   for the duration of a statement and is reset to 0 once it is closed by
 
725
   the same statement. A non-zero query_id is used to control which tables
 
726
   in the list of pre-opened and locked tables are actually being used.
 
727
  */
 
728
  query_id_t    query_id;
 
729
 
 
730
  /* 
 
731
    For each key that has quick_keys.is_set(key) == TRUE: estimate of #records
 
732
    and max #key parts that range access would use.
 
733
  */
 
734
  ha_rows       quick_rows[MAX_KEY];
 
735
 
 
736
  /* Bitmaps of key parts that =const for the entire join. */
 
737
  key_part_map  const_key_parts[MAX_KEY];
 
738
 
 
739
  uint          quick_key_parts[MAX_KEY];
 
740
  uint          quick_n_ranges[MAX_KEY];
 
741
 
 
742
  /* 
 
743
    Estimate of number of records that satisfy SARGable part of the table
 
744
    condition, or table->file->records if no SARGable condition could be
 
745
    constructed.
 
746
    This value is used by join optimizer as an estimate of number of records
 
747
    that will pass the table condition (condition that depends on fields of 
 
748
    this table and constants)
 
749
  */
 
750
  ha_rows       quick_condition_rows;
 
751
 
 
752
  /*
 
753
    If this table has TIMESTAMP field with auto-set property (pointed by
 
754
    timestamp_field member) then this variable indicates during which
 
755
    operations (insert only/on update/in both cases) we should set this
 
756
    field to current timestamp. If there are no such field in this table
 
757
    or we should not automatically set its value during execution of current
 
758
    statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
 
759
 
 
760
    Value of this variable is set for each statement in open_table() and
 
761
    if needed cleared later in statement processing code (see mysql_update()
 
762
    as example).
 
763
  */
 
764
  timestamp_auto_set_type timestamp_field_type;
 
765
  table_map     map;                    /* ID bit of table (1,2,4,8,16...) */
 
766
 
 
767
  uint          lock_position;          /* Position in MYSQL_LOCK.table */
 
768
  uint          lock_data_start;        /* Start pos. in MYSQL_LOCK.locks */
 
769
  uint          lock_count;             /* Number of locks */
 
770
  uint          tablenr,used_fields;
 
771
  uint          temp_pool_slot;         /* Used by intern temp tables */
 
772
  uint          status;                 /* What's in record[0] */
 
773
  uint          db_stat;                /* mode of file as in handler.h */
 
774
  /* number of select if it is derived table */
 
775
  uint          derived_select_number;
 
776
  int           current_lock;           /* Type of lock on table */
 
777
  my_bool copy_blobs;                   /* copy_blobs when storing */
 
778
 
 
779
  /*
 
780
    0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
 
781
    If maybe_null !=0, this table is inner w.r.t. some outer join operation,
 
782
    and null_row may be true.
 
783
  */
 
784
  uint maybe_null;
 
785
  /*
 
786
    If true, the current table row is considered to have all columns set to 
 
787
    NULL, including columns declared as "not null" (see maybe_null).
 
788
  */
 
789
  my_bool null_row;
 
790
 
 
791
  /*
 
792
    TODO: Each of the following flags take up 8 bits. They can just as easily
 
793
    be put into one single unsigned long and instead of taking up 18
 
794
    bytes, it would take up 4.
 
795
  */
 
796
  my_bool force_index;
 
797
 
 
798
  /**
 
799
    Flag set when the statement contains FORCE INDEX FOR ORDER BY
 
800
    See TABLE_LIST::process_index_hints().
 
801
  */
 
802
  my_bool force_index_order;
 
803
 
 
804
  /**
 
805
    Flag set when the statement contains FORCE INDEX FOR GROUP BY
 
806
    See TABLE_LIST::process_index_hints().
 
807
  */
 
808
  my_bool force_index_group;
 
809
  my_bool distinct,const_table,no_rows;
 
810
 
 
811
  /**
 
812
     If set, the optimizer has found that row retrieval should access index 
 
813
     tree only.
 
814
   */
 
815
  my_bool key_read;
 
816
  my_bool no_keyread;
 
817
  /*
 
818
    Placeholder for an open table which prevents other connections
 
819
    from taking name-locks on this table. Typically used with
 
820
    TABLE_SHARE::version member to take an exclusive name-lock on
 
821
    this table name -- a name lock that not only prevents other
 
822
    threads from opening the table, but also blocks other name
 
823
    locks. This is achieved by:
 
824
    - setting open_placeholder to 1 - this will block other name
 
825
      locks, as wait_for_locked_table_name will be forced to wait,
 
826
      see table_is_used for details.
 
827
    - setting version to 0 - this will force other threads to close
 
828
      the instance of this table and wait (this is the same approach
 
829
      as used for usual name locks).
 
830
    An exclusively name-locked table currently can have no handler
 
831
    object associated with it (db_stat is always 0), but please do
 
832
    not rely on that.
 
833
  */
 
834
  my_bool open_placeholder;
 
835
  my_bool locked_by_logger;
 
836
  my_bool no_replicate;
 
837
  my_bool locked_by_name;
 
838
  my_bool fulltext_searched;
 
839
  my_bool no_cache;
 
840
  /* To signal that the table is associated with a HANDLER statement */
 
841
  my_bool open_by_handler;
 
842
  /*
 
843
    To indicate that a non-null value of the auto_increment field
 
844
    was provided by the user or retrieved from the current record.
 
845
    Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
 
846
  */
 
847
  my_bool auto_increment_field_not_null;
 
848
  my_bool insert_or_update;             /* Can be used by the handler */
 
849
  my_bool alias_name_used;              /* true if table_name is alias */
 
850
  my_bool get_fields_in_item_tree;      /* Signal to fix_field */
 
851
  /* If MERGE children attached to parent. See top comment in ha_myisammrg.cc */
 
852
  my_bool children_attached;
 
853
 
 
854
  REGINFO reginfo;                      /* field connections */
 
855
  MEM_ROOT mem_root;
 
856
  GRANT_INFO grant;
 
857
  FILESORT_INFO sort;
 
858
#ifdef WITH_PARTITION_STORAGE_ENGINE
 
859
  partition_info *part_info;            /* Partition related information */
 
860
  bool no_partitions_used; /* If true, all partitions have been pruned away */
 
861
#endif
 
862
 
 
863
  bool fill_item_list(List<Item> *item_list) const;
 
864
  void reset_item_list(List<Item> *item_list) const;
 
865
  void clear_column_bitmaps(void);
 
866
  void prepare_for_position(void);
 
867
  void mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *map);
 
868
  void mark_columns_used_by_index(uint index);
 
869
  void restore_column_maps_after_mark_index();
 
870
  void mark_auto_increment_column(void);
 
871
  void mark_columns_needed_for_update(void);
 
872
  void mark_columns_needed_for_delete(void);
 
873
  void mark_columns_needed_for_insert(void);
 
874
  inline void column_bitmaps_set(MY_BITMAP *read_set_arg,
 
875
                                 MY_BITMAP *write_set_arg)
 
876
  {
 
877
    read_set= read_set_arg;
 
878
    write_set= write_set_arg;
 
879
    if (file)
 
880
      file->column_bitmaps_signal();
 
881
  }
 
882
  inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg,
 
883
                                           MY_BITMAP *write_set_arg)
 
884
  {
 
885
    read_set= read_set_arg;
 
886
    write_set= write_set_arg;
 
887
  }
 
888
  inline void use_all_columns()
 
889
  {
 
890
    column_bitmaps_set(&s->all_set, &s->all_set);
 
891
  }
 
892
  inline void default_column_bitmaps()
 
893
  {
 
894
    read_set= &def_read_set;
 
895
    write_set= &def_write_set;
 
896
  }
 
897
  /* Is table open or should be treated as such by name-locking? */
 
898
  inline bool is_name_opened() { return db_stat || open_placeholder; }
 
899
  /*
 
900
    Is this instance of the table should be reopen or represents a name-lock?
 
901
  */
 
902
  inline bool needs_reopen_or_name_lock()
 
903
  { return s->version != refresh_version; }
 
904
  bool is_children_attached(void);
 
905
  inline void set_keyread(bool flag)
 
906
  {
 
907
    DBUG_ASSERT(file);
 
908
    if (flag && !key_read)
 
909
    {
 
910
      key_read= 1;
 
911
      file->extra(HA_EXTRA_KEYREAD);
 
912
    }
 
913
    else if (!flag && key_read)
 
914
    {
 
915
      key_read= 0;
 
916
      file->extra(HA_EXTRA_NO_KEYREAD);
 
917
    }
 
918
  }
 
919
};
 
920
 
 
921
enum enum_schema_table_state
 
922
 
923
  NOT_PROCESSED= 0,
 
924
  PROCESSED_BY_CREATE_SORT_INDEX,
 
925
  PROCESSED_BY_JOIN_EXEC
 
926
};
 
927
 
 
928
typedef struct st_foreign_key_info
 
929
{
 
930
  LEX_STRING *forein_id;
 
931
  LEX_STRING *referenced_db;
 
932
  LEX_STRING *referenced_table;
 
933
  LEX_STRING *update_method;
 
934
  LEX_STRING *delete_method;
 
935
  LEX_STRING *referenced_key_name;
 
936
  List<LEX_STRING> foreign_fields;
 
937
  List<LEX_STRING> referenced_fields;
 
938
} FOREIGN_KEY_INFO;
 
939
 
 
940
/*
 
941
  Make sure that the order of schema_tables and enum_schema_tables are the same.
 
942
*/
 
943
 
 
944
enum enum_schema_tables
 
945
{
 
946
  SCH_CHARSETS= 0,
 
947
  SCH_COLLATIONS,
 
948
  SCH_COLLATION_CHARACTER_SET_APPLICABILITY,
 
949
  SCH_COLUMNS,
 
950
  SCH_COLUMN_PRIVILEGES,
 
951
  SCH_ENGINES,
 
952
  SCH_EVENTS,
 
953
  SCH_FILES,
 
954
  SCH_GLOBAL_STATUS,
 
955
  SCH_GLOBAL_VARIABLES,
 
956
  SCH_KEY_COLUMN_USAGE,
 
957
  SCH_OPEN_TABLES,
 
958
  SCH_PARTITIONS,
 
959
  SCH_PLUGINS,
 
960
  SCH_PROCESSLIST,
 
961
  SCH_PROFILES,
 
962
  SCH_REFERENTIAL_CONSTRAINTS,
 
963
  SCH_PROCEDURES,
 
964
  SCH_SCHEMATA,
 
965
  SCH_SCHEMA_PRIVILEGES,
 
966
  SCH_SESSION_STATUS,
 
967
  SCH_SESSION_VARIABLES,
 
968
  SCH_STATISTICS,
 
969
  SCH_STATUS,
 
970
  SCH_TABLES,
 
971
  SCH_TABLE_CONSTRAINTS,
 
972
  SCH_TABLE_NAMES,
 
973
  SCH_TABLE_PRIVILEGES,
 
974
  SCH_TRIGGERS,
 
975
  SCH_USER_PRIVILEGES,
 
976
  SCH_VARIABLES,
 
977
  SCH_VIEWS
 
978
};
 
979
 
 
980
 
 
981
#define MY_I_S_MAYBE_NULL 1
 
982
#define MY_I_S_UNSIGNED   2
 
983
 
 
984
 
 
985
#define SKIP_OPEN_TABLE 0                // do not open table
 
986
#define OPEN_FRM_ONLY   1                // open FRM file only
 
987
#define OPEN_FULL_TABLE 2                // open FRM,MYD, MYI files
 
988
 
 
989
typedef struct st_field_info
 
990
{
 
991
  /** 
 
992
      This is used as column name. 
 
993
  */
 
994
  const char* field_name;
 
995
  /**
 
996
     For string-type columns, this is the maximum number of
 
997
     characters. Otherwise, it is the 'display-length' for the column.
 
998
  */
 
999
  uint field_length;
 
1000
  /**
 
1001
     This denotes data type for the column. For the most part, there seems to
 
1002
     be one entry in the enum for each SQL data type, although there seem to
 
1003
     be a number of additional entries in the enum.
 
1004
  */
 
1005
  enum enum_field_types field_type;
 
1006
  int value;
 
1007
  /**
 
1008
     This is used to set column attributes. By default, columns are @c NOT
 
1009
     @c NULL and @c SIGNED, and you can deviate from the default
 
1010
     by setting the appopriate flags. You can use either one of the flags
 
1011
     @c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
 
1012
     combine them using the bitwise or operator @c |. Both flags are
 
1013
     defined in table.h.
 
1014
   */
 
1015
  uint field_flags;        // Field atributes(maybe_null, signed, unsigned etc.)
 
1016
  const char* old_name;
 
1017
  /**
 
1018
     This should be one of @c SKIP_OPEN_TABLE,
 
1019
     @c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
 
1020
  */
 
1021
  uint open_method;
 
1022
} ST_FIELD_INFO;
 
1023
 
 
1024
 
 
1025
struct TABLE_LIST;
 
1026
typedef class Item COND;
 
1027
 
 
1028
typedef struct st_schema_table
 
1029
{
 
1030
  const char* table_name;
 
1031
  ST_FIELD_INFO *fields_info;
 
1032
  /* Create information_schema table */
 
1033
  TABLE *(*create_table)  (THD *thd, TABLE_LIST *table_list);
 
1034
  /* Fill table with data */
 
1035
  int (*fill_table) (THD *thd, TABLE_LIST *tables, COND *cond);
 
1036
  /* Handle fileds for old SHOW */
 
1037
  int (*old_format) (THD *thd, struct st_schema_table *schema_table);
 
1038
  int (*process_table) (THD *thd, TABLE_LIST *tables, TABLE *table,
 
1039
                        bool res, LEX_STRING *db_name, LEX_STRING *table_name);
 
1040
  int idx_field1, idx_field2; 
 
1041
  bool hidden;
 
1042
  uint i_s_requested_object;  /* the object we need to open(TABLE | VIEW) */
 
1043
} ST_SCHEMA_TABLE;
 
1044
 
 
1045
 
 
1046
#define JOIN_TYPE_LEFT  1
 
1047
#define JOIN_TYPE_RIGHT 2
 
1048
 
 
1049
#define VIEW_ALGORITHM_UNDEFINED        0
 
1050
#define VIEW_ALGORITHM_TMPTABLE         1
 
1051
#define VIEW_ALGORITHM_MERGE            2
 
1052
 
 
1053
#define VIEW_SUID_INVOKER               0
 
1054
#define VIEW_SUID_DEFINER               1
 
1055
#define VIEW_SUID_DEFAULT               2
 
1056
 
 
1057
/* view WITH CHECK OPTION parameter options */
 
1058
#define VIEW_CHECK_NONE       0
 
1059
#define VIEW_CHECK_LOCAL      1
 
1060
#define VIEW_CHECK_CASCADED   2
 
1061
 
 
1062
/* result of view WITH CHECK OPTION parameter check */
 
1063
#define VIEW_CHECK_OK         0
 
1064
#define VIEW_CHECK_ERROR      1
 
1065
#define VIEW_CHECK_SKIP       2
 
1066
 
 
1067
/** The threshold size a blob field buffer before it is freed */
 
1068
#define MAX_TDC_BLOB_SIZE 65536
 
1069
 
 
1070
struct st_lex;
 
1071
class select_union;
 
1072
class TMP_TABLE_PARAM;
 
1073
 
 
1074
Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
 
1075
                        const char *name);
 
1076
 
 
1077
struct Field_translator
 
1078
{
 
1079
  Item *item;
 
1080
  const char *name;
 
1081
};
 
1082
 
 
1083
 
 
1084
/*
 
1085
  Column reference of a NATURAL/USING join. Since column references in
 
1086
  joins can be both from views and stored tables, may point to either a
 
1087
  Field (for tables), or a Field_translator (for views).
 
1088
*/
 
1089
 
 
1090
class Natural_join_column: public Sql_alloc
 
1091
{
 
1092
public:
 
1093
  Field_translator *view_field;  /* Column reference of merge view. */
 
1094
  Item_field       *table_field; /* Column reference of table or temp view. */
 
1095
  TABLE_LIST *table_ref; /* Original base table/view reference. */
 
1096
  /*
 
1097
    True if a common join column of two NATURAL/USING join operands. Notice
 
1098
    that when we have a hierarchy of nested NATURAL/USING joins, a column can
 
1099
    be common at some level of nesting but it may not be common at higher
 
1100
    levels of nesting. Thus this flag may change depending on at which level
 
1101
    we are looking at some column.
 
1102
  */
 
1103
  bool is_common;
 
1104
public:
 
1105
  Natural_join_column(Field_translator *field_param, TABLE_LIST *tab);
 
1106
  Natural_join_column(Item_field *field_param, TABLE_LIST *tab);
 
1107
  const char *name();
 
1108
  Item *create_item(THD *thd);
 
1109
  Field *field();
 
1110
  const char *table_name();
 
1111
  const char *db_name();
 
1112
  GRANT_INFO *grant();
 
1113
};
 
1114
 
 
1115
 
 
1116
/*
 
1117
  Table reference in the FROM clause.
 
1118
 
 
1119
  These table references can be of several types that correspond to
 
1120
  different SQL elements. Below we list all types of TABLE_LISTs with
 
1121
  the necessary conditions to determine when a TABLE_LIST instance
 
1122
  belongs to a certain type.
 
1123
 
 
1124
  1) table (TABLE_LIST::view == NULL)
 
1125
     - base table
 
1126
       (TABLE_LIST::derived == NULL)
 
1127
     - subquery - TABLE_LIST::table is a temp table
 
1128
       (TABLE_LIST::derived != NULL)
 
1129
     - information schema table
 
1130
       (TABLE_LIST::schema_table != NULL)
 
1131
       NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL
 
1132
  2) view (TABLE_LIST::view != NULL)
 
1133
     - merge    (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE)
 
1134
           also (TABLE_LIST::field_translation != NULL)
 
1135
     - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE)
 
1136
           also (TABLE_LIST::field_translation == NULL)
 
1137
  3) nested table reference (TABLE_LIST::nested_join != NULL)
 
1138
     - table sequence - e.g. (t1, t2, t3)
 
1139
       TODO: how to distinguish from a JOIN?
 
1140
     - general JOIN
 
1141
       TODO: how to distinguish from a table sequence?
 
1142
     - NATURAL JOIN
 
1143
       (TABLE_LIST::natural_join != NULL)
 
1144
       - JOIN ... USING
 
1145
         (TABLE_LIST::join_using_fields != NULL)
 
1146
*/
 
1147
 
 
1148
class Index_hint;
 
1149
struct TABLE_LIST
 
1150
{
 
1151
  TABLE_LIST() {}                          /* Remove gcc warning */
 
1152
 
 
1153
  /**
 
1154
    Prepare TABLE_LIST that consists of one table instance to use in
 
1155
    simple_open_and_lock_tables
 
1156
  */
 
1157
  inline void init_one_table(const char *db_name_arg,
 
1158
                             const char *table_name_arg,
 
1159
                             enum thr_lock_type lock_type_arg)
 
1160
  {
 
1161
    bzero((char*) this, sizeof(*this));
 
1162
    db= (char*) db_name_arg;
 
1163
    table_name= alias= (char*) table_name_arg;
 
1164
    lock_type= lock_type_arg;
 
1165
  }
 
1166
 
 
1167
  /*
 
1168
    List of tables local to a subquery (used by SQL_LIST). Considers
 
1169
    views as leaves (unlike 'next_leaf' below). Created at parse time
 
1170
    in st_select_lex::add_table_to_list() -> table_list.link_in_list().
 
1171
  */
 
1172
  TABLE_LIST *next_local;
 
1173
  /* link in a global list of all queries tables */
 
1174
  TABLE_LIST *next_global, **prev_global;
 
1175
  char          *db, *alias, *table_name, *schema_table_name;
 
1176
  char          *option;                /* Used by cache index  */
 
1177
  Item          *on_expr;               /* Used with outer join */
 
1178
  /*
 
1179
    The structure of ON expression presented in the member above
 
1180
    can be changed during certain optimizations. This member
 
1181
    contains a snapshot of AND-OR structure of the ON expression
 
1182
    made after permanent transformations of the parse tree, and is
 
1183
    used to restore ON clause before every reexecution of a prepared
 
1184
    statement or stored procedure.
 
1185
  */
 
1186
  Item          *prep_on_expr;
 
1187
  COND_EQUAL    *cond_equal;            /* Used with outer join */
 
1188
  /*
 
1189
    During parsing - left operand of NATURAL/USING join where 'this' is
 
1190
    the right operand. After parsing (this->natural_join == this) iff
 
1191
    'this' represents a NATURAL or USING join operation. Thus after
 
1192
    parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
 
1193
  */
 
1194
  TABLE_LIST *natural_join;
 
1195
  /*
 
1196
    True if 'this' represents a nested join that is a NATURAL JOIN.
 
1197
    For one of the operands of 'this', the member 'natural_join' points
 
1198
    to the other operand of 'this'.
 
1199
  */
 
1200
  bool is_natural_join;
 
1201
  /* Field names in a USING clause for JOIN ... USING. */
 
1202
  List<String> *join_using_fields;
 
1203
  /*
 
1204
    Explicitly store the result columns of either a NATURAL/USING join or
 
1205
    an operand of such a join.
 
1206
  */
 
1207
  List<Natural_join_column> *join_columns;
 
1208
  /* TRUE if join_columns contains all columns of this table reference. */
 
1209
  bool is_join_columns_complete;
 
1210
 
 
1211
  /*
 
1212
    List of nodes in a nested join tree, that should be considered as
 
1213
    leaves with respect to name resolution. The leaves are: views,
 
1214
    top-most nodes representing NATURAL/USING joins, subqueries, and
 
1215
    base tables. All of these TABLE_LIST instances contain a
 
1216
    materialized list of columns. The list is local to a subquery.
 
1217
  */
 
1218
  TABLE_LIST *next_name_resolution_table;
 
1219
  /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
 
1220
  List<Index_hint> *index_hints;
 
1221
  TABLE        *table;                          /* opened table */
 
1222
  uint          table_id; /* table id (from binlog) for opened table */
 
1223
  /*
 
1224
    select_result for derived table to pass it from table creation to table
 
1225
    filling procedure
 
1226
  */
 
1227
  select_union  *derived_result;
 
1228
  /*
 
1229
    Reference from aux_tables to local list entry of main select of
 
1230
    multi-delete statement:
 
1231
    delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
 
1232
    here it will be reference of first occurrence of t1 to second (as you
 
1233
    can see this lists can't be merged)
 
1234
  */
 
1235
  TABLE_LIST    *correspondent_table;
 
1236
  /**
 
1237
     @brief Normally, this field is non-null for anonymous derived tables only.
 
1238
 
 
1239
     @details This field is set to non-null for 
 
1240
     
 
1241
     - Anonymous derived tables, In this case it points to the SELECT_LEX_UNIT
 
1242
     representing the derived table. E.g. for a query
 
1243
     
 
1244
     @verbatim SELECT * FROM (SELECT a FROM t1) b @endverbatim
 
1245
     
 
1246
     For the @c TABLE_LIST representing the derived table @c b, @c derived
 
1247
     points to the SELECT_LEX_UNIT representing the result of the query within
 
1248
     parenteses.
 
1249
     
 
1250
     - Views. This is set for views with @verbatim ALGORITHM = TEMPTABLE
 
1251
     @endverbatim by mysql_make_view().
 
1252
     
 
1253
     @note Inside views, a subquery in the @c FROM clause is not allowed.
 
1254
     @note Do not use this field to separate views/base tables/anonymous
 
1255
     derived tables. Use TABLE_LIST::is_anonymous_derived_table().
 
1256
  */
 
1257
  st_select_lex_unit *derived;          /* SELECT_LEX_UNIT of derived table */
 
1258
  ST_SCHEMA_TABLE *schema_table;        /* Information_schema table */
 
1259
  st_select_lex *schema_select_lex;
 
1260
  /*
 
1261
    True when the view field translation table is used to convert
 
1262
    schema table fields for backwards compatibility with SHOW command.
 
1263
  */
 
1264
  bool schema_table_reformed;
 
1265
  TMP_TABLE_PARAM *schema_table_param;
 
1266
  /* link to select_lex where this table was used */
 
1267
  st_select_lex *select_lex;
 
1268
  st_lex        *view;                  /* link on VIEW lex for merging */
 
1269
  Field_translator *field_translation;  /* array of VIEW fields */
 
1270
  /* pointer to element after last one in translation table above */
 
1271
  Field_translator *field_translation_end;
 
1272
  /*
 
1273
    List (based on next_local) of underlying tables of this view. I.e. it
 
1274
    does not include the tables of subqueries used in the view. Is set only
 
1275
    for merged views.
 
1276
  */
 
1277
  TABLE_LIST    *merge_underlying_list;
 
1278
  /*
 
1279
    - 0 for base tables
 
1280
    - in case of the view it is the list of all (not only underlying
 
1281
    tables but also used in subquery ones) tables of the view.
 
1282
  */
 
1283
  List<TABLE_LIST> *view_tables;
 
1284
  /* most upper view this table belongs to */
 
1285
  TABLE_LIST    *belong_to_view;
 
1286
  /*
 
1287
    The view directly referencing this table
 
1288
    (non-zero only for merged underlying tables of a view).
 
1289
  */
 
1290
  TABLE_LIST    *referencing_view;
 
1291
  /* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
 
1292
  TABLE_LIST    *parent_l;
 
1293
  /*
 
1294
    Security  context (non-zero only for tables which belong
 
1295
    to view with SQL SECURITY DEFINER)
 
1296
  */
 
1297
  Security_context *security_ctx;
 
1298
  /*
 
1299
    This view security context (non-zero only for views with
 
1300
    SQL SECURITY DEFINER)
 
1301
  */
 
1302
  Security_context *view_sctx;
 
1303
  /*
 
1304
    List of all base tables local to a subquery including all view
 
1305
    tables. Unlike 'next_local', this in this list views are *not*
 
1306
    leaves. Created in setup_tables() -> make_leaves_list().
 
1307
  */
 
1308
  bool allowed_show;
 
1309
  TABLE_LIST    *next_leaf;
 
1310
  Item          *where;                 /* VIEW WHERE clause condition */
 
1311
  Item          *check_option;          /* WITH CHECK OPTION condition */
 
1312
  LEX_STRING    select_stmt;            /* text of (CREATE/SELECT) statement */
 
1313
  LEX_STRING    md5;                    /* md5 of query text */
 
1314
  LEX_STRING    source;                 /* source of CREATE VIEW */
 
1315
  LEX_STRING    view_db;                /* saved view database */
 
1316
  LEX_STRING    view_name;              /* saved view name */
 
1317
  LEX_STRING    timestamp;              /* GMT time stamp of last operation */
 
1318
  st_lex_user   definer;                /* definer of view */
 
1319
  ulonglong     file_version;           /* version of file's field set */
 
1320
  ulonglong     updatable_view;         /* VIEW can be updated */
 
1321
  /** 
 
1322
      @brief The declared algorithm, if this is a view.
 
1323
      @details One of
 
1324
      - VIEW_ALGORITHM_UNDEFINED
 
1325
      - VIEW_ALGORITHM_TMPTABLE
 
1326
      - VIEW_ALGORITHM_MERGE
 
1327
      @to do Replace with an enum 
 
1328
  */
 
1329
  ulonglong     algorithm;
 
1330
  ulonglong     view_suid;              /* view is suid (TRUE dy default) */
 
1331
  ulonglong     with_check;             /* WITH CHECK OPTION */
 
1332
  /*
 
1333
    effective value of WITH CHECK OPTION (differ for temporary table
 
1334
    algorithm)
 
1335
  */
 
1336
  uint8         effective_with_check;
 
1337
  /** 
 
1338
      @brief The view algorithm that is actually used, if this is a view.
 
1339
      @details One of
 
1340
      - VIEW_ALGORITHM_UNDEFINED
 
1341
      - VIEW_ALGORITHM_TMPTABLE
 
1342
      - VIEW_ALGORITHM_MERGE
 
1343
      @to do Replace with an enum 
 
1344
  */
 
1345
  uint8         effective_algorithm;
 
1346
  GRANT_INFO    grant;
 
1347
  /* data need by some engines in query cache*/
 
1348
  ulonglong     engine_data;
 
1349
  /* call back function for asking handler about caching in query cache */
 
1350
  qc_engine_callback callback_func;
 
1351
  thr_lock_type lock_type;
 
1352
  uint          outer_join;             /* Which join type */
 
1353
  uint          shared;                 /* Used in multi-upd */
 
1354
  size_t        db_length;
 
1355
  size_t        table_name_length;
 
1356
  bool          updatable;              /* VIEW/TABLE can be updated now */
 
1357
  bool          straight;               /* optimize with prev table */
 
1358
  bool          updating;               /* for replicate-do/ignore table */
 
1359
  bool          force_index;            /* prefer index over table scan */
 
1360
  bool          ignore_leaves;          /* preload only non-leaf nodes */
 
1361
  table_map     dep_tables;             /* tables the table depends on      */
 
1362
  table_map     on_expr_dep_tables;     /* tables on expression depends on  */
 
1363
  struct st_nested_join *nested_join;   /* if the element is a nested join  */
 
1364
  TABLE_LIST *embedding;             /* nested join containing the table */
 
1365
  List<TABLE_LIST> *join_list;/* join list the table belongs to   */
 
1366
  bool          cacheable_table;        /* stop PS caching */
 
1367
  /* used in multi-upd/views privilege check */
 
1368
  bool          table_in_first_from_clause;
 
1369
  bool          skip_temporary;         /* this table shouldn't be temporary */
 
1370
  /* TRUE if this merged view contain auto_increment field */
 
1371
  bool          contain_auto_increment;
 
1372
  bool          multitable_view;        /* TRUE iff this is multitable view */
 
1373
  bool          compact_view_format;    /* Use compact format for SHOW CREATE VIEW */
 
1374
  /* view where processed */
 
1375
  bool          where_processed;
 
1376
  /* TRUE <=> VIEW CHECK OPTION expression has been processed */
 
1377
  bool          check_option_processed;
 
1378
  /* FRMTYPE_ERROR if any type is acceptable */
 
1379
  enum frm_type_enum required_type;
 
1380
  handlerton    *db_type;               /* table_type for handler */
 
1381
  char          timestamp_buffer[20];   /* buffer for timestamp (19+1) */
 
1382
  /*
 
1383
    This TABLE_LIST object is just placeholder for prelocking, it will be
 
1384
    used for implicit LOCK TABLES only and won't be used in real statement.
 
1385
  */
 
1386
  bool          prelocking_placeholder;
 
1387
  /*
 
1388
    This TABLE_LIST object corresponds to the table to be created
 
1389
    so it is possible that it does not exist (used in CREATE TABLE
 
1390
    ... SELECT implementation).
 
1391
  */
 
1392
  bool          create;
 
1393
  bool          internal_tmp_table;
 
1394
 
 
1395
 
 
1396
  /* View creation context. */
 
1397
 
 
1398
  View_creation_ctx *view_creation_ctx;
 
1399
 
 
1400
  /*
 
1401
    Attributes to save/load view creation context in/from frm-file.
 
1402
 
 
1403
    Ther are required only to be able to use existing parser to load
 
1404
    view-definition file. As soon as the parser parsed the file, view
 
1405
    creation context is initialized and the attributes become redundant.
 
1406
 
 
1407
    These attributes MUST NOT be used for any purposes but the parsing.
 
1408
  */
 
1409
 
 
1410
  LEX_STRING view_client_cs_name;
 
1411
  LEX_STRING view_connection_cl_name;
 
1412
 
 
1413
  /*
 
1414
    View definition (SELECT-statement) in the UTF-form.
 
1415
  */
 
1416
 
 
1417
  LEX_STRING view_body_utf8;
 
1418
 
 
1419
   /* End of view definition context. */
 
1420
 
 
1421
  /**
 
1422
    Indicates what triggers we need to pre-load for this TABLE_LIST
 
1423
    when opening an associated TABLE. This is filled after
 
1424
    the parsed tree is created.
 
1425
  */
 
1426
  uint8 trg_event_map;
 
1427
 
 
1428
  uint i_s_requested_object;
 
1429
  bool has_db_lookup_value;
 
1430
  bool has_table_lookup_value;
 
1431
  uint table_open_method;
 
1432
  enum enum_schema_table_state schema_table_state;
 
1433
  void calc_md5(char *buffer);
 
1434
  void set_underlying_merge();
 
1435
  int view_check_option(THD *thd, bool ignore_failure);
 
1436
  bool setup_underlying(THD *thd);
 
1437
  void cleanup_items();
 
1438
  bool placeholder()
 
1439
  {
 
1440
    return derived || view || schema_table || (create && !table->db_stat) ||
 
1441
           !table;
 
1442
  }
 
1443
  void print(THD *thd, String *str, enum_query_type query_type);
 
1444
  bool check_single_table(TABLE_LIST **table, table_map map,
 
1445
                          TABLE_LIST *view);
 
1446
  bool set_insert_values(MEM_ROOT *mem_root);
 
1447
  void hide_view_error(THD *thd);
 
1448
  TABLE_LIST *find_underlying_table(TABLE *table);
 
1449
  TABLE_LIST *first_leaf_for_name_resolution();
 
1450
  TABLE_LIST *last_leaf_for_name_resolution();
 
1451
  bool is_leaf_for_name_resolution();
 
1452
  inline TABLE_LIST *top_table()
 
1453
    { return belong_to_view ? belong_to_view : this; }
 
1454
  inline bool prepare_check_option(THD *thd)
 
1455
  {
 
1456
    bool res= FALSE;
 
1457
    if (effective_with_check)
 
1458
      res= prep_check_option(thd, effective_with_check);
 
1459
    return res;
 
1460
  }
 
1461
  inline bool prepare_where(THD *thd, Item **conds,
 
1462
                            bool no_where_clause)
 
1463
  {
 
1464
    if (effective_algorithm == VIEW_ALGORITHM_MERGE)
 
1465
      return prep_where(thd, conds, no_where_clause);
 
1466
    return FALSE;
 
1467
  }
 
1468
 
 
1469
  void register_want_access(ulong want_access);
 
1470
  bool prepare_security(THD *thd);
 
1471
#ifndef NO_EMBEDDED_ACCESS_CHECKS
 
1472
  Security_context *find_view_security_context(THD *thd);
 
1473
  bool prepare_view_securety_context(THD *thd);
 
1474
#endif
 
1475
  /*
 
1476
    Cleanup for re-execution in a prepared statement or a stored
 
1477
    procedure.
 
1478
  */
 
1479
  void reinit_before_use(THD *thd);
 
1480
  Item_subselect *containing_subselect();
 
1481
 
 
1482
  /* 
 
1483
    Compiles the tagged hints list and fills up st_table::keys_in_use_for_query,
 
1484
    st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
 
1485
    st_table::force_index and st_table::covering_keys.
 
1486
  */
 
1487
  bool process_index_hints(TABLE *table);
 
1488
 
 
1489
  /* Access MERGE child def version.  See top comment in ha_myisammrg.cc */
 
1490
  inline ulong get_child_def_version()
 
1491
  {
 
1492
    return child_def_version;
 
1493
  }
 
1494
  inline void set_child_def_version(ulong version)
 
1495
  {
 
1496
    child_def_version= version;
 
1497
  }
 
1498
  inline void init_child_def_version()
 
1499
  {
 
1500
    child_def_version= ~0UL;
 
1501
  }
 
1502
 
 
1503
  /**
 
1504
    Compare the version of metadata from the previous execution
 
1505
    (if any) with values obtained from the current table
 
1506
    definition cache element.
 
1507
 
 
1508
    @sa check_and_update_table_version()
 
1509
  */
 
1510
  inline
 
1511
  bool is_table_ref_id_equal(TABLE_SHARE *s) const
 
1512
  {
 
1513
    return (m_table_ref_type == s->get_table_ref_type() &&
 
1514
            m_table_ref_version == s->get_table_ref_version());
 
1515
  }
 
1516
 
 
1517
  /**
 
1518
    Record the value of metadata version of the corresponding
 
1519
    table definition cache element in this parse tree node.
 
1520
 
 
1521
    @sa check_and_update_table_version()
 
1522
  */
 
1523
  inline
 
1524
  void set_table_ref_id(TABLE_SHARE *s)
 
1525
  {
 
1526
    m_table_ref_type= s->get_table_ref_type();
 
1527
    m_table_ref_version= s->get_table_ref_version();
 
1528
  }
 
1529
 
 
1530
  /**
 
1531
     @brief True if this TABLE_LIST represents an anonymous derived table,
 
1532
     i.e.  the result of a subquery.
 
1533
  */
 
1534
  bool is_anonymous_derived_table() const { return derived && !view; }
 
1535
 
 
1536
  /**
 
1537
     @brief Returns the name of the database that the referenced table belongs
 
1538
     to.
 
1539
  */
 
1540
  char *get_db_name() { return view != NULL ? view_db.str : db; }
 
1541
 
 
1542
  /**
 
1543
     @brief Returns the name of the table that this TABLE_LIST represents.
 
1544
 
 
1545
     @details The unqualified table name or view name for a table or view,
 
1546
     respectively.
 
1547
   */
 
1548
  char *get_table_name() { return view != NULL ? view_name.str : table_name; }
 
1549
 
 
1550
private:
 
1551
  bool prep_check_option(THD *thd, uint8 check_opt_type);
 
1552
  bool prep_where(THD *thd, Item **conds, bool no_where_clause);
 
1553
  /*
 
1554
    Cleanup for re-execution in a prepared statement or a stored
 
1555
    procedure.
 
1556
  */
 
1557
 
 
1558
  /* Remembered MERGE child def version.  See top comment in ha_myisammrg.cc */
 
1559
  ulong         child_def_version;
 
1560
  /** See comments for set_metadata_id() */
 
1561
  enum enum_table_ref_type m_table_ref_type;
 
1562
  /** See comments for set_metadata_id() */
 
1563
  ulong m_table_ref_version;
 
1564
};
 
1565
 
 
1566
class Item;
 
1567
 
 
1568
/*
 
1569
  Iterator over the fields of a generic table reference.
 
1570
*/
 
1571
 
 
1572
class Field_iterator: public Sql_alloc
 
1573
{
 
1574
public:
 
1575
  Field_iterator() {}                         /* Remove gcc warning */
 
1576
  virtual ~Field_iterator() {}
 
1577
  virtual void set(TABLE_LIST *)= 0;
 
1578
  virtual void next()= 0;
 
1579
  virtual bool end_of_fields()= 0;              /* Return 1 at end of list */
 
1580
  virtual const char *name()= 0;
 
1581
  virtual Item *create_item(THD *)= 0;
 
1582
  virtual Field *field()= 0;
 
1583
};
 
1584
 
 
1585
 
 
1586
/* 
 
1587
  Iterator over the fields of a base table, view with temporary
 
1588
  table, or subquery.
 
1589
*/
 
1590
 
 
1591
class Field_iterator_table: public Field_iterator
 
1592
{
 
1593
  Field **ptr;
 
1594
public:
 
1595
  Field_iterator_table() :ptr(0) {}
 
1596
  void set(TABLE_LIST *table) { ptr= table->table->field; }
 
1597
  void set_table(TABLE *table) { ptr= table->field; }
 
1598
  void next() { ptr++; }
 
1599
  bool end_of_fields() { return *ptr == 0; }
 
1600
  const char *name();
 
1601
  Item *create_item(THD *thd);
 
1602
  Field *field() { return *ptr; }
 
1603
};
 
1604
 
 
1605
 
 
1606
/* Iterator over the fields of a merge view. */
 
1607
 
 
1608
class Field_iterator_view: public Field_iterator
 
1609
{
 
1610
  Field_translator *ptr, *array_end;
 
1611
  TABLE_LIST *view;
 
1612
public:
 
1613
  Field_iterator_view() :ptr(0), array_end(0) {}
 
1614
  void set(TABLE_LIST *table);
 
1615
  void next() { ptr++; }
 
1616
  bool end_of_fields() { return ptr == array_end; }
 
1617
  const char *name();
 
1618
  Item *create_item(THD *thd);
 
1619
  Item **item_ptr() {return &ptr->item; }
 
1620
  Field *field() { return 0; }
 
1621
  inline Item *item() { return ptr->item; }
 
1622
  Field_translator *field_translator() { return ptr; }
 
1623
};
 
1624
 
 
1625
 
 
1626
/*
 
1627
  Field_iterator interface to the list of materialized fields of a
 
1628
  NATURAL/USING join.
 
1629
*/
 
1630
 
 
1631
class Field_iterator_natural_join: public Field_iterator
 
1632
{
 
1633
  List_iterator_fast<Natural_join_column> column_ref_it;
 
1634
  Natural_join_column *cur_column_ref;
 
1635
public:
 
1636
  Field_iterator_natural_join() :cur_column_ref(NULL) {}
 
1637
  ~Field_iterator_natural_join() {}
 
1638
  void set(TABLE_LIST *table);
 
1639
  void next();
 
1640
  bool end_of_fields() { return !cur_column_ref; }
 
1641
  const char *name() { return cur_column_ref->name(); }
 
1642
  Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
 
1643
  Field *field() { return cur_column_ref->field(); }
 
1644
  Natural_join_column *column_ref() { return cur_column_ref; }
 
1645
};
 
1646
 
 
1647
 
 
1648
/*
 
1649
  Generic iterator over the fields of an arbitrary table reference.
 
1650
 
 
1651
  DESCRIPTION
 
1652
    This class unifies the various ways of iterating over the columns
 
1653
    of a table reference depending on the type of SQL entity it
 
1654
    represents. If such an entity represents a nested table reference,
 
1655
    this iterator encapsulates the iteration over the columns of the
 
1656
    members of the table reference.
 
1657
 
 
1658
  IMPLEMENTATION
 
1659
    The implementation assumes that all underlying NATURAL/USING table
 
1660
    references already contain their result columns and are linked into
 
1661
    the list TABLE_LIST::next_name_resolution_table.
 
1662
*/
 
1663
 
 
1664
class Field_iterator_table_ref: public Field_iterator
 
1665
{
 
1666
  TABLE_LIST *table_ref, *first_leaf, *last_leaf;
 
1667
  Field_iterator_table        table_field_it;
 
1668
  Field_iterator_view         view_field_it;
 
1669
  Field_iterator_natural_join natural_join_it;
 
1670
  Field_iterator *field_it;
 
1671
  void set_field_iterator();
 
1672
public:
 
1673
  Field_iterator_table_ref() :field_it(NULL) {}
 
1674
  void set(TABLE_LIST *table);
 
1675
  void next();
 
1676
  bool end_of_fields()
 
1677
  { return (table_ref == last_leaf && field_it->end_of_fields()); }
 
1678
  const char *name() { return field_it->name(); }
 
1679
  const char *get_table_name();
 
1680
  const char *get_db_name();
 
1681
  GRANT_INFO *grant();
 
1682
  Item *create_item(THD *thd) { return field_it->create_item(thd); }
 
1683
  Field *field() { return field_it->field(); }
 
1684
  Natural_join_column *get_or_create_column_ref(THD *thd, TABLE_LIST *parent_table_ref);
 
1685
  Natural_join_column *get_natural_column_ref();
 
1686
};
 
1687
 
 
1688
 
 
1689
typedef struct st_nested_join
 
1690
{
 
1691
  List<TABLE_LIST>  join_list;       /* list of elements in the nested join */
 
1692
  table_map         used_tables;     /* bitmap of tables in the nested join */
 
1693
  table_map         not_null_tables; /* tables that rejects nulls           */
 
1694
  struct st_join_table *first_nested;/* the first nested table in the plan  */
 
1695
  /* 
 
1696
    Used to count tables in the nested join in 2 isolated places:
 
1697
    1. In make_outerjoin_info(). 
 
1698
    2. check_interleaving_with_nj/restore_prev_nj_state (these are called
 
1699
       by the join optimizer. 
 
1700
    Before each use the counters are zeroed by reset_nj_counters.
 
1701
  */
 
1702
  uint              counter;
 
1703
  nested_join_map   nj_map;          /* Bit used to identify this nested join*/
 
1704
} NESTED_JOIN;
 
1705
 
 
1706
 
 
1707
typedef struct st_changed_table_list
 
1708
{
 
1709
  struct        st_changed_table_list *next;
 
1710
  char          *key;
 
1711
  uint32        key_length;
 
1712
} CHANGED_TABLE_LIST;
 
1713
 
 
1714
 
 
1715
typedef struct st_open_table_list{
 
1716
  struct st_open_table_list *next;
 
1717
  char  *db,*table;
 
1718
  uint32 in_use,locked;
 
1719
} OPEN_TABLE_LIST;
 
1720
 
 
1721
 
 
1722
static inline my_bitmap_map *tmp_use_all_columns(TABLE *table,
 
1723
                                                 MY_BITMAP *bitmap)
 
1724
{
 
1725
  my_bitmap_map *old= bitmap->bitmap;
 
1726
  bitmap->bitmap= table->s->all_set.bitmap;
 
1727
  return old;
 
1728
}
 
1729
 
 
1730
 
 
1731
static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
 
1732
                                          my_bitmap_map *old)
 
1733
{
 
1734
  bitmap->bitmap= old;
 
1735
}
 
1736
 
 
1737
/* The following is only needed for debugging */
 
1738
 
 
1739
static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table,
 
1740
                                                      MY_BITMAP *bitmap)
 
1741
{
 
1742
#ifndef DBUG_OFF
 
1743
  return tmp_use_all_columns(table, bitmap);
 
1744
#else
 
1745
  return 0;
 
1746
#endif
 
1747
}
 
1748
 
 
1749
static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap,
 
1750
                                               my_bitmap_map *old)
 
1751
{
 
1752
#ifndef DBUG_OFF
 
1753
  tmp_restore_column_map(bitmap, old);
 
1754
#endif
 
1755
}
 
1756
 
 
1757
 
 
1758
/* 
 
1759
  Variant of the above : handle both read and write sets.
 
1760
  Provide for the possiblity of the read set being the same as the write set
 
1761
*/
 
1762
static inline void dbug_tmp_use_all_columns(TABLE *table,
 
1763
                                            my_bitmap_map **save,
 
1764
                                            MY_BITMAP *read_set,
 
1765
                                            MY_BITMAP *write_set)
 
1766
{
 
1767
#ifndef DBUG_OFF
 
1768
  save[0]= read_set->bitmap;
 
1769
  save[1]= write_set->bitmap;
 
1770
  (void) tmp_use_all_columns(table, read_set);
 
1771
  (void) tmp_use_all_columns(table, write_set);
 
1772
#endif
 
1773
}
 
1774
 
 
1775
 
 
1776
static inline void dbug_tmp_restore_column_maps(MY_BITMAP *read_set,
 
1777
                                                MY_BITMAP *write_set,
 
1778
                                                my_bitmap_map **old)
 
1779
{
 
1780
#ifndef DBUG_OFF
 
1781
  tmp_restore_column_map(read_set, old[0]);
 
1782
  tmp_restore_column_map(write_set, old[1]);
 
1783
#endif
 
1784
}
 
1785
 
 
1786
 
 
1787
size_t max_row_length(TABLE *table, const uchar *data);
 
1788