~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/foreign_key.cc

  • Committer: Mark Atwood
  • Date: 2011-10-27 05:08:12 UTC
  • mfrom: (2445.1.11 rf)
  • Revision ID: me@mark.atwood.name-20111027050812-1icvs72lb0u4xdc4
mergeĀ lp:~olafvdspek/drizzle/refactor8

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"
 
20
#include <config.h>
21
21
 
22
22
#include <string>
23
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;
 
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
extern const charset_info_st *system_charset_info;
34
33
 
35
34
void add_foreign_key_to_table_message(
36
35
    message::Table *table_message,
60
59
  pfkey->set_match(match_opt_arg);
61
60
  pfkey->set_update_option(update_opt_arg);
62
61
  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
 
 
 
62
  pfkey->set_references_table_name(table->table.data());
 
63
 
 
64
  List<Key_part_spec>::iterator col_it(cols.begin());
 
65
  while (Key_part_spec* keypart= col_it++)
 
66
  {
 
67
    pfkey->add_column_names(keypart->field_name.data());
 
68
  }
 
69
 
 
70
  List<Key_part_spec>::iterator ref_it(ref_cols.begin());
 
71
  while (Key_part_spec* keypart= ref_it++)
 
72
  {
 
73
    pfkey->add_references_columns(keypart->field_name.data());
 
74
  }
 
75
 
 
76
}
 
77
 
 
78
/**
 
79
  Make a deep copy of each list element.
 
80
 
 
81
  @note A template function and not a template method of class List
 
82
  is employed because of explicit template instantiation:
 
83
  in server code there are explicit instantiations of List<T> and
 
84
  an explicit instantiation of a template requires that any method
 
85
  of the instantiated class used in the template can be resolved.
 
86
  Evidently not all template arguments have clone() method with
 
87
  the right signature.
 
88
 
 
89
  @return You must query the error state in Session for out-of-memory
 
90
  situation after calling this function.
 
91
*/
 
92
 
 
93
template <typename T>
 
94
void list_copy_and_replace_each_value(List<T> &list, memory::Root *mem_root)
 
95
{
 
96
  /* Make a deep copy of each element */
 
97
  typename List<T>::iterator it(list.begin());
 
98
  while (T* el= it++)
 
99
    it.replace(el->clone(mem_root));
79
100
}
80
101
 
81
102
Foreign_key::Foreign_key(const Foreign_key &rhs, memory::Root *mem_root)
108
129
  /* Ensure that 'a' is the generated key */
109
130
  if (a->generated)
110
131
  {
111
 
    if (b->generated && a->columns.elements > b->columns.elements)
 
132
    if (b->generated && a->columns.size() > b->columns.size())
112
133
      std::swap(a, b);                       // Put shorter key in 'a'
113
134
  }
114
135
  else
119
140
  }
120
141
 
121
142
  /* Test if 'a' is a prefix of 'b' */
122
 
  if (a->columns.elements > b->columns.elements)
 
143
  if (a->columns.size() > b->columns.size())
123
144
    return true;                                // Can't be prefix
124
145
 
125
 
  List_iterator<Key_part_spec> col_it1(a->columns);
126
 
  List_iterator<Key_part_spec> col_it2(b->columns);
 
146
  List<Key_part_spec>::iterator col_it1(a->columns.begin());
 
147
  List<Key_part_spec>::iterator col_it2(b->columns.begin());
127
148
  const Key_part_spec *col1, *col2;
128
149
 
129
150
#ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
130
151
  while ((col1= col_it1++))
131
152
  {
132
153
    bool found= 0;
133
 
    col_it2.rewind();
 
154
    col_it2=b->columns.begin();
134
155
    while ((col2= col_it2++))
135
156
    {
136
157
      if (*col1 == *col2)
164
185
*/
165
186
bool Foreign_key::validate(List<CreateField> &table_fields)
166
187
{
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++))
 
188
  List<Key_part_spec>::iterator cols(columns.begin());
 
189
  while (Key_part_spec* column= cols++)
172
190
  {
173
 
    it.rewind();
174
 
    while ((sql_field= it++) &&
175
 
           my_strcasecmp(system_charset_info,
176
 
                         column->field_name.str,
177
 
                         sql_field->field_name)) {}
 
191
    List<CreateField>::iterator it= table_fields.begin();
 
192
    CreateField* sql_field;
 
193
    while ((sql_field= it++) 
 
194
      && my_strcasecmp(system_charset_info, column->field_name.data(), sql_field->field_name))
 
195
    {
 
196
    }
178
197
    if (!sql_field)
179
198
    {
180
 
      my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
 
199
      my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.data());
181
200
      return true;
182
201
    }
183
202
  }