~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Monty Taylor
  • Date: 2009-12-25 08:50:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1255.
  • Revision ID: mordred@inaugust.com-20091225085015-83sux5qsvy312gew
MEM_ROOT == memory::Root

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
30
30
 
31
31
#include <iostream>
32
32
#include <string>
33
 
#include <drizzled/message/statement_transform.h>
 
33
#include <drizzled/message/table.pb.h>
34
34
#include <google/protobuf/io/zero_copy_stream.h>
35
35
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
36
 
42
42
  Written from Google proto example
43
43
*/
44
44
 
 
45
static void print_field(const message::Table::Field &field)
 
46
{
 
47
  cout << "\t`" << field.name() << "`";
 
48
 
 
49
  message::Table::Field::FieldType field_type= field.type();
 
50
 
 
51
  switch (field_type)
 
52
  {
 
53
    case message::Table::Field::DOUBLE:
 
54
    cout << " DOUBLE ";
 
55
    break;
 
56
  case message::Table::Field::VARCHAR:
 
57
    cout << " VARCHAR(" << field.string_options().length() << ")";
 
58
    break;
 
59
  case message::Table::Field::BLOB:
 
60
    cout << " BLOB "; /* FIXME: or text, depends on collation */
 
61
    if(field.string_options().has_collation_id())
 
62
      cout << "COLLATION=" << field.string_options().collation_id() << " ";
 
63
    break;
 
64
  case message::Table::Field::ENUM:
 
65
    {
 
66
      int x;
 
67
 
 
68
      cout << " ENUM(";
 
69
      for (x= 0; x < field.set_options().field_value_size() ; x++)
 
70
      {
 
71
        const string type= field.set_options().field_value(x);
 
72
 
 
73
        if (x != 0)
 
74
          cout << ",";
 
75
        cout << "'" << type << "'";
 
76
      }
 
77
      cout << ") ";
 
78
      break;
 
79
    }
 
80
  case message::Table::Field::INTEGER:
 
81
    cout << " INT" ;
 
82
    break;
 
83
  case message::Table::Field::BIGINT:
 
84
    cout << " BIGINT ";
 
85
    break;
 
86
  case message::Table::Field::DECIMAL:
 
87
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
 
88
    break;
 
89
  case message::Table::Field::DATE:
 
90
    cout << " DATE ";
 
91
    break;
 
92
  case message::Table::Field::TIME:
 
93
    cout << " TIME ";
 
94
    break;
 
95
  case message::Table::Field::TIMESTAMP:
 
96
    cout << " TIMESTAMP ";
 
97
    break;
 
98
  case message::Table::Field::DATETIME:
 
99
    cout << " DATETIME ";
 
100
    break;
 
101
  }
 
102
 
 
103
  if (field.type() == message::Table::Field::INTEGER
 
104
      || field.type() == message::Table::Field::BIGINT)
 
105
  {
 
106
    if (field.has_constraints()
 
107
        && field.constraints().has_is_unsigned())
 
108
      if (field.constraints().is_unsigned())
 
109
        cout << " UNSIGNED";
 
110
 
 
111
    if (field.has_numeric_options() &&
 
112
      field.numeric_options().is_autoincrement())
 
113
      cout << " AUTOINCREMENT ";
 
114
  }
 
115
 
 
116
  if (!( field.has_constraints()
 
117
         && field.constraints().is_nullable()))
 
118
    cout << " NOT NULL ";
 
119
 
 
120
  if (field.type() == message::Table::Field::BLOB
 
121
      || field.type() == message::Table::Field::VARCHAR)
 
122
  {
 
123
    if (field.string_options().has_collation())
 
124
      cout << " COLLATE " << field.string_options().collation();
 
125
  }
 
126
 
 
127
  if (field.options().has_default_value())
 
128
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
 
129
 
 
130
  if (field.options().has_default_bin_value())
 
131
  {
 
132
    string v= field.options().default_bin_value();
 
133
    cout << " DEFAULT 0x";
 
134
    for(unsigned int i=0; i< v.length(); i++)
 
135
    {
 
136
      printf("%.2x", *(v.c_str()+i));
 
137
    }
 
138
  }
 
139
 
 
140
  if (field.type() == message::Table::Field::TIMESTAMP)
 
141
    if (field.timestamp_options().has_auto_updates()
 
142
      && field.timestamp_options().auto_updates())
 
143
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
144
 
 
145
  if (field.has_comment())
 
146
    cout << " COMMENT `" << field.comment() << "` ";
 
147
}
 
148
 
 
149
static void print_engine(const message::Table::StorageEngine &engine)
 
150
{
 
151
  int32_t x;
 
152
 
 
153
  cout << " ENGINE = " << engine.name()  << endl;
 
154
 
 
155
  for (x= 0; x < engine.option_size(); ++x) {
 
156
    const message::Table::StorageEngine::EngineOption option= engine.option(x);
 
157
    cout << "\t" << option.option_name() << " = "
 
158
         << option.option_value() << endl;
 
159
  }
 
160
}
 
161
 
 
162
static void print_index(const message::Table::Index &index)
 
163
{
 
164
 
 
165
  if (index.is_primary())
 
166
    cout << " PRIMARY";
 
167
  else if (index.is_unique())
 
168
    cout << " UNIQUE";
 
169
  cout << " KEY `" << index.name() << "` (";
 
170
  {
 
171
    int32_t x;
 
172
 
 
173
    for (x= 0; x < index.index_part_size() ; x++)
 
174
    {
 
175
      const message::Table::Index::IndexPart part= index.index_part(x);
 
176
 
 
177
      if (x != 0)
 
178
        cout << ",";
 
179
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
 
180
      if (part.has_compare_length())
 
181
        cout << "(" << part.compare_length() << ")";
 
182
    }
 
183
    cout << ")";
 
184
  }
 
185
  cout << "\t";
 
186
}
 
187
 
 
188
static void print_table_options(const message::Table::TableOptions &options)
 
189
{
 
190
  if (options.has_comment())
 
191
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
192
 
 
193
  if (options.has_collation())
 
194
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
195
 
 
196
  if (options.has_auto_increment())
 
197
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
198
 
 
199
  if (options.has_collation_id())
 
200
    cout << "-- collation_id = " << options.collation_id() << 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
}
 
241
 
 
242
 
 
243
static void print_table(const message::Table &table)
 
244
{
 
245
  int32_t x;
 
246
 
 
247
  cout << "CREATE ";
 
248
 
 
249
  if (table.type() == message::Table::TEMPORARY)
 
250
    cout << "TEMPORARY ";
 
251
 
 
252
  cout << "TABLE `" << table.name() << "` (" << endl;
 
253
 
 
254
  for (x= 0; x < table.field_size() ; x++)
 
255
  {
 
256
    const message::Table::Field field = table.field(x);
 
257
 
 
258
    if (x != 0)
 
259
      cout << "," << endl;
 
260
 
 
261
    print_field(field);
 
262
  }
 
263
 
 
264
  for (x= 0; x < table.indexes_size() ; x++)
 
265
  {
 
266
    const message::Table::Index index= table.indexes(x);
 
267
 
 
268
    if (x != 0)
 
269
      cout << "," << endl;;
 
270
 
 
271
    print_index(index);
 
272
 
 
273
  }
 
274
  cout << endl;
 
275
 
 
276
  cout << ") " << endl;
 
277
 
 
278
  print_engine(table.engine());
 
279
 
 
280
  if (table.has_options())
 
281
    print_table_options(table.options());
 
282
  /*
 
283
  if (table->has_stats())
 
284
    print_table_stats(&table->stats());
 
285
  */
 
286
}
 
287
 
45
288
int main(int argc, char* argv[])
46
289
{
47
290
  GOOGLE_PROTOBUF_VERIFY_VERSION;
75
318
    close(fd);
76
319
  }
77
320
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
321
  print_table(table);
82
322
 
83
323
  return 0;
84
324
}