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;
105
build_table_filename(path, table_list->db, table_list->table_name, false);
107
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
108
if (multi_alloc_root(&mem_root,
109
&share, sizeof(*share),
110
&key_buff, key_length,
111
&path_buff, path.length() + 1,
114
memset(share, 0, sizeof(*share));
116
share->set_table_cache_key(key_buff, key, key_length);
118
share->path.str= path_buff,
119
share->path.length= path.length();
120
strcpy(share->path.str, path.c_str());
121
share->normalized_path.str= share->path.str;
122
share->normalized_path.length= path.length();
124
share->version= refresh_version;
126
memcpy(&share->mem_root, &mem_root, sizeof(mem_root));
127
pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST);
128
pthread_cond_init(&share->cond, NULL);
134
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
136
enum_field_types field_type;
138
switch(proto_field_type)
140
case message::Table::Field::INTEGER:
141
field_type= DRIZZLE_TYPE_LONG;
143
case message::Table::Field::DOUBLE:
144
field_type= DRIZZLE_TYPE_DOUBLE;
146
case message::Table::Field::TIMESTAMP:
147
field_type= DRIZZLE_TYPE_TIMESTAMP;
149
case message::Table::Field::BIGINT:
150
field_type= DRIZZLE_TYPE_LONGLONG;
152
case message::Table::Field::DATETIME:
153
field_type= DRIZZLE_TYPE_DATETIME;
155
case message::Table::Field::DATE:
156
field_type= DRIZZLE_TYPE_DATE;
158
case message::Table::Field::VARCHAR:
159
field_type= DRIZZLE_TYPE_VARCHAR;
161
case message::Table::Field::DECIMAL:
162
field_type= DRIZZLE_TYPE_DECIMAL;
164
case message::Table::Field::ENUM:
165
field_type= DRIZZLE_TYPE_ENUM;
167
case message::Table::Field::BLOB:
168
field_type= DRIZZLE_TYPE_BLOB;
171
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
178
static Item *default_value_item(enum_field_types field_type,
179
const CHARSET_INFO *charset,
180
bool default_null, const string *default_value,
181
const string *default_bin_value)
183
Item *default_item= NULL;
188
return new Item_null();
193
case DRIZZLE_TYPE_LONG:
194
case DRIZZLE_TYPE_LONGLONG:
195
default_item= new Item_int(default_value->c_str(),
196
(int64_t) internal::my_strtoll10(default_value->c_str(),
199
default_value->length());
201
case DRIZZLE_TYPE_DOUBLE:
202
default_item= new Item_float(default_value->c_str(),
203
default_value->length());
205
case DRIZZLE_TYPE_NULL:
207
case DRIZZLE_TYPE_TIMESTAMP:
208
case DRIZZLE_TYPE_DATETIME:
209
case DRIZZLE_TYPE_DATE:
210
if (default_value->compare("NOW()") == 0)
212
case DRIZZLE_TYPE_ENUM:
213
default_item= new Item_string(default_value->c_str(),
214
default_value->length(),
215
system_charset_info);
217
case DRIZZLE_TYPE_VARCHAR:
218
case DRIZZLE_TYPE_BLOB: /* Blob is here due to TINYTEXT. Feel the hate. */
219
if (charset==&my_charset_bin)
221
default_item= new Item_string(default_bin_value->c_str(),
222
default_bin_value->length(),
227
default_item= new Item_string(default_value->c_str(),
228
default_value->length(),
229
system_charset_info);
232
case DRIZZLE_TYPE_DECIMAL:
233
default_item= new Item_decimal(default_value->c_str(),
234
default_value->length(),
235
system_charset_info);
242
int parse_table_proto(Session& session,
243
message::Table &table,
248
if (! table.IsInitialized())
250
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
251
return ER_CORRUPT_TABLE_DEFINITION;
254
share->setTableProto(new(nothrow) message::Table(table));
256
share->storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
257
assert(share->storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
259
message::Table::TableOptions table_options;
261
if (table.has_options())
262
table_options= table.options();
264
uint32_t db_create_options= 0;
266
if (table_options.has_pack_keys())
268
if (table_options.pack_keys())
269
db_create_options|= HA_OPTION_PACK_KEYS;
271
db_create_options|= HA_OPTION_NO_PACK_KEYS;
274
if (table_options.pack_record())
275
db_create_options|= HA_OPTION_PACK_RECORD;
277
/* db_create_options was stored as 2 bytes in FRM
278
Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
280
share->db_create_options= (db_create_options & 0x0000FFFF);
281
share->db_options_in_use= share->db_create_options;
283
share->row_type= table_options.has_row_type() ?
284
(enum row_type) table_options.row_type() : ROW_TYPE_DEFAULT;
286
share->block_size= table_options.has_block_size() ?
287
table_options.block_size() : 0;
289
share->table_charset= get_charset(table_options.has_collation_id()?
290
table_options.collation_id() : 0);
292
if (!share->table_charset)
294
/* unknown charset in head[38] or pre-3.23 frm */
295
if (use_mb(default_charset_info))
297
/* Warn that we may be changing the size of character columns */
298
errmsg_printf(ERRMSG_LVL_WARN,
299
_("'%s' had no or invalid character set, "
300
"and default character set is multi-byte, "
301
"so character column sizes may have changed"),
304
share->table_charset= default_charset_info;
307
share->db_record_offset= 1;
309
share->blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
311
share->keys= table.indexes_size();
314
for (int indx= 0; indx < table.indexes_size(); indx++)
315
share->key_parts+= table.indexes(indx).index_part_size();
317
share->key_info= (KEY*) alloc_root(&share->mem_root,
318
table.indexes_size() * sizeof(KEY)
319
+share->key_parts*sizeof(KEY_PART_INFO));
321
KEY_PART_INFO *key_part;
323
key_part= reinterpret_cast<KEY_PART_INFO*>
324
(share->key_info+table.indexes_size());
327
ulong *rec_per_key= (ulong*) alloc_root(&share->mem_root,
328
sizeof(ulong*)*share->key_parts);
330
share->keynames.count= table.indexes_size();
331
share->keynames.name= NULL;
332
share->keynames.type_names= (const char**)
333
alloc_root(&share->mem_root, sizeof(char*) * (table.indexes_size()+1));
335
share->keynames.type_lengths= (unsigned int*)
336
alloc_root(&share->mem_root,
337
sizeof(unsigned int) * (table.indexes_size()+1));
339
share->keynames.type_names[share->keynames.count]= NULL;
340
share->keynames.type_lengths[share->keynames.count]= 0;
342
KEY* keyinfo= share->key_info;
343
for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
345
message::Table::Index indx= table.indexes(keynr);
350
if (indx.is_unique())
351
keyinfo->flags|= HA_NOSAME;
353
if (indx.has_options())
355
message::Table::Index::IndexOptions indx_options= indx.options();
356
if (indx_options.pack_key())
357
keyinfo->flags|= HA_PACK_KEY;
359
if (indx_options.var_length_key())
360
keyinfo->flags|= HA_VAR_LENGTH_PART;
362
if (indx_options.null_part_key())
363
keyinfo->flags|= HA_NULL_PART_KEY;
365
if (indx_options.binary_pack_key())
366
keyinfo->flags|= HA_BINARY_PACK_KEY;
368
if (indx_options.has_partial_segments())
369
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
371
if (indx_options.auto_generated_key())
372
keyinfo->flags|= HA_GENERATED_KEY;
374
if (indx_options.has_key_block_size())
376
keyinfo->flags|= HA_USES_BLOCK_SIZE;
377
keyinfo->block_size= indx_options.key_block_size();
381
keyinfo->block_size= 0;
387
case message::Table::Index::UNKNOWN_INDEX:
388
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
390
case message::Table::Index::BTREE:
391
keyinfo->algorithm= HA_KEY_ALG_BTREE;
393
case message::Table::Index::HASH:
394
keyinfo->algorithm= HA_KEY_ALG_HASH;
398
/* TODO: suitable warning ? */
399
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
403
keyinfo->key_length= indx.key_length();
405
keyinfo->key_parts= indx.index_part_size();
407
keyinfo->key_part= key_part;
408
keyinfo->rec_per_key= rec_per_key;
410
for (unsigned int partnr= 0;
411
partnr < keyinfo->key_parts;
412
partnr++, key_part++)
414
message::Table::Index::IndexPart part;
415
part= indx.index_part(partnr);
419
key_part->field= NULL;
420
key_part->fieldnr= part.fieldnr() + 1; // start from 1.
421
key_part->null_bit= 0;
422
/* key_part->null_offset is only set if null_bit (see later) */
423
/* key_part->key_type= */ /* I *THINK* this may be okay.... */
424
/* key_part->type ???? */
425
key_part->key_part_flag= 0;
426
if (part.has_in_reverse_order())
427
key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
429
key_part->length= part.compare_length();
431
key_part->store_length= key_part->length;
433
/* key_part->offset is set later */
434
key_part->key_type= part.key_type();
437
if (! indx.has_comment())
439
keyinfo->comment.length= 0;
440
keyinfo->comment.str= NULL;
444
keyinfo->flags|= HA_USES_COMMENT;
445
keyinfo->comment.length= indx.comment().length();
446
keyinfo->comment.str= strmake_root(&share->mem_root,
447
indx.comment().c_str(),
448
keyinfo->comment.length);
451
keyinfo->name= strmake_root(&share->mem_root,
453
indx.name().length());
455
share->keynames.type_names[keynr]= keyinfo->name;
456
share->keynames.type_lengths[keynr]= indx.name().length();
459
share->keys_for_keyread.reset();
460
set_prefix(share->keys_in_use, share->keys);
462
share->fields= table.field_size();
464
share->field= (Field**) alloc_root(&share->mem_root,
465
((share->fields+1) * sizeof(Field*)));
466
share->field[share->fields]= NULL;
468
uint32_t null_fields= 0;
471
uint32_t *field_offsets= (uint32_t*)malloc(share->fields * sizeof(uint32_t));
472
uint32_t *field_pack_length=(uint32_t*)malloc(share->fields*sizeof(uint32_t));
474
assert(field_offsets && field_pack_length); // TODO: fixme
476
uint32_t interval_count= 0;
477
uint32_t interval_parts= 0;
479
uint32_t stored_columns_reclength= 0;
481
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
483
message::Table::Field pfield= table.field(fieldnr);
484
if (pfield.constraints().is_nullable())
487
enum_field_types drizzle_field_type=
488
proto_field_type_to_drizzle_type(pfield.type());
490
field_offsets[fieldnr]= stored_columns_reclength;
492
/* the below switch is very similar to
493
CreateField::create_length_to_internal_length in field.cc
494
(which should one day be replace by just this code)
496
switch(drizzle_field_type)
498
case DRIZZLE_TYPE_BLOB:
499
case DRIZZLE_TYPE_VARCHAR:
501
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
503
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
504
field_options.collation_id() : 0);
507
cs= default_charset_info;
509
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
510
field_options.length() * cs->mbmaxlen);
513
case DRIZZLE_TYPE_ENUM:
515
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
517
field_pack_length[fieldnr]=
518
get_enum_pack_length(field_options.field_value_size());
521
interval_parts+= field_options.field_value_size();
524
case DRIZZLE_TYPE_DECIMAL:
526
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
528
field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
532
/* Zero is okay here as length is fixed for other types. */
533
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
536
share->reclength+= field_pack_length[fieldnr];
537
stored_columns_reclength+= field_pack_length[fieldnr];
540
/* data_offset added to stored_rec_length later */
541
share->stored_rec_length= stored_columns_reclength;
543
share->null_fields= null_fields;
545
ulong null_bits= null_fields;
546
if (! table_options.pack_record())
548
ulong data_offset= (null_bits + 7)/8;
551
share->reclength+= data_offset;
552
share->stored_rec_length+= data_offset;
554
ulong rec_buff_length;
556
rec_buff_length= ALIGN_SIZE(share->reclength + 1);
557
share->rec_buff_length= rec_buff_length;
559
unsigned char* record= NULL;
561
if (! (record= (unsigned char *) alloc_root(&share->mem_root,
565
memset(record, 0, rec_buff_length);
569
if (! table_options.pack_record())
571
null_count++; // one bit for delete mark.
575
share->default_values= record;
579
share->intervals= (TYPELIB *) alloc_root(&share->mem_root,
580
interval_count*sizeof(TYPELIB));
583
share->intervals= NULL;
585
share->fieldnames.type_names= (const char **) alloc_root(&share->mem_root,
586
(share->fields + 1) * sizeof(char*));
588
share->fieldnames.type_lengths= (unsigned int *) alloc_root(&share->mem_root,
589
(share->fields + 1) * sizeof(unsigned int));
591
share->fieldnames.type_names[share->fields]= NULL;
592
share->fieldnames.type_lengths[share->fields]= 0;
593
share->fieldnames.count= share->fields;
596
/* Now fix the TYPELIBs for the intervals (enum values)
600
uint32_t interval_nr= 0;
602
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
604
message::Table::Field pfield= table.field(fieldnr);
607
share->fieldnames.type_names[fieldnr]= strmake_root(&share->mem_root,
608
pfield.name().c_str(),
609
pfield.name().length());
611
share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
614
if (pfield.type() != message::Table::Field::ENUM)
617
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
619
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
620
field_options.collation_id() : 0);
623
charset= default_charset_info;
625
TYPELIB *t= &(share->intervals[interval_nr]);
627
t->type_names= (const char**)alloc_root(&share->mem_root,
628
(field_options.field_value_size() + 1) * sizeof(char*));
630
t->type_lengths= (unsigned int*) alloc_root(&share->mem_root,
631
(field_options.field_value_size() + 1) * sizeof(unsigned int));
633
t->type_names[field_options.field_value_size()]= NULL;
634
t->type_lengths[field_options.field_value_size()]= 0;
636
t->count= field_options.field_value_size();
639
for (int n= 0; n < field_options.field_value_size(); n++)
641
t->type_names[n]= strmake_root(&share->mem_root,
642
field_options.field_value(n).c_str(),
643
field_options.field_value(n).length());
646
* Go ask the charset what the length is as for "" length=1
647
* and there's stripping spaces or some other crack going on.
650
lengthsp= charset->cset->lengthsp(charset,
652
field_options.field_value(n).length());
653
t->type_lengths[n]= lengthsp;
659
/* and read the fields */
662
bool use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
665
use_hash= ! hash_init(&share->name_hash,
670
(hash_get_key) get_field_name,
674
unsigned char* null_pos= record;;
675
int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
677
for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
679
message::Table::Field pfield= table.field(fieldnr);
681
enum column_format_type column_format= COLUMN_FORMAT_TYPE_DEFAULT;
683
switch (pfield.format())
685
case message::Table::Field::DefaultFormat:
686
column_format= COLUMN_FORMAT_TYPE_DEFAULT;
688
case message::Table::Field::FixedFormat:
689
column_format= COLUMN_FORMAT_TYPE_FIXED;
691
case message::Table::Field::DynamicFormat:
692
column_format= COLUMN_FORMAT_TYPE_DYNAMIC;
698
Field::utype unireg_type= Field::NONE;
700
if (pfield.has_numeric_options() &&
701
pfield.numeric_options().is_autoincrement())
703
unireg_type= Field::NEXT_NUMBER;
706
if (pfield.has_options() &&
707
pfield.options().has_default_value() &&
708
pfield.options().default_value().compare("NOW()") == 0)
710
if (pfield.options().has_update_value() &&
711
pfield.options().update_value().compare("NOW()") == 0)
713
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
715
else if (! pfield.options().has_update_value())
717
unireg_type= Field::TIMESTAMP_DN_FIELD;
720
assert(1); // Invalid update value.
722
else if (pfield.has_options() &&
723
pfield.options().has_update_value() &&
724
pfield.options().update_value().compare("NOW()") == 0)
726
unireg_type= Field::TIMESTAMP_UN_FIELD;
730
if (!pfield.has_comment())
732
comment.str= (char*)"";
737
size_t len= pfield.comment().length();
738
const char* str= pfield.comment().c_str();
740
comment.str= strmake_root(&share->mem_root, str, len);
744
enum_field_types field_type;
746
field_type= proto_field_type_to_drizzle_type(pfield.type());
748
const CHARSET_INFO *charset= &my_charset_bin;
750
if (field_type == DRIZZLE_TYPE_BLOB ||
751
field_type == DRIZZLE_TYPE_VARCHAR)
753
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
755
charset= get_charset(field_options.has_collation_id() ?
756
field_options.collation_id() : 0);
759
charset= default_charset_info;
762
if (field_type == DRIZZLE_TYPE_ENUM)
764
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
766
charset= get_charset(field_options.has_collation_id()?
767
field_options.collation_id() : 0);
770
charset= default_charset_info;
774
if (field_type == DRIZZLE_TYPE_DECIMAL
775
|| field_type == DRIZZLE_TYPE_DOUBLE)
777
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
779
if (! pfield.has_numeric_options() || ! fo.has_scale())
782
We don't write the default to table proto so
783
if no decimals specified for DOUBLE, we use the default.
785
decimals= NOT_FIXED_DEC;
789
if (fo.scale() > DECIMAL_MAX_SCALE)
794
decimals= static_cast<uint8_t>(fo.scale());
798
Item *default_value= NULL;
800
if (pfield.options().has_default_value() ||
801
pfield.options().has_default_null() ||
802
pfield.options().has_default_bin_value())
804
default_value= default_value_item(field_type,
806
pfield.options().default_null(),
807
&pfield.options().default_value(),
808
&pfield.options().default_bin_value());
812
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
813
memset(&temp_table, 0, sizeof(temp_table));
815
temp_table.in_use= &session;
816
temp_table.s->db_low_byte_first= true; //Cursor->low_byte_first();
817
temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
819
uint32_t field_length= 0; //Assignment is for compiler complaint.
823
case DRIZZLE_TYPE_BLOB:
824
case DRIZZLE_TYPE_VARCHAR:
826
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
828
charset= get_charset(field_options.has_collation_id() ?
829
field_options.collation_id() : 0);
832
charset= default_charset_info;
834
field_length= field_options.length() * charset->mbmaxlen;
837
case DRIZZLE_TYPE_DOUBLE:
839
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
840
if (!fo.has_precision() && !fo.has_scale())
842
field_length= DBL_DIG+7;
846
field_length= fo.precision();
848
if (field_length < decimals &&
849
decimals != NOT_FIXED_DEC)
851
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
857
case DRIZZLE_TYPE_DECIMAL:
859
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
861
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
865
case DRIZZLE_TYPE_TIMESTAMP:
866
case DRIZZLE_TYPE_DATETIME:
867
field_length= DateTime::MAX_STRING_LENGTH;
869
case DRIZZLE_TYPE_DATE:
870
field_length= Date::MAX_STRING_LENGTH;
872
case DRIZZLE_TYPE_ENUM:
876
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
878
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
880
if (fo.field_value(valnr).length() > field_length)
882
field_length= charset->cset->numchars(charset,
883
fo.field_value(valnr).c_str(),
884
fo.field_value(valnr).c_str()
885
+ 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, TableIdentifier &identifier, TableShare *share)
1250
message::Table table;
1252
error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1254
if (error != EEXIST)
1263
if (not table.IsInitialized())
1271
error= parse_table_proto(session, table, share);
1273
share->table_category= TABLE_CATEGORY_USER;
1276
if (error && !error_given)
1278
share->error= error;
1279
share->open_table_error(error, (share->open_errno= errno), 0);
1287
Open a table based on a TableShare
1290
open_table_from_share()
1291
session Thread Cursor
1292
share Table definition
1293
alias Alias for table
1294
db_stat open flags (for example HA_OPEN_KEYFILE|
1295
HA_OPEN_RNDFILE..) can be 0 (example in
1297
ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1298
outparam result table
1302
1 Error (see open_table_error)
1303
2 Error (see open_table_error)
1304
3 Wrong data in .frm cursor
1305
4 Error (see open_table_error)
1306
5 Error (see open_table_error: charset unavailable)
1307
7 Table definition has changed in engine
1310
int open_table_from_share(Session *session, TableShare *share, const char *alias,
1311
uint32_t db_stat, uint32_t ha_open_flags,
1315
uint32_t records, i, bitmap_size;
1316
bool error_reported= false;
1317
unsigned char *record, *bitmaps;
1320
/* Parsing of partitioning information from .frm needs session->lex set up. */
1321
assert(session->lex->is_lex_started);
1324
outparam->resetTable(session, share, db_stat);
1327
if (not (outparam->alias= strdup(alias)))
1330
/* Allocate Cursor */
1331
if (not (outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
1336
if ((db_stat & HA_OPEN_KEYFILE))
1341
if (!(record= (unsigned char*) alloc_root(&outparam->mem_root,
1342
share->rec_buff_length * records)))
1347
/* We are probably in hard repair, and the buffers should not be used */
1348
outparam->record[0]= outparam->record[1]= share->default_values;
1352
outparam->record[0]= record;
1354
outparam->record[1]= record+ share->rec_buff_length;
1356
outparam->record[1]= outparam->record[0]; // Safety
1361
We need this because when we read var-length rows, we are not updating
1362
bytes after end of varchar
1366
memcpy(outparam->record[0], share->default_values, share->rec_buff_length);
1367
memcpy(outparam->record[1], share->default_values, share->null_bytes);
1369
memcpy(outparam->record[1], share->default_values,
1370
share->rec_buff_length);
1374
if (!(field_ptr = (Field **) alloc_root(&outparam->mem_root,
1375
(uint32_t) ((share->fields+1)*
1379
outparam->field= field_ptr;
1381
record= (unsigned char*) outparam->record[0]-1; /* Fieldstart = 1 */
1383
outparam->null_flags= (unsigned char*) record+1;
1385
/* Setup copy of fields from share, but use the right alias and record */
1386
for (i= 0 ; i < share->fields; i++, field_ptr++)
1388
if (!((*field_ptr)= share->field[i]->clone(&outparam->mem_root, outparam)))
1391
(*field_ptr)= 0; // End marker
1393
if (share->found_next_number_field)
1394
outparam->found_next_number_field=
1395
outparam->field[(uint32_t) (share->found_next_number_field - share->field)];
1396
if (share->timestamp_field)
1397
outparam->timestamp_field= (Field_timestamp*) outparam->field[share->timestamp_field_offset];
1400
/* Fix key->name and key_part->field */
1401
if (share->key_parts)
1403
KEY *key_info, *key_info_end;
1404
KEY_PART_INFO *key_part;
1406
n_length= share->keys*sizeof(KEY) + share->key_parts*sizeof(KEY_PART_INFO);
1407
if (!(key_info= (KEY*) alloc_root(&outparam->mem_root, n_length)))
1409
outparam->key_info= key_info;
1410
key_part= (reinterpret_cast<KEY_PART_INFO*> (key_info+share->keys));
1412
memcpy(key_info, share->key_info, sizeof(*key_info)*share->keys);
1413
memcpy(key_part, share->key_info[0].key_part, (sizeof(*key_part) *
1416
for (key_info_end= key_info + share->keys ;
1417
key_info < key_info_end ;
1420
KEY_PART_INFO *key_part_end;
1422
key_info->table= outparam;
1423
key_info->key_part= key_part;
1425
for (key_part_end= key_part+ key_info->key_parts ;
1426
key_part < key_part_end ;
1429
Field *field= key_part->field= outparam->field[key_part->fieldnr-1];
1431
if (field->key_length() != key_part->length &&
1432
!(field->flags & BLOB_FLAG))
1435
We are using only a prefix of the column as a key:
1436
Create a new field for the key part that matches the index
1438
field= key_part->field=field->new_field(&outparam->mem_root,
1440
field->field_length= key_part->length;
1446
/* Allocate bitmaps */
1448
bitmap_size= share->column_bitmap_size;
1449
if (!(bitmaps= (unsigned char*) alloc_root(&outparam->mem_root, bitmap_size*3)))
1451
outparam->def_read_set.init((my_bitmap_map*) bitmaps, share->fields);
1452
outparam->def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), share->fields);
1453
outparam->tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), share->fields);
1454
outparam->default_column_bitmaps();
1456
/* The table struct is now initialized; Open the table */
1461
if ((ha_err= (outparam->cursor->
1462
ha_open(outparam, share->normalized_path.str,
1463
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1464
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
1465
(db_stat & HA_WAIT_IF_LOCKED) ? HA_OPEN_WAIT_IF_LOCKED :
1466
(db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO)) ?
1467
HA_OPEN_ABORT_IF_LOCKED :
1468
HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1472
case HA_ERR_NO_SUCH_TABLE:
1474
The table did not exists in storage engine, use same error message
1475
as if the .frm cursor didn't exist
1482
Too many files opened, use same error message as if the .frm
1489
outparam->print_error(ha_err, MYF(0));
1490
error_reported= true;
1491
if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1499
#if defined(HAVE_purify)
1500
memset(bitmaps, 0, bitmap_size*3);
1506
if (!error_reported)
1507
share->open_table_error(error, errno, 0);
1508
delete outparam->cursor;
1509
outparam->cursor= 0; // For easier error checking
1510
outparam->db_stat= 0;
1511
free_root(&outparam->mem_root, MYF(0)); // Safe to call on zeroed root
1512
free((char*) outparam->alias);
1516
bool Table::fill_item_list(List<Item> *item_list) const
1519
All Item_field's created using a direct pointer to a field
1520
are fixed in Item_field constructor.
1522
for (Field **ptr= field; *ptr; ptr++)
1524
Item_field *item= new Item_field(*ptr);
1525
if (!item || item_list->push_back(item))
1531
int Table::closefrm(bool free_share)
77
// @note this should all be the destructor
78
int Table::delete_table(bool free_share)