~drizzle-trunk/drizzle/development

1309.2.4 by Brian Aker
New version of show columns code.
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
22
#include <plugin/show_dictionary/dictionary.h>
23
#include <drizzled/identifier.h>
1753.2.1 by Andrew Hutchings
Use collation in SHOW FIELDS FROM so that it can detect the difference beween TEXT and BLOB (and VARCHAR/VARBINARY)
24
#include <string>
1309.2.4 by Brian Aker
New version of show columns code.
25
26
using namespace std;
27
using namespace drizzled;
28
29
ShowColumns::ShowColumns() :
1874.2.4 by Brian Aker
Have show functions be invisible.
30
  show_dictionary::Show("SHOW_COLUMNS")
1309.2.4 by Brian Aker
New version of show columns code.
31
{
32
  add_field("Field");
33
  add_field("Type");
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
34
  add_field("Null", plugin::TableFunction::BOOLEAN, 0 , false);
1309.2.4 by Brian Aker
New version of show columns code.
35
  add_field("Default");
1643.3.10 by Brian Aker
Column support, clean up of IS/DD for NULL type.
36
  add_field("Default_is_NULL", plugin::TableFunction::BOOLEAN, 0, false);
1309.2.4 by Brian Aker
New version of show columns code.
37
  add_field("On_Update");
38
}
39
40
ShowColumns::Generator::Generator(Field **arg) :
1874.2.4 by Brian Aker
Have show functions be invisible.
41
  show_dictionary::Show::Generator(arg),
1309.2.4 by Brian Aker
New version of show columns code.
42
  is_tables_primed(false),
43
  is_columns_primed(false),
44
  column_iterator(0)
45
{
2059.1.1 by Andrew Hutchings
Refix show_dictionary plugin crash when not using 'SHOW' to access.
46
  if (not isShowQuery())
47
   return;
48
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
49
  statement::Show& select= static_cast<statement::Show&>(statement());
1309.2.4 by Brian Aker
New version of show columns code.
50
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
51
  if (not select.getShowTable().empty() && not select.getShowSchema().empty())
1448 by Brian Aker
Fix for bug on show tables.:w
52
  {
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
53
    table_name.append(select.getShowTable().c_str());
54
    identifier::Table identifier(select.getShowSchema().c_str(), select.getShowTable().c_str());
1309.2.4 by Brian Aker
New version of show columns code.
55
2179.6.8 by Stewart Smith
add authorization check to SHOW COLUMNS
56
    if (not plugin::Authorization::isAuthorized(*getSession().user(),
57
                                            identifier, false))
58
    {
59
      drizzled::error::access(*getSession().user(), identifier);
60
      return;
61
    }
62
2159.2.4 by Brian Aker
Merge in error wasteful removal.
63
    table_proto= plugin::StorageEngine::getTableMessage(getSession(), identifier);
2159.2.3 by Brian Aker
Remove dead call for getTableMessage()
64
65
    if (table_proto)
66
      is_tables_primed= true;
1448 by Brian Aker
Fix for bug on show tables.:w
67
  }
1309.2.4 by Brian Aker
New version of show columns code.
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
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
85
  if (column_iterator >= getTableProto()->field_size())
1309.2.4 by Brian Aker
New version of show columns code.
86
    return false;
87
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
88
  column= getTableProto()->field(column_iterator);
1309.2.4 by Brian Aker
New version of show columns code.
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 */
2363.1.3 by Brian Aker
Fix for bug lp:743783 , we just now use type: in the plugin (i.e. we use the API).
122
  push(drizzled::message::type(column));
1309.2.4 by Brian Aker
New version of show columns code.
123
124
  /* Null */
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
125
  push(not column.constraints().is_notnull());
1309.2.4 by Brian Aker
New version of show columns code.
126
127
  /* Default */
1638.3.2 by Stewart Smith
use default_expression and update_expression instead of default_value and update_value in the table proto for NOW()
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());
1309.2.4 by Brian Aker
New version of show columns code.
134
135
  /* Default_is_NULL */
136
  push(column.options().default_null());
137
138
  /* On_Update */
1638.3.2 by Stewart Smith
use default_expression and update_expression instead of default_value and update_value in the table proto for NOW()
139
  push(column.options().update_expression());
1309.2.4 by Brian Aker
New version of show columns code.
140
}