2
2
Copyright (c) 2000, 2013, Oracle and/or its affiliates.
3
Copyright (c) 2010, 2013, Monty Program Ab.
3
Copyright (c) 2010, 2014, SkySQL Ab.
5
5
This program is free software; you can redistribute it and/or modify
6
6
it under the terms of the GNU General Public License as published by
74
74
static int mysql_prepare_create_table(THD *, HA_CREATE_INFO *, Alter_info *,
75
75
bool, uint *, handler *, KEY **, uint *,
77
static uint blob_length_by_type(enum_field_types type);
79
80
@brief Helper function for explain_filename
3618
3618
key_part_info->fieldnr= field;
3619
3619
key_part_info->offset= (uint16) sql_field->offset;
3620
3620
key_part_info->key_type=sql_field->pack_flag;
3621
length= sql_field->key_length;
3621
uint key_part_length= sql_field->key_length;
3623
3623
if (column->length)
3625
3625
if (f_is_blob(sql_field->pack_flag))
3627
if ((length=column->length) > max_key_length ||
3628
length > file->max_key_part_length())
3627
key_part_length= min(column->length,
3628
blob_length_by_type(sql_field->sql_type)
3629
* sql_field->charset->mbmaxlen);
3630
if (key_part_length > max_key_length ||
3631
key_part_length > file->max_key_part_length())
3630
length=min(max_key_length, file->max_key_part_length());
3633
key_part_length= min(max_key_length, file->max_key_part_length());
3631
3634
if (key->type == Key::MULTIPLE)
3633
3636
/* not a critical problem */
3634
char warn_buff[MYSQL_ERRMSG_SIZE];
3635
my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
3637
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3638
ER_TOO_LONG_KEY, warn_buff);
3637
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3638
ER_TOO_LONG_KEY, ER(ER_TOO_LONG_KEY),
3639
3640
/* Align key length to multibyte char boundary */
3640
length-= length % sql_field->charset->mbmaxlen;
3641
key_part_length-= key_part_length % sql_field->charset->mbmaxlen;
3644
my_error(ER_TOO_LONG_KEY,MYF(0),length);
3645
my_error(ER_TOO_LONG_KEY, MYF(0), key_part_length);
3645
3646
DBUG_RETURN(TRUE);
3649
3650
// Catch invalid use of partial keys
3650
3651
else if (!f_is_geom(sql_field->pack_flag) &&
3651
3652
// is the key partial?
3652
column->length != length &&
3653
column->length != key_part_length &&
3653
3654
// is prefix length bigger than field length?
3654
(column->length > length ||
3655
(column->length > key_part_length ||
3655
3656
// can the field have a partial key?
3656
3657
!Field::type_can_have_key_part (sql_field->sql_type) ||
3657
3658
// a packed field can't be used in a partial key
3660
3661
((file->ha_table_flags() & HA_NO_PREFIX_CHAR_KEYS) &&
3661
3662
// and is this a 'unique' key?
3662
3663
(key_info->flags & HA_NOSAME))))
3664
3665
my_message(ER_WRONG_SUB_KEY, ER(ER_WRONG_SUB_KEY), MYF(0));
3665
3666
DBUG_RETURN(TRUE);
3667
3668
else if (!(file->ha_table_flags() & HA_NO_PREFIX_CHAR_KEYS))
3668
length=column->length;
3669
key_part_length= column->length;
3670
else if (length == 0 && (sql_field->flags & NOT_NULL_FLAG))
3671
else if (key_part_length == 0 && (sql_field->flags & NOT_NULL_FLAG))
3672
3673
my_error(ER_WRONG_KEY_COLUMN, MYF(0), column->field_name.str);
3673
3674
DBUG_RETURN(TRUE);
3675
if (length > file->max_key_part_length() && key->type != Key::FULLTEXT)
3676
if (key_part_length > file->max_key_part_length() &&
3677
key->type != Key::FULLTEXT)
3677
length= file->max_key_part_length();
3679
key_part_length= file->max_key_part_length();
3678
3680
if (key->type == Key::MULTIPLE)
3680
3682
/* not a critical problem */
3681
char warn_buff[MYSQL_ERRMSG_SIZE];
3682
my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
3684
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3685
ER_TOO_LONG_KEY, warn_buff);
3683
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3684
ER_TOO_LONG_KEY, ER(ER_TOO_LONG_KEY),
3686
3686
/* Align key length to multibyte char boundary */
3687
length-= length % sql_field->charset->mbmaxlen;
3687
key_part_length-= key_part_length % sql_field->charset->mbmaxlen;
3691
my_error(ER_TOO_LONG_KEY,MYF(0),length);
3691
my_error(ER_TOO_LONG_KEY, MYF(0), key_part_length);
3692
3692
DBUG_RETURN(TRUE);
3695
key_part_info->length=(uint16) length;
3695
key_part_info->length= (uint16) key_part_length;
3696
3696
/* Use packed keys for long strings on the first column */
3697
3697
if (!((*db_options) & HA_OPTION_NO_PACK_KEYS) &&
3698
3698
!((create_info->table_options & HA_OPTION_NO_PACK_KEYS)) &&
3699
(length >= KEY_DEFAULT_PACK_LENGTH &&
3699
(key_part_length >= KEY_DEFAULT_PACK_LENGTH &&
3700
3700
(sql_field->sql_type == MYSQL_TYPE_STRING ||
3701
3701
sql_field->sql_type == MYSQL_TYPE_VARCHAR ||
3702
3702
sql_field->pack_flag & FIELDFLAG_BLOB)))
3708
3708
key_info->flags|= HA_PACK_KEY;
3710
3710
/* Check if the key segment is partial, set the key flag accordingly */
3711
if (length != sql_field->key_length)
3711
if (key_part_length != sql_field->key_length)
3712
3712
key_info->flags|= HA_KEY_HAS_PART_KEY_SEG;
3714
key_length+= key_part_length;
3715
3715
key_part_info++;
3717
3717
/* Create the key name based on the first column (if not given) */