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/hash/crc32.h"
39
* Header file which contains all classes related to I_S
42
typedef class Item COND;
48
* Represents a field (column) in an I_S table.
53
ColumnInfo(const std::string& in_name,
55
enum enum_field_types in_type,
58
const std::string& in_old_name)
72
type(DRIZZLE_TYPE_VARCHAR),
78
* @return the name of this column.
80
const std::string &getName() const
86
* This method is only ever called from the
87
* InfoSchemaMethods::oldFormat() methods. It is mostly
88
* for old SHOW compatability. It is used when a list
89
* of fields need to be generated for SHOW. The names
90
* for those fields (or columns) are found by calling
91
* this method on each column in the I_S table.
93
* @return the old name of this column.
95
const std::string &getOldName() const
101
* @return the flags for this column.
103
uint32_t getFlags() const
109
* @return the length of this column.
111
uint32_t getLength() const
117
* @return the value of this column.
119
int32_t getValue() const
125
* @return this column's type.
127
enum enum_field_types getType() const
135
* This is used as column name.
137
const std::string name;
140
* For string-type columns, this is the maximum number of
141
* characters. Otherwise, it is the 'display-length' for the column.
146
* This denotes data type for the column. For the most part, there seems to
147
* be one entry in the enum for each SQL data type, although there seem to
148
* be a number of additional entries in the enum.
150
enum enum_field_types type;
155
* This is used to set column attributes. By default, columns are @c NOT
156
* @c NULL and @c SIGNED, and you can deviate from the default
157
* by setting the appopriate flags. You can use either one of the flags
158
* @c MY_I_S_MAYBE_NULL and @cMY_I_S_UNSIGNED or
159
* combine them using the bitwise or operator @c |. Both flags are
160
* defined in table.h.
165
* The name of this column which is used for old SHOW
168
const std::string old_name;
173
* @class InfoSchemaMethods
175
* The methods that an I_S table can support
177
class InfoSchemaMethods
180
virtual ~InfoSchemaMethods() {}
182
virtual int fillTable(Session *session,
184
InfoSchemaTable *schema_table);
185
virtual int processTable(InfoSchemaTable *store_table,
186
Session *session, TableList *tables,
187
Table *table, bool res, LEX_STRING *db_name,
188
LEX_STRING *table_name);
189
virtual int oldFormat(Session *session,
190
InfoSchemaTable *schema_table) const;
194
* @class InfoSchemaRecord
195
* @brief represents a row in an I_S table
197
class InfoSchemaRecord
207
InfoSchemaRecord(unsigned char *buf,
214
record= new unsigned char[rec_len];
215
memcpy(record, buf, rec_len);
216
checksum= drizzled::hash::crc32((const char *) record, rec_len);
219
InfoSchemaRecord(const InfoSchemaRecord &rhs)
224
record= new(std::nothrow) unsigned char[rec_len];
225
memcpy(record, rhs.record, rec_len);
226
checksum= drizzled::hash::crc32((const char *) record, rec_len);
238
* Copy this record into the memory passed to this function.
240
* @param[out] buf copy the record into this memory
242
void copyRecordInto(unsigned char *buf)
244
memcpy(buf, record, rec_len);
248
* @return the length of this record
250
size_t getRecordLength() const
256
* @return the checksum of the data in this record
258
uint32_t getChecksum() const
264
* @param[in] crc a checksum to compare
265
* @return true if the input checksum matches the checksum for this record; false otherwise
267
bool checksumMatches(uint32_t crc) const
269
return (checksum == crc);
274
unsigned char *record;
286
inline void operator()(const T *ptr) const
292
class FindRowByChecksum
296
FindRowByChecksum(uint32_t in_cs)
301
inline bool operator()(const InfoSchemaRecord *rec) const
303
return (cs == rec->getChecksum());
308
* @class InfoSchemaTable
310
* Represents an I_S table.
312
class InfoSchemaTable : public Plugin
315
InfoSchemaTable(const InfoSchemaTable &);
316
InfoSchemaTable& operator=(const InfoSchemaTable &);
319
typedef std::vector<const ColumnInfo *> Columns;
320
typedef std::vector<InfoSchemaRecord *> Rows;
322
InfoSchemaTable(const std::string& tab_name,
323
Columns& in_column_info,
327
bool in_opt_possible,
329
InfoSchemaMethods *in_methods)
331
Plugin(tab_name, "InfoSchemaTable"),
333
is_opt_possible(in_opt_possible),
334
first_column_index(idx_col1),
335
second_column_index(idx_col2),
336
requested_object(req_object),
337
column_info(in_column_info),
339
i_s_methods(in_methods)
342
explicit InfoSchemaTable(const std::string& tab_name)
344
Plugin(tab_name, "InfoSchemaTable"),
346
is_opt_possible(false),
347
first_column_index(0),
348
second_column_index(0),
355
virtual ~InfoSchemaTable()
357
std::for_each(rows.begin(),
364
* Set the methods available on this I_S table.
365
* @param[in] new_methods the methods to use
367
void setInfoSchemaMethods(InfoSchemaMethods *new_methods)
369
i_s_methods= new_methods;
375
* @param[in] session a session handler
376
* @return 0 on success; 1 on error
378
int fillTable(Session *session, Table *table)
380
int retval= i_s_methods->fillTable(session, table, this);
385
* Fill and store records into an I_S table.
387
* @param[in] session a session handler
388
* @param[in] tables table list (processed table)
389
* @param[in] table I_S table
390
* @param[in] res 1 means error during opening of the processed table
391
* 0 means processed table opened without error
392
* @param[in] db_name database name
393
* @param[in] tab_name table name
394
* @return 0 on success; 1 on error
396
int processTable(Session *session, TableList *tables, Table *table,
397
bool res, LEX_STRING *db_name, LEX_STRING *tab_name)
399
int retval= i_s_methods->processTable(this, session, tables, table,
400
res, db_name, tab_name);
405
* For old SHOW compatibility. It is used when old SHOW doesn't
406
* have generated column names. Generates the list of fields
409
* @param[in] session a session handler
410
* @param[in] schema_table pointer to element of the I_S tables list
412
int oldFormat(Session *session, InfoSchemaTable *schema_table) const
414
int retval= i_s_methods->oldFormat(session, schema_table);
419
* @param[in] new_first_index value to set first column index to
421
void setFirstColumnIndex(int32_t new_first_index)
423
first_column_index= new_first_index;
427
* @param[in] new_second_index value to set second column index to
429
void setSecondColumnIndex(int32_t new_second_index)
431
second_column_index= new_second_index;
435
* @param[in] in_column_info the columns info to use for this I_S table
437
void setColumnInfo(ColumnInfo *in_column_info)
439
ColumnInfo *tmp= in_column_info;
440
for (; tmp->getName().length() != 0; tmp++)
442
column_info.push_back(tmp);
447
* @return the name of the I_S table.
449
const std::string &getTableName() const
455
* @return the names of the I_S tables.
457
static void getTableNames(std::set<std::string>& tables_names);
460
* @return true if this I_S table is hidden; false otherwise.
462
bool isHidden() const
468
* @return true if I_S optimizations can be performed on this
469
* I_S table when running the fillTable method; false
472
bool isOptimizationPossible() const
474
return is_opt_possible;
478
* @return the index for the first field.
480
int32_t getFirstColumnIndex() const
482
return first_column_index;
486
* @return the index the second field.
488
int32_t getSecondColumnIndex() const
490
return second_column_index;
494
* @return the requested object.
496
uint32_t getRequestedObject() const
498
return requested_object;
502
* @return the columns for this I_S table
504
const Columns &getColumns() const
510
* @return the rows for this I_S table.
518
* Clear the rows for this table and de-allocate all memory for this rows.
522
std::for_each(rows.begin(),
529
* Add a row to the std::vector of rows for this I_S table. For the moment, we check to make sure
530
* that we do not add any duplicate rows. This is done in a niave manner for the moment by
531
* checking the crc of the raw data for each row. We will not add this row to the std::vector of
532
* rows for this I_S table is a duplicate row already exists in the std::vector.
534
* @param[in] buf raw data for the record to add
535
* @param[in] len size of the raw data for the record to add
537
void addRow(unsigned char *buf, size_t len)
539
uint32_t cs= drizzled::hash::crc32((const char *) buf, len);
540
Rows::iterator it= std::find_if(rows.begin(),
542
FindRowByChecksum(cs));
543
if (it == rows.end())
545
InfoSchemaRecord *record= new InfoSchemaRecord(buf, len);
546
rows.push_back(record);
551
* @param[in] index the index of this column
552
* @return the name for the column at the given index
554
const std::string &getColumnName(int index) const
556
return column_info[index]->getName();
562
* Boolean which indicates whether this I_S table
563
* is hidden or not. If it is hidden, it will not show
564
* up in the list of I_S tables.
569
* Boolean which indicates whether optimizations are
570
* possible on this I_S table when performing the
573
bool is_opt_possible;
576
* The index of the first column.
578
int32_t first_column_index;
581
* The index of the second column.
583
int32_t second_column_index;
586
* The object to open (TABLE | VIEW).
588
uint32_t requested_object;
591
* The columns for this I_S table.
596
* The rows for this I_S table.
601
* Contains the methods available on this I_S table.
603
InfoSchemaMethods *i_s_methods;
606
static bool addPlugin(plugin::InfoSchemaTable *schema_table);
607
static void removePlugin(plugin::InfoSchemaTable *table);
609
static plugin::InfoSchemaTable *getTable(const char *table_name);
610
static int addTableToList(Session *session, std::vector<LEX_STRING*> &files,
614
} /* namespace plugin */
615
} /* namespace drizzled */
617
#endif /* DRIZZLED_PLUGIN_INFO_SCHEMA_TABLE_H */