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.
26
#include "drizzled/server_includes.h"
27
#include "drizzled/session.h"
28
#include "drizzled/show.h"
29
#include "drizzled/tztime.h"
31
#include "helper_methods.h"
32
#include "statistics.h"
36
using namespace drizzled;
40
* Vectors of columns for the statistics I_S table.
42
static vector<const plugin::ColumnInfo *> *columns= NULL;
45
* Methods for the statistics I_S table.
47
static plugin::InfoSchemaMethods *methods= NULL;
50
* statistics I_S table.
52
static plugin::InfoSchemaTable *stats_table= NULL;
55
* Populate the vectors of columns for the I_S table.
57
* @return a pointer to a std::vector of Columns.
59
vector<const plugin::ColumnInfo *> *StatisticsIS::createColumns()
63
columns= new vector<const plugin::ColumnInfo *>;
67
clearColumns(*columns);
70
columns->push_back(new plugin::ColumnInfo("TABLE_CATALOG",
77
columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
84
columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
91
columns->push_back(new plugin::ColumnInfo("NON_UNIQUE",
93
DRIZZLE_TYPE_LONGLONG,
98
columns->push_back(new plugin::ColumnInfo("INDEX_SCHEMA",
100
DRIZZLE_TYPE_VARCHAR,
105
columns->push_back(new plugin::ColumnInfo("INDEX_NAME",
107
DRIZZLE_TYPE_VARCHAR,
112
columns->push_back(new plugin::ColumnInfo("SEQ_IN_INDEX",
114
DRIZZLE_TYPE_LONGLONG,
119
columns->push_back(new plugin::ColumnInfo("COLUMN_NAME",
121
DRIZZLE_TYPE_VARCHAR,
126
columns->push_back(new plugin::ColumnInfo("COLLATION",
128
DRIZZLE_TYPE_VARCHAR,
133
columns->push_back(new plugin::ColumnInfo("CARDINALITY",
134
MY_INT64_NUM_DECIMAL_DIGITS,
135
DRIZZLE_TYPE_LONGLONG,
140
columns->push_back(new plugin::ColumnInfo("SUB_PART",
142
DRIZZLE_TYPE_LONGLONG,
147
columns->push_back(new plugin::ColumnInfo("PACKED",
149
DRIZZLE_TYPE_VARCHAR,
154
columns->push_back(new plugin::ColumnInfo("NULLABLE",
156
DRIZZLE_TYPE_VARCHAR,
161
columns->push_back(new plugin::ColumnInfo("INDEX_TYPE",
163
DRIZZLE_TYPE_VARCHAR,
168
columns->push_back(new plugin::ColumnInfo("COMMENT",
170
DRIZZLE_TYPE_VARCHAR,
175
columns->push_back(new plugin::ColumnInfo("INDEX_COMMENT",
176
INDEX_COMMENT_MAXLEN,
177
DRIZZLE_TYPE_VARCHAR,
186
* Initialize the I_S table.
188
* @return a pointer to an I_S table
190
plugin::InfoSchemaTable *StatisticsIS::getTable()
192
columns= createColumns();
196
methods= new StatsISMethods();
199
if (stats_table == NULL)
201
stats_table= new plugin::InfoSchemaTable("STATISTICS",
204
OPEN_TABLE_ONLY | OPTIMIZE_I_S_TABLE,
212
* Delete memory allocated for the table, columns and methods.
214
void StatisticsIS::cleanup()
216
clearColumns(*columns);
222
int StatsISMethods::processTable(plugin::InfoSchemaTable *store_table,
228
LEX_STRING *table_name)
230
const CHARSET_INFO * const cs= system_charset_info;
233
if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
236
I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
237
rather than in SHOW KEYS
239
if (session->is_error())
241
push_warning(session,
242
DRIZZLE_ERROR::WARN_LEVEL_WARN,
243
session->main_da.sql_errno(),
244
session->main_da.message());
246
session->clear_error();
253
Table *show_table= tables->table;
254
KEY *key_info=show_table->s->key_info;
255
if (show_table->cursor)
257
show_table->cursor->info(HA_STATUS_VARIABLE |
261
for (uint32_t i= 0; i < show_table->s->keys; i++, key_info++)
263
KEY_PART_INFO *key_part= key_info->key_part;
265
for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
267
table->restoreRecordAsDefault();
268
table->setWriteSet(1);
269
table->setWriteSet(2);
270
table->setWriteSet(3);
271
table->setWriteSet(4);
272
table->setWriteSet(5);
273
table->setWriteSet(6);
274
table->setWriteSet(8);
275
table->setWriteSet(9);
276
table->setWriteSet(10);
277
table->setWriteSet(12);
278
table->setWriteSet(13);
279
table->setWriteSet(14);
280
table->setWriteSet(15);
281
table->field[1]->store(db_name->str, db_name->length, cs);
282
table->field[2]->store(table_name->str, table_name->length, cs);
283
table->field[3]->store((int64_t) ((key_info->flags &
284
HA_NOSAME) ? 0 : 1), true);
285
table->field[4]->store(db_name->str, db_name->length, cs);
286
table->field[5]->store(key_info->name, strlen(key_info->name), cs);
287
table->field[6]->store((int64_t) (j+1), true);
288
str= (key_part->field ? key_part->field->field_name :
290
table->field[7]->store(str, strlen(str), cs);
291
if (show_table->cursor)
293
if (show_table->index_flags(i) & HA_READ_ORDER)
295
table->field[8]->store(((key_part->key_part_flag &
298
table->field[8]->set_notnull();
300
KEY *key= show_table->key_info + i;
301
if (key->rec_per_key[j])
303
ha_rows records=(show_table->cursor->stats.records /
304
key->rec_per_key[j]);
305
table->field[9]->store((int64_t) records, true);
306
table->field[9]->set_notnull();
308
str= show_table->cursor->index_type(i);
309
table->field[13]->store(str, strlen(str), cs);
311
if ((key_part->field &&
313
show_table->s->field[key_part->fieldnr-1]->key_length()))
315
table->field[10]->store((int64_t) key_part->length /
316
key_part->field->charset()->mbmaxlen, true);
317
table->field[10]->set_notnull();
319
uint32_t flags= key_part->field ? key_part->field->flags : 0;
320
const char *pos= (char*) ((flags & NOT_NULL_FLAG) ? "" : "YES");
321
table->field[12]->store(pos, strlen(pos), cs);
322
if (!show_table->s->keys_in_use.test(i))
324
table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
328
table->field[14]->store("", 0, cs);
330
table->field[14]->set_notnull();
331
assert(test(key_info->flags & HA_USES_COMMENT) ==
332
(key_info->comment.length > 0));
333
if (key_info->flags & HA_USES_COMMENT)
335
table->field[15]->store(key_info->comment.str,
336
key_info->comment.length, cs);
338
store_table->addRow(table->record[0], table->s->reclength);