~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/statistics.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
 *   statistics I_S table methods.
 
24
 */
 
25
 
 
26
#include "drizzled/server_includes.h"
 
27
#include "drizzled/session.h"
 
28
#include "drizzled/show.h"
 
29
#include "drizzled/tztime.h"
 
30
 
 
31
#include "helper_methods.h"
 
32
#include "statistics.h"
 
33
 
 
34
#include <vector>
 
35
 
 
36
using namespace drizzled;
 
37
using namespace std;
 
38
 
 
39
/*
 
40
 * Vectors of columns for the statistics I_S table.
 
41
 */
 
42
static vector<const plugin::ColumnInfo *> *columns= NULL;
 
43
 
 
44
/*
 
45
 * Methods for the statistics I_S table.
 
46
 */
 
47
static plugin::InfoSchemaMethods *methods= NULL;
 
48
 
 
49
/*
 
50
 * statistics I_S table.
 
51
 */
 
52
static plugin::InfoSchemaTable *stats_table= NULL;
 
53
 
 
54
/**
 
55
 * Populate the vectors of columns for the I_S table.
 
56
 *
 
57
 * @return a pointer to a std::vector of Columns.
 
58
 */
 
59
vector<const plugin::ColumnInfo *> *StatisticsIS::createColumns()
 
60
{
 
61
  if (columns == NULL)
 
62
  {
 
63
    columns= new vector<const plugin::ColumnInfo *>;
 
64
  }
 
65
  else
 
66
  {
 
67
    clearColumns(*columns);
 
68
  }
 
69
 
 
70
  columns->push_back(new plugin::ColumnInfo("TABLE_CATALOG",
 
71
                                            FN_REFLEN,
 
72
                                            DRIZZLE_TYPE_VARCHAR,
 
73
                                            0,
 
74
                                            1,
 
75
                                            ""));
 
76
 
 
77
  columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
 
78
                                            NAME_CHAR_LEN,
 
79
                                            DRIZZLE_TYPE_VARCHAR,
 
80
                                            0,
 
81
                                            0,
 
82
                                            ""));
 
83
  
 
84
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
 
85
                                            NAME_CHAR_LEN,
 
86
                                            DRIZZLE_TYPE_VARCHAR,
 
87
                                            0,
 
88
                                            0,
 
89
                                            "Table"));
 
90
 
 
91
  columns->push_back(new plugin::ColumnInfo("NON_UNIQUE",
 
92
                                            1,
 
93
                                            DRIZZLE_TYPE_LONGLONG,
 
94
                                            0,
 
95
                                            0,
 
96
                                            "Non_unique"));
 
97
 
 
98
  columns->push_back(new plugin::ColumnInfo("INDEX_SCHEMA",
 
99
                                            NAME_CHAR_LEN,
 
100
                                            DRIZZLE_TYPE_VARCHAR,
 
101
                                            0,
 
102
                                            0,
 
103
                                            ""));
 
104
 
 
105
  columns->push_back(new plugin::ColumnInfo("INDEX_NAME",
 
106
                                            NAME_CHAR_LEN,
 
107
                                            DRIZZLE_TYPE_VARCHAR,
 
108
                                            0,
 
109
                                            0,
 
110
                                            "Key_name"));
 
111
 
 
112
  columns->push_back(new plugin::ColumnInfo("SEQ_IN_INDEX",
 
113
                                            2,
 
114
                                            DRIZZLE_TYPE_LONGLONG,
 
115
                                            0,
 
116
                                            0,
 
117
                                            "Seq_in_index"));
 
118
 
 
119
  columns->push_back(new plugin::ColumnInfo("COLUMN_NAME",
 
120
                                            NAME_CHAR_LEN,
 
121
                                            DRIZZLE_TYPE_VARCHAR,
 
122
                                            0,
 
123
                                            0,
 
124
                                            "Column_name"));
 
125
 
 
126
  columns->push_back(new plugin::ColumnInfo("COLLATION",
 
127
                                            1,
 
128
                                            DRIZZLE_TYPE_VARCHAR,
 
129
                                            0,
 
130
                                            1,
 
131
                                            "Collation"));
 
132
 
 
133
  columns->push_back(new plugin::ColumnInfo("CARDINALITY",
 
134
                                            MY_INT64_NUM_DECIMAL_DIGITS,
 
135
                                            DRIZZLE_TYPE_LONGLONG,
 
136
                                            0,
 
137
                                            1,
 
138
                                            "Cardinality"));
 
139
 
 
140
  columns->push_back(new plugin::ColumnInfo("SUB_PART",
 
141
                                            3,
 
142
                                            DRIZZLE_TYPE_LONGLONG,
 
143
                                            0,
 
144
                                            1,
 
145
                                            "Sub_part"));
 
146
 
 
147
  columns->push_back(new plugin::ColumnInfo("PACKED",
 
148
                                            10,
 
149
                                            DRIZZLE_TYPE_VARCHAR,
 
150
                                            0,
 
151
                                            1,
 
152
                                            "Packed"));
 
153
 
 
154
  columns->push_back(new plugin::ColumnInfo("NULLABLE",
 
155
                                            3,
 
156
                                            DRIZZLE_TYPE_VARCHAR,
 
157
                                            0,
 
158
                                            0,
 
159
                                            "Null"));
 
160
 
 
161
  columns->push_back(new plugin::ColumnInfo("INDEX_TYPE",
 
162
                                            16,
 
163
                                            DRIZZLE_TYPE_VARCHAR,
 
164
                                            0,
 
165
                                            0,
 
166
                                            "Index_type"));
 
167
 
 
168
  columns->push_back(new plugin::ColumnInfo("COMMENT",
 
169
                                            16,
 
170
                                            DRIZZLE_TYPE_VARCHAR,
 
171
                                            0,
 
172
                                            1,
 
173
                                            "Comment"));
 
174
 
 
175
  columns->push_back(new plugin::ColumnInfo("INDEX_COMMENT",
 
176
                                            INDEX_COMMENT_MAXLEN,
 
177
                                            DRIZZLE_TYPE_VARCHAR,
 
178
                                            0,
 
179
                                            0,
 
180
                                            "Index_Comment"));
 
181
 
 
182
  return columns;
 
183
}
 
184
 
 
185
/**
 
186
 * Initialize the I_S table.
 
187
 *
 
188
 * @return a pointer to an I_S table
 
189
 */
 
190
plugin::InfoSchemaTable *StatisticsIS::getTable()
 
191
{
 
192
  columns= createColumns();
 
193
 
 
194
  if (methods == NULL)
 
195
  {
 
196
    methods= new StatsISMethods();
 
197
  }
 
198
 
 
199
  if (stats_table == NULL)
 
200
  {
 
201
    stats_table= new plugin::InfoSchemaTable("STATISTICS",
 
202
                                             *columns,
 
203
                                             1, 2, false, true,
 
204
                                             OPEN_TABLE_ONLY | OPTIMIZE_I_S_TABLE,
 
205
                                             methods);
 
206
  }
 
207
 
 
208
  return stats_table;
 
209
}
 
210
 
 
211
/**
 
212
 * Delete memory allocated for the table, columns and methods.
 
213
 */
 
214
void StatisticsIS::cleanup()
 
215
{
 
216
  clearColumns(*columns);
 
217
  delete stats_table;
 
218
  delete methods;
 
219
  delete columns;
 
220
}
 
221
 
 
222
int StatsISMethods::processTable(plugin::InfoSchemaTable *store_table,
 
223
                                 Session *session, 
 
224
                                 TableList *tables,
 
225
                                 Table *table, 
 
226
                                 bool res,
 
227
                                 LEX_STRING *db_name,
 
228
                                 LEX_STRING *table_name)
 
229
{
 
230
  const CHARSET_INFO * const cs= system_charset_info;
 
231
  if (res)
 
232
  {
 
233
    if (session->lex->sql_command != SQLCOM_SHOW_KEYS)
 
234
    {
 
235
      /*
 
236
        I.e. we are in SELECT FROM INFORMATION_SCHEMA.STATISTICS
 
237
        rather than in SHOW KEYS
 
238
      */
 
239
      if (session->is_error())
 
240
      {
 
241
        push_warning(session, 
 
242
                     DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
243
                     session->main_da.sql_errno(), 
 
244
                     session->main_da.message());
 
245
      }
 
246
      session->clear_error();
 
247
      res= 0;
 
248
    }
 
249
    return res;
 
250
  }
 
251
  else
 
252
  {
 
253
    Table *show_table= tables->table;
 
254
    KEY *key_info=show_table->s->key_info;
 
255
    if (show_table->cursor)
 
256
    {
 
257
      show_table->cursor->info(HA_STATUS_VARIABLE |
 
258
                               HA_STATUS_NO_LOCK |
 
259
                               HA_STATUS_TIME);
 
260
    }
 
261
    for (uint32_t i= 0; i < show_table->s->keys; i++, key_info++)
 
262
    {
 
263
      KEY_PART_INFO *key_part= key_info->key_part;
 
264
      const char *str;
 
265
      for (uint32_t j= 0; j < key_info->key_parts; j++, key_part++)
 
266
      {
 
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 :
 
289
             "?unknown field?");
 
290
        table->field[7]->store(str, strlen(str), cs);
 
291
        if (show_table->cursor)
 
292
        {
 
293
          if (show_table->index_flags(i) & HA_READ_ORDER)
 
294
          {
 
295
            table->field[8]->store(((key_part->key_part_flag &
 
296
                                     HA_REVERSE_SORT) ?
 
297
                                    "D" : "A"), 1, cs);
 
298
            table->field[8]->set_notnull();
 
299
          }
 
300
          KEY *key= show_table->key_info + i;
 
301
          if (key->rec_per_key[j])
 
302
          {
 
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();
 
307
          }
 
308
          str= show_table->cursor->index_type(i);
 
309
          table->field[13]->store(str, strlen(str), cs);
 
310
        }
 
311
        if ((key_part->field &&
 
312
             key_part->length !=
 
313
             show_table->s->field[key_part->fieldnr-1]->key_length()))
 
314
        {
 
315
          table->field[10]->store((int64_t) key_part->length /
 
316
                                  key_part->field->charset()->mbmaxlen, true);
 
317
          table->field[10]->set_notnull();
 
318
        }
 
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))
 
323
        {
 
324
          table->field[14]->store(STRING_WITH_LEN("disabled"), cs);
 
325
        }
 
326
        else
 
327
        {
 
328
          table->field[14]->store("", 0, cs);
 
329
        }
 
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)
 
334
        {
 
335
          table->field[15]->store(key_info->comment.str,
 
336
                                  key_info->comment.length, cs);
 
337
        }
 
338
        store_table->addRow(table->record[0], table->s->reclength);
 
339
      }
 
340
    }
 
341
  }
 
342
  return res;
 
343
}