~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/statistics.cc

Does not work (compile issue in plugin).

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