~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/table_constraints.cc

Merge in security refactor.

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
 *   table constraints I_S table methods.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "drizzled/session.h"
 
28
#include "drizzled/show.h"
 
29
#include "drizzled/sql_table.h"
 
30
#include "drizzled/global_charset_info.h"
 
31
 
 
32
 
 
33
#include "helper_methods.h"
 
34
#include "table_constraints.h"
 
35
 
 
36
#include <vector>
 
37
 
 
38
using namespace drizzled;
 
39
using namespace std;
 
40
 
 
41
/*
 
42
 * Vectors of columns for the table constraints I_S table.
 
43
 */
 
44
static vector<const plugin::ColumnInfo *> *columns= NULL;
 
45
 
 
46
/*
 
47
 * Methods for the table constraints I_S table.
 
48
 */
 
49
static plugin::InfoSchemaMethods *methods= NULL;
 
50
 
 
51
/*
 
52
 * table constraints I_S table.
 
53
 */
 
54
static plugin::InfoSchemaTable *tc_table= NULL;
 
55
 
 
56
/**
 
57
 * Populate the vectors of columns for the I_S table.
 
58
 *
 
59
 * @return a pointer to a std::vector of Columns.
 
60
 */
 
61
vector<const plugin::ColumnInfo *> *TableConstraintsIS::createColumns()
 
62
{
 
63
  if (columns == NULL)
 
64
  {
 
65
    columns= new vector<const plugin::ColumnInfo *>;
 
66
  }
 
67
  else
 
68
  {
 
69
    clearColumns(*columns);
 
70
  }
 
71
 
 
72
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_CATALOG",
 
73
                                            FN_REFLEN,
 
74
                                            DRIZZLE_TYPE_VARCHAR,
 
75
                                            0,
 
76
                                            1,
 
77
                                            ""));
 
78
 
 
79
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_SCHEMA",
 
80
                                            NAME_CHAR_LEN,
 
81
                                            DRIZZLE_TYPE_VARCHAR,
 
82
                                            0,
 
83
                                            0,
 
84
                                            ""));
 
85
 
 
86
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_NAME",
 
87
                                            NAME_CHAR_LEN,
 
88
                                            DRIZZLE_TYPE_VARCHAR,
 
89
                                            0,
 
90
                                            0,
 
91
                                            ""));
 
92
 
 
93
  columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
 
94
                                            NAME_CHAR_LEN,
 
95
                                            DRIZZLE_TYPE_VARCHAR,
 
96
                                            0,
 
97
                                            0,
 
98
                                            ""));
 
99
 
 
100
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
 
101
                                            NAME_CHAR_LEN,
 
102
                                            DRIZZLE_TYPE_VARCHAR,
 
103
                                            0,
 
104
                                            0,
 
105
                                            ""));
 
106
 
 
107
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_TYPE",
 
108
                                            NAME_CHAR_LEN,
 
109
                                            DRIZZLE_TYPE_VARCHAR,
 
110
                                            0,
 
111
                                            0,
 
112
                                            ""));
 
113
  return columns;
 
114
}
 
115
 
 
116
/**
 
117
 * Initialize the I_S table.
 
118
 *
 
119
 * @return a pointer to an I_S table
 
120
 */
 
121
plugin::InfoSchemaTable *TableConstraintsIS::getTable()
 
122
{
 
123
  columns= createColumns();
 
124
 
 
125
  if (methods == NULL)
 
126
  {
 
127
    methods= new TabConstraintsISMethods();
 
128
  }
 
129
 
 
130
  if (tc_table == NULL)
 
131
  {
 
132
    tc_table= new plugin::InfoSchemaTable("TABLE_CONSTRAINTS",
 
133
                                          *columns,
 
134
                                          3, 4, false, true,
 
135
                                          0,
 
136
                                          methods);
 
137
  }
 
138
 
 
139
  return tc_table;
 
140
}
 
141
 
 
142
/**
 
143
 * Delete memory allocated for the table, columns and methods.
 
144
 */
 
145
void TableConstraintsIS::cleanup()
 
146
{
 
147
  clearColumns(*columns);
 
148
  delete tc_table;
 
149
  delete methods;
 
150
  delete columns;
 
151
}
 
152
 
 
153
static bool store_constraints(Table *table, 
 
154
                              LEX_STRING *db_name,
 
155
                              LEX_STRING *table_name, 
 
156
                              const char *key_name,
 
157
                              uint32_t key_len, 
 
158
                              const char *con_type, 
 
159
                              uint32_t con_len,
 
160
                              plugin::InfoSchemaTable *schema_table)
 
161
{
 
162
  const CHARSET_INFO * const cs= system_charset_info;
 
163
  table->restoreRecordAsDefault();
 
164
  table->setWriteSet(1);
 
165
  table->setWriteSet(2);
 
166
  table->setWriteSet(3);
 
167
  table->setWriteSet(4);
 
168
  table->setWriteSet(5);
 
169
  table->field[1]->store(db_name->str, db_name->length, cs);
 
170
  table->field[2]->store(key_name, key_len, cs);
 
171
  table->field[3]->store(db_name->str, db_name->length, cs);
 
172
  table->field[4]->store(table_name->str, table_name->length, cs);
 
173
  table->field[5]->store(con_type, con_len, cs);
 
174
  schema_table->addRow(table->record[0], table->s->reclength);
 
175
  return false;
 
176
}
 
177
 
 
178
int TabConstraintsISMethods::processTable(plugin::InfoSchemaTable *store_table,
 
179
                                          Session *session, 
 
180
                                          TableList *tables,
 
181
                                          Table *table, 
 
182
                                          bool res,
 
183
                                          LEX_STRING *db_name,
 
184
                                          LEX_STRING *table_name)
 
185
{
 
186
  if (res)
 
187
  {
 
188
    if (session->is_error())
 
189
    {
 
190
      push_warning(session, 
 
191
                   DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
192
                   session->main_da.sql_errno(), 
 
193
                   session->main_da.message());
 
194
    }
 
195
    session->clear_error();
 
196
    return 0;
 
197
  }
 
198
  else
 
199
  {
 
200
    List<FOREIGN_KEY_INFO> f_key_list;
 
201
    Table *show_table= tables->table;
 
202
    KEY *key_info=show_table->key_info;
 
203
    uint32_t primary_key= show_table->s->primary_key;
 
204
    show_table->cursor->info(HA_STATUS_VARIABLE |
 
205
                             HA_STATUS_NO_LOCK |
 
206
                             HA_STATUS_TIME);
 
207
    for (uint32_t i= 0; i < show_table->s->keys; i++, key_info++)
 
208
    {
 
209
      if (i != primary_key && ! (key_info->flags & HA_NOSAME))
 
210
      {
 
211
        continue;
 
212
      }
 
213
 
 
214
      if (i == primary_key && is_primary_key(key_info))
 
215
      {
 
216
        if (store_constraints(table, 
 
217
                              db_name, 
 
218
                              table_name, 
 
219
                              key_info->name,
 
220
                              strlen(key_info->name),
 
221
                              STRING_WITH_LEN("PRIMARY KEY"),
 
222
                              store_table))
 
223
        {
 
224
          return 1;
 
225
        }
 
226
      }
 
227
      else if (key_info->flags & HA_NOSAME)
 
228
      {
 
229
        if (store_constraints(table, 
 
230
                              db_name, 
 
231
                              table_name, 
 
232
                              key_info->name,
 
233
                              strlen(key_info->name),
 
234
                              STRING_WITH_LEN("UNIQUE"),
 
235
                              store_table))
 
236
        {
 
237
          return 1;
 
238
        }
 
239
      }
 
240
    }
 
241
 
 
242
    show_table->cursor->get_foreign_key_list(session, &f_key_list);
 
243
    FOREIGN_KEY_INFO *f_key_info= NULL;
 
244
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
 
245
    while ((f_key_info= it++))
 
246
    {
 
247
      if (store_constraints(table, 
 
248
                            db_name, 
 
249
                            table_name,
 
250
                            f_key_info->forein_id->str,
 
251
                            strlen(f_key_info->forein_id->str),
 
252
                            "FOREIGN KEY", 11,
 
253
                            store_table))
 
254
      {
 
255
        return 1;
 
256
      }
 
257
    }
 
258
  }
 
259
  return res;
 
260
}
 
261