23
23
#ifndef DRIZZLED_TABLE_H
24
24
#define DRIZZLED_TABLE_H
27
#include <boost/dynamic_bitset.hpp>
29
#include "drizzled/order.h"
30
#include "drizzled/filesort_info.h"
31
#include "drizzled/natural_join_column.h"
32
#include "drizzled/field_iterator.h"
33
#include "drizzled/cursor.h"
34
#include "drizzled/lex_string.h"
35
#include "drizzled/table_list.h"
36
#include "drizzled/definition/table.h"
37
#include "drizzled/atomics.h"
38
#include "drizzled/query_id.h"
26
#include <plugin/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>
44
38
class Item_subselect;
45
39
class Select_Lex_Unit;
48
class SecurityContext;
42
class Security_context;
45
/*************************************************************************/
50
48
class Field_timestamp;
53
extern uint64_t refresh_version;
55
51
typedef enum enum_table_category TABLE_CATEGORY;
56
typedef struct st_columndef MI_COLUMNDEF;
59
* Class representing a set of records, either in a temporary,
60
* normal, or derived table.
53
TABLE_CATEGORY get_table_category(const LEX_STRING *db,
54
const LEX_STRING *name);
57
extern uint32_t refresh_version;
59
typedef struct st_table_field_w_type
64
Field **field; /**< Pointer to fields collection */
67
bool create_myisam_from_heap(Session *session, Table *table,
68
MI_COLUMNDEF *start_recinfo,
69
MI_COLUMNDEF **recinfo,
70
int error, bool ignore_last_dupp_key_error);
67
Field **getFields() const
72
Field *getField(uint32_t arg) const
77
void setFields(Field **arg)
82
void setFieldAt(Field *arg, uint32_t arg_pos)
87
Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
76
Field **field; /* Pointer to fields */
91
Table *getNext() const
101
void setNext(Table *arg)
108
getNext()->setPrev(getPrev()); /* remove from used chain */
109
getPrev()->setNext(getNext());
115
Table *getPrev() const
125
void setPrev(Table *arg)
130
boost::dynamic_bitset<> *read_set; /* Active column sets */
131
boost::dynamic_bitset<> *write_set; /* Active column sets */
134
uint32_t db_stat; /**< information about the cursor as in Cursor.h */
136
boost::dynamic_bitset<> def_read_set; /**< Default read set of columns */
137
boost::dynamic_bitset<> def_write_set; /**< Default write set of columns */
138
boost::dynamic_bitset<> tmp_set; /* Not sure about this... */
140
Session *in_use; /**< Pointer to the current session using this object */
141
Session *getSession()
146
unsigned char *getInsertRecord()
151
unsigned char *getUpdateRecord()
156
unsigned char *record[2]; /**< Pointer to "records" */
157
std::vector<unsigned char> insert_values; /* used by INSERT ... UPDATE */
158
KeyInfo *key_info; /**< data of keys in database */
159
Field *next_number_field; /**< Set if next_number is activated. @TODO What the heck is the difference between this and the next member? */
160
Field *found_next_number_field; /**< Points to the "next-number" field (autoincrement field) */
161
Field_timestamp *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
163
TableList *pos_in_table_list; /* Element referring to this table */
166
const char *getAlias() const
168
return _alias.c_str();
176
void setAlias(const char *arg)
182
std::string _alias; /**< alias or table name if no alias */
185
unsigned char *null_flags;
187
uint32_t lock_position; /**< Position in DRIZZLE_LOCK.table */
188
uint32_t lock_data_start; /**< Start pos. in DRIZZLE_LOCK.locks */
189
uint32_t lock_count; /**< Number of locks */
82
MY_BITMAP *read_set; /* Active column sets */
83
MY_BITMAP *write_set; /* Active column sets */
86
uint32_t db_stat; /* mode of file as in handler.h */
88
my_bitmap_map *bitmap_init_value;
89
MY_BITMAP def_read_set, def_write_set, tmp_set; /* containers */
91
Session *in_use; /* Which thread uses this */
93
unsigned char *record[2]; /* Pointer to records */
94
unsigned char *write_row_record; /* Used as optimisation in
96
unsigned char *insert_values; /* used by INSERT ... UPDATE */
97
KEY *key_info; /* data of keys in database */
98
Field *next_number_field; /* Set if next_number is activated */
99
Field *found_next_number_field; /* Set on open */
100
Field_timestamp *timestamp_field;
102
TableList *pos_in_table_list;/* Element referring to this table */
104
const char *alias; /* alias or table name */
105
unsigned char *null_flags;
107
uint32_t lock_position; /* Position in DRIZZLE_LOCK.table */
108
uint32_t lock_data_start; /* Start pos. in DRIZZLE_LOCK.locks */
109
uint32_t lock_count; /* Number of locks */
190
110
uint32_t used_fields;
191
uint32_t status; /* What's in getInsertRecord() */
111
uint32_t temp_pool_slot; /* Used by intern temp tables */
112
uint32_t status; /* What's in record[0] */
192
113
/* number of select if it is derived table */
193
uint32_t derived_select_number;
194
int current_lock; /**< Type of lock on table */
195
bool copy_blobs; /**< Should blobs by copied when storing? */
114
uint32_t derived_select_number;
115
int current_lock; /* Type of lock on table */
116
bool copy_blobs; /* copy_blobs when storing */
198
119
0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
317
242
For each key that has quick_keys.test(key) == true: estimate of #records
318
243
and max #key parts that range access would use.
320
ha_rows quick_rows[MAX_KEY];
245
ha_rows quick_rows[MAX_KEY];
322
247
/* Bitmaps of key parts that =const for the entire join. */
323
248
key_part_map const_key_parts[MAX_KEY];
325
uint32_t quick_key_parts[MAX_KEY];
326
uint32_t quick_n_ranges[MAX_KEY];
329
memory::Root mem_root;
333
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
336
memory::Root *getMemRoot()
338
if (not mem_root.alloc_root_inited())
346
void *alloc_root(size_t arg)
348
if (not mem_root.alloc_root_inited())
353
return mem_root.alloc_root(arg);
356
char *strmake_root(const char *str_arg, size_t len_arg)
358
if (not mem_root.alloc_root_inited())
363
return mem_root.strmake_root(str_arg, len_arg);
371
int report_error(int error);
373
* Free information allocated by openfrm
375
* @param If true if we also want to free table_share
376
* @note this should all be the destructor
378
int delete_table(bool free_share= false);
380
void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
250
uint32_t quick_key_parts[MAX_KEY];
251
uint32_t quick_n_ranges[MAX_KEY];
254
filesort_info_st sort;
258
: s(NULL), field(NULL),
259
file(NULL), next(NULL), prev(NULL),
260
read_set(NULL), write_set(NULL),
261
tablenr(0), db_stat(0),
262
bitmap_init_value(NULL),
263
/* TODO: ensure that MY_BITMAP has a constructor for def_read_set, def_write_set and tmp_set */
265
write_row_record(NULL), insert_values(NULL), key_info(NULL),
266
next_number_field(NULL), found_next_number_field(NULL),
267
timestamp_field(NULL),
268
pos_in_table_list(NULL), group(NULL), alias(NULL), null_flags(NULL),
269
lock_position(0), lock_data_start(0), lock_count(0),
270
used_fields(0), temp_pool_slot(0),
271
status(0), derived_select_number(0), current_lock(F_UNLCK),
272
copy_blobs(false), maybe_null(false), null_row(false),
273
force_index(false), distinct(false), const_table(false),
274
no_rows(false), key_read(false), no_keyread(false),
275
open_placeholder(false), locked_by_name(false), no_cache(false),
276
auto_increment_field_not_null(false), insert_or_update(false),
277
alias_name_used(false), get_fields_in_item_tree(false),
278
query_id(0), quick_condition_rows(0),
279
timestamp_field_type(TIMESTAMP_NO_AUTO_SET), map(0)
281
memset(record, 0, sizeof(unsigned char *) * 2);
283
covering_keys.reset();
288
keys_in_use_for_query.reset();
289
keys_in_use_for_group_by.reset();
290
keys_in_use_for_order_by.reset();
292
memset(quick_rows, 0, sizeof(query_id_t) * MAX_KEY);
293
memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
295
memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
296
memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
298
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
299
memset(&sort, 0, sizeof(filesort_info_st));
302
void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg)
316
db_stat= db_stat_arg;
318
bitmap_init_value= NULL;
321
memset(record, 0, sizeof(unsigned char *) * 2);
323
write_row_record= NULL;
326
next_number_field= NULL;
327
found_next_number_field= NULL;
328
timestamp_field= NULL;
330
pos_in_table_list= NULL;
341
derived_select_number= 0;
342
current_lock= F_UNLCK;
356
open_placeholder= false;
357
locked_by_name= false;
360
auto_increment_field_not_null= false;
361
insert_or_update= false;
362
alias_name_used= false;
363
get_fields_in_item_tree= false;
366
quick_condition_rows= 0;
368
timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
373
covering_keys.reset();
378
keys_in_use_for_query.reset();
379
keys_in_use_for_group_by.reset();
380
keys_in_use_for_order_by.reset();
382
memset(quick_rows, 0, sizeof(query_id_t) * MAX_KEY);
383
memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
385
memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
386
memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
388
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
389
memset(&sort, 0, sizeof(filesort_info_st));
382
394
/* SHARE methods */
383
virtual const TableShare *getShare() const= 0; /* Get rid of this long term */
384
virtual TableShare *getMutableShare()= 0; /* Get rid of this long term */
385
virtual bool hasShare() const= 0; /* Get rid of this long term */
386
virtual void setShare(TableShare *new_share)= 0; /* Get rid of this long term */
388
virtual void release(void)= 0;
390
uint32_t sizeKeys() { return getMutableShare()->sizeKeys(); }
391
uint32_t sizeFields() { return getMutableShare()->sizeFields(); }
392
uint32_t getRecordLength() const { return getShare()->getRecordLength(); }
393
uint32_t sizeBlobFields() { return getMutableShare()->blob_fields; }
394
uint32_t *getBlobField() { return &getMutableShare()->blob_field[0]; }
397
virtual bool hasVariableWidth() const
399
return getShare()->hasVariableWidth(); // We should calculate this.
402
virtual void setVariableWidth(void);
404
Field_blob *getBlobFieldAt(uint32_t arg) const
406
if (arg < getShare()->blob_fields)
407
return (Field_blob*) field[getShare()->blob_field[arg]]; /*NOTE: Using 'Table.field' NOT SharedTable.field. */
411
inline uint8_t getBlobPtrSize() { return getShare()->blob_ptr_size; }
412
inline uint32_t getNullBytes() { return getShare()->null_bytes; }
413
inline uint32_t getNullFields() { return getShare()->null_fields; }
414
inline unsigned char *getDefaultValues() { return getMutableShare()->getDefaultValues(); }
415
inline const char *getSchemaName() const { return getShare()->getSchemaName(); }
416
inline const char *getTableName() const { return getShare()->getTableName(); }
418
inline bool isDatabaseLowByteFirst() { return getShare()->db_low_byte_first; } /* Portable row format */
419
inline bool isNameLock() const { return open_placeholder; }
421
uint32_t index_flags(uint32_t idx) const
423
return getShare()->storage_engine->index_flags(getShare()->getKeyInfo(idx).algorithm);
426
inline plugin::StorageEngine *getEngine() const /* table_type for handler */
428
return getShare()->storage_engine;
431
Cursor &getCursor() const /* table_type for handler */
395
inline TableShare *getShare() { return s; } /* Get rid of this long term */
396
inline void setShare(TableShare *new_share) { s= new_share; } /* Get rid of this long term */
397
inline uint32_t sizeKeys() { return s->keys; }
398
inline uint32_t sizeFields() { return s->fields; }
399
inline uint32_t getRecordLength() { return s->reclength; }
400
inline uint32_t sizeBlobFields() { return s->blob_fields; }
401
inline uint32_t *getBlobField() { return s->blob_field; }
402
inline uint32_t getNullBytes() { return s->null_bytes; }
403
inline uint32_t getNullFields() { return s->null_fields; }
404
inline unsigned char *getDefaultValues() { return s->default_values; }
406
inline bool isDatabaseLowByteFirst() { return s->db_low_byte_first; } /* Portable row format */
407
inline bool isCrashed() { return s->crashed; }
408
inline bool isNameLock() { return s->name_lock; }
409
inline bool isReplaceWithNameLock() { return s->replace_with_name_lock; }
410
inline bool isWaitingOnCondition() { return s->waiting_on_cond; } /* Protection against free */
412
/* For TMP tables, should be pulled out as a class */
413
void updateCreateInfo(HA_CREATE_INFO *create_info);
414
void setup_tmp_table_column_bitmaps(unsigned char *bitmaps);
415
bool create_myisam_tmp_table(KEY *keyinfo,
416
MI_COLUMNDEF *start_recinfo,
417
MI_COLUMNDEF **recinfo,
419
void free_tmp_table(Session *session);
420
bool open_tmp_table();
437
421
size_t max_row_length(const unsigned char *data);
438
422
uint32_t find_shortest_key(const key_map *usable_keys);
439
423
bool compare_record(Field **ptr);
440
bool records_are_comparable();
441
bool compare_records();
424
bool compare_record();
442
425
/* TODO: the (re)storeRecord's may be able to be further condensed */
443
426
void storeRecord();
444
427
void storeRecordAsInsert();
569
515
void setup_table_map(TableList *table_list, uint32_t tablenr);
570
516
inline void mark_as_null_row()
573
status|= STATUS_NULL_ROW;
574
memset(null_flags, 255, getShare()->null_bytes);
577
void free_io_cache();
578
void filesort_free_buffers(bool full= false);
579
void intern_close_table();
581
void print_error(int error, myf errflag)
583
getShare()->storage_engine->print_error(error, errflag, *this);
588
key if error because of duplicated keys
590
uint32_t get_dup_key(int error)
592
cursor->errkey = (uint32_t) -1;
593
if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
594
error == HA_ERR_FOUND_DUPP_UNIQUE ||
595
error == HA_ERR_DROP_INDEX_FK)
596
cursor->info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
598
return(cursor->errkey);
602
This is a short term fix. Long term we will used the TableIdentifier to do the actual comparison.
604
bool operator<(const Table &right) const
606
return getShare()->getCacheKey() < right.getShare()->getCacheKey();
609
static bool compare(const Table *a, const Table *b)
614
friend std::ostream& operator<<(std::ostream& output, const Table &table)
616
if (table.getShare())
619
output << table.getShare()->getSchemaName();
621
output << table.getShare()->getTableName();
623
output << table.getShare()->getTableTypeAsString();
628
output << "Table:(has no share)";
631
return output; // for multiple << operators.
635
virtual bool isPlaceHolder(void) const
646
* This class defines the information for foreign keys.
653
* This is the constructor with all properties set.
655
* @param[in] in_foreign_id The id of the foreign key
656
* @param[in] in_referenced_db The referenced database name of the foreign key
657
* @param[in] in_referenced_table The referenced table name of the foreign key
658
* @param[in] in_update_method The update method of the foreign key.
659
* @param[in] in_delete_method The delete method of the foreign key.
660
* @param[in] in_referenced_key_name The name of referenced key
661
* @param[in] in_foreign_fields The foreign fields
662
* @param[in] in_referenced_fields The referenced fields
664
ForeignKeyInfo(LEX_STRING *in_foreign_id,
665
LEX_STRING *in_referenced_db,
666
LEX_STRING *in_referenced_table,
667
LEX_STRING *in_update_method,
668
LEX_STRING *in_delete_method,
669
LEX_STRING *in_referenced_key_name,
670
List<LEX_STRING> in_foreign_fields,
671
List<LEX_STRING> in_referenced_fields)
673
foreign_id(in_foreign_id),
674
referenced_db(in_referenced_db),
675
referenced_table(in_referenced_table),
676
update_method(in_update_method),
677
delete_method(in_delete_method),
678
referenced_key_name(in_referenced_key_name),
679
foreign_fields(in_foreign_fields),
680
referenced_fields(in_referenced_fields)
685
* This is the default constructor. All properties are set to default values for their types.
688
: foreign_id(NULL), referenced_db(NULL), referenced_table(NULL),
689
update_method(NULL), delete_method(NULL), referenced_key_name(NULL)
694
* Gets the foreign id.
696
* @ retval the foreign id
698
const LEX_STRING *getForeignId() const
705
* Gets the name of the referenced database.
707
* @ retval the name of the referenced database
709
const LEX_STRING *getReferencedDb() const
711
return referenced_db;
716
* Gets the name of the referenced table.
718
* @ retval the name of the referenced table
720
const LEX_STRING *getReferencedTable() const
722
return referenced_table;
727
* Gets the update method.
729
* @ retval the update method
731
const LEX_STRING *getUpdateMethod() const
733
return update_method;
738
* Gets the delete method.
740
* @ retval the delete method
742
const LEX_STRING *getDeleteMethod() const
744
return delete_method;
749
* Gets the name of the referenced key.
751
* @ retval the name of the referenced key
753
const LEX_STRING *getReferencedKeyName() const
755
return referenced_key_name;
760
* Gets the foreign fields.
762
* @ retval the foreign fields
764
const List<LEX_STRING> &getForeignFields() const
766
return foreign_fields;
771
* Gets the referenced fields.
773
* @ retval the referenced fields
775
const List<LEX_STRING> &getReferencedFields() const
777
return referenced_fields;
783
LEX_STRING *foreign_id;
785
* The name of the reference database.
787
LEX_STRING *referenced_db;
789
* The name of the reference table.
791
LEX_STRING *referenced_table;
795
LEX_STRING *update_method;
799
LEX_STRING *delete_method;
801
* The name of the referenced key.
803
LEX_STRING *referenced_key_name;
805
* The foreign fields.
807
List<LEX_STRING> foreign_fields;
809
* The referenced fields.
811
List<LEX_STRING> referenced_fields;
519
status|=STATUS_NULL_ROW;
520
memset(null_flags, 255, s->null_bytes);
525
Table *create_virtual_tmp_table(Session *session,
526
List<Create_field> &field_list);
528
typedef struct st_foreign_key_info
530
LEX_STRING *forein_id;
531
LEX_STRING *referenced_db;
532
LEX_STRING *referenced_table;
533
LEX_STRING *update_method;
534
LEX_STRING *delete_method;
535
LEX_STRING *referenced_key_name;
536
List<LEX_STRING> foreign_fields;
537
List<LEX_STRING> referenced_fields;
540
typedef struct st_field_info
543
This is used as column name.
545
const char* field_name;
547
For string-type columns, this is the maximum number of
548
characters. Otherwise, it is the 'display-length' for the column.
550
uint32_t field_length;
552
This denotes data type for the column. For the most part, there seems to
553
be one entry in the enum for each SQL data type, although there seem to
554
be a number of additional entries in the enum.
556
enum enum_field_types field_type;
559
This is used to set column attributes. By default, columns are @c NOT
560
@c NULL and @c SIGNED, and you can deviate from the default
561
by setting the appopriate flags. You can use either one of the flags
562
@c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
563
combine them using the bitwise or operator @c |. Both flags are
566
uint32_t field_flags; // Field atributes(maybe_null, signed, unsigned etc.)
567
const char* old_name;
569
This should be one of @c SKIP_OPEN_TABLE,
570
@c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
572
uint32_t open_method;
816
#define JOIN_TYPE_LEFT 1
817
#define JOIN_TYPE_RIGHT 2
577
typedef class Item COND;
579
struct InfoSchemaTable
581
const char* table_name;
582
ST_FIELD_INFO *fields_info;
583
/* Create information_schema table */
584
Table *(*create_table) (Session *session, TableList *table_list);
585
/* Fill table with data */
586
int (*fill_table) (Session *session, TableList *tables, COND *cond);
587
/* Handle fileds for old SHOW */
588
int (*old_format) (Session *session, struct InfoSchemaTable *schema_table);
589
int (*process_table) (Session *session, TableList *tables, Table *table,
590
bool res, LEX_STRING *db_name, LEX_STRING *table_name);
591
int idx_field1, idx_field2;
593
uint32_t i_s_requested_object; /* the object we need to open(Table | VIEW) */
597
#define JOIN_TYPE_LEFT 1
598
#define JOIN_TYPE_RIGHT 2
820
601
class select_union;
821
602
class Tmp_Table_Param;
823
void free_blobs(Table *table);
824
int set_zone(int nr,int min_zone,int max_zone);
825
uint32_t convert_period_to_month(uint32_t period);
826
uint32_t convert_month_to_period(uint32_t month);
828
int test_if_number(char *str,int *res,bool allow_wildcards);
829
void change_byte(unsigned char *,uint,char,char);
831
namespace optimizer { class SqlSelect; }
833
void change_double_for_sort(double nr,unsigned char *to);
834
double my_double_round(double value, int64_t dec, bool dec_unsigned,
836
int get_quick_record(optimizer::SqlSelect *select);
838
void find_date(char *pos,uint32_t *vek,uint32_t flag);
839
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
840
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
841
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
842
void append_unescaped(String *res, const char *pos, uint32_t length);
844
int rename_file_ext(const char * from,const char * to,const char * ext);
845
bool check_column_name(const char *name);
846
bool check_db_name(Session *session, SchemaIdentifier &schema);
847
bool check_table_name(const char *name, uint32_t length);
849
} /* namespace drizzled */
851
#include "drizzled/table/instance.h"
852
#include "drizzled/table/concurrent.h"
604
typedef struct st_changed_table_list
606
struct st_changed_table_list *next;
612
typedef struct st_open_table_list
614
struct st_open_table_list *next;
617
uint32_t in_use,locked;
854
621
#endif /* DRIZZLED_TABLE_H */