~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

Re-org'd the replication stuff into slots.

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