~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/table_constraints.cc

  • Committer: Brian Aker
  • Date: 2009-11-18 23:28:30 UTC
  • mfrom: (1215.2.25 is-split)
  • Revision ID: brian@gaz-20091118232830-v28y7o26squz3c9c
Merge of Padraig

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