1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
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.
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.
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
21
#ifndef DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H
22
#define DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H
24
#include "drizzled/plugin/plugin.h"
25
#include "drizzled/algorithm/crc32.h"
26
#include "drizzled/common.h"
39
typedef struct drizzle_lex_string LEX_STRING;
41
extern const std::string INFORMATION_SCHEMA_NAME;
50
* Header file which contains all classes related to I_S
53
typedef class Item COND;
54
class InfoSchemaTable;
60
* Represents a field (column) in an I_S table.
65
ColumnInfo(const std::string& in_name,
67
enum enum_field_types in_type,
70
const std::string& in_old_name)
84
type(DRIZZLE_TYPE_VARCHAR),
90
* @return the name of this column.
92
const std::string &getName() const
98
* This method is only ever called from the
99
* InfoSchemaMethods::oldFormat() methods. It is mostly
100
* for old SHOW compatability. It is used when a list
101
* of fields need to be generated for SHOW. The names
102
* for those fields (or columns) are found by calling
103
* this method on each column in the I_S table.
105
* @return the old name of this column.
107
const std::string &getOldName() const
113
* @return the flags for this column.
115
uint32_t getFlags() const
121
* @return the length of this column.
123
uint32_t getLength() const
129
* @return the value of this column.
131
int32_t getValue() const
137
* @return this column's type.
139
enum enum_field_types getType() const
147
* This is used as column name.
149
const std::string name;
152
* For string-type columns, this is the maximum number of
153
* characters. Otherwise, it is the 'display-length' for the column.
158
* This denotes data type for the column. For the most part, there seems to
159
* be one entry in the enum for each SQL data type, although there seem to
160
* be a number of additional entries in the enum.
162
enum enum_field_types type;
167
* This is used to set column attributes. By default, columns are @c NOT
168
* @c NULL and @c SIGNED, and you can deviate from the default
169
* by setting the appopriate flags. You can use either one of the flags
170
* @c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
171
* combine them using the bitwise or operator @c |. Both flags are
172
* defined in table.h.
177
* The name of this column which is used for old SHOW
180
const std::string old_name;
185
* @class InfoSchemaMethods
187
* The methods that an I_S table can support
189
class InfoSchemaMethods
192
virtual ~InfoSchemaMethods() {}
194
virtual int fillTable(Session *session,
196
InfoSchemaTable *schema_table);
197
virtual int processTable(InfoSchemaTable *store_table,
198
Session *session, TableList *tables,
199
Table *table, bool res, LEX_STRING *db_name,
200
LEX_STRING *table_name);
201
virtual int oldFormat(Session *session,
202
InfoSchemaTable *schema_table) const;
206
* @class InfoSchemaRecord
207
* @brief represents a row in an I_S table
209
class InfoSchemaRecord
213
InfoSchemaRecord(unsigned char *buf, size_t in_len);
214
InfoSchemaRecord(const InfoSchemaRecord &rhs);
225
* Copy this record into the memory passed to this function.
227
* @param[out] buf copy the record into this memory
229
void copyRecordInto(unsigned char *buf)
231
memcpy(buf, record, rec_len);
235
* @return the length of this record
237
size_t getRecordLength() const
243
* @return the checksum of the data in this record
245
uint32_t getChecksum() const
251
* @param[in] crc a checksum to compare
252
* @return true if the input checksum matches the checksum for this record; false otherwise
254
bool checksumMatches(uint32_t crc) const
256
return (checksum == crc);
261
unsigned char *record;
273
inline void operator()(const T *ptr) const
279
class FindRowByChecksum
283
FindRowByChecksum(uint32_t in_cs)
288
inline bool operator()(const InfoSchemaRecord *rec) const
290
return (cs == rec->getChecksum());
295
* @class InfoSchemaTable
297
* Represents an I_S table.
299
class InfoSchemaTable : public Plugin
302
InfoSchemaTable(const InfoSchemaTable &);
303
InfoSchemaTable& operator=(const InfoSchemaTable &);
306
typedef std::vector<const ColumnInfo *> Columns;
307
typedef std::vector<InfoSchemaRecord *> Rows;
309
InfoSchemaTable(const std::string& tab_name,
310
Columns& in_column_info,
314
bool in_opt_possible,
316
InfoSchemaMethods *in_methods)
318
Plugin(tab_name, "InfoSchemaTable"),
320
is_opt_possible(in_opt_possible),
321
first_column_index(idx_col1),
322
second_column_index(idx_col2),
323
requested_object(req_object),
324
column_info(in_column_info),
326
i_s_methods(in_methods),
330
InfoSchemaTable(const std::string& tab_name,
331
Columns& in_column_info,
335
bool in_opt_possible,
337
InfoSchemaMethods *in_methods,
338
const std::string in_plugin_name)
340
Plugin(tab_name, "InfoSchemaTable"),
342
is_opt_possible(in_opt_possible),
343
first_column_index(idx_col1),
344
second_column_index(idx_col2),
345
requested_object(req_object),
346
column_info(in_column_info),
348
i_s_methods(in_methods),
349
plugin_name(in_plugin_name)
352
explicit InfoSchemaTable(const std::string& tab_name)
354
Plugin(tab_name, "InfoSchemaTable"),
356
is_opt_possible(false),
357
first_column_index(0),
358
second_column_index(0),
366
virtual ~InfoSchemaTable()
368
std::for_each(rows.begin(),
375
* Set the methods available on this I_S table.
376
* @param[in] new_methods the methods to use
378
void setInfoSchemaMethods(InfoSchemaMethods *new_methods)
380
i_s_methods= new_methods;
386
* @param[in] session a session handler
387
* @return 0 on success; 1 on error
389
int fillTable(Session *session, Table *table)
391
int retval= i_s_methods->fillTable(session, table, this);
396
* Fill and store records into an I_S table.
398
* @param[in] session a session handler
399
* @param[in] tables table list (processed table)
400
* @param[in] table I_S table
401
* @param[in] res 1 means error during opening of the processed table
402
* 0 means processed table opened without error
403
* @param[in] db_name database name
404
* @param[in] tab_name table name
405
* @return 0 on success; 1 on error
407
int processTable(Session *session, TableList *tables, Table *table,
408
bool res, LEX_STRING *db_name, LEX_STRING *tab_name)
410
int retval= i_s_methods->processTable(this, session, tables, table,
411
res, db_name, tab_name);
416
* For old SHOW compatibility. It is used when old SHOW doesn't
417
* have generated column names. Generates the list of fields
420
* @param[in] session a session handler
421
* @param[in] schema_table pointer to element of the I_S tables list
423
int oldFormat(Session *session, InfoSchemaTable *schema_table) const
425
int retval= i_s_methods->oldFormat(session, schema_table);
430
* @param[in] new_first_index value to set first column index to
432
void setFirstColumnIndex(int32_t new_first_index)
434
first_column_index= new_first_index;
438
* @param[in] new_second_index value to set second column index to
440
void setSecondColumnIndex(int32_t new_second_index)
442
second_column_index= new_second_index;
446
* @param[in] in_column_info the columns info to use for this I_S table
448
void setColumnInfo(ColumnInfo *in_column_info)
450
ColumnInfo *tmp= in_column_info;
451
for (; tmp->getName().length() != 0; tmp++)
453
column_info.push_back(tmp);
458
* @return the name of the I_S table.
460
const std::string &getTableName() const
466
* @return the names of the I_S tables.
468
static void getTableNames(std::set<std::string>& tables_names);
471
* @return true if this I_S table is hidden; false otherwise.
473
bool isHidden() const
479
* @return true if I_S optimizations can be performed on this
480
* I_S table when running the fillTable method; false
483
bool isOptimizationPossible() const
485
return is_opt_possible;
489
* @return the index for the first field.
491
int32_t getFirstColumnIndex() const
493
return first_column_index;
497
* @return the index the second field.
499
int32_t getSecondColumnIndex() const
501
return second_column_index;
505
* @return the requested object.
507
uint32_t getRequestedObject() const
509
return requested_object;
513
* @return the plugin name.
515
const std::string &getPluginName() const
521
* @return the columns for this I_S table
523
const Columns &getColumns() const
529
* @return the rows for this I_S table.
537
* Clear the rows for this table and de-allocate all memory for this rows.
541
std::for_each(rows.begin(),
548
* Add a row to the std::vector of rows for this I_S table. For the moment, we check to make sure
549
* that we do not add any duplicate rows. This is done in a niave manner for the moment by
550
* checking the crc of the raw data for each row. We will not add this row to the std::vector of
551
* rows for this I_S table is a duplicate row already exists in the std::vector.
553
* @param[in] buf raw data for the record to add
554
* @param[in] len size of the raw data for the record to add
556
void addRow(unsigned char *buf, size_t len)
558
uint32_t cs= algorithm::crc32((const char *) buf, len);
559
Rows::iterator it= std::find_if(rows.begin(),
561
FindRowByChecksum(cs));
562
if (it == rows.end())
564
InfoSchemaRecord *record= new InfoSchemaRecord(buf, len);
565
rows.push_back(record);
570
* @param[in] index the index of this column
571
* @return the name for the column at the given index
573
const std::string &getColumnName(int index) const
575
return column_info[index]->getName();
581
* Boolean which indicates whether this I_S table
582
* is hidden or not. If it is hidden, it will not show
583
* up in the list of I_S tables.
588
* Boolean which indicates whether optimizations are
589
* possible on this I_S table when performing the
592
bool is_opt_possible;
595
* The index of the first column.
597
int32_t first_column_index;
600
* The index of the second column.
602
int32_t second_column_index;
605
* The object to open (TABLE | VIEW).
607
uint32_t requested_object;
610
* The columns for this I_S table.
615
* The rows for this I_S table.
620
* Contains the methods available on this I_S table.
622
InfoSchemaMethods *i_s_methods;
625
* The name of the plugin associated with this I_S table
626
* NULL for non I_S tables.
628
const std::string plugin_name;
631
static bool addPlugin(plugin::InfoSchemaTable *schema_table);
632
static void removePlugin(plugin::InfoSchemaTable *table);
634
static plugin::InfoSchemaTable *getTable(const char *table_name);
635
static int addTableToList(Session *session, std::vector<LEX_STRING*> &files,
639
} /* namespace plugin */
640
} /* namespace drizzled */
642
#endif /* DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H */