drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/table.h
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, Inc.
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#pragma once
22
23#include <string>
24#include <boost/dynamic_bitset.hpp>
25
26#include <drizzled/order.h>
27#include <drizzled/filesort_info.h>
28#include <drizzled/natural_join_column.h>
29#include <drizzled/field_iterator.h>
30#include <drizzled/cursor.h>
31#include <drizzled/lex_string.h>
32#include <drizzled/table/instance.h>
33#include <drizzled/atomics.h>
34
35#include <drizzled/visibility.h>
36
37namespace drizzled {
38
39 /* Bits in form->status */
40#define STATUS_NO_RECORD (1+2) /* Record isn't usably */
41#define STATUS_GARBAGE 1
42#define STATUS_NOT_FOUND 2 /* No record in database when needed */
43#define STATUS_NO_PARENT 4 /* Parent record wasn't found */
44#define STATUS_NULL_ROW 32 /* table->null_row is set */
45
46
47class RegInfo
48{
49public: /* Extra info about reg */
50 JoinTable *join_tab; /* Used by SELECT() */
51 enum thr_lock_type lock_type; /* How database is used */
52 bool not_exists_optimize;
53 bool impossible_range;
54 RegInfo()
55 : join_tab(NULL), lock_type(TL_UNLOCK),
56 not_exists_optimize(false), impossible_range(false) {}
57 void reset()
58 {
59 join_tab= NULL;
60 lock_type= TL_UNLOCK;
61 not_exists_optimize= false;
62 impossible_range= false;
63 }
64};
65
70class DRIZZLED_API Table
71{
73
74public:
75 Field **getFields() const
76 {
77 return field;
78 }
79
80 Field *getField(uint32_t arg) const
81 {
82 return field[arg];
83 }
84
85 void setFields(Field **arg)
86 {
87 field= arg;
88 }
89
90 void setFieldAt(Field *arg, uint32_t arg_pos)
91 {
92 field[arg_pos]= arg;
93 }
94
96
97private:
98 Table *next;
99
100public:
101 Table *getNext() const
102 {
103 return next;
104 }
105
106 Table **getNextPtr()
107 {
108 return &next;
109 }
110
111 void setNext(Table *arg)
112 {
113 next= arg;
114 }
115
116 void unlink()
117 {
118 getNext()->setPrev(getPrev()); /* remove from used chain */
119 getPrev()->setNext(getNext());
120 }
121
122private:
123 Table *prev;
124public:
125 Table *getPrev() const
126 {
127 return prev;
128 }
129
130 Table **getPrevPtr()
131 {
132 return &prev;
133 }
134
135 void setPrev(Table *arg)
136 {
137 prev= arg;
138 }
139
140 boost::dynamic_bitset<> *read_set; /* Active column sets */
141 boost::dynamic_bitset<> *write_set; /* Active column sets */
142
143 uint32_t tablenr;
144 uint32_t db_stat;
145
146 boost::dynamic_bitset<> def_read_set;
147 boost::dynamic_bitset<> def_write_set;
148 boost::dynamic_bitset<> tmp_set; /* Not sure about this... */
149
151 Session *getSession()
152 {
153 return in_use;
154 }
155
156 unsigned char *getInsertRecord() const
157 {
158 return record[0];
159 }
160
161 unsigned char *getUpdateRecord()
162 {
163 return record[1];
164 }
165
166 unsigned char *record[2];
167 std::vector<unsigned char> insert_values; /* used by INSERT ... UPDATE */
172
173 TableList *pos_in_table_list; /* Element referring to this table */
174 Order *group;
175
176 const char *getAlias() const
177 {
178 return _alias.c_str();
179 }
180
181 void clearAlias()
182 {
183 _alias.clear();
184 }
185
186 void setAlias(const char *arg)
187 {
188 _alias= arg;
189 }
190
191private:
192 std::string _alias;
193public:
194
195 unsigned char *null_flags;
196
197 uint32_t lock_position;
199 uint32_t lock_count;
200 uint32_t used_fields;
201 uint32_t status; /* What's in getInsertRecord() */
202 /* number of select if it is derived table */
203 uint32_t derived_select_number;
206
207 /*
208 0 or JOIN_TYPE_{LEFT|RIGHT}. Currently this is only compared to 0.
209 If maybe_null !=0, this table is inner w.r.t. some outer join operation,
210 and null_row may be true.
211 */
212 bool maybe_null;
213
214 /*
215 If true, the current table row is considered to have all columns set to
216 NULL, including columns declared as "not null" (see maybe_null).
217 */
218 bool null_row;
219
220 bool force_index;
221 bool distinct;
222 bool const_table;
223 bool no_rows;
224 bool key_read;
225 bool no_keyread;
226 /*
227 Placeholder for an open table which prevents other connections
228 from taking name-locks on this table. Typically used with
229 TableShare::version member to take an exclusive name-lock on
230 this table name -- a name lock that not only prevents other
231 threads from opening the table, but also blocks other name
232 locks. This is achieved by:
233 - setting open_placeholder to 1 - this will block other name
234 locks, as wait_for_locked_table_name will be forced to wait,
235 see table_is_used for details.
236 - setting version to 0 - this will force other threads to close
237 the instance of this table and wait (this is the same approach
238 as used for usual name locks).
239 An exclusively name-locked table currently can have no Cursor
240 object associated with it (db_stat is always 0), but please do
241 not rely on that.
242 */
243 bool open_placeholder;
244 bool locked_by_name;
245 bool no_cache;
246 /*
247 To indicate that a non-null value of the auto_increment field
248 was provided by the user or retrieved from the current record.
249 Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode.
250 */
251 bool auto_increment_field_not_null;
252 bool alias_name_used; /* true if table_name is alias */
253
254 /*
255 The ID of the query that opened and is using this table. Has different
256 meanings depending on the table type.
257
258 Temporary tables:
259
260 table->query_id is set to session->query_id for the duration of a statement
261 and is reset to 0 once it is closed by the same statement. A non-zero
262 table->query_id means that a statement is using the table even if it's
263 not the current statement (table is in use by some outer statement).
264
265 Non-temporary tables:
266
267 Under pre-locked or LOCK TABLES mode: query_id is set to session->query_id
268 for the duration of a statement and is reset to 0 once it is closed by
269 the same statement. A non-zero query_id is used to control which tables
270 in the list of pre-opened and locked tables are actually being used.
271 */
272 query_id_t query_id;
273
283
284 /*
285 If this table has TIMESTAMP field with auto-set property (pointed by
286 timestamp_field member) then this variable indicates during which
287 operations (insert only/on update/in both cases) we should set this
288 field to current timestamp. If there are no such field in this table
289 or we should not automatically set its value during execution of current
290 statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
291
292 Value of this variable is set for each statement in open_table() and
293 if needed cleared later in statement processing code (see update_query()
294 as example).
295 */
296 timestamp_auto_set_type timestamp_field_type;
297 table_map map;
298
299 RegInfo reginfo; /* field connections */
300
301 /*
302 Map of keys that can be used to retrieve all data from this table
303 needed by the query without reading the row.
304 */
305 key_map covering_keys;
306 key_map quick_keys;
307 key_map merge_keys;
308
309 /*
310 A set of keys that can be used in the query that references this
311 table.
312
313 All indexes disabled on the table's TableShare (see Table::s) will be
314 subtracted from this set upon instantiation. Thus for any Table t it holds
315 that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
316 must not introduce any new keys here (see setup_tables).
317
318 The set is implemented as a bitmap.
319 */
320 key_map keys_in_use_for_query;
321
322 /* Map of keys that can be used to calculate GROUP BY without sorting */
323 key_map keys_in_use_for_group_by;
324
325 /* Map of keys that can be used to calculate ORDER BY without sorting */
326 key_map keys_in_use_for_order_by;
327
328 /*
329 For each key that has quick_keys.test(key) == true: estimate of #records
330 and max #key parts that range access would use.
331 */
332 ha_rows quick_rows[MAX_KEY];
333
334 /* Bitmaps of key parts that =const for the entire join. */
335 key_part_map const_key_parts[MAX_KEY];
336
337 uint32_t quick_key_parts[MAX_KEY];
338 uint32_t quick_n_ranges[MAX_KEY];
339
340private:
341 memory::Root mem_root;
342
343 void init_mem_root()
344 {
345 if (not mem_root.alloc_root_inited())
346 mem_root.init(TABLE_ALLOC_BLOCK_SIZE);
347 }
348public:
349 memory::Root& mem()
350 {
351 init_mem_root();
352 return mem_root;
353 }
354
355 unsigned char* alloc(size_t arg)
356 {
357 init_mem_root();
358 return mem_root.alloc(arg);
359 }
360
361 char* strdup(const char* str_arg, size_t len_arg)
362 {
363 init_mem_root();
364 return mem_root.strdup(str_arg, len_arg);
365 }
366
367 filesort_info sort;
368
369 Table();
370 virtual ~Table();
371
372 int report_error(int error);
379 int delete_table(bool free_share= false);
380
381 void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
382
383 /* SHARE methods */
384 virtual const TableShare *getShare() const= 0; /* Get rid of this long term */
385 virtual TableShare *getMutableShare()= 0; /* Get rid of this long term */
386 virtual bool hasShare() const= 0; /* Get rid of this long term */
387 virtual void setShare(TableShare *new_share)= 0; /* Get rid of this long term */
388
389 virtual void release(void)= 0;
390
391 uint32_t sizeKeys() { return getMutableShare()->sizeKeys(); }
392 uint32_t sizeFields() { return getMutableShare()->sizeFields(); }
393 uint32_t getRecordLength() const { return getShare()->getRecordLength(); }
394 uint32_t sizeBlobFields() { return getMutableShare()->blob_fields; }
395 uint32_t *getBlobField() { return &getMutableShare()->blob_field[0]; }
396
397public:
398 virtual bool hasVariableWidth() const
399 {
400 return getShare()->hasVariableWidth(); // We should calculate this.
401 }
402
403 virtual void setVariableWidth(void);
404
405 Field_blob *getBlobFieldAt(uint32_t arg) const
406 {
407 if (arg < getShare()->blob_fields)
408 return (Field_blob*) field[getShare()->blob_field[arg]]; /*NOTE: Using 'Table.field' NOT SharedTable.field. */
409
410 return NULL;
411 }
412 inline uint8_t getBlobPtrSize() const { return getShare()->sizeBlobPtr(); }
413 inline uint32_t getNullBytes() const { return getShare()->null_bytes; }
414 inline uint32_t getNullFields() const { return getShare()->null_fields; }
415 inline unsigned char *getDefaultValues() { return getMutableShare()->getDefaultValues(); }
416 inline const char *getSchemaName() const { return getShare()->getSchemaName(); }
417 inline const char *getTableName() const { return getShare()->getTableName(); }
418
419 inline bool isDatabaseLowByteFirst() const { return getShare()->db_low_byte_first; } /* Portable row format */
420 inline bool isNameLock() const { return open_placeholder; }
421
422 uint32_t index_flags(uint32_t idx) const;
423
424 inline plugin::StorageEngine *getEngine() const /* table_type for handler */
425 {
426 return getShare()->getEngine();
427 }
428
429 Cursor &getCursor() const /* table_type for handler */
430 {
431 assert(cursor);
432 return *cursor;
433 }
434
435 size_t max_row_length(const unsigned char *data);
436 uint32_t find_shortest_key(const key_map *usable_keys);
437 bool compare_record(Field **ptr);
438 bool records_are_comparable();
439 bool compare_records();
440 /* TODO: the (re)storeRecord's may be able to be further condensed */
441 void storeRecord();
442 void storeRecordAsInsert();
443 void storeRecordAsDefault();
444 void restoreRecord();
445 void restoreRecordAsDefault();
446 void emptyRecord();
447
448
449 /* See if this can be blown away */
450 inline uint32_t getDBStat () const { return db_stat; }
451
468 void fill_item_list(List<Item>&) const;
469 void clear_column_bitmaps(void);
470 void prepare_for_position(void);
471 void mark_columns_used_by_index_no_reset(uint32_t index, boost::dynamic_bitset<>& bitmap);
472 void mark_columns_used_by_index_no_reset(uint32_t index);
473 void mark_columns_used_by_index(uint32_t index);
474 void restore_column_maps_after_mark_index();
475 void mark_auto_increment_column(void);
476 void mark_columns_needed_for_update(void);
477 void mark_columns_needed_for_delete(void);
478 void mark_columns_needed_for_insert(void);
479 void column_bitmaps_set(boost::dynamic_bitset<>& read_set_arg,
480 boost::dynamic_bitset<>& write_set_arg);
481
482 void restore_column_map(const boost::dynamic_bitset<>& old);
483
484 const boost::dynamic_bitset<> use_all_columns(boost::dynamic_bitset<>& map);
485 inline void use_all_columns()
486 {
487 column_bitmaps_set(getMutableShare()->all_set, getMutableShare()->all_set);
488 }
489
490 inline void default_column_bitmaps()
491 {
492 read_set= &def_read_set;
493 write_set= &def_write_set;
494 }
495
496 /* Both of the below should go away once we can move this bit to the field objects */
497 inline bool isReadSet(uint32_t index) const
498 {
499 return read_set->test(index);
500 }
501
502 inline void setReadSet(uint32_t index)
503 {
504 read_set->set(index);
505 }
506
507 inline void setReadSet()
508 {
509 read_set->set();
510 }
511
512 inline void clearReadSet(uint32_t index)
513 {
514 read_set->reset(index);
515 }
516
517 inline void clearReadSet()
518 {
519 read_set->reset();
520 }
521
522 inline bool isWriteSet(uint32_t index)
523 {
524 return write_set->test(index);
525 }
526
527 inline void setWriteSet(uint32_t index)
528 {
529 write_set->set(index);
530 }
531
532 inline void setWriteSet()
533 {
534 write_set->set();
535 }
536
537 inline void clearWriteSet(uint32_t index)
538 {
539 write_set->reset(index);
540 }
541
542 inline void clearWriteSet()
543 {
544 write_set->reset();
545 }
546
547 /* Is table open or should be treated as such by name-locking? */
548 bool is_name_opened() const
549 {
550 return db_stat || open_placeholder;
551 }
552
553 /*
554 Is this instance of the table should be reopen or represents a name-lock?
555 */
556 bool needs_reopen_or_name_lock() const;
557
565 void setup_table_map(TableList *table_list, uint32_t tablenr);
566 inline void mark_as_null_row()
567 {
568 null_row= 1;
569 status|= STATUS_NULL_ROW;
570 memset(null_flags, 255, getShare()->null_bytes);
571 }
572
573 void free_io_cache();
574 void filesort_free_buffers(bool full= false);
575 void intern_close_table();
576
577 void print_error(int error, myf errflag) const;
578
583 uint32_t get_dup_key(int error) const
584 {
585 cursor->errkey = (uint32_t) -1;
586 if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
587 error == HA_ERR_FOUND_DUPP_UNIQUE ||
588 error == HA_ERR_DROP_INDEX_FK)
589 cursor->info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
590
591 return(cursor->errkey);
592 }
593
594 /*
595 This is a short term fix. Long term we will used the TableIdentifier to do the actual comparison.
596 */
597 bool operator<(const Table &right) const
598 {
599 return getShare()->getCacheKey() < right.getShare()->getCacheKey();
600 }
601
602 static bool compare(const Table *a, const Table *b)
603 {
604 return *a < *b;
605 }
606
607 friend std::ostream& operator<<(std::ostream& output, const Table &table)
608 {
609 if (table.getShare())
610 {
611 output << "Table:(";
612 output << table.getShare()->getSchemaName();
613 output << ", ";
614 output << table.getShare()->getTableName();
615 output << ", ";
616 output << table.getShare()->getTableTypeAsString();
617 output << ")";
618 }
619 else
620 {
621 output << "Table:(has no share)";
622 }
623
624 return output; // for multiple << operators.
625 }
626
627public:
628 virtual bool isPlaceHolder(void) const
629 {
630 return false;
631 }
632};
633
642{
643public:
658 lex_string_t *in_referenced_db,
659 lex_string_t *in_referenced_table,
660 lex_string_t *in_update_method,
661 lex_string_t *in_delete_method,
662 lex_string_t *in_referenced_key_name,
663 List<lex_string_t> in_foreign_fields,
664 List<lex_string_t> in_referenced_fields)
665 :
666 foreign_id(in_foreign_id),
667 referenced_db(in_referenced_db),
668 referenced_table(in_referenced_table),
669 update_method(in_update_method),
670 delete_method(in_delete_method),
671 referenced_key_name(in_referenced_key_name),
672 foreign_fields(in_foreign_fields),
673 referenced_fields(in_referenced_fields)
674 {}
675
681 : foreign_id(NULL), referenced_db(NULL), referenced_table(NULL),
683 {}
684
692 {
693 return foreign_id;
694 }
695
703 {
704 return referenced_db;
705 }
706
714 {
715 return referenced_table;
716 }
717
725 {
726 return update_method;
727 }
728
736 {
737 return delete_method;
738 }
739
747 {
748 return referenced_key_name;
749 }
750
758 {
759 return foreign_fields;
760 }
761
769 {
770 return referenced_fields;
771 }
772private:
805};
806
807#define JOIN_TYPE_LEFT 1
808#define JOIN_TYPE_RIGHT 2
809
810void free_blobs(Table *table);
811int set_zone(int nr,int min_zone,int max_zone);
812uint32_t convert_period_to_month(uint32_t period);
813uint32_t convert_month_to_period(uint32_t month);
814
815int test_if_number(char *str,int *res,bool allow_wildcards);
816void change_byte(unsigned char *,uint,char,char);
817void change_double_for_sort(double nr,unsigned char *to);
818int get_quick_record(optimizer::SqlSelect *select);
819
820void find_date(char *pos,uint32_t *vek,uint32_t flag);
821TYPELIB* convert_strings_to_array_type(char** typelibs, char** end);
823ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
824void append_unescaped(String *res, const char *pos, uint32_t length);
825
826bool check_column_name(const char*);
827bool check_table_name(str_ref);
828
829} /* namespace drizzled */
830
831#include <drizzled/table/singular.h>
832#include <drizzled/table/concurrent.h>
833
const lex_string_t * getReferencedTable() const
Gets the name of the referenced table.
lex_string_t * referenced_table
List< lex_string_t > foreign_fields
const List< lex_string_t > & getReferencedFields() const
Gets the referenced fields.
lex_string_t * referenced_key_name
ForeignKeyInfo()
This is the default constructor. All properties are set to default values for their types.
List< lex_string_t > referenced_fields
const lex_string_t * getReferencedDb() const
Gets the name of the referenced database.
const lex_string_t * getUpdateMethod() const
Gets the update method.
const lex_string_t * getDeleteMethod() const
Gets the delete method.
const lex_string_t * getForeignId() const
Gets the foreign id.
const lex_string_t * getReferencedKeyName() const
Gets the name of the referenced key.
ForeignKeyInfo(lex_string_t *in_foreign_id, lex_string_t *in_referenced_db, lex_string_t *in_referenced_table, lex_string_t *in_update_method, lex_string_t *in_delete_method, lex_string_t *in_referenced_key_name, List< lex_string_t > in_foreign_fields, List< lex_string_t > in_referenced_fields)
This is the constructor with all properties set.
const List< lex_string_t > & getForeignFields() const
Gets the foreign fields.
table_map map
ID bit of table (1,2,4,8,16...).
std::string _alias
Field * next_number_field
uint32_t lock_data_start
boost::dynamic_bitset def_write_set
ha_rows quick_condition_rows
uint32_t get_dup_key(int error) const
unsigned char * record[2]
Field * found_next_number_field
boost::dynamic_bitset def_read_set
field::Epoch * timestamp_field
unsigned char * alloc(size_t Size)
Allocate a chunk of memory from the Root structure provided, obtaining more memory from the heap if n...
Definition root.cc:140
char * strdup(const char *)
Duplicate a null-terminated string into memory allocated from within the specified Root.
Definition root.cc:327
void init(size_t block_size=ROOT_MIN_BLOCK_SIZE)
Initialize memory root.
Definition root.cc:57
TODO: Rename this file - func.h is stupid.
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38