~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/info_schema/tables.cc

MergedĀ fromĀ me.

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