~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
#include <drizzled/item/null.h>
55
55
#include <drizzled/temporal.h>
56
56
 
 
57
#include "drizzled/table_share_instance.h"
 
58
 
57
59
#include "drizzled/table_proto.h"
58
60
 
59
61
using namespace std;
72
74
 
73
75
/*************************************************************************/
74
76
 
75
 
/* Get column name from column hash */
76
 
 
77
 
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
78
 
{
79
 
  *length= (uint32_t) strlen((*buff)->field_name);
80
 
  return (unsigned char*) (*buff)->field_name;
81
 
}
82
 
 
83
 
/*
84
 
  Allocate a setup TableShare structure
85
 
 
86
 
  SYNOPSIS
87
 
    alloc_table_share()
88
 
    TableList           Take database and table name from there
89
 
    key                 Table cache key (db \0 table_name \0...)
90
 
    key_length          Length of key
91
 
 
92
 
  RETURN
93
 
    0  Error (out of memory)
94
 
    #  Share
95
 
*/
96
 
 
97
 
TableShare *alloc_table_share(TableList *table_list, char *key,
98
 
                               uint32_t key_length)
99
 
{
100
 
  memory::Root mem_root;
101
 
  TableShare *share;
102
 
  char *key_buff, *path_buff;
103
 
  char path[FN_REFLEN];
104
 
  uint32_t path_length;
105
 
 
106
 
  path_length= build_table_filename(path, sizeof(path) - 1,
107
 
                                    table_list->db,
108
 
                                    table_list->table_name, false);
109
 
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
110
 
  if (multi_alloc_root(&mem_root,
111
 
                       &share, sizeof(*share),
112
 
                       &key_buff, key_length,
113
 
                       &path_buff, path_length + 1,
114
 
                       NULL))
115
 
  {
116
 
    memset(share, 0, sizeof(*share));
117
 
 
118
 
    share->set_table_cache_key(key_buff, key, key_length);
119
 
 
120
 
    share->path.str= path_buff;
121
 
    share->path.length= path_length;
122
 
    strcpy(share->path.str, path);
123
 
    share->normalized_path.str=    share->path.str;
124
 
    share->normalized_path.length= path_length;
125
 
 
126
 
    share->version=       refresh_version;
127
 
 
128
 
    memcpy(&share->mem_root, &mem_root, sizeof(mem_root));
129
 
    pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST);
130
 
    pthread_cond_init(&share->cond, NULL);
131
 
  }
132
 
  return(share);
133
 
}
134
 
 
135
 
 
136
 
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
137
 
{
138
 
  enum_field_types field_type;
139
 
 
140
 
  switch(proto_field_type)
141
 
  {
142
 
  case message::Table::Field::INTEGER:
143
 
    field_type= DRIZZLE_TYPE_LONG;
144
 
    break;
145
 
  case message::Table::Field::DOUBLE:
146
 
    field_type= DRIZZLE_TYPE_DOUBLE;
147
 
    break;
148
 
  case message::Table::Field::TIMESTAMP:
149
 
    field_type= DRIZZLE_TYPE_TIMESTAMP;
150
 
    break;
151
 
  case message::Table::Field::BIGINT:
152
 
    field_type= DRIZZLE_TYPE_LONGLONG;
153
 
    break;
154
 
  case message::Table::Field::DATETIME:
155
 
    field_type= DRIZZLE_TYPE_DATETIME;
156
 
    break;
157
 
  case message::Table::Field::DATE:
158
 
    field_type= DRIZZLE_TYPE_DATE;
159
 
    break;
160
 
  case message::Table::Field::VARCHAR:
161
 
    field_type= DRIZZLE_TYPE_VARCHAR;
162
 
    break;
163
 
  case message::Table::Field::DECIMAL:
164
 
    field_type= DRIZZLE_TYPE_DECIMAL;
165
 
    break;
166
 
  case message::Table::Field::ENUM:
167
 
    field_type= DRIZZLE_TYPE_ENUM;
168
 
    break;
169
 
  case message::Table::Field::BLOB:
170
 
    field_type= DRIZZLE_TYPE_BLOB;
171
 
    break;
172
 
  default:
173
 
    field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
174
 
    assert(1);
175
 
  }
176
 
 
177
 
  return field_type;
178
 
}
179
 
 
180
 
static Item *default_value_item(enum_field_types field_type,
181
 
                                const CHARSET_INFO *charset,
182
 
                                bool default_null, const string *default_value,
183
 
                                const string *default_bin_value)
184
 
{
185
 
  Item *default_item= NULL;
186
 
  int error= 0;
187
 
 
188
 
  if (default_null)
189
 
  {
190
 
    return new Item_null();
191
 
  }
192
 
 
193
 
  switch(field_type)
194
 
  {
195
 
  case DRIZZLE_TYPE_LONG:
196
 
  case DRIZZLE_TYPE_LONGLONG:
197
 
    default_item= new Item_int(default_value->c_str(),
198
 
                               (int64_t) internal::my_strtoll10(default_value->c_str(),
199
 
                                                                NULL,
200
 
                                                                &error),
201
 
                               default_value->length());
202
 
    break;
203
 
  case DRIZZLE_TYPE_DOUBLE:
204
 
    default_item= new Item_float(default_value->c_str(),
205
 
                                 default_value->length());
206
 
    break;
207
 
  case DRIZZLE_TYPE_NULL:
208
 
    assert(false);
209
 
  case DRIZZLE_TYPE_TIMESTAMP:
210
 
  case DRIZZLE_TYPE_DATETIME:
211
 
  case DRIZZLE_TYPE_DATE:
212
 
    if (default_value->compare("NOW()") == 0)
213
 
      break;
214
 
  case DRIZZLE_TYPE_ENUM:
215
 
    default_item= new Item_string(default_value->c_str(),
216
 
                                  default_value->length(),
217
 
                                  system_charset_info);
218
 
    break;
219
 
  case DRIZZLE_TYPE_VARCHAR:
220
 
  case DRIZZLE_TYPE_BLOB: /* Blob is here due to TINYTEXT. Feel the hate. */
221
 
    if (charset==&my_charset_bin)
222
 
    {
223
 
      default_item= new Item_string(default_bin_value->c_str(),
224
 
                                    default_bin_value->length(),
225
 
                                    &my_charset_bin);
226
 
    }
227
 
    else
228
 
    {
229
 
      default_item= new Item_string(default_value->c_str(),
230
 
                                    default_value->length(),
231
 
                                    system_charset_info);
232
 
    }
233
 
    break;
234
 
  case DRIZZLE_TYPE_DECIMAL:
235
 
    default_item= new Item_decimal(default_value->c_str(),
236
 
                                   default_value->length(),
237
 
                                   system_charset_info);
238
 
    break;
239
 
  }
240
 
 
241
 
  return default_item;
242
 
}
243
 
 
244
 
int parse_table_proto(Session& session,
245
 
                      message::Table &table,
246
 
                      TableShare *share)
247
 
{
248
 
  int error= 0;
249
 
 
250
 
  if (! table.IsInitialized())
251
 
  {
252
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
253
 
    return ER_CORRUPT_TABLE_DEFINITION;
254
 
  }
255
 
 
256
 
  share->setTableProto(new(nothrow) message::Table(table));
257
 
 
258
 
  share->storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
259
 
  assert(share->storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
260
 
 
261
 
  message::Table::TableOptions table_options;
262
 
 
263
 
  if (table.has_options())
264
 
    table_options= table.options();
265
 
 
266
 
  uint32_t db_create_options= 0;
267
 
 
268
 
  if (table_options.has_pack_keys())
269
 
  {
270
 
    if (table_options.pack_keys())
271
 
      db_create_options|= HA_OPTION_PACK_KEYS;
272
 
    else
273
 
      db_create_options|= HA_OPTION_NO_PACK_KEYS;
274
 
  }
275
 
 
276
 
  if (table_options.pack_record())
277
 
    db_create_options|= HA_OPTION_PACK_RECORD;
278
 
 
279
 
  /* db_create_options was stored as 2 bytes in FRM
280
 
     Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
281
 
   */
282
 
  share->db_create_options= (db_create_options & 0x0000FFFF);
283
 
  share->db_options_in_use= share->db_create_options;
284
 
 
285
 
  share->row_type= table_options.has_row_type() ?
286
 
    (enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
287
 
 
288
 
  share->block_size= table_options.has_block_size() ?
289
 
    table_options.block_size() : 0;
290
 
 
291
 
  share->table_charset= get_charset(table_options.has_collation_id()?
292
 
                                    table_options.collation_id() : 0);
293
 
 
294
 
  if (!share->table_charset)
295
 
  {
296
 
    /* unknown charset in head[38] or pre-3.23 frm */
297
 
    if (use_mb(default_charset_info))
298
 
    {
299
 
      /* Warn that we may be changing the size of character columns */
300
 
      errmsg_printf(ERRMSG_LVL_WARN,
301
 
                    _("'%s' had no or invalid character set, "
302
 
                      "and default character set is multi-byte, "
303
 
                      "so character column sizes may have changed"),
304
 
                    share->path.str);
305
 
    }
306
 
    share->table_charset= default_charset_info;
307
 
  }
308
 
 
309
 
  share->db_record_offset= 1;
310
 
 
311
 
  share->blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
312
 
 
313
 
  share->keys= table.indexes_size();
314
 
 
315
 
  share->key_parts= 0;
316
 
  for (int indx= 0; indx < table.indexes_size(); indx++)
317
 
    share->key_parts+= table.indexes(indx).index_part_size();
318
 
 
319
 
  share->key_info= (KEY*) alloc_root(&share->mem_root,
320
 
                                     table.indexes_size() * sizeof(KEY)
321
 
                                     +share->key_parts*sizeof(KEY_PART_INFO));
322
 
 
323
 
  KEY_PART_INFO *key_part;
324
 
 
325
 
  key_part= reinterpret_cast<KEY_PART_INFO*>
326
 
    (share->key_info+table.indexes_size());
327
 
 
328
 
 
329
 
  ulong *rec_per_key= (ulong*) alloc_root(&share->mem_root,
330
 
                                            sizeof(ulong*)*share->key_parts);
331
 
 
332
 
  share->keynames.count= table.indexes_size();
333
 
  share->keynames.name= NULL;
334
 
  share->keynames.type_names= (const char**)
335
 
    alloc_root(&share->mem_root, sizeof(char*) * (table.indexes_size()+1));
336
 
 
337
 
  share->keynames.type_lengths= (unsigned int*)
338
 
    alloc_root(&share->mem_root,
339
 
               sizeof(unsigned int) * (table.indexes_size()+1));
340
 
 
341
 
  share->keynames.type_names[share->keynames.count]= NULL;
342
 
  share->keynames.type_lengths[share->keynames.count]= 0;
343
 
 
344
 
  KEY* keyinfo= share->key_info;
345
 
  for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
346
 
  {
347
 
    message::Table::Index indx= table.indexes(keynr);
348
 
 
349
 
    keyinfo->table= 0;
350
 
    keyinfo->flags= 0;
351
 
 
352
 
    if (indx.is_unique())
353
 
      keyinfo->flags|= HA_NOSAME;
354
 
 
355
 
    if (indx.has_options())
356
 
    {
357
 
      message::Table::Index::IndexOptions indx_options= indx.options();
358
 
      if (indx_options.pack_key())
359
 
        keyinfo->flags|= HA_PACK_KEY;
360
 
 
361
 
      if (indx_options.var_length_key())
362
 
        keyinfo->flags|= HA_VAR_LENGTH_PART;
363
 
 
364
 
      if (indx_options.null_part_key())
365
 
        keyinfo->flags|= HA_NULL_PART_KEY;
366
 
 
367
 
      if (indx_options.binary_pack_key())
368
 
        keyinfo->flags|= HA_BINARY_PACK_KEY;
369
 
 
370
 
      if (indx_options.has_partial_segments())
371
 
        keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
372
 
 
373
 
      if (indx_options.auto_generated_key())
374
 
        keyinfo->flags|= HA_GENERATED_KEY;
375
 
 
376
 
      if (indx_options.has_key_block_size())
377
 
      {
378
 
        keyinfo->flags|= HA_USES_BLOCK_SIZE;
379
 
        keyinfo->block_size= indx_options.key_block_size();
380
 
      }
381
 
      else
382
 
      {
383
 
        keyinfo->block_size= 0;
384
 
      }
385
 
    }
386
 
 
387
 
    switch (indx.type())
388
 
    {
389
 
    case message::Table::Index::UNKNOWN_INDEX:
390
 
      keyinfo->algorithm= HA_KEY_ALG_UNDEF;
391
 
      break;
392
 
    case message::Table::Index::BTREE:
393
 
      keyinfo->algorithm= HA_KEY_ALG_BTREE;
394
 
      break;
395
 
    case message::Table::Index::HASH:
396
 
      keyinfo->algorithm= HA_KEY_ALG_HASH;
397
 
      break;
398
 
 
399
 
    default:
400
 
      /* TODO: suitable warning ? */
401
 
      keyinfo->algorithm= HA_KEY_ALG_UNDEF;
402
 
      break;
403
 
    }
404
 
 
405
 
    keyinfo->key_length= indx.key_length();
406
 
 
407
 
    keyinfo->key_parts= indx.index_part_size();
408
 
 
409
 
    keyinfo->key_part= key_part;
410
 
    keyinfo->rec_per_key= rec_per_key;
411
 
 
412
 
    for (unsigned int partnr= 0;
413
 
         partnr < keyinfo->key_parts;
414
 
         partnr++, key_part++)
415
 
    {
416
 
      message::Table::Index::IndexPart part;
417
 
      part= indx.index_part(partnr);
418
 
 
419
 
      *rec_per_key++= 0;
420
 
 
421
 
      key_part->field= NULL;
422
 
      key_part->fieldnr= part.fieldnr() + 1; // start from 1.
423
 
      key_part->null_bit= 0;
424
 
      /* key_part->null_offset is only set if null_bit (see later) */
425
 
      /* key_part->key_type= */ /* I *THINK* this may be okay.... */
426
 
      /* key_part->type ???? */
427
 
      key_part->key_part_flag= 0;
428
 
      if (part.has_in_reverse_order())
429
 
        key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
430
 
 
431
 
      key_part->length= part.compare_length();
432
 
 
433
 
      key_part->store_length= key_part->length;
434
 
 
435
 
      /* key_part->offset is set later */
436
 
      key_part->key_type= part.key_type();
437
 
    }
438
 
 
439
 
    if (! indx.has_comment())
440
 
    {
441
 
      keyinfo->comment.length= 0;
442
 
      keyinfo->comment.str= NULL;
443
 
    }
444
 
    else
445
 
    {
446
 
      keyinfo->flags|= HA_USES_COMMENT;
447
 
      keyinfo->comment.length= indx.comment().length();
448
 
      keyinfo->comment.str= strmake_root(&share->mem_root,
449
 
                                         indx.comment().c_str(),
450
 
                                         keyinfo->comment.length);
451
 
    }
452
 
 
453
 
    keyinfo->name= strmake_root(&share->mem_root,
454
 
                                indx.name().c_str(),
455
 
                                indx.name().length());
456
 
 
457
 
    share->keynames.type_names[keynr]= keyinfo->name;
458
 
    share->keynames.type_lengths[keynr]= indx.name().length();
459
 
  }
460
 
 
461
 
  share->keys_for_keyread.reset();
462
 
  set_prefix(share->keys_in_use, share->keys);
463
 
 
464
 
  share->fields= table.field_size();
465
 
 
466
 
  share->field= (Field**) alloc_root(&share->mem_root,
467
 
                                     ((share->fields+1) * sizeof(Field*)));
468
 
  share->field[share->fields]= NULL;
469
 
 
470
 
  uint32_t null_fields= 0;
471
 
  share->reclength= 0;
472
 
 
473
 
  uint32_t *field_offsets= (uint32_t*)malloc(share->fields * sizeof(uint32_t));
474
 
  uint32_t *field_pack_length=(uint32_t*)malloc(share->fields*sizeof(uint32_t));
475
 
 
476
 
  assert(field_offsets && field_pack_length); // TODO: fixme
477
 
 
478
 
  uint32_t interval_count= 0;
479
 
  uint32_t interval_parts= 0;
480
 
 
481
 
  uint32_t stored_columns_reclength= 0;
482
 
 
483
 
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
484
 
  {
485
 
    message::Table::Field pfield= table.field(fieldnr);
486
 
    if (pfield.constraints().is_nullable())
487
 
      null_fields++;
488
 
 
489
 
    enum_field_types drizzle_field_type=
490
 
      proto_field_type_to_drizzle_type(pfield.type());
491
 
 
492
 
    field_offsets[fieldnr]= stored_columns_reclength;
493
 
 
494
 
    /* the below switch is very similar to
495
 
       CreateField::create_length_to_internal_length in field.cc
496
 
       (which should one day be replace by just this code)
497
 
    */
498
 
    switch(drizzle_field_type)
499
 
    {
500
 
    case DRIZZLE_TYPE_BLOB:
501
 
    case DRIZZLE_TYPE_VARCHAR:
502
 
      {
503
 
        message::Table::Field::StringFieldOptions field_options= pfield.string_options();
504
 
 
505
 
        const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
506
 
                                            field_options.collation_id() : 0);
507
 
 
508
 
        if (! cs)
509
 
          cs= default_charset_info;
510
 
 
511
 
        field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
512
 
                                                     field_options.length() * cs->mbmaxlen);
513
 
      }
514
 
      break;
515
 
    case DRIZZLE_TYPE_ENUM:
516
 
      {
517
 
        message::Table::Field::SetFieldOptions field_options= pfield.set_options();
518
 
 
519
 
        field_pack_length[fieldnr]=
520
 
          get_enum_pack_length(field_options.field_value_size());
521
 
 
522
 
        interval_count++;
523
 
        interval_parts+= field_options.field_value_size();
524
 
      }
525
 
      break;
526
 
    case DRIZZLE_TYPE_DECIMAL:
527
 
      {
528
 
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
529
 
 
530
 
        field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
531
 
      }
532
 
      break;
533
 
    default:
534
 
      /* Zero is okay here as length is fixed for other types. */
535
 
      field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
536
 
    }
537
 
 
538
 
    share->reclength+= field_pack_length[fieldnr];
539
 
    stored_columns_reclength+= field_pack_length[fieldnr];
540
 
  }
541
 
 
542
 
  /* data_offset added to stored_rec_length later */
543
 
  share->stored_rec_length= stored_columns_reclength;
544
 
 
545
 
  share->null_fields= null_fields;
546
 
 
547
 
  ulong null_bits= null_fields;
548
 
  if (! table_options.pack_record())
549
 
    null_bits++;
550
 
  ulong data_offset= (null_bits + 7)/8;
551
 
 
552
 
 
553
 
  share->reclength+= data_offset;
554
 
  share->stored_rec_length+= data_offset;
555
 
 
556
 
  ulong rec_buff_length;
557
 
 
558
 
  rec_buff_length= ALIGN_SIZE(share->reclength + 1);
559
 
  share->rec_buff_length= rec_buff_length;
560
 
 
561
 
  unsigned char* record= NULL;
562
 
 
563
 
  if (! (record= (unsigned char *) alloc_root(&share->mem_root,
564
 
                                              rec_buff_length)))
565
 
    abort();
566
 
 
567
 
  memset(record, 0, rec_buff_length);
568
 
 
569
 
  int null_count= 0;
570
 
 
571
 
  if (! table_options.pack_record())
572
 
  {
573
 
    null_count++; // one bit for delete mark.
574
 
    *record|= 1;
575
 
  }
576
 
 
577
 
  share->default_values= record;
578
 
 
579
 
  if (interval_count)
580
 
  {
581
 
    share->intervals= (TYPELIB *) alloc_root(&share->mem_root,
582
 
                                           interval_count*sizeof(TYPELIB));
583
 
  }
584
 
  else
585
 
    share->intervals= NULL;
586
 
 
587
 
  share->fieldnames.type_names= (const char **) alloc_root(&share->mem_root,
588
 
                                                          (share->fields + 1) * sizeof(char*));
589
 
 
590
 
  share->fieldnames.type_lengths= (unsigned int *) alloc_root(&share->mem_root,
591
 
                                                             (share->fields + 1) * sizeof(unsigned int));
592
 
 
593
 
  share->fieldnames.type_names[share->fields]= NULL;
594
 
  share->fieldnames.type_lengths[share->fields]= 0;
595
 
  share->fieldnames.count= share->fields;
596
 
 
597
 
 
598
 
  /* Now fix the TYPELIBs for the intervals (enum values)
599
 
     and field names.
600
 
   */
601
 
 
602
 
  uint32_t interval_nr= 0;
603
 
 
604
 
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
605
 
  {
606
 
    message::Table::Field pfield= table.field(fieldnr);
607
 
 
608
 
    /* field names */
609
 
    share->fieldnames.type_names[fieldnr]= strmake_root(&share->mem_root,
610
 
                                                        pfield.name().c_str(),
611
 
                                                        pfield.name().length());
612
 
 
613
 
    share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
614
 
 
615
 
    /* enum typelibs */
616
 
    if (pfield.type() != message::Table::Field::ENUM)
617
 
      continue;
618
 
 
619
 
    message::Table::Field::SetFieldOptions field_options= pfield.set_options();
620
 
 
621
 
    const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
622
 
                                             field_options.collation_id() : 0);
623
 
 
624
 
    if (! charset)
625
 
      charset= default_charset_info;
626
 
 
627
 
    TYPELIB *t= &(share->intervals[interval_nr]);
628
 
 
629
 
    t->type_names= (const char**)alloc_root(&share->mem_root,
630
 
                                            (field_options.field_value_size() + 1) * sizeof(char*));
631
 
 
632
 
    t->type_lengths= (unsigned int*) alloc_root(&share->mem_root,
633
 
                                                (field_options.field_value_size() + 1) * sizeof(unsigned int));
634
 
 
635
 
    t->type_names[field_options.field_value_size()]= NULL;
636
 
    t->type_lengths[field_options.field_value_size()]= 0;
637
 
 
638
 
    t->count= field_options.field_value_size();
639
 
    t->name= NULL;
640
 
 
641
 
    for (int n= 0; n < field_options.field_value_size(); n++)
642
 
    {
643
 
      t->type_names[n]= strmake_root(&share->mem_root,
644
 
                                     field_options.field_value(n).c_str(),
645
 
                                     field_options.field_value(n).length());
646
 
 
647
 
      /* 
648
 
       * Go ask the charset what the length is as for "" length=1
649
 
       * and there's stripping spaces or some other crack going on.
650
 
       */
651
 
      uint32_t lengthsp;
652
 
      lengthsp= charset->cset->lengthsp(charset,
653
 
                                        t->type_names[n],
654
 
                                        field_options.field_value(n).length());
655
 
      t->type_lengths[n]= lengthsp;
656
 
    }
657
 
    interval_nr++;
658
 
  }
659
 
 
660
 
 
661
 
  /* and read the fields */
662
 
  interval_nr= 0;
663
 
 
664
 
  bool use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
665
 
 
666
 
  if (use_hash)
667
 
    use_hash= ! hash_init(&share->name_hash,
668
 
                          system_charset_info,
669
 
                          share->fields,
670
 
                          0,
671
 
                          0,
672
 
                          (hash_get_key) get_field_name,
673
 
                          0,
674
 
                          0);
675
 
 
676
 
  unsigned char* null_pos= record;;
677
 
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
678
 
 
679
 
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
680
 
  {
681
 
    message::Table::Field pfield= table.field(fieldnr);
682
 
 
683
 
    enum column_format_type column_format= COLUMN_FORMAT_TYPE_DEFAULT;
684
 
 
685
 
    switch (pfield.format())
686
 
    {
687
 
    case message::Table::Field::DefaultFormat:
688
 
      column_format= COLUMN_FORMAT_TYPE_DEFAULT;
689
 
      break;
690
 
    case message::Table::Field::FixedFormat:
691
 
      column_format= COLUMN_FORMAT_TYPE_FIXED;
692
 
      break;
693
 
    case message::Table::Field::DynamicFormat:
694
 
      column_format= COLUMN_FORMAT_TYPE_DYNAMIC;
695
 
      break;
696
 
    default:
697
 
      assert(1);
698
 
    }
699
 
 
700
 
    Field::utype unireg_type= Field::NONE;
701
 
 
702
 
    if (pfield.has_numeric_options() &&
703
 
        pfield.numeric_options().is_autoincrement())
704
 
    {
705
 
      unireg_type= Field::NEXT_NUMBER;
706
 
    }
707
 
 
708
 
    if (pfield.has_options() &&
709
 
        pfield.options().has_default_value() &&
710
 
        pfield.options().default_value().compare("NOW()") == 0)
711
 
    {
712
 
      if (pfield.options().has_update_value() &&
713
 
          pfield.options().update_value().compare("NOW()") == 0)
714
 
      {
715
 
        unireg_type= Field::TIMESTAMP_DNUN_FIELD;
716
 
      }
717
 
      else if (! pfield.options().has_update_value())
718
 
      {
719
 
        unireg_type= Field::TIMESTAMP_DN_FIELD;
720
 
      }
721
 
      else
722
 
        assert(1); // Invalid update value.
723
 
    }
724
 
    else if (pfield.has_options() &&
725
 
             pfield.options().has_update_value() &&
726
 
             pfield.options().update_value().compare("NOW()") == 0)
727
 
    {
728
 
      unireg_type= Field::TIMESTAMP_UN_FIELD;
729
 
    }
730
 
 
731
 
    LEX_STRING comment;
732
 
    if (!pfield.has_comment())
733
 
    {
734
 
      comment.str= (char*)"";
735
 
      comment.length= 0;
736
 
    }
737
 
    else
738
 
    {
739
 
      size_t len= pfield.comment().length();
740
 
      const char* str= pfield.comment().c_str();
741
 
 
742
 
      comment.str= strmake_root(&share->mem_root, str, len);
743
 
      comment.length= len;
744
 
    }
745
 
 
746
 
    enum_field_types field_type;
747
 
 
748
 
    field_type= proto_field_type_to_drizzle_type(pfield.type());
749
 
 
750
 
    const CHARSET_INFO *charset= &my_charset_bin;
751
 
 
752
 
    if (field_type == DRIZZLE_TYPE_BLOB ||
753
 
        field_type == DRIZZLE_TYPE_VARCHAR)
754
 
    {
755
 
      message::Table::Field::StringFieldOptions field_options= pfield.string_options();
756
 
 
757
 
      charset= get_charset(field_options.has_collation_id() ?
758
 
                           field_options.collation_id() : 0);
759
 
 
760
 
      if (! charset)
761
 
        charset= default_charset_info;
762
 
    }
763
 
 
764
 
    if (field_type == DRIZZLE_TYPE_ENUM)
765
 
    {
766
 
      message::Table::Field::SetFieldOptions field_options= pfield.set_options();
767
 
 
768
 
      charset= get_charset(field_options.has_collation_id()?
769
 
                           field_options.collation_id() : 0);
770
 
 
771
 
      if (! charset)
772
 
              charset= default_charset_info;
773
 
    }
774
 
 
775
 
    uint8_t decimals= 0;
776
 
    if (field_type == DRIZZLE_TYPE_DECIMAL
777
 
        || field_type == DRIZZLE_TYPE_DOUBLE)
778
 
    {
779
 
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
780
 
 
781
 
      if (! pfield.has_numeric_options() || ! fo.has_scale())
782
 
      {
783
 
        /*
784
 
          We don't write the default to table proto so
785
 
          if no decimals specified for DOUBLE, we use the default.
786
 
        */
787
 
        decimals= NOT_FIXED_DEC;
788
 
      }
789
 
      else
790
 
      {
791
 
        if (fo.scale() > DECIMAL_MAX_SCALE)
792
 
        {
793
 
          error= 4;
794
 
          goto err;
795
 
        }
796
 
        decimals= static_cast<uint8_t>(fo.scale());
797
 
      }
798
 
    }
799
 
 
800
 
    Item *default_value= NULL;
801
 
 
802
 
    if (pfield.options().has_default_value() ||
803
 
        pfield.options().has_default_null()  ||
804
 
        pfield.options().has_default_bin_value())
805
 
    {
806
 
      default_value= default_value_item(field_type,
807
 
                                        charset,
808
 
                                        pfield.options().default_null(),
809
 
                                        &pfield.options().default_value(),
810
 
                                        &pfield.options().default_bin_value());
811
 
    }
812
 
 
813
 
 
814
 
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
815
 
    memset(&temp_table, 0, sizeof(temp_table));
816
 
    temp_table.s= share;
817
 
    temp_table.in_use= &session;
818
 
    temp_table.s->db_low_byte_first= true; //Cursor->low_byte_first();
819
 
    temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
820
 
 
821
 
    uint32_t field_length= 0; //Assignment is for compiler complaint.
822
 
 
823
 
    switch (field_type)
824
 
    {
825
 
    case DRIZZLE_TYPE_BLOB:
826
 
    case DRIZZLE_TYPE_VARCHAR:
827
 
    {
828
 
      message::Table::Field::StringFieldOptions field_options= pfield.string_options();
829
 
 
830
 
      charset= get_charset(field_options.has_collation_id() ?
831
 
                           field_options.collation_id() : 0);
832
 
 
833
 
      if (! charset)
834
 
        charset= default_charset_info;
835
 
 
836
 
      field_length= field_options.length() * charset->mbmaxlen;
837
 
    }
838
 
      break;
839
 
    case DRIZZLE_TYPE_DOUBLE:
840
 
    {
841
 
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
842
 
      if (!fo.has_precision() && !fo.has_scale())
843
 
      {
844
 
        field_length= DBL_DIG+7;
845
 
      }
846
 
      else
847
 
      {
848
 
        field_length= fo.precision();
849
 
      }
850
 
      if (field_length < decimals &&
851
 
          decimals != NOT_FIXED_DEC)
852
 
      {
853
 
        my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
854
 
        error= 1;
855
 
        goto err;
856
 
      }
857
 
      break;
858
 
    }
859
 
    case DRIZZLE_TYPE_DECIMAL:
860
 
    {
861
 
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
862
 
 
863
 
      field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
864
 
                                                   false);
865
 
      break;
866
 
    }
867
 
    case DRIZZLE_TYPE_TIMESTAMP:
868
 
    case DRIZZLE_TYPE_DATETIME:
869
 
      field_length= DateTime::MAX_STRING_LENGTH;
870
 
      break;
871
 
    case DRIZZLE_TYPE_DATE:
872
 
      field_length= Date::MAX_STRING_LENGTH;
873
 
      break;
874
 
    case DRIZZLE_TYPE_ENUM:
875
 
    {
876
 
      field_length= 0;
877
 
 
878
 
      message::Table::Field::SetFieldOptions fo= pfield.set_options();
879
 
 
880
 
      for(int valnr= 0; valnr < fo.field_value_size(); valnr++)
881
 
      {
882
 
        if (fo.field_value(valnr).length() > field_length)
883
 
          field_length= charset->cset->numchars(charset,
884
 
                                                fo.field_value(valnr).c_str(),
885
 
                                                fo.field_value(valnr).c_str()
886
 
                                                + fo.field_value(valnr).length())
887
 
            * charset->mbmaxlen;
888
 
      }
889
 
    }
890
 
      break;
891
 
    case DRIZZLE_TYPE_LONG:
892
 
      {
893
 
        uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
894
 
          field_length= MAX_INT_WIDTH+sign_len;
895
 
      }
896
 
      break;
897
 
    case DRIZZLE_TYPE_LONGLONG:
898
 
      field_length= MAX_BIGINT_WIDTH;
899
 
      break;
900
 
    case DRIZZLE_TYPE_NULL:
901
 
      abort(); // Programming error
902
 
    }
903
 
 
904
 
    Field* f= make_field(share,
905
 
                         &share->mem_root,
906
 
                         record + field_offsets[fieldnr] + data_offset,
907
 
                         field_length,
908
 
                         pfield.constraints().is_nullable(),
909
 
                         null_pos,
910
 
                         null_bit_pos,
911
 
                         decimals,
912
 
                         field_type,
913
 
                         charset,
914
 
                         (Field::utype) MTYP_TYPENR(unireg_type),
915
 
                         ((field_type == DRIZZLE_TYPE_ENUM) ?
916
 
                          share->intervals + (interval_nr++)
917
 
                          : (TYPELIB*) 0),
918
 
                         share->fieldnames.type_names[fieldnr]);
919
 
 
920
 
    share->field[fieldnr]= f;
921
 
 
922
 
    f->init(&temp_table); /* blob default values need table obj */
923
 
 
924
 
    if (! (f->flags & NOT_NULL_FLAG))
925
 
    {
926
 
      *f->null_ptr|= f->null_bit;
927
 
      if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
928
 
        null_pos++;
929
 
      null_count++;
930
 
    }
931
 
 
932
 
    if (default_value)
933
 
    {
934
 
      enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
935
 
      session.count_cuted_fields= CHECK_FIELD_WARN;
936
 
      int res= default_value->save_in_field(f, 1);
937
 
      session.count_cuted_fields= old_count_cuted_fields;
938
 
      if (res != 0 && res != 3) /* @TODO Huh? */
939
 
      {
940
 
        my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
941
 
        error= 1;
942
 
        goto err;
943
 
      }
944
 
    }
945
 
    else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
946
 
             (f->flags & NOT_NULL_FLAG))
947
 
    {
948
 
      f->set_notnull();
949
 
      f->store((int64_t) 1, true);
950
 
    }
951
 
    else
952
 
      f->reset();
953
 
 
954
 
    /* hack to undo f->init() */
955
 
    f->table= NULL;
956
 
    f->orig_table= NULL;
957
 
 
958
 
    f->field_index= fieldnr;
959
 
    f->comment= comment;
960
 
    if (! default_value &&
961
 
        ! (f->unireg_check==Field::NEXT_NUMBER) &&
962
 
        (f->flags & NOT_NULL_FLAG) &&
963
 
        (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
964
 
    {
965
 
      f->flags|= NO_DEFAULT_VALUE_FLAG;
966
 
    }
967
 
 
968
 
    if (f->unireg_check == Field::NEXT_NUMBER)
969
 
      share->found_next_number_field= &(share->field[fieldnr]);
970
 
 
971
 
    if (share->timestamp_field == f)
972
 
      share->timestamp_field_offset= fieldnr;
973
 
 
974
 
    if (use_hash) /* supposedly this never fails... but comments lie */
975
 
      (void) my_hash_insert(&share->name_hash,
976
 
                            (unsigned char*)&(share->field[fieldnr]));
977
 
 
978
 
  }
979
 
 
980
 
  keyinfo= share->key_info;
981
 
  for (unsigned int keynr= 0; keynr < share->keys; keynr++, keyinfo++)
982
 
  {
983
 
    key_part= keyinfo->key_part;
984
 
 
985
 
    for (unsigned int partnr= 0;
986
 
         partnr < keyinfo->key_parts;
987
 
         partnr++, key_part++)
988
 
    {
989
 
      /* 
990
 
       * Fix up key_part->offset by adding data_offset.
991
 
       * We really should compute offset as well.
992
 
       * But at least this way we are a little better.
993
 
       */
994
 
      key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
995
 
    }
996
 
  }
997
 
 
998
 
  /*
999
 
    We need to set the unused bits to 1. If the number of bits is a multiple
1000
 
    of 8 there are no unused bits.
1001
 
  */
1002
 
 
1003
 
  if (null_count & 7)
1004
 
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1005
 
 
1006
 
  share->null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1007
 
 
1008
 
  share->last_null_bit_pos= null_bit_pos;
1009
 
 
1010
 
  free(field_offsets);
1011
 
  field_offsets= NULL;
1012
 
  free(field_pack_length);
1013
 
  field_pack_length= NULL;
1014
 
 
1015
 
  /* Fix key stuff */
1016
 
  if (share->key_parts)
1017
 
  {
1018
 
    uint32_t primary_key= (uint32_t) (find_type((char*) "PRIMARY",
1019
 
                                                &share->keynames, 3) - 1); /* @TODO Huh? */
1020
 
 
1021
 
    keyinfo= share->key_info;
1022
 
    key_part= keyinfo->key_part;
1023
 
 
1024
 
    for (uint32_t key= 0; key < share->keys; key++,keyinfo++)
1025
 
    {
1026
 
      uint32_t usable_parts= 0;
1027
 
 
1028
 
      if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1029
 
      {
1030
 
        /*
1031
 
          If the UNIQUE key doesn't have NULL columns and is not a part key
1032
 
          declare this as a primary key.
1033
 
        */
1034
 
        primary_key=key;
1035
 
        for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1036
 
        {
1037
 
          uint32_t fieldnr= key_part[i].fieldnr;
1038
 
          if (! fieldnr ||
1039
 
              share->field[fieldnr-1]->null_ptr ||
1040
 
              share->field[fieldnr-1]->key_length() != key_part[i].length)
1041
 
          {
1042
 
            primary_key= MAX_KEY; // Can't be used
1043
 
            break;
1044
 
          }
1045
 
        }
1046
 
      }
1047
 
 
1048
 
      for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1049
 
      {
1050
 
        Field *field;
1051
 
        if (! key_part->fieldnr)
1052
 
        {
1053
 
          abort(); // goto err;
1054
 
        }
1055
 
        field= key_part->field= share->field[key_part->fieldnr-1];
1056
 
        key_part->type= field->key_type();
1057
 
        if (field->null_ptr)
1058
 
        {
1059
 
          key_part->null_offset=(uint32_t) ((unsigned char*) field->null_ptr -
1060
 
                                        share->default_values);
1061
 
          key_part->null_bit= field->null_bit;
1062
 
          key_part->store_length+=HA_KEY_NULL_LENGTH;
1063
 
          keyinfo->flags|=HA_NULL_PART_KEY;
1064
 
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1065
 
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1066
 
        }
1067
 
        if (field->type() == DRIZZLE_TYPE_BLOB ||
1068
 
            field->real_type() == DRIZZLE_TYPE_VARCHAR)
1069
 
        {
1070
 
          if (field->type() == DRIZZLE_TYPE_BLOB)
1071
 
            key_part->key_part_flag|= HA_BLOB_PART;
1072
 
          else
1073
 
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1074
 
          keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1075
 
          key_part->store_length+=HA_KEY_BLOB_LENGTH;
1076
 
          keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1077
 
        }
1078
 
        if (i == 0 && key != primary_key)
1079
 
          field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1080
 
                           (keyinfo->key_parts == 1)) ?
1081
 
                           UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1082
 
        if (i == 0)
1083
 
          field->key_start.set(key);
1084
 
        if (field->key_length() == key_part->length &&
1085
 
            !(field->flags & BLOB_FLAG))
1086
 
        {
1087
 
          enum ha_key_alg algo= share->key_info[key].algorithm;
1088
 
          if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1089
 
          {
1090
 
            share->keys_for_keyread.set(key);
1091
 
            field->part_of_key.set(key);
1092
 
            field->part_of_key_not_clustered.set(key);
1093
 
          }
1094
 
          if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1095
 
            field->part_of_sortkey.set(key);
1096
 
        }
1097
 
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1098
 
            usable_parts == i)
1099
 
          usable_parts++;                       // For FILESORT
1100
 
        field->flags|= PART_KEY_FLAG;
1101
 
        if (key == primary_key)
1102
 
        {
1103
 
          field->flags|= PRI_KEY_FLAG;
1104
 
          /*
1105
 
            If this field is part of the primary key and all keys contains
1106
 
            the primary key, then we can use any key to find this column
1107
 
          */
1108
 
          if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1109
 
          {
1110
 
            field->part_of_key= share->keys_in_use;
1111
 
            if (field->part_of_sortkey.test(key))
1112
 
              field->part_of_sortkey= share->keys_in_use;
1113
 
          }
1114
 
        }
1115
 
        if (field->key_length() != key_part->length)
1116
 
        {
1117
 
          key_part->key_part_flag|= HA_PART_KEY_SEG;
1118
 
        }
1119
 
      }
1120
 
      keyinfo->usable_key_parts= usable_parts; // Filesort
1121
 
 
1122
 
      set_if_bigger(share->max_key_length,keyinfo->key_length+
1123
 
                    keyinfo->key_parts);
1124
 
      share->total_key_length+= keyinfo->key_length;
1125
 
 
1126
 
      if (keyinfo->flags & HA_NOSAME)
1127
 
      {
1128
 
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
1129
 
      }
1130
 
    }
1131
 
    if (primary_key < MAX_KEY &&
1132
 
        (share->keys_in_use.test(primary_key)))
1133
 
    {
1134
 
      share->primary_key= primary_key;
1135
 
      /*
1136
 
        If we are using an integer as the primary key then allow the user to
1137
 
        refer to it as '_rowid'
1138
 
      */
1139
 
      if (share->key_info[primary_key].key_parts == 1)
1140
 
      {
1141
 
        Field *field= share->key_info[primary_key].key_part[0].field;
1142
 
        if (field && field->result_type() == INT_RESULT)
1143
 
        {
1144
 
          /* note that fieldnr here (and rowid_field_offset) starts from 1 */
1145
 
          share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
1146
 
                                      fieldnr);
1147
 
        }
1148
 
      }
1149
 
    }
1150
 
    else
1151
 
      share->primary_key = MAX_KEY; // we do not have a primary key
1152
 
  }
1153
 
  else
1154
 
    share->primary_key= MAX_KEY;
1155
 
 
1156
 
  if (share->found_next_number_field)
1157
 
  {
1158
 
    Field *reg_field= *share->found_next_number_field;
1159
 
    if ((int) (share->next_number_index= (uint32_t)
1160
 
               find_ref_key(share->key_info, share->keys,
1161
 
                            share->default_values, reg_field,
1162
 
                            &share->next_number_key_offset,
1163
 
                            &share->next_number_keypart)) < 0)
1164
 
    {
1165
 
      /* Wrong field definition */
1166
 
      error= 4;
1167
 
      goto err;
1168
 
    }
1169
 
    else
1170
 
      reg_field->flags |= AUTO_INCREMENT_FLAG;
1171
 
  }
1172
 
 
1173
 
  if (share->blob_fields)
1174
 
  {
1175
 
    Field **ptr;
1176
 
    uint32_t k, *save;
1177
 
 
1178
 
    /* Store offsets to blob fields to find them fast */
1179
 
    if (!(share->blob_field= save=
1180
 
          (uint*) alloc_root(&share->mem_root,
1181
 
                             (uint32_t) (share->blob_fields* sizeof(uint32_t)))))
1182
 
      goto err;
1183
 
    for (k= 0, ptr= share->field ; *ptr ; ptr++, k++)
1184
 
    {
1185
 
      if ((*ptr)->flags & BLOB_FLAG)
1186
 
        (*save++)= k;
1187
 
    }
1188
 
  }
1189
 
 
1190
 
  share->db_low_byte_first= true; // @todo Question this.
1191
 
  share->column_bitmap_size= bitmap_buffer_size(share->fields);
1192
 
 
1193
 
  my_bitmap_map *bitmaps;
1194
 
 
1195
 
  if (!(bitmaps= (my_bitmap_map*) alloc_root(&share->mem_root,
1196
 
                                             share->column_bitmap_size)))
1197
 
    goto err;
1198
 
  share->all_set.init(bitmaps, share->fields);
1199
 
  share->all_set.setAll();
1200
 
 
1201
 
  return (0);
1202
 
 
1203
 
err:
1204
 
  if (field_offsets)
1205
 
    free(field_offsets);
1206
 
  if (field_pack_length)
1207
 
    free(field_pack_length);
1208
 
 
1209
 
  share->error= error;
1210
 
  share->open_errno= errno;
1211
 
  share->errarg= 0;
1212
 
  hash_free(&share->name_hash);
1213
 
  share->open_table_error(error, share->open_errno, 0);
1214
 
 
1215
 
  return error;
1216
 
}
1217
 
 
1218
 
/*
1219
 
  Read table definition from a binary / text based .frm cursor
1220
 
 
1221
 
  SYNOPSIS
1222
 
  open_table_def()
1223
 
  session               Thread Cursor
1224
 
  share         Fill this with table definition
1225
 
 
1226
 
  NOTES
1227
 
    This function is called when the table definition is not cached in
1228
 
    table_def_cache
1229
 
    The data is returned in 'share', which is alloced by
1230
 
    alloc_table_share().. The code assumes that share is initialized.
1231
 
 
1232
 
  RETURN VALUES
1233
 
   0    ok
1234
 
   1    Error (see open_table_error)
1235
 
   2    Error (see open_table_error)
1236
 
   3    Wrong data in .frm cursor
1237
 
   4    Error (see open_table_error)
1238
 
   5    Error (see open_table_error: charset unavailable)
1239
 
   6    Unknown .frm version
1240
 
*/
1241
 
 
1242
 
int open_table_def(Session& session, TableShare *share)
1243
 
{
1244
 
  int error;
1245
 
  bool error_given;
1246
 
 
1247
 
  error= 1;
1248
 
  error_given= 0;
1249
 
 
1250
 
  message::Table table;
1251
 
 
1252
 
  error= plugin::StorageEngine::getTableDefinition(session, share->normalized_path.str,
1253
 
                                                   share->getSchemaName(),
1254
 
                                                   share->table_name.str,
1255
 
                                                   false,
1256
 
                                                   &table);
1257
 
 
1258
 
  if (error != EEXIST)
1259
 
  {
1260
 
    if (error > 0)
1261
 
    {
1262
 
      errno= error;
1263
 
      error= 1;
1264
 
    }
1265
 
    else
1266
 
    {
1267
 
      if (not table.IsInitialized())
1268
 
      {
1269
 
        error= 4;
1270
 
      }
1271
 
    }
1272
 
    goto err_not_open;
1273
 
  }
1274
 
 
1275
 
  error= parse_table_proto(session, table, share);
1276
 
 
1277
 
  share->table_category= TABLE_CATEGORY_USER;
1278
 
 
1279
 
err_not_open:
1280
 
  if (error && !error_given)
1281
 
  {
1282
 
    share->error= error;
1283
 
    share->open_table_error(error, (share->open_errno= errno), 0);
1284
 
  }
1285
 
 
1286
 
  return(error);
1287
 
}
1288
 
 
1289
 
 
1290
 
/*
1291
 
  Open a table based on a TableShare
1292
 
 
1293
 
  SYNOPSIS
1294
 
    open_table_from_share()
1295
 
    session                     Thread Cursor
1296
 
    share               Table definition
1297
 
    alias               Alias for table
1298
 
    db_stat             open flags (for example HA_OPEN_KEYFILE|
1299
 
                        HA_OPEN_RNDFILE..) can be 0 (example in
1300
 
                        ha_example_table)
1301
 
    ha_open_flags       HA_OPEN_ABORT_IF_LOCKED etc..
1302
 
    outparam            result table
1303
 
 
1304
 
  RETURN VALUES
1305
 
   0    ok
1306
 
   1    Error (see open_table_error)
1307
 
   2    Error (see open_table_error)
1308
 
   3    Wrong data in .frm cursor
1309
 
   4    Error (see open_table_error)
1310
 
   5    Error (see open_table_error: charset unavailable)
1311
 
   7    Table definition has changed in engine
1312
 
*/
1313
 
 
1314
 
int open_table_from_share(Session *session, TableShare *share, const char *alias,
1315
 
                          uint32_t db_stat, uint32_t ha_open_flags,
1316
 
                          Table *outparam)
1317
 
{
1318
 
  int error;
1319
 
  uint32_t records, i, bitmap_size;
1320
 
  bool error_reported= false;
1321
 
  unsigned char *record, *bitmaps;
1322
 
  Field **field_ptr;
1323
 
 
1324
 
  /* Parsing of partitioning information from .frm needs session->lex set up. */
1325
 
  assert(session->lex->is_lex_started);
1326
 
 
1327
 
  error= 1;
1328
 
  outparam->resetTable(session, share, db_stat);
1329
 
 
1330
 
 
1331
 
  if (not (outparam->alias= strdup(alias)))
1332
 
    goto err;
1333
 
 
1334
 
  /* Allocate Cursor */
1335
 
  if (not (outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
1336
 
    goto err;
1337
 
 
1338
 
  error= 4;
1339
 
  records= 0;
1340
 
  if ((db_stat & HA_OPEN_KEYFILE))
1341
 
    records=1;
1342
 
 
1343
 
  records++;
1344
 
 
1345
 
  if (!(record= (unsigned char*) alloc_root(&outparam->mem_root,
1346
 
                                   share->rec_buff_length * records)))
1347
 
    goto err;
1348
 
 
1349
 
  if (records == 0)
1350
 
  {
1351
 
    /* We are probably in hard repair, and the buffers should not be used */
1352
 
    outparam->record[0]= outparam->record[1]= share->default_values;
1353
 
  }
1354
 
  else
1355
 
  {
1356
 
    outparam->record[0]= record;
1357
 
    if (records > 1)
1358
 
      outparam->record[1]= record+ share->rec_buff_length;
1359
 
    else
1360
 
      outparam->record[1]= outparam->record[0];   // Safety
1361
 
  }
1362
 
 
1363
 
#ifdef HAVE_purify
1364
 
  /*
1365
 
    We need this because when we read var-length rows, we are not updating
1366
 
    bytes after end of varchar
1367
 
  */
1368
 
  if (records > 1)
1369
 
  {
1370
 
    memcpy(outparam->record[0], share->default_values, share->rec_buff_length);
1371
 
    memcpy(outparam->record[1], share->default_values, share->null_bytes);
1372
 
    if (records > 2)
1373
 
      memcpy(outparam->record[1], share->default_values,
1374
 
             share->rec_buff_length);
1375
 
  }
1376
 
#endif
1377
 
 
1378
 
  if (!(field_ptr = (Field **) alloc_root(&outparam->mem_root,
1379
 
                                          (uint32_t) ((share->fields+1)*
1380
 
                                                  sizeof(Field*)))))
1381
 
    goto err;
1382
 
 
1383
 
  outparam->field= field_ptr;
1384
 
 
1385
 
  record= (unsigned char*) outparam->record[0]-1;       /* Fieldstart = 1 */
1386
 
 
1387
 
  outparam->null_flags= (unsigned char*) record+1;
1388
 
 
1389
 
  /* Setup copy of fields from share, but use the right alias and record */
1390
 
  for (i= 0 ; i < share->fields; i++, field_ptr++)
1391
 
  {
1392
 
    if (!((*field_ptr)= share->field[i]->clone(&outparam->mem_root, outparam)))
1393
 
      goto err;
1394
 
  }
1395
 
  (*field_ptr)= 0;                              // End marker
1396
 
 
1397
 
  if (share->found_next_number_field)
1398
 
    outparam->found_next_number_field=
1399
 
      outparam->field[(uint32_t) (share->found_next_number_field - share->field)];
1400
 
  if (share->timestamp_field)
1401
 
    outparam->timestamp_field= (Field_timestamp*) outparam->field[share->timestamp_field_offset];
1402
 
 
1403
 
 
1404
 
  /* Fix key->name and key_part->field */
1405
 
  if (share->key_parts)
1406
 
  {
1407
 
    KEY *key_info, *key_info_end;
1408
 
    KEY_PART_INFO *key_part;
1409
 
    uint32_t n_length;
1410
 
    n_length= share->keys*sizeof(KEY) + share->key_parts*sizeof(KEY_PART_INFO);
1411
 
    if (!(key_info= (KEY*) alloc_root(&outparam->mem_root, n_length)))
1412
 
      goto err;
1413
 
    outparam->key_info= key_info;
1414
 
    key_part= (reinterpret_cast<KEY_PART_INFO*> (key_info+share->keys));
1415
 
 
1416
 
    memcpy(key_info, share->key_info, sizeof(*key_info)*share->keys);
1417
 
    memcpy(key_part, share->key_info[0].key_part, (sizeof(*key_part) *
1418
 
                                                   share->key_parts));
1419
 
 
1420
 
    for (key_info_end= key_info + share->keys ;
1421
 
         key_info < key_info_end ;
1422
 
         key_info++)
1423
 
    {
1424
 
      KEY_PART_INFO *key_part_end;
1425
 
 
1426
 
      key_info->table= outparam;
1427
 
      key_info->key_part= key_part;
1428
 
 
1429
 
      for (key_part_end= key_part+ key_info->key_parts ;
1430
 
           key_part < key_part_end ;
1431
 
           key_part++)
1432
 
      {
1433
 
        Field *field= key_part->field= outparam->field[key_part->fieldnr-1];
1434
 
 
1435
 
        if (field->key_length() != key_part->length &&
1436
 
            !(field->flags & BLOB_FLAG))
1437
 
        {
1438
 
          /*
1439
 
            We are using only a prefix of the column as a key:
1440
 
            Create a new field for the key part that matches the index
1441
 
          */
1442
 
          field= key_part->field=field->new_field(&outparam->mem_root,
1443
 
                                                  outparam, 0);
1444
 
          field->field_length= key_part->length;
1445
 
        }
1446
 
      }
1447
 
    }
1448
 
  }
1449
 
 
1450
 
  /* Allocate bitmaps */
1451
 
 
1452
 
  bitmap_size= share->column_bitmap_size;
1453
 
  if (!(bitmaps= (unsigned char*) alloc_root(&outparam->mem_root, bitmap_size*3)))
1454
 
    goto err;
1455
 
  outparam->def_read_set.init((my_bitmap_map*) bitmaps, share->fields);
1456
 
  outparam->def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), share->fields);
1457
 
  outparam->tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), share->fields);
1458
 
  outparam->default_column_bitmaps();
1459
 
 
1460
 
  /* The table struct is now initialized;  Open the table */
1461
 
  error= 2;
1462
 
  if (db_stat)
1463
 
  {
1464
 
    int ha_err;
1465
 
    if ((ha_err= (outparam->cursor->
1466
 
                  ha_open(outparam, share->normalized_path.str,
1467
 
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1468
 
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
1469
 
                           (db_stat & HA_WAIT_IF_LOCKED) ?  HA_OPEN_WAIT_IF_LOCKED :
1470
 
                           (db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO)) ?
1471
 
                          HA_OPEN_ABORT_IF_LOCKED :
1472
 
                           HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1473
 
    {
1474
 
      switch (ha_err)
1475
 
      {
1476
 
        case HA_ERR_NO_SUCH_TABLE:
1477
 
          /*
1478
 
            The table did not exists in storage engine, use same error message
1479
 
            as if the .frm cursor didn't exist
1480
 
          */
1481
 
          error= 1;
1482
 
          errno= ENOENT;
1483
 
          break;
1484
 
        case EMFILE:
1485
 
          /*
1486
 
            Too many files opened, use same error message as if the .frm
1487
 
            cursor can't open
1488
 
           */
1489
 
          error= 1;
1490
 
          errno= EMFILE;
1491
 
          break;
1492
 
        default:
1493
 
          outparam->print_error(ha_err, MYF(0));
1494
 
          error_reported= true;
1495
 
          if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1496
 
            error= 7;
1497
 
          break;
1498
 
      }
1499
 
      goto err;
1500
 
    }
1501
 
  }
1502
 
 
1503
 
#if defined(HAVE_purify)
1504
 
  memset(bitmaps, 0, bitmap_size*3);
1505
 
#endif
1506
 
 
1507
 
  return 0;
1508
 
 
1509
 
 err:
1510
 
  if (!error_reported)
1511
 
    share->open_table_error(error, errno, 0);
1512
 
  delete outparam->cursor;
1513
 
  outparam->cursor= 0;                          // For easier error checking
1514
 
  outparam->db_stat= 0;
1515
 
  free_root(&outparam->mem_root, MYF(0));       // Safe to call on zeroed root
1516
 
  free((char*) outparam->alias);
1517
 
  return (error);
1518
 
}
1519
 
 
1520
 
bool Table::fill_item_list(List<Item> *item_list) const
1521
 
{
1522
 
  /*
1523
 
    All Item_field's created using a direct pointer to a field
1524
 
    are fixed in Item_field constructor.
1525
 
  */
1526
 
  for (Field **ptr= field; *ptr; ptr++)
1527
 
  {
1528
 
    Item_field *item= new Item_field(*ptr);
1529
 
    if (!item || item_list->push_back(item))
1530
 
      return true;
1531
 
  }
1532
 
  return false;
1533
 
}
1534
 
 
1535
 
int Table::closefrm(bool free_share)
 
77
// @note this should all be the destructor
 
78
int Table::delete_table(bool free_share)
1536
79
{
1537
80
  int error= 0;
1538
81
 
1543
86
  if (field)
1544
87
  {
1545
88
    for (Field **ptr=field ; *ptr ; ptr++)
 
89
    {
1546
90
      delete *ptr;
 
91
    }
1547
92
    field= 0;
1548
93
  }
1549
94
  delete cursor;
1550
95
  cursor= 0;                            /* For easier errorchecking */
 
96
 
1551
97
  if (free_share)
1552
98
  {
1553
 
    if (s->tmp_table == STANDARD_TABLE)
 
99
    if (s->getType() == message::Table::STANDARD)
 
100
    {
1554
101
      TableShare::release(s);
 
102
    }
1555
103
    else
1556
 
      s->free_table_share();
 
104
    {
 
105
      delete s;
 
106
    }
 
107
 
 
108
    s= NULL;
1557
109
  }
1558
 
  free_root(&mem_root, MYF(0));
 
110
  mem_root.free_root(MYF(0));
1559
111
 
1560
112
  return error;
1561
113
}
1582
134
  record[0]= (unsigned char *) NULL;
1583
135
  record[1]= (unsigned char *) NULL;
1584
136
 
1585
 
  insert_values= NULL;
 
137
  insert_values.clear();
1586
138
  key_info= NULL;
1587
139
  next_number_field= NULL;
1588
140
  found_next_number_field= NULL;
1644
196
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
1645
197
 
1646
198
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
1647
 
  memset(&sort, 0, sizeof(filesort_info_st));
1648
199
}
1649
200
 
1650
201
 
1657
208
  for (ptr= table->getBlobField(), end=ptr + table->sizeBlobFields();
1658
209
       ptr != end ;
1659
210
       ptr++)
1660
 
    ((Field_blob*) table->field[*ptr])->free();
 
211
  {
 
212
    ((Field_blob*) table->getField(*ptr))->free();
 
213
  }
1661
214
}
1662
215
 
1663
216
 
1664
 
        /* error message when opening a form cursor */
1665
 
 
1666
 
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1667
 
{
1668
 
  int err_no;
1669
 
  char buff[FN_REFLEN];
1670
 
  myf errortype= ME_ERROR+ME_WAITTANG;
1671
 
 
1672
 
  switch (pass_error) {
1673
 
  case 7:
1674
 
  case 1:
1675
 
    if (db_errno == ENOENT)
1676
 
      my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
1677
 
    else
1678
 
    {
1679
 
      sprintf(buff,"%s",normalized_path.str);
1680
 
      my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
1681
 
               errortype, buff, db_errno);
1682
 
    }
1683
 
    break;
1684
 
  case 2:
1685
 
  {
1686
 
    Cursor *cursor= 0;
1687
 
    const char *datext= "";
1688
 
 
1689
 
    if (db_type() != NULL)
1690
 
    {
1691
 
      if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
1692
 
      {
1693
 
        if (!(datext= *db_type()->bas_ext()))
1694
 
          datext= "";
1695
 
      }
1696
 
    }
1697
 
    err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
1698
 
      ER_FILE_USED : ER_CANT_OPEN_FILE;
1699
 
    sprintf(buff,"%s%s", normalized_path.str,datext);
1700
 
    my_error(err_no,errortype, buff, db_errno);
1701
 
    delete cursor;
1702
 
    break;
1703
 
  }
1704
 
  case 5:
1705
 
  {
1706
 
    const char *csname= get_charset_name((uint32_t) pass_errarg);
1707
 
    char tmp[10];
1708
 
    if (!csname || csname[0] =='?')
1709
 
    {
1710
 
      snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
1711
 
      csname= tmp;
1712
 
    }
1713
 
    my_printf_error(ER_UNKNOWN_COLLATION,
1714
 
                    _("Unknown collation '%s' in table '%-.64s' definition"),
1715
 
                    MYF(0), csname, table_name.str);
1716
 
    break;
1717
 
  }
1718
 
  case 6:
1719
 
    sprintf(buff,"%s", normalized_path.str);
1720
 
    my_printf_error(ER_NOT_FORM_FILE,
1721
 
                    _("Table '%-.64s' was created with a different version "
1722
 
                    "of Drizzle and cannot be read"),
1723
 
                    MYF(0), buff);
1724
 
    break;
1725
 
  case 8:
1726
 
    break;
1727
 
  default:                              /* Better wrong error than none */
1728
 
  case 4:
1729
 
    sprintf(buff,"%s", normalized_path.str);
1730
 
    my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
1731
 
    break;
1732
 
  }
1733
 
  return;
1734
 
} /* open_table_error */
1735
 
 
1736
 
 
1737
217
TYPELIB *typelib(memory::Root *mem_root, List<String> &strings)
1738
218
{
1739
 
  TYPELIB *result= (TYPELIB*) alloc_root(mem_root, sizeof(TYPELIB));
 
219
  TYPELIB *result= (TYPELIB*) mem_root->alloc_root(sizeof(TYPELIB));
1740
220
  if (!result)
1741
221
    return 0;
1742
222
  result->count= strings.elements;
1743
223
  result->name= "";
1744
224
  uint32_t nbytes= (sizeof(char*) + sizeof(uint32_t)) * (result->count + 1);
1745
225
  
1746
 
  if (!(result->type_names= (const char**) alloc_root(mem_root, nbytes)))
 
226
  if (!(result->type_names= (const char**) mem_root->alloc_root(nbytes)))
1747
227
    return 0;
1748
228
    
1749
229
  result->type_lengths= (uint*) (result->type_names + result->count + 1);
1810
290
        (mblen= my_ismbchar(default_charset_info, pos, end)))
1811
291
    {
1812
292
      res->append(pos, mblen);
1813
 
      pos+= mblen;
 
293
      pos+= mblen - 1;
1814
294
      if (pos >= end)
1815
295
        break;
1816
296
      continue;
1856
336
 
1857
337
void Table::setup_tmp_table_column_bitmaps(unsigned char *bitmaps)
1858
338
{
1859
 
  uint32_t field_count= s->fields;
 
339
  uint32_t field_count= s->sizeFields();
1860
340
 
1861
341
  this->def_read_set.init((my_bitmap_map*) bitmaps, field_count);
1862
342
  this->tmp_set.init((my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), field_count);
1864
344
  /* write_set and all_set are copies of read_set */
1865
345
  def_write_set= def_read_set;
1866
346
  s->all_set= def_read_set;
1867
 
  this->s->all_set.setAll();
 
347
  this->getMutableShare()->all_set.setAll();
1868
348
  default_column_bitmaps();
1869
349
}
1870
350
 
1871
351
 
1872
 
 
1873
 
void Table::updateCreateInfo(message::Table *table_proto)
1874
 
{
1875
 
  message::Table::TableOptions *table_options= table_proto->mutable_options();
1876
 
  table_options->set_block_size(s->block_size);
1877
 
  table_options->set_comment(s->getComment());
1878
 
}
1879
 
 
1880
352
int rename_file_ext(const char * from,const char * to,const char * ext)
1881
353
{
1882
354
  string from_s, to_s;
1889
361
}
1890
362
 
1891
363
/*
1892
 
  DESCRIPTION
1893
 
    given a buffer with a key value, and a map of keyparts
1894
 
    that are present in this value, returns the length of the value
1895
 
*/
1896
 
uint32_t calculate_key_len(Table *table, uint32_t key,
1897
 
                       const unsigned char *,
1898
 
                       key_part_map keypart_map)
1899
 
{
1900
 
  /* works only with key prefixes */
1901
 
  assert(((keypart_map + 1) & keypart_map) == 0);
1902
 
 
1903
 
  KEY *key_info= table->s->key_info+key;
1904
 
  KEY_PART_INFO *key_part= key_info->key_part;
1905
 
  KEY_PART_INFO *end_key_part= key_part + key_info->key_parts;
1906
 
  uint32_t length= 0;
1907
 
 
1908
 
  while (key_part < end_key_part && keypart_map)
1909
 
  {
1910
 
    length+= key_part->store_length;
1911
 
    keypart_map >>= 1;
1912
 
    key_part++;
1913
 
  }
1914
 
  return length;
1915
 
}
1916
 
 
1917
 
/*
1918
364
  Check if database name is valid
1919
365
 
1920
366
  SYNPOSIS
1922
368
    org_name            Name of database and length
1923
369
 
1924
370
  RETURN
1925
 
    0   ok
1926
 
    1   error
 
371
    false error
 
372
    true ok
1927
373
*/
1928
374
 
1929
 
bool check_db_name(LEX_STRING *org_name)
 
375
bool check_db_name(Session *session, SchemaIdentifier &schema_identifier)
1930
376
{
1931
 
  char *name= org_name->str;
1932
 
  uint32_t name_length= org_name->length;
1933
 
 
1934
 
  if (not plugin::Authorization::isAuthorized(current_session->getSecurityContext(),
1935
 
                                              string(name, name_length)))
 
377
  if (not plugin::Authorization::isAuthorized(session->getSecurityContext(), schema_identifier))
1936
378
  {
1937
 
    return 1;
 
379
    return false;
1938
380
  }
1939
381
 
1940
 
  if (!name_length || name_length > NAME_LEN || name[name_length - 1] == ' ')
1941
 
    return 1;
1942
 
 
1943
 
  my_casedn_str(files_charset_info, name);
1944
 
 
1945
 
  return check_identifier_name(org_name);
 
382
  return schema_identifier.isValid();
1946
383
}
1947
384
 
1948
385
/*
2034
471
{
2035
472
 
2036
473
  if ((cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
2037
 
      s->primary_key < MAX_KEY)
 
474
      s->hasPrimaryKey())
2038
475
  {
2039
 
    mark_columns_used_by_index_no_reset(s->primary_key);
 
476
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
2040
477
  }
2041
478
  return;
2042
479
}
2097
534
void Table::mark_columns_used_by_index_no_reset(uint32_t index,
2098
535
                                                MyBitmap *bitmap)
2099
536
{
2100
 
  KEY_PART_INFO *key_part= key_info[index].key_part;
2101
 
  KEY_PART_INFO *key_part_end= (key_part +
 
537
  KeyPartInfo *key_part= key_info[index].key_part;
 
538
  KeyPartInfo *key_part_end= (key_part +
2102
539
                                key_info[index].key_parts);
2103
540
  for (;key_part != key_part_end; key_part++)
2104
541
    bitmap->setBit(key_part->fieldnr-1);
2154
591
    be able to do an delete
2155
592
 
2156
593
  */
2157
 
  if (s->primary_key == MAX_KEY)
 
594
  if (not s->hasPrimaryKey())
2158
595
  {
2159
596
    /* fallback to use all columns in the table to identify row */
2160
597
    use_all_columns();
2161
598
    return;
2162
599
  }
2163
600
  else
2164
 
    mark_columns_used_by_index_no_reset(s->primary_key);
 
601
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
2165
602
 
2166
603
  /* If we the engine wants all predicates we mark all keys */
2167
604
  if (cursor->getEngine()->check_flag(HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE))
2202
639
    the primary key, the hidden primary key or all columns to be
2203
640
    able to do an update
2204
641
  */
2205
 
  if (s->primary_key == MAX_KEY)
 
642
  if (not s->hasPrimaryKey())
2206
643
  {
2207
644
    /* fallback to use all columns in the table to identify row */
2208
645
    use_all_columns();
2209
646
    return;
2210
647
  }
2211
648
  else
2212
 
    mark_columns_used_by_index_no_reset(s->primary_key);
 
649
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
2213
650
 
2214
651
  if (cursor->getEngine()->check_flag(HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE))
2215
652
  {
2251
688
  {
2252
689
    Field_blob* const blob= (Field_blob*) field[*ptr];
2253
690
    length+= blob->get_length((const unsigned char*)
2254
 
                              (data + blob->offset(record[0]))) +
 
691
                              (data + blob->offset(getInsertRecord()))) +
2255
692
      HA_KEY_BLOB_LENGTH;
2256
693
  }
2257
694
  return length;
2260
697
/****************************************************************************
2261
698
 Functions for creating temporary tables.
2262
699
****************************************************************************/
2263
 
 
2264
 
 
2265
 
/* Prototypes */
2266
 
void free_tmp_table(Session *session, Table *entry);
2267
 
 
2268
700
/**
2269
701
  Create field for temporary table from given field.
2270
702
 
2301
733
      (org_field->flags & BLOB_FLAG))
2302
734
    new_field= new Field_varstring(convert_blob_length,
2303
735
                                   org_field->maybe_null(),
2304
 
                                   org_field->field_name, table->s,
 
736
                                   org_field->field_name, table->getMutableShare(),
2305
737
                                   org_field->charset());
2306
738
  else
2307
739
    new_field= org_field->new_field(session->mem_root, table,
2308
 
                                    table == org_field->table);
 
740
                                    table == org_field->getTable());
2309
741
  if (new_field)
2310
742
  {
2311
743
    new_field->init(table);
2318
750
    if (org_field->maybe_null() || (item && item->maybe_null))
2319
751
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
2320
752
    if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
2321
 
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
 
753
      table->getMutableShare()->db_create_options|= HA_OPTION_PACK_RECORD;
2322
754
    else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
2323
755
      ((Field_double *) new_field)->not_fixed= true;
2324
756
  }
2356
788
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
2357
789
#define RATIO_TO_PACK_ROWS             2
2358
790
 
2359
 
static void make_internal_temporary_table_path(Session *session, char *path)
2360
 
{
2361
 
  snprintf(path, FN_REFLEN, "%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
2362
 
           session->thread_id, session->tmp_table++);
2363
 
 
2364
 
  internal::fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
2365
 
}
2366
 
 
2367
791
Table *
2368
792
create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
2369
793
                 order_st *group, bool distinct, bool save_sum_fields,
2370
794
                 uint64_t select_options, ha_rows rows_limit,
2371
795
                 const char *table_alias)
2372
796
{
2373
 
  memory::Root *mem_root_save, own_root;
 
797
  memory::Root *mem_root_save;
2374
798
  Table *table;
2375
 
  TableShare *share;
2376
799
  uint  i,field_count,null_count,null_pack_length;
2377
800
  uint32_t  copy_func_count= param->func_count;
2378
801
  uint32_t  hidden_null_count, hidden_null_pack_length, hidden_field_count;
2382
805
  bool  using_unique_constraint= false;
2383
806
  bool  use_packed_rows= true;
2384
807
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
2385
 
  char  *tmpname;
2386
 
  char  path[FN_REFLEN];
2387
808
  unsigned char *pos, *group_buff, *bitmaps;
2388
809
  unsigned char *null_flags;
2389
810
  Field **reg_field, **from_field, **default_field;
2390
 
  uint32_t *blob_field;
2391
811
  CopyField *copy= 0;
2392
 
  KEY *keyinfo;
2393
 
  KEY_PART_INFO *key_part_info;
 
812
  KeyInfo *keyinfo;
 
813
  KeyPartInfo *key_part_info;
2394
814
  Item **copy_func;
2395
815
  MI_COLUMNDEF *recinfo;
2396
816
  uint32_t total_uneven_bit_length= 0;
2397
817
  bool force_copy_fields= param->force_copy_fields;
2398
818
  uint64_t max_rows= 0;
2399
819
 
2400
 
  status_var_increment(session->status_var.created_tmp_tables);
2401
 
 
2402
 
  make_internal_temporary_table_path(session, path);
 
820
  session->status_var.created_tmp_tables++;
2403
821
 
2404
822
  if (group)
2405
823
  {
2406
824
    if (! param->quick_group)
 
825
    {
2407
826
      group= 0;                                 // Can't use group key
 
827
    }
2408
828
    else for (order_st *tmp=group ; tmp ; tmp=tmp->next)
2409
829
    {
2410
830
      /*
2434
854
    these items are stored in the temporary table.
2435
855
  */
2436
856
  if (param->precomputed_group_by)
 
857
  {
2437
858
    copy_func_count+= param->sum_func_count;
2438
 
 
2439
 
  memory::init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
2440
 
 
2441
 
  if (!multi_alloc_root(&own_root,
2442
 
                        &table, sizeof(*table),
2443
 
                        &share, sizeof(*share),
2444
 
                        &reg_field, sizeof(Field*) * (field_count+1),
2445
 
                        &default_field, sizeof(Field*) * (field_count),
2446
 
                        &blob_field, sizeof(uint32_t)*(field_count+1),
2447
 
                        &from_field, sizeof(Field*)*field_count,
2448
 
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
2449
 
                        &param->keyinfo, sizeof(*param->keyinfo),
2450
 
                        &key_part_info,
2451
 
                        sizeof(*key_part_info)*(param->group_parts+1),
2452
 
                        &param->start_recinfo,
2453
 
                        sizeof(*param->recinfo)*(field_count*2+4),
2454
 
                        &tmpname, (uint32_t) strlen(path)+1,
2455
 
                        &group_buff, (group && ! using_unique_constraint ?
2456
 
                                      param->group_length : 0),
2457
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
2458
 
                        NULL))
 
859
  }
 
860
 
 
861
  TableShareInstance *share= session->getTemporaryShare(message::Table::INTERNAL); // This will not go into the tableshare cache, so no key is used.
 
862
 
 
863
  if (not share->getMemRoot()->multi_alloc_root(0,
 
864
                                                &default_field, sizeof(Field*) * (field_count),
 
865
                                                &from_field, sizeof(Field*)*field_count,
 
866
                                                &copy_func, sizeof(*copy_func)*(copy_func_count+1),
 
867
                                                &param->keyinfo, sizeof(*param->keyinfo),
 
868
                                                &key_part_info, sizeof(*key_part_info)*(param->group_parts+1),
 
869
                                                &param->start_recinfo, sizeof(*param->recinfo)*(field_count*2+4),
 
870
                                                &group_buff, (group && ! using_unique_constraint ?
 
871
                                                              param->group_length : 0),
 
872
                                                &bitmaps, bitmap_buffer_size(field_count)*2,
 
873
                                                NULL))
2459
874
  {
2460
875
    return NULL;
2461
876
  }
2462
877
  /* CopyField belongs to Tmp_Table_Param, allocate it in Session mem_root */
2463
878
  if (!(param->copy_field= copy= new (session->mem_root) CopyField[field_count]))
2464
879
  {
2465
 
    free_root(&own_root, MYF(0));
2466
880
    return NULL;
2467
881
  }
2468
882
  param->items_to_copy= copy_func;
2469
 
  strcpy(tmpname,path);
2470
883
  /* make table according to fields */
2471
884
 
2472
 
  memset(table, 0, sizeof(*table));
2473
 
  memset(reg_field, 0, sizeof(Field*)*(field_count+1));
 
885
  table= share->getTable();
 
886
 
2474
887
  memset(default_field, 0, sizeof(Field*) * (field_count));
2475
888
  memset(from_field, 0, sizeof(Field*)*field_count);
2476
889
 
2477
 
  table->mem_root= own_root;
2478
890
  mem_root_save= session->mem_root;
2479
 
  session->mem_root= &table->mem_root;
 
891
  session->mem_root= table->getMemRoot();
2480
892
 
2481
 
  table->field=reg_field;
 
893
  share->setFields(field_count+1);
 
894
  table->setFields(share->getFields(true));
 
895
  reg_field= share->getFields(true);
2482
896
  table->alias= table_alias;
2483
897
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
2484
898
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
2485
899
  table->map=1;
2486
900
  table->copy_blobs= 1;
 
901
  assert(session);
2487
902
  table->in_use= session;
2488
903
  table->quick_keys.reset();
2489
904
  table->covering_keys.reset();
2490
905
  table->keys_in_use_for_query.reset();
2491
906
 
2492
907
  table->setShare(share);
2493
 
  share->init(tmpname, tmpname);
2494
 
  share->blob_field= blob_field;
 
908
  share->blob_field.resize(field_count+1);
 
909
  uint32_t *blob_field= &share->blob_field[0];
2495
910
  share->blob_ptr_size= portable_sizeof_char_ptr;
2496
911
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
2497
912
  share->table_charset= param->table_charset;
2498
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
2499
913
  share->keys_for_keyread.reset();
2500
914
  share->keys_in_use.reset();
2501
915
 
2564
978
          }
2565
979
          session->mem_root= mem_root_save;
2566
980
          session->change_item_tree(argp, new Item_field(new_field));
2567
 
          session->mem_root= &table->mem_root;
 
981
          session->mem_root= table->getMemRoot();
2568
982
          if (!(new_field->flags & NOT_NULL_FLAG))
2569
983
          {
2570
984
            null_count++;
2639
1053
      null_count= 0;
2640
1054
    }
2641
1055
  }
2642
 
  assert(fieldnr == (uint32_t) (reg_field - table->field));
2643
 
  assert(field_count >= (uint32_t) (reg_field - table->field));
 
1056
  assert(fieldnr == (uint32_t) (reg_field - table->getFields()));
 
1057
  assert(field_count >= (uint32_t) (reg_field - table->getFields()));
2644
1058
  field_count= fieldnr;
2645
1059
  *reg_field= 0;
2646
1060
  *blob_field= 0;                               // End marker
2648
1062
 
2649
1063
  /* If result table is small; use a heap */
2650
1064
  /* future: storage engine selection can be made dynamic? */
2651
 
  if (blob_count || using_unique_constraint ||
 
1065
  if (blob_count || using_unique_constraint || 
 
1066
      (session->lex->select_lex.options & SELECT_BIG_RESULT) ||
 
1067
      (session->lex->current_select->olap == ROLLUP_TYPE) ||
2652
1068
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
2653
1069
  {
2654
1070
    share->storage_engine= myisam_engine;
2655
 
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
 
1071
    table->cursor= share->db_type()->getCursor(*share);
2656
1072
    if (group &&
2657
1073
        (param->group_parts > table->cursor->getEngine()->max_key_parts() ||
2658
1074
         param->group_length > table->cursor->getEngine()->max_key_length()))
 
1075
    {
2659
1076
      using_unique_constraint= true;
 
1077
    }
2660
1078
  }
2661
1079
  else
2662
1080
  {
2663
1081
    share->storage_engine= heap_engine;
2664
 
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
 
1082
    table->cursor= share->db_type()->getCursor(*share);
2665
1083
  }
2666
1084
  if (! table->cursor)
2667
1085
    goto err;
2689
1107
  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)))
2690
1108
    use_packed_rows= 1;
2691
1109
 
2692
 
  share->reclength= reclength;
 
1110
  share->setRecordLength(reclength);
2693
1111
  {
2694
1112
    uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
2695
1113
    share->rec_buff_length= alloc_length;
2696
 
    if (!(table->record[0]= (unsigned char*)
2697
 
                            alloc_root(&table->mem_root, alloc_length*3)))
 
1114
    if (!(table->record[0]= (unsigned char*) table->alloc_root(alloc_length*2)))
 
1115
    {
2698
1116
      goto err;
2699
 
    table->record[1]= table->record[0]+alloc_length;
2700
 
    share->default_values= table->record[1]+alloc_length;
 
1117
    }
 
1118
    table->record[1]= table->getInsertRecord()+alloc_length;
 
1119
    share->resizeDefaultValues(alloc_length);
2701
1120
  }
2702
1121
  copy_func[0]= 0;                              // End marker
2703
1122
  param->func_count= copy_func - param->items_to_copy;
2705
1124
  table->setup_tmp_table_column_bitmaps(bitmaps);
2706
1125
 
2707
1126
  recinfo=param->start_recinfo;
2708
 
  null_flags=(unsigned char*) table->record[0];
2709
 
  pos=table->record[0]+ null_pack_length;
 
1127
  null_flags=(unsigned char*) table->getInsertRecord();
 
1128
  pos=table->getInsertRecord()+ null_pack_length;
2710
1129
  if (null_pack_length)
2711
1130
  {
2712
1131
    memset(recinfo, 0, sizeof(*recinfo));
2715
1134
    recinfo++;
2716
1135
    memset(null_flags, 255, null_pack_length);  // Set null fields
2717
1136
 
2718
 
    table->null_flags= (unsigned char*) table->record[0];
 
1137
    table->null_flags= (unsigned char*) table->getInsertRecord();
2719
1138
    share->null_fields= null_count+ hidden_null_count;
2720
1139
    share->null_bytes= null_pack_length;
2721
1140
  }
2722
1141
  null_count= (blob_count == 0) ? 1 : 0;
2723
1142
  hidden_field_count=param->hidden_field_count;
2724
 
  for (i= 0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
1143
  for (i= 0,reg_field= table->getFields(); i < field_count; i++,reg_field++,recinfo++)
2725
1144
  {
2726
1145
    Field *field= *reg_field;
2727
1146
    uint32_t length;
2768
1187
      ptrdiff_t diff;
2769
1188
      Field *orig_field= default_field[i];
2770
1189
      /* Get the value from default_values */
2771
 
      diff= (ptrdiff_t) (orig_field->table->s->default_values-
2772
 
                            orig_field->table->record[0]);
 
1190
      diff= (ptrdiff_t) (orig_field->getTable()->getDefaultValues() - orig_field->getTable()->getInsertRecord());
2773
1191
      orig_field->move_field_offset(diff);      // Points now at default_values
2774
1192
      if (orig_field->is_real_null())
2775
1193
        field->set_null();
2778
1196
        field->set_notnull();
2779
1197
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
2780
1198
      }
2781
 
      orig_field->move_field_offset(-diff);     // Back to record[0]
 
1199
      orig_field->move_field_offset(-diff);     // Back to getInsertRecord()
2782
1200
    }
2783
1201
 
2784
1202
    if (from_field[i])
2797
1215
      recinfo->type=FIELD_NORMAL;
2798
1216
    if (!--hidden_field_count)
2799
1217
      null_count=(null_count+7) & ~7;           // move to next byte
2800
 
 
2801
 
    // fix table name in field entry
2802
 
    field->table_name= &table->alias;
2803
1218
  }
2804
1219
 
2805
1220
  param->copy_field_end=copy;
2807
1222
  table->storeRecordAsDefault();        // Make empty default record
2808
1223
 
2809
1224
  if (session->variables.tmp_table_size == ~ (uint64_t) 0)              // No limit
 
1225
  {
2810
1226
    max_rows= ~(uint64_t) 0;
 
1227
  }
2811
1228
  else
 
1229
  {
2812
1230
    max_rows= (uint64_t) (((share->db_type() == heap_engine) ?
2813
 
                          min(session->variables.tmp_table_size,
2814
 
                              session->variables.max_heap_table_size) :
2815
 
                          session->variables.tmp_table_size) /
2816
 
                         share->reclength);
 
1231
                           min(session->variables.tmp_table_size,
 
1232
                               session->variables.max_heap_table_size) :
 
1233
                           session->variables.tmp_table_size) /
 
1234
                          share->getRecordLength());
 
1235
  }
2817
1236
 
2818
1237
  set_if_bigger(max_rows, (uint64_t)1); // For dummy start options
2819
1238
  /*
2849
1268
      bool maybe_null=(*cur_group->item)->maybe_null;
2850
1269
      key_part_info->null_bit= 0;
2851
1270
      key_part_info->field=  field;
2852
 
      key_part_info->offset= field->offset(table->record[0]);
 
1271
      key_part_info->offset= field->offset(table->getInsertRecord());
2853
1272
      key_part_info->length= (uint16_t) field->key_length();
2854
1273
      key_part_info->type=   (uint8_t) field->key_type();
2855
1274
      key_part_info->key_type= 
2877
1296
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
2878
1297
          key_part_info->null_bit=field->null_bit;
2879
1298
          key_part_info->null_offset= (uint32_t) (field->null_ptr -
2880
 
                                              (unsigned char*) table->record[0]);
 
1299
                                              (unsigned char*) table->getInsertRecord());
2881
1300
          cur_group->buff++;                        // Pointer to field data
2882
1301
          group_buff++;                         // Skipp null flag
2883
1302
        }
2911
1330
                         (share->uniques ? test(null_pack_length) : 0));
2912
1331
    table->distinct= 1;
2913
1332
    share->keys= 1;
2914
 
    if (!(key_part_info= (KEY_PART_INFO*)
2915
 
          alloc_root(&table->mem_root,
2916
 
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
 
1333
    if (!(key_part_info= (KeyPartInfo*)
 
1334
         table->alloc_root(keyinfo->key_parts * sizeof(KeyPartInfo))))
2917
1335
      goto err;
2918
 
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
 
1336
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KeyPartInfo));
2919
1337
    table->key_info=keyinfo;
2920
1338
    keyinfo->key_part=key_part_info;
2921
1339
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
2934
1352
      key_part_info->null_bit= 0;
2935
1353
      key_part_info->offset=hidden_null_pack_length;
2936
1354
      key_part_info->length=null_pack_length;
2937
 
      key_part_info->field= new Field_varstring(table->record[0],
 
1355
      key_part_info->field= new Field_varstring(table->getInsertRecord(),
2938
1356
                                                (uint32_t) key_part_info->length,
2939
1357
                                                0,
2940
1358
                                                (unsigned char*) 0,
2941
1359
                                                (uint32_t) 0,
2942
1360
                                                NULL,
2943
 
                                                table->s,
 
1361
                                                table->getMutableShare(),
2944
1362
                                                &my_charset_bin);
2945
1363
      if (!key_part_info->field)
2946
1364
        goto err;
2950
1368
      key_part_info++;
2951
1369
    }
2952
1370
    /* Create a distinct key over the columns we are going to return */
2953
 
    for (i=param->hidden_field_count, reg_field=table->field + i ;
 
1371
    for (i=param->hidden_field_count, reg_field=table->getFields() + i ;
2954
1372
         i < field_count;
2955
1373
         i++, reg_field++, key_part_info++)
2956
1374
    {
2957
1375
      key_part_info->null_bit= 0;
2958
1376
      key_part_info->field=    *reg_field;
2959
 
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
 
1377
      key_part_info->offset=   (*reg_field)->offset(table->getInsertRecord());
2960
1378
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
2961
 
      /* TODO:
2962
 
        The below method of computing the key format length of the
 
1379
      /* @todo The below method of computing the key format length of the
2963
1380
        key part is a copy/paste from optimizer/range.cc, and table.cc.
2964
1381
        This should be factored out, e.g. as a method of Field.
2965
1382
        In addition it is not clear if any of the Field::*_length
2992
1409
                                       &param->recinfo, select_options))
2993
1410
      goto err;
2994
1411
  }
 
1412
  assert(table->in_use);
2995
1413
  if (table->open_tmp_table())
2996
1414
    goto err;
2997
1415
 
3001
1419
 
3002
1420
err:
3003
1421
  session->mem_root= mem_root_save;
3004
 
  table->free_tmp_table(session);
 
1422
  table= NULL;
 
1423
 
3005
1424
  return NULL;
3006
1425
}
3007
1426
 
3014
1433
    The created table doesn't have a table Cursor associated with
3015
1434
    it, has no keys, no group/distinct, no copy_funcs array.
3016
1435
    The sole purpose of this Table object is to use the power of Field
3017
 
    class to read/write data to/from table->record[0]. Then one can store
 
1436
    class to read/write data to/from table->getInsertRecord(). Then one can store
3018
1437
    the record in any container (RB tree, hash, etc).
3019
1438
    The table is created in Session mem_root, so are the table's fields.
3020
1439
    Consequently, if you don't BLOB fields, you don't need to free it.
3026
1445
    0 if out of memory, Table object in case of success
3027
1446
*/
3028
1447
 
3029
 
Table *create_virtual_tmp_table(Session *session, List<CreateField> &field_list)
 
1448
Table *Session::create_virtual_tmp_table(List<CreateField> &field_list)
3030
1449
{
3031
1450
  uint32_t field_count= field_list.elements;
3032
1451
  uint32_t blob_count= 0;
3035
1454
  uint32_t record_length= 0;
3036
1455
  uint32_t null_count= 0;                 /* number of columns which may be null */
3037
1456
  uint32_t null_pack_length;              /* NULL representation array length */
3038
 
  uint32_t *blob_field;
3039
1457
  unsigned char *bitmaps;
3040
1458
  Table *table;
3041
 
  TableShare *share;
3042
 
 
3043
 
  if (!multi_alloc_root(session->mem_root,
3044
 
                        &table, sizeof(*table),
3045
 
                        &share, sizeof(*share),
3046
 
                        &field, (field_count + 1) * sizeof(Field*),
3047
 
                        &blob_field, (field_count+1) *sizeof(uint32_t),
3048
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
3049
 
                        NULL))
 
1459
 
 
1460
  TableShareInstance *share= getTemporaryShare(message::Table::INTERNAL); // This will not go into the tableshare cache, so no key is used.
 
1461
 
 
1462
  if (! share->getMemRoot()->multi_alloc_root(0,
 
1463
                                              &bitmaps, bitmap_buffer_size(field_count)*2,
 
1464
                                              NULL))
 
1465
  {
3050
1466
    return NULL;
 
1467
  }
3051
1468
 
3052
 
  memset(table, 0, sizeof(*table));
3053
 
  memset(share, 0, sizeof(*share));
3054
 
  table->field= field;
3055
 
  table->s= share;
3056
 
  share->blob_field= blob_field;
 
1469
  table= share->getTable();
 
1470
  share->setFields(field_count + 1);
 
1471
  table->setFields(share->getFields(true));
 
1472
  field= share->getFields(true);
 
1473
  share->blob_field.resize(field_count+1);
3057
1474
  share->fields= field_count;
3058
1475
  share->blob_ptr_size= portable_sizeof_char_ptr;
3059
1476
  table->setup_tmp_table_column_bitmaps(bitmaps);
3060
1477
 
 
1478
  table->in_use= this;           /* field->reset() may access table->in_use */
 
1479
 
3061
1480
  /* Create all fields and calculate the total length of record */
3062
1481
  List_iterator_fast<CreateField> it(field_list);
3063
1482
  while ((cdef= it++))
3064
1483
  {
3065
 
    *field= make_field(share,
3066
 
                       NULL,
3067
 
                       0,
3068
 
                       cdef->length,
3069
 
                       (cdef->flags & NOT_NULL_FLAG) ? false : true,
3070
 
                       (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
3071
 
                       (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
3072
 
                       cdef->decimals,
3073
 
                       cdef->sql_type,
3074
 
                       cdef->charset,
3075
 
                       cdef->unireg_check,
3076
 
                       cdef->interval,
3077
 
                       cdef->field_name);
 
1484
    *field= share->make_field(NULL,
 
1485
                              cdef->length,
 
1486
                              (cdef->flags & NOT_NULL_FLAG) ? false : true,
 
1487
                              (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
 
1488
                              (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
 
1489
                              cdef->decimals,
 
1490
                              cdef->sql_type,
 
1491
                              cdef->charset,
 
1492
                              cdef->unireg_check,
 
1493
                              cdef->interval,
 
1494
                              cdef->field_name);
3078
1495
    if (!*field)
3079
1496
      goto error;
3080
1497
    (*field)->init(table);
3083
1500
      null_count++;
3084
1501
 
3085
1502
    if ((*field)->flags & BLOB_FLAG)
3086
 
      share->blob_field[blob_count++]= (uint32_t) (field - table->field);
 
1503
      share->blob_field[blob_count++]= (uint32_t) (field - table->getFields());
3087
1504
 
3088
1505
    field++;
3089
1506
  }
3092
1509
  share->blob_fields= blob_count;
3093
1510
 
3094
1511
  null_pack_length= (null_count + 7)/8;
3095
 
  share->reclength= record_length + null_pack_length;
3096
 
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
3097
 
  table->record[0]= (unsigned char*) session->alloc(share->rec_buff_length);
3098
 
  if (!table->record[0])
 
1512
  share->setRecordLength(record_length + null_pack_length);
 
1513
  share->rec_buff_length= ALIGN_SIZE(share->getRecordLength() + 1);
 
1514
  table->record[0]= (unsigned char*)alloc(share->rec_buff_length);
 
1515
  if (not table->getInsertRecord())
3099
1516
    goto error;
3100
1517
 
3101
1518
  if (null_pack_length)
3102
1519
  {
3103
 
    table->null_flags= (unsigned char*) table->record[0];
 
1520
    table->null_flags= (unsigned char*) table->getInsertRecord();
3104
1521
    share->null_fields= null_count;
3105
1522
    share->null_bytes= null_pack_length;
3106
1523
  }
3107
 
 
3108
 
  table->in_use= session;           /* field->reset() may access table->in_use */
3109
1524
  {
3110
1525
    /* Set up field pointers */
3111
 
    unsigned char *null_pos= table->record[0];
 
1526
    unsigned char *null_pos= table->getInsertRecord();
3112
1527
    unsigned char *field_pos= null_pos + share->null_bytes;
3113
1528
    uint32_t null_bit= 1;
3114
1529
 
3115
 
    for (field= table->field; *field; ++field)
 
1530
    for (field= table->getFields(); *field; ++field)
3116
1531
    {
3117
1532
      Field *cur_field= *field;
3118
1533
      if ((cur_field->flags & NOT_NULL_FLAG))
3132
1547
      field_pos+= cur_field->pack_length();
3133
1548
    }
3134
1549
  }
 
1550
 
3135
1551
  return table;
 
1552
 
3136
1553
error:
3137
 
  for (field= table->field; *field; ++field)
 
1554
  for (field= table->getFields(); *field; ++field)
 
1555
  {
3138
1556
    delete *field;                         /* just invokes field destructor */
 
1557
  }
3139
1558
  return 0;
3140
1559
}
3141
1560
 
3142
1561
bool Table::open_tmp_table()
3143
1562
{
3144
1563
  int error;
3145
 
  if ((error=cursor->ha_open(this, s->table_name.str,O_RDWR,
3146
 
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
1564
  
 
1565
  TableIdentifier identifier(s->getSchemaName(), s->getTableName(), s->getPath());
 
1566
  if ((error=cursor->ha_open(identifier,
 
1567
                             this,
 
1568
                             O_RDWR,
 
1569
                             HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
3147
1570
  {
3148
1571
    print_error(error, MYF(0));
3149
1572
    db_stat= 0;
3183
1606
     true  - Error
3184
1607
*/
3185
1608
 
3186
 
bool Table::create_myisam_tmp_table(KEY *keyinfo,
 
1609
bool Table::create_myisam_tmp_table(KeyInfo *keyinfo,
3187
1610
                                    MI_COLUMNDEF *start_recinfo,
3188
1611
                                    MI_COLUMNDEF **recinfo,
3189
1612
                                    uint64_t options)
3193
1616
  MI_UNIQUEDEF uniquedef;
3194
1617
  TableShare *share= s;
3195
1618
 
3196
 
  if (share->keys)
 
1619
  if (share->sizeKeys())
3197
1620
  {                                             // Get keys for ni_create
3198
1621
    bool using_unique_constraint= false;
3199
 
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&this->mem_root,
3200
 
                                            sizeof(*seg) * keyinfo->key_parts);
3201
 
    if (!seg)
3202
 
      goto err;
 
1622
    HA_KEYSEG *seg= (HA_KEYSEG*) this->mem_root.alloc_root(sizeof(*seg) * keyinfo->key_parts);
 
1623
    if (not seg)
 
1624
      return true;
3203
1625
 
3204
1626
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
3205
1627
    if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
3220
1642
      (*recinfo)->type= FIELD_CHECK;
3221
1643
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
3222
1644
      (*recinfo)++;
3223
 
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
 
1645
      share->setRecordLength(share->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
3224
1646
    }
3225
1647
    else
3226
1648
    {
3253
1675
      if (!(key_field->flags & NOT_NULL_FLAG))
3254
1676
      {
3255
1677
        seg->null_bit= key_field->null_bit;
3256
 
        seg->null_pos= (uint32_t) (key_field->null_ptr - (unsigned char*) record[0]);
 
1678
        seg->null_pos= (uint32_t) (key_field->null_ptr - (unsigned char*) getInsertRecord());
3257
1679
        /*
3258
1680
          We are using a GROUP BY on something that contains NULL
3259
1681
          In this case we have to tell MyISAM that two NULL should
3265
1687
    }
3266
1688
  }
3267
1689
  MI_CREATE_INFO create_info;
3268
 
  memset(&create_info, 0, sizeof(create_info));
3269
1690
 
3270
1691
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
3271
1692
      OPTION_BIG_TABLES)
3272
1693
    create_info.data_file_length= ~(uint64_t) 0;
3273
1694
 
3274
 
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
3275
 
                       (uint32_t) (*recinfo-start_recinfo),
3276
 
                       start_recinfo,
3277
 
                       share->uniques, &uniquedef,
3278
 
                       &create_info,
3279
 
                       HA_CREATE_TMP_TABLE)))
 
1695
  if ((error= mi_create(share->getTableName(), share->sizeKeys(), &keydef,
 
1696
                        (uint32_t) (*recinfo-start_recinfo),
 
1697
                        start_recinfo,
 
1698
                        share->uniques, &uniquedef,
 
1699
                        &create_info,
 
1700
                        HA_CREATE_TMP_TABLE)))
3280
1701
  {
3281
1702
    print_error(error, MYF(0));
3282
1703
    db_stat= 0;
3283
 
    goto err;
 
1704
 
 
1705
    return true;
3284
1706
  }
3285
 
  status_var_increment(in_use->status_var.created_tmp_disk_tables);
 
1707
  in_use->status_var.created_tmp_disk_tables++;
3286
1708
  share->db_record_offset= 1;
3287
1709
  return false;
3288
 
 err:
3289
 
  return true;
3290
1710
}
3291
1711
 
3292
1712
 
3295
1715
  memory::Root own_root= mem_root;
3296
1716
  const char *save_proc_info;
3297
1717
 
3298
 
  save_proc_info=session->get_proc_info();
 
1718
  save_proc_info= session->get_proc_info();
3299
1719
  session->set_proc_info("removing tmp table");
3300
1720
 
3301
1721
  // Release latches since this can take a long time
3304
1724
  if (cursor)
3305
1725
  {
3306
1726
    if (db_stat)
3307
 
      cursor->closeMarkForDelete(s->table_name.str);
 
1727
    {
 
1728
      cursor->closeMarkForDelete(s->getTableName());
 
1729
    }
3308
1730
 
3309
 
    s->db_type()->doDropTable(*session, s->table_name.str);
 
1731
    TableIdentifier identifier(s->getSchemaName(), s->getTableName(), s->getTableName());
 
1732
    s->db_type()->doDropTable(*session, identifier);
3310
1733
 
3311
1734
    delete cursor;
3312
1735
  }
3313
1736
 
3314
1737
  /* free blobs */
3315
1738
  for (Field **ptr= field ; *ptr ; ptr++)
 
1739
  {
3316
1740
    (*ptr)->free();
 
1741
  }
3317
1742
  free_io_cache();
3318
1743
 
3319
 
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
3320
 
  session->set_proc_info(save_proc_info);
3321
 
}
3322
 
 
3323
 
/**
3324
 
  If a HEAP table gets full, create a MyISAM table and copy all rows
3325
 
  to this.
3326
 
*/
3327
 
 
3328
 
bool create_myisam_from_heap(Session *session, Table *table,
3329
 
                             MI_COLUMNDEF *start_recinfo,
3330
 
                             MI_COLUMNDEF **recinfo,
3331
 
                             int error, bool ignore_last_dupp_key_error)
3332
 
{
3333
 
  Table new_table;
3334
 
  TableShare share;
3335
 
  const char *save_proc_info;
3336
 
  int write_err;
3337
 
 
3338
 
  if (table->s->db_type() != heap_engine ||
3339
 
      error != HA_ERR_RECORD_FILE_FULL)
3340
 
  {
3341
 
    table->print_error(error, MYF(0));
3342
 
    return true;
3343
 
  }
3344
 
 
3345
 
  // Release latches since this can take a long time
3346
 
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
3347
 
 
3348
 
  new_table= *table;
3349
 
  share= *table->s;
3350
 
  new_table.s= &share;
3351
 
  new_table.s->storage_engine= myisam_engine;
3352
 
  if (not (new_table.cursor= new_table.s->db_type()->getCursor(share, &new_table.mem_root)))
3353
 
    return true;                                // End of memory
3354
 
 
3355
 
  save_proc_info=session->get_proc_info();
3356
 
  session->set_proc_info("converting HEAP to MyISAM");
3357
 
 
3358
 
  if (new_table.create_myisam_tmp_table(table->key_info, start_recinfo,
3359
 
                                        recinfo, session->lex->select_lex.options |
3360
 
                                        session->options))
3361
 
    goto err2;
3362
 
  if (new_table.open_tmp_table())
3363
 
    goto err1;
3364
 
  if (table->cursor->indexes_are_disabled())
3365
 
    new_table.cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL);
3366
 
  table->cursor->ha_index_or_rnd_end();
3367
 
  table->cursor->ha_rnd_init(1);
3368
 
  if (table->no_rows)
3369
 
  {
3370
 
    new_table.cursor->extra(HA_EXTRA_NO_ROWS);
3371
 
    new_table.no_rows=1;
3372
 
  }
3373
 
 
3374
 
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
3375
 
  new_table.cursor->extra(HA_EXTRA_WRITE_CACHE);
3376
 
 
3377
 
  /*
3378
 
    copy all old rows from heap table to MyISAM table
3379
 
    This is the only code that uses record[1] to read/write but this
3380
 
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
3381
 
  */
3382
 
  while (!table->cursor->rnd_next(new_table.record[1]))
3383
 
  {
3384
 
    write_err= new_table.cursor->ha_write_row(new_table.record[1]);
3385
 
    if (write_err)
3386
 
      goto err;
3387
 
  }
3388
 
  /* copy row that filled HEAP table */
3389
 
  if ((write_err=new_table.cursor->ha_write_row(table->record[0])))
3390
 
  {
3391
 
    if (new_table.cursor->is_fatal_error(write_err, HA_CHECK_DUP) ||
3392
 
        !ignore_last_dupp_key_error)
3393
 
      goto err;
3394
 
  }
3395
 
 
3396
 
  /* remove heap table and change to use myisam table */
3397
 
  (void) table->cursor->ha_rnd_end();
3398
 
  (void) table->cursor->close();                  // This deletes the table !
3399
 
  delete table->cursor;
3400
 
  table->cursor= NULL;
3401
 
  new_table.s= table->s;                       // Keep old share
3402
 
  *table= new_table;
3403
 
  *table->s= share;
3404
 
 
3405
 
  table->cursor->change_table_ptr(table, table->s);
3406
 
  table->use_all_columns();
3407
 
  if (save_proc_info)
3408
 
  {
3409
 
    const char *new_proc_info=
3410
 
      (!strcmp(save_proc_info,"Copying to tmp table") ?
3411
 
      "Copying to tmp table on disk" : save_proc_info);
3412
 
    session->set_proc_info(new_proc_info);
3413
 
  }
3414
 
  return false;
3415
 
 
3416
 
 err:
3417
 
  table->print_error(write_err, MYF(0));
3418
 
  (void) table->cursor->ha_rnd_end();
3419
 
  (void) new_table.cursor->close();
3420
 
 err1:
3421
 
  new_table.s->db_type()->doDropTable(*session, new_table.s->table_name.str);
3422
 
 err2:
3423
 
  delete new_table.cursor;
3424
 
  session->set_proc_info(save_proc_info);
3425
 
  table->mem_root= new_table.mem_root;
3426
 
  return true;
 
1744
  own_root.free_root(MYF(0)); /* the table is allocated in its own root */
 
1745
  session->set_proc_info(save_proc_info);
3427
1746
}
3428
1747
 
3429
1748
my_bitmap_map *Table::use_all_columns(MyBitmap *bitmap)
3444
1763
  uint32_t best= MAX_KEY;
3445
1764
  if (usable_keys->any())
3446
1765
  {
3447
 
    for (uint32_t nr= 0; nr < s->keys ; nr++)
 
1766
    for (uint32_t nr= 0; nr < s->sizeKeys() ; nr++)
3448
1767
    {
3449
1768
      if (usable_keys->test(nr))
3450
1769
      {
3482
1801
bool Table::compare_record()
3483
1802
{
3484
1803
  if (s->blob_fields + s->varchar_fields == 0)
3485
 
    return memcmp(this->record[0], this->record[1], (size_t) s->reclength);
 
1804
    return memcmp(this->getInsertRecord(), this->getUpdateRecord(), (size_t) s->getRecordLength());
3486
1805
  
3487
1806
  /* Compare null bits */
3488
1807
  if (memcmp(null_flags, null_flags + s->rec_buff_length, s->null_bytes))
3504
1823
 */
3505
1824
void Table::storeRecord()
3506
1825
{
3507
 
  memcpy(record[1], record[0], (size_t) s->reclength);
 
1826
  memcpy(getUpdateRecord(), getInsertRecord(), (size_t) s->getRecordLength());
3508
1827
}
3509
1828
 
3510
1829
/*
3513
1832
 */
3514
1833
void Table::storeRecordAsInsert()
3515
1834
{
3516
 
  memcpy(insert_values, record[0], (size_t) s->reclength);
 
1835
  assert(insert_values.size() >= s->getRecordLength());
 
1836
  memcpy(&insert_values[0], getInsertRecord(), (size_t) s->getRecordLength());
3517
1837
}
3518
1838
 
3519
1839
/*
3522
1842
 */
3523
1843
void Table::storeRecordAsDefault()
3524
1844
{
3525
 
  memcpy(s->default_values, record[0], (size_t) s->reclength);
 
1845
  memcpy(s->getDefaultValues(), getInsertRecord(), (size_t) s->getRecordLength());
3526
1846
}
3527
1847
 
3528
1848
/*
3531
1851
 */
3532
1852
void Table::restoreRecord()
3533
1853
{
3534
 
  memcpy(record[0], record[1], (size_t) s->reclength);
 
1854
  memcpy(getInsertRecord(), getUpdateRecord(), (size_t) s->getRecordLength());
3535
1855
}
3536
1856
 
3537
1857
/*
3540
1860
 */
3541
1861
void Table::restoreRecordAsDefault()
3542
1862
{
3543
 
  memcpy(record[0], s->default_values, (size_t) s->reclength);
 
1863
  memcpy(getInsertRecord(), s->getDefaultValues(), (size_t) s->getRecordLength());
3544
1864
}
3545
1865
 
3546
1866
/*
3553
1873
  memset(null_flags, 255, s->null_bytes);
3554
1874
}
3555
1875
 
3556
 
Table::Table()
3557
 
  : s(NULL),
3558
 
    field(NULL),
3559
 
    cursor(NULL),
3560
 
    next(NULL),
3561
 
    prev(NULL),
3562
 
    read_set(NULL),
3563
 
    write_set(NULL),
3564
 
    tablenr(0),
3565
 
    db_stat(0),
3566
 
    in_use(NULL),
3567
 
    insert_values(NULL),
3568
 
    key_info(NULL),
3569
 
    next_number_field(NULL),
3570
 
    found_next_number_field(NULL),
3571
 
    timestamp_field(NULL),
3572
 
    pos_in_table_list(NULL),
3573
 
    group(NULL),
3574
 
    alias(NULL),
3575
 
    null_flags(NULL),
3576
 
    lock_position(0),
3577
 
    lock_data_start(0),
3578
 
    lock_count(0),
3579
 
    used_fields(0),
3580
 
    status(0),
3581
 
    derived_select_number(0),
3582
 
    current_lock(F_UNLCK),
3583
 
    copy_blobs(false),
3584
 
    maybe_null(false),
3585
 
    null_row(false),
3586
 
    force_index(false),
3587
 
    distinct(false),
3588
 
    const_table(false),
3589
 
    no_rows(false),
3590
 
    key_read(false),
3591
 
    no_keyread(false),
3592
 
    open_placeholder(false),
3593
 
    locked_by_name(false),
3594
 
    no_cache(false),
3595
 
    auto_increment_field_not_null(false),
3596
 
    alias_name_used(false),
3597
 
    query_id(0),
3598
 
    quick_condition_rows(0),
3599
 
    timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
3600
 
    map(0)
 
1876
Table::Table() : 
 
1877
  s(NULL),
 
1878
  field(NULL),
 
1879
  cursor(NULL),
 
1880
  next(NULL),
 
1881
  prev(NULL),
 
1882
  read_set(NULL),
 
1883
  write_set(NULL),
 
1884
  tablenr(0),
 
1885
  db_stat(0),
 
1886
  in_use(NULL),
 
1887
  key_info(NULL),
 
1888
  next_number_field(NULL),
 
1889
  found_next_number_field(NULL),
 
1890
  timestamp_field(NULL),
 
1891
  pos_in_table_list(NULL),
 
1892
  group(NULL),
 
1893
  alias(NULL),
 
1894
  null_flags(NULL),
 
1895
  lock_position(0),
 
1896
  lock_data_start(0),
 
1897
  lock_count(0),
 
1898
  used_fields(0),
 
1899
  status(0),
 
1900
  derived_select_number(0),
 
1901
  current_lock(F_UNLCK),
 
1902
  copy_blobs(false),
 
1903
  maybe_null(false),
 
1904
  null_row(false),
 
1905
  force_index(false),
 
1906
  distinct(false),
 
1907
  const_table(false),
 
1908
  no_rows(false),
 
1909
  key_read(false),
 
1910
  no_keyread(false),
 
1911
  open_placeholder(false),
 
1912
  locked_by_name(false),
 
1913
  no_cache(false),
 
1914
  auto_increment_field_not_null(false),
 
1915
  alias_name_used(false),
 
1916
  query_id(0),
 
1917
  quick_condition_rows(0),
 
1918
  timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
 
1919
  map(0),
 
1920
  is_placeholder_created(0)
3601
1921
{
 
1922
  memset(&def_read_set, 0, sizeof(MyBitmap)); /**< Default read set of columns */
 
1923
  memset(&def_write_set, 0, sizeof(MyBitmap)); /**< Default write set of columns */
 
1924
  memset(&tmp_set, 0, sizeof(MyBitmap)); /* Not sure about this... */
 
1925
 
3602
1926
  record[0]= (unsigned char *) 0;
3603
1927
  record[1]= (unsigned char *) 0;
3604
1928
 
 
1929
  reginfo.reset();
3605
1930
  covering_keys.reset();
3606
 
 
3607
1931
  quick_keys.reset();
3608
1932
  merge_keys.reset();
3609
1933
 
3616
1940
 
3617
1941
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
3618
1942
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
3619
 
 
3620
 
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
3621
 
  memset(&sort, 0, sizeof(filesort_info_st));
3622
1943
}
3623
1944
 
3624
1945
/*****************************************************************************
3641
1962
  */
3642
1963
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
3643
1964
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got error %d when reading table '%s'"),
3644
 
                    error, s->path.str);
 
1965
                  error, s->getPath());
3645
1966
  print_error(error, MYF(0));
3646
1967
 
3647
1968
  return 1;
3655
1976
  null_row= 0;
3656
1977
  status= STATUS_NO_RECORD;
3657
1978
  maybe_null= table_list->outer_join;
3658
 
  TableList *embedding= table_list->embedding;
 
1979
  TableList *embedding= table_list->getEmbedding();
3659
1980
  while (!maybe_null && embedding)
3660
1981
  {
3661
1982
    maybe_null= embedding->outer_join;
3662
 
    embedding= embedding->embedding;
 
1983
    embedding= embedding->getEmbedding();
3663
1984
  }
3664
1985
  tablenr= table_number;
3665
1986
  map= (table_map) 1 << table_number;
3669
1990
}
3670
1991
 
3671
1992
 
3672
 
/*
3673
 
  Used by ALTER Table when the table is a temporary one. It changes something
3674
 
  only if the ALTER contained a RENAME clause (otherwise, table_name is the old
3675
 
  name).
3676
 
  Prepares a table cache key, which is the concatenation of db, table_name and
3677
 
  session->slave_proxy_id, separated by '\0'.
3678
 
*/
3679
 
 
3680
 
bool Table::rename_temporary_table(const char *db, const char *table_name)
 
1993
bool Table::fill_item_list(List<Item> *item_list) const
3681
1994
{
3682
 
  char *key;
3683
 
  uint32_t key_length;
3684
 
  TableShare *share= s;
3685
 
 
3686
 
  if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
3687
 
    return true;
3688
 
 
3689
 
  key_length= TableShare::createKey(key, db, table_name);
3690
 
  share->set_table_cache_key(key, key_length);
3691
 
 
 
1995
  /*
 
1996
    All Item_field's created using a direct pointer to a field
 
1997
    are fixed in Item_field constructor.
 
1998
  */
 
1999
  for (Field **ptr= field; *ptr; ptr++)
 
2000
  {
 
2001
    Item_field *item= new Item_field(*ptr);
 
2002
    if (!item || item_list->push_back(item))
 
2003
      return true;
 
2004
  }
3692
2005
  return false;
3693
2006
}
3694
2007