mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
cleanup: make_long_hash_field_name() and add_hash_field()
This commit is contained in:
@@ -2582,51 +2582,35 @@ int mysql_add_invisible_field(THD *thd, List<Create_field> * field_list,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define LONG_HASH_FIELD_NAME_LENGTH 30
|
#define INTERNAL_FIELD_NAME_LENGTH 30
|
||||||
static inline void make_long_hash_field_name(LEX_CSTRING *buf, uint num)
|
|
||||||
|
static Lex_ident_column make_internal_field_name(THD *thd, const char *prefix,
|
||||||
|
List<Create_field> *create_list)
|
||||||
{
|
{
|
||||||
buf->length= my_snprintf((char *)buf->str,
|
char buf[INTERNAL_FIELD_NAME_LENGTH]= {0};
|
||||||
LONG_HASH_FIELD_NAME_LENGTH, "DB_ROW_HASH_%u", num);
|
LEX_CSTRING name= { buf, 0 };
|
||||||
|
bool dup_found= true;
|
||||||
|
for (uint num= 1; dup_found; num++)
|
||||||
|
{
|
||||||
|
name.length= my_snprintf(buf, sizeof(buf), "%s%u", prefix, num);
|
||||||
|
for (auto &dup_field : *create_list)
|
||||||
|
if ((dup_found= dup_field.field_name.streq(name)))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Lex_ident_column(thd->strmake_lex_cstring(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static Create_field *add_internal_field(THD *thd, Type_handler *type_handler,
|
||||||
Add fully invisible hash field to table in case of long
|
uint flags, const char *prefix,
|
||||||
unique column
|
List<Create_field> *create_list)
|
||||||
@param thd Thread Context.
|
|
||||||
@param create_list List of table fields.
|
|
||||||
@param key_info current long unique key info
|
|
||||||
*/
|
|
||||||
static Create_field *add_hash_field(THD *thd, List<Create_field> *create_list,
|
|
||||||
KEY *key_info)
|
|
||||||
{
|
{
|
||||||
List_iterator<Create_field> it(*create_list);
|
class Column_derived_attributes dat(&my_charset_bin);
|
||||||
Create_field *dup_field, *cf= new (thd->mem_root) Create_field();
|
Create_field *cf= new (thd->mem_root) Create_field();
|
||||||
cf->flags|= UNSIGNED_FLAG | LONG_UNIQUE_HASH_FIELD;
|
cf->flags|= flags | NOT_NULL_FLAG;
|
||||||
cf->decimals= 0;
|
|
||||||
cf->length= cf->char_length= cf->pack_length= HA_HASH_FIELD_LENGTH;
|
|
||||||
cf->invisible= INVISIBLE_FULL;
|
cf->invisible= INVISIBLE_FULL;
|
||||||
cf->pack_flag|= FIELDFLAG_MAYBE_NULL;
|
cf->field_name= make_internal_field_name(thd, prefix, create_list);
|
||||||
cf->vcol_info= new (thd->mem_root) Virtual_column_info();
|
cf->set_handler(type_handler);
|
||||||
cf->vcol_info->set_vcol_type(VCOL_GENERATED_VIRTUAL);
|
cf->prepare_stage1(thd, thd->mem_root, COLUMN_DEFINITION_TABLE_FIELD, &dat);
|
||||||
uint num= 1;
|
|
||||||
Lex_ident_column field_name;
|
|
||||||
field_name.str= (char *)thd->alloc(LONG_HASH_FIELD_NAME_LENGTH);
|
|
||||||
make_long_hash_field_name(&field_name, num);
|
|
||||||
/*
|
|
||||||
Check for collisions
|
|
||||||
*/
|
|
||||||
while ((dup_field= it++))
|
|
||||||
{
|
|
||||||
if (dup_field->field_name.streq(field_name))
|
|
||||||
{
|
|
||||||
num++;
|
|
||||||
make_long_hash_field_name(&field_name, num);
|
|
||||||
it.rewind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cf->field_name= Lex_ident_column(field_name);
|
|
||||||
cf->set_handler(&type_handler_slonglong);
|
|
||||||
key_info->algorithm= HA_KEY_ALG_LONG_HASH;
|
|
||||||
create_list->push_back(cf,thd->mem_root);
|
create_list->push_back(cf,thd->mem_root);
|
||||||
return cf;
|
return cf;
|
||||||
}
|
}
|
||||||
@@ -3523,19 +3507,24 @@ mysql_prepare_create_table_finalize(THD *thd, HA_CREATE_INFO *create_info,
|
|||||||
!(file->ha_table_flags() & HA_CAN_HASH_KEYS ) &&
|
!(file->ha_table_flags() & HA_CAN_HASH_KEYS ) &&
|
||||||
file->ha_table_flags() & HA_CAN_VIRTUAL_COLUMNS))
|
file->ha_table_flags() & HA_CAN_VIRTUAL_COLUMNS))
|
||||||
{
|
{
|
||||||
Create_field *hash_fld= add_hash_field(thd, &alter_info->create_list,
|
Create_field *hash_fld=
|
||||||
key_info);
|
add_internal_field(thd, &type_handler_ulonglong,
|
||||||
|
UNSIGNED_FLAG | LONG_UNIQUE_HASH_FIELD,
|
||||||
|
"DB_ROW_HASH_", &alter_info->create_list);
|
||||||
if (!hash_fld)
|
if (!hash_fld)
|
||||||
DBUG_RETURN(TRUE);
|
DBUG_RETURN(TRUE);
|
||||||
|
key_info->algorithm= HA_KEY_ALG_LONG_HASH;
|
||||||
hash_fld->offset= record_offset;
|
hash_fld->offset= record_offset;
|
||||||
hash_fld->charset= create_info->default_table_charset;
|
hash_fld->charset= create_info->default_table_charset;
|
||||||
|
hash_fld->vcol_info= new (thd->mem_root) Virtual_column_info();
|
||||||
|
hash_fld->vcol_info->set_vcol_type(VCOL_GENERATED_VIRTUAL);
|
||||||
|
DBUG_ASSERT(hash_fld->pack_length == HA_HASH_FIELD_LENGTH);
|
||||||
record_offset+= hash_fld->pack_length;
|
record_offset+= hash_fld->pack_length;
|
||||||
if (key_info->flags & HA_NULL_PART_KEY)
|
if (key_info->flags & HA_NULL_PART_KEY)
|
||||||
null_fields++;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
hash_fld->flags|= NOT_NULL_FLAG;
|
null_fields++;
|
||||||
hash_fld->pack_flag&= ~FIELDFLAG_MAYBE_NULL;
|
hash_fld->flags&= ~NOT_NULL_FLAG;
|
||||||
|
hash_fld->pack_flag|= FIELDFLAG_MAYBE_NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (validate_comment_length(thd, &key->key_create_info.comment,
|
if (validate_comment_length(thd, &key->key_create_info.comment,
|
||||||
|
Reference in New Issue
Block a user