303
TableDefinitionCache &TableShare::getCache()
455
const TableDefinitionCache &TableShare::getCache()
305
457
return table_def_cache;
460
TableShare::TableShare(TableIdentifier::Type type_arg) :
461
table_category(TABLE_UNKNOWN_CATEGORY),
463
found_next_number_field(NULL),
464
timestamp_field(NULL),
471
stored_rec_length(0),
474
storage_engine(NULL),
478
last_null_bit_pos(0),
484
max_unique_length(0),
489
timestamp_field_offset(0),
491
db_create_options(0),
492
db_options_in_use(0),
494
rowid_field_offset(0),
495
primary_key(MAX_KEY),
496
next_number_index(0),
497
next_number_key_offset(0),
498
next_number_keypart(0),
502
column_bitmap_size(0),
504
db_low_byte_first(false),
506
replace_with_name_lock(false),
507
waiting_on_cond(false),
510
event_observers(NULL),
513
memset(&name_hash, 0, sizeof(HASH));
516
memset(&db, 0, sizeof(LEX_STRING));
517
memset(&table_name, 0, sizeof(LEX_STRING));
518
memset(&path, 0, sizeof(LEX_STRING));
519
memset(&normalized_path, 0, sizeof(LEX_STRING));
521
if (type_arg == message::Table::INTERNAL)
523
TableIdentifier::build_tmptable_filename(private_key_for_cache);
524
init(&private_key_for_cache[0], &private_key_for_cache[0]);
532
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
533
table_category(TABLE_UNKNOWN_CATEGORY),
535
found_next_number_field(NULL),
536
timestamp_field(NULL),
543
stored_rec_length(0),
546
storage_engine(NULL),
547
tmp_table(message::Table::INTERNAL),
550
last_null_bit_pos(0),
556
max_unique_length(0),
561
timestamp_field_offset(0),
563
db_create_options(0),
564
db_options_in_use(0),
566
rowid_field_offset(0),
567
primary_key(MAX_KEY),
568
next_number_index(0),
569
next_number_key_offset(0),
570
next_number_keypart(0),
574
column_bitmap_size(0),
576
db_low_byte_first(false),
578
replace_with_name_lock(false),
579
waiting_on_cond(false),
582
event_observers(NULL),
585
memset(&name_hash, 0, sizeof(HASH));
587
assert(identifier.getKey() == key);
590
memset(&path, 0, sizeof(LEX_STRING));
591
memset(&normalized_path, 0, sizeof(LEX_STRING));
593
private_key_for_cache= key;
595
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
596
table_category= TABLE_CATEGORY_TEMPORARY;
597
tmp_table= message::Table::INTERNAL;
599
db.str= &private_key_for_cache[0];
600
db.length= strlen(&private_key_for_cache[0]);
602
table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
603
table_name.length= strlen(table_name.str);
604
path.str= (char *)"";
605
normalized_path.str= path.str;
606
path.length= normalized_path.length= 0;
607
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
608
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
612
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
613
table_category(TABLE_UNKNOWN_CATEGORY),
615
found_next_number_field(NULL),
616
timestamp_field(NULL),
623
stored_rec_length(0),
626
storage_engine(NULL),
627
tmp_table(identifier.getType()),
630
last_null_bit_pos(0),
636
max_unique_length(0),
641
timestamp_field_offset(0),
643
db_create_options(0),
644
db_options_in_use(0),
646
rowid_field_offset(0),
647
primary_key(MAX_KEY),
648
next_number_index(0),
649
next_number_key_offset(0),
650
next_number_keypart(0),
654
column_bitmap_size(0),
656
db_low_byte_first(false),
658
replace_with_name_lock(false),
659
waiting_on_cond(false),
662
event_observers(NULL),
665
memset(&name_hash, 0, sizeof(HASH));
668
memset(&db, 0, sizeof(LEX_STRING));
669
memset(&table_name, 0, sizeof(LEX_STRING));
670
memset(&path, 0, sizeof(LEX_STRING));
671
memset(&normalized_path, 0, sizeof(LEX_STRING));
673
private_key_for_cache= identifier.getKey();
674
assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
675
private_normalized_path.resize(identifier.getPath().size() + 1);
676
memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
679
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
680
table_category= TABLE_CATEGORY_TEMPORARY;
681
tmp_table= message::Table::INTERNAL;
682
db.str= &private_key_for_cache[0];
683
db.length= strlen(&private_key_for_cache[0]);
684
table_name.str= db.str + 1;
685
table_name.length= strlen(table_name.str);
686
path.str= &private_normalized_path[0];
687
normalized_path.str= path.str;
688
path.length= normalized_path.length= private_normalized_path.size();
694
Used for shares that will go into the cache.
696
TableShare::TableShare(TableIdentifier::Type type_arg,
697
TableIdentifier &identifier,
699
uint32_t path_length_arg) :
700
table_category(TABLE_UNKNOWN_CATEGORY),
702
found_next_number_field(NULL),
703
timestamp_field(NULL),
710
stored_rec_length(0),
713
storage_engine(NULL),
717
last_null_bit_pos(0),
723
max_unique_length(0),
728
timestamp_field_offset(0),
730
db_create_options(0),
731
db_options_in_use(0),
733
rowid_field_offset(0),
734
primary_key(MAX_KEY),
735
next_number_index(0),
736
next_number_key_offset(0),
737
next_number_keypart(0),
741
column_bitmap_size(0),
743
db_low_byte_first(false),
745
replace_with_name_lock(false),
746
waiting_on_cond(false),
749
event_observers(NULL),
752
memset(&name_hash, 0, sizeof(HASH));
755
memset(&db, 0, sizeof(LEX_STRING));
756
memset(&table_name, 0, sizeof(LEX_STRING));
757
memset(&path, 0, sizeof(LEX_STRING));
758
memset(&normalized_path, 0, sizeof(LEX_STRING));
760
mem_root.init_alloc_root(TABLE_ALLOC_BLOCK_SIZE);
764
private_key_for_cache= identifier.getKey();
766
Let us use the fact that the key is "db/0/table_name/0" + optional
767
part for temporary tables.
769
db.str= &private_key_for_cache[0];
770
db.length= strlen(db.str);
771
table_name.str= db.str + db.length + 1;
772
table_name.length= strlen(table_name.str);
776
_path.append(path_arg, path_length_arg);
780
TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
783
if (mem_root.multi_alloc_root(0,
784
&path_buff, _path.length() + 1,
787
setPath(path_buff, _path.length());
788
strcpy(path_buff, _path.c_str());
789
setNormalizedPath(path_buff, _path.length());
791
version= refresh_version;
793
pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
794
pthread_cond_init(&cond, NULL);
798
assert(0); // We should throw here.
804
void TableShare::init(const char *new_table_name,
805
const char *new_path)
808
memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
809
table_category= TABLE_CATEGORY_TEMPORARY;
810
tmp_table= message::Table::INTERNAL;
813
table_name.str= (char*) new_table_name;
814
table_name.length= strlen(new_table_name);
815
path.str= (char*) new_path;
816
normalized_path.str= (char*) new_path;
817
path.length= normalized_path.length= strlen(new_path);
820
TableShare::~TableShare()
822
assert(ref_count == 0);
825
If someone is waiting for this to be deleted, inform it about this.
826
Don't do a delete until we know that no one is refering to this anymore.
828
if (tmp_table == message::Table::STANDARD)
830
/* share->mutex is locked in release_table_share() */
831
while (waiting_on_cond)
833
pthread_cond_broadcast(&cond);
834
pthread_cond_wait(&cond, &mutex);
836
/* No thread refers to this anymore */
837
pthread_mutex_unlock(&mutex);
838
pthread_mutex_destroy(&mutex);
839
pthread_cond_destroy(&cond);
841
hash_free(&name_hash);
843
storage_engine= NULL;
848
mem_root.free_root(MYF(0)); // Free's share
851
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
853
private_key_for_cache.clear();
854
private_key_for_cache= identifier_arg.getKey();
857
Let us use the fact that the key is "db/0/table_name/0" + optional
858
part for temporary tables.
860
db.str= &private_key_for_cache[0];
861
db.length= strlen(db.str);
862
table_name.str= db.str + db.length + 1;
863
table_name.length= strlen(table_name.str);
865
table_proto->set_name(identifier_arg.getTableName());
866
table_proto->set_schema(identifier_arg.getSchemaName());
869
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
873
if (! table.IsInitialized())
875
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
876
return ER_CORRUPT_TABLE_DEFINITION;
879
setTableProto(new(nothrow) message::Table(table));
881
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
882
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
884
message::Table::TableOptions table_options;
886
if (table.has_options())
887
table_options= table.options();
889
uint32_t local_db_create_options= 0;
891
if (table_options.pack_record())
892
local_db_create_options|= HA_OPTION_PACK_RECORD;
894
/* local_db_create_options was stored as 2 bytes in FRM
895
Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
897
db_create_options= (local_db_create_options & 0x0000FFFF);
898
db_options_in_use= db_create_options;
900
block_size= table_options.has_block_size() ?
901
table_options.block_size() : 0;
903
table_charset= get_charset(table_options.collation_id());
908
snprintf(errmsg, sizeof(errmsg),
909
_("Table %s has invalid/unknown collation: %d,%s"),
911
table_options.collation_id(),
912
table_options.collation().c_str());
915
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
916
return ER_CORRUPT_TABLE_DEFINITION;
921
blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
923
keys= table.indexes_size();
926
for (int indx= 0; indx < table.indexes_size(); indx++)
927
key_parts+= table.indexes(indx).index_part_size();
929
key_info= (KeyInfo*) alloc_root( table.indexes_size() * sizeof(KeyInfo) +key_parts*sizeof(KeyPartInfo));
931
KeyPartInfo *key_part;
933
key_part= reinterpret_cast<KeyPartInfo*>
934
(key_info+table.indexes_size());
937
ulong *rec_per_key= (ulong*) alloc_root(sizeof(ulong*)*key_parts);
939
KeyInfo* keyinfo= key_info;
940
for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
942
message::Table::Index indx= table.indexes(keynr);
947
if (indx.is_unique())
948
keyinfo->flags|= HA_NOSAME;
950
if (indx.has_options())
952
message::Table::Index::Options indx_options= indx.options();
953
if (indx_options.pack_key())
954
keyinfo->flags|= HA_PACK_KEY;
956
if (indx_options.var_length_key())
957
keyinfo->flags|= HA_VAR_LENGTH_PART;
959
if (indx_options.null_part_key())
960
keyinfo->flags|= HA_NULL_PART_KEY;
962
if (indx_options.binary_pack_key())
963
keyinfo->flags|= HA_BINARY_PACK_KEY;
965
if (indx_options.has_partial_segments())
966
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
968
if (indx_options.auto_generated_key())
969
keyinfo->flags|= HA_GENERATED_KEY;
971
if (indx_options.has_key_block_size())
973
keyinfo->flags|= HA_USES_BLOCK_SIZE;
974
keyinfo->block_size= indx_options.key_block_size();
978
keyinfo->block_size= 0;
984
case message::Table::Index::UNKNOWN_INDEX:
985
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
987
case message::Table::Index::BTREE:
988
keyinfo->algorithm= HA_KEY_ALG_BTREE;
990
case message::Table::Index::HASH:
991
keyinfo->algorithm= HA_KEY_ALG_HASH;
995
/* TODO: suitable warning ? */
996
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1000
keyinfo->key_length= indx.key_length();
1002
keyinfo->key_parts= indx.index_part_size();
1004
keyinfo->key_part= key_part;
1005
keyinfo->rec_per_key= rec_per_key;
1007
for (unsigned int partnr= 0;
1008
partnr < keyinfo->key_parts;
1009
partnr++, key_part++)
1011
message::Table::Index::IndexPart part;
1012
part= indx.index_part(partnr);
1016
key_part->field= NULL;
1017
key_part->fieldnr= part.fieldnr() + 1; // start from 1.
1018
key_part->null_bit= 0;
1019
/* key_part->null_offset is only set if null_bit (see later) */
1020
/* key_part->key_type= */ /* I *THINK* this may be okay.... */
1021
/* key_part->type ???? */
1022
key_part->key_part_flag= 0;
1023
if (part.has_in_reverse_order())
1024
key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
1026
key_part->length= part.compare_length();
1030
if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
1031
|| table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
1033
uint32_t collation_id;
1035
if (table.field(part.fieldnr()).string_options().has_collation_id())
1036
collation_id= table.field(part.fieldnr()).string_options().collation_id();
1038
collation_id= table.options().collation_id();
1040
const CHARSET_INFO *cs= get_charset(collation_id);
1042
mbmaxlen= cs->mbmaxlen;
1044
key_part->length*= mbmaxlen;
1046
key_part->store_length= key_part->length;
1048
/* key_part->offset is set later */
1049
key_part->key_type= 0;
1052
if (! indx.has_comment())
1054
keyinfo->comment.length= 0;
1055
keyinfo->comment.str= NULL;
1059
keyinfo->flags|= HA_USES_COMMENT;
1060
keyinfo->comment.length= indx.comment().length();
1061
keyinfo->comment.str= strmake_root(indx.comment().c_str(), keyinfo->comment.length);
1064
keyinfo->name= strmake_root(indx.name().c_str(), indx.name().length());
1066
addKeyName(string(keyinfo->name, indx.name().length()));
1069
keys_for_keyread.reset();
1070
set_prefix(keys_in_use, keys);
1072
fields= table.field_size();
1074
setFields(fields + 1);
1075
field[fields]= NULL;
1077
uint32_t local_null_fields= 0;
1080
vector<uint32_t> field_offsets;
1081
vector<uint32_t> field_pack_length;
1083
field_offsets.resize(fields);
1084
field_pack_length.resize(fields);
1086
uint32_t interval_count= 0;
1087
uint32_t interval_parts= 0;
1089
uint32_t stored_columns_reclength= 0;
1091
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1093
message::Table::Field pfield= table.field(fieldnr);
1094
if (pfield.constraints().is_nullable())
1095
local_null_fields++;
1097
enum_field_types drizzle_field_type=
1098
proto_field_type_to_drizzle_type(pfield.type());
1100
field_offsets[fieldnr]= stored_columns_reclength;
1102
/* the below switch is very similar to
1103
CreateField::create_length_to_internal_length in field.cc
1104
(which should one day be replace by just this code)
1106
switch(drizzle_field_type)
1108
case DRIZZLE_TYPE_BLOB:
1109
case DRIZZLE_TYPE_VARCHAR:
1111
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1113
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
1114
field_options.collation_id() : 0);
1117
cs= default_charset_info;
1119
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
1120
field_options.length() * cs->mbmaxlen);
1123
case DRIZZLE_TYPE_ENUM:
1125
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1127
field_pack_length[fieldnr]=
1128
get_enum_pack_length(field_options.field_value_size());
1131
interval_parts+= field_options.field_value_size();
1134
case DRIZZLE_TYPE_DECIMAL:
1136
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1138
field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
1142
/* Zero is okay here as length is fixed for other types. */
1143
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
1146
reclength+= field_pack_length[fieldnr];
1147
stored_columns_reclength+= field_pack_length[fieldnr];
1150
/* data_offset added to stored_rec_length later */
1151
stored_rec_length= stored_columns_reclength;
1153
null_fields= local_null_fields;
1155
ulong null_bits= local_null_fields;
1156
if (! table_options.pack_record())
1158
ulong data_offset= (null_bits + 7)/8;
1161
reclength+= data_offset;
1162
stored_rec_length+= data_offset;
1164
ulong local_rec_buff_length;
1166
local_rec_buff_length= ALIGN_SIZE(reclength + 1);
1167
rec_buff_length= local_rec_buff_length;
1169
resizeDefaultValues(local_rec_buff_length);
1170
unsigned char* record= getDefaultValues();
1173
if (! table_options.pack_record())
1175
null_count++; // one bit for delete mark.
1180
intervals.resize(interval_count);
1182
/* Now fix the TYPELIBs for the intervals (enum values)
1186
uint32_t interval_nr= 0;
1188
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1190
message::Table::Field pfield= table.field(fieldnr);
1193
if (pfield.type() != message::Table::Field::ENUM)
1196
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1198
if (field_options.field_value_size() > Field_enum::max_supported_elements)
1201
snprintf(errmsg, sizeof(errmsg),
1202
_("ENUM column %s has greater than %d possible values"),
1203
pfield.name().c_str(),
1204
Field_enum::max_supported_elements);
1207
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1208
return ER_CORRUPT_TABLE_DEFINITION;
1212
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
1213
field_options.collation_id() : 0);
1216
charset= default_charset_info;
1218
TYPELIB *t= (&intervals[interval_nr]);
1220
t->type_names= (const char**)alloc_root((field_options.field_value_size() + 1) * sizeof(char*));
1222
t->type_lengths= (unsigned int*) alloc_root((field_options.field_value_size() + 1) * sizeof(unsigned int));
1224
t->type_names[field_options.field_value_size()]= NULL;
1225
t->type_lengths[field_options.field_value_size()]= 0;
1227
t->count= field_options.field_value_size();
1230
for (int n= 0; n < field_options.field_value_size(); n++)
1232
t->type_names[n]= strmake_root(field_options.field_value(n).c_str(), field_options.field_value(n).length());
1235
* Go ask the charset what the length is as for "" length=1
1236
* and there's stripping spaces or some other crack going on.
1239
lengthsp= charset->cset->lengthsp(charset,
1241
field_options.field_value(n).length());
1242
t->type_lengths[n]= lengthsp;
1248
/* and read the fields */
1251
bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
1254
use_hash= ! hash_init(&name_hash,
1255
system_charset_info,
1259
(hash_get_key) get_field_name,
1263
unsigned char* null_pos= getDefaultValues();
1264
int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1266
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1268
message::Table::Field pfield= table.field(fieldnr);
1270
Field::utype unireg_type= Field::NONE;
1272
if (pfield.has_numeric_options() &&
1273
pfield.numeric_options().is_autoincrement())
1275
unireg_type= Field::NEXT_NUMBER;
1278
if (pfield.has_options() &&
1279
pfield.options().has_default_expression() &&
1280
pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
1282
if (pfield.options().has_update_expression() &&
1283
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1285
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
1287
else if (! pfield.options().has_update_expression())
1289
unireg_type= Field::TIMESTAMP_DN_FIELD;
1292
assert(1); // Invalid update value.
1294
else if (pfield.has_options() &&
1295
pfield.options().has_update_expression() &&
1296
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1298
unireg_type= Field::TIMESTAMP_UN_FIELD;
1302
if (!pfield.has_comment())
1304
comment.str= (char*)"";
1309
size_t len= pfield.comment().length();
1310
const char* str= pfield.comment().c_str();
1312
comment.str= strmake_root(str, len);
1313
comment.length= len;
1316
enum_field_types field_type;
1318
field_type= proto_field_type_to_drizzle_type(pfield.type());
1320
const CHARSET_INFO *charset= &my_charset_bin;
1322
if (field_type == DRIZZLE_TYPE_BLOB ||
1323
field_type == DRIZZLE_TYPE_VARCHAR)
1325
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1327
charset= get_charset(field_options.has_collation_id() ?
1328
field_options.collation_id() : 0);
1331
charset= default_charset_info;
1334
if (field_type == DRIZZLE_TYPE_ENUM)
1336
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1338
charset= get_charset(field_options.has_collation_id()?
1339
field_options.collation_id() : 0);
1342
charset= default_charset_info;
1345
uint8_t decimals= 0;
1346
if (field_type == DRIZZLE_TYPE_DECIMAL
1347
|| field_type == DRIZZLE_TYPE_DOUBLE)
1349
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1351
if (! pfield.has_numeric_options() || ! fo.has_scale())
1354
We don't write the default to table proto so
1355
if no decimals specified for DOUBLE, we use the default.
1357
decimals= NOT_FIXED_DEC;
1361
if (fo.scale() > DECIMAL_MAX_SCALE)
1367
decimals= static_cast<uint8_t>(fo.scale());
1371
Item *default_value= NULL;
1373
if (pfield.options().has_default_value() ||
1374
pfield.options().default_null() ||
1375
pfield.options().has_default_bin_value())
1377
default_value= default_value_item(field_type,
1379
pfield.options().default_null(),
1380
&pfield.options().default_value(),
1381
&pfield.options().default_bin_value());
1385
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
1386
memset(&temp_table, 0, sizeof(temp_table));
1387
temp_table.setShare(this);
1388
temp_table.in_use= &session;
1389
temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
1390
temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1392
uint32_t field_length= 0; //Assignment is for compiler complaint.
1396
case DRIZZLE_TYPE_BLOB:
1397
case DRIZZLE_TYPE_VARCHAR:
1399
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1401
charset= get_charset(field_options.has_collation_id() ?
1402
field_options.collation_id() : 0);
1405
charset= default_charset_info;
1407
field_length= field_options.length() * charset->mbmaxlen;
1410
case DRIZZLE_TYPE_DOUBLE:
1412
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1413
if (!fo.has_precision() && !fo.has_scale())
1415
field_length= DBL_DIG+7;
1419
field_length= fo.precision();
1421
if (field_length < decimals &&
1422
decimals != NOT_FIXED_DEC)
1424
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1431
case DRIZZLE_TYPE_DECIMAL:
1433
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1435
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1439
case DRIZZLE_TYPE_TIMESTAMP:
1440
case DRIZZLE_TYPE_DATETIME:
1441
field_length= DateTime::MAX_STRING_LENGTH;
1443
case DRIZZLE_TYPE_DATE:
1444
field_length= Date::MAX_STRING_LENGTH;
1446
case DRIZZLE_TYPE_ENUM:
1450
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1452
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1454
if (fo.field_value(valnr).length() > field_length)
1456
field_length= charset->cset->numchars(charset,
1457
fo.field_value(valnr).c_str(),
1458
fo.field_value(valnr).c_str()
1459
+ fo.field_value(valnr).length())
1460
* charset->mbmaxlen;
1465
case DRIZZLE_TYPE_LONG:
1467
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1468
field_length= MAX_INT_WIDTH+sign_len;
1471
case DRIZZLE_TYPE_LONGLONG:
1472
field_length= MAX_BIGINT_WIDTH;
1474
case DRIZZLE_TYPE_NULL:
1475
abort(); // Programming error
1478
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1480
pfield.constraints().is_nullable(),
1486
(Field::utype) MTYP_TYPENR(unireg_type),
1487
((field_type == DRIZZLE_TYPE_ENUM) ?
1488
&intervals[interval_nr++]
1490
getTableProto()->field(fieldnr).name().c_str());
1494
f->init(&temp_table); /* blob default values need table obj */
1496
if (! (f->flags & NOT_NULL_FLAG))
1498
*f->null_ptr|= f->null_bit;
1499
if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
1506
enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1507
session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1508
int res= default_value->save_in_field(f, 1);
1509
session.count_cuted_fields= old_count_cuted_fields;
1510
if (res != 0 && res != 3) /* @TODO Huh? */
1512
my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1518
else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1519
(f->flags & NOT_NULL_FLAG))
1522
f->store((int64_t) 1, true);
1529
/* hack to undo f->init() */
1531
f->orig_table= NULL;
1533
f->field_index= fieldnr;
1534
f->comment= comment;
1535
if (! default_value &&
1536
! (f->unireg_check==Field::NEXT_NUMBER) &&
1537
(f->flags & NOT_NULL_FLAG) &&
1538
(f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1540
f->flags|= NO_DEFAULT_VALUE_FLAG;
1543
if (f->unireg_check == Field::NEXT_NUMBER)
1544
found_next_number_field= &(field[fieldnr]);
1546
if (timestamp_field == f)
1547
timestamp_field_offset= fieldnr;
1549
if (use_hash) /* supposedly this never fails... but comments lie */
1550
(void) my_hash_insert(&name_hash,
1551
(unsigned char*)&(field[fieldnr]));
1556
for (unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1558
key_part= keyinfo->key_part;
1560
for (unsigned int partnr= 0;
1561
partnr < keyinfo->key_parts;
1562
partnr++, key_part++)
1565
* Fix up key_part->offset by adding data_offset.
1566
* We really should compute offset as well.
1567
* But at least this way we are a little better.
1569
key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1574
We need to set the unused bits to 1. If the number of bits is a multiple
1575
of 8 there are no unused bits.
1579
*(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1581
null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1583
last_null_bit_pos= null_bit_pos;
1588
uint32_t local_primary_key= 0;
1589
doesKeyNameExist("PRIMARY", local_primary_key);
1592
key_part= keyinfo->key_part;
1594
for (uint32_t key= 0; key < keys; key++,keyinfo++)
1596
uint32_t usable_parts= 0;
1598
if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1601
If the UNIQUE key doesn't have NULL columns and is not a part key
1602
declare this as a primary key.
1604
local_primary_key=key;
1605
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1607
uint32_t fieldnr= key_part[i].fieldnr;
1609
field[fieldnr-1]->null_ptr ||
1610
field[fieldnr-1]->key_length() != key_part[i].length)
1612
local_primary_key= MAX_KEY; // Can't be used
1618
for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1621
if (! key_part->fieldnr)
1625
local_field= key_part->field= field[key_part->fieldnr-1];
1626
key_part->type= local_field->key_type();
1627
if (local_field->null_ptr)
1629
key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - getDefaultValues());
1630
key_part->null_bit= local_field->null_bit;
1631
key_part->store_length+=HA_KEY_NULL_LENGTH;
1632
keyinfo->flags|=HA_NULL_PART_KEY;
1633
keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1634
keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1636
if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1637
local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1639
if (local_field->type() == DRIZZLE_TYPE_BLOB)
1640
key_part->key_part_flag|= HA_BLOB_PART;
1642
key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1643
keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1644
key_part->store_length+=HA_KEY_BLOB_LENGTH;
1645
keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1647
if (i == 0 && key != local_primary_key)
1648
local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1649
(keyinfo->key_parts == 1)) ?
1650
UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1652
local_field->key_start.set(key);
1653
if (local_field->key_length() == key_part->length &&
1654
!(local_field->flags & BLOB_FLAG))
1656
enum ha_key_alg algo= key_info[key].algorithm;
1657
if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1659
keys_for_keyread.set(key);
1660
local_field->part_of_key.set(key);
1661
local_field->part_of_key_not_clustered.set(key);
1663
if (db_type()->index_flags(algo) & HA_READ_ORDER)
1664
local_field->part_of_sortkey.set(key);
1666
if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1668
usable_parts++; // For FILESORT
1669
local_field->flags|= PART_KEY_FLAG;
1670
if (key == local_primary_key)
1672
local_field->flags|= PRI_KEY_FLAG;
1674
If this field is part of the primary key and all keys contains
1675
the primary key, then we can use any key to find this column
1677
if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1679
local_field->part_of_key= keys_in_use;
1680
if (local_field->part_of_sortkey.test(key))
1681
local_field->part_of_sortkey= keys_in_use;
1684
if (local_field->key_length() != key_part->length)
1686
key_part->key_part_flag|= HA_PART_KEY_SEG;
1689
keyinfo->usable_key_parts= usable_parts; // Filesort
1691
set_if_bigger(max_key_length,keyinfo->key_length+
1692
keyinfo->key_parts);
1693
total_key_length+= keyinfo->key_length;
1695
if (keyinfo->flags & HA_NOSAME)
1697
set_if_bigger(max_unique_length,keyinfo->key_length);
1700
if (local_primary_key < MAX_KEY &&
1701
(keys_in_use.test(local_primary_key)))
1703
primary_key= local_primary_key;
1705
If we are using an integer as the primary key then allow the user to
1706
refer to it as '_rowid'
1708
if (key_info[local_primary_key].key_parts == 1)
1710
Field *local_field= key_info[local_primary_key].key_part[0].field;
1711
if (local_field && local_field->result_type() == INT_RESULT)
1713
/* note that fieldnr here (and rowid_field_offset) starts from 1 */
1714
rowid_field_offset= (key_info[local_primary_key].key_part[0].
1721
if (found_next_number_field)
1723
Field *reg_field= *found_next_number_field;
1724
if ((int) (next_number_index= (uint32_t)
1725
find_ref_key(key_info, keys,
1726
getDefaultValues(), reg_field,
1727
&next_number_key_offset,
1728
&next_number_keypart)) < 0)
1730
/* Wrong field definition */
1737
reg_field->flags |= AUTO_INCREMENT_FLAG;
1745
/* Store offsets to blob fields to find them fast */
1746
blob_field.resize(blob_fields);
1747
save= &blob_field[0];
1749
for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1751
if ((*iter)->flags & BLOB_FLAG)
1756
db_low_byte_first= true; // @todo Question this.
1757
column_bitmap_size= bitmap_buffer_size(fields);
1759
all_bitmap.resize(column_bitmap_size);
1760
all_set.init(&all_bitmap[0], fields);
1766
int TableShare::parse_table_proto(Session& session, message::Table &table)
1768
int local_error= inner_parse_table_proto(session, table);
1770
if (not local_error)
1776
hash_free(&name_hash);
1777
open_table_error(local_error, open_errno, 0);
1784
Read table definition from a binary / text based .frm cursor
1788
session Thread Cursor
1789
share Fill this with table definition
1792
This function is called when the table definition is not cached in
1794
The data is returned in 'share', which is alloced by
1795
alloc_table_share().. The code assumes that share is initialized.
1799
1 Error (see open_table_error)
1800
2 Error (see open_table_error)
1801
3 Wrong data in .frm cursor
1802
4 Error (see open_table_error)
1803
5 Error (see open_table_error: charset unavailable)
1804
6 Unknown .frm version
1807
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1815
message::Table table;
1817
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1819
if (local_error != EEXIST)
1821
if (local_error > 0)
1828
if (not table.IsInitialized())
1836
local_error= parse_table_proto(session, table);
1838
setTableCategory(TABLE_CATEGORY_USER);
1841
if (local_error && !error_given)
1844
open_table_error(error, (open_errno= errno), 0);
1852
Open a table based on a TableShare
1855
open_table_from_share()
1856
session Thread Cursor
1857
share Table definition
1858
alias Alias for table
1859
db_stat open flags (for example HA_OPEN_KEYFILE|
1860
HA_OPEN_RNDFILE..) can be 0 (example in
1862
ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1863
outparam result table
1867
1 Error (see open_table_error)
1868
2 Error (see open_table_error)
1869
3 Wrong data in .frm cursor
1870
4 Error (see open_table_error)
1871
5 Error (see open_table_error: charset unavailable)
1872
7 Table definition has changed in engine
1875
int TableShare::open_table_from_share(Session *session,
1876
const TableIdentifier &identifier,
1878
uint32_t db_stat, uint32_t ha_open_flags,
1882
uint32_t records, bitmap_size;
1883
bool error_reported= false;
1884
unsigned char *record, *bitmaps;
1887
/* Parsing of partitioning information from .frm needs session->lex set up. */
1888
assert(session->lex->is_lex_started);
1891
outparam.resetTable(session, this, db_stat);
1894
if (not (outparam.alias= strdup(alias)))
1897
/* Allocate Cursor */
1898
if (not (outparam.cursor= db_type()->getCursor(*this, outparam.getMemRoot())))
1903
if ((db_stat & HA_OPEN_KEYFILE))
1908
if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1913
/* We are probably in hard repair, and the buffers should not be used */
1914
outparam.record[0]= outparam.record[1]= getDefaultValues();
1918
outparam.record[0]= record;
1920
outparam.record[1]= record+ rec_buff_length;
1922
outparam.record[1]= outparam.record[0]; // Safety
1927
We need this because when we read var-length rows, we are not updating
1928
bytes after end of varchar
1932
memcpy(outparam.record[0], getDefaultValues(), rec_buff_length);
1933
memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1935
memcpy(outparam.record[1], getDefaultValues(), rec_buff_length);
1940
memcpy(outparam.record[1], getDefaultValues(), null_bytes);
1943
if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1948
outparam.setFields(field_ptr);
1950
record= (unsigned char*) outparam.record[0]-1; /* Fieldstart = 1 */
1952
outparam.null_flags= (unsigned char*) record+1;
1954
/* Setup copy of fields from share, but use the right alias and record */
1955
for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1957
if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1960
(*field_ptr)= 0; // End marker
1962
if (found_next_number_field)
1963
outparam.found_next_number_field=
1964
outparam.getField(positionFields(found_next_number_field));
1965
if (timestamp_field)
1966
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1969
/* Fix key->name and key_part->field */
1972
KeyInfo *local_key_info, *key_info_end;
1973
KeyPartInfo *key_part;
1975
n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1976
if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1978
outparam.key_info= local_key_info;
1979
key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1981
memcpy(local_key_info, key_info, sizeof(*local_key_info)*keys);
1982
memcpy(key_part, key_info[0].key_part, (sizeof(*key_part) *
1985
for (key_info_end= local_key_info + keys ;
1986
local_key_info < key_info_end ;
1989
KeyPartInfo *key_part_end;
1991
local_key_info->table= &outparam;
1992
local_key_info->key_part= key_part;
1994
for (key_part_end= key_part+ local_key_info->key_parts ;
1995
key_part < key_part_end ;
1998
Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
2000
if (local_field->key_length() != key_part->length &&
2001
!(local_field->flags & BLOB_FLAG))
2004
We are using only a prefix of the column as a key:
2005
Create a new field for the key part that matches the index
2007
local_field= key_part->field= local_field->new_field(outparam.getMemRoot(), &outparam, 0);
2008
local_field->field_length= key_part->length;
2014
/* Allocate bitmaps */
2016
bitmap_size= column_bitmap_size;
2017
if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
2021
outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
2022
outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
2023
outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
2024
outparam.default_column_bitmaps();
2026
/* The table struct is now initialized; Open the table */
2030
assert(!(db_stat & HA_WAIT_IF_LOCKED));
2033
if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam, getNormalizedPath(),
2034
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
2035
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
2039
case HA_ERR_NO_SUCH_TABLE:
2041
The table did not exists in storage engine, use same error message
2042
as if the .frm cursor didn't exist
2049
Too many files opened, use same error message as if the .frm
2056
outparam.print_error(ha_err, MYF(0));
2057
error_reported= true;
2058
if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
2066
#if defined(HAVE_purify)
2067
memset(bitmaps, 0, bitmap_size*3);
2073
if (!error_reported)
2074
open_table_error(local_error, errno, 0);
2076
delete outparam.cursor;
2077
outparam.cursor= 0; // For easier error checking
2078
outparam.db_stat= 0;
2079
outparam.getMemRoot()->free_root(MYF(0)); // Safe to call on zeroed root
2080
free((char*) outparam.alias);
2081
return (local_error);
2084
/* error message when opening a form cursor */
2085
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
2088
char buff[FN_REFLEN];
2089
myf errortype= ME_ERROR+ME_WAITTANG;
2091
switch (pass_error) {
2094
if (db_errno == ENOENT)
2096
my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
2100
snprintf(buff, sizeof(buff), "%s",normalized_path.str);
2101
my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
2102
errortype, buff, db_errno);
2107
err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2108
ER_FILE_USED : ER_CANT_OPEN_FILE;
2109
my_error(err_no, errortype, normalized_path.str, db_errno);
2114
const char *csname= get_charset_name((uint32_t) pass_errarg);
2116
if (!csname || csname[0] =='?')
2118
snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
2121
my_printf_error(ER_UNKNOWN_COLLATION,
2122
_("Unknown collation '%s' in table '%-.64s' definition"),
2123
MYF(0), csname, table_name.str);
2127
snprintf(buff, sizeof(buff), "%s", normalized_path.str);
2128
my_printf_error(ER_NOT_FORM_FILE,
2129
_("Table '%-.64s' was created with a different version "
2130
"of Drizzle and cannot be read"),
2135
default: /* Better wrong error than none */
2137
snprintf(buff, sizeof(buff), "%s", normalized_path.str);
2138
my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
2142
} /* open_table_error */
2144
Field *TableShare::make_field(unsigned char *ptr,
2145
uint32_t field_length,
2147
unsigned char *null_pos,
2148
unsigned char null_bit,
2150
enum_field_types field_type,
2151
const CHARSET_INFO * field_charset,
2152
Field::utype unireg_check,
2154
const char *field_name)
2163
null_bit= ((unsigned char) 1) << null_bit;
2168
case DRIZZLE_TYPE_DATE:
2169
case DRIZZLE_TYPE_DATETIME:
2170
case DRIZZLE_TYPE_TIMESTAMP:
2171
field_charset= &my_charset_bin;
2177
case DRIZZLE_TYPE_ENUM:
2178
return new (&mem_root) Field_enum(ptr,
2183
get_enum_pack_length(interval->count),
2186
case DRIZZLE_TYPE_VARCHAR:
2187
return new (&mem_root) Field_varstring(ptr,field_length,
2188
HA_VARCHAR_PACKLENGTH(field_length),
2193
case DRIZZLE_TYPE_BLOB:
2194
return new (&mem_root) Field_blob(ptr,
2199
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
2201
case DRIZZLE_TYPE_DECIMAL:
2202
return new (&mem_root) Field_decimal(ptr,
2210
false /* is_unsigned */);
2211
case DRIZZLE_TYPE_DOUBLE:
2212
return new (&mem_root) Field_double(ptr,
2220
false /* is_unsigned */);
2221
case DRIZZLE_TYPE_LONG:
2222
return new (&mem_root) Field_long(ptr,
2229
false /* is_unsigned */);
2230
case DRIZZLE_TYPE_LONGLONG:
2231
return new (&mem_root) Field_int64_t(ptr,
2238
false /* is_unsigned */);
2239
case DRIZZLE_TYPE_TIMESTAMP:
2240
return new (&mem_root) Field_timestamp(ptr,
2248
case DRIZZLE_TYPE_DATE:
2249
return new (&mem_root) Field_date(ptr,
2254
case DRIZZLE_TYPE_DATETIME:
2255
return new (&mem_root) Field_datetime(ptr,
2260
case DRIZZLE_TYPE_NULL:
2261
return new (&mem_root) Field_null(ptr,
2265
default: // Impossible (Wrong version)
308
2272
} /* namespace drizzled */