1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "drizzled/global.h"
21
#include "drizzled/server_includes.h" /* @TODO remove this when header include is refactored more... */
22
#include "drizzled/foreign_key.h"
23
#include "drizzled/error.h"
25
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
27
ref_table(rhs.ref_table),
28
ref_columns(rhs.ref_columns),
29
delete_opt(rhs.delete_opt),
30
update_opt(rhs.update_opt),
31
match_opt(rhs.match_opt)
33
list_copy_and_replace_each_value(ref_columns, mem_root);
37
Test if a foreign key (= generated key) is a prefix of the given key
38
(ignoring key name, key type and order of columns)
41
This is only used to test if an index for a FOREIGN KEY exists
44
We only compare field names
47
0 Generated key is a prefix of other key
50
bool foreign_key_prefix(Key *a, Key *b)
52
/* Ensure that 'a' is the generated key */
55
if (b->generated && a->columns.elements > b->columns.elements)
56
std::swap(a, b); // Put shorter key in 'a'
61
return true; // No foreign key
62
std::swap(a, b); // Put generated key in 'a'
65
/* Test if 'a' is a prefix of 'b' */
66
if (a->columns.elements > b->columns.elements)
67
return true; // Can't be prefix
69
List_iterator<Key_part_spec> col_it1(a->columns);
70
List_iterator<Key_part_spec> col_it2(b->columns);
71
const Key_part_spec *col1, *col2;
73
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
74
while ((col1= col_it1++))
78
while ((col2= col_it2++))
89
return false; // Is prefix
91
while ((col1= col_it1++))
94
if (!(*col1 == *col2))
97
return false; // Is prefix
102
Check if the foreign key options are compatible with columns
103
on which the FK is created.
109
bool Foreign_key::validate(List<Create_field> &table_fields)
111
Create_field *sql_field;
112
Key_part_spec *column;
113
List_iterator<Key_part_spec> cols(columns);
114
List_iterator<Create_field> it(table_fields);
115
while ((column= cols++))
118
while ((sql_field= it++) &&
119
my_strcasecmp(system_charset_info,
120
column->field_name.str,
121
sql_field->field_name)) {}
124
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
127
if (type == Key::FOREIGN_KEY && sql_field->vcol_info)
129
if (delete_opt == FK_OPTION_SET_NULL)
131
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
132
"ON DELETE SET NULL");
135
if (update_opt == FK_OPTION_SET_NULL)
137
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
138
"ON UPDATE SET NULL");
141
if (update_opt == FK_OPTION_CASCADE)
143
my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
144
"ON UPDATE CASCADE");