~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/foreign_key.cc

  • Committer: Monty Taylor
  • Date: 2008-11-16 06:29:53 UTC
  • mto: (584.1.9 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116062953-ivdltjmfe009b5fr
Moved stuff into item/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <string>
23
 
 
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"
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
extern const CHARSET_INFO *system_charset_info;
34
 
 
35
 
void add_foreign_key_to_table_message(
36
 
    message::Table *table_message,
37
 
    const char* fkey_name,
38
 
    List<Key_part_spec> &cols,
39
 
    Table_ident *table,
40
 
    List<Key_part_spec> &ref_cols,
41
 
    message::Table::ForeignKeyConstraint::ForeignKeyOption delete_opt_arg,
42
 
    message::Table::ForeignKeyConstraint::ForeignKeyOption update_opt_arg,
43
 
    message::Table::ForeignKeyConstraint::ForeignKeyMatchOption match_opt_arg)
44
 
{
45
 
  message::Table::ForeignKeyConstraint *pfkey= table_message->add_fk_constraint();
46
 
  if (fkey_name)
47
 
    pfkey->set_name(fkey_name);
48
 
  else if (table_message->has_name())
49
 
  {
50
 
    std::string name(table_message->name());
51
 
    char number[20];
52
 
 
53
 
    name.append("_ibfk_");
54
 
    snprintf(number, sizeof(number), "%d", table_message->fk_constraint_size());
55
 
    name.append(number);
56
 
 
57
 
    pfkey->set_name(name);
58
 
  }
59
 
 
60
 
  pfkey->set_match(match_opt_arg);
61
 
  pfkey->set_update_option(update_opt_arg);
62
 
  pfkey->set_delete_option(delete_opt_arg);
63
 
 
64
 
  pfkey->set_references_table_name(table->table.str);
65
 
 
66
 
  Key_part_spec *keypart;
67
 
  List_iterator<Key_part_spec> col_it(cols);
68
 
  while ((keypart= col_it++))
69
 
  {
70
 
    pfkey->add_column_names(keypart->field_name.str);
71
 
  }
72
 
 
73
 
  List_iterator<Key_part_spec> ref_it(ref_cols);
74
 
  while ((keypart= ref_it++))
75
 
  {
76
 
    pfkey->add_references_columns(keypart->field_name.str);
77
 
  }
78
 
 
79
 
}
80
 
 
81
 
Foreign_key::Foreign_key(const Foreign_key &rhs, memory::Root *mem_root)
82
 
  :Key(rhs),
83
 
  ref_table(rhs.ref_table),
84
 
  ref_columns(rhs.ref_columns),
85
 
  delete_opt(rhs.delete_opt),
86
 
  update_opt(rhs.update_opt),
87
 
  match_opt(rhs.match_opt)
88
 
{
89
 
  list_copy_and_replace_each_value(ref_columns, mem_root);
90
 
}
91
 
 
92
 
/*
93
 
  Test if a foreign key (= generated key) is a prefix of the given key
94
 
  (ignoring key name, key type and order of columns)
95
 
 
96
 
  NOTES:
97
 
    This is only used to test if an index for a FOREIGN KEY exists
98
 
 
99
 
  IMPLEMENTATION
100
 
    We only compare field names
101
 
 
102
 
  RETURN
103
 
    0   Generated key is a prefix of other key
104
 
    1   Not equal
105
 
*/
106
 
bool foreign_key_prefix(Key *a, Key *b)
107
 
{
108
 
  /* Ensure that 'a' is the generated key */
109
 
  if (a->generated)
110
 
  {
111
 
    if (b->generated && a->columns.elements > b->columns.elements)
112
 
      std::swap(a, b);                       // Put shorter key in 'a'
113
 
  }
114
 
  else
115
 
  {
116
 
    if (!b->generated)
117
 
      return true;                              // No foreign key
118
 
    std::swap(a, b);                       // Put generated key in 'a'
119
 
  }
120
 
 
121
 
  /* Test if 'a' is a prefix of 'b' */
122
 
  if (a->columns.elements > b->columns.elements)
123
 
    return true;                                // Can't be prefix
124
 
 
125
 
  List_iterator<Key_part_spec> col_it1(a->columns);
126
 
  List_iterator<Key_part_spec> col_it2(b->columns);
127
 
  const Key_part_spec *col1, *col2;
128
 
 
129
 
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
130
 
  while ((col1= col_it1++))
131
 
  {
132
 
    bool found= 0;
133
 
    col_it2.rewind();
134
 
    while ((col2= col_it2++))
135
 
    {
136
 
      if (*col1 == *col2)
137
 
      {
138
 
        found= true;
139
 
        break;
140
 
      }
141
 
    }
142
 
    if (!found)
143
 
      return true;                              // Error
144
 
  }
145
 
  return false;                                 // Is prefix
146
 
#else
147
 
  while ((col1= col_it1++))
148
 
  {
149
 
    col2= col_it2++;
150
 
    if (!(*col1 == *col2))
151
 
      return true;
152
 
  }
153
 
  return false;                                 // Is prefix
154
 
#endif
155
 
}
156
 
 
157
 
/*
158
 
  Check if the foreign key options are compatible with columns
159
 
  on which the FK is created.
160
 
 
161
 
  RETURN
162
 
    0   Key valid
163
 
    1   Key invalid
164
 
*/
165
 
bool Foreign_key::validate(List<CreateField> &table_fields)
166
 
{
167
 
  CreateField  *sql_field;
168
 
  Key_part_spec *column;
169
 
  List_iterator<Key_part_spec> cols(columns);
170
 
  List_iterator<CreateField> it(table_fields);
171
 
  while ((column= cols++))
172
 
  {
173
 
    it.rewind();
174
 
    while ((sql_field= it++) &&
175
 
           my_strcasecmp(system_charset_info,
176
 
                         column->field_name.str,
177
 
                         sql_field->field_name)) {}
178
 
    if (!sql_field)
179
 
    {
180
 
      my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
181
 
      return true;
182
 
    }
183
 
  }
184
 
  return false;
185
 
}
186
 
 
187
 
} /* namespace drizzled */