303
TableDefinitionCache &TableShare::getCache()
431
const TableDefinitionCache &TableShare::getCache()
305
433
return table_def_cache;
436
TableShare::TableShare(TableIdentifier::Type type_arg) :
437
table_category(TABLE_UNKNOWN_CATEGORY),
439
found_next_number_field(NULL),
440
timestamp_field(NULL),
442
mem_root(TABLE_ALLOC_BLOCK_SIZE),
447
stored_rec_length(0),
450
storage_engine(NULL),
454
last_null_bit_pos(0),
460
max_unique_length(0),
465
timestamp_field_offset(0),
467
db_create_options(0),
468
db_options_in_use(0),
470
rowid_field_offset(0),
471
primary_key(MAX_KEY),
472
next_number_index(0),
473
next_number_key_offset(0),
474
next_number_keypart(0),
478
column_bitmap_size(0),
480
db_low_byte_first(false),
482
replace_with_name_lock(false),
483
waiting_on_cond(false),
486
event_observers(NULL)
490
memset(&db, 0, sizeof(LEX_STRING));
491
memset(&table_name, 0, sizeof(LEX_STRING));
492
memset(&path, 0, sizeof(LEX_STRING));
493
memset(&normalized_path, 0, sizeof(LEX_STRING));
495
if (type_arg == message::Table::INTERNAL)
497
TableIdentifier::build_tmptable_filename(private_key_for_cache);
498
init(&private_key_for_cache[0], &private_key_for_cache[0]);
506
TableShare::TableShare(TableIdentifier &identifier, const TableIdentifier::Key &key) :// Used by placeholder
507
table_category(TABLE_UNKNOWN_CATEGORY),
509
found_next_number_field(NULL),
510
timestamp_field(NULL),
512
mem_root(TABLE_ALLOC_BLOCK_SIZE),
517
stored_rec_length(0),
520
storage_engine(NULL),
521
tmp_table(message::Table::INTERNAL),
524
last_null_bit_pos(0),
530
max_unique_length(0),
535
timestamp_field_offset(0),
537
db_create_options(0),
538
db_options_in_use(0),
540
rowid_field_offset(0),
541
primary_key(MAX_KEY),
542
next_number_index(0),
543
next_number_key_offset(0),
544
next_number_keypart(0),
548
column_bitmap_size(0),
550
db_low_byte_first(false),
552
replace_with_name_lock(false),
553
waiting_on_cond(false),
556
event_observers(NULL)
558
assert(identifier.getKey() == key);
561
memset(&path, 0, sizeof(LEX_STRING));
562
memset(&normalized_path, 0, sizeof(LEX_STRING));
564
private_key_for_cache= key;
566
table_category= TABLE_CATEGORY_TEMPORARY;
567
tmp_table= message::Table::INTERNAL;
569
db.str= &private_key_for_cache[0];
570
db.length= strlen(&private_key_for_cache[0]);
572
table_name.str= &private_key_for_cache[0] + strlen(&private_key_for_cache[0]) + 1;
573
table_name.length= strlen(table_name.str);
574
path.str= (char *)"";
575
normalized_path.str= path.str;
576
path.length= normalized_path.length= 0;
577
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
578
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
582
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
583
table_category(TABLE_UNKNOWN_CATEGORY),
585
found_next_number_field(NULL),
586
timestamp_field(NULL),
588
mem_root(TABLE_ALLOC_BLOCK_SIZE),
593
stored_rec_length(0),
596
storage_engine(NULL),
597
tmp_table(identifier.getType()),
600
last_null_bit_pos(0),
606
max_unique_length(0),
611
timestamp_field_offset(0),
613
db_create_options(0),
614
db_options_in_use(0),
616
rowid_field_offset(0),
617
primary_key(MAX_KEY),
618
next_number_index(0),
619
next_number_key_offset(0),
620
next_number_keypart(0),
624
column_bitmap_size(0),
626
db_low_byte_first(false),
628
replace_with_name_lock(false),
629
waiting_on_cond(false),
632
event_observers(NULL)
635
memset(&db, 0, sizeof(LEX_STRING));
636
memset(&table_name, 0, sizeof(LEX_STRING));
637
memset(&path, 0, sizeof(LEX_STRING));
638
memset(&normalized_path, 0, sizeof(LEX_STRING));
640
private_key_for_cache= identifier.getKey();
641
assert(identifier.getPath().size()); // Since we are doing a create table, this should be a positive value
642
private_normalized_path.resize(identifier.getPath().size() + 1);
643
memcpy(&private_normalized_path[0], identifier.getPath().c_str(), identifier.getPath().size());
646
table_category= TABLE_CATEGORY_TEMPORARY;
647
tmp_table= message::Table::INTERNAL;
648
db.str= &private_key_for_cache[0];
649
db.length= strlen(&private_key_for_cache[0]);
650
table_name.str= db.str + 1;
651
table_name.length= strlen(table_name.str);
652
path.str= &private_normalized_path[0];
653
normalized_path.str= path.str;
654
path.length= normalized_path.length= private_normalized_path.size();
660
Used for shares that will go into the cache.
662
TableShare::TableShare(TableIdentifier::Type type_arg,
663
TableIdentifier &identifier,
665
uint32_t path_length_arg) :
666
table_category(TABLE_UNKNOWN_CATEGORY),
668
found_next_number_field(NULL),
669
timestamp_field(NULL),
671
mem_root(TABLE_ALLOC_BLOCK_SIZE),
676
stored_rec_length(0),
679
storage_engine(NULL),
683
last_null_bit_pos(0),
689
max_unique_length(0),
694
timestamp_field_offset(0),
696
db_create_options(0),
697
db_options_in_use(0),
699
rowid_field_offset(0),
700
primary_key(MAX_KEY),
701
next_number_index(0),
702
next_number_key_offset(0),
703
next_number_keypart(0),
707
column_bitmap_size(0),
709
db_low_byte_first(false),
711
replace_with_name_lock(false),
712
waiting_on_cond(false),
715
event_observers(NULL)
718
memset(&db, 0, sizeof(LEX_STRING));
719
memset(&table_name, 0, sizeof(LEX_STRING));
720
memset(&path, 0, sizeof(LEX_STRING));
721
memset(&normalized_path, 0, sizeof(LEX_STRING));
726
private_key_for_cache= identifier.getKey();
728
Let us use the fact that the key is "db/0/table_name/0" + optional
729
part for temporary tables.
731
db.str= &private_key_for_cache[0];
732
db.length= strlen(db.str);
733
table_name.str= db.str + db.length + 1;
734
table_name.length= strlen(table_name.str);
738
_path.append(path_arg, path_length_arg);
742
TableIdentifier::build_table_filename(_path, db.str, table_name.str, false);
745
if (mem_root.multi_alloc_root(0,
746
&path_buff, _path.length() + 1,
749
setPath(path_buff, _path.length());
750
strcpy(path_buff, _path.c_str());
751
setNormalizedPath(path_buff, _path.length());
753
version= refresh_version;
755
pthread_mutex_init(&mutex, MY_MUTEX_INIT_FAST);
756
pthread_cond_init(&cond, NULL);
760
assert(0); // We should throw here.
764
void TableShare::init(const char *new_table_name,
765
const char *new_path)
768
table_category= TABLE_CATEGORY_TEMPORARY;
769
tmp_table= message::Table::INTERNAL;
772
table_name.str= (char*) new_table_name;
773
table_name.length= strlen(new_table_name);
774
path.str= (char*) new_path;
775
normalized_path.str= (char*) new_path;
776
path.length= normalized_path.length= strlen(new_path);
779
TableShare::~TableShare()
781
assert(ref_count == 0);
784
If someone is waiting for this to be deleted, inform it about this.
785
Don't do a delete until we know that no one is refering to this anymore.
787
if (tmp_table == message::Table::STANDARD)
789
/* share->mutex is locked in release_table_share() */
790
while (waiting_on_cond)
792
pthread_cond_broadcast(&cond);
793
pthread_cond_wait(&cond, &mutex);
795
/* No thread refers to this anymore */
796
pthread_mutex_unlock(&mutex);
797
pthread_mutex_destroy(&mutex);
798
pthread_cond_destroy(&cond);
801
storage_engine= NULL;
806
mem_root.free_root(MYF(0)); // Free's share
809
void TableShare::setIdentifier(TableIdentifier &identifier_arg)
811
private_key_for_cache.clear();
812
private_key_for_cache= identifier_arg.getKey();
815
Let us use the fact that the key is "db/0/table_name/0" + optional
816
part for temporary tables.
818
db.str= &private_key_for_cache[0];
819
db.length= strlen(db.str);
820
table_name.str= db.str + db.length + 1;
821
table_name.length= strlen(table_name.str);
823
table_proto->set_name(identifier_arg.getTableName());
824
table_proto->set_schema(identifier_arg.getSchemaName());
827
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
831
if (! table.IsInitialized())
833
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
834
return ER_CORRUPT_TABLE_DEFINITION;
837
setTableProto(new(nothrow) message::Table(table));
839
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
840
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
842
message::Table::TableOptions table_options;
844
if (table.has_options())
845
table_options= table.options();
847
uint32_t local_db_create_options= 0;
849
if (table_options.pack_record())
850
local_db_create_options|= HA_OPTION_PACK_RECORD;
852
/* local_db_create_options was stored as 2 bytes in FRM
853
Any HA_OPTION_ that doesn't fit into 2 bytes was silently truncated away.
855
db_create_options= (local_db_create_options & 0x0000FFFF);
856
db_options_in_use= db_create_options;
858
block_size= table_options.has_block_size() ?
859
table_options.block_size() : 0;
861
table_charset= get_charset(table_options.collation_id());
866
snprintf(errmsg, sizeof(errmsg),
867
_("Table %s has invalid/unknown collation: %d,%s"),
869
table_options.collation_id(),
870
table_options.collation().c_str());
873
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
874
return ER_CORRUPT_TABLE_DEFINITION;
879
blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
881
keys= table.indexes_size();
884
for (int indx= 0; indx < table.indexes_size(); indx++)
885
key_parts+= table.indexes(indx).index_part_size();
887
key_info= (KeyInfo*) alloc_root( table.indexes_size() * sizeof(KeyInfo) +key_parts*sizeof(KeyPartInfo));
889
KeyPartInfo *key_part;
891
key_part= reinterpret_cast<KeyPartInfo*>
892
(key_info+table.indexes_size());
895
ulong *rec_per_key= (ulong*) alloc_root(sizeof(ulong*)*key_parts);
897
KeyInfo* keyinfo= key_info;
898
for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
900
message::Table::Index indx= table.indexes(keynr);
905
if (indx.is_unique())
906
keyinfo->flags|= HA_NOSAME;
908
if (indx.has_options())
910
message::Table::Index::Options indx_options= indx.options();
911
if (indx_options.pack_key())
912
keyinfo->flags|= HA_PACK_KEY;
914
if (indx_options.var_length_key())
915
keyinfo->flags|= HA_VAR_LENGTH_PART;
917
if (indx_options.null_part_key())
918
keyinfo->flags|= HA_NULL_PART_KEY;
920
if (indx_options.binary_pack_key())
921
keyinfo->flags|= HA_BINARY_PACK_KEY;
923
if (indx_options.has_partial_segments())
924
keyinfo->flags|= HA_KEY_HAS_PART_KEY_SEG;
926
if (indx_options.auto_generated_key())
927
keyinfo->flags|= HA_GENERATED_KEY;
929
if (indx_options.has_key_block_size())
931
keyinfo->flags|= HA_USES_BLOCK_SIZE;
932
keyinfo->block_size= indx_options.key_block_size();
936
keyinfo->block_size= 0;
942
case message::Table::Index::UNKNOWN_INDEX:
943
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
945
case message::Table::Index::BTREE:
946
keyinfo->algorithm= HA_KEY_ALG_BTREE;
948
case message::Table::Index::HASH:
949
keyinfo->algorithm= HA_KEY_ALG_HASH;
953
/* TODO: suitable warning ? */
954
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
958
keyinfo->key_length= indx.key_length();
960
keyinfo->key_parts= indx.index_part_size();
962
keyinfo->key_part= key_part;
963
keyinfo->rec_per_key= rec_per_key;
965
for (unsigned int partnr= 0;
966
partnr < keyinfo->key_parts;
967
partnr++, key_part++)
969
message::Table::Index::IndexPart part;
970
part= indx.index_part(partnr);
974
key_part->field= NULL;
975
key_part->fieldnr= part.fieldnr() + 1; // start from 1.
976
key_part->null_bit= 0;
977
/* key_part->null_offset is only set if null_bit (see later) */
978
/* key_part->key_type= */ /* I *THINK* this may be okay.... */
979
/* key_part->type ???? */
980
key_part->key_part_flag= 0;
981
if (part.has_in_reverse_order())
982
key_part->key_part_flag= part.in_reverse_order()? HA_REVERSE_SORT : 0;
984
key_part->length= part.compare_length();
988
if (table.field(part.fieldnr()).type() == message::Table::Field::VARCHAR
989
|| table.field(part.fieldnr()).type() == message::Table::Field::BLOB)
991
uint32_t collation_id;
993
if (table.field(part.fieldnr()).string_options().has_collation_id())
994
collation_id= table.field(part.fieldnr()).string_options().collation_id();
996
collation_id= table.options().collation_id();
998
const CHARSET_INFO *cs= get_charset(collation_id);
1000
mbmaxlen= cs->mbmaxlen;
1002
key_part->length*= mbmaxlen;
1004
key_part->store_length= key_part->length;
1006
/* key_part->offset is set later */
1007
key_part->key_type= 0;
1010
if (! indx.has_comment())
1012
keyinfo->comment.length= 0;
1013
keyinfo->comment.str= NULL;
1017
keyinfo->flags|= HA_USES_COMMENT;
1018
keyinfo->comment.length= indx.comment().length();
1019
keyinfo->comment.str= strmake_root(indx.comment().c_str(), keyinfo->comment.length);
1022
keyinfo->name= strmake_root(indx.name().c_str(), indx.name().length());
1024
addKeyName(string(keyinfo->name, indx.name().length()));
1027
keys_for_keyread.reset();
1028
set_prefix(keys_in_use, keys);
1030
fields= table.field_size();
1032
setFields(fields + 1);
1033
field[fields]= NULL;
1035
uint32_t local_null_fields= 0;
1038
vector<uint32_t> field_offsets;
1039
vector<uint32_t> field_pack_length;
1041
field_offsets.resize(fields);
1042
field_pack_length.resize(fields);
1044
uint32_t interval_count= 0;
1045
uint32_t interval_parts= 0;
1047
uint32_t stored_columns_reclength= 0;
1049
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1051
message::Table::Field pfield= table.field(fieldnr);
1052
if (pfield.constraints().is_nullable())
1053
local_null_fields++;
1055
enum_field_types drizzle_field_type=
1056
proto_field_type_to_drizzle_type(pfield.type());
1058
field_offsets[fieldnr]= stored_columns_reclength;
1060
/* the below switch is very similar to
1061
CreateField::create_length_to_internal_length in field.cc
1062
(which should one day be replace by just this code)
1064
switch(drizzle_field_type)
1066
case DRIZZLE_TYPE_BLOB:
1067
case DRIZZLE_TYPE_VARCHAR:
1069
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1071
const CHARSET_INFO *cs= get_charset(field_options.has_collation_id() ?
1072
field_options.collation_id() : 0);
1075
cs= default_charset_info;
1077
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type,
1078
field_options.length() * cs->mbmaxlen);
1081
case DRIZZLE_TYPE_ENUM:
1083
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1085
field_pack_length[fieldnr]=
1086
get_enum_pack_length(field_options.field_value_size());
1089
interval_parts+= field_options.field_value_size();
1092
case DRIZZLE_TYPE_DECIMAL:
1094
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1096
field_pack_length[fieldnr]= my_decimal_get_binary_size(fo.precision(), fo.scale());
1100
/* Zero is okay here as length is fixed for other types. */
1101
field_pack_length[fieldnr]= calc_pack_length(drizzle_field_type, 0);
1104
reclength+= field_pack_length[fieldnr];
1105
stored_columns_reclength+= field_pack_length[fieldnr];
1108
/* data_offset added to stored_rec_length later */
1109
stored_rec_length= stored_columns_reclength;
1111
null_fields= local_null_fields;
1113
ulong null_bits= local_null_fields;
1114
if (! table_options.pack_record())
1116
ulong data_offset= (null_bits + 7)/8;
1119
reclength+= data_offset;
1120
stored_rec_length+= data_offset;
1122
ulong local_rec_buff_length;
1124
local_rec_buff_length= ALIGN_SIZE(reclength + 1);
1125
rec_buff_length= local_rec_buff_length;
1127
resizeDefaultValues(local_rec_buff_length);
1128
unsigned char* record= getDefaultValues();
1131
if (! table_options.pack_record())
1133
null_count++; // one bit for delete mark.
1138
intervals.resize(interval_count);
1140
/* Now fix the TYPELIBs for the intervals (enum values)
1144
uint32_t interval_nr= 0;
1146
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1148
message::Table::Field pfield= table.field(fieldnr);
1151
if (pfield.type() != message::Table::Field::ENUM)
1154
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1156
if (field_options.field_value_size() > Field_enum::max_supported_elements)
1159
snprintf(errmsg, sizeof(errmsg),
1160
_("ENUM column %s has greater than %d possible values"),
1161
pfield.name().c_str(),
1162
Field_enum::max_supported_elements);
1165
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
1166
return ER_CORRUPT_TABLE_DEFINITION;
1170
const CHARSET_INFO *charset= get_charset(field_options.has_collation_id() ?
1171
field_options.collation_id() : 0);
1174
charset= default_charset_info;
1176
TYPELIB *t= (&intervals[interval_nr]);
1178
t->type_names= (const char**)alloc_root((field_options.field_value_size() + 1) * sizeof(char*));
1180
t->type_lengths= (unsigned int*) alloc_root((field_options.field_value_size() + 1) * sizeof(unsigned int));
1182
t->type_names[field_options.field_value_size()]= NULL;
1183
t->type_lengths[field_options.field_value_size()]= 0;
1185
t->count= field_options.field_value_size();
1188
for (int n= 0; n < field_options.field_value_size(); n++)
1190
t->type_names[n]= strmake_root(field_options.field_value(n).c_str(), field_options.field_value(n).length());
1193
* Go ask the charset what the length is as for "" length=1
1194
* and there's stripping spaces or some other crack going on.
1197
lengthsp= charset->cset->lengthsp(charset,
1199
field_options.field_value(n).length());
1200
t->type_lengths[n]= lengthsp;
1206
/* and read the fields */
1209
bool use_hash= fields >= MAX_FIELDS_BEFORE_HASH;
1211
unsigned char* null_pos= getDefaultValues();
1212
int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
1214
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
1216
message::Table::Field pfield= table.field(fieldnr);
1218
Field::utype unireg_type= Field::NONE;
1220
if (pfield.has_numeric_options() &&
1221
pfield.numeric_options().is_autoincrement())
1223
unireg_type= Field::NEXT_NUMBER;
1226
if (pfield.has_options() &&
1227
pfield.options().has_default_expression() &&
1228
pfield.options().default_expression().compare("CURRENT_TIMESTAMP") == 0)
1230
if (pfield.options().has_update_expression() &&
1231
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1233
unireg_type= Field::TIMESTAMP_DNUN_FIELD;
1235
else if (! pfield.options().has_update_expression())
1237
unireg_type= Field::TIMESTAMP_DN_FIELD;
1240
assert(1); // Invalid update value.
1242
else if (pfield.has_options() &&
1243
pfield.options().has_update_expression() &&
1244
pfield.options().update_expression().compare("CURRENT_TIMESTAMP") == 0)
1246
unireg_type= Field::TIMESTAMP_UN_FIELD;
1250
if (!pfield.has_comment())
1252
comment.str= (char*)"";
1257
size_t len= pfield.comment().length();
1258
const char* str= pfield.comment().c_str();
1260
comment.str= strmake_root(str, len);
1261
comment.length= len;
1264
enum_field_types field_type;
1266
field_type= proto_field_type_to_drizzle_type(pfield.type());
1268
const CHARSET_INFO *charset= &my_charset_bin;
1270
if (field_type == DRIZZLE_TYPE_BLOB ||
1271
field_type == DRIZZLE_TYPE_VARCHAR)
1273
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1275
charset= get_charset(field_options.has_collation_id() ?
1276
field_options.collation_id() : 0);
1279
charset= default_charset_info;
1282
if (field_type == DRIZZLE_TYPE_ENUM)
1284
message::Table::Field::EnumerationValues field_options= pfield.enumeration_values();
1286
charset= get_charset(field_options.has_collation_id()?
1287
field_options.collation_id() : 0);
1290
charset= default_charset_info;
1293
uint8_t decimals= 0;
1294
if (field_type == DRIZZLE_TYPE_DECIMAL
1295
|| field_type == DRIZZLE_TYPE_DOUBLE)
1297
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1299
if (! pfield.has_numeric_options() || ! fo.has_scale())
1302
We don't write the default to table proto so
1303
if no decimals specified for DOUBLE, we use the default.
1305
decimals= NOT_FIXED_DEC;
1309
if (fo.scale() > DECIMAL_MAX_SCALE)
1315
decimals= static_cast<uint8_t>(fo.scale());
1319
Item *default_value= NULL;
1321
if (pfield.options().has_default_value() ||
1322
pfield.options().default_null() ||
1323
pfield.options().has_default_bin_value())
1325
default_value= default_value_item(field_type,
1327
pfield.options().default_null(),
1328
&pfield.options().default_value(),
1329
&pfield.options().default_bin_value());
1333
Table temp_table; /* Use this so that BLOB DEFAULT '' works */
1334
memset(&temp_table, 0, sizeof(temp_table));
1335
temp_table.setShare(this);
1336
temp_table.in_use= &session;
1337
temp_table.getMutableShare()->db_low_byte_first= true; //Cursor->low_byte_first();
1338
temp_table.getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1340
uint32_t field_length= 0; //Assignment is for compiler complaint.
1344
case DRIZZLE_TYPE_BLOB:
1345
case DRIZZLE_TYPE_VARCHAR:
1347
message::Table::Field::StringFieldOptions field_options= pfield.string_options();
1349
charset= get_charset(field_options.has_collation_id() ?
1350
field_options.collation_id() : 0);
1353
charset= default_charset_info;
1355
field_length= field_options.length() * charset->mbmaxlen;
1358
case DRIZZLE_TYPE_DOUBLE:
1360
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1361
if (!fo.has_precision() && !fo.has_scale())
1363
field_length= DBL_DIG+7;
1367
field_length= fo.precision();
1369
if (field_length < decimals &&
1370
decimals != NOT_FIXED_DEC)
1372
my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
1379
case DRIZZLE_TYPE_DECIMAL:
1381
message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
1383
field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
1387
case DRIZZLE_TYPE_TIMESTAMP:
1388
case DRIZZLE_TYPE_DATETIME:
1389
field_length= DateTime::MAX_STRING_LENGTH;
1391
case DRIZZLE_TYPE_DATE:
1392
field_length= Date::MAX_STRING_LENGTH;
1394
case DRIZZLE_TYPE_ENUM:
1398
message::Table::Field::EnumerationValues fo= pfield.enumeration_values();
1400
for (int valnr= 0; valnr < fo.field_value_size(); valnr++)
1402
if (fo.field_value(valnr).length() > field_length)
1404
field_length= charset->cset->numchars(charset,
1405
fo.field_value(valnr).c_str(),
1406
fo.field_value(valnr).c_str()
1407
+ fo.field_value(valnr).length())
1408
* charset->mbmaxlen;
1413
case DRIZZLE_TYPE_LONG:
1415
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1416
field_length= MAX_INT_WIDTH+sign_len;
1419
case DRIZZLE_TYPE_LONGLONG:
1420
field_length= MAX_BIGINT_WIDTH;
1422
case DRIZZLE_TYPE_NULL:
1423
abort(); // Programming error
1426
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1428
pfield.constraints().is_nullable(),
1434
(Field::utype) MTYP_TYPENR(unireg_type),
1435
((field_type == DRIZZLE_TYPE_ENUM) ?
1436
&intervals[interval_nr++]
1438
getTableProto()->field(fieldnr).name().c_str());
1442
f->init(&temp_table); /* blob default values need table obj */
1444
if (! (f->flags & NOT_NULL_FLAG))
1446
*f->null_ptr|= f->null_bit;
1447
if (! (null_bit_pos= (null_bit_pos + 1) & 7)) /* @TODO Ugh. */
1454
enum_check_fields old_count_cuted_fields= session.count_cuted_fields;
1455
session.count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
1456
int res= default_value->save_in_field(f, 1);
1457
session.count_cuted_fields= old_count_cuted_fields;
1458
if (res != 0 && res != 3) /* @TODO Huh? */
1460
my_error(ER_INVALID_DEFAULT, MYF(0), f->field_name);
1466
else if (f->real_type() == DRIZZLE_TYPE_ENUM &&
1467
(f->flags & NOT_NULL_FLAG))
1470
f->store((int64_t) 1, true);
1477
/* hack to undo f->init() */
1479
f->orig_table= NULL;
1481
f->field_index= fieldnr;
1482
f->comment= comment;
1483
if (! default_value &&
1484
! (f->unireg_check==Field::NEXT_NUMBER) &&
1485
(f->flags & NOT_NULL_FLAG) &&
1486
(f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1488
f->flags|= NO_DEFAULT_VALUE_FLAG;
1491
if (f->unireg_check == Field::NEXT_NUMBER)
1492
found_next_number_field= &(field[fieldnr]);
1494
if (timestamp_field == f)
1495
timestamp_field_offset= fieldnr;
1497
if (use_hash) /* supposedly this never fails... but comments lie */
1499
const char *local_field_name= field[fieldnr]->field_name;
1500
name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1506
for (unsigned int keynr= 0; keynr < keys; keynr++, keyinfo++)
1508
key_part= keyinfo->key_part;
1510
for (unsigned int partnr= 0;
1511
partnr < keyinfo->key_parts;
1512
partnr++, key_part++)
1515
* Fix up key_part->offset by adding data_offset.
1516
* We really should compute offset as well.
1517
* But at least this way we are a little better.
1519
key_part->offset= field_offsets[key_part->fieldnr-1] + data_offset;
1524
We need to set the unused bits to 1. If the number of bits is a multiple
1525
of 8 there are no unused bits.
1529
*(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1531
null_bytes= (null_pos - (unsigned char*) record + (null_bit_pos + 7) / 8);
1533
last_null_bit_pos= null_bit_pos;
1538
uint32_t local_primary_key= 0;
1539
doesKeyNameExist("PRIMARY", local_primary_key);
1542
key_part= keyinfo->key_part;
1544
for (uint32_t key= 0; key < keys; key++,keyinfo++)
1546
uint32_t usable_parts= 0;
1548
if (local_primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1551
If the UNIQUE key doesn't have NULL columns and is not a part key
1552
declare this as a primary key.
1554
local_primary_key=key;
1555
for (uint32_t i= 0; i < keyinfo->key_parts; i++)
1557
uint32_t fieldnr= key_part[i].fieldnr;
1559
field[fieldnr-1]->null_ptr ||
1560
field[fieldnr-1]->key_length() != key_part[i].length)
1562
local_primary_key= MAX_KEY; // Can't be used
1568
for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1571
if (! key_part->fieldnr)
1575
local_field= key_part->field= field[key_part->fieldnr-1];
1576
key_part->type= local_field->key_type();
1577
if (local_field->null_ptr)
1579
key_part->null_offset=(uint32_t) ((unsigned char*) local_field->null_ptr - getDefaultValues());
1580
key_part->null_bit= local_field->null_bit;
1581
key_part->store_length+=HA_KEY_NULL_LENGTH;
1582
keyinfo->flags|=HA_NULL_PART_KEY;
1583
keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1584
keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1586
if (local_field->type() == DRIZZLE_TYPE_BLOB ||
1587
local_field->real_type() == DRIZZLE_TYPE_VARCHAR)
1589
if (local_field->type() == DRIZZLE_TYPE_BLOB)
1590
key_part->key_part_flag|= HA_BLOB_PART;
1592
key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1593
keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1594
key_part->store_length+=HA_KEY_BLOB_LENGTH;
1595
keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1597
if (i == 0 && key != local_primary_key)
1598
local_field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1599
(keyinfo->key_parts == 1)) ?
1600
UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1602
local_field->key_start.set(key);
1603
if (local_field->key_length() == key_part->length &&
1604
!(local_field->flags & BLOB_FLAG))
1606
enum ha_key_alg algo= key_info[key].algorithm;
1607
if (db_type()->index_flags(algo) & HA_KEYREAD_ONLY)
1609
keys_for_keyread.set(key);
1610
local_field->part_of_key.set(key);
1611
local_field->part_of_key_not_clustered.set(key);
1613
if (db_type()->index_flags(algo) & HA_READ_ORDER)
1614
local_field->part_of_sortkey.set(key);
1616
if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1618
usable_parts++; // For FILESORT
1619
local_field->flags|= PART_KEY_FLAG;
1620
if (key == local_primary_key)
1622
local_field->flags|= PRI_KEY_FLAG;
1624
If this field is part of the primary key and all keys contains
1625
the primary key, then we can use any key to find this column
1627
if (storage_engine->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX))
1629
local_field->part_of_key= keys_in_use;
1630
if (local_field->part_of_sortkey.test(key))
1631
local_field->part_of_sortkey= keys_in_use;
1634
if (local_field->key_length() != key_part->length)
1636
key_part->key_part_flag|= HA_PART_KEY_SEG;
1639
keyinfo->usable_key_parts= usable_parts; // Filesort
1641
set_if_bigger(max_key_length,keyinfo->key_length+
1642
keyinfo->key_parts);
1643
total_key_length+= keyinfo->key_length;
1645
if (keyinfo->flags & HA_NOSAME)
1647
set_if_bigger(max_unique_length,keyinfo->key_length);
1650
if (local_primary_key < MAX_KEY &&
1651
(keys_in_use.test(local_primary_key)))
1653
primary_key= local_primary_key;
1655
If we are using an integer as the primary key then allow the user to
1656
refer to it as '_rowid'
1658
if (key_info[local_primary_key].key_parts == 1)
1660
Field *local_field= key_info[local_primary_key].key_part[0].field;
1661
if (local_field && local_field->result_type() == INT_RESULT)
1663
/* note that fieldnr here (and rowid_field_offset) starts from 1 */
1664
rowid_field_offset= (key_info[local_primary_key].key_part[0].
1671
if (found_next_number_field)
1673
Field *reg_field= *found_next_number_field;
1674
if ((int) (next_number_index= (uint32_t)
1675
find_ref_key(key_info, keys,
1676
getDefaultValues(), reg_field,
1677
&next_number_key_offset,
1678
&next_number_keypart)) < 0)
1680
/* Wrong field definition */
1687
reg_field->flags |= AUTO_INCREMENT_FLAG;
1695
/* Store offsets to blob fields to find them fast */
1696
blob_field.resize(blob_fields);
1697
save= &blob_field[0];
1699
for (Fields::iterator iter= field.begin(); iter != field.end()-1; iter++, k++)
1701
if ((*iter)->flags & BLOB_FLAG)
1706
db_low_byte_first= true; // @todo Question this.
1707
column_bitmap_size= bitmap_buffer_size(fields);
1709
all_bitmap.resize(column_bitmap_size);
1710
all_set.init(&all_bitmap[0], fields);
1716
int TableShare::parse_table_proto(Session& session, message::Table &table)
1718
int local_error= inner_parse_table_proto(session, table);
1720
if (not local_error)
1726
open_table_error(local_error, open_errno, 0);
1733
Read table definition from a binary / text based .frm cursor
1737
session Thread Cursor
1738
share Fill this with table definition
1741
This function is called when the table definition is not cached in
1743
The data is returned in 'share', which is alloced by
1744
alloc_table_share().. The code assumes that share is initialized.
1748
1 Error (see open_table_error)
1749
2 Error (see open_table_error)
1750
3 Wrong data in .frm cursor
1751
4 Error (see open_table_error)
1752
5 Error (see open_table_error: charset unavailable)
1753
6 Unknown .frm version
1756
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1764
message::Table table;
1766
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1768
if (local_error != EEXIST)
1770
if (local_error > 0)
1777
if (not table.IsInitialized())
1785
local_error= parse_table_proto(session, table);
1787
setTableCategory(TABLE_CATEGORY_USER);
1790
if (local_error && !error_given)
1793
open_table_error(error, (open_errno= errno), 0);
1801
Open a table based on a TableShare
1804
open_table_from_share()
1805
session Thread Cursor
1806
share Table definition
1807
alias Alias for table
1808
db_stat open flags (for example HA_OPEN_KEYFILE|
1809
HA_OPEN_RNDFILE..) can be 0 (example in
1811
ha_open_flags HA_OPEN_ABORT_IF_LOCKED etc..
1812
outparam result table
1816
1 Error (see open_table_error)
1817
2 Error (see open_table_error)
1818
3 Wrong data in .frm cursor
1819
4 Error (see open_table_error)
1820
5 Error (see open_table_error: charset unavailable)
1821
7 Table definition has changed in engine
1824
int TableShare::open_table_from_share(Session *session,
1825
const TableIdentifier &identifier,
1827
uint32_t db_stat, uint32_t ha_open_flags,
1831
uint32_t records, bitmap_size;
1832
bool error_reported= false;
1833
unsigned char *record, *bitmaps;
1836
/* Parsing of partitioning information from .frm needs session->lex set up. */
1837
assert(session->lex->is_lex_started);
1840
outparam.resetTable(session, this, db_stat);
1842
if (not (outparam.alias= strdup(alias)))
1845
/* Allocate Cursor */
1846
if (not (outparam.cursor= db_type()->getCursor(*this)))
1851
if ((db_stat & HA_OPEN_KEYFILE))
1856
if (!(record= (unsigned char*) outparam.alloc_root(rec_buff_length * records)))
1861
/* We are probably in hard repair, and the buffers should not be used */
1862
outparam.record[0]= outparam.record[1]= getDefaultValues();
1866
outparam.record[0]= record;
1868
outparam.record[1]= record+ rec_buff_length;
1870
outparam.record[1]= outparam.getInsertRecord(); // Safety
1875
We need this because when we read var-length rows, we are not updating
1876
bytes after end of varchar
1880
memcpy(outparam.getInsertRecord(), getDefaultValues(), rec_buff_length);
1881
memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1883
memcpy(outparam.getUpdateRecord(), getDefaultValues(), rec_buff_length);
1888
memcpy(outparam.getUpdateRecord(), getDefaultValues(), null_bytes);
1891
if (!(field_ptr = (Field **) outparam.alloc_root( (uint32_t) ((fields+1)* sizeof(Field*)))))
1896
outparam.setFields(field_ptr);
1898
record= (unsigned char*) outparam.getInsertRecord()-1; /* Fieldstart = 1 */
1900
outparam.null_flags= (unsigned char*) record+1;
1902
/* Setup copy of fields from share, but use the right alias and record */
1903
for (uint32_t i= 0 ; i < fields; i++, field_ptr++)
1905
if (!((*field_ptr)= field[i]->clone(outparam.getMemRoot(), &outparam)))
1908
(*field_ptr)= 0; // End marker
1910
if (found_next_number_field)
1911
outparam.found_next_number_field=
1912
outparam.getField(positionFields(found_next_number_field));
1913
if (timestamp_field)
1914
outparam.timestamp_field= (Field_timestamp*) outparam.getField(timestamp_field_offset);
1917
/* Fix key->name and key_part->field */
1920
KeyInfo *local_key_info, *key_info_end;
1921
KeyPartInfo *key_part;
1923
n_length= keys*sizeof(KeyInfo) + key_parts*sizeof(KeyPartInfo);
1924
if (!(local_key_info= (KeyInfo*) outparam.alloc_root(n_length)))
1926
outparam.key_info= local_key_info;
1927
key_part= (reinterpret_cast<KeyPartInfo*> (local_key_info+keys));
1929
memcpy(local_key_info, key_info, sizeof(*local_key_info)*keys);
1930
memcpy(key_part, key_info[0].key_part, (sizeof(*key_part) *
1933
for (key_info_end= local_key_info + keys ;
1934
local_key_info < key_info_end ;
1937
KeyPartInfo *key_part_end;
1939
local_key_info->table= &outparam;
1940
local_key_info->key_part= key_part;
1942
for (key_part_end= key_part+ local_key_info->key_parts ;
1943
key_part < key_part_end ;
1946
Field *local_field= key_part->field= outparam.getField(key_part->fieldnr-1);
1948
if (local_field->key_length() != key_part->length &&
1949
!(local_field->flags & BLOB_FLAG))
1952
We are using only a prefix of the column as a key:
1953
Create a new field for the key part that matches the index
1955
local_field= key_part->field= local_field->new_field(outparam.getMemRoot(), &outparam, 0);
1956
local_field->field_length= key_part->length;
1962
/* Allocate bitmaps */
1964
bitmap_size= column_bitmap_size;
1965
if (!(bitmaps= (unsigned char*) outparam.alloc_root(bitmap_size*3)))
1969
outparam.def_read_set.init((my_bitmap_map*) bitmaps, fields);
1970
outparam.def_write_set.init((my_bitmap_map*) (bitmaps+bitmap_size), fields);
1971
outparam.tmp_set.init((my_bitmap_map*) (bitmaps+bitmap_size*2), fields);
1972
outparam.default_column_bitmaps();
1974
/* The table struct is now initialized; Open the table */
1978
assert(!(db_stat & HA_WAIT_IF_LOCKED));
1981
if ((ha_err= (outparam.cursor->ha_open(identifier, &outparam, getNormalizedPath(),
1982
(db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1983
(db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE : HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1987
case HA_ERR_NO_SUCH_TABLE:
1989
The table did not exists in storage engine, use same error message
1990
as if the .frm cursor didn't exist
1997
Too many files opened, use same error message as if the .frm
2004
outparam.print_error(ha_err, MYF(0));
2005
error_reported= true;
2006
if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
2014
#if defined(HAVE_purify)
2015
memset(bitmaps, 0, bitmap_size*3);
2021
if (!error_reported)
2022
open_table_error(local_error, errno, 0);
2024
delete outparam.cursor;
2025
outparam.cursor= 0; // For easier error checking
2026
outparam.db_stat= 0;
2027
outparam.getMemRoot()->free_root(MYF(0)); // Safe to call on zeroed root
2028
free((char*) outparam.alias);
2030
return (local_error);
2033
/* error message when opening a form cursor */
2034
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
2037
char buff[FN_REFLEN];
2038
myf errortype= ME_ERROR+ME_WAITTANG;
2040
switch (pass_error) {
2043
if (db_errno == ENOENT)
2045
my_error(ER_NO_SUCH_TABLE, MYF(0), db.str, table_name.str);
2049
snprintf(buff, sizeof(buff), "%s",normalized_path.str);
2050
my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
2051
errortype, buff, db_errno);
2056
err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2057
ER_FILE_USED : ER_CANT_OPEN_FILE;
2058
my_error(err_no, errortype, normalized_path.str, db_errno);
2063
const char *csname= get_charset_name((uint32_t) pass_errarg);
2065
if (!csname || csname[0] =='?')
2067
snprintf(tmp, sizeof(tmp), "#%d", pass_errarg);
2070
my_printf_error(ER_UNKNOWN_COLLATION,
2071
_("Unknown collation '%s' in table '%-.64s' definition"),
2072
MYF(0), csname, table_name.str);
2076
snprintf(buff, sizeof(buff), "%s", normalized_path.str);
2077
my_printf_error(ER_NOT_FORM_FILE,
2078
_("Table '%-.64s' was created with a different version "
2079
"of Drizzle and cannot be read"),
2084
default: /* Better wrong error than none */
2086
snprintf(buff, sizeof(buff), "%s", normalized_path.str);
2087
my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
2091
} /* open_table_error */
2093
Field *TableShare::make_field(unsigned char *ptr,
2094
uint32_t field_length,
2096
unsigned char *null_pos,
2097
unsigned char null_bit,
2099
enum_field_types field_type,
2100
const CHARSET_INFO * field_charset,
2101
Field::utype unireg_check,
2103
const char *field_name)
2112
null_bit= ((unsigned char) 1) << null_bit;
2117
case DRIZZLE_TYPE_DATE:
2118
case DRIZZLE_TYPE_DATETIME:
2119
case DRIZZLE_TYPE_TIMESTAMP:
2120
field_charset= &my_charset_bin;
2126
case DRIZZLE_TYPE_ENUM:
2127
return new (&mem_root) Field_enum(ptr,
2132
get_enum_pack_length(interval->count),
2135
case DRIZZLE_TYPE_VARCHAR:
2136
return new (&mem_root) Field_varstring(ptr,field_length,
2137
HA_VARCHAR_PACKLENGTH(field_length),
2142
case DRIZZLE_TYPE_BLOB:
2143
return new (&mem_root) Field_blob(ptr,
2148
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
2150
case DRIZZLE_TYPE_DECIMAL:
2151
return new (&mem_root) Field_decimal(ptr,
2159
false /* is_unsigned */);
2160
case DRIZZLE_TYPE_DOUBLE:
2161
return new (&mem_root) Field_double(ptr,
2169
false /* is_unsigned */);
2170
case DRIZZLE_TYPE_LONG:
2171
return new (&mem_root) Field_long(ptr,
2178
false /* is_unsigned */);
2179
case DRIZZLE_TYPE_LONGLONG:
2180
return new (&mem_root) Field_int64_t(ptr,
2187
false /* is_unsigned */);
2188
case DRIZZLE_TYPE_TIMESTAMP:
2189
return new (&mem_root) Field_timestamp(ptr,
2197
case DRIZZLE_TYPE_DATE:
2198
return new (&mem_root) Field_date(ptr,
2203
case DRIZZLE_TYPE_DATETIME:
2204
return new (&mem_root) Field_datetime(ptr,
2209
case DRIZZLE_TYPE_NULL:
2210
return new (&mem_root) Field_null(ptr,
2214
default: // Impossible (Wrong version)
308
2221
} /* namespace drizzled */