~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/info_schema.h

Merged Trunk.

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