~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

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
 
1
9
#include <iostream>
2
 
#include <fstream>
3
10
#include <string>
4
 
#include "table.pb.h"
 
11
#include <drizzled/message/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
using namespace std;
 
16
using namespace drizzled::message;
 
17
using namespace google::protobuf::io;
6
18
 
7
 
/* 
 
19
/*
8
20
  Written from Google proto example
9
21
*/
10
22
 
11
 
void print_field(const drizzle::Table::Field *field) 
 
23
void print_field(const ::drizzled::message::Table::Field &field)
12
24
{
13
 
  using namespace drizzle;
14
 
  cout << "\t`" << field->name() << "`";
15
 
  switch (field->type())
 
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)
16
36
  {
17
37
    case Table::Field::DOUBLE:
18
38
    cout << " DOUBLE ";
19
39
    break;
20
40
  case Table::Field::VARCHAR:
21
 
    cout << " VARCHAR(" << field->string_options().length() << ")";
22
 
    break;
23
 
  case Table::Field::TEXT:
24
 
    cout << " TEXT ";
 
41
    cout << " VARCHAR(" << field.string_options().length() << ")";
25
42
    break;
26
43
  case Table::Field::BLOB:
27
 
    cout << " 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
47
    break;
29
48
  case Table::Field::ENUM:
30
49
    {
31
50
      int x;
32
51
 
33
52
      cout << " ENUM(";
34
 
      for (x= 0; x < field->set_options().value_size() ; x++)
 
53
      for (x= 0; x < field.set_options().field_value_size() ; x++)
35
54
      {
36
 
        const string type= field->set_options().value(x);
 
55
        const string type= field.set_options().field_value(x);
37
56
 
38
57
        if (x != 0)
39
58
          cout << ",";
42
61
      cout << ") ";
43
62
      break;
44
63
    }
45
 
  case Table::Field::SET:
46
 
    cout << " SET ";
47
 
    break;
48
64
  case Table::Field::TINYINT:
49
65
    cout << " TINYINT ";
50
66
    break;
51
 
  case Table::Field::SMALLINT:
52
 
    cout << " SMALLINT ";
53
 
    break;
54
67
  case Table::Field::INTEGER:
55
 
    cout << " INTEGER ";
 
68
    cout << " INT" ;
56
69
    break;
57
70
  case Table::Field::BIGINT:
58
71
    cout << " BIGINT ";
59
72
    break;
60
73
  case Table::Field::DECIMAL:
61
 
    cout << " DECIMAL(" << field->numeric_options().precision() << "," << field->numeric_options().scale() << ") ";
62
 
    break;
63
 
  case Table::Field::VARBINARY:
64
 
    cout << " VARBINARY(" << field->string_options().length() << ") ";
 
74
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
65
75
    break;
66
76
  case Table::Field::DATE:
67
77
    cout << " DATE ";
75
85
  case Table::Field::DATETIME:
76
86
    cout << " DATETIME ";
77
87
    break;
78
 
  }
79
 
 
80
 
  if (field->type() == Table::Field::INTEGER
81
 
      || field->type() == Table::Field::BIGINT
82
 
      || field->type() == Table::Field::SMALLINT
83
 
      || field->type() == Table::Field::TINYINT) {
84
 
    if (field->has_constraints()
85
 
        && field->constraints().has_is_unsigned())
86
 
      if (field->constraints().is_unsigned())
 
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())
87
106
        cout << " UNSIGNED";
88
107
 
89
 
    if (field->has_numeric_options() &&
90
 
      field->numeric_options().is_autoincrement())
 
108
    if (field.has_numeric_options() &&
 
109
      field.numeric_options().is_autoincrement())
91
110
      cout << " AUTOINCREMENT ";
92
111
  }
93
112
 
94
 
  if (! field->has_constraints()
95
 
      && field->constraints().is_nullable())
 
113
  if (!( field.has_constraints()
 
114
         && field.constraints().is_nullable()))
96
115
    cout << " NOT NULL ";
97
116
 
98
 
  if (field->type() == Table::Field::TEXT
99
 
      || field->type() == Table::Field::VARCHAR) {
100
 
    if (field->string_options().has_charset())
101
 
      cout << " CHARACTER SET " << field->string_options().charset();
102
 
 
103
 
    if (field->string_options().has_collation())
104
 
      cout << " COLLATE " << field->string_options().collation();
105
 
  }
106
 
 
107
 
  if (field->options().has_default_value())
108
 
    cout << " DEFAULT `" << field->options().default_value() << "` " ;
109
 
 
110
 
  if (field->type() == Table::Field::TIMESTAMP)
111
 
    if (field->timestamp_options().has_auto_updates()
112
 
      && field->timestamp_options().auto_updates())
 
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())
113
140
      cout << " ON UPDATE CURRENT_TIMESTAMP";
114
141
 
115
 
  if (field->has_comment())
116
 
    cout << " COMMENT `" << field->comment() << "` ";
 
142
  if (field.has_comment())
 
143
    cout << " COMMENT `" << field.comment() << "` ";
117
144
}
118
145
 
119
 
void print_engine(const drizzle::Table::StorageEngine *engine) {
120
 
  using namespace drizzle;
121
 
  uint32_t x;
122
 
 
123
 
  cout << " ENGINE = " << engine->name()  << endl;
124
 
 
125
 
  for (x= 0; x < engine->option_size(); ++x) {
126
 
    const Table::StorageEngine::EngineOption option= engine->option(x);
127
 
    cout << "\t" << option.name() << " = " << option.value() << endl;
 
146
void print_engine(const ::drizzled::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 Table::StorageEngine::EngineOption option= engine.option(x);
 
154
    cout << "\t" << option.option_name() << " = "
 
155
         << option.option_value() << endl;
128
156
  }
129
157
}
130
158
 
131
 
void print_index(const drizzle::Table::Index *index) {
132
 
  using namespace drizzle;
133
 
  uint32_t x;
 
159
void print_index(const ::drizzled::message::Table::Index &index)
 
160
{
134
161
 
135
 
  if (index->is_primary())
136
 
    cout << " PRIMARY"; 
137
 
  else if (index->is_unique())
138
 
    cout << " UNIQUE"; 
139
 
  cout << " KEY `" << index->name() << "` (";
 
162
  if (index.is_primary())
 
163
    cout << " PRIMARY";
 
164
  else if (index.is_unique())
 
165
    cout << " UNIQUE";
 
166
  cout << " KEY `" << index.name() << "` (";
140
167
  {
141
 
    int x;
 
168
    int32_t x;
142
169
 
143
 
    for (x= 0; x < index->index_part_size() ; x++)
 
170
    for (x= 0; x < index.index_part_size() ; x++)
144
171
    {
145
 
      const Table::Index::IndexPart part= index->index_part(x);
 
172
      const Table::Index::IndexPart part= index.index_part(x);
146
173
 
147
174
      if (x != 0)
148
175
        cout << ",";
149
 
      cout << "`" << part.field().name() << "`";
 
176
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
150
177
      if (part.has_compare_length())
151
178
        cout << "(" << part.compare_length() << ")";
152
179
    }
155
182
  cout << "\t";
156
183
}
157
184
 
158
 
void print_table(const drizzle::Table *table) 
159
 
{
160
 
  using namespace drizzle;
161
 
  uint32_t x;
 
185
void print_table_stats(const ::drizzled::message::Table::TableStats&) 
 
186
{
 
187
 
 
188
}
 
189
 
 
190
void print_table_options(const ::drizzled::message::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 ::drizzled::message::Table &table)
 
251
{
 
252
  int32_t x;
162
253
 
163
254
  cout << "CREATE ";
164
255
 
165
 
  if (table->type() == Table::TEMPORARY)
 
256
  if (table.type() == Table::TEMPORARY)
166
257
    cout << "TEMPORARY ";
167
258
 
168
 
  cout << "TABLE `" << table->name() << "` (" << endl;
169
 
  
170
 
  for (x= 0; x < table->field_size() ; x++)
 
259
  cout << "TABLE `" << table.name() << "` (" << endl;
 
260
 
 
261
  for (x= 0; x < table.field_size() ; x++)
171
262
  {
172
 
    const Table::Field field = table->field(x);
 
263
    const Table::Field field = table.field(x);
173
264
 
174
265
    if (x != 0)
175
266
      cout << "," << endl;
176
267
 
177
 
    print_field(&field);
 
268
    print_field(field);
178
269
  }
179
270
 
180
 
  for (x= 0; x < table->index_size() ; x++)
 
271
  for (x= 0; x < table.indexes_size() ; x++)
181
272
  {
182
 
    const Table::Index index= table->index(x);
 
273
    const Table::Index index= table.indexes(x);
183
274
 
184
275
    if (x != 0)
185
276
      cout << "," << endl;;
186
277
 
187
 
    print_index(&index);
 
278
    print_index(index);
188
279
 
189
280
  }
190
281
  cout << endl;
191
282
 
192
283
  cout << ") " << endl;
193
 
  
194
 
  print_engine(&table->engine());
195
 
 
 
284
 
 
285
  print_engine(table.engine());
 
286
 
 
287
  if (table.has_options())
 
288
    print_table_options(table.options());
196
289
  /*
197
 
  if (table->has_options())
198
 
    print_table_options(&table->options());
199
290
  if (table->has_stats())
200
291
    print_table_stats(&table->stats());
201
292
  */
202
 
  if (table->has_comment())
203
 
    cout << " COMMENT = `" << table->comment() << "` " << endl;
204
 
}
205
 
 
206
 
void print_table_stats(const drizzle::Table::TableStats *stats) {
207
 
  
208
 
}
209
 
 
210
 
void print_table_options(const drizzle::Table::TableOptions *options) {
211
 
  if (options->has_collation())
212
 
    cout << " COLLATE = `" << options->collation() << "` " << endl;
213
 
  if (options->has_charset())
214
 
    cout << " CHARACTER SET = `" << options->charset() << "` " << endl;
215
 
}
216
 
 
217
 
int main(int argc, char* argv[]) 
 
293
}
 
294
 
 
295
int main(int argc, char* argv[])
218
296
{
219
297
  GOOGLE_PROTOBUF_VERIFY_VERSION;
220
298
 
223
301
    return -1;
224
302
  }
225
303
 
226
 
  drizzle::Table table;
 
304
  Table table;
227
305
 
228
306
  {
229
 
    // Read the existing address book.
230
 
    fstream input(argv[1], ios::in | ios::binary);
231
 
    if (!table.ParseFromIstream(&input)) 
 
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))
232
318
    {
233
319
      cerr << "Failed to parse table." << endl;
 
320
      close(fd);
234
321
      return -1;
235
322
    }
 
323
 
 
324
    close(fd);
236
325
  }
237
326
 
238
 
  print_table(&table);
 
327
  print_table(table);
239
328
 
240
329
  return 0;
241
330
}