~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.h

  • Committer: Barry.Leslie at PrimeBase
  • Date: 2010-10-20 20:41:00 UTC
  • mfrom: (1863 staging)
  • mto: This revision was merged to the branch mainline in revision 1871.
  • Revision ID: barry.leslie@primebase.com-20101020204100-oyj6p5cfssjw3p62
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
20
20
 
21
21
/* Structs that defines the Table */
22
22
 
23
 
 
24
 
 
25
23
#ifndef DRIZZLED_TABLE_H
26
24
#define DRIZZLED_TABLE_H
27
25
 
28
26
#include <string>
29
27
#include <boost/dynamic_bitset.hpp>
30
28
 
31
 
#include <drizzled/order.h>
32
 
#include <drizzled/filesort_info.h>
33
 
#include <drizzled/natural_join_column.h>
34
 
#include <drizzled/field_iterator.h>
35
 
#include <drizzled/cursor.h>
36
 
#include <drizzled/lex_string.h>
37
 
#include <drizzled/table/instance.h>
38
 
#include <drizzled/atomics.h>
39
 
#include <drizzled/query_id.h>
40
 
 
41
 
#include <drizzled/visibility.h>
 
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/table_share.h"
 
37
#include "drizzled/atomics.h"
 
38
#include "drizzled/query_id.h"
42
39
 
43
40
namespace drizzled
44
41
{
45
42
 
46
 
class COND_EQUAL;
47
 
class Field_blob;
48
43
class Item;
49
44
class Item_subselect;
 
45
class Select_Lex_Unit;
 
46
class Select_Lex;
 
47
class COND_EQUAL;
50
48
class SecurityContext;
51
 
class Select_Lex;
52
 
class Select_Lex_Unit;
53
49
class TableList;
54
 
namespace field { class Epoch; }
55
 
namespace plugin { class StorageEngine; }
 
50
class Field_timestamp;
 
51
class Field_blob;
 
52
 
 
53
extern uint64_t refresh_version;
56
54
 
57
55
typedef enum enum_table_category TABLE_CATEGORY;
58
56
typedef struct st_columndef MI_COLUMNDEF;
61
59
 * Class representing a set of records, either in a temporary, 
62
60
 * normal, or derived table.
63
61
 */
64
 
class DRIZZLED_API Table 
 
62
class Table 
65
63
{
 
64
  TableShare *_share; /**< Pointer to the shared metadata about the table */
 
65
 
66
66
  Field **field; /**< Pointer to fields collection */
 
67
public:
67
68
 
68
 
public:
69
69
  Field **getFields() const
70
70
  {
71
71
    return field;
87
87
  }
88
88
 
89
89
  Cursor *cursor; /**< Pointer to the storage engine's Cursor managing this table */
90
 
 
91
90
private:
92
91
  Table *next;
93
 
 
94
92
public:
95
93
  Table *getNext() const
96
94
  {
147
145
    return in_use;
148
146
  }
149
147
 
150
 
  unsigned char *getInsertRecord() const
 
148
  unsigned char *getInsertRecord()
151
149
  {
152
150
    return record[0];
153
151
  }
162
160
  KeyInfo  *key_info; /**< data of keys in database */
163
161
  Field *next_number_field; /**< Set if next_number is activated. @TODO What the heck is the difference between this and the next member? */
164
162
  Field *found_next_number_field; /**< Points to the "next-number" field (autoincrement field) */
165
 
  field::Epoch *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
 
163
  Field_timestamp *timestamp_field; /**< Points to the auto-setting timestamp field, if any */
166
164
 
167
165
  TableList *pos_in_table_list; /* Element referring to this table */
168
 
  Order *group;
 
166
  order_st *group;
169
167
  
170
168
  const char *getAlias() const
171
169
  {
172
 
    return _alias.c_str();
173
 
  }
174
 
 
175
 
  void clearAlias()
176
 
  {
177
 
    _alias.clear();
178
 
  }
179
 
 
180
 
  void setAlias(const char *arg)
181
 
  {
182
 
    _alias= arg;
183
 
  }
184
 
 
185
 
private:
186
 
  std::string _alias; /**< alias or table name if no alias */
187
 
public:
 
170
    return alias;
 
171
  }
 
172
 
 
173
  const char *alias; /**< alias or table name if no alias */
188
174
 
189
175
  unsigned char *null_flags;
190
176
 
284
270
    statement then the variable contains TIMESTAMP_NO_AUTO_SET (i.e. 0).
285
271
 
286
272
    Value of this variable is set for each statement in open_table() and
287
 
    if needed cleared later in statement processing code (see update_query()
 
273
    if needed cleared later in statement processing code (see mysql_update()
288
274
    as example).
289
275
  */
290
276
  timestamp_auto_set_type timestamp_field_type;
312
298
    The set is implemented as a bitmap.
313
299
  */
314
300
  key_map keys_in_use_for_query;
315
 
 
316
301
  /* Map of keys that can be used to calculate GROUP BY without sorting */
317
302
  key_map keys_in_use_for_group_by;
318
 
 
319
303
  /* Map of keys that can be used to calculate ORDER BY without sorting */
320
304
  key_map keys_in_use_for_order_by;
321
305
 
372
356
  filesort_info sort;
373
357
 
374
358
  Table();
375
 
  virtual ~Table();
 
359
  virtual ~Table() { };
376
360
 
377
361
  int report_error(int error);
378
362
  /**
386
370
  void resetTable(Session *session, TableShare *share, uint32_t db_stat_arg);
387
371
 
388
372
  /* SHARE methods */
389
 
  virtual const TableShare *getShare() const= 0; /* Get rid of this long term */
390
 
  virtual TableShare *getMutableShare()= 0; /* Get rid of this long term */
391
 
  virtual bool hasShare() const= 0; /* Get rid of this long term */
392
 
  virtual void setShare(TableShare *new_share)= 0; /* Get rid of this long term */
393
 
 
394
 
  virtual void release(void)= 0;
395
 
 
396
 
  uint32_t sizeKeys() { return getMutableShare()->sizeKeys(); }
397
 
  uint32_t sizeFields() { return getMutableShare()->sizeFields(); }
398
 
  uint32_t getRecordLength() const { return getShare()->getRecordLength(); }
399
 
  uint32_t sizeBlobFields() { return getMutableShare()->blob_fields; }
400
 
  uint32_t *getBlobField() { return &getMutableShare()->blob_field[0]; }
 
373
  virtual const TableShare *getShare() const { assert(_share); return _share; } /* Get rid of this long term */
 
374
  virtual TableShare *getMutableShare() { assert(_share); return _share; } /* Get rid of this long term */
 
375
  bool hasShare() const { return _share ? true : false ; } /* Get rid of this long term */
 
376
  virtual void setShare(TableShare *new_share) { _share= new_share; } /* Get rid of this long term */
 
377
  uint32_t sizeKeys() { return _share->sizeKeys(); }
 
378
  uint32_t sizeFields() { return _share->sizeFields(); }
 
379
  uint32_t getRecordLength() const { return _share->getRecordLength(); }
 
380
  uint32_t sizeBlobFields() { return _share->blob_fields; }
 
381
  uint32_t *getBlobField() { return &_share->blob_field[0]; }
401
382
 
402
383
public:
403
384
  virtual bool hasVariableWidth() const
414
395
 
415
396
    return NULL;
416
397
  }
417
 
  inline uint8_t getBlobPtrSize() const { return getShare()->sizeBlobPtr(); }
418
 
  inline uint32_t getNullBytes() const { return getShare()->null_bytes; }
419
 
  inline uint32_t getNullFields() const { return getShare()->null_fields; }
 
398
  inline uint8_t getBlobPtrSize() { return getShare()->blob_ptr_size; }
 
399
  inline uint32_t getNullBytes() { return getShare()->null_bytes; }
 
400
  inline uint32_t getNullFields() { return getShare()->null_fields; }
420
401
  inline unsigned char *getDefaultValues() { return  getMutableShare()->getDefaultValues(); }
421
402
  inline const char *getSchemaName()  const { return getShare()->getSchemaName(); }
422
403
  inline const char *getTableName()  const { return getShare()->getTableName(); }
423
404
 
424
 
  inline bool isDatabaseLowByteFirst() const { return getShare()->db_low_byte_first; } /* Portable row format */
425
 
  inline bool isNameLock() const { return open_placeholder; }
 
405
  inline bool isDatabaseLowByteFirst() { return getShare()->db_low_byte_first; } /* Portable row format */
 
406
  inline bool isNameLock() const { return getShare()->isNameLock(); }
 
407
  inline bool isReplaceWithNameLock() { return getShare()->replace_with_name_lock; }
426
408
 
427
 
  uint32_t index_flags(uint32_t idx) const;
 
409
  uint32_t index_flags(uint32_t idx) const
 
410
  {
 
411
    return getShare()->storage_engine->index_flags(getShare()->getKeyInfo(idx).algorithm);
 
412
  }
428
413
 
429
414
  inline plugin::StorageEngine *getEngine() const   /* table_type for handler */
430
415
  {
431
 
    return getShare()->getEngine();
 
416
    return getShare()->storage_engine;
432
417
  }
433
418
 
434
419
  Cursor &getCursor() const /* table_type for handler */
440
425
  size_t max_row_length(const unsigned char *data);
441
426
  uint32_t find_shortest_key(const key_map *usable_keys);
442
427
  bool compare_record(Field **ptr);
443
 
  bool records_are_comparable();
444
 
  bool compare_records();
 
428
  bool compare_record();
445
429
  /* TODO: the (re)storeRecord's may be able to be further condensed */
446
430
  void storeRecord();
447
431
  void storeRecordAsInsert();
450
434
  void restoreRecordAsDefault();
451
435
  void emptyRecord();
452
436
 
453
 
 
454
437
  /* See if this can be blown away */
455
438
  inline uint32_t getDBStat () { return db_stat; }
456
439
  inline uint32_t setDBStat () { return db_stat; }
499
482
  }
500
483
 
501
484
  /* Both of the below should go away once we can move this bit to the field objects */
502
 
  inline bool isReadSet(uint32_t index) const
 
485
  inline bool isReadSet(uint32_t index)
503
486
  {
504
487
    return read_set->test(index);
505
488
  }
554
537
  {
555
538
    return db_stat || open_placeholder;
556
539
  }
557
 
 
558
540
  /*
559
541
    Is this instance of the table should be reopen or represents a name-lock?
560
542
  */
561
 
  bool needs_reopen_or_name_lock() const;
 
543
  inline bool needs_reopen_or_name_lock()
 
544
  { 
 
545
    return getShare()->getVersion() != refresh_version;
 
546
  }
562
547
 
563
548
  /**
564
549
    clean/setup table fields and map.
579
564
  void filesort_free_buffers(bool full= false);
580
565
  void intern_close_table();
581
566
 
582
 
  void print_error(int error, myf errflag) const;
 
567
  void print_error(int error, myf errflag)
 
568
  {
 
569
    getShare()->storage_engine->print_error(error, errflag, *this);
 
570
  }
583
571
 
584
572
  /**
585
573
    @return
586
574
    key if error because of duplicated keys
587
575
  */
588
 
  uint32_t get_dup_key(int error) const
 
576
  uint32_t get_dup_key(int error)
589
577
  {
590
578
    cursor->errkey  = (uint32_t) -1;
591
579
    if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
601
589
  */
602
590
  bool operator<(const Table &right) const
603
591
  {
604
 
    return getShare()->getCacheKey() < right.getShare()->getCacheKey();
 
592
    int result= strcasecmp(this->getShare()->getSchemaName(), right.getShare()->getSchemaName());
 
593
 
 
594
    if (result <  0)
 
595
      return true;
 
596
 
 
597
    if (result >  0)
 
598
      return false;
 
599
 
 
600
    result= strcasecmp(this->getShare()->getTableName(), right.getShare()->getTableName());
 
601
 
 
602
    if (result <  0)
 
603
      return true;
 
604
 
 
605
    if (result >  0)
 
606
      return false;
 
607
 
 
608
    if (this->getShare()->getTableProto()->type()  < right.getShare()->getTableProto()->type())
 
609
      return true;
 
610
 
 
611
    return false;
605
612
  }
606
613
 
607
614
  static bool compare(const Table *a, const Table *b)
611
618
 
612
619
  friend std::ostream& operator<<(std::ostream& output, const Table &table)
613
620
  {
614
 
    if (table.getShare())
615
 
    {
616
 
      output << "Table:(";
617
 
      output << table.getShare()->getSchemaName();
618
 
      output << ", ";
619
 
      output <<  table.getShare()->getTableName();
620
 
      output << ", ";
621
 
      output <<  table.getShare()->getTableTypeAsString();
622
 
      output << ")";
623
 
    }
624
 
    else
625
 
    {
626
 
      output << "Table:(has no share)";
627
 
    }
 
621
    output << "Table:(";
 
622
    output << table.getShare()->getSchemaName();
 
623
    output << ", ";
 
624
    output <<  table.getShare()->getTableName();
 
625
    output << ", ";
 
626
    output <<  table.getShare()->getTableTypeAsString();
 
627
    output << ")";
628
628
 
629
629
    return output;  // for multiple << operators.
630
630
  }
828
828
 
829
829
namespace optimizer { class SqlSelect; }
830
830
 
 
831
ha_rows filesort(Session *session,
 
832
                 Table *form,
 
833
                 SortField *sortorder,
 
834
                 uint32_t s_length,
 
835
                 optimizer::SqlSelect *select,
 
836
                 ha_rows max_rows,
 
837
                 bool sort_positions,
 
838
                 ha_rows *examined_rows);
 
839
 
 
840
void filesort_free_buffers(Table *table, bool full);
831
841
void change_double_for_sort(double nr,unsigned char *to);
 
842
double my_double_round(double value, int64_t dec, bool dec_unsigned,
 
843
                       bool truncate);
832
844
int get_quick_record(optimizer::SqlSelect *select);
833
845
 
834
846
void find_date(char *pos,uint32_t *vek,uint32_t flag);
835
847
TYPELIB *convert_strings_to_array_type(char * *typelibs, char * *end);
836
848
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings);
837
849
ulong get_form_pos(int file, unsigned char *head, TYPELIB *save_names);
 
850
ulong next_io_size(ulong pos);
838
851
void append_unescaped(String *res, const char *pos, uint32_t length);
839
852
 
840
 
DRIZZLED_API int rename_file_ext(const char * from,const char * to,const char * ext);
 
853
int rename_file_ext(const char * from,const char * to,const char * ext);
841
854
bool check_column_name(const char *name);
 
855
bool check_db_name(Session *session, SchemaIdentifier &schema);
842
856
bool check_table_name(const char *name, uint32_t length);
843
857
 
844
858
} /* namespace drizzled */
845
859
 
846
 
#include <drizzled/table/singular.h>
847
 
#include <drizzled/table/concurrent.h>
 
860
#include "drizzled/table/instance.h"
 
861
#include "drizzled/table/concurrent.h"
848
862
 
849
863
#endif /* DRIZZLED_TABLE_H */