~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Monty Taylor
  • Date: 2009-09-22 23:50:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090922235012-i0a3bs91f6krqduc
Fixed multi_malloc.h include guard.
Added include guard checking script.

Show diffs side-by-side

added added

removed removed

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