~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/schema_dictionary/tables.cc

  • Committer: Monty Taylor
  • Date: 2010-03-11 18:27:20 UTC
  • mfrom: (1333 staging)
  • mto: This revision was merged to the branch mainline in revision 1348.
  • Revision ID: mordred@inaugust.com-20100311182720-hd1h87y6cb1b1mp0
Merged trunk.

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 Sun Microsystems
 
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/schema_dictionary/dictionary.h"
 
23
#include "drizzled/table_identifier.h"
 
24
 
 
25
using namespace std;
 
26
using namespace drizzled;
 
27
 
 
28
static const string STANDARD("STANDARD");
 
29
static const string TEMPORARY("TEMPORARY");
 
30
static const string INTERNAL("INTERNAL");
 
31
static const string FUNCTION("FUNCTION");
 
32
 
 
33
static const string DEFAULT("DEFAULT");
 
34
static const string FIXED("FIXED");
 
35
static const string DYNAMIC("DYNAMIC");
 
36
static const string COMPRESSED("COMPRESSED");
 
37
static const string REDUNDANT("REDUNDANT");
 
38
static const string COMPACT("COMPACT");
 
39
static const string PAGE("PAGE");
 
40
 
 
41
static const string VARCHAR("VARCHAR");
 
42
static const string DOUBLE("DOUBLE");
 
43
static const string BLOB("BLOB");
 
44
static const string ENUM("ENUM");
 
45
static const string INTEGER("INTEGER");
 
46
static const string BIGINT("BIGINT");
 
47
static const string DECIMAL("DECIMAL");
 
48
static const string DATE("DATE");
 
49
static const string TIMESTAMP("TIMESTAMP");
 
50
static const string DATETIME("DATETIME");
 
51
 
 
52
TablesTool::TablesTool() :
 
53
  SchemasTool("TABLES")
 
54
{
 
55
  add_field("TABLE_SCHEMA");
 
56
  add_field("TABLE_NAME");
 
57
  add_field("TABLE_TYPE");
 
58
  add_field("ENGINE");
 
59
  add_field("ROW_FORMAT", 10);
 
60
  add_field("TABLE_COLLATION");
 
61
  add_field("TABLE_COMMENT", 2048);
 
62
}
 
63
 
 
64
TablesTool::Generator::Generator(Field **arg) :
 
65
  SchemasTool::Generator(arg),
 
66
  is_tables_primed(false)
 
67
{
 
68
}
 
69
 
 
70
bool TablesTool::Generator::nextTableCore()
 
71
{
 
72
  if (is_tables_primed)
 
73
  {
 
74
    table_iterator++;
 
75
  }
 
76
  else
 
77
  {
 
78
    if (not isSchemaPrimed())
 
79
     return false;
 
80
 
 
81
    table_names.clear();
 
82
    plugin::StorageEngine::getTableNames(schema_name(), table_names);
 
83
    table_iterator= table_names.begin();
 
84
    is_tables_primed= true;
 
85
  }
 
86
 
 
87
  if (table_iterator == table_names.end())
 
88
    return false;
 
89
 
 
90
  table_proto.Clear();
 
91
  {
 
92
    Session *session= current_session;
 
93
    char path[FN_REFLEN];
 
94
    build_table_filename(path, sizeof(path), schema_name().c_str(), table_name().c_str(), false);
 
95
    plugin::StorageEngine::getTableDefinition(*session,
 
96
                                             path,
 
97
                                             schema_name().c_str(),
 
98
                                             table_name().c_str(),
 
99
                                             false,
 
100
                                             &table_proto);
 
101
  }
 
102
 
 
103
  if (checkTableName())
 
104
    return false;
 
105
 
 
106
  return true;
 
107
}
 
108
 
 
109
bool TablesTool::Generator::nextTable()
 
110
{
 
111
  while (not nextTableCore())
 
112
  {
 
113
 
 
114
    if (is_tables_primed && table_iterator != table_names.end())
 
115
      continue;
 
116
 
 
117
    if (not nextSchema())
 
118
      return false;
 
119
    is_tables_primed= false;
 
120
  }
 
121
 
 
122
  return true;
 
123
}
 
124
 
 
125
bool TablesTool::Generator::checkTableName()
 
126
{
 
127
  if (isWild(table_name()))
 
128
    return true;
 
129
 
 
130
  if (not table_predicate.empty() && table_predicate.compare(table_name()))
 
131
    return true;
 
132
 
 
133
  return false;
 
134
}
 
135
 
 
136
bool TablesTool::Generator::populate()
 
137
{
 
138
  if (not nextTable())
 
139
    return false;
 
140
 
 
141
  fill();
 
142
 
 
143
  return true;
 
144
}
 
145
 
 
146
void TablesTool::Generator::pushRow(message::Table::TableOptions::RowType type)
 
147
{
 
148
  switch (type)
 
149
  {
 
150
  default:
 
151
  case message::Table::TableOptions::ROW_TYPE_DEFAULT:
 
152
    push(DEFAULT);
 
153
    break;
 
154
  case message::Table::TableOptions::ROW_TYPE_FIXED:
 
155
    push(FIXED);
 
156
    break;
 
157
  case message::Table::TableOptions::ROW_TYPE_DYNAMIC:
 
158
    push(DYNAMIC);
 
159
    break;
 
160
  case message::Table::TableOptions::ROW_TYPE_COMPRESSED:
 
161
    push(COMPRESSED);
 
162
    break;
 
163
  case message::Table::TableOptions::ROW_TYPE_REDUNDANT:
 
164
    push(REDUNDANT);
 
165
    break;
 
166
  case message::Table::TableOptions::ROW_TYPE_COMPACT:
 
167
    push(COMPACT);
 
168
    break;
 
169
  case message::Table::TableOptions::ROW_TYPE_PAGE:
 
170
    push(PAGE);
 
171
    break;
 
172
  }
 
173
}
 
174
 
 
175
void TablesTool::Generator::pushType(message::Table::Field::FieldType type)
 
176
{
 
177
  switch (type)
 
178
  {
 
179
  default:
 
180
  case message::Table::Field::VARCHAR:
 
181
    push(VARCHAR);
 
182
    break;
 
183
  case message::Table::Field::DOUBLE:
 
184
    push(DOUBLE);
 
185
    break;
 
186
  case message::Table::Field::BLOB:
 
187
    push(BLOB);
 
188
    break;
 
189
  case message::Table::Field::ENUM:
 
190
    push(ENUM);
 
191
    break;
 
192
  case message::Table::Field::INTEGER:
 
193
    push(INTEGER);
 
194
    break;
 
195
  case message::Table::Field::BIGINT:
 
196
    push(BIGINT);
 
197
    break;
 
198
  case message::Table::Field::DECIMAL:
 
199
    push(DECIMAL);
 
200
    break;
 
201
  case message::Table::Field::DATE:
 
202
    push(DATE);
 
203
    break;
 
204
  case message::Table::Field::TIMESTAMP:
 
205
    push(TIMESTAMP);
 
206
    break;
 
207
  case message::Table::Field::DATETIME:
 
208
    push(DATETIME);
 
209
    break;
 
210
  }
 
211
}
 
212
 
 
213
void TablesTool::Generator::fill()
 
214
{
 
215
 
 
216
  /* TABLE_SCHEMA */
 
217
  push(schema_name());
 
218
 
 
219
  /* TABLE_NAME */
 
220
  push(table_name());
 
221
 
 
222
  /* TABLE_TYPE */
 
223
  {
 
224
    switch (table_proto.type())
 
225
    {
 
226
    default:
 
227
    case message::Table::STANDARD:
 
228
      push(STANDARD);
 
229
      break;
 
230
    case message::Table::TEMPORARY:
 
231
      push(TEMPORARY);
 
232
      break;
 
233
    case message::Table::INTERNAL:
 
234
      push(INTERNAL);
 
235
      break;
 
236
    case message::Table::FUNCTION:
 
237
      push(FUNCTION);
 
238
      break;
 
239
    }
 
240
  }
 
241
 
 
242
  /* ENGINE */
 
243
  push(table_proto.engine().name());
 
244
 
 
245
  /* ROW_FORMAT */
 
246
  pushRow(table_proto.options().row_type());
 
247
 
 
248
  /* TABLE_COLLATION */
 
249
  push(table_proto.options().collation());
 
250
 
 
251
  /* TABLE_COMMENT */
 
252
  push(table_proto.options().comment());
 
253
}
 
254
 
 
255
bool ShowTables::Generator::checkSchema()
 
256
{
 
257
  Session *session= current_session;
 
258
 
 
259
  if (session->lex->select_lex.db)
 
260
  {
 
261
    return schema_name().compare(session->lex->select_lex.db);
 
262
  }
 
263
  return session->db.compare(schema_name());
 
264
}