~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/table.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
/* Structs that defines the Table */
22
 
 
23
 
#ifndef DRIZZLED_TABLE_H
24
 
#define DRIZZLED_TABLE_H
25
 
 
26
 
#include <string>
27
 
#include <boost/dynamic_bitset.hpp>
28
 
 
29
 
#include "drizzled/order.h"
30
 
#include "drizzled/filesort_info.h"
31
 
#include "drizzled/natural_join_column.h"
32
 
#include "drizzled/field_iterator.h"
33
 
#include "drizzled/cursor.h"
34
 
#include "drizzled/lex_string.h"
35
 
#include "drizzled/table_list.h"
36
 
#include "drizzled/definition/table.h"
37
 
#include "drizzled/atomics.h"
38
 
#include "drizzled/query_id.h"
39
 
 
40
 
namespace drizzled
41
 
{
42
 
 
43
 
class Item;
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
/* Structs that defines the TABLE */
 
18
 
 
19
class Item;                             /* Needed by ORDER */
44
20
class Item_subselect;
45
 
class Select_Lex_Unit;
46
 
class Select_Lex;
 
21
class st_select_lex_unit;
 
22
class st_select_lex;
47
23
class COND_EQUAL;
48
 
class SecurityContext;
49
 
class TableList;
 
24
class Security_context;
 
25
 
 
26
/*************************************************************************/
 
27
 
 
28
/* Order clause list element */
 
29
 
 
30
typedef struct st_order {
 
31
  struct st_order *next;
 
32
  Item   **item;                        /* Point at item in select fields */
 
33
  Item   *item_ptr;                     /* Storage for initial item */
 
34
  Item   **item_copy;                   /* For SPs; the original item ptr */
 
35
  int    counter;                       /* position in SELECT list, correct
 
36
                                           only if counter_used is true*/
 
37
  bool   asc;                           /* true if ascending */
 
38
  bool   free_me;                       /* true if item isn't shared  */
 
39
  bool   in_field_list;                 /* true if in select field list */
 
40
  bool   counter_used;                  /* parameter was counter of columns */
 
41
  Field  *field;                        /* If tmp-table group */
 
42
  char   *buff;                         /* If tmp-table group */
 
43
  table_map used, depend_map;
 
44
} ORDER;
 
45
 
 
46
enum tmp_table_type
 
47
{
 
48
  NO_TMP_TABLE, NON_TRANSACTIONAL_TMP_TABLE, TRANSACTIONAL_TMP_TABLE,
 
49
  INTERNAL_TMP_TABLE, SYSTEM_TMP_TABLE, TMP_TABLE_FRM_FILE_ONLY
 
50
};
 
51
 
 
52
enum frm_type_enum
 
53
{
 
54
  FRMTYPE_ERROR= 0,
 
55
  FRMTYPE_TABLE,
 
56
};
 
57
 
 
58
frm_type_enum mysql_frm_type(THD *thd, char *path, enum legacy_db_type *dbt);
 
59
 
 
60
 
 
61
enum release_type { RELEASE_NORMAL, RELEASE_WAIT_FOR_DROP };
 
62
 
 
63
typedef struct st_filesort_info
 
64
{
 
65
  IO_CACHE *io_cache;           /* If sorted through filesort */
 
66
  uchar     **sort_keys;        /* Buffer for sorting keys */
 
67
  uchar     *buffpek;           /* Buffer for buffpek structures */
 
68
  uint      buffpek_len;        /* Max number of buffpeks in the buffer */
 
69
  uchar     *addon_buf;         /* Pointer to a buffer if sorted with fields */
 
70
  size_t    addon_length;       /* Length of the buffer */
 
71
  struct st_sort_addon_field *addon_field;     /* Pointer to the fields info */
 
72
  void    (*unpack)(struct st_sort_addon_field *, uchar *); /* To unpack back */
 
73
  uchar     *record_pointers;    /* If sorted in memory */
 
74
  ha_rows   found_records;      /* How many records in sort */
 
75
} FILESORT_INFO;
 
76
 
 
77
 
 
78
/*
 
79
  Values in this enum are used to indicate how a tables TIMESTAMP field
 
80
  should be treated. It can be set to the current timestamp on insert or
 
81
  update or both.
 
82
  WARNING: The values are used for bit operations. If you change the
 
83
  enum, you must keep the bitwise relation of the values. For example:
 
84
  (int) TIMESTAMP_AUTO_SET_ON_BOTH must be equal to
 
85
  (int) TIMESTAMP_AUTO_SET_ON_INSERT | (int) TIMESTAMP_AUTO_SET_ON_UPDATE.
 
86
  We use an enum here so that the debugger can display the value names.
 
87
*/
 
88
enum timestamp_auto_set_type
 
89
{
 
90
  TIMESTAMP_NO_AUTO_SET= 0, TIMESTAMP_AUTO_SET_ON_INSERT= 1,
 
91
  TIMESTAMP_AUTO_SET_ON_UPDATE= 2, TIMESTAMP_AUTO_SET_ON_BOTH= 3
 
92
};
 
93
#define clear_timestamp_auto_bits(_target_, _bits_) \
 
94
  (_target_)= (enum timestamp_auto_set_type)((int)(_target_) & ~(int)(_bits_))
 
95
 
50
96
class Field_timestamp;
51
97
class Field_blob;
52
 
 
53
 
extern uint64_t refresh_version;
54
 
 
 
98
class Table_triggers_list;
 
99
 
 
100
/**
 
101
  Category of table found in the table share.
 
102
*/
 
103
enum enum_table_category
 
104
{
 
105
  /**
 
106
    Unknown value.
 
107
  */
 
108
  TABLE_UNKNOWN_CATEGORY=0,
 
109
 
 
110
  /**
 
111
    Temporary table.
 
112
    The table is visible only in the session.
 
113
    Therefore,
 
114
    - FLUSH TABLES WITH READ LOCK
 
115
    - SET GLOBAL READ_ONLY = ON
 
116
    do not apply to this table.
 
117
    Note that LOCK TABLE t FOR READ/WRITE
 
118
    can be used on temporary tables.
 
119
    Temporary tables are not part of the table cache.
 
120
  */
 
121
  TABLE_CATEGORY_TEMPORARY=1,
 
122
 
 
123
  /**
 
124
    User table.
 
125
    These tables do honor:
 
126
    - LOCK TABLE t FOR READ/WRITE
 
127
    - FLUSH TABLES WITH READ LOCK
 
128
    - SET GLOBAL READ_ONLY = ON
 
129
    User tables are cached in the table cache.
 
130
  */
 
131
  TABLE_CATEGORY_USER=2,
 
132
 
 
133
  /**
 
134
    System table, maintained by the server.
 
135
    These tables do honor:
 
136
    - LOCK TABLE t FOR READ/WRITE
 
137
    - FLUSH TABLES WITH READ LOCK
 
138
    - SET GLOBAL READ_ONLY = ON
 
139
    Typically, writes to system tables are performed by
 
140
    the server implementation, not explicitly be a user.
 
141
    System tables are cached in the table cache.
 
142
  */
 
143
  TABLE_CATEGORY_SYSTEM=3,
 
144
 
 
145
  /**
 
146
    Information schema tables.
 
147
    These tables are an interface provided by the system
 
148
    to inspect the system metadata.
 
149
    These tables do *not* honor:
 
150
    - LOCK TABLE t FOR READ/WRITE
 
151
    - FLUSH TABLES WITH READ LOCK
 
152
    - SET GLOBAL READ_ONLY = ON
 
153
    as there is no point in locking explicitely
 
154
    an INFORMATION_SCHEMA table.
 
155
    Nothing is directly written to information schema tables.
 
156
    Note that this value is not used currently,
 
157
    since information schema tables are not shared,
 
158
    but implemented as session specific temporary tables.
 
159
  */
 
160
  /*
 
161
    TODO: Fixing the performance issues of I_S will lead
 
162
    to I_S tables in the table cache, which should use
 
163
    this table type.
 
164
  */
 
165
  TABLE_CATEGORY_INFORMATION=4,
 
166
};
55
167
typedef enum enum_table_category TABLE_CATEGORY;
56
 
typedef struct st_columndef MI_COLUMNDEF;
57
 
 
58
 
/**
59
 
 * Class representing a set of records, either in a temporary, 
60
 
 * normal, or derived table.
61
 
 */
62
 
class Table 
63
 
{
64
 
  Field **field; /**< Pointer to fields collection */
65
 
public:
66
 
 
67
 
  Field **getFields() const
68
 
  {
69
 
    return field;
70
 
  }
71
 
 
72
 
  Field *getField(uint32_t arg) const
73
 
  {
74
 
    return field[arg];
75
 
  }
76
 
 
77
 
  void setFields(Field **arg)
78
 
  {
79
 
    field= arg;
80
 
  }
81
 
 
82
 
  void setFieldAt(Field *arg, uint32_t arg_pos)
83
 
  {
84
 
    field[arg_pos]= arg;
85
 
  }
86
 
 
87
 
  Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
88
 
private:
89
 
  Table *next;
90
 
public:
91
 
  Table *getNext() const
92
 
  {
93
 
    return next;
94
 
  }
95
 
 
96
 
  Table **getNextPtr()
97
 
  {
98
 
    return &next;
99
 
  }
100
 
 
101
 
  void setNext(Table *arg)
102
 
  {
103
 
    next= arg;
104
 
  }
105
 
 
106
 
  void unlink()
107
 
  {
108
 
    getNext()->setPrev(getPrev());              /* remove from used chain */
109
 
    getPrev()->setNext(getNext());
110
 
  }
111
 
 
112
 
private:
113
 
  Table *prev;
114
 
public:
115
 
  Table *getPrev() const
116
 
  {
117
 
    return prev;
118
 
  }
119
 
 
120
 
  Table **getPrevPtr()
121
 
  {
122
 
    return &prev;
123
 
  }
124
 
 
125
 
  void setPrev(Table *arg)
126
 
  {
127
 
    prev= arg;
128
 
  }
129
 
 
130
 
  boost::dynamic_bitset<> *read_set; /* Active column sets */
131
 
  boost::dynamic_bitset<> *write_set; /* Active column sets */
132
 
 
133
 
  uint32_t tablenr;
134
 
  uint32_t db_stat; /**< information about the cursor as in Cursor.h */
135
 
 
136
 
  boost::dynamic_bitset<> def_read_set; /**< Default read set of columns */
137
 
  boost::dynamic_bitset<> def_write_set; /**< Default write set of columns */
138
 
  boost::dynamic_bitset<> tmp_set; /* Not sure about this... */
139
 
 
140
 
  Session *in_use; /**< Pointer to the current session using this object */
141
 
  Session *getSession()
142
 
  {
143
 
    return in_use;
144
 
  }
145
 
 
146
 
  unsigned char *getInsertRecord()
147
 
  {
148
 
    return record[0];
149
 
  }
150
 
 
151
 
  unsigned char *getUpdateRecord()
152
 
  {
153
 
    return record[1];
154
 
  }
155
 
 
156
 
  unsigned char *record[2]; /**< Pointer to "records" */
157
 
  std::vector<unsigned char> insert_values; /* used by INSERT ... UPDATE */
158
 
  KeyInfo  *key_info; /**< data of keys in database */
159
 
  Field *next_number_field; /**< Set if next_number is activated. @TODO What the heck is the difference between this and the next member? */
160
 
  Field *found_next_number_field; /**< Points to the "next-number" field (autoincrement field) */
161
 
  Field_timestamp *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
162
 
 
163
 
  TableList *pos_in_table_list; /* Element referring to this table */
164
 
  Order *group;
165
 
  
166
 
  const char *getAlias() const
167
 
  {
168
 
    return _alias.c_str();
169
 
  }
170
 
 
171
 
  void clearAlias()
172
 
  {
173
 
    _alias.clear();
174
 
  }
175
 
 
176
 
  void setAlias(const char *arg)
177
 
  {
178
 
    _alias= arg;
179
 
  }
180
 
 
181
 
private:
182
 
  std::string _alias; /**< alias or table name if no alias */
183
 
public:
184
 
 
185
 
  unsigned char *null_flags;
186
 
 
187
 
  uint32_t lock_position; /**< Position in DRIZZLE_LOCK.table */
188
 
  uint32_t lock_data_start; /**< Start pos. in DRIZZLE_LOCK.locks */
189
 
  uint32_t lock_count; /**< Number of locks */
190
 
  uint32_t used_fields;
191
 
  uint32_t status; /* What's in getInsertRecord() */
 
168
 
 
169
TABLE_CATEGORY get_table_category(const LEX_STRING *db,
 
170
                                  const LEX_STRING *name);
 
171
 
 
172
/*
 
173
  This structure is shared between different table objects. There is one
 
174
  instance of table share per one table in the database.
 
175
*/
 
176
 
 
177
typedef struct st_table_share
 
178
{
 
179
  st_table_share() {}                    /* Remove gcc warning */
 
180
 
 
181
  /** Category of this table. */
 
182
  TABLE_CATEGORY table_category;
 
183
 
 
184
  /* hash of field names (contains pointers to elements of field array) */
 
185
  HASH  name_hash;                      /* hash of field names */
 
186
  MEM_ROOT mem_root;
 
187
  TYPELIB keynames;                     /* Pointers to keynames */
 
188
  TYPELIB fieldnames;                   /* Pointer to fieldnames */
 
189
  TYPELIB *intervals;                   /* pointer to interval info */
 
190
  pthread_mutex_t mutex;                /* For locking the share  */
 
191
  pthread_cond_t cond;                  /* To signal that share is ready */
 
192
  struct st_table_share *next,          /* Link to unused shares */
 
193
    **prev;
 
194
 
 
195
  /* The following is copied to each TABLE on OPEN */
 
196
  Field **field;
 
197
  Field **found_next_number_field;
 
198
  Field *timestamp_field;               /* Used only during open */
 
199
  KEY  *key_info;                       /* data of keys in database */
 
200
  uint  *blob_field;                    /* Index to blobs in Field arrray*/
 
201
 
 
202
  uchar *default_values;                /* row with default values */
 
203
  LEX_STRING comment;                   /* Comment about table */
 
204
  CHARSET_INFO *table_charset;          /* Default charset of string fields */
 
205
 
 
206
  MY_BITMAP all_set;
 
207
  /*
 
208
    Key which is used for looking-up table in table cache and in the list
 
209
    of thread's temporary tables. Has the form of:
 
210
      "database_name\0table_name\0" + optional part for temporary tables.
 
211
 
 
212
    Note that all three 'table_cache_key', 'db' and 'table_name' members
 
213
    must be set (and be non-zero) for tables in table cache. They also
 
214
    should correspond to each other.
 
215
    To ensure this one can use set_table_cache() methods.
 
216
  */
 
217
  LEX_STRING table_cache_key;
 
218
  LEX_STRING db;                        /* Pointer to db */
 
219
  LEX_STRING table_name;                /* Table name (for open) */
 
220
  LEX_STRING path;                      /* Path to .frm file (from datadir) */
 
221
  LEX_STRING normalized_path;           /* unpack_filename(path) */
 
222
  LEX_STRING connect_string;
 
223
 
 
224
  /* 
 
225
     Set of keys in use, implemented as a Bitmap.
 
226
     Excludes keys disabled by ALTER TABLE ... DISABLE KEYS.
 
227
  */
 
228
  key_map keys_in_use;
 
229
  key_map keys_for_keyread;
 
230
  ha_rows min_rows, max_rows;           /* create information */
 
231
  ulong   avg_row_length;               /* create information */
 
232
  ulong   version, mysql_version;
 
233
  ulong   timestamp_offset;             /* Set to offset+1 of record */
 
234
  ulong   reclength;                    /* Recordlength */
 
235
 
 
236
  plugin_ref db_plugin;                 /* storage engine plugin */
 
237
  inline handlerton *db_type() const    /* table_type for handler */
 
238
  { 
 
239
    // DBUG_ASSERT(db_plugin);
 
240
    return db_plugin ? plugin_data(db_plugin, handlerton*) : NULL;
 
241
  }
 
242
  enum row_type row_type;               /* How rows are stored */
 
243
  enum ha_storage_media default_storage_media;
 
244
  char *tablespace;
 
245
  enum tmp_table_type tmp_table;
 
246
  enum ha_choice transactional;
 
247
  enum ha_choice page_checksum;
 
248
 
 
249
  uint ref_count;                       /* How many TABLE objects uses this */
 
250
  uint open_count;                      /* Number of tables in open list */
 
251
  uint blob_ptr_size;                   /* 4 or 8 */
 
252
  uint key_block_size;                  /* create key_block_size, if used */
 
253
  uint null_bytes, last_null_bit_pos;
 
254
  uint fields;                          /* Number of fields */
 
255
  uint rec_buff_length;                 /* Size of table->record[] buffer */
 
256
  uint keys, key_parts;
 
257
  uint max_key_length, max_unique_length, total_key_length;
 
258
  uint uniques;                         /* Number of UNIQUE index */
 
259
  uint null_fields;                     /* number of null fields */
 
260
  uint blob_fields;                     /* number of blob fields */
 
261
  uint timestamp_field_offset;          /* Field number for timestamp field */
 
262
  uint varchar_fields;                  /* number of varchar fields */
 
263
  uint db_create_options;               /* Create options from database */
 
264
  uint db_options_in_use;               /* Options in use */
 
265
  uint db_record_offset;                /* if HA_REC_IN_SEQ */
 
266
  uint raid_type, raid_chunks;
 
267
  uint rowid_field_offset;              /* Field_nr +1 to rowid field */
 
268
  /* Index of auto-updated TIMESTAMP field in field array */
 
269
  uint primary_key;
 
270
  uint next_number_index;               /* autoincrement key number */
 
271
  uint next_number_key_offset;          /* autoinc keypart offset in a key */
 
272
  uint next_number_keypart;             /* autoinc keypart number in a key */
 
273
  uint error, open_errno, errarg;       /* error from open_table_def() */
 
274
  uint column_bitmap_size;
 
275
  uchar frm_version;
 
276
  bool null_field_first;
 
277
  bool system;                          /* Set if system table (one record) */
 
278
  bool crypted;                         /* If .frm file is crypted */
 
279
  bool db_low_byte_first;               /* Portable row format */
 
280
  bool crashed;
 
281
  bool name_lock, replace_with_name_lock;
 
282
  bool waiting_on_cond;                 /* Protection against free */
 
283
  ulong table_map_id;                   /* for row-based replication */
 
284
  uint64_t table_map_version;
 
285
 
 
286
  /*
 
287
    Cache for row-based replication table share checks that does not
 
288
    need to be repeated. Possible values are: -1 when cache value is
 
289
    not calculated yet, 0 when table *shall not* be replicated, 1 when
 
290
    table *may* be replicated.
 
291
  */
 
292
  int cached_row_logging_check;
 
293
 
 
294
  /*
 
295
    Set share's table cache key and update its db and table name appropriately.
 
296
 
 
297
    SYNOPSIS
 
298
      set_table_cache_key()
 
299
        key_buff    Buffer with already built table cache key to be
 
300
                    referenced from share.
 
301
        key_length  Key length.
 
302
 
 
303
    NOTES
 
304
      Since 'key_buff' buffer will be referenced from share it should has same
 
305
      life-time as share itself.
 
306
      This method automatically ensures that TABLE_SHARE::table_name/db have
 
307
      appropriate values by using table cache key as their source.
 
308
  */
 
309
 
 
310
  void set_table_cache_key(char *key_buff, uint key_length)
 
311
  {
 
312
    table_cache_key.str= key_buff;
 
313
    table_cache_key.length= key_length;
 
314
    /*
 
315
      Let us use the fact that the key is "db/0/table_name/0" + optional
 
316
      part for temporary tables.
 
317
    */
 
318
    db.str=            table_cache_key.str;
 
319
    db.length=         strlen(db.str);
 
320
    table_name.str=    db.str + db.length + 1;
 
321
    table_name.length= strlen(table_name.str);
 
322
  }
 
323
 
 
324
 
 
325
  /*
 
326
    Set share's table cache key and update its db and table name appropriately.
 
327
 
 
328
    SYNOPSIS
 
329
      set_table_cache_key()
 
330
        key_buff    Buffer to be used as storage for table cache key
 
331
                    (should be at least key_length bytes).
 
332
        key         Value for table cache key.
 
333
        key_length  Key length.
 
334
 
 
335
    NOTE
 
336
      Since 'key_buff' buffer will be used as storage for table cache key
 
337
      it should has same life-time as share itself.
 
338
  */
 
339
 
 
340
  void set_table_cache_key(char *key_buff, const char *key, uint key_length)
 
341
  {
 
342
    memcpy(key_buff, key, key_length);
 
343
    set_table_cache_key(key_buff, key_length);
 
344
  }
 
345
 
 
346
  inline bool honor_global_locks()
 
347
  {
 
348
    return ((table_category == TABLE_CATEGORY_USER)
 
349
            || (table_category == TABLE_CATEGORY_SYSTEM));
 
350
  }
 
351
 
 
352
  inline ulong get_table_def_version()
 
353
  {
 
354
    return table_map_id;
 
355
  }
 
356
 
 
357
} TABLE_SHARE;
 
358
 
 
359
 
 
360
extern ulong refresh_version;
 
361
 
 
362
/* Information for one open table */
 
363
enum index_hint_type
 
364
{
 
365
  INDEX_HINT_IGNORE,
 
366
  INDEX_HINT_USE,
 
367
  INDEX_HINT_FORCE
 
368
};
 
369
 
 
370
struct st_table {
 
371
  st_table() {}                               /* Remove gcc warning */
 
372
 
 
373
  TABLE_SHARE   *s;
 
374
  handler       *file;
 
375
  struct st_table *next, *prev;
 
376
 
 
377
  THD   *in_use;                        /* Which thread uses this */
 
378
  Field **field;                        /* Pointer to fields */
 
379
 
 
380
  uchar *record[2];                     /* Pointer to records */
 
381
  uchar *write_row_record;              /* Used as optimisation in
 
382
                                           THD::write_row */
 
383
  uchar *insert_values;                  /* used by INSERT ... UPDATE */
 
384
  /* 
 
385
    Map of keys that can be used to retrieve all data from this table 
 
386
    needed by the query without reading the row.
 
387
  */
 
388
  key_map covering_keys;
 
389
  key_map quick_keys, merge_keys;
 
390
  /*
 
391
    A set of keys that can be used in the query that references this
 
392
    table.
 
393
 
 
394
    All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be 
 
395
    subtracted from this set upon instantiation. Thus for any TABLE t it holds
 
396
    that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we 
 
397
    must not introduce any new keys here (see setup_tables).
 
398
 
 
399
    The set is implemented as a bitmap.
 
400
  */
 
401
  key_map keys_in_use_for_query;
 
402
  /* Map of keys that can be used to calculate GROUP BY without sorting */
 
403
  key_map keys_in_use_for_group_by;
 
404
  /* Map of keys that can be used to calculate ORDER BY without sorting */
 
405
  key_map keys_in_use_for_order_by;
 
406
  KEY  *key_info;                       /* data of keys in database */
 
407
 
 
408
  Field *next_number_field;             /* Set if next_number is activated */
 
409
  Field *found_next_number_field;       /* Set on open */
 
410
  Field_timestamp *timestamp_field;
 
411
 
 
412
  /* Table's triggers, 0 if there are no of them */
 
413
  Table_triggers_list *triggers;
 
414
  TABLE_LIST *pos_in_table_list;/* Element referring to this table */
 
415
  ORDER         *group;
 
416
  const char    *alias;                   /* alias or table name */
 
417
  uchar         *null_flags;
 
418
  my_bitmap_map *bitmap_init_value;
 
419
  MY_BITMAP     def_read_set, def_write_set, tmp_set; /* containers */
 
420
  MY_BITMAP     *read_set, *write_set;          /* Active column sets */
 
421
  /*
 
422
   The ID of the query that opened and is using this table. Has different
 
423
   meanings depending on the table type.
 
424
 
 
425
   Temporary tables:
 
426
 
 
427
   table->query_id is set to thd->query_id for the duration of a statement
 
428
   and is reset to 0 once it is closed by the same statement. A non-zero
 
429
   table->query_id means that a statement is using the table even if it's
 
430
   not the current statement (table is in use by some outer statement).
 
431
 
 
432
   Non-temporary tables:
 
433
 
 
434
   Under pre-locked or LOCK TABLES mode: query_id is set to thd->query_id
 
435
   for the duration of a statement and is reset to 0 once it is closed by
 
436
   the same statement. A non-zero query_id is used to control which tables
 
437
   in the list of pre-opened and locked tables are actually being used.
 
438
  */
 
439
  query_id_t    query_id;
 
440
 
 
441
  /* 
 
442
    For each key that has quick_keys.is_set(key) == TRUE: estimate of #records
 
443
    and max #key parts that range access would use.
 
444
  */
 
445
  ha_rows       quick_rows[MAX_KEY];
 
446
 
 
447
  /* Bitmaps of key parts that =const for the entire join. */
 
448
  key_part_map  const_key_parts[MAX_KEY];
 
449
 
 
450
  uint          quick_key_parts[MAX_KEY];
 
451
  uint          quick_n_ranges[MAX_KEY];
 
452
 
 
453
  /* 
 
454
    Estimate of number of records that satisfy SARGable part of the table
 
455
    condition, or table->file->records if no SARGable condition could be
 
456
    constructed.
 
457
    This value is used by join optimizer as an estimate of number of records
 
458
    that will pass the table condition (condition that depends on fields of 
 
459
    this table and constants)
 
460
  */
 
461
  ha_rows       quick_condition_rows;
 
462
 
 
463
  /*
 
464
    If this table has TIMESTAMP field with auto-set property (pointed by
 
465
    timestamp_field member) then this variable indicates during which
 
466
    operations (insert only/on update/in both cases) we should set this
 
467
    field to current timestamp. If there are no such field in this table
 
468
    or we should not automatically set its value during execution of current
 
469
    statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
 
470
 
 
471
    Value of this variable is set for each statement in open_table() and
 
472
    if needed cleared later in statement processing code (see mysql_update()
 
473
    as example).
 
474
  */
 
475
  timestamp_auto_set_type timestamp_field_type;
 
476
  table_map     map;                    /* ID bit of table (1,2,4,8,16...) */
 
477
 
 
478
  uint          lock_position;          /* Position in MYSQL_LOCK.table */
 
479
  uint          lock_data_start;        /* Start pos. in MYSQL_LOCK.locks */
 
480
  uint          lock_count;             /* Number of locks */
 
481
  uint          tablenr,used_fields;
 
482
  uint          temp_pool_slot;         /* Used by intern temp tables */
 
483
  uint          status;                 /* What's in record[0] */
 
484
  uint          db_stat;                /* mode of file as in handler.h */
192
485
  /* number of select if it is derived table */
193
 
  uint32_t derived_select_number;
194
 
  int current_lock; /**< Type of lock on table */
195
 
  bool copy_blobs; /**< Should blobs by copied when storing? */
 
486
  uint          derived_select_number;
 
487
  int           current_lock;           /* Type of lock on table */
 
488
  my_bool copy_blobs;                   /* copy_blobs when storing */
196
489
 
197
490
  /*
198
491
    0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
199
492
    If maybe_null !=0, this table is inner w.r.t. some outer join operation,
200
493
    and null_row may be true.
201
494
  */
202
 
  bool maybe_null;
203
 
 
 
495
  uint maybe_null;
204
496
  /*
205
 
    If true, the current table row is considered to have all columns set to
 
497
    If true, the current table row is considered to have all columns set to 
206
498
    NULL, including columns declared as "not null" (see maybe_null).
207
499
  */
208
 
  bool null_row;
 
500
  my_bool null_row;
209
501
 
210
 
  bool force_index;
211
 
  bool distinct;
212
 
  bool const_table;
213
 
  bool no_rows;
214
 
  bool key_read;
215
 
  bool no_keyread;
 
502
  /*
 
503
    TODO: Each of the following flags take up 8 bits. They can just as easily
 
504
    be put into one single unsigned long and instead of taking up 18
 
505
    bytes, it would take up 4.
 
506
  */
 
507
  my_bool force_index;
 
508
  my_bool distinct,const_table,no_rows;
 
509
  my_bool key_read, no_keyread;
216
510
  /*
217
511
    Placeholder for an open table which prevents other connections
218
512
    from taking name-locks on this table. Typically used with
219
 
    TableShare::version member to take an exclusive name-lock on
 
513
    TABLE_SHARE::version member to take an exclusive name-lock on
220
514
    this table name -- a name lock that not only prevents other
221
515
    threads from opening the table, but also blocks other name
222
516
    locks. This is achieved by:
226
520
    - setting version to 0 - this will force other threads to close
227
521
      the instance of this table and wait (this is the same approach
228
522
      as used for usual name locks).
229
 
    An exclusively name-locked table currently can have no Cursor
 
523
    An exclusively name-locked table currently can have no handler
230
524
    object associated with it (db_stat is always 0), but please do
231
525
    not rely on that.
232
526
  */
233
 
  bool open_placeholder;
234
 
  bool locked_by_name;
235
 
  bool no_cache;
 
527
  my_bool open_placeholder;
 
528
  my_bool locked_by_logger;
 
529
  my_bool no_replicate;
 
530
  my_bool locked_by_name;
 
531
  my_bool fulltext_searched;
 
532
  my_bool no_cache;
 
533
  /* To signal that the table is associated with a HANDLER statement */
 
534
  my_bool open_by_handler;
236
535
  /*
237
536
    To indicate that a non-null value of the auto_increment field
238
537
    was provided by the user or retrieved from the current record.
239
538
    Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
240
539
  */
241
 
  bool auto_increment_field_not_null;
242
 
  bool alias_name_used; /* true if table_name is alias */
243
 
 
244
 
  /*
245
 
   The ID of the query that opened and is using this table. Has different
246
 
   meanings depending on the table type.
247
 
 
248
 
   Temporary tables:
249
 
 
250
 
   table->query_id is set to session->query_id for the duration of a statement
251
 
   and is reset to 0 once it is closed by the same statement. A non-zero
252
 
   table->query_id means that a statement is using the table even if it's
253
 
   not the current statement (table is in use by some outer statement).
254
 
 
255
 
   Non-temporary tables:
256
 
 
257
 
   Under pre-locked or LOCK TABLES mode: query_id is set to session->query_id
258
 
   for the duration of a statement and is reset to 0 once it is closed by
259
 
   the same statement. A non-zero query_id is used to control which tables
260
 
   in the list of pre-opened and locked tables are actually being used.
261
 
  */
262
 
  query_id_t query_id;
263
 
 
264
 
  /**
265
 
   * Estimate of number of records that satisfy SARGable part of the table
266
 
   * condition, or table->cursor->records if no SARGable condition could be
267
 
   * constructed.
268
 
   * This value is used by join optimizer as an estimate of number of records
269
 
   * that will pass the table condition (condition that depends on fields of
270
 
   * this table and constants)
271
 
   */
272
 
  ha_rows quick_condition_rows;
273
 
 
274
 
  /*
275
 
    If this table has TIMESTAMP field with auto-set property (pointed by
276
 
    timestamp_field member) then this variable indicates during which
277
 
    operations (insert only/on update/in both cases) we should set this
278
 
    field to current timestamp. If there are no such field in this table
279
 
    or we should not automatically set its value during execution of current
280
 
    statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
281
 
 
282
 
    Value of this variable is set for each statement in open_table() and
283
 
    if needed cleared later in statement processing code (see mysql_update()
284
 
    as example).
285
 
  */
286
 
  timestamp_auto_set_type timestamp_field_type;
287
 
  table_map map; ///< ID bit of table (1,2,4,8,16...)
288
 
 
289
 
  RegInfo reginfo; /* field connections */
290
 
 
291
 
  /*
292
 
    Map of keys that can be used to retrieve all data from this table
293
 
    needed by the query without reading the row.
294
 
  */
295
 
  key_map covering_keys;
296
 
  key_map quick_keys;
297
 
  key_map merge_keys;
298
 
 
299
 
  /*
300
 
    A set of keys that can be used in the query that references this
301
 
    table.
302
 
 
303
 
    All indexes disabled on the table's TableShare (see Table::s) will be
304
 
    subtracted from this set upon instantiation. Thus for any Table t it holds
305
 
    that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
306
 
    must not introduce any new keys here (see setup_tables).
307
 
 
308
 
    The set is implemented as a bitmap.
309
 
  */
310
 
  key_map keys_in_use_for_query;
311
 
  /* Map of keys that can be used to calculate GROUP BY without sorting */
312
 
  key_map keys_in_use_for_group_by;
313
 
  /* Map of keys that can be used to calculate ORDER BY without sorting */
314
 
  key_map keys_in_use_for_order_by;
315
 
 
316
 
  /*
317
 
    For each key that has quick_keys.test(key) == true: estimate of #records
318
 
    and max #key parts that range access would use.
319
 
  */
320
 
  ha_rows quick_rows[MAX_KEY];
321
 
 
322
 
  /* Bitmaps of key parts that =const for the entire join. */
323
 
  key_part_map  const_key_parts[MAX_KEY];
324
 
 
325
 
  uint32_t quick_key_parts[MAX_KEY];
326
 
  uint32_t quick_n_ranges[MAX_KEY];
327
 
 
328
 
private:
329
 
  memory::Root mem_root;
330
 
 
331
 
  void init_mem_root()
332
 
  {
333
 
    init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
334
 
  }
335
 
public:
336
 
  memory::Root *getMemRoot()
337
 
  {
338
 
    if (not mem_root.alloc_root_inited())
339
 
    {
340
 
      init_mem_root();
341
 
    }
342
 
 
343
 
    return &mem_root;
344
 
  }
345
 
 
346
 
  void *alloc_root(size_t arg)
347
 
  {
348
 
    if (not mem_root.alloc_root_inited())
349
 
    {
350
 
      init_mem_root();
351
 
    }
352
 
 
353
 
    return mem_root.alloc_root(arg);
354
 
  }
355
 
 
356
 
  char *strmake_root(const char *str_arg, size_t len_arg)
357
 
  {
358
 
    if (not mem_root.alloc_root_inited())
359
 
    {
360
 
      init_mem_root();
361
 
    }
362
 
 
363
 
    return mem_root.strmake_root(str_arg, len_arg);
364
 
  }
365
 
 
366
 
  filesort_info sort;
367
 
 
368
 
  Table();
369
 
  virtual ~Table();
370
 
 
371
 
  int report_error(int error);
372
 
  /**
373
 
   * Free information allocated by openfrm
374
 
   *
375
 
   * @param If true if we also want to free table_share
376
 
   * @note this should all be the destructor
377
 
   */
378
 
  int delete_table(bool free_share= false);
379
 
 
380
 
  void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
381
 
 
382
 
  /* SHARE methods */
383
 
  virtual const TableShare *getShare() const= 0; /* Get rid of this long term */
384
 
  virtual TableShare *getMutableShare()= 0; /* Get rid of this long term */
385
 
  virtual bool hasShare() const= 0; /* Get rid of this long term */
386
 
  virtual void setShare(TableShare *new_share)= 0; /* Get rid of this long term */
387
 
 
388
 
  virtual void release(void)= 0;
389
 
 
390
 
  uint32_t sizeKeys() { return getMutableShare()->sizeKeys(); }
391
 
  uint32_t sizeFields() { return getMutableShare()->sizeFields(); }
392
 
  uint32_t getRecordLength() const { return getShare()->getRecordLength(); }
393
 
  uint32_t sizeBlobFields() { return getMutableShare()->blob_fields; }
394
 
  uint32_t *getBlobField() { return &getMutableShare()->blob_field[0]; }
395
 
 
396
 
public:
397
 
  virtual bool hasVariableWidth() const
398
 
  {
399
 
    return getShare()->hasVariableWidth(); // We should calculate this.
400
 
  }
401
 
 
402
 
  virtual void setVariableWidth(void);
403
 
 
404
 
  Field_blob *getBlobFieldAt(uint32_t arg) const
405
 
  {
406
 
    if (arg < getShare()->blob_fields)
407
 
      return (Field_blob*) field[getShare()->blob_field[arg]]; /*NOTE: Using 'Table.field' NOT SharedTable.field. */
408
 
 
409
 
    return NULL;
410
 
  }
411
 
  inline uint8_t getBlobPtrSize() { return getShare()->blob_ptr_size; }
412
 
  inline uint32_t getNullBytes() { return getShare()->null_bytes; }
413
 
  inline uint32_t getNullFields() { return getShare()->null_fields; }
414
 
  inline unsigned char *getDefaultValues() { return  getMutableShare()->getDefaultValues(); }
415
 
  inline const char *getSchemaName()  const { return getShare()->getSchemaName(); }
416
 
  inline const char *getTableName()  const { return getShare()->getTableName(); }
417
 
 
418
 
  inline bool isDatabaseLowByteFirst() { return getShare()->db_low_byte_first; } /* Portable row format */
419
 
  inline bool isNameLock() const { return open_placeholder; }
420
 
 
421
 
  uint32_t index_flags(uint32_t idx) const
422
 
  {
423
 
    return getShare()->storage_engine->index_flags(getShare()->getKeyInfo(idx).algorithm);
424
 
  }
425
 
 
426
 
  inline plugin::StorageEngine *getEngine() const   /* table_type for handler */
427
 
  {
428
 
    return getShare()->storage_engine;
429
 
  }
430
 
 
431
 
  Cursor &getCursor() const /* table_type for handler */
432
 
  {
433
 
    assert(cursor);
434
 
    return *cursor;
435
 
  }
436
 
 
437
 
  size_t max_row_length(const unsigned char *data);
438
 
  uint32_t find_shortest_key(const key_map *usable_keys);
439
 
  bool compare_record(Field **ptr);
440
 
  bool records_are_comparable();
441
 
  bool compare_records();
442
 
  /* TODO: the (re)storeRecord's may be able to be further condensed */
443
 
  void storeRecord();
444
 
  void storeRecordAsInsert();
445
 
  void storeRecordAsDefault();
446
 
  void restoreRecord();
447
 
  void restoreRecordAsDefault();
448
 
  void emptyRecord();
449
 
 
450
 
 
451
 
  /* See if this can be blown away */
452
 
  inline uint32_t getDBStat () { return db_stat; }
453
 
  inline uint32_t setDBStat () { return db_stat; }
454
 
  /**
455
 
   * Create Item_field for each column in the table.
456
 
   *
457
 
   * @param[out] a pointer to an empty list used to store items
458
 
   *
459
 
   * @details
460
 
   *
461
 
   * Create Item_field object for each column in the table and
462
 
   * initialize it with the corresponding Field. New items are
463
 
   * created in the current Session memory root.
464
 
   *
465
 
   * @retval
466
 
   *  false on success
467
 
   * @retval
468
 
   *  true when out of memory
469
 
   */
 
540
  my_bool auto_increment_field_not_null;
 
541
  my_bool insert_or_update;             /* Can be used by the handler */
 
542
  my_bool alias_name_used;              /* true if table_name is alias */
 
543
  my_bool get_fields_in_item_tree;      /* Signal to fix_field */
 
544
 
 
545
  REGINFO reginfo;                      /* field connections */
 
546
  MEM_ROOT mem_root;
 
547
  FILESORT_INFO sort;
 
548
 
470
549
  bool fill_item_list(List<Item> *item_list) const;
 
550
  void reset_item_list(List<Item> *item_list) const;
471
551
  void clear_column_bitmaps(void);
472
552
  void prepare_for_position(void);
473
 
  void mark_columns_used_by_index_no_reset(uint32_t index, boost::dynamic_bitset<>& bitmap);
474
 
  void mark_columns_used_by_index_no_reset(uint32_t index);
475
 
  void mark_columns_used_by_index(uint32_t index);
 
553
  void mark_columns_used_by_index_no_reset(uint index, MY_BITMAP *map);
 
554
  void mark_columns_used_by_index(uint index);
476
555
  void restore_column_maps_after_mark_index();
477
556
  void mark_auto_increment_column(void);
478
557
  void mark_columns_needed_for_update(void);
479
558
  void mark_columns_needed_for_delete(void);
480
559
  void mark_columns_needed_for_insert(void);
481
 
  void column_bitmaps_set(boost::dynamic_bitset<>& read_set_arg,
482
 
                          boost::dynamic_bitset<>& write_set_arg);
483
 
 
484
 
  void restore_column_map(const boost::dynamic_bitset<>& old);
485
 
 
486
 
  const boost::dynamic_bitset<> use_all_columns(boost::dynamic_bitset<>& map);
 
560
  inline void column_bitmaps_set(MY_BITMAP *read_set_arg,
 
561
                                 MY_BITMAP *write_set_arg)
 
562
  {
 
563
    read_set= read_set_arg;
 
564
    write_set= write_set_arg;
 
565
    if (file)
 
566
      file->column_bitmaps_signal();
 
567
  }
 
568
  inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg,
 
569
                                           MY_BITMAP *write_set_arg)
 
570
  {
 
571
    read_set= read_set_arg;
 
572
    write_set= write_set_arg;
 
573
  }
487
574
  inline void use_all_columns()
488
575
  {
489
 
    column_bitmaps_set(getMutableShare()->all_set, getMutableShare()->all_set);
 
576
    column_bitmaps_set(&s->all_set, &s->all_set);
490
577
  }
491
 
 
492
578
  inline void default_column_bitmaps()
493
579
  {
494
580
    read_set= &def_read_set;
495
581
    write_set= &def_write_set;
496
582
  }
497
 
 
498
 
  /* Both of the below should go away once we can move this bit to the field objects */
499
 
  inline bool isReadSet(uint32_t index)
500
 
  {
501
 
    return read_set->test(index);
502
 
  }
503
 
 
504
 
  inline void setReadSet(uint32_t index)
505
 
  {
506
 
    read_set->set(index);
507
 
  }
508
 
 
509
 
  inline void setReadSet()
510
 
  {
511
 
    read_set->set();
512
 
  }
513
 
 
514
 
  inline void clearReadSet(uint32_t index)
515
 
  {
516
 
    read_set->reset(index);
517
 
  }
518
 
 
519
 
  inline void clearReadSet()
520
 
  {
521
 
    read_set->reset();
522
 
  }
523
 
 
524
 
  inline bool isWriteSet(uint32_t index)
525
 
  {
526
 
    return write_set->test(index);
527
 
  }
528
 
 
529
 
  inline void setWriteSet(uint32_t index)
530
 
  {
531
 
    write_set->set(index);
532
 
  }
533
 
 
534
 
  inline void setWriteSet()
535
 
  {
536
 
    write_set->set();
537
 
  }
538
 
 
539
 
  inline void clearWriteSet(uint32_t index)
540
 
  {
541
 
    write_set->reset(index);
542
 
  }
543
 
 
544
 
  inline void clearWriteSet()
545
 
  {
546
 
    write_set->reset();
547
 
  }
548
 
 
549
583
  /* Is table open or should be treated as such by name-locking? */
550
 
  inline bool is_name_opened()
551
 
  {
552
 
    return db_stat || open_placeholder;
553
 
  }
 
584
  inline bool is_name_opened() { return db_stat || open_placeholder; }
554
585
  /*
555
586
    Is this instance of the table should be reopen or represents a name-lock?
556
587
  */
557
588
  inline bool needs_reopen_or_name_lock()
558
 
  { 
559
 
    return getShare()->getVersion() != refresh_version;
560
 
  }
561
 
 
562
 
  /**
563
 
    clean/setup table fields and map.
564
 
 
565
 
    @param table        Table structure pointer (which should be setup)
566
 
    @param table_list   TableList structure pointer (owner of Table)
567
 
    @param tablenr     table number
568
 
  */
569
 
  void setup_table_map(TableList *table_list, uint32_t tablenr);
570
 
  inline void mark_as_null_row()
571
 
  {
572
 
    null_row= 1;
573
 
    status|= STATUS_NULL_ROW;
574
 
    memset(null_flags, 255, getShare()->null_bytes);
575
 
  }
576
 
 
577
 
  void free_io_cache();
578
 
  void filesort_free_buffers(bool full= false);
579
 
  void intern_close_table();
580
 
 
581
 
  void print_error(int error, myf errflag)
582
 
  {
583
 
    getShare()->storage_engine->print_error(error, errflag, *this);
584
 
  }
585
 
 
586
 
  /**
587
 
    @return
588
 
    key if error because of duplicated keys
589
 
  */
590
 
  uint32_t get_dup_key(int error)
591
 
  {
592
 
    cursor->errkey  = (uint32_t) -1;
593
 
    if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
594
 
        error == HA_ERR_FOUND_DUPP_UNIQUE ||
595
 
        error == HA_ERR_DROP_INDEX_FK)
596
 
      cursor->info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
597
 
 
598
 
    return(cursor->errkey);
599
 
  }
600
 
 
601
 
  /*
602
 
    This is a short term fix. Long term we will used the TableIdentifier to do the actual comparison.
603
 
  */
604
 
  bool operator<(const Table &right) const
605
 
  {
606
 
    return getShare()->getCacheKey() < right.getShare()->getCacheKey();
607
 
  }
608
 
 
609
 
  static bool compare(const Table *a, const Table *b)
610
 
  {
611
 
    return *a < *b;
612
 
  }
613
 
 
614
 
  friend std::ostream& operator<<(std::ostream& output, const Table &table)
615
 
  {
616
 
    if (table.getShare())
617
 
    {
618
 
      output << "Table:(";
619
 
      output << table.getShare()->getSchemaName();
620
 
      output << ", ";
621
 
      output <<  table.getShare()->getTableName();
622
 
      output << ", ";
623
 
      output <<  table.getShare()->getTableTypeAsString();
624
 
      output << ")";
625
 
    }
626
 
    else
627
 
    {
628
 
      output << "Table:(has no share)";
629
 
    }
630
 
 
631
 
    return output;  // for multiple << operators.
632
 
  }
633
 
 
634
 
public:
635
 
  virtual bool isPlaceHolder(void) const
636
 
  {
637
 
    return false;
638
 
  }
639
 
};
640
 
 
641
 
/**
642
 
 * @class
643
 
 *  ForeignKeyInfo
644
 
 *
645
 
 * @brief
646
 
 *  This class defines the information for foreign keys.
647
 
 */
648
 
class ForeignKeyInfo
649
 
{
650
 
public:
651
 
    /**
652
 
     * @brief
653
 
     *  This is the constructor with all properties set.
654
 
     *
655
 
     * @param[in] in_foreign_id The id of the foreign key
656
 
     * @param[in] in_referenced_db The referenced database name of the foreign key
657
 
     * @param[in] in_referenced_table The referenced table name of the foreign key
658
 
     * @param[in] in_update_method The update method of the foreign key.
659
 
     * @param[in] in_delete_method The delete method of the foreign key.
660
 
     * @param[in] in_referenced_key_name The name of referenced key
661
 
     * @param[in] in_foreign_fields The foreign fields
662
 
     * @param[in] in_referenced_fields The referenced fields
663
 
     */
664
 
    ForeignKeyInfo(LEX_STRING *in_foreign_id,
665
 
                   LEX_STRING *in_referenced_db,
666
 
                   LEX_STRING *in_referenced_table,
667
 
                   LEX_STRING *in_update_method,
668
 
                   LEX_STRING *in_delete_method,
669
 
                   LEX_STRING *in_referenced_key_name,
670
 
                   List<LEX_STRING> in_foreign_fields,
671
 
                   List<LEX_STRING> in_referenced_fields)
672
 
    :
673
 
      foreign_id(in_foreign_id),
674
 
      referenced_db(in_referenced_db),
675
 
      referenced_table(in_referenced_table),
676
 
      update_method(in_update_method),
677
 
      delete_method(in_delete_method),
678
 
      referenced_key_name(in_referenced_key_name),
679
 
      foreign_fields(in_foreign_fields),
680
 
      referenced_fields(in_referenced_fields)
681
 
    {}
682
 
 
683
 
    /**
684
 
     * @brief
685
 
     *  This is the default constructor. All properties are set to default values for their types.
686
 
     */
687
 
    ForeignKeyInfo()
688
 
    : foreign_id(NULL), referenced_db(NULL), referenced_table(NULL),
689
 
      update_method(NULL), delete_method(NULL), referenced_key_name(NULL)
690
 
    {}
691
 
 
692
 
    /**
693
 
     * @brief
694
 
     *  Gets the foreign id.
695
 
     *
696
 
     * @ retval  the foreign id
697
 
     */
698
 
    const LEX_STRING *getForeignId() const
699
 
    {
700
 
        return foreign_id;
701
 
    }
702
 
 
703
 
    /**
704
 
     * @brief
705
 
     *  Gets the name of the referenced database.
706
 
     *
707
 
     * @ retval  the name of the referenced database
708
 
     */
709
 
    const LEX_STRING *getReferencedDb() const
710
 
    {
711
 
        return referenced_db;
712
 
    }
713
 
 
714
 
    /**
715
 
     * @brief
716
 
     *  Gets the name of the referenced table.
717
 
     *
718
 
     * @ retval  the name of the referenced table
719
 
     */
720
 
    const LEX_STRING *getReferencedTable() const
721
 
    {
722
 
        return referenced_table;
723
 
    }
724
 
 
725
 
    /**
726
 
     * @brief
727
 
     *  Gets the update method.
728
 
     *
729
 
     * @ retval  the update method
730
 
     */
731
 
    const LEX_STRING *getUpdateMethod() const
732
 
    {
733
 
        return update_method;
734
 
    }
735
 
 
736
 
    /**
737
 
     * @brief
738
 
     *  Gets the delete method.
739
 
     *
740
 
     * @ retval  the delete method
741
 
     */
742
 
    const LEX_STRING *getDeleteMethod() const
743
 
    {
744
 
        return delete_method;
745
 
    }
746
 
 
747
 
    /**
748
 
     * @brief
749
 
     *  Gets the name of the referenced key.
750
 
     *
751
 
     * @ retval  the name of the referenced key
752
 
     */
753
 
    const LEX_STRING *getReferencedKeyName() const
754
 
    {
755
 
        return referenced_key_name;
756
 
    }
757
 
 
758
 
    /**
759
 
     * @brief
760
 
     *  Gets the foreign fields.
761
 
     *
762
 
     * @ retval  the foreign fields
763
 
     */
764
 
    const List<LEX_STRING> &getForeignFields() const
765
 
    {
766
 
        return foreign_fields;
767
 
    }
768
 
 
769
 
    /**
770
 
     * @brief
771
 
     *  Gets the referenced fields.
772
 
     *
773
 
     * @ retval  the referenced fields
774
 
     */
775
 
    const List<LEX_STRING> &getReferencedFields() const
776
 
    {
777
 
        return referenced_fields;
778
 
    }
779
 
private:
780
 
    /**
781
 
     * The foreign id.
782
 
     */
783
 
    LEX_STRING *foreign_id;
784
 
    /**
785
 
     * The name of the reference database.
786
 
     */
787
 
    LEX_STRING *referenced_db;
788
 
    /**
789
 
     * The name of the reference table.
790
 
     */
791
 
    LEX_STRING *referenced_table;
792
 
    /**
793
 
     * The update method.
794
 
     */
795
 
    LEX_STRING *update_method;
796
 
    /**
797
 
     * The delete method.
798
 
     */
799
 
    LEX_STRING *delete_method;
800
 
    /**
801
 
     * The name of the referenced key.
802
 
     */
803
 
    LEX_STRING *referenced_key_name;
804
 
    /**
805
 
     * The foreign fields.
806
 
     */
807
 
    List<LEX_STRING> foreign_fields;
808
 
    /**
809
 
     * The referenced fields.
810
 
     */
811
 
    List<LEX_STRING> referenced_fields;
812
 
};
813
 
 
814
 
class TableList;
815
 
 
816
 
#define JOIN_TYPE_LEFT  1
817
 
#define JOIN_TYPE_RIGHT 2
 
589
  { return s->version != refresh_version; }
 
590
};
 
591
 
 
592
enum enum_schema_table_state
 
593
 
594
  NOT_PROCESSED= 0,
 
595
  PROCESSED_BY_CREATE_SORT_INDEX,
 
596
  PROCESSED_BY_JOIN_EXEC
 
597
};
 
598
 
 
599
typedef struct st_foreign_key_info
 
600
{
 
601
  LEX_STRING *forein_id;
 
602
  LEX_STRING *referenced_db;
 
603
  LEX_STRING *referenced_table;
 
604
  LEX_STRING *update_method;
 
605
  LEX_STRING *delete_method;
 
606
  LEX_STRING *referenced_key_name;
 
607
  List<LEX_STRING> foreign_fields;
 
608
  List<LEX_STRING> referenced_fields;
 
609
} FOREIGN_KEY_INFO;
 
610
 
 
611
/*
 
612
  Make sure that the order of schema_tables and enum_schema_tables are the same.
 
613
*/
 
614
 
 
615
enum enum_schema_tables
 
616
{
 
617
  SCH_CHARSETS= 0,
 
618
  SCH_COLLATIONS,
 
619
  SCH_COLLATION_CHARACTER_SET_APPLICABILITY,
 
620
  SCH_COLUMNS,
 
621
  SCH_GLOBAL_STATUS,
 
622
  SCH_GLOBAL_VARIABLES,
 
623
  SCH_KEY_COLUMN_USAGE,
 
624
  SCH_OPEN_TABLES,
 
625
  SCH_PLUGINS,
 
626
  SCH_PROCESSLIST,
 
627
  SCH_REFERENTIAL_CONSTRAINTS,
 
628
  SCH_SCHEMATA,
 
629
  SCH_SESSION_STATUS,
 
630
  SCH_SESSION_VARIABLES,
 
631
  SCH_STATISTICS,
 
632
  SCH_STATUS,
 
633
  SCH_TABLES,
 
634
  SCH_TABLE_CONSTRAINTS,
 
635
  SCH_TABLE_NAMES,
 
636
  SCH_VARIABLES,
 
637
};
 
638
 
 
639
 
 
640
#define MY_I_S_MAYBE_NULL 1
 
641
#define MY_I_S_UNSIGNED   2
 
642
 
 
643
 
 
644
#define SKIP_OPEN_TABLE 0                // do not open table
 
645
#define OPEN_FRM_ONLY   1                // open FRM file only
 
646
#define OPEN_FULL_TABLE 2                // open FRM,MYD, MYI files
 
647
 
 
648
typedef struct st_field_info
 
649
{
 
650
  /** 
 
651
      This is used as column name. 
 
652
  */
 
653
  const char* field_name;
 
654
  /**
 
655
     For string-type columns, this is the maximum number of
 
656
     characters. Otherwise, it is the 'display-length' for the column.
 
657
  */
 
658
  uint field_length;
 
659
  /**
 
660
     This denotes data type for the column. For the most part, there seems to
 
661
     be one entry in the enum for each SQL data type, although there seem to
 
662
     be a number of additional entries in the enum.
 
663
  */
 
664
  enum enum_field_types field_type;
 
665
  int value;
 
666
  /**
 
667
     This is used to set column attributes. By default, columns are @c NOT
 
668
     @c NULL and @c SIGNED, and you can deviate from the default
 
669
     by setting the appopriate flags. You can use either one of the flags
 
670
     @c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
 
671
     combine them using the bitwise or operator @c |. Both flags are
 
672
     defined in table.h.
 
673
   */
 
674
  uint field_flags;        // Field atributes(maybe_null, signed, unsigned etc.)
 
675
  const char* old_name;
 
676
  /**
 
677
     This should be one of @c SKIP_OPEN_TABLE,
 
678
     @c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
 
679
  */
 
680
  uint open_method;
 
681
} ST_FIELD_INFO;
 
682
 
 
683
 
 
684
struct TABLE_LIST;
 
685
typedef class Item COND;
 
686
 
 
687
typedef struct st_schema_table
 
688
{
 
689
  const char* table_name;
 
690
  ST_FIELD_INFO *fields_info;
 
691
  /* Create information_schema table */
 
692
  TABLE *(*create_table)  (THD *thd, TABLE_LIST *table_list);
 
693
  /* Fill table with data */
 
694
  int (*fill_table) (THD *thd, TABLE_LIST *tables, COND *cond);
 
695
  /* Handle fileds for old SHOW */
 
696
  int (*old_format) (THD *thd, struct st_schema_table *schema_table);
 
697
  int (*process_table) (THD *thd, TABLE_LIST *tables, TABLE *table,
 
698
                        bool res, LEX_STRING *db_name, LEX_STRING *table_name);
 
699
  int idx_field1, idx_field2; 
 
700
  bool hidden;
 
701
  uint i_s_requested_object;  /* the object we need to open(TABLE | VIEW) */
 
702
} ST_SCHEMA_TABLE;
 
703
 
 
704
 
 
705
#define JOIN_TYPE_LEFT  1
 
706
#define JOIN_TYPE_RIGHT 2
818
707
 
819
708
struct st_lex;
820
709
class select_union;
821
 
class Tmp_Table_Param;
822
 
 
823
 
void free_blobs(Table *table);
824
 
int set_zone(int nr,int min_zone,int max_zone);
825
 
uint32_t convert_period_to_month(uint32_t period);
826
 
uint32_t convert_month_to_period(uint32_t month);
827
 
 
828
 
int test_if_number(char *str,int *res,bool allow_wildcards);
829
 
void change_byte(unsigned char *,uint,char,char);
830
 
 
831
 
namespace optimizer { class SqlSelect; }
832
 
 
833
 
void change_double_for_sort(double nr,unsigned char *to);
834
 
double my_double_round(double value, int64_t dec, bool dec_unsigned,
835
 
                       bool truncate);
836
 
int get_quick_record(optimizer::SqlSelect *select);
837
 
 
838
 
void find_date(char *pos,uint32_t *vek,uint32_t flag);
839
 
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
840
 
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
841
 
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
842
 
void append_unescaped(String *res, const char *pos, uint32_t length);
843
 
 
844
 
int rename_file_ext(const char * from,const char * to,const char * ext);
845
 
bool check_column_name(const char *name);
846
 
bool check_db_name(Session *session, SchemaIdentifier &schema);
847
 
bool check_table_name(const char *name, uint32_t length);
848
 
 
849
 
} /* namespace drizzled */
850
 
 
851
 
#include "drizzled/table/instance.h"
852
 
#include "drizzled/table/concurrent.h"
853
 
 
854
 
#endif /* DRIZZLED_TABLE_H */
 
710
class TMP_TABLE_PARAM;
 
711
 
 
712
Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
 
713
                        const char *name);
 
714
 
 
715
struct Field_translator
 
716
{
 
717
  Item *item;
 
718
  const char *name;
 
719
};
 
720
 
 
721
 
 
722
/*
 
723
  Column reference of a NATURAL/USING join. Since column references in
 
724
  joins can be both from views and stored tables, may point to either a
 
725
  Field (for tables), or a Field_translator (for views).
 
726
*/
 
727
 
 
728
class Natural_join_column: public Sql_alloc
 
729
{
 
730
public:
 
731
  Field_translator *view_field;  /* Column reference of merge view. */
 
732
  Field            *table_field; /* Column reference of table or temp view. */
 
733
  TABLE_LIST *table_ref; /* Original base table/view reference. */
 
734
  /*
 
735
    True if a common join column of two NATURAL/USING join operands. Notice
 
736
    that when we have a hierarchy of nested NATURAL/USING joins, a column can
 
737
    be common at some level of nesting but it may not be common at higher
 
738
    levels of nesting. Thus this flag may change depending on at which level
 
739
    we are looking at some column.
 
740
  */
 
741
  bool is_common;
 
742
public:
 
743
  Natural_join_column(Field_translator *field_param, TABLE_LIST *tab);
 
744
  Natural_join_column(Field *field_param, TABLE_LIST *tab);
 
745
  const char *name();
 
746
  Item *create_item(THD *thd);
 
747
  Field *field();
 
748
  const char *table_name();
 
749
  const char *db_name();
 
750
};
 
751
 
 
752
 
 
753
/*
 
754
  Table reference in the FROM clause.
 
755
 
 
756
  These table references can be of several types that correspond to
 
757
  different SQL elements. Below we list all types of TABLE_LISTs with
 
758
  the necessary conditions to determine when a TABLE_LIST instance
 
759
  belongs to a certain type.
 
760
 
 
761
  1) table (TABLE_LIST::view == NULL)
 
762
     - base table
 
763
       (TABLE_LIST::derived == NULL)
 
764
     - subquery - TABLE_LIST::table is a temp table
 
765
       (TABLE_LIST::derived != NULL)
 
766
     - information schema table
 
767
       (TABLE_LIST::schema_table != NULL)
 
768
       NOTICE: for schema tables TABLE_LIST::field_translation may be != NULL
 
769
  2) view (TABLE_LIST::view != NULL)
 
770
     - merge    (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_MERGE)
 
771
           also (TABLE_LIST::field_translation != NULL)
 
772
     - tmptable (TABLE_LIST::effective_algorithm == VIEW_ALGORITHM_TMPTABLE)
 
773
           also (TABLE_LIST::field_translation == NULL)
 
774
  3) nested table reference (TABLE_LIST::nested_join != NULL)
 
775
     - table sequence - e.g. (t1, t2, t3)
 
776
       TODO: how to distinguish from a JOIN?
 
777
     - general JOIN
 
778
       TODO: how to distinguish from a table sequence?
 
779
     - NATURAL JOIN
 
780
       (TABLE_LIST::natural_join != NULL)
 
781
       - JOIN ... USING
 
782
         (TABLE_LIST::join_using_fields != NULL)
 
783
     - semi-join
 
784
       ;
 
785
*/
 
786
 
 
787
class Index_hint;
 
788
struct TABLE_LIST
 
789
{
 
790
  TABLE_LIST() {}                          /* Remove gcc warning */
 
791
 
 
792
  /**
 
793
    Prepare TABLE_LIST that consists of one table instance to use in
 
794
    simple_open_and_lock_tables
 
795
  */
 
796
  inline void init_one_table(const char *db_name_arg,
 
797
                             const char *table_name_arg,
 
798
                             enum thr_lock_type lock_type_arg)
 
799
  {
 
800
    bzero((char*) this, sizeof(*this));
 
801
    db= (char*) db_name_arg;
 
802
    table_name= alias= (char*) table_name_arg;
 
803
    lock_type= lock_type_arg;
 
804
  }
 
805
 
 
806
  /*
 
807
    List of tables local to a subquery (used by SQL_LIST). Considers
 
808
    views as leaves (unlike 'next_leaf' below). Created at parse time
 
809
    in st_select_lex::add_table_to_list() -> table_list.link_in_list().
 
810
  */
 
811
  TABLE_LIST *next_local;
 
812
  /* link in a global list of all queries tables */
 
813
  TABLE_LIST *next_global, **prev_global;
 
814
  char          *db, *alias, *table_name, *schema_table_name;
 
815
  char          *option;                /* Used by cache index  */
 
816
  Item          *on_expr;               /* Used with outer join */
 
817
  Item          *sj_on_expr;
 
818
  /*
 
819
    (Valid only for semi-join nests) Bitmap of tables that are within the
 
820
    semi-join (this is different from bitmap of all nest's children because
 
821
    tables that were pulled out of the semi-join nest remain listed as
 
822
    nest's children).
 
823
  */
 
824
  table_map     sj_inner_tables;
 
825
  /* Number of IN-compared expressions */
 
826
  uint          sj_in_exprs; 
 
827
  /*
 
828
    The structure of ON expression presented in the member above
 
829
    can be changed during certain optimizations. This member
 
830
    contains a snapshot of AND-OR structure of the ON expression
 
831
    made after permanent transformations of the parse tree, and is
 
832
    used to restore ON clause before every reexecution of a prepared
 
833
    statement or stored procedure.
 
834
  */
 
835
  Item          *prep_on_expr;
 
836
  COND_EQUAL    *cond_equal;            /* Used with outer join */
 
837
  /*
 
838
    During parsing - left operand of NATURAL/USING join where 'this' is
 
839
    the right operand. After parsing (this->natural_join == this) iff
 
840
    'this' represents a NATURAL or USING join operation. Thus after
 
841
    parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
 
842
  */
 
843
  TABLE_LIST *natural_join;
 
844
  /*
 
845
    True if 'this' represents a nested join that is a NATURAL JOIN.
 
846
    For one of the operands of 'this', the member 'natural_join' points
 
847
    to the other operand of 'this'.
 
848
  */
 
849
  bool is_natural_join;
 
850
  /* Field names in a USING clause for JOIN ... USING. */
 
851
  List<String> *join_using_fields;
 
852
  /*
 
853
    Explicitly store the result columns of either a NATURAL/USING join or
 
854
    an operand of such a join.
 
855
  */
 
856
  List<Natural_join_column> *join_columns;
 
857
  /* TRUE if join_columns contains all columns of this table reference. */
 
858
  bool is_join_columns_complete;
 
859
 
 
860
  /*
 
861
    List of nodes in a nested join tree, that should be considered as
 
862
    leaves with respect to name resolution. The leaves are: views,
 
863
    top-most nodes representing NATURAL/USING joins, subqueries, and
 
864
    base tables. All of these TABLE_LIST instances contain a
 
865
    materialized list of columns. The list is local to a subquery.
 
866
  */
 
867
  TABLE_LIST *next_name_resolution_table;
 
868
  /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
 
869
  List<Index_hint> *index_hints;
 
870
  TABLE        *table;                          /* opened table */
 
871
  uint          table_id; /* table id (from binlog) for opened table */
 
872
  /*
 
873
    select_result for derived table to pass it from table creation to table
 
874
    filling procedure
 
875
  */
 
876
  select_union  *derived_result;
 
877
  /*
 
878
    Reference from aux_tables to local list entry of main select of
 
879
    multi-delete statement:
 
880
    delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
 
881
    here it will be reference of first occurrence of t1 to second (as you
 
882
    can see this lists can't be merged)
 
883
  */
 
884
  TABLE_LIST    *correspondent_table;
 
885
  st_select_lex_unit *derived;          /* SELECT_LEX_UNIT of derived table */
 
886
  ST_SCHEMA_TABLE *schema_table;        /* Information_schema table */
 
887
  st_select_lex *schema_select_lex;
 
888
  /*
 
889
    True when the view field translation table is used to convert
 
890
    schema table fields for backwards compatibility with SHOW command.
 
891
  */
 
892
  bool schema_table_reformed;
 
893
  TMP_TABLE_PARAM *schema_table_param;
 
894
  /* link to select_lex where this table was used */
 
895
  st_select_lex *select_lex;
 
896
  Field_translator *field_translation;  /* array of VIEW fields */
 
897
  /* pointer to element after last one in translation table above */
 
898
  Field_translator *field_translation_end;
 
899
  /*
 
900
    List (based on next_local) of underlying tables of this view. I.e. it
 
901
    does not include the tables of subqueries used in the view. Is set only
 
902
    for merged views.
 
903
  */
 
904
  TABLE_LIST    *merge_underlying_list;
 
905
  /*
 
906
    - 0 for base tables
 
907
    - in case of the view it is the list of all (not only underlying
 
908
    tables but also used in subquery ones) tables of the view.
 
909
  */
 
910
  List<TABLE_LIST> *view_tables;
 
911
  /* most upper view this table belongs to */
 
912
  TABLE_LIST    *belong_to_view;
 
913
  /* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
 
914
  TABLE_LIST    *parent_l;
 
915
  /*
 
916
    List of all base tables local to a subquery including all view
 
917
    tables. Unlike 'next_local', this in this list views are *not*
 
918
    leaves. Created in setup_tables() -> make_leaves_list().
 
919
  */
 
920
  TABLE_LIST    *next_leaf;
 
921
  /* data need by some engines in query cache*/
 
922
  uint64_t     engine_data;
 
923
  /* call back function for asking handler about caching in query cache */
 
924
  qc_engine_callback callback_func;
 
925
  thr_lock_type lock_type;
 
926
  uint          outer_join;             /* Which join type */
 
927
  uint          shared;                 /* Used in multi-upd */
 
928
  size_t        db_length;
 
929
  size_t        table_name_length;
 
930
  bool          straight;               /* optimize with prev table */
 
931
  bool          updating;               /* for replicate-do/ignore table */
 
932
  bool          force_index;            /* prefer index over table scan */
 
933
  bool          ignore_leaves;          /* preload only non-leaf nodes */
 
934
  table_map     dep_tables;             /* tables the table depends on      */
 
935
  table_map     on_expr_dep_tables;     /* tables on expression depends on  */
 
936
  struct st_nested_join *nested_join;   /* if the element is a nested join  */
 
937
  TABLE_LIST *embedding;             /* nested join containing the table */
 
938
  List<TABLE_LIST> *join_list;/* join list the table belongs to   */
 
939
  bool          cacheable_table;        /* stop PS caching */
 
940
  /* FRMTYPE_ERROR if any type is acceptable */
 
941
  enum frm_type_enum required_type;
 
942
  handlerton    *db_type;               /* table_type for handler */
 
943
  char          timestamp_buffer[20];   /* buffer for timestamp (19+1) */
 
944
  /*
 
945
    This TABLE_LIST object corresponds to the table to be created
 
946
    so it is possible that it does not exist (used in CREATE TABLE
 
947
    ... SELECT implementation).
 
948
  */
 
949
  bool          create;
 
950
  /* For transactional locking. */
 
951
  int           lock_timeout;           /* NOWAIT or WAIT [X]               */
 
952
  bool          lock_transactional;     /* If transactional lock requested. */
 
953
  bool          internal_tmp_table;
 
954
  /** TRUE if an alias for this table was specified in the SQL. */
 
955
  bool          is_alias;
 
956
  /** TRUE if the table is referred to in the statement using a fully
 
957
      qualified name (<db_name>.<table_name>).
 
958
  */
 
959
  bool          is_fqtn;
 
960
 
 
961
  uint i_s_requested_object;
 
962
  bool has_db_lookup_value;
 
963
  bool has_table_lookup_value;
 
964
  uint table_open_method;
 
965
  enum enum_schema_table_state schema_table_state;
 
966
  void set_underlying_merge();
 
967
  bool setup_underlying(THD *thd);
 
968
  void cleanup_items();
 
969
  /*
 
970
    If you change placeholder(), please check the condition in
 
971
    check_transactional_lock() too.
 
972
  */
 
973
  bool placeholder()
 
974
  {
 
975
    return derived || schema_table || (create && !table->db_stat) || !table;
 
976
  }
 
977
  void print(THD *thd, String *str, enum_query_type query_type);
 
978
  bool set_insert_values(MEM_ROOT *mem_root);
 
979
  TABLE_LIST *find_underlying_table(TABLE *table);
 
980
  TABLE_LIST *first_leaf_for_name_resolution();
 
981
  TABLE_LIST *last_leaf_for_name_resolution();
 
982
  bool is_leaf_for_name_resolution();
 
983
  inline TABLE_LIST *top_table()
 
984
    { return belong_to_view ? belong_to_view : this; }
 
985
 
 
986
  /*
 
987
    Cleanup for re-execution in a prepared statement or a stored
 
988
    procedure.
 
989
  */
 
990
  void reinit_before_use(THD *thd);
 
991
  Item_subselect *containing_subselect();
 
992
 
 
993
  /* 
 
994
    Compiles the tagged hints list and fills up st_table::keys_in_use_for_query,
 
995
    st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
 
996
    st_table::force_index and st_table::covering_keys.
 
997
  */
 
998
  bool process_index_hints(TABLE *table);
 
999
 
 
1000
private:
 
1001
  bool prep_where(THD *thd, Item **conds, bool no_where_clause);
 
1002
  /*
 
1003
    Cleanup for re-execution in a prepared statement or a stored
 
1004
    procedure.
 
1005
  */
 
1006
};
 
1007
 
 
1008
class Item;
 
1009
 
 
1010
/*
 
1011
  Iterator over the fields of a generic table reference.
 
1012
*/
 
1013
 
 
1014
class Field_iterator: public Sql_alloc
 
1015
{
 
1016
public:
 
1017
  Field_iterator() {}                         /* Remove gcc warning */
 
1018
  virtual ~Field_iterator() {}
 
1019
  virtual void set(TABLE_LIST *)= 0;
 
1020
  virtual void next()= 0;
 
1021
  virtual bool end_of_fields()= 0;              /* Return 1 at end of list */
 
1022
  virtual const char *name()= 0;
 
1023
  virtual Item *create_item(THD *)= 0;
 
1024
  virtual Field *field()= 0;
 
1025
};
 
1026
 
 
1027
 
 
1028
/* 
 
1029
  Iterator over the fields of a base table, view with temporary
 
1030
  table, or subquery.
 
1031
*/
 
1032
 
 
1033
class Field_iterator_table: public Field_iterator
 
1034
{
 
1035
  Field **ptr;
 
1036
public:
 
1037
  Field_iterator_table() :ptr(0) {}
 
1038
  void set(TABLE_LIST *table) { ptr= table->table->field; }
 
1039
  void set_table(TABLE *table) { ptr= table->field; }
 
1040
  void next() { ptr++; }
 
1041
  bool end_of_fields() { return *ptr == 0; }
 
1042
  const char *name();
 
1043
  Item *create_item(THD *thd);
 
1044
  Field *field() { return *ptr; }
 
1045
};
 
1046
 
 
1047
 
 
1048
/* Iterator over the fields of a merge view. */
 
1049
 
 
1050
class Field_iterator_view: public Field_iterator
 
1051
{
 
1052
  Field_translator *ptr, *array_end;
 
1053
  TABLE_LIST *view;
 
1054
public:
 
1055
  Field_iterator_view() :ptr(0), array_end(0) {}
 
1056
  void set(TABLE_LIST *table);
 
1057
  void next() { ptr++; }
 
1058
  bool end_of_fields() { return ptr == array_end; }
 
1059
  const char *name();
 
1060
  Item *create_item(THD *thd);
 
1061
  Item **item_ptr() {return &ptr->item; }
 
1062
  Field *field() { return 0; }
 
1063
  inline Item *item() { return ptr->item; }
 
1064
  Field_translator *field_translator() { return ptr; }
 
1065
};
 
1066
 
 
1067
 
 
1068
/*
 
1069
  Field_iterator interface to the list of materialized fields of a
 
1070
  NATURAL/USING join.
 
1071
*/
 
1072
 
 
1073
class Field_iterator_natural_join: public Field_iterator
 
1074
{
 
1075
  List_iterator_fast<Natural_join_column> column_ref_it;
 
1076
  Natural_join_column *cur_column_ref;
 
1077
public:
 
1078
  Field_iterator_natural_join() :cur_column_ref(NULL) {}
 
1079
  ~Field_iterator_natural_join() {}
 
1080
  void set(TABLE_LIST *table);
 
1081
  void next();
 
1082
  bool end_of_fields() { return !cur_column_ref; }
 
1083
  const char *name() { return cur_column_ref->name(); }
 
1084
  Item *create_item(THD *thd) { return cur_column_ref->create_item(thd); }
 
1085
  Field *field() { return cur_column_ref->field(); }
 
1086
  Natural_join_column *column_ref() { return cur_column_ref; }
 
1087
};
 
1088
 
 
1089
 
 
1090
/*
 
1091
  Generic iterator over the fields of an arbitrary table reference.
 
1092
 
 
1093
  DESCRIPTION
 
1094
    This class unifies the various ways of iterating over the columns
 
1095
    of a table reference depending on the type of SQL entity it
 
1096
    represents. If such an entity represents a nested table reference,
 
1097
    this iterator encapsulates the iteration over the columns of the
 
1098
    members of the table reference.
 
1099
 
 
1100
  IMPLEMENTATION
 
1101
    The implementation assumes that all underlying NATURAL/USING table
 
1102
    references already contain their result columns and are linked into
 
1103
    the list TABLE_LIST::next_name_resolution_table.
 
1104
*/
 
1105
 
 
1106
class Field_iterator_table_ref: public Field_iterator
 
1107
{
 
1108
  TABLE_LIST *table_ref, *first_leaf, *last_leaf;
 
1109
  Field_iterator_table        table_field_it;
 
1110
  Field_iterator_view         view_field_it;
 
1111
  Field_iterator_natural_join natural_join_it;
 
1112
  Field_iterator *field_it;
 
1113
  void set_field_iterator();
 
1114
public:
 
1115
  Field_iterator_table_ref() :field_it(NULL) {}
 
1116
  void set(TABLE_LIST *table);
 
1117
  void next();
 
1118
  bool end_of_fields()
 
1119
  { return (table_ref == last_leaf && field_it->end_of_fields()); }
 
1120
  const char *name() { return field_it->name(); }
 
1121
  const char *table_name();
 
1122
  const char *db_name();
 
1123
  Item *create_item(THD *thd) { return field_it->create_item(thd); }
 
1124
  Field *field() { return field_it->field(); }
 
1125
  Natural_join_column *get_or_create_column_ref(TABLE_LIST *parent_table_ref);
 
1126
  Natural_join_column *get_natural_column_ref();
 
1127
};
 
1128
 
 
1129
 
 
1130
typedef struct st_nested_join
 
1131
{
 
1132
  List<TABLE_LIST>  join_list;       /* list of elements in the nested join */
 
1133
  table_map         used_tables;     /* bitmap of tables in the nested join */
 
1134
  table_map         not_null_tables; /* tables that rejects nulls           */
 
1135
  struct st_join_table *first_nested;/* the first nested table in the plan  */
 
1136
  /* 
 
1137
    Used to count tables in the nested join in 2 isolated places:
 
1138
    1. In make_outerjoin_info(). 
 
1139
    2. check_interleaving_with_nj/restore_prev_nj_state (these are called
 
1140
       by the join optimizer. 
 
1141
    Before each use the counters are zeroed by reset_nj_counters.
 
1142
  */
 
1143
  uint              counter_;
 
1144
  nested_join_map   nj_map;          /* Bit used to identify this nested join*/
 
1145
  /*
 
1146
    (Valid only for semi-join nests) Bitmap of tables outside the semi-join
 
1147
    that are used within the semi-join's ON condition.
 
1148
  */
 
1149
  table_map         sj_depends_on;
 
1150
  /* Outer non-trivially correlated tables */
 
1151
  table_map         sj_corr_tables;
 
1152
  List<Item>        sj_outer_expr_list;
 
1153
} NESTED_JOIN;
 
1154
 
 
1155
 
 
1156
typedef struct st_changed_table_list
 
1157
{
 
1158
  struct        st_changed_table_list *next;
 
1159
  char          *key;
 
1160
  uint32        key_length;
 
1161
} CHANGED_TABLE_LIST;
 
1162
 
 
1163
 
 
1164
typedef struct st_open_table_list{
 
1165
  struct st_open_table_list *next;
 
1166
  char  *db,*table;
 
1167
  uint32 in_use,locked;
 
1168
} OPEN_TABLE_LIST;
 
1169
 
 
1170
typedef struct st_table_field_w_type
 
1171
{
 
1172
  LEX_STRING name;
 
1173
  LEX_STRING type;
 
1174
  LEX_STRING cset;
 
1175
} TABLE_FIELD_W_TYPE;
 
1176
 
 
1177
 
 
1178
my_bool
 
1179
table_check_intact(TABLE *table, const uint table_f_count,
 
1180
                   const TABLE_FIELD_W_TYPE *table_def);
 
1181
 
 
1182
static inline my_bitmap_map *tmp_use_all_columns(TABLE *table,
 
1183
                                                 MY_BITMAP *bitmap)
 
1184
{
 
1185
  my_bitmap_map *old= bitmap->bitmap;
 
1186
  bitmap->bitmap= table->s->all_set.bitmap;
 
1187
  return old;
 
1188
}
 
1189
 
 
1190
 
 
1191
static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
 
1192
                                          my_bitmap_map *old)
 
1193
{
 
1194
  bitmap->bitmap= old;
 
1195
}
 
1196
 
 
1197
/* The following is only needed for debugging */
 
1198
 
 
1199
static inline my_bitmap_map *dbug_tmp_use_all_columns(TABLE *table,
 
1200
                                                      MY_BITMAP *bitmap)
 
1201
{
 
1202
#ifndef DBUG_OFF
 
1203
  return tmp_use_all_columns(table, bitmap);
 
1204
#else
 
1205
  return 0;
 
1206
#endif
 
1207
}
 
1208
 
 
1209
static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap,
 
1210
                                               my_bitmap_map *old)
 
1211
{
 
1212
#ifndef DBUG_OFF
 
1213
  tmp_restore_column_map(bitmap, old);
 
1214
#endif
 
1215
}
 
1216
 
 
1217
size_t max_row_length(TABLE *table, const uchar *data);
 
1218