~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/foreign_key.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
 
22
 
#include <string>
23
 
 
 
20
#include "drizzled/global.h"
 
21
#include "drizzled/server_includes.h" /* @TODO remove this when header include is refactored more... */
24
22
#include "drizzled/foreign_key.h"
25
23
#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)
 
24
 
 
25
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
82
26
  :Key(rhs),
83
27
  ref_table(rhs.ref_table),
84
28
  ref_columns(rhs.ref_columns),
162
106
    0   Key valid
163
107
    1   Key invalid
164
108
*/
165
 
bool Foreign_key::validate(List<CreateField> &table_fields)
 
109
bool Foreign_key::validate(List<Create_field> &table_fields)
166
110
{
167
 
  CreateField  *sql_field;
 
111
  Create_field  *sql_field;
168
112
  Key_part_spec *column;
169
113
  List_iterator<Key_part_spec> cols(columns);
170
 
  List_iterator<CreateField> it(table_fields);
 
114
  List_iterator<Create_field> it(table_fields);
171
115
  while ((column= cols++))
172
116
  {
173
117
    it.rewind();
180
124
      my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
181
125
      return true;
182
126
    }
 
127
    if (type == Key::FOREIGN_KEY && sql_field->vcol_info)
 
128
    {
 
129
      if (delete_opt == FK_OPTION_SET_NULL)
 
130
      {
 
131
        my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
 
132
                 "ON DELETE SET NULL");
 
133
        return true;
 
134
      }
 
135
      if (update_opt == FK_OPTION_SET_NULL)
 
136
      {
 
137
        my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
 
138
                 "ON UPDATE SET NULL");
 
139
        return true;
 
140
      }
 
141
      if (update_opt == FK_OPTION_CASCADE)
 
142
      {
 
143
        my_error(ER_WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN, MYF(0),
 
144
                 "ON UPDATE CASCADE");
 
145
        return true;
 
146
      }
 
147
    }
183
148
  }
184
149
  return false;
185
150
}
186
151
 
187
 
} /* namespace drizzled */
 
152
 
 
153