934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Sun Microsystems
|
|
5 |
*
|
|
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.
|
|
9 |
*
|
|
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.
|
|
14 |
*
|
|
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
|
|
18 |
*/
|
|
19 |
||
1241.9.1
by Monty Taylor
Removed global.h. Fixed all the headers. |
20 |
#include "config.h" |
21 |
||
22 |
#include <string> |
|
23 |
||
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
24 |
#include "drizzled/foreign_key.h" |
25 |
#include "drizzled/error.h" |
|
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
26 |
#include "drizzled/create_field.h" |
1241.9.64
by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal. |
27 |
#include "drizzled/internal/my_sys.h" |
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
28 |
|
1241.9.1
by Monty Taylor
Removed global.h. Fixed all the headers. |
29 |
extern const CHARSET_INFO *system_charset_info; |
30 |
||
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
31 |
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root) |
32 |
:Key(rhs), |
|
33 |
ref_table(rhs.ref_table), |
|
34 |
ref_columns(rhs.ref_columns), |
|
35 |
delete_opt(rhs.delete_opt), |
|
36 |
update_opt(rhs.update_opt), |
|
37 |
match_opt(rhs.match_opt) |
|
38 |
{
|
|
39 |
list_copy_and_replace_each_value(ref_columns, mem_root); |
|
40 |
}
|
|
41 |
||
42 |
/*
|
|
43 |
Test if a foreign key (= generated key) is a prefix of the given key
|
|
44 |
(ignoring key name, key type and order of columns)
|
|
45 |
||
46 |
NOTES:
|
|
47 |
This is only used to test if an index for a FOREIGN KEY exists
|
|
48 |
||
49 |
IMPLEMENTATION
|
|
50 |
We only compare field names
|
|
51 |
||
52 |
RETURN
|
|
53 |
0 Generated key is a prefix of other key
|
|
54 |
1 Not equal
|
|
55 |
*/
|
|
56 |
bool foreign_key_prefix(Key *a, Key *b) |
|
57 |
{
|
|
58 |
/* Ensure that 'a' is the generated key */
|
|
59 |
if (a->generated) |
|
60 |
{
|
|
61 |
if (b->generated && a->columns.elements > b->columns.elements) |
|
62 |
std::swap(a, b); // Put shorter key in 'a' |
|
63 |
}
|
|
64 |
else
|
|
65 |
{
|
|
66 |
if (!b->generated) |
|
67 |
return true; // No foreign key |
|
68 |
std::swap(a, b); // Put generated key in 'a' |
|
69 |
}
|
|
70 |
||
71 |
/* Test if 'a' is a prefix of 'b' */
|
|
72 |
if (a->columns.elements > b->columns.elements) |
|
73 |
return true; // Can't be prefix |
|
74 |
||
75 |
List_iterator<Key_part_spec> col_it1(a->columns); |
|
76 |
List_iterator<Key_part_spec> col_it2(b->columns); |
|
77 |
const Key_part_spec *col1, *col2; |
|
78 |
||
79 |
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
|
|
80 |
while ((col1= col_it1++)) |
|
81 |
{
|
|
82 |
bool found= 0; |
|
83 |
col_it2.rewind(); |
|
84 |
while ((col2= col_it2++)) |
|
85 |
{
|
|
86 |
if (*col1 == *col2) |
|
87 |
{
|
|
88 |
found= true; |
|
89 |
break; |
|
90 |
}
|
|
91 |
}
|
|
92 |
if (!found) |
|
93 |
return true; // Error |
|
94 |
}
|
|
95 |
return false; // Is prefix |
|
96 |
#else
|
|
97 |
while ((col1= col_it1++)) |
|
98 |
{
|
|
99 |
col2= col_it2++; |
|
100 |
if (!(*col1 == *col2)) |
|
101 |
return true; |
|
102 |
}
|
|
103 |
return false; // Is prefix |
|
104 |
#endif
|
|
105 |
}
|
|
106 |
||
107 |
/*
|
|
108 |
Check if the foreign key options are compatible with columns
|
|
109 |
on which the FK is created.
|
|
110 |
||
111 |
RETURN
|
|
112 |
0 Key valid
|
|
113 |
1 Key invalid
|
|
114 |
*/
|
|
1052.2.3
by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards. |
115 |
bool Foreign_key::validate(List<CreateField> &table_fields) |
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
116 |
{
|
1052.2.3
by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards. |
117 |
CreateField *sql_field; |
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
118 |
Key_part_spec *column; |
119 |
List_iterator<Key_part_spec> cols(columns); |
|
1052.2.3
by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards. |
120 |
List_iterator<CreateField> it(table_fields); |
934.2.20
by Jay Pipes
Move Foreign_key implementation into its own implementation file and out of session.cc |
121 |
while ((column= cols++)) |
122 |
{
|
|
123 |
it.rewind(); |
|
124 |
while ((sql_field= it++) && |
|
125 |
my_strcasecmp(system_charset_info, |
|
126 |
column->field_name.str, |
|
127 |
sql_field->field_name)) {} |
|
128 |
if (!sql_field) |
|
129 |
{
|
|
130 |
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str); |
|
131 |
return true; |
|
132 |
}
|
|
133 |
}
|
|
134 |
return false; |
|
135 |
}
|
|
136 |
||
137 |
||
138 |