Drizzled Public API Documentation

show_columns.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 #include <string>
25 
26 using namespace std;
27 using namespace drizzled;
28 
29 ShowColumns::ShowColumns() :
30  show_dictionary::Show("SHOW_COLUMNS")
31 {
32  add_field("Field");
33  add_field("Type");
34  add_field("Null", plugin::TableFunction::BOOLEAN, 0 , false);
35  add_field("Default");
36  add_field("Default_is_NULL", plugin::TableFunction::BOOLEAN, 0, false);
37  add_field("On_Update");
38 }
39 
40 ShowColumns::Generator::Generator(Field **arg) :
41  show_dictionary::Show::Generator(arg),
42  is_tables_primed(false),
43  is_columns_primed(false),
44  column_iterator(0)
45 {
46  if (not isShowQuery())
47  return;
48 
49  statement::Show& select= static_cast<statement::Show&>(statement());
50 
51  if (not select.getShowTable().empty() && not select.getShowSchema().empty())
52  {
53  table_name.append(select.getShowTable().c_str());
54  identifier::Table identifier(select.getShowSchema().c_str(), select.getShowTable().c_str());
55 
56  if (not plugin::Authorization::isAuthorized(*getSession().user(),
57  identifier, false))
58  {
59  drizzled::error::access(*getSession().user(), identifier);
60  return;
61  }
62 
63  table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
64 
65  if (table_proto)
66  is_tables_primed= true;
67  }
68 }
69 
70 bool ShowColumns::Generator::nextColumnCore()
71 {
72  if (is_columns_primed)
73  {
74  column_iterator++;
75  }
76  else
77  {
78  if (not isTablesPrimed())
79  return false;
80 
81  column_iterator= 0;
82  is_columns_primed= true;
83  }
84 
85  if (column_iterator >= getTableProto()->field_size())
86  return false;
87 
88  column= getTableProto()->field(column_iterator);
89 
90  return true;
91 }
92 
93 
94 bool ShowColumns::Generator::nextColumn()
95 {
96  while (not nextColumnCore())
97  {
98  return false;
99  }
100 
101  return true;
102 }
103 
104 bool ShowColumns::Generator::populate()
105 {
106 
107  if (not nextColumn())
108  return false;
109 
110  fill();
111 
112  return true;
113 }
114 
115 
116 void ShowColumns::Generator::fill()
117 {
118  /* Field */
119  push(column.name());
120 
121  /* Type */
122  push(drizzled::message::type(column));
123 
124  /* Null */
125  push(not column.constraints().is_notnull());
126 
127  /* Default */
128  if (column.options().has_default_value())
129  push(column.options().default_value());
130  else if (column.options().has_default_expression())
131  push(column.options().default_expression());
132  else
133  push(column.options().default_bin_value());
134 
135  /* Default_is_NULL */
136  push(column.options().default_null());
137 
138  /* On_Update */
139  push(column.options().update_expression());
140 }