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