1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
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.
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.
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
23
* statistics I_S table methods.
27
#include "drizzled/session.h"
28
#include "drizzled/show.h"
29
#include "drizzled/tztime.h"
30
#include "drizzled/global_charset_info.h"
33
#include "helper_methods.h"
34
#include "statistics.h"
38
using namespace drizzled;
42
* Vectors of columns for the statistics I_S table.
44
static vector<const plugin::ColumnInfo *> *columns= NULL;
47
* Methods for the statistics I_S table.
49
static plugin::InfoSchemaMethods *methods= NULL;
52
* statistics I_S table.
54
static plugin::InfoSchemaTable *stats_table= NULL;
57
* Populate the vectors of columns for the I_S table.
59
* @return a pointer to a std::vector of Columns.
61
vector<const plugin::ColumnInfo *> *StatisticsIS::createColumns()
65
columns= new vector<const plugin::ColumnInfo *>;
69
clearColumns(*columns);
72
columns->push_back(new plugin::ColumnInfo("TABLE_CATALOG",
79
columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
86
columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
93
columns->push_back(new plugin::ColumnInfo("NON_UNIQUE",
95
DRIZZLE_TYPE_LONGLONG,
100
columns->push_back(new plugin::ColumnInfo("INDEX_SCHEMA",
102
DRIZZLE_TYPE_VARCHAR,
107
columns->push_back(new plugin::ColumnInfo("INDEX_NAME",
109
DRIZZLE_TYPE_VARCHAR,
114
columns->push_back(new plugin::ColumnInfo("SEQ_IN_INDEX",
116
DRIZZLE_TYPE_LONGLONG,
121
columns->push_back(new plugin::ColumnInfo("COLUMN_NAME",
123
DRIZZLE_TYPE_VARCHAR,
128
columns->push_back(new plugin::ColumnInfo("COLLATION",
130
DRIZZLE_TYPE_VARCHAR,
135
columns->push_back(new plugin::ColumnInfo("CARDINALITY",
136
MY_INT64_NUM_DECIMAL_DIGITS,
137
DRIZZLE_TYPE_LONGLONG,
142
columns->push_back(new plugin::ColumnInfo("SUB_PART",
144
DRIZZLE_TYPE_LONGLONG,
149
columns->push_back(new plugin::ColumnInfo("PACKED",
151
DRIZZLE_TYPE_VARCHAR,
156
columns->push_back(new plugin::ColumnInfo("NULLABLE",
158
DRIZZLE_TYPE_VARCHAR,
163
columns->push_back(new plugin::ColumnInfo("INDEX_TYPE",
165
DRIZZLE_TYPE_VARCHAR,
170
columns->push_back(new plugin::ColumnInfo("COMMENT",
172
DRIZZLE_TYPE_VARCHAR,
177
columns->push_back(new plugin::ColumnInfo("INDEX_COMMENT",
178
INDEX_COMMENT_MAXLEN,
179
DRIZZLE_TYPE_VARCHAR,
188
* Initialize the I_S table.
190
* @return a pointer to an I_S table
192
plugin::InfoSchemaTable *StatisticsIS::getTable()
194
columns= createColumns();
198
methods= new StatsISMethods();
201
if (stats_table == NULL)
203
stats_table= new plugin::InfoSchemaTable("STATISTICS",
214
* Delete memory allocated for the table, columns and methods.
216
void StatisticsIS::cleanup()
218
clearColumns(*columns);
224
int StatsISMethods::processTable(plugin::InfoSchemaTable *store_table,
230
LEX_STRING *table_name)
232
const CHARSET_INFO * const cs= system_charset_info;
235
if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
238
I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
239
rather than in SHOW KEYS
241
if (session->is_error())
243
push_warning(session,
244
DRIZZLE_ERROR::WARN_LEVEL_WARN,
245
session->main_da.sql_errno(),
246
session->main_da.message());
248
session->clear_error();
255
Table *show_table= tables->table;
256
KEY *key_info=show_table->s->key_info;
257
if (show_table->cursor)
259
show_table->cursor->info(HA_STATUS_VARIABLE |
263
for (uint32_t i= 0; i < show_table->s->keys; i++, key_info++)
265
KEY_PART_INFO *key_part= key_info->key_part;
267
for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
269
table->restoreRecordAsDefault();
270
table->setWriteSet(1);
271
table->setWriteSet(2);
272
table->setWriteSet(3);
273
table->setWriteSet(4);
274
table->setWriteSet(5);
275
table->setWriteSet(6);
276
table->setWriteSet(8);
277
table->setWriteSet(9);
278
table->setWriteSet(10);
279
table->setWriteSet(12);
280
table->setWriteSet(13);
281
table->setWriteSet(14);
282
table->setWriteSet(15);
283
table->field[1]->store(db_name->str, db_name->length, cs);
284
table->field[2]->store(table_name->str, table_name->length, cs);
285
table->field[3]->store((int64_t) ((key_info->flags &
286
HA_NOSAME) ? 0 : 1), true);
287
table->field[4]->store(db_name->str, db_name->length, cs);
288
table->field[5]->store(key_info->name, strlen(key_info->name), cs);
289
table->field[6]->store((int64_t) (j+1), true);
290
str= (key_part->field ? key_part->field->field_name :
292
table->field[7]->store(str, strlen(str), cs);
293
if (show_table->cursor)
295
if (show_table->index_flags(i) & HA_READ_ORDER)
297
table->field[8]->store(((key_part->key_part_flag &
300
table->field[8]->set_notnull();
302
KEY *key= show_table->key_info + i;
303
if (key->rec_per_key[j])
305
ha_rows records=(show_table->cursor->stats.records /
306
key->rec_per_key[j]);
307
table->field[9]->store((int64_t) records, true);
308
table->field[9]->set_notnull();
310
str= show_table->cursor->index_type(i);
311
table->field[13]->store(str, strlen(str), cs);
313
if ((key_part->field &&
315
show_table->s->field[key_part->fieldnr-1]->key_length()))
317
table->field[10]->store((int64_t) key_part->length /
318
key_part->field->charset()->mbmaxlen, true);
319
table->field[10]->set_notnull();
321
uint32_t flags= key_part->field ? key_part->field->flags : 0;
322
const char *pos= (char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
323
table->field[12]->store(pos, strlen(pos), cs);
324
if (!show_table->s->keys_in_use.test(i))
326
table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
330
table->field[14]->store("", 0, cs);
332
table->field[14]->set_notnull();
333
assert(test(key_info->flags & HA_USES_COMMENT) ==
334
(key_info->comment.length > 0));
335
if (key_info->flags & HA_USES_COMMENT)
337
table->field[15]->store(key_info->comment.str,
338
key_info->comment.length, cs);
340
store_table->addRow(table->record[0], table->s->reclength);