~jaypipes/drizzle/replication-to-transaction

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Brian Aker
  • Date: 2010-03-03 05:02:36 UTC
  • mfrom: (1309.2.19 build)
  • Revision ID: brian@gaz-20100303050236-y7dotibgwks12gyp
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
#include <iostream>
32
32
#include <string>
33
 
#include <drizzled/message/table.pb.h>
 
33
#include <drizzled/message/statement_transform.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::TIMESTAMP:
93
 
    cout << " TIMESTAMP ";
94
 
    break;
95
 
  case message::Table::Field::DATETIME:
96
 
    cout << " DATETIME ";
97
 
    break;
98
 
  }
99
 
 
100
 
  if (field.type() == message::Table::Field::INTEGER
101
 
      || field.type() == message::Table::Field::BIGINT)
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_row_type())
200
 
    cout << " ROW_TYPE = " << options.row_type() << endl;
201
 
 
202
 
  if (options.has_data_file_name())
203
 
    cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
204
 
 
205
 
  if (options.has_index_file_name())
206
 
    cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
207
 
 
208
 
  if (options.has_max_rows())
209
 
    cout << " MAX_ROWS = " << options.max_rows() << endl;
210
 
 
211
 
  if (options.has_min_rows())
212
 
    cout << " MIN_ROWS = " << options.min_rows() << endl;
213
 
 
214
 
  if (options.has_auto_increment_value())
215
 
    cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
216
 
 
217
 
  if (options.has_avg_row_length())
218
 
    cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
219
 
 
220
 
  if (options.has_key_block_size())
221
 
    cout << " KEY_BLOCK_SIZE = "  << options.key_block_size() << endl;
222
 
 
223
 
  if (options.has_block_size())
224
 
    cout << " BLOCK_SIZE = " << options.block_size() << endl;
225
 
 
226
 
  if (options.has_comment())
227
 
    cout << " COMMENT = '" << options.comment() << "'" << endl;
228
 
 
229
 
  if (options.has_pack_keys())
230
 
    cout << " PACK_KEYS = " << options.pack_keys() << endl;
231
 
  if (options.has_pack_record())
232
 
    cout << " PACK_RECORD = " << options.pack_record() << endl;
233
 
  if (options.has_checksum())
234
 
    cout << " CHECKSUM = " << options.checksum() << endl;
235
 
  if (options.has_page_checksum())
236
 
    cout << " PAGE_CHECKSUM = " << options.page_checksum() << endl;
237
 
}
238
 
 
239
 
 
240
 
static void print_table(const message::Table &table)
241
 
{
242
 
  int32_t x;
243
 
 
244
 
  cout << "CREATE ";
245
 
 
246
 
  if (table.type() == message::Table::TEMPORARY)
247
 
    cout << "TEMPORARY ";
248
 
 
249
 
  cout << "TABLE `" << table.name() << "` (" << endl;
250
 
 
251
 
  for (x= 0; x < table.field_size() ; x++)
252
 
  {
253
 
    const message::Table::Field field = table.field(x);
254
 
 
255
 
    if (x != 0)
256
 
      cout << "," << endl;
257
 
 
258
 
    print_field(field);
259
 
  }
260
 
 
261
 
  for (x= 0; x < table.indexes_size() ; x++)
262
 
  {
263
 
    const message::Table::Index index= table.indexes(x);
264
 
 
265
 
    if (x != 0)
266
 
      cout << "," << endl;;
267
 
 
268
 
    print_index(index);
269
 
 
270
 
  }
271
 
  cout << endl;
272
 
 
273
 
  cout << ") " << endl;
274
 
 
275
 
  print_engine(table.engine());
276
 
 
277
 
  if (table.has_options())
278
 
    print_table_options(table.options());
279
 
  /*
280
 
  if (table->has_stats())
281
 
    print_table_stats(&table->stats());
282
 
  */
283
 
}
284
 
 
285
45
int main(int argc, char* argv[])
286
46
{
287
47
  GOOGLE_PROTOBUF_VERIFY_VERSION;
315
75
    close(fd);
316
76
  }
317
77
 
318
 
  print_table(table);
 
78
  string output;
 
79
  (void) message::transformTableDefinitionToSql(table, output);
 
80
 
 
81
  cout << output << endl;
319
82
 
320
83
  return 0;
321
84
}