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