~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/tables.cc

Updating from additional schemas added.

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
 
 *   tables 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/internal/m_string.h"
31
 
 
32
 
#include "helper_methods.h"
33
 
#include "tables.h"
34
 
 
35
 
#include <vector>
36
 
 
37
 
using namespace drizzled;
38
 
using namespace std;
39
 
 
40
 
/*
41
 
 * Vectors of columns for the tables I_S table.
42
 
 */
43
 
static vector<const plugin::ColumnInfo *> *columns= NULL;
44
 
 
45
 
/*
46
 
 * Methods for the tables I_S table.
47
 
 */
48
 
static plugin::InfoSchemaMethods *methods= NULL;
49
 
 
50
 
/*
51
 
 * tables I_S table.
52
 
 */
53
 
static plugin::InfoSchemaTable *tbls_table= NULL;
54
 
 
55
 
/**
56
 
 * Populate the vectors of columns for the I_S table.
57
 
 *
58
 
 * @return a pointer to a std::vector of Columns.
59
 
 */
60
 
vector<const plugin::ColumnInfo *> *TablesIS::createColumns()
61
 
{
62
 
  if (columns == NULL)
63
 
  {
64
 
    columns= new vector<const plugin::ColumnInfo *>;
65
 
  }
66
 
  else
67
 
  {
68
 
    clearColumns(*columns);
69
 
  }
70
 
 
71
 
  columns->push_back(new plugin::ColumnInfo("TABLE_CATALOG",
72
 
                                            FN_REFLEN,
73
 
                                            DRIZZLE_TYPE_VARCHAR,
74
 
                                            0,
75
 
                                            1,
76
 
                                            ""));
77
 
 
78
 
  columns->push_back(new plugin::ColumnInfo("TABLE_SCHEMA",
79
 
                                            NAME_CHAR_LEN,
80
 
                                            DRIZZLE_TYPE_VARCHAR,
81
 
                                            0,
82
 
                                            0,
83
 
                                            ""));
84
 
 
85
 
  columns->push_back(new plugin::ColumnInfo("TABLE_NAME",
86
 
                                            NAME_CHAR_LEN,
87
 
                                            DRIZZLE_TYPE_VARCHAR,
88
 
                                            0,
89
 
                                            0,
90
 
                                            "Name"));
91
 
 
92
 
  columns->push_back(new plugin::ColumnInfo("TABLE_TYPE",
93
 
                                            NAME_CHAR_LEN,
94
 
                                            DRIZZLE_TYPE_VARCHAR,
95
 
                                            0,
96
 
                                            0,
97
 
                                            ""));
98
 
 
99
 
  columns->push_back(new plugin::ColumnInfo("ENGINE",
100
 
                                            NAME_CHAR_LEN,
101
 
                                            DRIZZLE_TYPE_VARCHAR,
102
 
                                            0,
103
 
                                            1,
104
 
                                            "Engine"));
105
 
 
106
 
  columns->push_back(new plugin::ColumnInfo("VERSION",
107
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
108
 
                                            DRIZZLE_TYPE_LONGLONG,
109
 
                                            0,
110
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
111
 
                                            "Version"));
112
 
 
113
 
  columns->push_back(new plugin::ColumnInfo("ROW_FORMAT",
114
 
                                            10,
115
 
                                            DRIZZLE_TYPE_VARCHAR,
116
 
                                            0,
117
 
                                            1,
118
 
                                            "Row_format"));
119
 
 
120
 
  columns->push_back(new plugin::ColumnInfo("TABLE_ROWS",
121
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
122
 
                                            DRIZZLE_TYPE_LONGLONG,
123
 
                                            0,
124
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
125
 
                                            "Rows"));
126
 
 
127
 
  columns->push_back(new plugin::ColumnInfo("AVG_ROW_LENGTH",
128
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
129
 
                                            DRIZZLE_TYPE_LONGLONG,
130
 
                                            0,
131
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
132
 
                                            "Avg_row_length"));
133
 
 
134
 
  columns->push_back(new plugin::ColumnInfo("DATA_LENGTH",
135
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
136
 
                                            DRIZZLE_TYPE_LONGLONG,
137
 
                                            0,
138
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
139
 
                                            "Data_length"));
140
 
 
141
 
  columns->push_back(new plugin::ColumnInfo("MAX_DATA_LENGTH",
142
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
143
 
                                            DRIZZLE_TYPE_LONGLONG,
144
 
                                            0,
145
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
146
 
                                            "Max_data_length"));
147
 
 
148
 
  columns->push_back(new plugin::ColumnInfo("INDEX_LENGTH",
149
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
150
 
                                            DRIZZLE_TYPE_LONGLONG,
151
 
                                            0,
152
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
153
 
                                            "Index_length"));
154
 
 
155
 
  columns->push_back(new plugin::ColumnInfo("DATA_FREE",
156
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
157
 
                                            DRIZZLE_TYPE_LONGLONG,
158
 
                                            0,
159
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
160
 
                                            "Data_free"));
161
 
 
162
 
  columns->push_back(new plugin::ColumnInfo("AUTO_INCREMENT",
163
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
164
 
                                            DRIZZLE_TYPE_LONGLONG,
165
 
                                            0,
166
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
167
 
                                            "Auto_increment"));
168
 
 
169
 
  columns->push_back(new plugin::ColumnInfo("CREATE_TIME",
170
 
                                            0,
171
 
                                            DRIZZLE_TYPE_DATETIME,
172
 
                                            0,
173
 
                                            1,
174
 
                                            "Create_time"));
175
 
 
176
 
  columns->push_back(new plugin::ColumnInfo("UPDATE_TIME",
177
 
                                            0,
178
 
                                            DRIZZLE_TYPE_DATETIME,
179
 
                                            0,
180
 
                                            1,
181
 
                                            "Update_time"));
182
 
 
183
 
  columns->push_back(new plugin::ColumnInfo("CHECK_TIME",
184
 
                                            0,
185
 
                                            DRIZZLE_TYPE_DATETIME,
186
 
                                            0,
187
 
                                            1,
188
 
                                            "Check_time"));
189
 
 
190
 
  columns->push_back(new plugin::ColumnInfo("TABLE_COLLATION",
191
 
                                            64,
192
 
                                            DRIZZLE_TYPE_VARCHAR,
193
 
                                            0,
194
 
                                            1,
195
 
                                            "Collation"));
196
 
 
197
 
  columns->push_back(new plugin::ColumnInfo("CHECKSUM",
198
 
                                            MY_INT64_NUM_DECIMAL_DIGITS,
199
 
                                            DRIZZLE_TYPE_LONGLONG,
200
 
                                            0,
201
 
                                            (MY_I_S_MAYBE_NULL | MY_I_S_UNSIGNED),
202
 
                                            "Checksum"));
203
 
 
204
 
  columns->push_back(new plugin::ColumnInfo("CREATE_OPTIONS",
205
 
                                            255,
206
 
                                            DRIZZLE_TYPE_VARCHAR,
207
 
                                            0,
208
 
                                            1,
209
 
                                            "Create_options"));
210
 
 
211
 
  columns->push_back(new plugin::ColumnInfo("TABLE_COMMENT",
212
 
                                            TABLE_COMMENT_MAXLEN,
213
 
                                            DRIZZLE_TYPE_VARCHAR,
214
 
                                            0,
215
 
                                            0,
216
 
                                            "Comment"));
217
 
 
218
 
  columns->push_back(new plugin::ColumnInfo("PLUGIN_NAME",
219
 
                                            64,
220
 
                                            DRIZZLE_TYPE_VARCHAR,
221
 
                                            0,
222
 
                                            MY_I_S_MAYBE_NULL,
223
 
                                            "Plugin_name"));
224
 
 
225
 
  return columns;
226
 
}
227
 
 
228
 
/**
229
 
 * Initialize the I_S table.
230
 
 *
231
 
 * @return a pointer to an I_S table
232
 
 */
233
 
plugin::InfoSchemaTable *TablesIS::getTable()
234
 
{
235
 
  columns= createColumns();
236
 
 
237
 
  if (methods == NULL)
238
 
  {
239
 
    methods= new TablesISMethods();
240
 
  }
241
 
 
242
 
  if (tbls_table == NULL)
243
 
  {
244
 
    tbls_table= new plugin::InfoSchemaTable("OLD_TABLES",
245
 
                                            *columns,
246
 
                                            1, 2, false, true,
247
 
                                            0,
248
 
                                            methods);
249
 
  }
250
 
 
251
 
  return tbls_table;
252
 
}
253
 
 
254
 
/**
255
 
 * Delete memory allocated for the table, columns and methods.
256
 
 */
257
 
void TablesIS::cleanup()
258
 
{
259
 
  clearColumns(*columns);
260
 
  delete tbls_table;
261
 
  delete methods;
262
 
  delete columns;
263
 
}
264
 
 
265
 
int TablesISMethods::processTable(plugin::InfoSchemaTable *store_table,
266
 
                                  Session *session, 
267
 
                                  TableList *tables,
268
 
                                  Table *table, 
269
 
                                  bool res,
270
 
                                  LEX_STRING *db_name,
271
 
                                  LEX_STRING *table_name)
272
 
{
273
 
  const char *tmp_buff= NULL;
274
 
  DRIZZLE_TIME time;
275
 
  const CHARSET_INFO * const cs= system_charset_info;
276
 
 
277
 
  table->restoreRecordAsDefault();
278
 
  table->setWriteSet(1);
279
 
  table->setWriteSet(2);
280
 
  table->setWriteSet(3);
281
 
  table->setWriteSet(4);
282
 
  table->setWriteSet(5);
283
 
  table->setWriteSet(6);
284
 
  table->setWriteSet(7);
285
 
  table->setWriteSet(8);
286
 
  table->setWriteSet(9);
287
 
  table->setWriteSet(11);
288
 
  table->setWriteSet(12);
289
 
  table->setWriteSet(13);
290
 
  table->setWriteSet(14);
291
 
  table->setWriteSet(15);
292
 
  table->setWriteSet(16);
293
 
  table->setWriteSet(17);
294
 
  table->setWriteSet(18);
295
 
  table->setWriteSet(19);
296
 
  table->setWriteSet(20);
297
 
  table->setWriteSet(21);
298
 
  table->field[1]->store(db_name->str, db_name->length, cs);
299
 
  table->field[2]->store(table_name->str, table_name->length, cs);
300
 
 
301
 
  if (res)
302
 
  {
303
 
    /*
304
 
      there was errors during opening tables
305
 
    */
306
 
    const char *error= session->is_error() ? session->main_da.message() : "";
307
 
    table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
308
 
    table->field[20]->store(error, strlen(error), cs);
309
 
    session->clear_error();
310
 
  }
311
 
  else
312
 
  {
313
 
    char option_buff[400];
314
 
    char *ptr= NULL;
315
 
    Table *show_table= tables->table;
316
 
    TableShare *share= show_table->s;
317
 
    Cursor *cursor= show_table->cursor;
318
 
    drizzled::plugin::StorageEngine *tmp_db_type= share->db_type();
319
 
 
320
 
    if (share->tmp_table == SYSTEM_TMP_TABLE)
321
 
    {
322
 
      table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"), cs);
323
 
    }
324
 
    else if (share->tmp_table)
325
 
    {
326
 
      table->field[3]->store(STRING_WITH_LEN("LOCAL TEMPORARY"), cs);
327
 
    }
328
 
    else
329
 
    {
330
 
      table->field[3]->store(STRING_WITH_LEN("BASE Table"), cs);
331
 
    }
332
 
 
333
 
    for (int i= 4; i < 20; i++)
334
 
    {
335
 
      if (i == 7 || (i > 12 && i < 17) || i == 18)
336
 
      {
337
 
        continue;
338
 
      }
339
 
      table->field[i]->set_notnull();
340
 
    }
341
 
    const string &engine_name= drizzled::plugin::StorageEngine::resolveName(tmp_db_type);
342
 
    table->field[4]->store(engine_name.c_str(), engine_name.size(), cs);
343
 
    table->field[5]->store((int64_t) 0, true);
344
 
 
345
 
    ptr=option_buff;
346
 
 
347
 
    if (share->db_create_options & HA_OPTION_PACK_KEYS)
348
 
    {
349
 
      ptr= strcpy(ptr," pack_keys=1")+12;
350
 
    }
351
 
    if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
352
 
    {
353
 
      ptr= strcpy(ptr," pack_keys=0")+12;
354
 
    }
355
 
    if (share->row_type != ROW_TYPE_DEFAULT)
356
 
    {
357
 
      ptr+= sprintf(ptr, " row_format=%s", ha_row_type[(uint32_t)share->row_type]);
358
 
    }
359
 
    if (share->block_size)
360
 
    {
361
 
      ptr= strcpy(ptr, " block_size=")+12;
362
 
      ptr= int64_t10_to_str(share->block_size, ptr, 10);
363
 
    }
364
 
 
365
 
    table->field[19]->store(option_buff+1,
366
 
                            (ptr == option_buff ? 0 :
367
 
                             (uint32_t) (ptr-option_buff)-1), cs);
368
 
 
369
 
    tmp_buff= (share->table_charset ?
370
 
               share->table_charset->name : "default");
371
 
    table->field[17]->store(tmp_buff, strlen(tmp_buff), cs);
372
 
 
373
 
    if (share->hasComment())
374
 
      table->field[20]->store(share->getComment(),
375
 
                              share->getCommentLength(), cs);
376
 
 
377
 
    drizzled::plugin::InfoSchemaTable *tmp_table= 
378
 
      drizzled::plugin::InfoSchemaTable::getTable(table_name->str);
379
 
 
380
 
    if (tmp_table != NULL)
381
 
    {
382
 
      const std::string &plugin_name= tmp_table->getPluginName();
383
 
 
384
 
      if (! plugin_name.empty())
385
 
      {
386
 
        table->field[21]->set_notnull();
387
 
        table->field[21]->store(plugin_name.c_str(), plugin_name.size(), cs);
388
 
      }
389
 
    }
390
 
 
391
 
    if (cursor)
392
 
    {
393
 
      cursor->info(HA_STATUS_VARIABLE | 
394
 
                   HA_STATUS_TIME | 
395
 
                   HA_STATUS_AUTO |
396
 
                   HA_STATUS_NO_LOCK);
397
 
      enum row_type row_type = cursor->get_row_type();
398
 
      switch (row_type) 
399
 
      {
400
 
      case ROW_TYPE_NOT_USED:
401
 
      case ROW_TYPE_DEFAULT:
402
 
        tmp_buff= ((share->db_options_in_use &
403
 
                    HA_OPTION_COMPRESS_RECORD) ? "Compressed" :
404
 
                   (share->db_options_in_use & HA_OPTION_PACK_RECORD) ?
405
 
                   "Dynamic" : "Fixed");
406
 
        break;
407
 
      case ROW_TYPE_FIXED:
408
 
        tmp_buff= "Fixed";
409
 
        break;
410
 
      case ROW_TYPE_DYNAMIC:
411
 
        tmp_buff= "Dynamic";
412
 
        break;
413
 
      case ROW_TYPE_COMPRESSED:
414
 
        tmp_buff= "Compressed";
415
 
        break;
416
 
      case ROW_TYPE_REDUNDANT:
417
 
        tmp_buff= "Redundant";
418
 
        break;
419
 
      case ROW_TYPE_COMPACT:
420
 
        tmp_buff= "Compact";
421
 
        break;
422
 
      case ROW_TYPE_PAGE:
423
 
        tmp_buff= "Paged";
424
 
        break;
425
 
      }
426
 
      table->field[6]->store(tmp_buff, strlen(tmp_buff), cs);
427
 
      table->field[7]->store((int64_t) cursor->stats.records, true);
428
 
      table->field[7]->set_notnull();
429
 
      table->field[8]->store((int64_t) cursor->stats.mean_rec_length, true);
430
 
      table->field[9]->store((int64_t) cursor->stats.data_file_length, true);
431
 
      if (cursor->stats.max_data_file_length)
432
 
      {
433
 
        table->field[10]->store((int64_t) cursor->stats.max_data_file_length,
434
 
                                true);
435
 
      }
436
 
      table->field[11]->store((int64_t) cursor->stats.index_file_length, true);
437
 
      table->field[12]->store((int64_t) cursor->stats.delete_length, true);
438
 
      if (show_table->found_next_number_field)
439
 
      {
440
 
        table->field[13]->store((int64_t) cursor->stats.auto_increment_value, true);
441
 
        table->field[13]->set_notnull();
442
 
      }
443
 
      if (cursor->stats.create_time)
444
 
      {
445
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
446
 
                                                  (time_t) cursor->stats.create_time);
447
 
        table->field[14]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
448
 
        table->field[14]->set_notnull();
449
 
      }
450
 
      if (cursor->stats.update_time)
451
 
      {
452
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
453
 
                                                  (time_t) cursor->stats.update_time);
454
 
        table->field[15]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
455
 
        table->field[15]->set_notnull();
456
 
      }
457
 
      if (cursor->stats.check_time)
458
 
      {
459
 
        session->variables.time_zone->gmt_sec_to_TIME(&time,
460
 
                                                  (time_t) cursor->stats.check_time);
461
 
        table->field[16]->store_time(&time, DRIZZLE_TIMESTAMP_DATETIME);
462
 
        table->field[16]->set_notnull();
463
 
      }
464
 
      if (cursor->getEngine()->check_flag(HTON_BIT_HAS_CHECKSUM))
465
 
      {
466
 
        table->field[18]->store((int64_t) cursor->checksum(), true);
467
 
        table->field[18]->set_notnull();
468
 
      }
469
 
    }
470
 
  }
471
 
  store_table->addRow(table->record[0], table->s->reclength);
472
 
  return false;
473
 
}
474