~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.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) 2009 Sun Microsystems, Inc.
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
 
 
23
1
#include <sys/types.h>
24
2
#include <sys/stat.h>
25
3
#include <fcntl.h>
30
8
 
31
9
#include <iostream>
32
10
#include <string>
33
 
#include <drizzled/message/statement_transform.h>
 
11
#include <drizzled/message/table.pb.h>
34
12
#include <google/protobuf/io/zero_copy_stream.h>
35
13
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
14
 
42
20
  Written from Google proto example
43
21
*/
44
22
 
 
23
static void print_field(const message::Table::Field &field)
 
24
{
 
25
  cout << "\t`" << field.name() << "`";
 
26
 
 
27
  message::Table::Field::FieldType field_type= field.type();
 
28
 
 
29
  if(field_type==message::Table::Field::VIRTUAL)
 
30
  {
 
31
    cout << " VIRTUAL"; // FIXME
 
32
    field_type= field.virtual_options().type();
 
33
  }
 
34
 
 
35
  switch (field_type)
 
36
  {
 
37
    case message::Table::Field::DOUBLE:
 
38
    cout << " DOUBLE ";
 
39
    break;
 
40
  case message::Table::Field::VARCHAR:
 
41
    cout << " VARCHAR(" << field.string_options().length() << ")";
 
42
    break;
 
43
  case message::Table::Field::BLOB:
 
44
    cout << " BLOB "; /* FIXME: or text, depends on collation */
 
45
    if(field.string_options().has_collation_id())
 
46
      cout << "COLLATION=" << field.string_options().collation_id() << " ";
 
47
    break;
 
48
  case message::Table::Field::ENUM:
 
49
    {
 
50
      int x;
 
51
 
 
52
      cout << " ENUM(";
 
53
      for (x= 0; x < field.set_options().field_value_size() ; x++)
 
54
      {
 
55
        const string type= field.set_options().field_value(x);
 
56
 
 
57
        if (x != 0)
 
58
          cout << ",";
 
59
        cout << "'" << type << "'";
 
60
      }
 
61
      cout << ") ";
 
62
      break;
 
63
    }
 
64
  case message::Table::Field::TINYINT:
 
65
    cout << " TINYINT ";
 
66
    break;
 
67
  case message::Table::Field::INTEGER:
 
68
    cout << " INT" ;
 
69
    break;
 
70
  case message::Table::Field::BIGINT:
 
71
    cout << " BIGINT ";
 
72
    break;
 
73
  case message::Table::Field::DECIMAL:
 
74
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
 
75
    break;
 
76
  case message::Table::Field::DATE:
 
77
    cout << " DATE ";
 
78
    break;
 
79
  case message::Table::Field::TIME:
 
80
    cout << " TIME ";
 
81
    break;
 
82
  case message::Table::Field::TIMESTAMP:
 
83
    cout << " TIMESTAMP ";
 
84
    break;
 
85
  case message::Table::Field::DATETIME:
 
86
    cout << " DATETIME ";
 
87
    break;
 
88
  case message::Table::Field::VIRTUAL:
 
89
    abort(); // handled above.
 
90
  }
 
91
 
 
92
  if(field.type()==message::Table::Field::VIRTUAL)
 
93
  {
 
94
    cout << " AS (" << field.virtual_options().expression() << ") ";
 
95
    if(field.virtual_options().physically_stored())
 
96
      cout << " STORED ";
 
97
  }
 
98
 
 
99
  if (field.type() == message::Table::Field::INTEGER
 
100
      || field.type() == message::Table::Field::BIGINT
 
101
      || field.type() == message::Table::Field::TINYINT)
 
102
  {
 
103
    if (field.has_constraints()
 
104
        && field.constraints().has_is_unsigned())
 
105
      if (field.constraints().is_unsigned())
 
106
        cout << " UNSIGNED";
 
107
 
 
108
    if (field.has_numeric_options() &&
 
109
      field.numeric_options().is_autoincrement())
 
110
      cout << " AUTOINCREMENT ";
 
111
  }
 
112
 
 
113
  if (!( field.has_constraints()
 
114
         && field.constraints().is_nullable()))
 
115
    cout << " NOT NULL ";
 
116
 
 
117
  if (field.type() == message::Table::Field::BLOB
 
118
      || field.type() == message::Table::Field::VARCHAR)
 
119
  {
 
120
    if (field.string_options().has_collation())
 
121
      cout << " COLLATE " << field.string_options().collation();
 
122
  }
 
123
 
 
124
  if (field.options().has_default_value())
 
125
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
 
126
 
 
127
  if (field.options().has_default_bin_value())
 
128
  {
 
129
    string v= field.options().default_bin_value();
 
130
    cout << " DEFAULT 0x";
 
131
    for(unsigned int i=0; i< v.length(); i++)
 
132
    {
 
133
      printf("%.2x", *(v.c_str()+i));
 
134
    }
 
135
  }
 
136
 
 
137
  if (field.type() == message::Table::Field::TIMESTAMP)
 
138
    if (field.timestamp_options().has_auto_updates()
 
139
      && field.timestamp_options().auto_updates())
 
140
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
141
 
 
142
  if (field.has_comment())
 
143
    cout << " COMMENT `" << field.comment() << "` ";
 
144
}
 
145
 
 
146
static void print_engine(const message::Table::StorageEngine &engine)
 
147
{
 
148
  int32_t x;
 
149
 
 
150
  cout << " ENGINE = " << engine.name()  << endl;
 
151
 
 
152
  for (x= 0; x < engine.option_size(); ++x) {
 
153
    const message::Table::StorageEngine::EngineOption option= engine.option(x);
 
154
    cout << "\t" << option.option_name() << " = "
 
155
         << option.option_value() << endl;
 
156
  }
 
157
}
 
158
 
 
159
static void print_index(const message::Table::Index &index)
 
160
{
 
161
 
 
162
  if (index.is_primary())
 
163
    cout << " PRIMARY";
 
164
  else if (index.is_unique())
 
165
    cout << " UNIQUE";
 
166
  cout << " KEY `" << index.name() << "` (";
 
167
  {
 
168
    int32_t x;
 
169
 
 
170
    for (x= 0; x < index.index_part_size() ; x++)
 
171
    {
 
172
      const message::Table::Index::IndexPart part= index.index_part(x);
 
173
 
 
174
      if (x != 0)
 
175
        cout << ",";
 
176
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
 
177
      if (part.has_compare_length())
 
178
        cout << "(" << part.compare_length() << ")";
 
179
    }
 
180
    cout << ")";
 
181
  }
 
182
  cout << "\t";
 
183
}
 
184
 
 
185
static void print_table_options(const message::Table::TableOptions &options)
 
186
{
 
187
  if (options.has_comment())
 
188
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
189
 
 
190
  if (options.has_collation())
 
191
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
192
 
 
193
  if (options.has_auto_increment())
 
194
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
195
 
 
196
  if (options.has_collation_id())
 
197
    cout << "-- collation_id = " << options.collation_id() << endl;
 
198
  
 
199
  if (options.has_connect_string())
 
200
    cout << " CONNECT_STRING = '" << options.connect_string() << "'"<<endl;
 
201
 
 
202
  if (options.has_row_type())
 
203
    cout << " ROW_TYPE = " << options.row_type() << endl;
 
204
 
 
205
  if (options.has_data_file_name())
 
206
    cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
 
207
 
 
208
  if (options.has_index_file_name())
 
209
    cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
 
210
 
 
211
  if (options.has_max_rows())
 
212
    cout << " MAX_ROWS = " << options.max_rows() << endl;
 
213
 
 
214
  if (options.has_min_rows())
 
215
    cout << " MIN_ROWS = " << options.min_rows() << endl;
 
216
 
 
217
  if (options.has_auto_increment_value())
 
218
    cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
 
219
 
 
220
  if (options.has_avg_row_length())
 
221
    cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
 
222
 
 
223
  if (options.has_key_block_size())
 
224
    cout << " KEY_BLOCK_SIZE = "  << options.key_block_size() << endl;
 
225
 
 
226
  if (options.has_block_size())
 
227
    cout << " BLOCK_SIZE = " << options.block_size() << endl;
 
228
 
 
229
  if (options.has_comment())
 
230
    cout << " COMMENT = '" << options.comment() << "'" << endl;
 
231
 
 
232
  if (options.has_pack_keys())
 
233
    cout << " PACK_KEYS = " << options.pack_keys() << endl;
 
234
  if (options.has_pack_record())
 
235
    cout << " PACK_RECORD = " << options.pack_record() << endl;
 
236
  if (options.has_checksum())
 
237
    cout << " CHECKSUM = " << options.checksum() << endl;
 
238
  if (options.has_page_checksum())
 
239
    cout << " PAGE_CHECKSUM = " << options.page_checksum() << endl;
 
240
  if (options.has_delay_key_write())
 
241
    cout << " DELAY_KEY_WRITE = " << options.delay_key_write() << endl;
 
242
}
 
243
 
 
244
 
 
245
static void print_table(const message::Table &table)
 
246
{
 
247
  int32_t x;
 
248
 
 
249
  cout << "CREATE ";
 
250
 
 
251
  if (table.type() == message::Table::TEMPORARY)
 
252
    cout << "TEMPORARY ";
 
253
 
 
254
  cout << "TABLE `" << table.name() << "` (" << endl;
 
255
 
 
256
  for (x= 0; x < table.field_size() ; x++)
 
257
  {
 
258
    const message::Table::Field field = table.field(x);
 
259
 
 
260
    if (x != 0)
 
261
      cout << "," << endl;
 
262
 
 
263
    print_field(field);
 
264
  }
 
265
 
 
266
  for (x= 0; x < table.indexes_size() ; x++)
 
267
  {
 
268
    const message::Table::Index index= table.indexes(x);
 
269
 
 
270
    if (x != 0)
 
271
      cout << "," << endl;;
 
272
 
 
273
    print_index(index);
 
274
 
 
275
  }
 
276
  cout << endl;
 
277
 
 
278
  cout << ") " << endl;
 
279
 
 
280
  print_engine(table.engine());
 
281
 
 
282
  if (table.has_options())
 
283
    print_table_options(table.options());
 
284
  /*
 
285
  if (table->has_stats())
 
286
    print_table_stats(&table->stats());
 
287
  */
 
288
}
 
289
 
45
290
int main(int argc, char* argv[])
46
291
{
47
292
  GOOGLE_PROTOBUF_VERIFY_VERSION;
75
320
    close(fd);
76
321
  }
77
322
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
323
  print_table(table);
82
324
 
83
325
  return 0;
84
326
}