diff options
Diffstat (limited to 'sql/table.cc')
-rw-r--r-- | sql/table.cc | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/sql/table.cc b/sql/table.cc index 6ac45445136..0e616bea6ef 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -17,7 +17,7 @@ /* Some general useful functions */ -#include "my_global.h" /* NO_EMBEDDED_ACCESS_CHECKS */ +#include <my_global.h> /* NO_EMBEDDED_ACCESS_CHECKS */ #include "sql_priv.h" #include "unireg.h" // REQUIRED: for other includes #include "table.h" @@ -946,10 +946,10 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, size_t length= *extra2++; if (!length) { - if (extra2 + 258 >= e2end) + if (extra2 + 2 >= e2end) goto err; length= uint2korr(extra2); - extra2+=2; + extra2+= 2; if (length < 256) goto err; } @@ -1666,10 +1666,44 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, if (key_parts) { uint add_first_key_parts= 0; - uint primary_key=(uint) (find_type(primary_key_name, &share->keynames, - FIND_TYPE_NO_PREFIX) - 1); longlong ha_option= handler_file->ha_table_flags(); keyinfo= share->key_info; + uint primary_key= my_strcasecmp(system_charset_info, share->keynames.type_names[0], + primary_key_name) ? MAX_KEY : 0; + + if (primary_key >= MAX_KEY && keyinfo->flags & HA_NOSAME) + { + /* + If the UNIQUE key doesn't have NULL columns and is not a part key + declare this as a primary key. + */ + primary_key= 0; + key_part= keyinfo->key_part; + for (i=0 ; i < keyinfo->user_defined_key_parts ;i++) + { + DBUG_ASSERT(key_part[i].fieldnr > 0); + // Table field corresponding to the i'th key part. + Field *table_field= share->field[key_part[i].fieldnr - 1]; + + /* + If the key column is of NOT NULL BLOB type, then it + will definitly have key prefix. And if key part prefix size + is equal to the BLOB column max size, then we can promote + it to primary key. + */ + if (!table_field->real_maybe_null() && + table_field->type() == MYSQL_TYPE_BLOB && + table_field->field_length == key_part[i].length) + continue; + + if (table_field->real_maybe_null() || + table_field->key_length() != key_part[i].length) + { + primary_key= MAX_KEY; // Can't be used + break; + } + } + } if (share->use_ext_keys) { @@ -1764,40 +1798,6 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, if (share->key_info[key].flags & HA_FULLTEXT) share->key_info[key].algorithm= HA_KEY_ALG_FULLTEXT; - if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME)) - { - /* - If the UNIQUE key doesn't have NULL columns and is not a part key - declare this as a primary key. - */ - primary_key=key; - key_part= keyinfo->key_part; - for (i=0 ; i < keyinfo->user_defined_key_parts ;i++) - { - DBUG_ASSERT(key_part[i].fieldnr > 0); - // Table field corresponding to the i'th key part. - Field *table_field= share->field[key_part[i].fieldnr - 1]; - - /* - If the key column is of NOT NULL BLOB type, then it - will definitly have key prefix. And if key part prefix size - is equal to the BLOB column max size, then we can promote - it to primary key. - */ - if (!table_field->real_maybe_null() && - table_field->type() == MYSQL_TYPE_BLOB && - table_field->field_length == key_part[i].length) - continue; - - if (table_field->real_maybe_null() || - table_field->key_length() != key_part[i].length) - { - primary_key= MAX_KEY; // Can't be used - break; - } - } - } - key_part= keyinfo->key_part; uint key_parts= share->use_ext_keys ? keyinfo->ext_key_parts : keyinfo->user_defined_key_parts; @@ -6067,11 +6067,19 @@ bool TABLE::alloc_keys(uint key_count) } +/* + Given a field, fill key_part_info + @param keyinfo Key to where key part is added (we will + only adjust key_length there) + @param field IN Table field for which key part is needed + @param key_part_info OUT key part structure to be filled. + @param fieldnr Field's number. +*/ void TABLE::create_key_part_by_field(KEY *keyinfo, KEY_PART_INFO *key_part_info, Field *field, uint fieldnr) -{ - field->flags|= PART_KEY_FLAG; +{ + DBUG_ASSERT(field->field_index + 1 == (int)fieldnr); key_part_info->null_bit= field->null_bit; key_part_info->null_offset= (uint) (field->null_ptr - (uchar*) record[0]); @@ -6228,6 +6236,7 @@ bool TABLE::add_tmp_key(uint key, uint key_parts, (*reg_field)->key_start.set_bit(key); (*reg_field)->part_of_key.set_bit(key); create_key_part_by_field(keyinfo, key_part_info, *reg_field, fld_idx+1); + (*reg_field)->flags|= PART_KEY_FLAG; key_start= FALSE; key_part_info++; } @@ -6727,7 +6736,7 @@ int TABLE::update_default_fields() DBUG_ASSERT(default_field); - /* Iterate over virtual fields in the table */ + /* Iterate over fields with default functions in the table */ for (dfield_ptr= default_field; *dfield_ptr; dfield_ptr++) { dfield= (*dfield_ptr); @@ -6744,12 +6753,16 @@ int TABLE::update_default_fields() if (res) DBUG_RETURN(res); } - /* Unset the explicit default flag for the next record. */ - dfield->flags&= ~HAS_EXPLICIT_VALUE; } DBUG_RETURN(res); } +void TABLE::reset_default_fields() +{ + if (default_field) + for (Field **df= default_field; *df; df++) + (*df)->flags&= ~HAS_EXPLICIT_VALUE; +} /* Prepare triggers for INSERT-like statement. |