~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/show_dictionary/show_columns.cc

Reverted 1103

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
static const string VARCHAR("VARCHAR");
30
 
/* VARBINARY already defined elsewhere */
31
 
static const string VARBIN("VARBINARY");
32
 
static const string DOUBLE("DOUBLE");
33
 
static const string BLOB("BLOB");
34
 
static const string TEXT("TEXT");
35
 
static const string ENUM("ENUM");
36
 
static const string INTEGER("INTEGER");
37
 
static const string BIGINT("BIGINT");
38
 
static const string DECIMAL("DECIMAL");
39
 
static const string DATE("DATE");
40
 
static const string TIMESTAMP("TIMESTAMP");
41
 
static const string DATETIME("DATETIME");
42
 
 
43
 
ShowColumns::ShowColumns() :
44
 
  show_dictionary::Show("SHOW_COLUMNS")
45
 
{
46
 
  add_field("Field");
47
 
  add_field("Type");
48
 
  add_field("Null", plugin::TableFunction::BOOLEAN, 0 , false);
49
 
  add_field("Default");
50
 
  add_field("Default_is_NULL", plugin::TableFunction::BOOLEAN, 0, false);
51
 
  add_field("On_Update");
52
 
}
53
 
 
54
 
ShowColumns::Generator::Generator(Field **arg) :
55
 
  show_dictionary::Show::Generator(arg),
56
 
  is_tables_primed(false),
57
 
  is_columns_primed(false),
58
 
  column_iterator(0)
59
 
{
60
 
  if (not isShowQuery())
61
 
   return;
62
 
 
63
 
  statement::Show *select= static_cast<statement::Show *>(getSession().lex->statement);
64
 
 
65
 
  if (not select->getShowTable().empty() && not select->getShowSchema().empty())
66
 
  {
67
 
    table_name.append(select->getShowTable().c_str());
68
 
    identifier::Table identifier(select->getShowSchema().c_str(), select->getShowTable().c_str());
69
 
 
70
 
    is_tables_primed= plugin::StorageEngine::getTableDefinition(getSession(),
71
 
                                                                identifier,
72
 
                                                                table_proto);
73
 
  }
74
 
}
75
 
 
76
 
bool ShowColumns::Generator::nextColumnCore()
77
 
{
78
 
  if (is_columns_primed)
79
 
  {
80
 
    column_iterator++;
81
 
  }
82
 
  else
83
 
  {
84
 
    if (not isTablesPrimed())
85
 
      return false;
86
 
 
87
 
    column_iterator= 0;
88
 
    is_columns_primed= true;
89
 
  }
90
 
 
91
 
  if (column_iterator >= getTableProto()->field_size())
92
 
    return false;
93
 
 
94
 
  column= getTableProto()->field(column_iterator);
95
 
 
96
 
  return true;
97
 
}
98
 
 
99
 
 
100
 
bool ShowColumns::Generator::nextColumn()
101
 
{
102
 
  while (not nextColumnCore())
103
 
  {
104
 
    return false;
105
 
  }
106
 
 
107
 
  return true;
108
 
}
109
 
 
110
 
bool ShowColumns::Generator::populate()
111
 
{
112
 
 
113
 
  if (not nextColumn())
114
 
    return false;
115
 
 
116
 
  fill();
117
 
 
118
 
  return true;
119
 
}
120
 
 
121
 
void ShowColumns::Generator::pushType(message::Table::Field::FieldType type, const string collation)
122
 
{
123
 
  switch (type)
124
 
  {
125
 
  default:
126
 
  case message::Table::Field::VARCHAR:
127
 
    push(collation.compare("binary") ? VARCHAR : VARBIN);
128
 
    break;
129
 
  case message::Table::Field::DOUBLE:
130
 
    push(DOUBLE);
131
 
    break;
132
 
  case message::Table::Field::BLOB:
133
 
    push(collation.compare("binary") ? TEXT : BLOB);
134
 
    break;
135
 
  case message::Table::Field::ENUM:
136
 
    push(ENUM);
137
 
    break;
138
 
  case message::Table::Field::INTEGER:
139
 
    push(INTEGER);
140
 
    break;
141
 
  case message::Table::Field::BIGINT:
142
 
    push(BIGINT);
143
 
    break;
144
 
  case message::Table::Field::DECIMAL:
145
 
    push(DECIMAL);
146
 
    break;
147
 
  case message::Table::Field::DATE:
148
 
    push(DATE);
149
 
    break;
150
 
  case message::Table::Field::EPOCH:
151
 
    push(TIMESTAMP);
152
 
    break;
153
 
  case message::Table::Field::DATETIME:
154
 
    push(DATETIME);
155
 
    break;
156
 
  }
157
 
}
158
 
 
159
 
 
160
 
void ShowColumns::Generator::fill()
161
 
{
162
 
  /* Field */
163
 
  push(column.name());
164
 
 
165
 
  /* Type */
166
 
  pushType(column.type(), column.string_options().collation());
167
 
 
168
 
  /* Null */
169
 
  push(not column.constraints().is_notnull());
170
 
 
171
 
  /* Default */
172
 
  if (column.options().has_default_value())
173
 
    push(column.options().default_value());
174
 
  else if (column.options().has_default_expression())
175
 
    push(column.options().default_expression());
176
 
  else
177
 
    push(column.options().default_bin_value());
178
 
 
179
 
  /* Default_is_NULL */
180
 
  push(column.options().default_null());
181
 
 
182
 
  /* On_Update */
183
 
  push(column.options().update_expression());
184
 
}