Drizzled Public API Documentation

show_table_status.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 
00023 #include <plugin/show_dictionary/dictionary.h>
00024 #include <drizzled/pthread_globals.h>
00025 
00026 using namespace drizzled;
00027 using namespace std;
00028 
00029 ShowTableStatus::ShowTableStatus() :
00030   show_dictionary::Show("SHOW_TABLE_STATUS")
00031 {
00032   add_field("Session", plugin::TableFunction::NUMBER, 0, false);
00033   add_field("Schema");
00034   add_field("Name");
00035   add_field("Type");
00036   add_field("Engine");
00037   add_field("Version");
00038   add_field("Rows");
00039   add_field("Avg_row_length");
00040   add_field("Table_size");
00041   add_field("Auto_increment");
00042 }
00043 
00044 ShowTableStatus::Generator::Generator(drizzled::Field **arg) :
00045   show_dictionary::Show::Generator(arg),
00046   is_primed(false),
00047   scopedLock(table::Cache::singleton().mutex())
00048 {
00049   if (not isShowQuery())
00050    return;
00051 
00052   statement::Show& select= static_cast<statement::Show&>(statement());
00053 
00054   schema_predicate.append(select.getShowSchema());
00055 
00056   util::string::const_shared_ptr schema(getSession().schema());
00057   if (schema_predicate.empty() and schema)
00058   {
00059     schema_predicate.append(*schema);
00060   }
00061 
00062   if (not schema_predicate.empty())
00063   {
00064     table::CacheMap &open_cache(table::getCache());
00065 
00066     for (table::CacheMap::const_iterator iter= open_cache.begin();
00067          iter != open_cache.end();
00068          iter++)
00069     {
00070       table_list.push_back(iter->second);
00071     }
00072 
00073     for (drizzled::Table *tmp_table= getSession().getTemporaryTables(); tmp_table; tmp_table= tmp_table->getNext())
00074     {
00075       if (tmp_table->getShare())
00076       {
00077         table_list.push_back(tmp_table);
00078       }
00079     }
00080     std::sort(table_list.begin(), table_list.end(), Table::compare);
00081   }
00082 }
00083 
00084 ShowTableStatus::Generator::~Generator()
00085 {
00086 }
00087 
00088 bool ShowTableStatus::Generator::nextCore()
00089 {
00090   if (is_primed)
00091   {
00092     table_list_iterator++;
00093   }
00094   else
00095   {
00096     is_primed= true;
00097     table_list_iterator= table_list.begin();
00098   }
00099 
00100   if (table_list_iterator == table_list.end())
00101     return false;
00102 
00103   table= *table_list_iterator;
00104 
00105   if (checkSchemaName())
00106     return false;
00107 
00108   return true;
00109 }
00110 
00111 bool ShowTableStatus::Generator::next()
00112 {
00113   while (not nextCore())
00114   {
00115     if (table_list_iterator != table_list.end())
00116       continue;
00117 
00118     return false;
00119   }
00120 
00121   return true;
00122 }
00123 
00124 bool ShowTableStatus::Generator::checkSchemaName()
00125 {
00126   if (not schema_predicate.empty() && schema_predicate.compare(schema_name()))
00127     return true;
00128 
00129   return false;
00130 }
00131 
00132 const char *ShowTableStatus::Generator::schema_name()
00133 {
00134   return table->getShare()->getSchemaName();
00135 }
00136 
00137 bool ShowTableStatus::Generator::populate()
00138 {
00139   if (not next())
00140     return false;
00141 
00142   fill();
00143 
00144   return true;
00145 }
00146 
00147 void ShowTableStatus::Generator::fill()
00148 {
00154   /* Session 1 */
00155   if (table->getSession())
00156     push(table->getSession()->getSessionId());
00157   else
00158     push(static_cast<int64_t>(0));
00159 
00160   /* Schema 2 */
00161   push(table->getShare()->getSchemaName());
00162 
00163   /* Name  3 */
00164   push(table->getShare()->getTableName());
00165 
00166   /* Type  4 */
00167   push(table->getShare()->getTableTypeAsString());
00168 
00169   /* Engine 5 */
00170   push(table->getEngine()->getName());
00171 
00172   /* Version 6 */
00173   push(static_cast<int64_t>(table->getShare()->getVersion()));
00174 
00175   /* Rows 7 */
00176   push(static_cast<uint64_t>(table->getCursor().records()));
00177 
00178   /* Avg_row_length 8 */
00179   push(table->getCursor().rowSize());
00180 
00181   /* Table_size 9 */
00182   push(table->getCursor().tableSize());
00183 
00184   /* Auto_increment 10 */
00185   bool session_set= false;
00186   if (table->in_use == NULL)
00187   {
00188     table->in_use= &getSession();
00189     session_set= true;
00190   }
00191 
00192   table->getCursor().info(HA_STATUS_AUTO);
00193   push(table->getCursor().getAutoIncrement());
00194 
00195   if (session_set)
00196     table->in_use= NULL;
00197 }