drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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
26using namespace std;
27using namespace drizzled;
28
29ShowColumns::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
40ShowColumns::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(getSession().catalog().identifier(),
55 select.getShowSchema().c_str(),
56 select.getShowTable().c_str());
57
58 if (not plugin::Authorization::isAuthorized(*getSession().user(),
59 identifier, false))
60 {
61 drizzled::error::access(*getSession().user(), identifier);
62 return;
63 }
64
65 table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
66
67 if (table_proto)
68 is_tables_primed= true;
69 }
70}
71
72bool ShowColumns::Generator::nextColumnCore()
73{
74 if (is_columns_primed)
75 {
76 column_iterator++;
77 }
78 else
79 {
80 if (not isTablesPrimed())
81 return false;
82
83 column_iterator= 0;
84 is_columns_primed= true;
85 }
86
87 if (column_iterator >= getTableProto()->field_size())
88 return false;
89
90 column= getTableProto()->field(column_iterator);
91
92 return true;
93}
94
95
96bool ShowColumns::Generator::nextColumn()
97{
98 while (not nextColumnCore())
99 {
100 return false;
101 }
102
103 return true;
104}
105
106bool ShowColumns::Generator::populate()
107{
108
109 if (not nextColumn())
110 return false;
111
112 fill();
113
114 return true;
115}
116
117
118void ShowColumns::Generator::fill()
119{
120 /* Field */
121 push(column.name());
122
123 /* Type */
124 push(drizzled::message::type(column));
125
126 /* Null */
127 push(not column.constraints().is_notnull());
128
129 /* Default */
130 if (column.options().has_default_value())
131 push(column.options().default_value());
132 else if (column.options().has_default_expression())
133 push(column.options().default_expression());
134 else
135 push(column.options().default_bin_value());
136
137 /* Default_is_NULL */
138 push(column.options().default_null());
139
140 /* On_Update */
141 push(column.options().update_expression());
142}
TODO: Rename this file - func.h is stupid.
STL namespace.