~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.h

  • Committer: Monty Taylor
  • Date: 2008-08-01 23:59:59 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801235959-n8ypy9r5aohown77
Gettext error compiles and passes test!

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