Drizzled Public API Documentation

show_indexes.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 #include <plugin/show_dictionary/dictionary.h>
23 #include <drizzled/identifier.h>
24 
25 
26 using namespace std;
27 using namespace drizzled;
28 
29 ShowIndexes::ShowIndexes() :
30  show_dictionary::Show("SHOW_INDEXES")
31 {
32  add_field("Table");
33  add_field("Unique", plugin::TableFunction::BOOLEAN, 0, false);
34  add_field("Key_name");
35  add_field("Seq_in_index", plugin::TableFunction::NUMBER, 0, false);
36  add_field("Column_name");
37 }
38 
39 ShowIndexes::Generator::Generator(Field **arg) :
40  show_dictionary::Show::Generator(arg),
41  is_tables_primed(false),
42  is_index_primed(false),
43  is_index_part_primed(false),
44  index_iterator(0),
45  index_part_iterator(0)
46 {
47  if (not isShowQuery())
48  return;
49 
50  statement::Show& select= static_cast<statement::Show&>(statement());
51 
52  if (not select.getShowTable().empty() && not select.getShowSchema().empty())
53  {
54  table_name.append(select.getShowTable().c_str());
55  identifier::Table identifier(getSession().catalog().identifier(),
56  select.getShowSchema().c_str(),
57  select.getShowTable().c_str());
58 
59  if (not plugin::Authorization::isAuthorized(*getSession().user(),
60  identifier, false))
61  {
62  drizzled::error::access(*getSession().user(), identifier);
63  return;
64  }
65 
66  table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
67 
68  if (table_proto)
69  is_tables_primed= true;
70  }
71 }
72 
73 bool ShowIndexes::Generator::nextIndexCore()
74 {
75  if (isIndexesPrimed())
76  {
77  index_iterator++;
78  }
79  else
80  {
81  if (not isTablesPrimed())
82  return false;
83 
84  index_iterator= 0;
85  is_index_primed= true;
86  }
87 
88  if (index_iterator >= getTableProto().indexes_size())
89  return false;
90 
91  index= getTableProto().indexes(index_iterator);
92 
93  return true;
94 }
95 
96 bool ShowIndexes::Generator::nextIndex()
97 {
98  while (not nextIndexCore())
99  {
100  return false;
101  }
102 
103  return true;
104 }
105 
106 bool ShowIndexes::Generator::nextIndexPartsCore()
107 {
108  if (is_index_part_primed)
109  {
110  index_part_iterator++;
111  }
112  else
113  {
114  if (not isIndexesPrimed())
115  return false;
116 
117  index_part_iterator= 0;
118  is_index_part_primed= true;
119  }
120 
121  if (index_part_iterator >= getIndex().index_part_size())
122  return false;
123 
124  index_part= getIndex().index_part(index_part_iterator);
125 
126  return true;
127 }
128 
129 
130 bool ShowIndexes::Generator::nextIndexParts()
131 {
132  while (not nextIndexPartsCore())
133  {
134  if (not nextIndex())
135  return false;
136  is_index_part_primed= false;
137  }
138 
139  return true;
140 }
141 
142 
143 
144 bool ShowIndexes::Generator::populate()
145 {
146  if (not nextIndexParts())
147  return false;
148 
149  fill();
150 
151  return true;
152 }
153 
154 void ShowIndexes::Generator::fill()
155 {
156  /* Table */
157  push(getTableName());
158 
159  /* Unique */
160  push(getIndex().is_unique());
161 
162  /* Key_name */
163  push(getIndex().name());
164 
165  /* Seq_in_index */
166  push(static_cast<int64_t>(index_part_iterator + 1));
167 
168  /* Column_name */
169  push(getTableProto().field(getIndexPart().fieldnr()).name());
170 }
TODO: Rename this file - func.h is stupid.