Drizzled Public API Documentation

show_table_status.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 
23 #include <plugin/show_dictionary/dictionary.h>
24 #include <drizzled/open_tables_state.h>
25 #include <drizzled/table/cache.h>
26 #include <drizzled/pthread_globals.h>
27 
28 using namespace drizzled;
29 using namespace std;
30 
31 ShowTableStatus::ShowTableStatus() :
32  show_dictionary::Show("SHOW_TABLE_STATUS")
33 {
34  add_field("Session", plugin::TableFunction::NUMBER, 0, false);
35  add_field("Schema");
36  add_field("Name");
37  add_field("Type");
38  add_field("Engine");
39  add_field("Version");
40  add_field("Rows");
41  add_field("Avg_row_length");
42  add_field("Table_size");
43  add_field("Auto_increment");
44 }
45 
46 ShowTableStatus::Generator::Generator(drizzled::Field **arg) :
47  show_dictionary::Show::Generator(arg),
48  is_primed(false),
49  scopedLock(table::Cache::mutex())
50 {
51  if (not isShowQuery())
52  return;
53 
54  statement::Show& select= static_cast<statement::Show&>(statement());
55 
56  schema_predicate.append(select.getShowSchema());
57 
58  util::string::ptr schema(getSession().schema());
59  if (schema_predicate.empty() and schema)
60  {
61  schema_predicate.append(*schema);
62  }
63 
64  if (not schema_predicate.empty())
65  {
66  table::CacheMap &open_cache(table::getCache());
67 
68  for (table::CacheMap::const_iterator iter= open_cache.begin();
69  iter != open_cache.end();
70  iter++)
71  {
72  table_list.push_back(iter->second);
73  }
74 
75  for (drizzled::Table *tmp_table= getSession().open_tables.getTemporaryTables(); tmp_table; tmp_table= tmp_table->getNext())
76  {
77  if (tmp_table->getShare())
78  {
79  table_list.push_back(tmp_table);
80  }
81  }
82  std::sort(table_list.begin(), table_list.end(), Table::compare);
83  }
84 }
85 
86 ShowTableStatus::Generator::~Generator()
87 {
88 }
89 
90 bool ShowTableStatus::Generator::nextCore()
91 {
92  if (is_primed)
93  {
94  table_list_iterator++;
95  }
96  else
97  {
98  is_primed= true;
99  table_list_iterator= table_list.begin();
100  }
101 
102  if (table_list_iterator == table_list.end())
103  return false;
104 
105  table= *table_list_iterator;
106 
107  if (checkSchemaName())
108  return false;
109 
110  return true;
111 }
112 
113 bool ShowTableStatus::Generator::next()
114 {
115  while (not nextCore())
116  {
117  if (table_list_iterator != table_list.end())
118  continue;
119 
120  return false;
121  }
122 
123  return true;
124 }
125 
126 bool ShowTableStatus::Generator::checkSchemaName()
127 {
128  if (not schema_predicate.empty() && schema_predicate.compare(schema_name()))
129  return true;
130 
131  return false;
132 }
133 
134 const char *ShowTableStatus::Generator::schema_name()
135 {
136  return table->getShare()->getSchemaName();
137 }
138 
139 bool ShowTableStatus::Generator::populate()
140 {
141  if (not next())
142  return false;
143 
144  fill();
145 
146  return true;
147 }
148 
150 {
156  /* Session 1 */
157  if (table->getSession())
158  push(table->getSession()->getSessionId());
159  else
160  push(static_cast<int64_t>(0));
161 
162  /* Schema 2 */
163  push(table->getShare()->getSchemaName());
164 
165  /* Name 3 */
166  push(table->getShare()->getTableName());
167 
168  /* Type 4 */
169  push(table->getShare()->getTableTypeAsString());
170 
171  /* Engine 5 */
172  push(table->getEngine()->getName());
173 
174  /* Version 6 */
175  push(static_cast<int64_t>(table->getShare()->getVersion()));
176 
177  /* Rows 7 */
178  push(static_cast<uint64_t>(table->getCursor().records()));
179 
180  /* Avg_row_length 8 */
181  push(table->getCursor().rowSize());
182 
183  /* Table_size 9 */
184  push(table->getCursor().tableSize());
185 
186  /* Auto_increment 10 */
187  bool session_set= false;
188  if (table->in_use == NULL)
189  {
190  table->in_use= &getSession();
191  session_set= true;
192  }
193 
194  table->getCursor().info(HA_STATUS_AUTO);
195  push(table->getCursor().getAutoIncrement());
196 
197  if (session_set)
198  table->in_use= NULL;
199 }