24 #include <drizzled/foreign_key.h>
25 #include <drizzled/error.h>
26 #include <drizzled/create_field.h>
27 #include <drizzled/internal/my_sys.h>
28 #include <drizzled/table_ident.h>
32 extern const charset_info_st *system_charset_info;
34 void add_foreign_key_to_table_message(
35 message::Table *table_message,
36 const char* fkey_name,
37 List<Key_part_spec> &cols,
39 List<Key_part_spec> &ref_cols,
40 message::Table::ForeignKeyConstraint::ForeignKeyOption delete_opt_arg,
41 message::Table::ForeignKeyConstraint::ForeignKeyOption update_opt_arg,
42 message::Table::ForeignKeyConstraint::ForeignKeyMatchOption match_opt_arg)
44 message::Table::ForeignKeyConstraint *pfkey= table_message->add_fk_constraint();
46 pfkey->set_name(fkey_name);
47 else if (table_message->has_name())
49 std::string name(table_message->name());
52 name.append(
"_ibfk_");
53 snprintf(number,
sizeof(number),
"%d", table_message->fk_constraint_size());
56 pfkey->set_name(name);
59 pfkey->set_match(match_opt_arg);
60 pfkey->set_update_option(update_opt_arg);
61 pfkey->set_delete_option(delete_opt_arg);
62 pfkey->set_references_table_name(table->table.data());
64 List<Key_part_spec>::iterator col_it(cols.begin());
65 while (Key_part_spec* keypart= col_it++)
67 pfkey->add_column_names(keypart->field_name.data());
70 List<Key_part_spec>::iterator ref_it(ref_cols.begin());
71 while (Key_part_spec* keypart= ref_it++)
73 pfkey->add_references_columns(keypart->field_name.data());
99 it.replace(el->clone(mem_root));
104 ref_table(rhs.ref_table),
105 ref_columns(rhs.ref_columns),
106 delete_opt(rhs.delete_opt),
107 update_opt(rhs.update_opt),
108 match_opt(rhs.match_opt)
127 bool foreign_key_prefix(
Key *a,
Key *b)
132 if (b->generated && a->columns.size() > b->columns.size())
143 if (a->columns.size() > b->columns.size())
146 List<Key_part_spec>::iterator col_it1(a->columns.begin());
147 List<Key_part_spec>::iterator col_it2(b->columns.begin());
148 const Key_part_spec *col1, *col2;
150 #ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
151 while ((col1= col_it1++))
154 col_it2=b->columns.begin();
155 while ((col2= col_it2++))
168 while ((col1= col_it1++))
171 if (!(*col1 == *col2))
186 bool Foreign_key::validate(List<CreateField> &table_fields)
188 List<Key_part_spec>::iterator cols(columns.begin());
189 while (Key_part_spec* column= cols++)
191 List<CreateField>::iterator it= table_fields.begin();
192 CreateField* sql_field;
193 while ((sql_field= it++)
194 && system_charset_info->strcasecmp(column->field_name.data(), sql_field->field_name))
199 my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.data());