~drizzle-trunk/drizzle/development

1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
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
 *   key column usage I_S table methods.
24
 */
25
1215.2.22 by Padraig O'Sullivan
Removed the info_schema_methods files and created a helper_methods header
26
#include "drizzled/server_includes.h"
27
#include "drizzled/session.h"
28
#include "drizzled/show.h"
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
29
1215.2.22 by Padraig O'Sullivan
Removed the info_schema_methods files and created a helper_methods header
30
#include "helper_methods.h"
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
31
#include "key_column_usage.h"
32
33
#include <vector>
34
35
using namespace drizzled;
36
using namespace std;
37
38
/*
39
 * Vectors of columns for the key column usage I_S table.
40
 */
41
static vector<const plugin::ColumnInfo *> *columns= NULL;
42
43
/*
44
 * Methods for the key column usage I_S table.
45
 */
46
static plugin::InfoSchemaMethods *methods= NULL;
47
48
/*
49
 * key column usage I_S table.
50
 */
51
static plugin::InfoSchemaTable *key_col_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 *> *KeyColumnUsageIS::createColumns()
59
{
60
  if (columns == NULL)
61
  {
62
    columns= new vector<const plugin::ColumnInfo *>;
63
  }
64
  else
65
  {
1215.2.25 by Padraig O'Sullivan
Resolved valgrind warnings in the I_S plugin.
66
    clearColumns(*columns);
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
67
  }
68
69
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_CATALOG",
70
                                            FN_REFLEN,
71
                                            DRIZZLE_TYPE_VARCHAR,
72
                                            0,
73
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
74
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
75
76
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_SCHEMA",
77
                                            NAME_CHAR_LEN,
78
                                            DRIZZLE_TYPE_VARCHAR,
79
                                            0,
80
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
81
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
82
83
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_NAME",
84
                                            NAME_CHAR_LEN,
85
                                            DRIZZLE_TYPE_VARCHAR,
86
                                            0,
87
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
88
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
89
90
  columns->push_back(new plugin::ColumnInfo("TABLE_CATALOG",
91
                                            FN_REFLEN,
92
                                            DRIZZLE_TYPE_VARCHAR,
93
                                            0,
94
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
95
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
96
97
  columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
98
                                            NAME_CHAR_LEN,
99
                                            DRIZZLE_TYPE_VARCHAR,
100
                                            0,
101
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
102
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
103
104
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
105
                                            NAME_CHAR_LEN,
106
                                            DRIZZLE_TYPE_VARCHAR,
107
                                            0,
108
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
109
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
110
111
  columns->push_back(new plugin::ColumnInfo("COLUMN_NAME",
112
                                            NAME_CHAR_LEN,
113
                                            DRIZZLE_TYPE_VARCHAR,
114
                                            0,
115
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
116
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
117
118
  columns->push_back(new plugin::ColumnInfo("ORDINAL_POSITION",
119
                                            10,
120
                                            DRIZZLE_TYPE_LONGLONG,
121
                                            0,
122
                                            0,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
123
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
124
125
  columns->push_back(new plugin::ColumnInfo("POSITION_IN_UNIQUE_CONSTRAINT",
126
                                            10,
127
                                            DRIZZLE_TYPE_LONGLONG,
128
                                            0,
129
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
130
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
131
132
  columns->push_back(new plugin::ColumnInfo("REFERENCED_TABLE_SCHEMA",
133
                                            NAME_CHAR_LEN,
134
                                            DRIZZLE_TYPE_VARCHAR,
135
                                            0,
136
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
137
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
138
139
  columns->push_back(new plugin::ColumnInfo("REFERENCED_TABLE_NAME",
140
                                            NAME_CHAR_LEN,
141
                                            DRIZZLE_TYPE_VARCHAR,
142
                                            0,
143
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
144
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
145
146
  columns->push_back(new plugin::ColumnInfo("REFERENCED_COLUMN_NAME",
147
                                            NAME_CHAR_LEN,
148
                                            DRIZZLE_TYPE_VARCHAR,
149
                                            0,
150
                                            1,
1225.1.14 by Padraig O'Sullivan
Removed the redundant open table method parameter to columns associated with an I_S table.
151
                                            ""));
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
152
153
  return columns;
154
}
155
156
/**
157
 * Initialize the I_S table.
158
 *
159
 * @return a pointer to an I_S table
160
 */
161
plugin::InfoSchemaTable *KeyColumnUsageIS::getTable()
162
{
163
  columns= createColumns();
164
165
  if (methods == NULL)
166
  {
167
    methods= new KeyColUsageISMethods();
168
  }
169
170
  if (key_col_table == NULL)
171
  {
172
    key_col_table= new plugin::InfoSchemaTable("KEY_COLUMN_USAGE",
1215.2.7 by Padraig O'Sullivan
Added a header file include I forgot.
173
                                               *columns,
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
174
                                               4, 5, false, true,
175
                                               OPEN_TABLE_ONLY,
176
                                               methods);
177
  }
178
179
  return key_col_table;
180
}
181
182
/**
183
 * Delete memory allocated for the table, columns and methods.
184
 */
185
void KeyColumnUsageIS::cleanup()
186
{
187
  clearColumns(*columns);
188
  delete key_col_table;
189
  delete methods;
190
  delete columns;
191
}
192
1225.1.20 by Padraig O'Sullivan
Removed all remnants of schema_table from the TableList class. This cleans up a bunch of code.
193
int KeyColUsageISMethods::processTable(plugin::InfoSchemaTable *store_table,
1225.1.2 by Padraig O'Sullivan
Resolved issue with segmentation fault on some queries on some I_S tables. Needed to change the
194
                                       Session *session,
1215.2.7 by Padraig O'Sullivan
Added a header file include I forgot.
195
                                       TableList *tables,
196
                                       Table *table, bool res,
197
                                       LEX_STRING *db_name,
1225.1.2 by Padraig O'Sullivan
Resolved issue with segmentation fault on some queries on some I_S tables. Needed to change the
198
                                       LEX_STRING *table_name)
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
199
{
200
  if (res)
201
  {
202
    if (session->is_error())
203
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
204
                   session->main_da.sql_errno(), session->main_da.message());
205
    session->clear_error();
206
    return(0);
207
  }
208
  else
209
  {
210
    List<FOREIGN_KEY_INFO> f_key_list;
211
    Table *show_table= tables->table;
212
    KEY *key_info=show_table->key_info;
213
    uint32_t primary_key= show_table->s->primary_key;
214
    show_table->cursor->info(HA_STATUS_VARIABLE |
215
                             HA_STATUS_NO_LOCK |
216
                             HA_STATUS_TIME);
217
    for (uint32_t i=0 ; i < show_table->s->keys ; i++, key_info++)
218
    {
219
      if (i != primary_key && !(key_info->flags & HA_NOSAME))
220
      {
221
        continue;
222
      }
223
      uint32_t f_idx= 0;
224
      KEY_PART_INFO *key_part= key_info->key_part;
225
      for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
226
      {
227
        if (key_part->field)
228
        {
229
          f_idx++;
230
          table->restoreRecordAsDefault();
231
          store_key_column_usage(table, db_name, table_name,
232
                                 key_info->name,
233
                                 strlen(key_info->name),
234
                                 key_part->field->field_name,
235
                                 strlen(key_part->field->field_name),
236
                                 (int64_t) f_idx);
1225.1.20 by Padraig O'Sullivan
Removed all remnants of schema_table from the TableList class. This cleans up a bunch of code.
237
          store_table->addRow(table->record[0],
1225.1.1 by Padraig O'Sullivan
Removed special case I_S code paths. All queries are now going against the I_S engine.
238
                                       table->s->reclength);
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
239
        }
240
      }
241
    }
242
243
    show_table->cursor->get_foreign_key_list(session, &f_key_list);
244
    FOREIGN_KEY_INFO *f_key_info;
245
    List_iterator_fast<FOREIGN_KEY_INFO> fkey_it(f_key_list);
246
    while ((f_key_info= fkey_it++))
247
    {
248
      LEX_STRING *f_info;
249
      LEX_STRING *r_info;
250
      List_iterator_fast<LEX_STRING> it(f_key_info->foreign_fields),
251
        it1(f_key_info->referenced_fields);
252
      uint32_t f_idx= 0;
253
      while ((f_info= it++))
254
      {
255
        r_info= it1++;
256
        f_idx++;
257
        table->restoreRecordAsDefault();
258
        store_key_column_usage(table, db_name, table_name,
259
                               f_key_info->forein_id->str,
260
                               f_key_info->forein_id->length,
261
                               f_info->str, f_info->length,
262
                               (int64_t) f_idx);
1225.1.6 by Padraig O'Sullivan
Now we are correctly setting the bitmaps in the I_S methods before adding a row to an I_S table.
263
        table->setWriteSet(8);
264
        table->setWriteSet(9);
265
        table->setWriteSet(10);
266
        table->setWriteSet(11);
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
267
        table->field[8]->store((int64_t) f_idx, true);
268
        table->field[8]->set_notnull();
269
        table->field[9]->store(f_key_info->referenced_db->str,
270
                               f_key_info->referenced_db->length,
271
                               system_charset_info);
272
        table->field[9]->set_notnull();
273
        table->field[10]->store(f_key_info->referenced_table->str,
274
                                f_key_info->referenced_table->length,
275
                                system_charset_info);
276
        table->field[10]->set_notnull();
277
        table->field[11]->store(r_info->str, r_info->length,
278
                                system_charset_info);
279
        table->field[11]->set_notnull();
1225.1.20 by Padraig O'Sullivan
Removed all remnants of schema_table from the TableList class. This cleans up a bunch of code.
280
        store_table->addRow(table->record[0], table->s->reclength);
1215.2.6 by Padraig O'Sullivan
Split the key column usage I_S table out into its own implementation and
281
      }
282
    }
283
  }
284
  return (res);
285
}