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(select.getShowSchema().c_str(), select.getShowTable().c_str());
56 
57  if (not plugin::Authorization::isAuthorized(*getSession().user(),
58  identifier, false))
59  {
60  drizzled::error::access(*getSession().user(), identifier);
61  return;
62  }
63 
64  table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
65 
66  if (table_proto)
67  is_tables_primed= true;
68  }
69 }
70 
71 bool ShowIndexes::Generator::nextIndexCore()
72 {
73  if (isIndexesPrimed())
74  {
75  index_iterator++;
76  }
77  else
78  {
79  if (not isTablesPrimed())
80  return false;
81 
82  index_iterator= 0;
83  is_index_primed= true;
84  }
85 
86  if (index_iterator >= getTableProto().indexes_size())
87  return false;
88 
89  index= getTableProto().indexes(index_iterator);
90 
91  return true;
92 }
93 
94 bool ShowIndexes::Generator::nextIndex()
95 {
96  while (not nextIndexCore())
97  {
98  return false;
99  }
100 
101  return true;
102 }
103 
104 bool ShowIndexes::Generator::nextIndexPartsCore()
105 {
106  if (is_index_part_primed)
107  {
108  index_part_iterator++;
109  }
110  else
111  {
112  if (not isIndexesPrimed())
113  return false;
114 
115  index_part_iterator= 0;
116  is_index_part_primed= true;
117  }
118 
119  if (index_part_iterator >= getIndex().index_part_size())
120  return false;
121 
122  index_part= getIndex().index_part(index_part_iterator);
123 
124  return true;
125 }
126 
127 
128 bool ShowIndexes::Generator::nextIndexParts()
129 {
130  while (not nextIndexPartsCore())
131  {
132  if (not nextIndex())
133  return false;
134  is_index_part_primed= false;
135  }
136 
137  return true;
138 }
139 
140 
141 
142 bool ShowIndexes::Generator::populate()
143 {
144  if (not nextIndexParts())
145  return false;
146 
147  fill();
148 
149  return true;
150 }
151 
152 void ShowIndexes::Generator::fill()
153 {
154  /* Table */
155  push(getTableName());
156 
157  /* Unique */
158  push(getIndex().is_unique());
159 
160  /* Key_name */
161  push(getIndex().name());
162 
163  /* Seq_in_index */
164  push(static_cast<int64_t>(index_part_iterator + 1));
165 
166  /* Column_name */
167  push(getTableProto().field(getIndexPart().fieldnr()).name());
168 }