23
23
#ifndef DRIZZLED_TABLE_H
24
24
#define DRIZZLED_TABLE_H
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"
26
#include <storage/myisam/myisam.h>
27
#include <drizzled/order.h>
28
#include <drizzled/filesort_info.h>
29
#include <drizzled/natural_join_column.h>
30
#include <drizzled/field_iterator.h>
31
#include <mysys/hash.h>
32
#include <drizzled/handler.h>
33
#include <drizzled/lex_string.h>
34
#include <drizzled/table_list.h>
35
#include <drizzled/table_share.h>
43
39
class Item_subselect;
44
40
class Select_Lex_Unit;
47
class SecurityContext;
43
class Security_context;
46
/*************************************************************************/
49
49
class Field_timestamp;
52
extern uint64_t refresh_version;
54
52
typedef enum enum_table_category TABLE_CATEGORY;
55
typedef struct st_columndef MI_COLUMNDEF;
54
TABLE_CATEGORY get_table_category(const LEX_STRING *db,
55
const LEX_STRING *name);
58
extern uint32_t refresh_version;
60
typedef struct st_table_field_w_type
57
67
bool create_myisam_from_heap(Session *session, Table *table,
58
68
MI_COLUMNDEF *start_recinfo,
59
69
MI_COLUMNDEF **recinfo,
60
70
int error, bool ignore_last_dupp_key_error);
63
* Class representing a set of records, either in a temporary,
64
* normal, or derived table.
70
TableShare *s; /**< Pointer to the shared metadata about the table */
71
Field **field; /**< Pointer to fields collection */
73
Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
77
MyBitmap *read_set; /* Active column sets */
78
MyBitmap *write_set; /* Active column sets */
81
uint32_t db_stat; /**< information about the cursor as in Cursor.h */
83
MyBitmap def_read_set; /**< Default read set of columns */
84
MyBitmap def_write_set; /**< Default write set of columns */
85
MyBitmap tmp_set; /* Not sure about this... */
87
Session *in_use; /**< Pointer to the current session using this object */
93
unsigned char *record[2]; /**< Pointer to "records" */
94
unsigned char *insert_values; /* used by INSERT ... UPDATE */
95
KEY *key_info; /**< data of keys in database */
96
Field *next_number_field; /**< Set if next_number is activated. @TODO What the heck is the difference between this and the next member? */
97
Field *found_next_number_field; /**< Points to the "next-number" field (autoincrement field) */
98
Field_timestamp *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
100
TableList *pos_in_table_list; /* Element referring to this table */
76
Table() {} /* Remove gcc warning */
79
inline TABLE_SHARE *getShare() { return s; } /* Get rid of this long term */
80
inline void setShare(TABLE_SHARE *new_share) { s= new_share; } /* Get rid of this long term */
81
inline uint32_t sizeKeys() { return s->keys; }
82
inline uint32_t sizeFields() { return s->fields; }
83
inline uint32_t getRecordLength() { return s->reclength; }
84
inline uint32_t sizeBlobFields() { return s->blob_fields; }
85
inline uint32_t *getBlobField() { return s->blob_field; }
86
inline uint32_t getNullBytes() { return s->null_bytes; }
87
inline uint32_t getNullFields() { return s->null_fields; }
88
inline unsigned char *getDefaultValues() { return s->default_values; }
90
inline bool isDatabaseLowByteFirst() { return s->db_low_byte_first; } /* Portable row format */
91
inline bool isCrashed() { return s->crashed; }
92
inline bool isNameLock() { return s->name_lock; }
93
inline bool isReplaceWithNameLock() { return s->replace_with_name_lock; }
94
inline bool isWaitingOnCondition() { return s->waiting_on_cond; } /* Protection against free */
96
/* For TMP tables, should be pulled out as a class */
97
void updateCreateInfo(HA_CREATE_INFO *create_info);
98
void setup_tmp_table_column_bitmaps();
99
bool create_myisam_tmp_table(KEY *keyinfo,
100
MI_COLUMNDEF *start_recinfo,
101
MI_COLUMNDEF **recinfo,
103
void free_tmp_table(Session *session);
104
bool open_tmp_table();
105
size_t max_row_length(const unsigned char *data);
106
uint32_t find_shortest_key(const key_map *usable_keys);
107
bool compare_record(Field **ptr);
108
bool compare_record();
110
bool table_check_intact(const uint32_t table_f_count, const TABLE_FIELD_W_TYPE *table_def);
112
/* See if this can be blown away */
113
inline uint32_t getDBStat () { return db_stat; }
114
inline uint32_t setDBStat () { return db_stat; }
115
uint32_t db_stat; /* mode of file as in handler.h */
120
Session *in_use; /* Which thread uses this */
121
Field **field; /* Pointer to fields */
123
unsigned char *record[2]; /* Pointer to records */
124
unsigned char *write_row_record; /* Used as optimisation in
125
Session::write_row */
126
unsigned char *insert_values; /* used by INSERT ... UPDATE */
128
Map of keys that can be used to retrieve all data from this table
129
needed by the query without reading the row.
131
key_map covering_keys;
132
key_map quick_keys, merge_keys;
134
A set of keys that can be used in the query that references this
137
All indexes disabled on the table's TABLE_SHARE (see Table::s) will be
138
subtracted from this set upon instantiation. Thus for any Table t it holds
139
that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
140
must not introduce any new keys here (see setup_tables).
142
The set is implemented as a bitmap.
144
key_map keys_in_use_for_query;
145
/* Map of keys that can be used to calculate GROUP BY without sorting */
146
key_map keys_in_use_for_group_by;
147
/* Map of keys that can be used to calculate ORDER BY without sorting */
148
key_map keys_in_use_for_order_by;
149
KEY *key_info; /* data of keys in database */
151
Field *next_number_field; /* Set if next_number is activated */
152
Field *found_next_number_field; /* Set on open */
153
Field_timestamp *timestamp_field;
154
Field **vfield; /* Pointer to virtual fields*/
156
TableList *pos_in_table_list;/* Element referring to this table */
102
const char *alias; /**< alias or table name if no alias */
103
unsigned char *null_flags;
105
uint32_t lock_position; /**< Position in DRIZZLE_LOCK.table */
106
uint32_t lock_data_start; /**< Start pos. in DRIZZLE_LOCK.locks */
107
uint32_t lock_count; /**< Number of locks */
108
uint32_t used_fields;
109
uint32_t status; /* What's in record[0] */
158
const char *alias; /* alias or table name */
159
unsigned char *null_flags;
160
my_bitmap_map *bitmap_init_value;
161
std::bitset<MAX_FIELDS> def_read_set, def_write_set, tmp_set; /* containers */
162
std::bitset<MAX_FIELDS> *read_set, *write_set; /* Active column sets */
164
The ID of the query that opened and is using this table. Has different
165
meanings depending on the table type.
169
table->query_id is set to session->query_id for the duration of a statement
170
and is reset to 0 once it is closed by the same statement. A non-zero
171
table->query_id means that a statement is using the table even if it's
172
not the current statement (table is in use by some outer statement).
174
Non-temporary tables:
176
Under pre-locked or LOCK TABLES mode: query_id is set to session->query_id
177
for the duration of a statement and is reset to 0 once it is closed by
178
the same statement. A non-zero query_id is used to control which tables
179
in the list of pre-opened and locked tables are actually being used.
184
For each key that has quick_keys.is_set(key) == true: estimate of #records
185
and max #key parts that range access would use.
187
ha_rows quick_rows[MAX_KEY];
189
/* Bitmaps of key parts that =const for the entire join. */
190
key_part_map const_key_parts[MAX_KEY];
192
uint quick_key_parts[MAX_KEY];
193
uint quick_n_ranges[MAX_KEY];
196
Estimate of number of records that satisfy SARGable part of the table
197
condition, or table->file->records if no SARGable condition could be
199
This value is used by join optimizer as an estimate of number of records
200
that will pass the table condition (condition that depends on fields of
201
this table and constants)
203
ha_rows quick_condition_rows;
206
If this table has TIMESTAMP field with auto-set property (pointed by
207
timestamp_field member) then this variable indicates during which
208
operations (insert only/on update/in both cases) we should set this
209
field to current timestamp. If there are no such field in this table
210
or we should not automatically set its value during execution of current
211
statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
213
Value of this variable is set for each statement in open_table() and
214
if needed cleared later in statement processing code (see mysql_update()
217
timestamp_auto_set_type timestamp_field_type;
218
table_map map; /* ID bit of table (1,2,4,8,16...) */
220
uint32_t lock_position; /* Position in DRIZZLE_LOCK.table */
221
uint32_t lock_data_start; /* Start pos. in DRIZZLE_LOCK.locks */
222
uint32_t lock_count; /* Number of locks */
223
uint tablenr,used_fields;
224
uint32_t temp_pool_slot; /* Used by intern temp tables */
225
uint status; /* What's in record[0] */
110
226
/* number of select if it is derived table */
111
uint32_t derived_select_number;
112
int current_lock; /**< Type of lock on table */
113
bool copy_blobs; /**< Should blobs by copied when storing? */
227
uint32_t derived_select_number;
228
int current_lock; /* Type of lock on table */
229
bool copy_blobs; /* copy_blobs when storing */
116
232
0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
142
257
- setting version to 0 - this will force other threads to close
143
258
the instance of this table and wait (this is the same approach
144
259
as used for usual name locks).
145
An exclusively name-locked table currently can have no Cursor
260
An exclusively name-locked table currently can have no handler
146
261
object associated with it (db_stat is always 0), but please do
147
262
not rely on that.
149
264
bool open_placeholder;
265
bool locked_by_logger;
150
267
bool locked_by_name;
269
/* To signal that the table is associated with a HANDLER statement */
270
bool open_by_handler;
153
272
To indicate that a non-null value of the auto_increment field
154
273
was provided by the user or retrieved from the current record.
155
274
Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
157
276
bool auto_increment_field_not_null;
158
bool alias_name_used; /* true if table_name is alias */
161
The ID of the query that opened and is using this table. Has different
162
meanings depending on the table type.
166
table->query_id is set to session->query_id for the duration of a statement
167
and is reset to 0 once it is closed by the same statement. A non-zero
168
table->query_id means that a statement is using the table even if it's
169
not the current statement (table is in use by some outer statement).
171
Non-temporary tables:
173
Under pre-locked or LOCK TABLES mode: query_id is set to session->query_id
174
for the duration of a statement and is reset to 0 once it is closed by
175
the same statement. A non-zero query_id is used to control which tables
176
in the list of pre-opened and locked tables are actually being used.
181
* Estimate of number of records that satisfy SARGable part of the table
182
* condition, or table->cursor->records if no SARGable condition could be
184
* This value is used by join optimizer as an estimate of number of records
185
* that will pass the table condition (condition that depends on fields of
186
* this table and constants)
188
ha_rows quick_condition_rows;
191
If this table has TIMESTAMP field with auto-set property (pointed by
192
timestamp_field member) then this variable indicates during which
193
operations (insert only/on update/in both cases) we should set this
194
field to current timestamp. If there are no such field in this table
195
or we should not automatically set its value during execution of current
196
statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
198
Value of this variable is set for each statement in open_table() and
199
if needed cleared later in statement processing code (see mysql_update()
202
timestamp_auto_set_type timestamp_field_type;
203
table_map map; ///< ID bit of table (1,2,4,8,16...)
205
RegInfo reginfo; /* field connections */
208
Map of keys that can be used to retrieve all data from this table
209
needed by the query without reading the row.
211
key_map covering_keys;
216
A set of keys that can be used in the query that references this
219
All indexes disabled on the table's TableShare (see Table::s) will be
220
subtracted from this set upon instantiation. Thus for any Table t it holds
221
that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
222
must not introduce any new keys here (see setup_tables).
224
The set is implemented as a bitmap.
226
key_map keys_in_use_for_query;
227
/* Map of keys that can be used to calculate GROUP BY without sorting */
228
key_map keys_in_use_for_group_by;
229
/* Map of keys that can be used to calculate ORDER BY without sorting */
230
key_map keys_in_use_for_order_by;
233
For each key that has quick_keys.test(key) == true: estimate of #records
234
and max #key parts that range access would use.
236
ha_rows quick_rows[MAX_KEY];
238
/* Bitmaps of key parts that =const for the entire join. */
239
key_part_map const_key_parts[MAX_KEY];
241
uint32_t quick_key_parts[MAX_KEY];
242
uint32_t quick_n_ranges[MAX_KEY];
244
memory::Root mem_root;
277
bool insert_or_update; /* Can be used by the handler */
278
bool alias_name_used; /* true if table_name is alias */
279
bool get_fields_in_item_tree; /* Signal to fix_field */
281
REGINFO reginfo; /* field connections */
245
283
filesort_info_st sort;
249
int report_error(int error);
251
* Free information allocated by openfrm
253
* @param If true if we also want to free table_share
255
int closefrm(bool free_share);
257
void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
260
inline const TableShare *getShare() const { assert(s); return s; } /* Get rid of this long term */
261
inline void setShare(TableShare *new_share) { s= new_share; } /* Get rid of this long term */
262
inline uint32_t sizeKeys() { return s->keys; }
263
inline uint32_t sizeFields() { return s->fields; }
264
inline uint32_t getRecordLength() { return s->reclength; }
265
inline uint32_t sizeBlobFields() { return s->blob_fields; }
266
inline uint32_t *getBlobField() { return s->blob_field; }
267
inline uint32_t getNullBytes() { return s->null_bytes; }
268
inline uint32_t getNullFields() { return s->null_fields; }
269
inline unsigned char *getDefaultValues() { return s->default_values; }
271
inline bool isDatabaseLowByteFirst() { return s->db_low_byte_first; } /* Portable row format */
272
inline bool isNameLock() { return s->name_lock; }
273
inline bool isReplaceWithNameLock() { return s->replace_with_name_lock; }
274
inline bool isWaitingOnCondition() { return s->waiting_on_cond; } /* Protection against free */
276
uint32_t index_flags(uint32_t idx) const
278
return s->storage_engine->index_flags(s->key_info[idx].algorithm);
281
inline plugin::StorageEngine *getEngine() const /* table_type for handler */
283
return s->storage_engine;
286
Cursor &getCursor() const /* table_type for handler */
292
/* For TMP tables, should be pulled out as a class */
293
void updateCreateInfo(message::Table *table_proto);
294
void setup_tmp_table_column_bitmaps(unsigned char *bitmaps);
295
bool create_myisam_tmp_table(KEY *keyinfo,
296
MI_COLUMNDEF *start_recinfo,
297
MI_COLUMNDEF **recinfo,
299
void free_tmp_table(Session *session);
300
bool open_tmp_table();
301
size_t max_row_length(const unsigned char *data);
302
uint32_t find_shortest_key(const key_map *usable_keys);
303
bool compare_record(Field **ptr);
304
bool compare_record();
305
/* TODO: the (re)storeRecord's may be able to be further condensed */
307
void storeRecordAsInsert();
308
void storeRecordAsDefault();
309
void restoreRecord();
310
void restoreRecordAsDefault();
313
/* See if this can be blown away */
314
inline uint32_t getDBStat () { return db_stat; }
315
inline uint32_t setDBStat () { return db_stat; }
317
* Create Item_field for each column in the table.
319
* @param[out] a pointer to an empty list used to store items
323
* Create Item_field object for each column in the table and
324
* initialize it with the corresponding Field. New items are
325
* created in the current Session memory root.
330
* true when out of memory
332
285
bool fill_item_list(List<Item> *item_list) const;
286
void reset_item_list(List<Item> *item_list) const;
333
287
void clear_column_bitmaps(void);
334
288
void prepare_for_position(void);
335
void mark_columns_used_by_index_no_reset(uint32_t index, MyBitmap *map);
336
void mark_columns_used_by_index_no_reset(uint32_t index);
289
void mark_columns_used_by_index_no_reset(uint32_t index, std::bitset<MAX_FIELDS> *map);
337
290
void mark_columns_used_by_index(uint32_t index);
338
291
void restore_column_maps_after_mark_index();
339
292
void mark_auto_increment_column(void);
340
293
void mark_columns_needed_for_update(void);
341
294
void mark_columns_needed_for_delete(void);
342
295
void mark_columns_needed_for_insert(void);
343
inline void column_bitmaps_set(MyBitmap *read_set_arg,
344
MyBitmap *write_set_arg)
346
read_set= read_set_arg;
347
write_set= write_set_arg;
350
void restore_column_map(my_bitmap_map *old);
352
my_bitmap_map *use_all_columns(MyBitmap *bitmap);
296
void mark_virtual_columns(void);
297
inline void column_bitmaps_set(std::bitset<MAX_FIELDS> *read_set_arg,
298
std::bitset<MAX_FIELDS> *write_set_arg)
300
read_set= read_set_arg;
301
write_set= write_set_arg;
303
file->column_bitmaps_signal();
305
inline void column_bitmaps_set_no_signal(std::bitset<MAX_FIELDS> *read_set_arg,
306
std::bitset<MAX_FIELDS> *write_set_arg)
308
read_set= read_set_arg;
309
write_set= write_set_arg;
312
void restore_column_map(std::bitset<MAX_FIELDS> *old);
314
std::bitset<MAX_FIELDS> *use_all_columns(std::bitset<MAX_FIELDS> *bitmap);
353
315
inline void use_all_columns()
355
317
column_bitmaps_set(&s->all_set, &s->all_set);
435
345
void setup_table_map(TableList *table_list, uint32_t tablenr);
436
346
inline void mark_as_null_row()
439
status|= STATUS_NULL_ROW;
349
status|=STATUS_NULL_ROW;
440
350
memset(null_flags, 255, s->null_bytes);
443
bool renameAlterTemporaryTable(TableIdentifier &identifier);
444
void free_io_cache();
445
void filesort_free_buffers(bool full= false);
446
void intern_close_table();
448
void print_error(int error, myf errflag)
450
s->storage_engine->print_error(error, errflag, *this);
455
key if error because of duplicated keys
457
uint32_t get_dup_key(int error)
459
cursor->errkey = (uint32_t) -1;
460
if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
461
error == HA_ERR_FOUND_DUPP_UNIQUE ||
462
error == HA_ERR_DROP_INDEX_FK)
463
cursor->info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
465
return(cursor->errkey);
469
This is a short term fix. Long term we will used the TableIdentifier to do the actual comparison.
471
bool operator<(const Table &right) const
473
int result= strcasecmp(this->getShare()->getSchemaName(), right.getShare()->getSchemaName());
481
result= strcasecmp(this->getShare()->getTableName(), right.getShare()->getTableName());
489
if (this->getShare()->getTableProto()->type() < right.getShare()->getTableProto()->type())
495
static bool compare(const Table *a, const Table *b)
500
friend std::ostream& operator<<(std::ostream& output, const Table &table)
503
output << table.getShare()->getSchemaName();
505
output << table.getShare()->getTableName();
507
output << table.getShare()->getTableTypeAsString();
510
return output; // for multiple << operators.
515
Table *create_virtual_tmp_table(Session *session, List<CreateField> &field_list);
355
Table *create_virtual_tmp_table(Session *session,
356
List<Create_field> &field_list);
517
358
typedef struct st_foreign_key_info
526
367
List<LEX_STRING> referenced_fields;
527
368
} FOREIGN_KEY_INFO;
370
typedef struct st_field_info
373
This is used as column name.
375
const char* field_name;
377
For string-type columns, this is the maximum number of
378
characters. Otherwise, it is the 'display-length' for the column.
380
uint32_t field_length;
382
This denotes data type for the column. For the most part, there seems to
383
be one entry in the enum for each SQL data type, although there seem to
384
be a number of additional entries in the enum.
386
enum enum_field_types field_type;
389
This is used to set column attributes. By default, columns are @c NOT
390
@c NULL and @c SIGNED, and you can deviate from the default
391
by setting the appopriate flags. You can use either one of the flags
392
@c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
393
combine them using the bitwise or operator @c |. Both flags are
396
uint32_t field_flags; // Field atributes(maybe_null, signed, unsigned etc.)
397
const char* old_name;
399
This should be one of @c SKIP_OPEN_TABLE,
400
@c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
402
uint32_t open_method;
407
typedef class Item COND;
409
struct InfoSchemaTable
411
const char* table_name;
412
ST_FIELD_INFO *fields_info;
413
/* Create information_schema table */
414
Table *(*create_table) (Session *session, TableList *table_list);
415
/* Fill table with data */
416
int (*fill_table) (Session *session, TableList *tables, COND *cond);
417
/* Handle fileds for old SHOW */
418
int (*old_format) (Session *session, struct InfoSchemaTable *schema_table);
419
int (*process_table) (Session *session, TableList *tables, Table *table,
420
bool res, LEX_STRING *db_name, LEX_STRING *table_name);
421
int idx_field1, idx_field2;
423
uint32_t i_s_requested_object; /* the object we need to open(Table | VIEW) */
533
427
#define JOIN_TYPE_LEFT 1
534
428
#define JOIN_TYPE_RIGHT 2
537
431
class select_union;
538
432
class Tmp_Table_Param;
540
struct open_table_list_st
547
open_table_list_st() :
554
TableShare *alloc_table_share(TableList *table_list, char *key,
555
uint32_t key_length);
556
int open_table_def(Session& session, TableIdentifier &identifier, TableShare *share);
557
void open_table_error(TableShare *share, int error, int db_errno, int errarg);
558
int open_table_from_share(Session *session, TableShare *share, const char *alias,
559
uint32_t db_stat, uint32_t ha_open_flags,
561
void free_blobs(Table *table);
562
int set_zone(int nr,int min_zone,int max_zone);
563
uint32_t convert_period_to_month(uint32_t period);
564
uint32_t convert_month_to_period(uint32_t month);
566
int test_if_number(char *str,int *res,bool allow_wildcards);
567
void change_byte(unsigned char *,uint,char,char);
569
namespace optimizer { class SqlSelect; }
571
ha_rows filesort(Session *session,
573
st_sort_field *sortorder,
575
optimizer::SqlSelect *select,
578
ha_rows *examined_rows);
580
void filesort_free_buffers(Table *table, bool full);
581
void change_double_for_sort(double nr,unsigned char *to);
582
double my_double_round(double value, int64_t dec, bool dec_unsigned,
584
int get_quick_record(optimizer::SqlSelect *select);
586
void find_date(char *pos,uint32_t *vek,uint32_t flag);
587
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
588
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
589
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
590
ulong next_io_size(ulong pos);
591
void append_unescaped(String *res, const char *pos, uint32_t length);
593
int rename_file_ext(const char * from,const char * to,const char * ext);
594
bool check_column_name(const char *name);
595
bool check_db_name(SchemaIdentifier &schema);
596
bool check_table_name(const char *name, uint32_t length);
598
} /* namespace drizzled */
434
typedef struct st_changed_table_list
436
struct st_changed_table_list *next;
442
typedef struct st_open_table_list
444
struct st_open_table_list *next;
446
uint32_t in_use,locked;
600
450
#endif /* DRIZZLED_TABLE_H */