~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/referential_constraints.cc

New merge for TableShare

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) 2009 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; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file 
 
23
 *   referential constraints I_S table methods.
 
24
 */
 
25
 
 
26
#include "drizzled/server_includes.h"
 
27
#include "drizzled/session.h"
 
28
#include "drizzled/show.h"
 
29
 
 
30
#include "helper_methods.h"
 
31
#include "referential_constraints.h"
 
32
 
 
33
#include <vector>
 
34
 
 
35
using namespace drizzled;
 
36
using namespace std;
 
37
 
 
38
/*
 
39
 * Vectors of columns for the referential constraints I_S table.
 
40
 */
 
41
static vector<const plugin::ColumnInfo *> *columns= NULL;
 
42
 
 
43
/*
 
44
 * Methods for the referential constraints I_S table.
 
45
 */
 
46
static plugin::InfoSchemaMethods *methods= NULL;
 
47
 
 
48
/*
 
49
 * processlist I_S table.
 
50
 */
 
51
static plugin::InfoSchemaTable *rc_table= NULL;
 
52
 
 
53
/**
 
54
 * Populate the vectors of columns for the I_S table.
 
55
 *
 
56
 * @return a pointer to a std::vector of Columns.
 
57
 */
 
58
vector<const plugin::ColumnInfo *> *ReferentialConstraintsIS::createColumns()
 
59
{
 
60
  if (columns == NULL)
 
61
  {
 
62
    columns= new vector<const plugin::ColumnInfo *>;
 
63
  }
 
64
  else
 
65
  {
 
66
    clearColumns(*columns);
 
67
  }
 
68
 
 
69
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_CATALOG",
 
70
                                            FN_REFLEN,
 
71
                                            DRIZZLE_TYPE_VARCHAR,
 
72
                                            0,
 
73
                                            1,
 
74
                                            "",
 
75
                                            OPEN_FULL_TABLE));
 
76
 
 
77
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_SCHEMA",
 
78
                                            NAME_CHAR_LEN,
 
79
                                            DRIZZLE_TYPE_VARCHAR,
 
80
                                            0,
 
81
                                            0,
 
82
                                            "",
 
83
                                            OPEN_FULL_TABLE));
 
84
 
 
85
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_NAME",
 
86
                                            NAME_CHAR_LEN,
 
87
                                            DRIZZLE_TYPE_VARCHAR,
 
88
                                            0,
 
89
                                            0,
 
90
                                            "",
 
91
                                            OPEN_FULL_TABLE));
 
92
 
 
93
  columns->push_back(new plugin::ColumnInfo("UNIQUE_CONSTRAINT_CATALOG",
 
94
                                            FN_REFLEN,
 
95
                                            DRIZZLE_TYPE_VARCHAR,
 
96
                                            0,
 
97
                                            1,
 
98
                                            "",
 
99
                                            OPEN_FULL_TABLE));
 
100
 
 
101
  columns->push_back(new plugin::ColumnInfo("UNIQUE_CONSTRAINT_SCHEMA",
 
102
                                            NAME_CHAR_LEN,
 
103
                                            DRIZZLE_TYPE_VARCHAR,
 
104
                                            0,
 
105
                                            0,
 
106
                                            "",
 
107
                                            OPEN_FULL_TABLE));
 
108
 
 
109
  columns->push_back(new plugin::ColumnInfo("UNIQUE_CONSTRAINT_NAME",
 
110
                                            NAME_CHAR_LEN,
 
111
                                            DRIZZLE_TYPE_VARCHAR,
 
112
                                            0,
 
113
                                            MY_I_S_MAYBE_NULL,
 
114
                                            "",
 
115
                                            OPEN_FULL_TABLE));
 
116
 
 
117
  columns->push_back(new plugin::ColumnInfo("MATCH_OPTION",
 
118
                                            NAME_CHAR_LEN,
 
119
                                            DRIZZLE_TYPE_VARCHAR,
 
120
                                            0,
 
121
                                            0,
 
122
                                            "",
 
123
                                            OPEN_FULL_TABLE));
 
124
 
 
125
  columns->push_back(new plugin::ColumnInfo("UPDATE_RULE",
 
126
                                            NAME_CHAR_LEN,
 
127
                                            DRIZZLE_TYPE_VARCHAR,
 
128
                                            0,
 
129
                                            0,
 
130
                                            "",
 
131
                                            OPEN_FULL_TABLE));
 
132
 
 
133
  columns->push_back(new plugin::ColumnInfo("DELETE_RULE",
 
134
                                            NAME_CHAR_LEN,
 
135
                                            DRIZZLE_TYPE_VARCHAR,
 
136
                                            0,
 
137
                                            0,
 
138
                                            "",
 
139
                                            OPEN_FULL_TABLE));
 
140
 
 
141
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
 
142
                                            NAME_CHAR_LEN,
 
143
                                            DRIZZLE_TYPE_VARCHAR,
 
144
                                            0,
 
145
                                            0,
 
146
                                            "",
 
147
                                            OPEN_FULL_TABLE));
 
148
 
 
149
  columns->push_back(new plugin::ColumnInfo("REFERENCED_TABLE_NAME",
 
150
                                            NAME_CHAR_LEN,
 
151
                                            DRIZZLE_TYPE_VARCHAR,
 
152
                                            0,
 
153
                                            0,
 
154
                                            "",
 
155
                                            OPEN_FULL_TABLE));
 
156
 
 
157
  return columns;
 
158
}
 
159
 
 
160
/**
 
161
 * Initialize the I_S table.
 
162
 *
 
163
 * @return a pointer to an I_S table
 
164
 */
 
165
plugin::InfoSchemaTable *ReferentialConstraintsIS::getTable()
 
166
{
 
167
  columns= createColumns();
 
168
 
 
169
  if (methods == NULL)
 
170
  {
 
171
    methods= new RefConstraintsISMethods();
 
172
  }
 
173
 
 
174
  if (rc_table == NULL)
 
175
  {
 
176
    rc_table= new plugin::InfoSchemaTable("REFERENTIAL_CONSTRAINTS",
 
177
                                          *columns,
 
178
                                          1, 9, false, true,
 
179
                                          OPEN_TABLE_ONLY,
 
180
                                          methods);
 
181
  }
 
182
 
 
183
  return rc_table;
 
184
}
 
185
 
 
186
/**
 
187
 * Delete memory allocated for the table, columns and methods.
 
188
 */
 
189
void ReferentialConstraintsIS::cleanup()
 
190
{
 
191
  clearColumns(*columns);
 
192
  delete rc_table;
 
193
  delete methods;
 
194
  delete columns;
 
195
}
 
196
 
 
197
int
 
198
RefConstraintsISMethods::processTable(Session *session, 
 
199
                                      TableList *tables,
 
200
                                      Table *table, 
 
201
                                      bool res,
 
202
                                      LEX_STRING *db_name, 
 
203
                                      LEX_STRING *table_name)
 
204
  const
 
205
{
 
206
  const CHARSET_INFO * const cs= system_charset_info;
 
207
 
 
208
  if (res)
 
209
  {
 
210
    if (session->is_error())
 
211
    {
 
212
      push_warning(session, 
 
213
                   DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
214
                   session->main_da.sql_errno(), 
 
215
                   session->main_da.message());
 
216
    }
 
217
    session->clear_error();
 
218
    return 0;
 
219
  }
 
220
 
 
221
  {
 
222
    List<FOREIGN_KEY_INFO> f_key_list;
 
223
    Table *show_table= tables->table;
 
224
    show_table->cursor->info(HA_STATUS_VARIABLE |
 
225
                             HA_STATUS_NO_LOCK |
 
226
                             HA_STATUS_TIME);
 
227
 
 
228
    show_table->cursor->get_foreign_key_list(session, &f_key_list);
 
229
    FOREIGN_KEY_INFO *f_key_info;
 
230
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
 
231
    while ((f_key_info= it++))
 
232
    {
 
233
      table->restoreRecordAsDefault();
 
234
      table->field[1]->store(db_name->str, db_name->length, cs);
 
235
      table->field[9]->store(table_name->str, table_name->length, cs);
 
236
      table->field[2]->store(f_key_info->forein_id->str,
 
237
                             f_key_info->forein_id->length, cs);
 
238
      table->field[4]->store(f_key_info->referenced_db->str,
 
239
                             f_key_info->referenced_db->length, cs);
 
240
      table->field[10]->store(f_key_info->referenced_table->str,
 
241
                              f_key_info->referenced_table->length, cs);
 
242
      if (f_key_info->referenced_key_name)
 
243
      {
 
244
        table->field[5]->store(f_key_info->referenced_key_name->str,
 
245
                               f_key_info->referenced_key_name->length, cs);
 
246
        table->field[5]->set_notnull();
 
247
      }
 
248
      else
 
249
      {
 
250
        table->field[5]->set_null();
 
251
      }
 
252
      table->field[6]->store(STRING_WITH_LEN("NONE"), cs);
 
253
      table->field[7]->store(f_key_info->update_method->str,
 
254
                             f_key_info->update_method->length, cs);
 
255
      table->field[8]->store(f_key_info->delete_method->str,
 
256
                             f_key_info->delete_method->length, cs);
 
257
      if (schema_table_store_record(session, table))
 
258
      {
 
259
        return 1;
 
260
      }
 
261
    }
 
262
  }
 
263
  return 0;
 
264
}
 
265