~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Lee Bieber
  • Date: 2011-03-29 22:31:41 UTC
  • mfrom: (2257.1.3 build)
  • Revision ID: kalebral@gmail.com-20110329223141-yxc22h3l2he58sk0
Merge Andrew - 743842: Build failure using GCC 4.6
Merge Stewart - 738022: CachedDirectory silently fails to add entries if stat() fails
Merge Olaf - Common fwd: add copyright, add more declaration

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* Some general useful functions */
18
18
 
19
 
#include "config.h"
 
19
#include <config.h>
20
20
 
21
21
#include <float.h>
22
22
#include <fcntl.h>
28
28
#include <drizzled/error.h>
29
29
#include <drizzled/gettext.h>
30
30
 
31
 
#include "drizzled/plugin/transactional_storage_engine.h"
32
 
#include "drizzled/plugin/authorization.h"
 
31
#include <drizzled/plugin/transactional_storage_engine.h>
 
32
#include <drizzled/plugin/authorization.h>
33
33
#include <drizzled/nested_join.h>
34
34
#include <drizzled/sql_parse.h>
35
35
#include <drizzled/item/sum.h>
42
42
#include <drizzled/field/double.h>
43
43
#include <drizzled/unireg.h>
44
44
#include <drizzled/message/table.pb.h>
45
 
#include "drizzled/sql_table.h"
46
 
#include "drizzled/charset.h"
47
 
#include "drizzled/internal/m_string.h"
48
 
#include "plugin/myisam/myisam.h"
49
 
 
 
45
#include <drizzled/sql_table.h>
 
46
#include <drizzled/charset.h>
 
47
#include <drizzled/internal/m_string.h>
 
48
#include <plugin/myisam/myisam.h>
 
49
#include <drizzled/plugin/storage_engine.h>
50
50
#include <drizzled/item/string.h>
51
51
#include <drizzled/item/int.h>
52
52
#include <drizzled/item/decimal.h>
53
53
#include <drizzled/item/float.h>
54
54
#include <drizzled/item/null.h>
55
55
#include <drizzled/temporal.h>
56
 
 
57
 
#include "drizzled/table/instance.h"
58
 
 
59
 
#include "drizzled/table_proto.h"
 
56
#include <drizzled/refresh_version.h>
 
57
#include <drizzled/table/singular.h>
 
58
#include <drizzled/table_proto.h>
 
59
#include <drizzled/typelib.h>
 
60
#include <drizzled/sql_lex.h>
 
61
#include <drizzled/statistics_variables.h>
 
62
#include <drizzled/system_variables.h>
60
63
 
61
64
using namespace std;
62
65
 
63
 
namespace drizzled
64
 
{
 
66
namespace drizzled {
65
67
 
66
 
extern pid_t current_pid;
67
68
extern plugin::StorageEngine *heap_engine;
68
69
extern plugin::StorageEngine *myisam_engine;
69
70
 
70
71
/* Functions defined in this cursor */
71
72
 
72
 
void open_table_error(TableShare *share, int error, int db_errno,
73
 
                      myf errortype, int errarg);
74
 
 
75
73
/*************************************************************************/
76
74
 
77
75
// @note this should all be the destructor
82
80
  if (db_stat)
83
81
    error= cursor->close();
84
82
  _alias.clear();
 
83
 
85
84
  if (field)
86
85
  {
87
86
    for (Field **ptr=field ; *ptr ; ptr++)
90
89
    }
91
90
    field= 0;
92
91
  }
93
 
  delete cursor;
94
 
  cursor= 0;                            /* For easier errorchecking */
 
92
  safe_delete(cursor);
95
93
 
96
94
  if (free_share)
97
95
  {
112
110
                       uint32_t db_stat_arg)
113
111
{
114
112
  setShare(share);
 
113
  in_use= session;
 
114
 
115
115
  field= NULL;
116
116
 
117
117
  cursor= NULL;
124
124
  tablenr= 0;
125
125
  db_stat= db_stat_arg;
126
126
 
127
 
  in_use= session;
128
127
  record[0]= (unsigned char *) NULL;
129
128
  record[1]= (unsigned char *) NULL;
130
129
 
196
195
 
197
196
/* Deallocate temporary blob storage */
198
197
 
199
 
void free_blobs(register Table *table)
 
198
void free_blobs(Table *table)
200
199
{
201
200
  uint32_t *ptr, *end;
202
201
  for (ptr= table->getBlobField(), end=ptr + table->sizeBlobFields();
213
212
  TYPELIB *result= (TYPELIB*) mem_root->alloc_root(sizeof(TYPELIB));
214
213
  if (!result)
215
214
    return 0;
216
 
  result->count= strings.elements;
 
215
  result->count= strings.size();
217
216
  result->name= "";
218
217
  uint32_t nbytes= (sizeof(char*) + sizeof(uint32_t)) * (result->count + 1);
219
218
  
222
221
    
223
222
  result->type_lengths= (uint*) (result->type_names + result->count + 1);
224
223
 
225
 
  List_iterator<String> it(strings);
 
224
  List<String>::iterator it(strings.begin());
226
225
  String *tmp;
227
226
  for (uint32_t i= 0; (tmp= it++); i++)
228
227
  {
238
237
 
239
238
        /* Check that the integer is in the internal */
240
239
 
241
 
int set_zone(register int nr, int min_zone, int max_zone)
 
240
int set_zone(int nr, int min_zone, int max_zone)
242
241
{
243
242
  if (nr<=min_zone)
244
243
    return (min_zone);
322
321
}
323
322
 
324
323
/*
325
 
  Check if database name is valid
326
 
 
327
 
  SYNPOSIS
328
 
    check_db_name()
329
 
    org_name            Name of database and length
330
 
 
331
 
  RETURN
332
 
    false error
333
 
    true ok
334
 
*/
335
 
 
336
 
bool check_db_name(Session *session, SchemaIdentifier &schema_identifier)
337
 
{
338
 
  if (not plugin::Authorization::isAuthorized(session->user(), schema_identifier))
339
 
  {
340
 
    return false;
341
 
  }
342
 
 
343
 
  return schema_identifier.isValid();
344
 
}
345
 
 
346
 
/*
347
324
  Allow anything as a table name, as long as it doesn't contain an
348
325
  ' ' at the end
349
326
  returns 1 on error
661
638
void Table::setVariableWidth(void)
662
639
{
663
640
  assert(in_use);
664
 
  if (in_use && in_use->lex->sql_command == SQLCOM_CREATE_TABLE)
 
641
  if (in_use && in_use->lex().sql_command == SQLCOM_CREATE_TABLE)
665
642
  {
666
643
    getMutableShare()->setVariableWidth();
667
644
    return;
783
760
  uint32_t fieldnr= 0;
784
761
  ulong reclength, string_total_length;
785
762
  bool  using_unique_constraint= false;
786
 
  bool  use_packed_rows= true;
787
763
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
788
764
  unsigned char *pos, *group_buff;
789
765
  unsigned char *null_flags;
838
814
    copy_func_count+= param->sum_func_count;
839
815
  }
840
816
 
841
 
  table::Instance *table;
842
 
  table= session->getInstanceTable(); // This will not go into the tableshare cache, so no key is used.
 
817
  table::Singular* table= &session->getInstanceTable(); // This will not go into the tableshare cache, so no key is used.
843
818
 
844
819
  if (not table->getMemRoot()->multi_alloc_root(0,
845
820
                                                &default_field, sizeof(Field*) * (field_count),
884
859
 
885
860
  table->getMutableShare()->blob_field.resize(field_count+1);
886
861
  uint32_t *blob_field= &table->getMutableShare()->blob_field[0];
887
 
  table->getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
888
862
  table->getMutableShare()->db_low_byte_first=1;                // True for HEAP and MyISAM
889
863
  table->getMutableShare()->table_charset= param->table_charset;
890
864
  table->getMutableShare()->keys_for_keyread.reset();
896
870
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
897
871
  param->using_indirect_summary_function= 0;
898
872
 
899
 
  List_iterator_fast<Item> li(fields);
 
873
  List<Item>::iterator li(fields.begin());
900
874
  Item *item;
901
875
  Field **tmp_from_field=from_field;
902
876
  while ((item=li++))
954
928
            string_total_length+= new_field->pack_length();
955
929
          }
956
930
          session->mem_root= mem_root_save;
957
 
          session->change_item_tree(argp, new Item_field(new_field));
 
931
          *argp= new Item_field(new_field);
958
932
          session->mem_root= table->getMemRoot();
959
933
          if (!(new_field->flags & NOT_NULL_FLAG))
960
934
          {
1035
1009
  field_count= fieldnr;
1036
1010
  *reg_field= 0;
1037
1011
  *blob_field= 0;                               // End marker
1038
 
  table->getMutableShare()->fields= field_count;
 
1012
  table->getMutableShare()->setFieldSize(field_count);
1039
1013
 
1040
1014
  /* If result table is small; use a heap */
1041
1015
  /* future: storage engine selection can be made dynamic? */
1042
1016
  if (blob_count || using_unique_constraint || 
1043
 
      (session->lex->select_lex.options & SELECT_BIG_RESULT) ||
1044
 
      (session->lex->current_select->olap == ROLLUP_TYPE) ||
 
1017
      (session->lex().select_lex.options & SELECT_BIG_RESULT) ||
 
1018
      (session->lex().current_select->olap == ROLLUP_TYPE) ||
1045
1019
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
1046
1020
  {
1047
1021
    table->getMutableShare()->storage_engine= myisam_engine;
1080
1054
  reclength+=null_pack_length;
1081
1055
  if (!reclength)
1082
1056
    reclength=1;                                // Dummy select
1083
 
  /* Use packed rows if there is blobs or a lot of space to gain */
1084
 
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
1085
 
    use_packed_rows= 1;
1086
1057
 
1087
1058
  table->getMutableShare()->setRecordLength(reclength);
1088
1059
  {
1647
1618
  query_id(0),
1648
1619
  quick_condition_rows(0),
1649
1620
  timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
1650
 
  map(0)
 
1621
  map(0),
 
1622
  quick_rows(),
 
1623
  const_key_parts(),
 
1624
  quick_key_parts(),
 
1625
  quick_n_ranges()
1651
1626
{
1652
1627
  record[0]= (unsigned char *) 0;
1653
1628
  record[1]= (unsigned char *) 0;
1654
 
 
1655
 
  reginfo.reset();
1656
 
  covering_keys.reset();
1657
 
  quick_keys.reset();
1658
 
  merge_keys.reset();
1659
 
 
1660
 
  keys_in_use_for_query.reset();
1661
 
  keys_in_use_for_group_by.reset();
1662
 
  keys_in_use_for_order_by.reset();
1663
 
 
1664
 
  memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
1665
 
  memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
1666
 
 
1667
 
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
1668
 
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
1669
1629
}
1670
1630
 
1671
1631
/*****************************************************************************
1687
1647
    print them to the .err log
1688
1648
  */
1689
1649
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
1690
 
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got error %d when reading table '%s'"),
 
1650
    errmsg_printf(error::ERROR, _("Got error %d when reading table '%s'"),
1691
1651
                  error, getShare()->getPath());
1692
1652
  print_error(error, MYF(0));
1693
1653
 
1716
1676
}
1717
1677
 
1718
1678
 
1719
 
bool Table::fill_item_list(List<Item> *item_list) const
 
1679
void Table::fill_item_list(List<Item>& items) const
1720
1680
{
1721
1681
  /*
1722
1682
    All Item_field's created using a direct pointer to a field
1723
1683
    are fixed in Item_field constructor.
1724
1684
  */
1725
1685
  for (Field **ptr= field; *ptr; ptr++)
1726
 
  {
1727
 
    Item_field *item= new Item_field(*ptr);
1728
 
    if (!item || item_list->push_back(item))
1729
 
      return true;
1730
 
  }
1731
 
  return false;
 
1686
    items.push_back(new Item_field(*ptr));
1732
1687
}
1733
1688
 
1734
1689
 
1735
1690
void Table::filesort_free_buffers(bool full)
1736
1691
{
1737
 
  if (sort.record_pointers)
1738
 
  {
1739
 
    free((unsigned char*) sort.record_pointers);
1740
 
    sort.record_pointers=0;
1741
 
  }
 
1692
  free(sort.record_pointers);
 
1693
  sort.record_pointers=0;
1742
1694
  if (full)
1743
1695
  {
1744
 
    if (sort.sort_keys )
1745
 
    {
1746
 
      if ((unsigned char*) sort.sort_keys)
1747
 
        free((unsigned char*) sort.sort_keys);
1748
 
      sort.sort_keys= 0;
1749
 
    }
1750
 
    if (sort.buffpek)
1751
 
    {
1752
 
      if ((unsigned char*) sort.buffpek)
1753
 
        free((unsigned char*) sort.buffpek);
1754
 
      sort.buffpek= 0;
1755
 
      sort.buffpek_len= 0;
1756
 
    }
1757
 
  }
1758
 
 
1759
 
  if (sort.addon_buf)
1760
 
  {
1761
 
    free((char *) sort.addon_buf);
1762
 
    free((char *) sort.addon_field);
1763
 
    sort.addon_buf=0;
1764
 
    sort.addon_field=0;
1765
 
  }
 
1696
    free(sort.sort_keys);
 
1697
    sort.sort_keys= 0;
 
1698
    free(sort.buffpek);
 
1699
    sort.buffpek= 0;
 
1700
    sort.buffpek_len= 0;
 
1701
  }
 
1702
  free(sort.addon_buf);
 
1703
  free(sort.addon_field);
 
1704
  sort.addon_buf=0;
 
1705
  sort.addon_field=0;
 
1706
}
 
1707
 
 
1708
/*
 
1709
  Is this instance of the table should be reopen or represents a name-lock?
 
1710
*/
 
1711
bool Table::needs_reopen_or_name_lock() const
 
1712
 
1713
  return getShare()->getVersion() != refresh_version;
 
1714
}
 
1715
 
 
1716
uint32_t Table::index_flags(uint32_t idx) const
 
1717
{
 
1718
  return getShare()->getEngine()->index_flags(getShare()->getKeyInfo(idx).algorithm);
 
1719
}
 
1720
 
 
1721
void Table::print_error(int error, myf errflag) const
 
1722
{
 
1723
  getShare()->getEngine()->print_error(error, errflag, *this);
1766
1724
}
1767
1725
 
1768
1726
} /* namespace drizzled */