~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

Merged from Toru - removal of my_time_t.

Show diffs side-by-side

added added

removed removed

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