73
75
/*************************************************************************/
75
/* Get column name from column hash */
77
static unsigned char *get_field_name(Field **buff, size_t *length, bool)
79
*length= (uint32_t) strlen((*buff)->field_name);
80
return (unsigned char*) (*buff)->field_name;
84
Allocate a setup TableShare structure
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
93
0 Error (out of memory)
97
TableShare *alloc_table_share(TableList *table_list, char *key,
100
memory::Root mem_root;
102
char *key_buff, *path_buff;
103
char path[FN_REFLEN];
104
uint32_t path_length;
106
path_length= build_table_filename(path, sizeof(path) - 1,
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,
116
memset(share, 0, sizeof(*share));
118
share->set_table_cache_key(key_buff, key, key_length);
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;
126
share->version= refresh_version;
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);
136
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
138
enum_field_types field_type;
140
switch(proto_field_type)
142
case message::Table::Field::INTEGER:
143
field_type= DRIZZLE_TYPE_LONG;
145
case message::Table::Field::DOUBLE:
146
field_type= DRIZZLE_TYPE_DOUBLE;
148
case message::Table::Field::TIMESTAMP:
149
field_type= DRIZZLE_TYPE_TIMESTAMP;
151
case message::Table::Field::BIGINT:
152
field_type= DRIZZLE_TYPE_LONGLONG;
154
case message::Table::Field::DATETIME:
155
field_type= DRIZZLE_TYPE_DATETIME;
157
case message::Table::Field::DATE:
158
field_type= DRIZZLE_TYPE_DATE;
160
case message::Table::Field::VARCHAR:
161
field_type= DRIZZLE_TYPE_VARCHAR;
163
case message::Table::Field::DECIMAL:
164
field_type= DRIZZLE_TYPE_DECIMAL;
166
case message::Table::Field::ENUM:
167
field_type= DRIZZLE_TYPE_ENUM;
169
case message::Table::Field::BLOB:
170
field_type= DRIZZLE_TYPE_BLOB;
173
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
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)
185
Item *default_item= NULL;
190
return new Item_null();
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(),
201
default_value->length());
203
case DRIZZLE_TYPE_DOUBLE:
204
default_item= new Item_float(default_value->c_str(),
205
default_value->length());
207
case DRIZZLE_TYPE_NULL:
209
case DRIZZLE_TYPE_TIMESTAMP:
210
case DRIZZLE_TYPE_DATETIME:
211
case DRIZZLE_TYPE_DATE:
212
if (default_value->compare("NOW()") == 0)
214
case DRIZZLE_TYPE_ENUM:
215
default_item= new Item_string(default_value->c_str(),
216
default_value->length(),
217
system_charset_info);
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)
223
default_item= new Item_string(default_bin_value->c_str(),
224
default_bin_value->length(),
229
default_item= new Item_string(default_value->c_str(),
230
default_value->length(),
231
system_charset_info);
234
case DRIZZLE_TYPE_DECIMAL:
235
default_item= new Item_decimal(default_value->c_str(),
236
default_value->length(),
237
system_charset_info);
244
int parse_table_proto(Session& session,
245
message::Table &table,
250
if (! table.IsInitialized())
252
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
253
return ER_CORRUPT_TABLE_DEFINITION;
256
share->setTableProto(new(nothrow) message::Table(table));
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.
261
message::Table::TableOptions table_options;
263
if (table.has_options())
264
table_options= table.options();
266
uint32_t db_create_options= 0;
268
if (table_options.has_pack_keys())
270
if (table_options.pack_keys())
271
db_create_options|= HA_OPTION_PACK_KEYS;
273
db_create_options|= HA_OPTION_NO_PACK_KEYS;
276
if (table_options.pack_record())
277
db_create_options|= HA_OPTION_PACK_RECORD;
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.
282
share->db_create_options= (db_create_options & 0x0000FFFF);
283
share->db_options_in_use= share->db_create_options;
285
share->row_type= table_options.has_row_type() ?
286
(enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
288
share->block_size= table_options.has_block_size() ?
289
table_options.block_size() : 0;
291
share->table_charset= get_charset(table_options.has_collation_id()?
292
table_options.collation_id() : 0);
294
if (!share->table_charset)
296
/* unknown charset in head[38] or pre-3.23 frm */
297
if (use_mb(default_charset_info))
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"),
306
share->table_charset= default_charset_info;
309
share->db_record_offset= 1;
311
share->blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
313
share->keys= table.indexes_size();
316
for (int indx= 0; indx < table.indexes_size(); indx++)
317
share->key_parts+= table.indexes(indx).index_part_size();
319
share->key_info= (KEY*) alloc_root(&share->mem_root,
320
table.indexes_size() * sizeof(KEY)
321
+share->key_parts*sizeof(KEY_PART_INFO));
323
KEY_PART_INFO *key_part;
325
key_part= reinterpret_cast<KEY_PART_INFO*>
326
(share->key_info+table.indexes_size());
329
ulong *rec_per_key= (ulong*) alloc_root(&share->mem_root,
330
sizeof(ulong*)*share->key_parts);
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));
337
share->keynames.type_lengths= (unsigned int*)
338
alloc_root(&share->mem_root,
339
sizeof(unsigned int) * (table.indexes_size()+1));
341
share->keynames.type_names[share->keynames.count]= NULL;
342
share->keynames.type_lengths[share->keynames.count]= 0;
344
KEY* keyinfo= share->key_info;
345
for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
347
message::Table::Index indx= table.indexes(keynr);
352
if (indx.is_unique())
353
keyinfo->flags|= HA_NOSAME;
355
if (indx.has_options())
357
message::Table::Index::IndexOptions indx_options= indx.options();
358
if (indx_options.pack_key())
359
keyinfo->flags|= HA_PACK_KEY;
361
if (indx_options.var_length_key())
362
keyinfo->flags|= HA_VAR_LENGTH_PART;
364
if (indx_options.null_part_key())
365
keyinfo->flags|= HA_NULL_PART_KEY;
367
if (indx_options.binary_pack_key())
368
keyinfo->flags|= HA_BINARY_PACK_KEY;
370
if (indx_options.has_partial_segments())
371
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
373
if (indx_options.auto_generated_key())
374
keyinfo->flags|= HA_GENERATED_KEY;
376
if (indx_options.has_key_block_size())
378
keyinfo->flags|= HA_USES_BLOCK_SIZE;
379
keyinfo->block_size= indx_options.key_block_size();
383
keyinfo->block_size= 0;
389
case message::Table::Index::UNKNOWN_INDEX:
390
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
392
case message::Table::Index::BTREE:
393
keyinfo->algorithm= HA_KEY_ALG_BTREE;
395
case message::Table::Index::HASH:
396
keyinfo->algorithm= HA_KEY_ALG_HASH;
400
/* TODO: suitable warning ? */
401
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
405
keyinfo->key_length= indx.key_length();
407
keyinfo->key_parts= indx.index_part_size();
409
keyinfo->key_part= key_part;
410
keyinfo->rec_per_key= rec_per_key;
412
for (unsigned int partnr= 0;
413
partnr < keyinfo->key_parts;
414
partnr++, key_part++)
416
message::Table::Index::IndexPart part;
417
part= indx.index_part(partnr);
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;
431
key_part->length= part.compare_length();
433
key_part->store_length= key_part->length;
435
/* key_part->offset is set later */
436
key_part->key_type= part.key_type();
439
if (! indx.has_comment())
441
keyinfo->comment.length= 0;
442
keyinfo->comment.str= NULL;
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);
453
keyinfo->name= strmake_root(&share->mem_root,
455
indx.name().length());
457
share->keynames.type_names[keynr]= keyinfo->name;
458
share->keynames.type_lengths[keynr]= indx.name().length();
461
share->keys_for_keyread.reset();
462
set_prefix(share->keys_in_use, share->keys);
464
share->fields= table.field_size();
466
share->field= (Field**) alloc_root(&share->mem_root,
467
((share->fields+1) * sizeof(Field*)));
468
share->field[share->fields]= NULL;
470
uint32_t null_fields= 0;
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));
476
assert(field_offsets && field_pack_length); // TODO: fixme
478
uint32_t interval_count= 0;
479
uint32_t interval_parts= 0;
481
uint32_t stored_columns_reclength= 0;
483
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
485
message::Table::Field pfield= table.field(fieldnr);
486
if (pfield.constraints().is_nullable())
489
enum_field_types drizzle_field_type=
490
proto_field_type_to_drizzle_type(pfield.type());
492
field_offsets[fieldnr]= stored_columns_reclength;
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)
498
switch(drizzle_field_type)
500
case DRIZZLE_TYPE_BLOB:
501
case DRIZZLE_TYPE_VARCHAR:
503
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
505
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
506
field_options.collation_id() : 0);
509
cs= default_charset_info;
511
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
512
field_options.length() * cs->mbmaxlen);
515
case DRIZZLE_TYPE_ENUM:
517
message::Table::Field::SetFieldOptions field_options= pfield.set_options();
519
field_pack_length[fieldnr]=
520
get_enum_pack_length(field_options.field_value_size());
523
interval_parts+= field_options.field_value_size();
526
case DRIZZLE_TYPE_DECIMAL:
528
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
530
field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
534
/* Zero is okay here as length is fixed for other types. */
535
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
538
share->reclength+= field_pack_length[fieldnr];
539
stored_columns_reclength+= field_pack_length[fieldnr];
542
/* data_offset added to stored_rec_length later */
543
share->stored_rec_length= stored_columns_reclength;
545
share->null_fields= null_fields;
547
ulong null_bits= null_fields;
548
if (! table_options.pack_record())
550
ulong data_offset= (null_bits + 7)/8;
553
share->reclength+= data_offset;
554
share->stored_rec_length+= data_offset;
556
ulong rec_buff_length;
558
rec_buff_length= ALIGN_SIZE(share->reclength + 1);
559
share->rec_buff_length= rec_buff_length;
561
unsigned char* record= NULL;
563
if (! (record= (unsigned char *) alloc_root(&share->mem_root,
567
memset(record, 0, rec_buff_length);
571
if (! table_options.pack_record())
573
null_count++; // one bit for delete mark.
577
share->default_values= record;
581
share->intervals= (TYPELIB *) alloc_root(&share->mem_root,
582
interval_count*sizeof(TYPELIB));
585
share->intervals= NULL;
587
share->fieldnames.type_names= (const char **) alloc_root(&share->mem_root,
588
(share->fields + 1) * sizeof(char*));
590
share->fieldnames.type_lengths= (unsigned int *) alloc_root(&share->mem_root,
591
(share->fields + 1) * sizeof(unsigned int));
593
share->fieldnames.type_names[share->fields]= NULL;
594
share->fieldnames.type_lengths[share->fields]= 0;
595
share->fieldnames.count= share->fields;
598
/* Now fix the TYPELIBs for the intervals (enum values)
602
uint32_t interval_nr= 0;
604
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
606
message::Table::Field pfield= table.field(fieldnr);
609
share->fieldnames.type_names[fieldnr]= strmake_root(&share->mem_root,
610
pfield.name().c_str(),
611
pfield.name().length());
613
share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
616
if (pfield.type() != message::Table::Field::ENUM)
619
message::Table::Field::SetFieldOptions field_options= pfield.set_options();
621
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
622
field_options.collation_id() : 0);
625
charset= default_charset_info;
627
TYPELIB *t= &(share->intervals[interval_nr]);
629
t->type_names= (const char**)alloc_root(&share->mem_root,
630
(field_options.field_value_size() + 1) * sizeof(char*));
632
t->type_lengths= (unsigned int*) alloc_root(&share->mem_root,
633
(field_options.field_value_size() + 1) * sizeof(unsigned int));
635
t->type_names[field_options.field_value_size()]= NULL;
636
t->type_lengths[field_options.field_value_size()]= 0;
638
t->count= field_options.field_value_size();
641
for (int n= 0; n < field_options.field_value_size(); n++)
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());
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.
652
lengthsp= charset->cset->lengthsp(charset,
654
field_options.field_value(n).length());
655
t->type_lengths[n]= lengthsp;
661
/* and read the fields */
664
bool use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
667
use_hash= ! hash_init(&share->name_hash,
672
(hash_get_key) get_field_name,
676
unsigned char* null_pos= record;;
677
int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
679
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
681
message::Table::Field pfield= table.field(fieldnr);
683
enum column_format_type column_format= COLUMN_FORMAT_TYPE_DEFAULT;
685
switch (pfield.format())
687
case message::Table::Field::DefaultFormat:
688
column_format= COLUMN_FORMAT_TYPE_DEFAULT;
690
case message::Table::Field::FixedFormat:
691
column_format= COLUMN_FORMAT_TYPE_FIXED;
693
case message::Table::Field::DynamicFormat:
694
column_format= COLUMN_FORMAT_TYPE_DYNAMIC;
700
Field::utype unireg_type= Field::NONE;
702
if (pfield.has_numeric_options() &&
703
pfield.numeric_options().is_autoincrement())
705
unireg_type= Field::NEXT_NUMBER;
708
if (pfield.has_options() &&
709
pfield.options().has_default_value() &&
710
pfield.options().default_value().compare("NOW()") == 0)
712
if (pfield.options().has_update_value() &&
713
pfield.options().update_value().compare("NOW()") == 0)
715
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
717
else if (! pfield.options().has_update_value())
719
unireg_type= Field::TIMESTAMP_DN_FIELD;
722
assert(1); // Invalid update value.
724
else if (pfield.has_options() &&
725
pfield.options().has_update_value() &&
726
pfield.options().update_value().compare("NOW()") == 0)
728
unireg_type= Field::TIMESTAMP_UN_FIELD;
732
if (!pfield.has_comment())
734
comment.str= (char*)"";
739
size_t len= pfield.comment().length();
740
const char* str= pfield.comment().c_str();
742
comment.str= strmake_root(&share->mem_root, str, len);
746
enum_field_types field_type;
748
field_type= proto_field_type_to_drizzle_type(pfield.type());
750
const CHARSET_INFO *charset= &my_charset_bin;
752
if (field_type == DRIZZLE_TYPE_BLOB ||
753
field_type == DRIZZLE_TYPE_VARCHAR)
755
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
757
charset= get_charset(field_options.has_collation_id() ?
758
field_options.collation_id() : 0);
761
charset= default_charset_info;
764
if (field_type == DRIZZLE_TYPE_ENUM)
766
message::Table::Field::SetFieldOptions field_options= pfield.set_options();
768
charset= get_charset(field_options.has_collation_id()?
769
field_options.collation_id() : 0);
772
charset= default_charset_info;
776
if (field_type == DRIZZLE_TYPE_DECIMAL
777
|| field_type == DRIZZLE_TYPE_DOUBLE)
779
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
781
if (! pfield.has_numeric_options() || ! fo.has_scale())
784
We don't write the default to table proto so
785
if no decimals specified for DOUBLE, we use the default.
787
decimals= NOT_FIXED_DEC;
791
if (fo.scale() > DECIMAL_MAX_SCALE)
796
decimals= static_cast<uint8_t>(fo.scale());
800
Item *default_value= NULL;
802
if (pfield.options().has_default_value() ||
803
pfield.options().has_default_null() ||
804
pfield.options().has_default_bin_value())
806
default_value= default_value_item(field_type,
808
pfield.options().default_null(),
809
&pfield.options().default_value(),
810
&pfield.options().default_bin_value());
814
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
815
memset(&temp_table, 0, sizeof(temp_table));
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;
821
uint32_t field_length= 0; //Assignment is for compiler complaint.
825
case DRIZZLE_TYPE_BLOB:
826
case DRIZZLE_TYPE_VARCHAR:
828
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
830
charset= get_charset(field_options.has_collation_id() ?
831
field_options.collation_id() : 0);
834
charset= default_charset_info;
836
field_length= field_options.length() * charset->mbmaxlen;
839
case DRIZZLE_TYPE_DOUBLE:
841
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
842
if (!fo.has_precision() && !fo.has_scale())
844
field_length= DBL_DIG+7;
848
field_length= fo.precision();
850
if (field_length < decimals &&
851
decimals != NOT_FIXED_DEC)
853
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
859
case DRIZZLE_TYPE_DECIMAL:
861
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
863
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
867
case DRIZZLE_TYPE_TIMESTAMP:
868
case DRIZZLE_TYPE_DATETIME:
869
field_length= DateTime::MAX_STRING_LENGTH;
871
case DRIZZLE_TYPE_DATE:
872
field_length= Date::MAX_STRING_LENGTH;
874
case DRIZZLE_TYPE_ENUM:
878
message::Table::Field::SetFieldOptions fo= pfield.set_options();
880
for(int valnr= 0; valnr < fo.field_value_size(); valnr++)
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())
891
case DRIZZLE_TYPE_LONG:
893
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
894
field_length= MAX_INT_WIDTH+sign_len;
897
case DRIZZLE_TYPE_LONGLONG:
898
field_length= MAX_BIGINT_WIDTH;
900
case DRIZZLE_TYPE_NULL:
901
abort(); // Programming error
904
Field* f= make_field(share,
906
record + field_offsets[fieldnr] + data_offset,
908
pfield.constraints().is_nullable(),
914
(Field::utype) MTYP_TYPENR(unireg_type),
915
((field_type == DRIZZLE_TYPE_ENUM) ?
916
share->intervals + (interval_nr++)
918
share->fieldnames.type_names[fieldnr]);
920
share->field[fieldnr]= f;
922
f->init(&temp_table); /* blob default values need table obj */
924
if (! (f->flags & NOT_NULL_FLAG))
926
*f->null_ptr|= f->null_bit;
927
if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
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? */
940
my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
945
else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
946
(f->flags & NOT_NULL_FLAG))
949
f->store((int64_t) 1, true);
954
/* hack to undo f->init() */
958
f->field_index= fieldnr;
960
if (! default_value &&
961
! (f->unireg_check==Field::NEXT_NUMBER) &&
962
(f->flags & NOT_NULL_FLAG) &&
963
(f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
965
f->flags|= NO_DEFAULT_VALUE_FLAG;
968
if (f->unireg_check == Field::NEXT_NUMBER)
969
share->found_next_number_field= &(share->field[fieldnr]);
971
if (share->timestamp_field == f)
972
share->timestamp_field_offset= fieldnr;
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]));
980
keyinfo= share->key_info;
981
for (unsigned int keynr= 0; keynr < share->keys; keynr++, keyinfo++)
983
key_part= keyinfo->key_part;
985
for (unsigned int partnr= 0;
986
partnr < keyinfo->key_parts;
987
partnr++, key_part++)
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.
994
key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
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.
1004
*(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1006
share->null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1008
share->last_null_bit_pos= null_bit_pos;
1010
free(field_offsets);
1011
field_offsets= NULL;
1012
free(field_pack_length);
1013
field_pack_length= NULL;
1016
if (share->key_parts)
1018
uint32_t primary_key= (uint32_t) (find_type((char*) "PRIMARY",
1019
&share->keynames, 3) - 1); /* @TODO Huh? */
1021
keyinfo= share->key_info;
1022
key_part= keyinfo->key_part;
1024
for (uint32_t key= 0; key < share->keys; key++,keyinfo++)
1026
uint32_t usable_parts= 0;
1028
if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1031
If the UNIQUE key doesn't have NULL columns and is not a part key
1032
declare this as a primary key.
1035
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1037
uint32_t fieldnr= key_part[i].fieldnr;
1039
share->field[fieldnr-1]->null_ptr ||
1040
share->field[fieldnr-1]->key_length() != key_part[i].length)
1042
primary_key= MAX_KEY; // Can't be used
1048
for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1051
if (! key_part->fieldnr)
1053
abort(); // goto err;
1055
field= key_part->field= share->field[key_part->fieldnr-1];
1056
key_part->type= field->key_type();
1057
if (field->null_ptr)
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;
1067
if (field->type() == DRIZZLE_TYPE_BLOB ||
1068
field->real_type() == DRIZZLE_TYPE_VARCHAR)
1070
if (field->type() == DRIZZLE_TYPE_BLOB)
1071
key_part->key_part_flag|= HA_BLOB_PART;
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;
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);
1083
field->key_start.set(key);
1084
if (field->key_length() == key_part->length &&
1085
!(field->flags & BLOB_FLAG))
1087
enum ha_key_alg algo= share->key_info[key].algorithm;
1088
if (share->db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1090
share->keys_for_keyread.set(key);
1091
field->part_of_key.set(key);
1092
field->part_of_key_not_clustered.set(key);
1094
if (share->db_type()->index_flags(algo) & HA_READ_ORDER)
1095
field->part_of_sortkey.set(key);
1097
if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1099
usable_parts++; // For FILESORT
1100
field->flags|= PART_KEY_FLAG;
1101
if (key == primary_key)
1103
field->flags|= PRI_KEY_FLAG;
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
1108
if (share->storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
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;
1115
if (field->key_length() != key_part->length)
1117
key_part->key_part_flag|= HA_PART_KEY_SEG;
1120
keyinfo->usable_key_parts= usable_parts; // Filesort
1122
set_if_bigger(share->max_key_length,keyinfo->key_length+
1123
keyinfo->key_parts);
1124
share->total_key_length+= keyinfo->key_length;
1126
if (keyinfo->flags & HA_NOSAME)
1128
set_if_bigger(share->max_unique_length,keyinfo->key_length);
1131
if (primary_key < MAX_KEY &&
1132
(share->keys_in_use.test(primary_key)))
1134
share->primary_key= primary_key;
1136
If we are using an integer as the primary key then allow the user to
1137
refer to it as '_rowid'
1139
if (share->key_info[primary_key].key_parts == 1)
1141
Field *field= share->key_info[primary_key].key_part[0].field;
1142
if (field && field->result_type() == INT_RESULT)
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].
1151
share->primary_key = MAX_KEY; // we do not have a primary key
1154
share->primary_key= MAX_KEY;
1156
if (share->found_next_number_field)
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)
1165
/* Wrong field definition */
1170
reg_field->flags |= AUTO_INCREMENT_FLAG;
1173
if (share->blob_fields)
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)))))
1183
for (k= 0, ptr= share->field ; *ptr ; ptr++, k++)
1185
if ((*ptr)->flags & BLOB_FLAG)
1190
share->db_low_byte_first= true; // @todo Question this.
1191
share->column_bitmap_size= bitmap_buffer_size(share->fields);
1193
my_bitmap_map *bitmaps;
1195
if (!(bitmaps= (my_bitmap_map*) alloc_root(&share->mem_root,
1196
share->column_bitmap_size)))
1198
share->all_set.init(bitmaps, share->fields);
1199
share->all_set.setAll();
1205
free(field_offsets);
1206
if (field_pack_length)
1207
free(field_pack_length);
1209
share->error= error;
1210
share->open_errno= errno;
1212
hash_free(&share->name_hash);
1213
share->open_table_error(error, share->open_errno, 0);
1219
Read table definition from a binary / text based .frm cursor
1223
session Thread Cursor
1224
share Fill this with table definition
1227
This function is called when the table definition is not cached in
1229
The data is returned in 'share', which is alloced by
1230
alloc_table_share().. The code assumes that share is initialized.
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
1242
int open_table_def(Session& session, TableShare *share)
1250
message::Table table;
1252
error= plugin::StorageEngine::getTableDefinition(session, share->normalized_path.str,
1253
share->getSchemaName(),
1254
share->table_name.str,
1258
if (error != EEXIST)
1267
if (not table.IsInitialized())
1275
error= parse_table_proto(session, table, share);
1277
share->table_category= TABLE_CATEGORY_USER;
1280
if (error && !error_given)
1282
share->error= error;
1283
share->open_table_error(error, (share->open_errno= errno), 0);
1291
Open a table based on a TableShare
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
1301
ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1302
outparam result table
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
1314
int open_table_from_share(Session *session, TableShare *share, const char *alias,
1315
uint32_t db_stat, uint32_t ha_open_flags,
1319
uint32_t records, i, bitmap_size;
1320
bool error_reported= false;
1321
unsigned char *record, *bitmaps;
1324
/* Parsing of partitioning information from .frm needs session->lex set up. */
1325
assert(session->lex->is_lex_started);
1328
outparam->resetTable(session, share, db_stat);
1331
if (not (outparam->alias= strdup(alias)))
1334
/* Allocate Cursor */
1335
if (not (outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
1340
if ((db_stat & HA_OPEN_KEYFILE))
1345
if (!(record= (unsigned char*) alloc_root(&outparam->mem_root,
1346
share->rec_buff_length * records)))
1351
/* We are probably in hard repair, and the buffers should not be used */
1352
outparam->record[0]= outparam->record[1]= share->default_values;
1356
outparam->record[0]= record;
1358
outparam->record[1]= record+ share->rec_buff_length;
1360
outparam->record[1]= outparam->record[0]; // Safety
1365
We need this because when we read var-length rows, we are not updating
1366
bytes after end of varchar
1370
memcpy(outparam->record[0], share->default_values, share->rec_buff_length);
1371
memcpy(outparam->record[1], share->default_values, share->null_bytes);
1373
memcpy(outparam->record[1], share->default_values,
1374
share->rec_buff_length);
1378
if (!(field_ptr = (Field **) alloc_root(&outparam->mem_root,
1379
(uint32_t) ((share->fields+1)*
1383
outparam->field= field_ptr;
1385
record= (unsigned char*) outparam->record[0]-1; /* Fieldstart = 1 */
1387
outparam->null_flags= (unsigned char*) record+1;
1389
/* Setup copy of fields from share, but use the right alias and record */
1390
for (i= 0 ; i < share->fields; i++, field_ptr++)
1392
if (!((*field_ptr)= share->field[i]->clone(&outparam->mem_root, outparam)))
1395
(*field_ptr)= 0; // End marker
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];
1404
/* Fix key->name and key_part->field */
1405
if (share->key_parts)
1407
KEY *key_info, *key_info_end;
1408
KEY_PART_INFO *key_part;
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)))
1413
outparam->key_info= key_info;
1414
key_part= (reinterpret_cast<KEY_PART_INFO*> (key_info+share->keys));
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) *
1420
for (key_info_end= key_info + share->keys ;
1421
key_info < key_info_end ;
1424
KEY_PART_INFO *key_part_end;
1426
key_info->table= outparam;
1427
key_info->key_part= key_part;
1429
for (key_part_end= key_part+ key_info->key_parts ;
1430
key_part < key_part_end ;
1433
Field *field= key_part->field= outparam->field[key_part->fieldnr-1];
1435
if (field->key_length() != key_part->length &&
1436
!(field->flags & BLOB_FLAG))
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
1442
field= key_part->field=field->new_field(&outparam->mem_root,
1444
field->field_length= key_part->length;
1450
/* Allocate bitmaps */
1452
bitmap_size= share->column_bitmap_size;
1453
if (!(bitmaps= (unsigned char*) alloc_root(&outparam->mem_root, bitmap_size*3)))
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();
1460
/* The table struct is now initialized; Open the table */
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))))
1476
case HA_ERR_NO_SUCH_TABLE:
1478
The table did not exists in storage engine, use same error message
1479
as if the .frm cursor didn't exist
1486
Too many files opened, use same error message as if the .frm
1493
outparam->print_error(ha_err, MYF(0));
1494
error_reported= true;
1495
if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1503
#if defined(HAVE_purify)
1504
memset(bitmaps, 0, bitmap_size*3);
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);
1520
bool Table::fill_item_list(List<Item> *item_list) const
1523
All Item_field's created using a direct pointer to a field
1524
are fixed in Item_field constructor.
1526
for (Field **ptr= field; *ptr; ptr++)
1528
Item_field *item= new Item_field(*ptr);
1529
if (!item || item_list->push_back(item))
1535
int Table::closefrm(bool free_share)
77
// @note this should all be the destructor
78
int Table::delete_table(bool free_share)