~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/info_schema_table.h

  • Committer: Monty Taylor
  • Date: 2009-09-30 07:01:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090930070132-b1ol1xu1rpajdddy
Small namespace cleanup.

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
#ifndef DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H
 
22
#define DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H
 
23
 
 
24
#include <string>
 
25
 
 
26
namespace drizzled
 
27
{
 
28
namespace plugin
 
29
{
 
30
 
 
31
/**
 
32
 * @file
 
33
 *   info_schema.h
 
34
 * @brief 
 
35
 *   Header file which contains all classes related to I_S
 
36
 */
 
37
 
 
38
typedef class Item COND;
 
39
 
 
40
 
 
41
/**
 
42
 * @class ColumnInfo
 
43
 * @brief
 
44
 *   Represents a field (column) in an I_S table.
 
45
 */
 
46
class ColumnInfo 
 
47
 
48
public: 
 
49
  ColumnInfo(const std::string& in_name, 
 
50
             uint32_t in_length, 
 
51
             enum enum_field_types in_type,
 
52
             int32_t in_value,
 
53
             uint32_t in_flags,
 
54
             const std::string& in_old_name,
 
55
             uint32_t in_open_method)
 
56
    :
 
57
      name(in_name),
 
58
      length(in_length),
 
59
      type(in_type),
 
60
      value(in_value),
 
61
      flags(in_flags),
 
62
      old_name(in_old_name),
 
63
      open_method(in_open_method)
 
64
  {}
 
65
 
 
66
  ColumnInfo()
 
67
    :
 
68
      name(),
 
69
      length(0),
 
70
      type(DRIZZLE_TYPE_VARCHAR),
 
71
      flags(0),
 
72
      old_name(),
 
73
      open_method(SKIP_OPEN_TABLE)
 
74
  {}
 
75
 
 
76
  /**
 
77
   * @return the name of this column.
 
78
   */
 
79
  const std::string &getName() const
 
80
  {
 
81
    return name;
 
82
  }
 
83
 
 
84
  /**
 
85
   * This method is only ever called from the
 
86
   * InfoSchemaMethods::oldFormat() methods. It is mostly
 
87
   * for old SHOW compatability. It is used when a list
 
88
   * of fields need to be generated for SHOW. The names
 
89
   * for those fields (or columns) are found by calling
 
90
   * this method on each column in the I_S table.
 
91
   *
 
92
   * @return the old name of this column.
 
93
   */
 
94
  const std::string &getOldName() const
 
95
  {
 
96
    return old_name;
 
97
  }
 
98
 
 
99
  /**
 
100
   * @return the open method for this column.
 
101
   */
 
102
  uint32_t getOpenMethod() const
 
103
  {
 
104
    return open_method;
 
105
  }
 
106
 
 
107
  /**
 
108
   * @return the flags for this column.
 
109
   */
 
110
  uint32_t getFlags() const
 
111
  {
 
112
    return flags;
 
113
  }
 
114
 
 
115
  /**
 
116
   * @return the length of this column.
 
117
   */
 
118
  uint32_t getLength() const
 
119
  {
 
120
    return length;
 
121
  }
 
122
 
 
123
  /**
 
124
   * @return the value of this column.
 
125
   */
 
126
  int32_t getValue() const
 
127
  {
 
128
    return value;
 
129
  }
 
130
 
 
131
  /**
 
132
   * @return this column's type.
 
133
   */
 
134
  enum enum_field_types getType() const
 
135
  {
 
136
    return type;
 
137
  }
 
138
 
 
139
private:
 
140
 
 
141
  /**
 
142
   * This is used as column name.
 
143
   */
 
144
  const std::string name;
 
145
 
 
146
  /**
 
147
   * For string-type columns, this is the maximum number of
 
148
   * characters. Otherwise, it is the 'display-length' for the column.
 
149
   */
 
150
  uint32_t length;
 
151
 
 
152
  /**
 
153
   * This denotes data type for the column. For the most part, there seems to
 
154
   * be one entry in the enum for each SQL data type, although there seem to
 
155
   * be a number of additional entries in the enum.
 
156
   */
 
157
  enum enum_field_types type;
 
158
 
 
159
  int32_t value;
 
160
 
 
161
  /**
 
162
   * This is used to set column attributes. By default, columns are @c NOT
 
163
   * @c NULL and @c SIGNED, and you can deviate from the default
 
164
   * by setting the appopriate flags. You can use either one of the flags
 
165
   * @c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
 
166
   * combine them using the bitwise or operator @c |. Both flags are
 
167
   * defined in table.h.
 
168
   */
 
169
  uint32_t flags;
 
170
 
 
171
  /**
 
172
   * The name of this column which is used for old SHOW
 
173
   * compatability.
 
174
   */
 
175
  const std::string old_name;
 
176
 
 
177
  /**
 
178
   * This should be one of @c SKIP_OPEN_TABLE,
 
179
   * @c OPEN_FRM_ONLY or @c OPEN_FULL_TABLE.
 
180
   */
 
181
  uint32_t open_method;
 
182
 
 
183
};
 
184
 
 
185
/**
 
186
 * @class InfoSchemaMethods
 
187
 * @brief
 
188
 *   The methods that an I_S table can support
 
189
 */
 
190
class InfoSchemaMethods
 
191
{
 
192
public:
 
193
  virtual ~InfoSchemaMethods() {}
 
194
 
 
195
  virtual Table *createSchemaTable(Session *session,
 
196
                                   TableList *table_list) const;
 
197
  virtual int fillTable(Session *session, 
 
198
                        TableList *tables,
 
199
                        COND *cond);
 
200
  virtual int processTable(Session *session, TableList *tables,
 
201
                           Table *table, bool res, LEX_STRING *db_name,
 
202
                           LEX_STRING *table_name) const;
 
203
  virtual int oldFormat(Session *session, 
 
204
                        InfoSchemaTable *schema_table) const;
 
205
};
 
206
 
 
207
/**
 
208
 * @class InfoSchemaTable
 
209
 * @brief 
 
210
 *   Represents an I_S table.
 
211
 */
 
212
class InfoSchemaTable : public Plugin
 
213
{
 
214
public:
 
215
 
 
216
  typedef std::vector<const ColumnInfo *> Columns;
 
217
  
 
218
  InfoSchemaTable(const std::string& tab_name,
 
219
                  Columns& in_column_info,
 
220
                  int idx_col1,
 
221
                  int idx_col2,
 
222
                  bool in_hidden,
 
223
                  bool in_opt_possible,
 
224
                  uint32_t req_object,
 
225
                  InfoSchemaMethods *in_methods)
 
226
    :
 
227
      Plugin(tab_name),
 
228
      table_name(tab_name),
 
229
      hidden(in_hidden),
 
230
      is_opt_possible(in_opt_possible),
 
231
      first_column_index(idx_col1),
 
232
      second_column_index(idx_col2),
 
233
      requested_object(req_object),
 
234
      column_info(in_column_info),
 
235
      i_s_methods(in_methods)
 
236
  {}
 
237
 
 
238
  explicit InfoSchemaTable(const std::string& tab_name)
 
239
    :
 
240
      Plugin(tab_name),
 
241
      table_name(),
 
242
      hidden(false),
 
243
      is_opt_possible(false),
 
244
      first_column_index(0),
 
245
      second_column_index(0),
 
246
      requested_object(0),
 
247
      column_info(),
 
248
      i_s_methods(NULL)
 
249
  {}
 
250
 
 
251
  /**
 
252
   * Set the methods available on this I_S table.
 
253
   * @param[in] new_methods the methods to use
 
254
   */
 
255
  void setInfoSchemaMethods(InfoSchemaMethods *new_methods)
 
256
  {
 
257
    i_s_methods= new_methods;
 
258
  }
 
259
 
 
260
  /**
 
261
   * Create the temporary I_S tables using schema_table data.
 
262
   *
 
263
   * @param[in] session a session handler
 
264
   * @param[in] table_list Used to pass I_S table information (fields,
 
265
   *                       tables, parameters, etc.) and table name
 
266
   * @retval \# pointer to created table
 
267
   * @retval NULL Can't create table
 
268
   */
 
269
  Table *createSchemaTable(Session *session, TableList *table_list) const
 
270
  {
 
271
    Table *retval= i_s_methods->createSchemaTable(session, table_list);
 
272
    return retval;
 
273
  }
 
274
 
 
275
  /**
 
276
   * Fill I_S table.
 
277
   *
 
278
   * @param[in] session a session handler
 
279
   * @param[in] tables I_S table
 
280
   * @param[in] cond 'WHERE' condition
 
281
   * @return 0 on success; 1 on error
 
282
   */
 
283
  int fillTable(Session *session, TableList *tables, COND *cond)
 
284
  {
 
285
    int retval= i_s_methods->fillTable(session, tables, cond);
 
286
    return retval;
 
287
  }
 
288
 
 
289
  /**
 
290
   * Fill and store records into an I_S table.
 
291
   *
 
292
   * @param[in] session a session handler
 
293
   * @param[in] tables table list (processed table)
 
294
   * @param[in] table I_S table
 
295
   * @param[in] res 1 means error during opening of the processed table
 
296
   *                0 means processed table opened without error
 
297
   * @param[in] db_name database name
 
298
   * @param[in] tab_name table name
 
299
   * @return 0 on success; 1 on error
 
300
   */
 
301
  int processTable(Session *session, TableList *tables, Table *table,
 
302
                   bool res, LEX_STRING *db_name, LEX_STRING *tab_name) const
 
303
  {
 
304
    int retval= i_s_methods->processTable(session, tables, table,
 
305
                                          res, db_name, tab_name);
 
306
    return retval;
 
307
  }
 
308
 
 
309
  /**
 
310
   * For old SHOW compatibility. It is used when old SHOW doesn't
 
311
   * have generated column names. Generates the list of fields
 
312
   * for SHOW.
 
313
   *
 
314
   * @param[in] session a session handler
 
315
   * @param[in] schema_table pointer to element of the I_S tables list
 
316
   */
 
317
  int oldFormat(Session *session, InfoSchemaTable *schema_table) const
 
318
  {
 
319
    int retval= i_s_methods->oldFormat(session, schema_table);
 
320
    return retval;
 
321
  }
 
322
 
 
323
  /**
 
324
   * Set the I_S tables name.
 
325
   * @param[in] new_name the name to set the table to
 
326
   */
 
327
  void setTableName(const std::string &new_name)
 
328
  {
 
329
    table_name= new_name;
 
330
  }
 
331
 
 
332
  /**
 
333
   * @param[in] new_first_index value to set first column index to
 
334
   */
 
335
  void setFirstColumnIndex(int32_t new_first_index)
 
336
  {
 
337
    first_column_index= new_first_index;
 
338
  }
 
339
 
 
340
  /**
 
341
   * @param[in] new_second_index value to set second column index to
 
342
   */
 
343
  void setSecondColumnIndex(int32_t new_second_index)
 
344
  {
 
345
    second_column_index= new_second_index;
 
346
  }
 
347
 
 
348
  /**
 
349
   * @param[in] in_column_info the columns info to use for this I_S table
 
350
   */
 
351
  void setColumnInfo(ColumnInfo *in_column_info)
 
352
  {
 
353
    ColumnInfo *tmp= in_column_info;
 
354
    for (; tmp->getName().length() != 0; tmp++)
 
355
    {
 
356
      column_info.push_back(tmp);
 
357
    }
 
358
  }
 
359
 
 
360
  /**
 
361
   * @return the name of the I_S table.
 
362
   */
 
363
  const std::string &getTableName() const
 
364
  {
 
365
    return table_name;
 
366
  }
 
367
 
 
368
  /**
 
369
   * @return true if this I_S table is hidden; false otherwise.
 
370
   */
 
371
  bool isHidden() const
 
372
  {
 
373
    return hidden;
 
374
  }
 
375
 
 
376
  /**
 
377
   * @return true if I_S optimizations can be performed on this
 
378
   *         I_S table when running the fillTable method; false
 
379
   *         otherwise.
 
380
   */
 
381
  bool isOptimizationPossible() const
 
382
  {
 
383
    return is_opt_possible;
 
384
  }
 
385
 
 
386
  /**
 
387
   * @return the index for the first field.
 
388
   */
 
389
  int32_t getFirstColumnIndex() const
 
390
  {
 
391
    return first_column_index;
 
392
  }
 
393
 
 
394
  /**
 
395
   * @return the index the second field.
 
396
   */
 
397
  int32_t getSecondColumnIndex() const
 
398
  {
 
399
    return second_column_index;
 
400
  }
 
401
 
 
402
  /**
 
403
   * @return the requested object.
 
404
   */
 
405
  uint32_t getRequestedObject() const
 
406
  {
 
407
    return requested_object;
 
408
  }
 
409
 
 
410
  /**
 
411
   * @return the columns for this I_S table
 
412
   */
 
413
  const Columns &getColumns() const
 
414
  {
 
415
    return column_info;
 
416
  }
 
417
 
 
418
  /**
 
419
   * @param[in] index the index of this column
 
420
   * @return the name for the column at the given index
 
421
   */
 
422
  const std::string &getColumnName(int index) const
 
423
  {
 
424
    return column_info[index]->getName();
 
425
  }
 
426
 
 
427
  /**
 
428
   * @param[in] index the index of this column
 
429
   * @return the open method for the column at the given index
 
430
   */
 
431
  uint32_t getColumnOpenMethod(int index) const
 
432
  {
 
433
    return column_info[index]->getOpenMethod();
 
434
  }
 
435
 
 
436
private:
 
437
  /**
 
438
   * I_S table name.
 
439
   */
 
440
  std::string table_name;
 
441
 
 
442
  /**
 
443
   * Boolean which indicates whether this I_S table
 
444
   * is hidden or not. If it is hidden, it will not show
 
445
   * up in the list of I_S tables.
 
446
   */
 
447
  bool hidden;
 
448
 
 
449
  /**
 
450
   * Boolean which indicates whether optimizations are
 
451
   * possible on this I_S table when performing the
 
452
   * fillTable method.
 
453
   */
 
454
  bool is_opt_possible;
 
455
 
 
456
  /**
 
457
   * The index of the first column.
 
458
   */
 
459
  int32_t first_column_index;
 
460
 
 
461
  /**
 
462
   * The index of the second column.
 
463
   */
 
464
  int32_t second_column_index;
 
465
 
 
466
  /**
 
467
   * The object to open (TABLE | VIEW).
 
468
   */
 
469
  uint32_t requested_object;
 
470
 
 
471
  /**
 
472
   * The columns for this I_S table.
 
473
   */
 
474
  Columns column_info;
 
475
 
 
476
  /**
 
477
   * Contains the methods available on this I_S table.
 
478
   */
 
479
  InfoSchemaMethods *i_s_methods;
 
480
 
 
481
public:
 
482
  static void add(plugin::InfoSchemaTable *schema_table);
 
483
  static void remove(plugin::InfoSchemaTable *table);
 
484
 
 
485
  static plugin::InfoSchemaTable *getTable(const char *table_name);
 
486
  static int addTableToList(Session *session, std::vector<LEX_STRING*> &files,
 
487
                            const char *wild);
 
488
};
 
489
 
 
490
} /* namespace plugin */
 
491
} /* namespace drizzled */
 
492
 
 
493
#endif /* DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H */