~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

  • Committer: Brian Aker
  • Date: 2009-02-12 22:45:08 UTC
  • Revision ID: brian@tangent.org-20090212224508-mrd9jwgn1zjdpqdk
Minor refactoring (we will need to disconnect the code from the include
file).

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/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
using namespace std;
 
16
using namespace drizzle;
 
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 ::drizzle::Table::Field &field)
12
24
{
13
 
  using namespace drizzle;
14
 
  cout << "\t`" << field->name() << "`";
15
 
  switch (field->type())
 
25
  cout << "\t`" << field.name() << "`";
 
26
  switch (field.type())
16
27
  {
17
28
    case Table::Field::DOUBLE:
18
29
    cout << " DOUBLE ";
19
30
    break;
20
31
  case Table::Field::VARCHAR:
21
 
    cout << " VARCHAR(" << field->string_options().length() << ")";
 
32
    cout << " VARCHAR(" << field.string_options().length() << ")";
22
33
    break;
23
34
  case Table::Field::TEXT:
24
35
    cout << " TEXT ";
31
42
      int x;
32
43
 
33
44
      cout << " ENUM(";
34
 
      for (x= 0; x < field->set_options().value_size() ; x++)
 
45
      for (x= 0; x < field.set_options().field_value_size() ; x++)
35
46
      {
36
 
        const string type= field->set_options().value(x);
 
47
        const string type= field.set_options().field_value(x);
37
48
 
38
49
        if (x != 0)
39
50
          cout << ",";
42
53
      cout << ") ";
43
54
      break;
44
55
    }
45
 
  case Table::Field::SET:
46
 
    cout << " SET ";
47
 
    break;
48
56
  case Table::Field::TINYINT:
49
57
    cout << " TINYINT ";
50
58
    break;
51
 
  case Table::Field::SMALLINT:
52
 
    cout << " SMALLINT ";
53
 
    break;
54
59
  case Table::Field::INTEGER:
55
 
    cout << " INTEGER ";
 
60
    cout << " INT" ;
56
61
    break;
57
62
  case Table::Field::BIGINT:
58
63
    cout << " BIGINT ";
59
64
    break;
60
65
  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() << ") ";
 
66
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
65
67
    break;
66
68
  case Table::Field::DATE:
67
69
    cout << " DATE ";
77
79
    break;
78
80
  }
79
81
 
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())
 
82
  if (field.type() == Table::Field::INTEGER
 
83
      || field.type() == Table::Field::BIGINT
 
84
      || field.type() == Table::Field::TINYINT)
 
85
  {
 
86
    if (field.has_constraints()
 
87
        && field.constraints().has_is_unsigned())
 
88
      if (field.constraints().is_unsigned())
87
89
        cout << " UNSIGNED";
88
90
 
89
 
    if (field->has_numeric_options() &&
90
 
      field->numeric_options().is_autoincrement())
 
91
    if (field.has_numeric_options() &&
 
92
      field.numeric_options().is_autoincrement())
91
93
      cout << " AUTOINCREMENT ";
92
94
  }
93
95
 
94
 
  if (! field->has_constraints()
95
 
      && field->constraints().is_nullable())
 
96
  if (! field.has_constraints()
 
97
      && field.constraints().is_nullable())
96
98
    cout << " NOT NULL ";
97
99
 
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();
 
100
  if (field.type() == Table::Field::TEXT
 
101
      || field.type() == Table::Field::VARCHAR)
 
102
  {
 
103
    if (field.string_options().has_collation())
 
104
      cout << " COLLATE " << field.string_options().collation();
105
105
  }
106
106
 
107
 
  if (field->options().has_default_value())
108
 
    cout << " DEFAULT `" << field->options().default_value() << "` " ;
 
107
  if (field.options().has_default_value())
 
108
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
109
109
 
110
 
  if (field->type() == Table::Field::TIMESTAMP)
111
 
    if (field->timestamp_options().has_auto_updates()
112
 
      && field->timestamp_options().auto_updates())
 
110
  if (field.type() == Table::Field::TIMESTAMP)
 
111
    if (field.timestamp_options().has_auto_updates()
 
112
      && field.timestamp_options().auto_updates())
113
113
      cout << " ON UPDATE CURRENT_TIMESTAMP";
114
114
 
115
 
  if (field->has_comment())
116
 
    cout << " COMMENT `" << field->comment() << "` ";
 
115
  if (field.has_comment())
 
116
    cout << " COMMENT `" << field.comment() << "` ";
117
117
}
118
118
 
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;
 
119
void print_engine(const ::drizzle::Table::StorageEngine &engine)
 
120
{
 
121
  int32_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.option_name() << " = "
 
128
         << option.option_value() << endl;
128
129
  }
129
130
}
130
131
 
131
 
void print_index(const drizzle::Table::Index *index) {
132
 
  using namespace drizzle;
133
 
  uint32_t x;
 
132
void print_index(const ::drizzle::Table::Index &index)
 
133
{
134
134
 
135
 
  if (index->is_primary())
136
 
    cout << " PRIMARY"; 
137
 
  else if (index->is_unique())
138
 
    cout << " UNIQUE"; 
139
 
  cout << " KEY `" << index->name() << "` (";
 
135
  if (index.is_primary())
 
136
    cout << " PRIMARY";
 
137
  else if (index.is_unique())
 
138
    cout << " UNIQUE";
 
139
  cout << " KEY `" << index.name() << "` (";
140
140
  {
141
 
    int x;
 
141
    int32_t x;
142
142
 
143
 
    for (x= 0; x < index->index_part_size() ; x++)
 
143
    for (x= 0; x < index.index_part_size() ; x++)
144
144
    {
145
 
      const Table::Index::IndexPart part= index->index_part(x);
 
145
      const Table::Index::IndexPart part= index.index_part(x);
146
146
 
147
147
      if (x != 0)
148
148
        cout << ",";
149
 
      cout << "`" << part.field().name() << "`";
 
149
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
150
150
      if (part.has_compare_length())
151
151
        cout << "(" << part.compare_length() << ")";
152
152
    }
155
155
  cout << "\t";
156
156
}
157
157
 
158
 
void print_table(const drizzle::Table *table) 
159
 
{
160
 
  using namespace drizzle;
161
 
  uint32_t x;
 
158
void print_table_stats(const ::drizzle::Table::TableStats&) 
 
159
{
 
160
 
 
161
}
 
162
 
 
163
void print_table_options(const ::drizzle::Table::TableOptions &options)
 
164
{
 
165
  if (options.has_comment())
 
166
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
167
 
 
168
  if (options.has_collation())
 
169
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
170
 
 
171
  if (options.has_auto_increment())
 
172
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
173
 
 
174
  if (options.has_collation_id())
 
175
    cout << "-- collation_id = " << options.collation_id() << endl;
 
176
  
 
177
  if (options.has_connect_string())
 
178
    cout << " CONNECT_STRING = '" << options.connect_string() << "'"<<endl;
 
179
 
 
180
  if (options.has_row_type())
 
181
    cout << " ROW_TYPE = " << options.row_type() << endl;
 
182
 
 
183
/*    optional string data_file_name = 5;
 
184
    optional string index_file_name = 6;
 
185
    optional uint64 max_rows = 7;
 
186
    optional uint64 min_rows = 8;
 
187
    optional uint64 auto_increment_value = 9;
 
188
    optional uint32 avg_row_length = 11;
 
189
    optional uint32 key_block_size = 12;
 
190
    optional uint32 block_size = 13;
 
191
    optional string comment = 14;
 
192
    optional bool pack_keys = 15;
 
193
    optional bool checksum = 16;
 
194
    optional bool page_checksum = 17;
 
195
    optional bool delay_key_write = 18;
 
196
*/
 
197
}
 
198
 
 
199
 
 
200
void print_table(const ::drizzle::Table &table)
 
201
{
 
202
  int32_t x;
162
203
 
163
204
  cout << "CREATE ";
164
205
 
165
 
  if (table->type() == Table::TEMPORARY)
 
206
  if (table.type() == Table::TEMPORARY)
166
207
    cout << "TEMPORARY ";
167
208
 
168
 
  cout << "TABLE `" << table->name() << "` (" << endl;
169
 
  
170
 
  for (x= 0; x < table->field_size() ; x++)
 
209
  cout << "TABLE `" << table.name() << "` (" << endl;
 
210
 
 
211
  for (x= 0; x < table.field_size() ; x++)
171
212
  {
172
 
    const Table::Field field = table->field(x);
 
213
    const Table::Field field = table.field(x);
173
214
 
174
215
    if (x != 0)
175
216
      cout << "," << endl;
176
217
 
177
 
    print_field(&field);
 
218
    print_field(field);
178
219
  }
179
220
 
180
 
  for (x= 0; x < table->index_size() ; x++)
 
221
  for (x= 0; x < table.indexes_size() ; x++)
181
222
  {
182
 
    const Table::Index index= table->index(x);
 
223
    const Table::Index index= table.indexes(x);
183
224
 
184
225
    if (x != 0)
185
226
      cout << "," << endl;;
186
227
 
187
 
    print_index(&index);
 
228
    print_index(index);
188
229
 
189
230
  }
190
231
  cout << endl;
191
232
 
192
233
  cout << ") " << endl;
193
 
  
194
 
  print_engine(&table->engine());
195
 
 
 
234
 
 
235
  print_engine(table.engine());
 
236
 
 
237
  if (table.has_options())
 
238
    print_table_options(table.options());
196
239
  /*
197
 
  if (table->has_options())
198
 
    print_table_options(&table->options());
199
240
  if (table->has_stats())
200
241
    print_table_stats(&table->stats());
201
242
  */
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[]) 
 
243
}
 
244
 
 
245
int main(int argc, char* argv[])
218
246
{
219
247
  GOOGLE_PROTOBUF_VERIFY_VERSION;
220
248
 
223
251
    return -1;
224
252
  }
225
253
 
226
 
  drizzle::Table table;
 
254
  Table table;
227
255
 
228
256
  {
229
 
    // Read the existing address book.
230
 
    fstream input(argv[1], ios::in | ios::binary);
231
 
    if (!table.ParseFromIstream(&input)) 
 
257
    int fd= open(argv[1], O_RDONLY);
 
258
 
 
259
    if(fd==-1)
 
260
    {
 
261
      perror("Failed to open table definition file");
 
262
      return -1;
 
263
    }
 
264
 
 
265
    ZeroCopyInputStream* input = new FileInputStream(fd);
 
266
 
 
267
    if (!table.ParseFromZeroCopyStream(input))
232
268
    {
233
269
      cerr << "Failed to parse table." << endl;
 
270
      close(fd);
234
271
      return -1;
235
272
    }
 
273
 
 
274
    close(fd);
236
275
  }
237
276
 
238
 
  print_table(&table);
 
277
  print_table(table);
239
278
 
240
279
  return 0;
241
280
}