Drizzled Public API Documentation

show_indexes.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <plugin/show_dictionary/dictionary.h>
00023 #include <drizzled/identifier.h>
00024 
00025 
00026 using namespace std;
00027 using namespace drizzled;
00028 
00029 ShowIndexes::ShowIndexes() :
00030   show_dictionary::Show("SHOW_INDEXES")
00031 {
00032   add_field("Table");
00033   add_field("Unique", plugin::TableFunction::BOOLEAN, 0, false);
00034   add_field("Key_name");
00035   add_field("Seq_in_index", plugin::TableFunction::NUMBER, 0, false);
00036   add_field("Column_name");
00037 }
00038 
00039 ShowIndexes::Generator::Generator(Field **arg) :
00040   show_dictionary::Show::Generator(arg),
00041   is_tables_primed(false),
00042   is_index_primed(false),
00043   is_index_part_primed(false),
00044   index_iterator(0),
00045   index_part_iterator(0)
00046 {
00047   if (not isShowQuery())
00048     return;
00049 
00050   statement::Show& select= static_cast<statement::Show&>(statement());
00051 
00052   if (not select.getShowTable().empty() && not select.getShowSchema().empty())
00053   {
00054     table_name.append(select.getShowTable().c_str());
00055     identifier::Table identifier(select.getShowSchema().c_str(), select.getShowTable().c_str());
00056 
00057     if (not plugin::Authorization::isAuthorized(*getSession().user(),
00058                                             identifier, false))
00059     {
00060       drizzled::error::access(*getSession().user(), identifier);
00061       return;
00062     }
00063 
00064     table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
00065 
00066     if (table_proto)
00067       is_tables_primed= true;
00068   }
00069 }
00070 
00071 bool ShowIndexes::Generator::nextIndexCore()
00072 {
00073   if (isIndexesPrimed())
00074   {
00075     index_iterator++;
00076   }
00077   else
00078   {
00079     if (not isTablesPrimed())
00080       return false;
00081 
00082     index_iterator= 0;
00083     is_index_primed= true;
00084   }
00085 
00086   if (index_iterator >= getTableProto().indexes_size())
00087     return false;
00088 
00089   index= getTableProto().indexes(index_iterator);
00090 
00091   return true;
00092 }
00093 
00094 bool ShowIndexes::Generator::nextIndex()
00095 {
00096   while (not nextIndexCore())
00097   {
00098     return false;
00099   }
00100 
00101   return true;
00102 }
00103 
00104 bool ShowIndexes::Generator::nextIndexPartsCore()
00105 {
00106   if (is_index_part_primed)
00107   {
00108     index_part_iterator++;
00109   }
00110   else
00111   {
00112     if (not isIndexesPrimed())
00113       return false;
00114 
00115     index_part_iterator= 0;
00116     is_index_part_primed= true;
00117   }
00118 
00119   if (index_part_iterator >= getIndex().index_part_size())
00120     return false;
00121 
00122   index_part= getIndex().index_part(index_part_iterator);
00123 
00124   return true;
00125 }
00126 
00127 
00128 bool ShowIndexes::Generator::nextIndexParts()
00129 {
00130   while (not nextIndexPartsCore())
00131   {
00132     if (not nextIndex())
00133       return false;
00134     is_index_part_primed= false;
00135   }
00136 
00137   return true;
00138 }
00139 
00140 
00141 
00142 bool ShowIndexes::Generator::populate()
00143 {
00144   if (not nextIndexParts())
00145     return false;
00146 
00147   fill();
00148 
00149   return true;
00150 }
00151 
00152 void ShowIndexes::Generator::fill()
00153 {
00154   /* Table */
00155   push(getTableName());
00156 
00157   /* Unique */
00158   push(getIndex().is_unique());
00159 
00160   /* Key_name */
00161   push(getIndex().name());
00162 
00163   /* Seq_in_index */
00164   push(static_cast<int64_t>(index_part_iterator + 1));
00165 
00166   /* Column_name */
00167   push(getTableProto().field(getIndexPart().fieldnr()).name());
00168 }