25#include <drizzled/error.h>
26#include <drizzled/charset.h>
27#include <drizzled/field.h>
28#include <drizzled/table.h>
29#include <drizzled/field/varstring.h>
30#include <drizzled/internal/my_sys.h>
31#include <drizzled/key_part_info.h>
51 const dict_col_t* col,
52 const unsigned char* data,
57 unsigned char* dest = field->ptr;
58 ulint flen = field->pack_length();
67 for (ptr = dest + len; ptr != dest; ) {
71 if (!(field->flags & UNSIGNED_FLAG)) {
72 ((
byte*) dest)[len - 1] ^= 0x80;
82 if (field->type() == DRIZZLE_TYPE_VARCHAR) {
88 dest, len, flen - field->key_length());
92 memcpy(dest, data, len);
109 memcpy(dest, data, len);
128 memcpy(dest, data, len);
140 const dict_index_t* index,
141 const ulint* offsets)
144 uint n_fields = table->getShare()->sizeFields();
149 for (i = 0; i < n_fields; i++) {
150 Field* field = table->getField(i);
153 const unsigned char* ifield;
157 ipos = dict_index_get_nth_col_pos(index, i);
159 if (UNIV_UNLIKELY(ipos == ULINT_UNDEFINED)) {
165 ifield = rec_get_nth_field(rec, offsets, ipos, &ilen);
168 if (ilen == UNIV_SQL_NULL) {
169 ut_ad(field->real_maybe_null());
173 field->set_notnull();
177 dict_index_get_nth_field(index, ipos)),
178 ifield, ilen, field);
190 uint n_fields = table->getShare()->sizeFields();
193 for (i = 0; i < n_fields; i++) {
194 table->getField(i)->set_default();
203innobase_convert_tablename(
208 char* slash = strchr(s,
'/');
214 strncpy(s, s, slash - s + 1);
222 strncpy(t, slash, slash - t + strlen(slash));
232innobase_check_index_keys(
237 const dict_table_t* table)
244 for (key_num = 0; key_num < num_of_keys; key_num++) {
245 const KeyInfo& key = key_info[key_num];
250 for (ulint i = 0; i < key_num; i++) {
251 const KeyInfo& key2 = key_info[i];
253 if (0 == strcmp(key.name, key2.name)) {
254 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0),
257 return(ER_WRONG_NAME_FOR_INDEX);
263 for (
const dict_index_t* index
264 = dict_table_get_first_index(table);
265 index; index = dict_table_get_next_index(index)) {
267 if (0 == strcmp(key.name, index->name)) {
268 my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0),
271 return(ER_WRONG_NAME_FOR_INDEX);
279 for (ulint i = 0; i < key.key_parts; i++) {
287 &is_unsigned, field)) {
294 if (field->type() == DRIZZLE_TYPE_VARCHAR) {
296 >= field->pack_length()
303 >= field->pack_length()) {
308 my_error(ER_WRONG_KEY_COLUMN, MYF(0),
310 return(ER_WRONG_KEY_COLUMN);
313 for (ulint j = 0; j < i; j++) {
322 my_error(ER_WRONG_KEY_COLUMN, MYF(0),
324 return(ER_WRONG_KEY_COLUMN);
336innobase_create_index_field_def(
350 field = key_part->field;
355 if (DATA_BLOB == col_type
356 || (key_part->length < field->pack_length()
357 && field->type() != DRIZZLE_TYPE_VARCHAR)
358 || (field->type() == DRIZZLE_TYPE_VARCHAR
359 && key_part->length < field->pack_length()
376innobase_create_index_def(
390 ulint n_fields = key->key_parts;
394 heap, n_fields *
sizeof *index->fields);
397 index->n_fields = n_fields;
398 len = strlen(key->name) + 1;
402 if (UNIV_LIKELY(!new_primary)) {
406 memcpy(index_name, key->name, len);
408 if (key->flags & HA_NOSAME) {
416 for (i = 0; i < n_fields; i++) {
417 innobase_create_index_field_def(&key->key_part[i], heap,
428innobase_copy_index_field_def(
430 const dict_field_t* field,
433 assert(field != NULL);
434 assert(index_field != NULL);
446innobase_copy_index_def(
448 const dict_index_t* index,
459 n_fields = index->n_user_defined_cols;
462 heap, n_fields *
sizeof *new_index->
fields);
468 new_index->
name = index->name;
470 for (i = 0; i < n_fields; i++) {
471 innobase_copy_index_field_def(&index->fields[i],
498innobase_create_key_def(
501 const dict_table_t*table,
520 new_primary = !system_charset_info->strcasecmp(key_info->name,
"PRIMARY");
527 if (!new_primary && (key_info->flags & HA_NOSAME)
528 && (!(key_info->flags & HA_KEY_HAS_PART_KEY_SEG))
530 uint key_part = key_info->key_parts;
535 if (key_info->key_part[key_part].null_bit == 0) {
543 const dict_index_t* index;
546 innobase_create_index_def(&key_info[i++], TRUE, TRUE,
549 row_mysql_lock_data_dictionary(trx);
551 index = dict_table_get_first_index(table);
559 || !system_charset_info->strcasecmp(index->name,
"PRIMARY"))
561 index = dict_table_get_next_index(index);
565 innobase_copy_index_def(index, indexdef++, heap);
566 index = dict_table_get_next_index(index);
575 innobase_create_index_def(&key_info[i++], new_primary, FALSE,
579 n_keys = indexdef - indexdefs;
589innobase_create_temporary_tablename(
593 const char* table_name)
597 static const char suffix[] =
"@0023 ";
599 len = strlen(table_name);
602 memcpy(name, table_name, len);
603 memcpy(name + len, suffix,
sizeof suffix);
604 name[len + (
sizeof suffix - 2)] =
id;
615ha_innobase::add_index(
622 dict_index_t** index;
623 dict_table_t* innodb_table;
624 dict_table_t* indexed_table;
629 ulint num_created = 0;
630 ibool dict_locked = FALSE;
638 if (srv_created_new_raw || srv_force_recovery) {
639 return(HA_ERR_WRONG_COMMAND);
656 innodb_table = indexed_table
657 = dict_table_get(
prebuilt->table->name, FALSE);
659 if (UNIV_UNLIKELY(!innodb_table)) {
660 error = HA_ERR_NO_SUCH_TABLE;
665 if (innobase_index_name_is_reserved(trx, key_info, num_of_keys)) {
669 error = innobase_check_index_keys(key_info, num_of_keys,
673 if (UNIV_UNLIKELY(error)) {
686 num_of_idx = num_of_keys;
688 index_defs = innobase_create_key_def(
689 trx, innodb_table, heap, key_info, num_of_idx);
696 heap, num_of_idx *
sizeof *index);
704 new_primary ? LOCK_X : LOCK_S);
706 if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
714 row_mysql_lock_data_dictionary(trx);
717 ut_d(dict_table_check_for_dup_indexes(innodb_table, FALSE));
722 if (UNIV_UNLIKELY(new_primary)) {
727 char* new_table_name = innobase_create_temporary_tablename(
728 heap,
'1', innodb_table->
name);
733 new_table_name, index_defs, innodb_table, trx);
735 if (!indexed_table) {
738 case DB_TABLESPACE_ALREADY_EXISTS:
739 case DB_DUPLICATE_KEY:
740 innobase_convert_tablename(new_table_name);
741 my_error(HA_ERR_TABLE_EXIST, MYF(0),
743 error = HA_ERR_TABLE_EXIST;
751 ut_d(dict_table_check_for_dup_indexes(innodb_table,
762 for (ulint i = 0; i < num_of_idx; i++) {
775 ut_ad(error == DB_SUCCESS);
791 if (UNIV_UNLIKELY(new_primary)) {
794 ut_ad(indexed_table != innodb_table);
799 if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
808 innodb_table, indexed_table,
809 index, num_of_idx, i_table);
817 const char* old_name;
821 row_mysql_lock_data_dictionary(trx);
824 ut_d(dict_table_check_for_dup_indexes(
prebuilt->table, TRUE));
829 if (error != DB_SUCCESS) {
841 old_name = innodb_table->
name;
842 tmp_name = innobase_create_temporary_tablename(heap,
'2',
848 if (error != DB_SUCCESS) {
853 case DB_TABLESPACE_ALREADY_EXISTS:
854 case DB_DUPLICATE_KEY:
855 innobase_convert_tablename(tmp_name);
856 my_error(HA_ERR_TABLE_EXIST, MYF(0), tmp_name);
857 error = HA_ERR_TABLE_EXIST;
872 innodb_table = indexed_table;
875 case DB_TOO_BIG_RECORD:
876 my_error(HA_ERR_TO_BIG_ROW, MYF(0));
878 case DB_PRIMARY_KEY_IS_NULL:
879 my_error(ER_PRIMARY_CANT_HAVE_NULL, MYF(0));
881 case DB_DUPLICATE_KEY:
889 if (indexed_table != innodb_table) {
894 row_mysql_lock_data_dictionary(trx);
903 if (error == DB_SUCCESS) {
908 share->idx_trans_tbl.index_count = 0;
923 ut_d(dict_table_check_for_dup_indexes(innodb_table, FALSE));
940ha_innobase::prepare_drop_index(
954 if (srv_created_new_raw || srv_force_recovery) {
955 return(HA_ERR_WRONG_COMMAND);
965 row_mysql_lock_data_dictionary(trx);
966 ut_d(dict_table_check_for_dup_indexes(
prebuilt->table, FALSE));
971 const dict_index_t* index
972 = dict_table_get_first_index(
prebuilt->table);
975 index = dict_table_get_next_index(index);
979 for (n_key = 0; n_key < num_of_keys; n_key++) {
983 key = i_table->
key_info + key_num[n_key];
984 index = dict_table_get_index_on_name_and_min_id(
988 errmsg_printf(ERRMSG_LVL_ERROR,
"InnoDB could not find key n:o %u "
989 "with name %s for table %s",
991 key ? key->name :
"NULL",
994 err = HA_ERR_KEY_NOT_FOUND;
1004 my_error(ER_REQUIRES_PRIMARY_KEY, MYF(0));
1025 if (trx->check_foreigns
1026 && session_sql_command(
user_session) != SQLCOM_CREATE_INDEX) {
1027 dict_index_t* index;
1029 for (index = dict_table_get_first_index(
prebuilt->table);
1031 index = dict_table_get_next_index(index)) {
1032 dict_foreign_t* foreign;
1040 foreign = dict_table_get_referenced_constraint(
1047 "Index needed in foreign key "
1052 err = HA_ERR_DROP_INDEX_FK;
1057 foreign = dict_table_get_foreign_constraint(
1066 if (!dict_foreign_find_equiv_index(
1074 }
else if (session_sql_command(
user_session) == SQLCOM_CREATE_INDEX) {
1080 dict_index_t* index;
1082 for (index = dict_table_get_first_index(
prebuilt->table);
1084 index = dict_table_get_next_index(index)) {
1085 dict_foreign_t* foreign;
1093 foreign = dict_table_get_foreign_constraint(
1096 if (foreign == NULL) {
1107 if (!dict_foreign_find_equiv_index(foreign)) {
1110 "Index needed in foreign key "
1115 err = HA_ERR_DROP_INDEX_FK;
1125 = dict_table_get_first_index(
prebuilt->table);
1129 index = dict_table_get_next_index(index);
1133 ut_d(dict_table_check_for_dup_indexes(
prebuilt->table, FALSE));
1144ha_innobase::final_drop_index(
1149 dict_index_t* index;
1153 if (srv_created_new_raw || srv_force_recovery) {
1154 return(HA_ERR_WRONG_COMMAND);
1177 row_mysql_lock_data_dictionary(trx);
1178 ut_d(dict_table_check_for_dup_indexes(
prebuilt->table, FALSE));
1180 if (UNIV_UNLIKELY(err)) {
1183 for (index = dict_table_get_first_index(
prebuilt->table);
1184 index; index = dict_table_get_next_index(index)) {
1194 index = dict_table_get_first_index(
prebuilt->table);
1197 dict_index_t* next_index;
1199 next_index = dict_table_get_next_index(index);
1210 for (index = dict_table_get_first_index(
prebuilt->table);
1211 index; index = dict_table_get_next_index(index)) {
1217 share->idx_trans_tbl.index_count = 0;
1220 ut_d(dict_table_check_for_dup_indexes(
prebuilt->table, FALSE));
row_prebuilt_t * prebuilt
UNIV_INTERN void update_session(Session *session)
UNIV_INLINE const dict_col_t * dict_index_get_nth_col(const dict_index_t *index, ulint pos)
UNIV_INLINE ulint dict_index_is_clust(const dict_index_t *index) __attribute__((pure))
UNIV_INLINE const dict_col_t * dict_field_get_col(const dict_field_t *field)
UNIV_INLINE ulint dict_table_get_n_user_cols(const dict_table_t *table)
UNIV_INTERN trx_t * innobase_trx_allocate(Session *session)
UNIV_INTERN ulint get_innobase_type_from_mysql_type(ulint *unsigned_flag, const void *f)
UNIV_INTERN int convert_error_code_to_mysql(int error, ulint flags, Session *session)
UNIV_INTERN void innobase_rec_to_mysql(Table *table, const rec_t *rec, const dict_index_t *index, const ulint *offsets)
static void innobase_col_to_mysql(const dict_col_t *col, const unsigned char *data, ulint len, Field *field)
UNIV_INTERN void innobase_rec_reset(Table *table)
UNIV_INTERN void log_buffer_flush_to_disk(void)
UNIV_INTERN char * mem_heap_strdup(mem_heap_t *heap, const char *str)
#define mem_heap_create(N)
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_free(heap)
UNIV_INTERN dict_index_t * row_merge_create_index(trx_t *trx, dict_table_t *table, const merge_index_def_t *index_def)
struct merge_index_def_struct merge_index_def_t
UNIV_INTERN void row_merge_drop_indexes(trx_t *trx, dict_table_t *table, dict_index_t **index, ulint num_created)
UNIV_INTERN ulint row_merge_rename_tables(dict_table_t *old_table, dict_table_t *new_table, const char *tmp_name, trx_t *trx)
UNIV_INTERN ulint row_merge_build_indexes(trx_t *trx, dict_table_t *old_table, dict_table_t *new_table, dict_index_t **indexes, ulint n_indexes, TABLE *table)
UNIV_INTERN ulint row_merge_lock_table(trx_t *trx, dict_table_t *table, enum lock_mode mode)
struct merge_index_field_struct merge_index_field_t
UNIV_INTERN dict_table_t * row_merge_create_temporary_table(const char *table_name, const merge_index_def_t *index_def, const dict_table_t *table, trx_t *trx)
UNIV_INTERN void row_merge_drop_index(dict_index_t *index, dict_table_t *table, trx_t *trx)
UNIV_INTERN ulint row_merge_drop_table(trx_t *trx, dict_table_t *table)
UNIV_INTERN ulint row_merge_rename_indexes(trx_t *trx, dict_table_t *table)
UNIV_INTERN void row_prebuilt_free(row_prebuilt_t *prebuilt, ibool dict_locked)
UNIV_INTERN ibool row_table_got_default_clust_index(const dict_table_t *table)
UNIV_INTERN row_prebuilt_t * row_create_prebuilt(dict_table_t *table)
UNIV_INTERN byte * row_mysql_store_true_var_len(byte *dest, ulint len, ulint lenlen)
UNIV_INTERN void row_mysql_store_blob_ref(byte *dest, ulint col_len, const void *data, ulint len)
UNIV_INTERN void row_mysql_unlock_data_dictionary(trx_t *trx)
UNIV_INTERN void srv_active_wake_master_thread(void)
dict_index_t * foreign_index
ulint n_mysql_handles_opened
merge_index_field_t * fields
const dict_index_t * error_info
UNIV_INTERN int trx_general_rollback_for_mysql(trx_t *trx, trx_savept_t *savept)
UNIV_INLINE void trx_start_if_not_started(trx_t *trx)
UNIV_INTERN void trx_set_detailed_error(trx_t *trx, const char *msg)
UNIV_INTERN void trx_search_latch_release_if_reserved(trx_t *trx)
UNIV_INTERN ulint trx_commit_for_mysql(trx_t *trx)
UNIV_INTERN void trx_free_for_mysql(trx_t *trx)
UNIV_INLINE void trx_set_dict_operation(trx_t *trx, enum trx_dict_op op)
#define UT_LIST_GET_LEN(BASE)
#define TEMP_INDEX_PREFIX