~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table/instance.cc

Merge Monty - Updates to pandora-build to support features of gcc 4.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.h"
22
22
 
23
23
#include <sys/types.h>
24
24
#include <sys/stat.h>
25
25
#include <fcntl.h>
26
26
 
27
 
#include <drizzled/session.h>
28
 
#include <plugin/myisam/myisam.h>
29
 
#include <drizzled/plugin/transactional_storage_engine.h>
30
 
#include <drizzled/statistics_variables.h>
31
 
#include <drizzled/table.h>
32
 
#include <drizzled/create_field.h>
33
 
 
34
 
namespace drizzled {
35
 
namespace table {
36
 
 
37
 
Singular::Singular(Session *session, std::list<CreateField>& field_list) :
 
27
#include "drizzled/session.h"
 
28
#include "plugin/myisam/myisam.h"
 
29
#include "drizzled/plugin/transactional_storage_engine.h"
 
30
 
 
31
#include "drizzled/table.h"
 
32
 
 
33
namespace drizzled
 
34
{
 
35
 
 
36
namespace table
 
37
{
 
38
 
 
39
Instance::Instance(Session *session, List<CreateField> &field_list) :
38
40
  _share(message::Table::INTERNAL),
39
41
  _has_variable_width(false)
40
42
{
41
 
  uint32_t field_count= field_list.size();
 
43
  uint32_t field_count= field_list.elements;
42
44
  uint32_t blob_count= 0;
 
45
  Field **field_arg;
 
46
  CreateField *cdef;                           /* column definition */
43
47
  uint32_t record_length= 0;
44
48
  uint32_t null_count= 0;                 /* number of columns which may be null */
45
49
  uint32_t null_pack_length;              /* NULL representation array length */
46
50
 
47
51
  getMutableShare()->setFields(field_count + 1);
48
52
  setFields(getMutableShare()->getFields(true));
49
 
  Field** field_arg= getMutableShare()->getFields(true);
 
53
  field_arg= getMutableShare()->getFields(true);
50
54
  getMutableShare()->blob_field.resize(field_count+1);
51
 
  getMutableShare()->setFieldSize(field_count);
 
55
  getMutableShare()->fields= field_count;
52
56
  getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
53
57
  setup_tmp_table_column_bitmaps();
54
58
 
55
59
  in_use= session;           /* field_arg->reset() may access in_use */
56
60
 
57
61
  /* Create all fields and calculate the total length of record */
58
 
  message::Table::Field null_field;
59
 
        BOOST_FOREACH(CreateField& it, field_list)
 
62
  List_iterator_fast<CreateField> it(field_list);
 
63
  while ((cdef= it++))
60
64
  {
61
 
    *field_arg= getMutableShare()->make_field(null_field,
62
 
                                              NULL,
63
 
                                              it.length,
64
 
                                              (it.flags & NOT_NULL_FLAG) ? false : true,
65
 
                                              (unsigned char *) ((it.flags & NOT_NULL_FLAG) ? 0 : ""),
66
 
                                              (it.flags & NOT_NULL_FLAG) ? 0 : 1,
67
 
                                              it.decimals,
68
 
                                              it.sql_type,
69
 
                                              it.charset,
70
 
                                              it.unireg_check,
71
 
                                              it.interval,
72
 
                                              it.field_name,
73
 
                                              it.flags & UNSIGNED_FLAG ? true : false);
 
65
    *field_arg= getMutableShare()->make_field(NULL,
 
66
                                                 cdef->length,
 
67
                                                 (cdef->flags & NOT_NULL_FLAG) ? false : true,
 
68
                                                 (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
 
69
                                                 (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
 
70
                                                 cdef->decimals,
 
71
                                                 cdef->sql_type,
 
72
                                                 cdef->charset,
 
73
                                                 cdef->unireg_check,
 
74
                                                 cdef->interval,
 
75
                                                 cdef->field_name);
 
76
    if (!*field_arg)
 
77
    {
 
78
      throw "Memory allocation failure";
 
79
    }
 
80
 
74
81
    (*field_arg)->init(this);
75
82
    record_length+= (*field_arg)->pack_length();
76
83
    if (! ((*field_arg)->flags & NOT_NULL_FLAG))
88
95
  null_pack_length= (null_count + 7)/8;
89
96
  getMutableShare()->setRecordLength(record_length + null_pack_length);
90
97
  getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
91
 
  record[0]= session->mem.alloc(getMutableShare()->rec_buff_length);
 
98
  record[0]= (unsigned char*)session->alloc(getMutableShare()->rec_buff_length);
 
99
  if (not getInsertRecord())
 
100
  {
 
101
    throw "Memory allocation failure";
 
102
  }
92
103
 
93
104
  if (null_pack_length)
94
105
  {
95
 
    null_flags= getInsertRecord();
 
106
    null_flags= (unsigned char*) getInsertRecord();
96
107
    getMutableShare()->null_fields= null_count;
97
108
    getMutableShare()->null_bytes= null_pack_length;
98
109
  }
124
135
  }
125
136
}
126
137
 
127
 
bool Singular::open_tmp_table()
 
138
bool Instance::open_tmp_table()
128
139
{
129
 
  identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getPath());
130
 
  if (int error= cursor->ha_open(identifier, O_RDWR, HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE))
 
140
  int error;
 
141
  
 
142
  TableIdentifier identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getPath());
 
143
  if ((error=cursor->ha_open(identifier,
 
144
                             O_RDWR,
 
145
                             HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
131
146
  {
132
147
    print_error(error, MYF(0));
133
148
    db_stat= 0;
167
182
     true  - Error
168
183
*/
169
184
 
170
 
bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
 
185
bool Instance::create_myisam_tmp_table(KeyInfo *keyinfo,
171
186
                                                 MI_COLUMNDEF *start_recinfo,
172
187
                                                 MI_COLUMNDEF **recinfo,
173
188
                                                 uint64_t options)
179
194
  if (getShare()->sizeKeys())
180
195
  {                                             // Get keys for ni_create
181
196
    bool using_unique_constraint= false;
182
 
    HA_KEYSEG *seg= new (mem()) HA_KEYSEG[keyinfo->key_parts];
 
197
    HA_KEYSEG *seg= (HA_KEYSEG*) getMemRoot()->alloc_root(sizeof(*seg) * keyinfo->key_parts);
 
198
    if (not seg)
 
199
      return true;
183
200
 
184
201
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
185
202
    if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
232
249
      if (!(key_field->flags & NOT_NULL_FLAG))
233
250
      {
234
251
        seg->null_bit= key_field->null_bit;
235
 
        seg->null_pos= (uint32_t) (key_field->null_ptr - getInsertRecord());
 
252
        seg->null_pos= (uint32_t) (key_field->null_ptr - (unsigned char*) getInsertRecord());
236
253
        /*
237
254
          We are using a GROUP BY on something that contains NULL
238
255
          In this case we have to tell MyISAM that two NULL should
274
291
    a tmp_set bitmap to be used by things like filesort.
275
292
*/
276
293
 
277
 
void Singular::setup_tmp_table_column_bitmaps()
 
294
void Instance::setup_tmp_table_column_bitmaps()
278
295
{
279
296
  uint32_t field_count= getShare()->sizeFields();
280
297
 
288
305
  default_column_bitmaps();
289
306
}
290
307
 
291
 
Singular::~Singular()
 
308
Instance::~Instance()
292
309
{
293
 
  const char* save_proc_info= in_use->get_proc_info();
 
310
  const char *save_proc_info;
 
311
 
 
312
  save_proc_info= in_use->get_proc_info();
294
313
  in_use->set_proc_info("removing tmp table");
295
314
 
296
315
  // Release latches since this can take a long time
299
318
  if (cursor)
300
319
  {
301
320
    if (db_stat)
302
 
      cursor->closeMarkForDelete();
303
 
 
304
 
    identifier::Table identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getTableName());
305
 
    drizzled::error_t ignored;
306
 
    plugin::StorageEngine::dropTable(*in_use, *getShare()->getEngine(), identifier, ignored);
 
321
    {
 
322
      cursor->closeMarkForDelete(getShare()->getTableName());
 
323
    }
 
324
 
 
325
    TableIdentifier identifier(getShare()->getSchemaName(), getShare()->getTableName(), getShare()->getTableName());
 
326
    getShare()->getEngine()->doDropTable(*in_use, identifier);
 
327
 
307
328
    delete cursor;
308
329
  }
309
330
 
310
331
  /* free blobs */
311
 
  for (Field **ptr= getFields(); *ptr; ptr++)
 
332
  for (Field **ptr= getFields() ; *ptr ; ptr++)
312
333
  {
313
334
    (*ptr)->free();
314
335
  }
315
336
  free_io_cache();
316
337
 
317
 
  mem().free_root(MYF(0));
 
338
  getMemRoot()->free_root(MYF(0));
318
339
  in_use->set_proc_info(save_proc_info);
319
340
}
320
341
 
 
342
 
321
343
} /* namespace table */
322
344
} /* namespace drizzled */