~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/table_constraints.cc

Updated an include guard thanks to a nice catch during code review from Jay. Thanks Jay!

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
 
 
76
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_SCHEMA",
 
77
                                            NAME_CHAR_LEN,
 
78
                                            DRIZZLE_TYPE_VARCHAR,
 
79
                                            0,
 
80
                                            0,
 
81
                                            ""));
 
82
 
 
83
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_NAME",
 
84
                                            NAME_CHAR_LEN,
 
85
                                            DRIZZLE_TYPE_VARCHAR,
 
86
                                            0,
 
87
                                            0,
 
88
                                            ""));
 
89
 
 
90
  columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
 
91
                                            NAME_CHAR_LEN,
 
92
                                            DRIZZLE_TYPE_VARCHAR,
 
93
                                            0,
 
94
                                            0,
 
95
                                            ""));
 
96
 
 
97
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
 
98
                                            NAME_CHAR_LEN,
 
99
                                            DRIZZLE_TYPE_VARCHAR,
 
100
                                            0,
 
101
                                            0,
 
102
                                            ""));
 
103
 
 
104
  columns->push_back(new plugin::ColumnInfo("CONSTRAINT_TYPE",
 
105
                                            NAME_CHAR_LEN,
 
106
                                            DRIZZLE_TYPE_VARCHAR,
 
107
                                            0,
 
108
                                            0,
 
109
                                            ""));
 
110
  return columns;
 
111
}
 
112
 
 
113
/**
 
114
 * Initialize the I_S table.
 
115
 *
 
116
 * @return a pointer to an I_S table
 
117
 */
 
118
plugin::InfoSchemaTable *TableConstraintsIS::getTable()
 
119
{
 
120
  columns= createColumns();
 
121
 
 
122
  if (methods == NULL)
 
123
  {
 
124
    methods= new TabConstraintsISMethods();
 
125
  }
 
126
 
 
127
  if (tc_table == NULL)
 
128
  {
 
129
    tc_table= new plugin::InfoSchemaTable("TABLE_CONSTRAINTS",
 
130
                                          *columns,
 
131
                                          3, 4, false, true,
 
132
                                          OPEN_TABLE_ONLY,
 
133
                                          methods);
 
134
  }
 
135
 
 
136
  return tc_table;
 
137
}
 
138
 
 
139
/**
 
140
 * Delete memory allocated for the table, columns and methods.
 
141
 */
 
142
void TableConstraintsIS::cleanup()
 
143
{
 
144
  clearColumns(*columns);
 
145
  delete tc_table;
 
146
  delete methods;
 
147
  delete columns;
 
148
}
 
149
 
 
150
static bool store_constraints(Table *table, 
 
151
                              LEX_STRING *db_name,
 
152
                              LEX_STRING *table_name, 
 
153
                              const char *key_name,
 
154
                              uint32_t key_len, 
 
155
                              const char *con_type, 
 
156
                              uint32_t con_len,
 
157
                              plugin::InfoSchemaTable *schema_table)
 
158
{
 
159
  const CHARSET_INFO * const cs= system_charset_info;
 
160
  table->restoreRecordAsDefault();
 
161
  table->setWriteSet(1);
 
162
  table->setWriteSet(2);
 
163
  table->setWriteSet(3);
 
164
  table->setWriteSet(4);
 
165
  table->setWriteSet(5);
 
166
  table->field[1]->store(db_name->str, db_name->length, cs);
 
167
  table->field[2]->store(key_name, key_len, cs);
 
168
  table->field[3]->store(db_name->str, db_name->length, cs);
 
169
  table->field[4]->store(table_name->str, table_name->length, cs);
 
170
  table->field[5]->store(con_type, con_len, cs);
 
171
  schema_table->addRow(table->record[0], table->s->reclength);
 
172
  return false;
 
173
}
 
174
 
 
175
int TabConstraintsISMethods::processTable(plugin::InfoSchemaTable *store_table,
 
176
                                          Session *session, 
 
177
                                          TableList *tables,
 
178
                                          Table *table, 
 
179
                                          bool res,
 
180
                                          LEX_STRING *db_name,
 
181
                                          LEX_STRING *table_name)
 
182
{
 
183
  if (res)
 
184
  {
 
185
    if (session->is_error())
 
186
    {
 
187
      push_warning(session, 
 
188
                   DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
189
                   session->main_da.sql_errno(), 
 
190
                   session->main_da.message());
 
191
    }
 
192
    session->clear_error();
 
193
    return 0;
 
194
  }
 
195
  else
 
196
  {
 
197
    List<FOREIGN_KEY_INFO> f_key_list;
 
198
    Table *show_table= tables->table;
 
199
    KEY *key_info=show_table->key_info;
 
200
    uint32_t primary_key= show_table->s->primary_key;
 
201
    show_table->cursor->info(HA_STATUS_VARIABLE |
 
202
                             HA_STATUS_NO_LOCK |
 
203
                             HA_STATUS_TIME);
 
204
    for (uint32_t i= 0; i < show_table->s->keys; i++, key_info++)
 
205
    {
 
206
      if (i != primary_key && ! (key_info->flags & HA_NOSAME))
 
207
      {
 
208
        continue;
 
209
      }
 
210
 
 
211
      if (i == primary_key && is_primary_key(key_info))
 
212
      {
 
213
        if (store_constraints(table, 
 
214
                              db_name, 
 
215
                              table_name, 
 
216
                              key_info->name,
 
217
                              strlen(key_info->name),
 
218
                              STRING_WITH_LEN("PRIMARY KEY"),
 
219
                              store_table))
 
220
        {
 
221
          return 1;
 
222
        }
 
223
      }
 
224
      else if (key_info->flags & HA_NOSAME)
 
225
      {
 
226
        if (store_constraints(table, 
 
227
                              db_name, 
 
228
                              table_name, 
 
229
                              key_info->name,
 
230
                              strlen(key_info->name),
 
231
                              STRING_WITH_LEN("UNIQUE"),
 
232
                              store_table))
 
233
        {
 
234
          return 1;
 
235
        }
 
236
      }
 
237
    }
 
238
 
 
239
    show_table->cursor->get_foreign_key_list(session, &f_key_list);
 
240
    FOREIGN_KEY_INFO *f_key_info= NULL;
 
241
    List_iterator_fast<FOREIGN_KEY_INFO> it(f_key_list);
 
242
    while ((f_key_info= it++))
 
243
    {
 
244
      if (store_constraints(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
                            store_table))
 
251
      {
 
252
        return 1;
 
253
      }
 
254
    }
 
255
  }
 
256
  return res;
 
257
}
 
258