~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Brian Aker
  • Date: 2009-10-01 22:56:26 UTC
  • mto: (1154.1.1 staging)
  • mto: This revision was merged to the branch mainline in revision 1155.
  • Revision ID: brian@gaz-20091001225626-sb1pdykpxlnkheaj
Remove Factory/make scheduler work like everything else.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include <drizzled/global.h>
 
22
#include <sys/types.h>
 
23
#include <sys/stat.h>
 
24
#include <fcntl.h>
 
25
#include <string.h>
 
26
#include <stdio.h>
 
27
#include <errno.h>
 
28
#include <unistd.h>
 
29
 
1
30
#include <iostream>
2
 
#include <fstream>
3
31
#include <string>
4
 
#include "table.pb.h"
 
32
#include <drizzled/message/table.pb.h>
 
33
#include <google/protobuf/io/zero_copy_stream.h>
 
34
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
35
 
5
36
using namespace std;
 
37
using namespace drizzled;
 
38
using namespace google;
6
39
 
7
 
/* 
 
40
/*
8
41
  Written from Google proto example
9
42
*/
10
43
 
11
 
void printType(const drizzle::Table::Field *field) 
 
44
static void print_field(const message::Table::Field &field)
12
45
{
13
 
  switch (field->type())
 
46
  cout << "\t`" << field.name() << "`";
 
47
 
 
48
  message::Table::Field::FieldType field_type= field.type();
 
49
 
 
50
  switch (field_type)
14
51
  {
15
 
  case drizzle::Table::DOUBLE:
 
52
    case message::Table::Field::DOUBLE:
16
53
    cout << " DOUBLE ";
17
54
    break;
18
 
  case drizzle::Table::VARCHAR:
19
 
    cout << " VARCHAR(" << field->length() << ")";
20
 
    break;
21
 
  case drizzle::Table::TEXT:
22
 
    cout << " TEXT ";
23
 
    break;
24
 
  case drizzle::Table::BLOB:
25
 
    cout << " BLOB ";
26
 
    break;
27
 
  case drizzle::Table::ENUM:
 
55
  case message::Table::Field::VARCHAR:
 
56
    cout << " VARCHAR(" << field.string_options().length() << ")";
 
57
    break;
 
58
  case message::Table::Field::BLOB:
 
59
    cout << " BLOB "; /* FIXME: or text, depends on collation */
 
60
    if(field.string_options().has_collation_id())
 
61
      cout << "COLLATION=" << field.string_options().collation_id() << " ";
 
62
    break;
 
63
  case message::Table::Field::ENUM:
28
64
    {
29
65
      int x;
30
66
 
31
67
      cout << " ENUM(";
32
 
      for (x= 0; x < field->values_size() ; x++)
 
68
      for (x= 0; x < field.set_options().field_value_size() ; x++)
33
69
      {
34
 
        const string type= field->values(x);
 
70
        const string type= field.set_options().field_value(x);
35
71
 
36
72
        if (x != 0)
37
73
          cout << ",";
40
76
      cout << ") ";
41
77
      break;
42
78
    }
43
 
  case drizzle::Table::SET:
44
 
    cout << " SET ";
45
 
    break;
46
 
  case drizzle::Table::TINYINT:
47
 
    cout << " TINYINNT ";
48
 
    break;
49
 
  case drizzle::Table::SMALLINT:
50
 
    cout << " SMALLINT ";
51
 
    break;
52
 
  case drizzle::Table::INTEGER:
53
 
    cout << " INTEGER ";
54
 
    break;
55
 
  case drizzle::Table::BIGINT:
 
79
  case message::Table::Field::INTEGER:
 
80
    cout << " INT" ;
 
81
    break;
 
82
  case message::Table::Field::BIGINT:
56
83
    cout << " BIGINT ";
57
84
    break;
58
 
  case drizzle::Table::DECIMAL:
59
 
    cout << " DECIMAL(" << field->length() << "," << field->scale() << ") ";
60
 
    break;
61
 
  case drizzle::Table::VARBINARY:
62
 
    cout << " VARBINARY(" << field->length() << ") ";
63
 
    break;
64
 
  case drizzle::Table::DATE:
 
85
  case message::Table::Field::DECIMAL:
 
86
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
 
87
    break;
 
88
  case message::Table::Field::DATE:
65
89
    cout << " DATE ";
66
90
    break;
67
 
  case drizzle::Table::TIME:
 
91
  case message::Table::Field::TIME:
68
92
    cout << " TIME ";
69
93
    break;
70
 
  case drizzle::Table::TIMESTAMP:
 
94
  case message::Table::Field::TIMESTAMP:
71
95
    cout << " TIMESTAMP ";
72
96
    break;
73
 
  case drizzle::Table::DATETIME:
 
97
  case message::Table::Field::DATETIME:
74
98
    cout << " DATETIME ";
75
99
    break;
76
100
  }
77
101
 
78
 
  if (field->has_characterset())
79
 
    cout << " CHARACTER SET " << field->characterset();
80
 
 
81
 
  if (field->has_collation())
82
 
    cout << " COLLATE " << field->collation();
83
 
 
84
 
  if (field->is_notnull())
 
102
  if (field.type() == message::Table::Field::INTEGER
 
103
      || field.type() == message::Table::Field::BIGINT)
 
104
  {
 
105
    if (field.has_constraints()
 
106
        && field.constraints().has_is_unsigned())
 
107
      if (field.constraints().is_unsigned())
 
108
        cout << " UNSIGNED";
 
109
 
 
110
    if (field.has_numeric_options() &&
 
111
      field.numeric_options().is_autoincrement())
 
112
      cout << " AUTOINCREMENT ";
 
113
  }
 
114
 
 
115
  if (!( field.has_constraints()
 
116
         && field.constraints().is_nullable()))
85
117
    cout << " NOT NULL ";
86
118
 
87
 
  if (field->has_default_value())
88
 
    cout << " DEFAULT `" << field->default_value() << "` " ;
89
 
 
90
 
  if (field->on_update())
91
 
    cout << " ON UPDATE CURRENT_TIMESTAMP";
92
 
 
93
 
  if (field->autoincrement())
94
 
    cout << " AUTOINCREMENT ";
95
 
 
96
 
  if (field->has_comment())
97
 
    cout << " COMMENT `" << field->comment() << "` ";
98
 
}
99
 
 
100
 
void printTable(const drizzle::Table *table) 
101
 
{
102
 
  uint32_t x;
103
 
 
104
 
  cout << "CREATE TABLE";
105
 
 
106
 
  if (table->temp())
107
 
    cout << " TEMPORARY";
108
 
 
109
 
  cout << " `" << table->name() << "` (" << endl;
 
119
  if (field.type() == message::Table::Field::BLOB
 
120
      || field.type() == message::Table::Field::VARCHAR)
 
121
  {
 
122
    if (field.string_options().has_collation())
 
123
      cout << " COLLATE " << field.string_options().collation();
 
124
  }
 
125
 
 
126
  if (field.options().has_default_value())
 
127
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
 
128
 
 
129
  if (field.options().has_default_bin_value())
 
130
  {
 
131
    string v= field.options().default_bin_value();
 
132
    cout << " DEFAULT 0x";
 
133
    for(unsigned int i=0; i< v.length(); i++)
 
134
    {
 
135
      printf("%.2x", *(v.c_str()+i));
 
136
    }
 
137
  }
 
138
 
 
139
  if (field.type() == message::Table::Field::TIMESTAMP)
 
140
    if (field.timestamp_options().has_auto_updates()
 
141
      && field.timestamp_options().auto_updates())
 
142
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
143
 
 
144
  if (field.has_comment())
 
145
    cout << " COMMENT `" << field.comment() << "` ";
 
146
}
 
147
 
 
148
static void print_engine(const message::Table::StorageEngine &engine)
 
149
{
 
150
  int32_t x;
 
151
 
 
152
  cout << " ENGINE = " << engine.name()  << endl;
 
153
 
 
154
  for (x= 0; x < engine.option_size(); ++x) {
 
155
    const message::Table::StorageEngine::EngineOption option= engine.option(x);
 
156
    cout << "\t" << option.option_name() << " = "
 
157
         << option.option_value() << endl;
 
158
  }
 
159
}
 
160
 
 
161
static void print_index(const message::Table::Index &index)
 
162
{
 
163
 
 
164
  if (index.is_primary())
 
165
    cout << " PRIMARY";
 
166
  else if (index.is_unique())
 
167
    cout << " UNIQUE";
 
168
  cout << " KEY `" << index.name() << "` (";
 
169
  {
 
170
    int32_t x;
 
171
 
 
172
    for (x= 0; x < index.index_part_size() ; x++)
 
173
    {
 
174
      const message::Table::Index::IndexPart part= index.index_part(x);
 
175
 
 
176
      if (x != 0)
 
177
        cout << ",";
 
178
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
 
179
      if (part.has_compare_length())
 
180
        cout << "(" << part.compare_length() << ")";
 
181
    }
 
182
    cout << ")";
 
183
  }
 
184
  cout << "\t";
 
185
}
 
186
 
 
187
static void print_table_options(const message::Table::TableOptions &options)
 
188
{
 
189
  if (options.has_comment())
 
190
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
191
 
 
192
  if (options.has_collation())
 
193
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
194
 
 
195
  if (options.has_auto_increment())
 
196
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
197
 
 
198
  if (options.has_collation_id())
 
199
    cout << "-- collation_id = " << options.collation_id() << endl;
110
200
  
111
 
  for (x= 0; x < table->field_size() ; x++)
112
 
  {
113
 
    const drizzle::Table::Field field = table->field(x);
 
201
  if (options.has_row_type())
 
202
    cout << " ROW_TYPE = " << options.row_type() << endl;
 
203
 
 
204
  if (options.has_data_file_name())
 
205
    cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
 
206
 
 
207
  if (options.has_index_file_name())
 
208
    cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
 
209
 
 
210
  if (options.has_max_rows())
 
211
    cout << " MAX_ROWS = " << options.max_rows() << endl;
 
212
 
 
213
  if (options.has_min_rows())
 
214
    cout << " MIN_ROWS = " << options.min_rows() << endl;
 
215
 
 
216
  if (options.has_auto_increment_value())
 
217
    cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
 
218
 
 
219
  if (options.has_avg_row_length())
 
220
    cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
 
221
 
 
222
  if (options.has_key_block_size())
 
223
    cout << " KEY_BLOCK_SIZE = "  << options.key_block_size() << endl;
 
224
 
 
225
  if (options.has_block_size())
 
226
    cout << " BLOCK_SIZE = " << options.block_size() << endl;
 
227
 
 
228
  if (options.has_comment())
 
229
    cout << " COMMENT = '" << options.comment() << "'" << endl;
 
230
 
 
231
  if (options.has_pack_keys())
 
232
    cout << " PACK_KEYS = " << options.pack_keys() << endl;
 
233
  if (options.has_pack_record())
 
234
    cout << " PACK_RECORD = " << options.pack_record() << endl;
 
235
  if (options.has_checksum())
 
236
    cout << " CHECKSUM = " << options.checksum() << endl;
 
237
  if (options.has_page_checksum())
 
238
    cout << " PAGE_CHECKSUM = " << options.page_checksum() << endl;
 
239
}
 
240
 
 
241
 
 
242
static void print_table(const message::Table &table)
 
243
{
 
244
  int32_t x;
 
245
 
 
246
  cout << "CREATE ";
 
247
 
 
248
  if (table.type() == message::Table::TEMPORARY)
 
249
    cout << "TEMPORARY ";
 
250
 
 
251
  cout << "TABLE `" << table.name() << "` (" << endl;
 
252
 
 
253
  for (x= 0; x < table.field_size() ; x++)
 
254
  {
 
255
    const message::Table::Field field = table.field(x);
 
256
 
 
257
    if (x != 0)
 
258
      cout << "," << endl;
 
259
 
 
260
    print_field(field);
 
261
  }
 
262
 
 
263
  for (x= 0; x < table.indexes_size() ; x++)
 
264
  {
 
265
    const message::Table::Index index= table.indexes(x);
114
266
 
115
267
    if (x != 0)
116
268
      cout << "," << endl;;
117
269
 
118
 
    cout << "\t`" << field.name() << "`";
119
 
    printType(&field);
120
 
  }
121
 
 
122
 
  for (x= 0; x < table->index_size() ; x++)
123
 
  {
124
 
    const drizzle::Table::Index index = table->index(x);
125
 
 
126
 
    cout << "," << endl;;
127
 
 
128
 
    cout << "\t";
129
 
 
130
 
    if (index.primary())
131
 
      cout << " PRIMARY KEY (`" << index.name() << "`)";
132
 
    else
133
 
    {
134
 
      int x;
135
 
 
136
 
      cout << " UNIQUE KEY `" << index.name() << "` (";
137
 
      for (x= 0; x < index.values_size() ; x++)
138
 
      {
139
 
        const drizzle::Table::KeyPart key= index.values(x);
140
 
 
141
 
        if (x != 0)
142
 
          cout << ",";
143
 
        cout << "`" << key.name() << "`";
144
 
      }
145
 
      cout << ")";
146
 
    }
 
270
    print_index(index);
 
271
 
147
272
  }
148
273
  cout << endl;
149
274
 
150
275
  cout << ") " << endl;
151
 
  if (table->has_collation())
152
 
    cout << " COLLATE = `" << table->collation() << "` " << endl;;
153
 
  if (table->has_characterset())
154
 
    cout << " CHARACTER SET = `" << table->characterset() << "` " << endl;;
155
 
  if (table->has_comment())
156
 
    cout << " COMMENT = `" << table->comment() << "` " << endl;;
157
 
  if (table->has_engine())
158
 
  if (table->has_data_directory())
159
 
    cout << " DATA DIRECTORY = `" << table->data_directory() << "` " << endl;;
160
 
  if (table->has_index_directory())
161
 
    cout << " INDEX DIRECTORY = `" << table->index_directory() << "`" << endl;
162
 
  cout << " ENGINE = " << table->engine() << ";"  << endl;
 
276
 
 
277
  print_engine(table.engine());
 
278
 
 
279
  if (table.has_options())
 
280
    print_table_options(table.options());
 
281
  /*
 
282
  if (table->has_stats())
 
283
    print_table_stats(&table->stats());
 
284
  */
163
285
}
164
286
 
165
 
int main(int argc, char* argv[]) 
 
287
int main(int argc, char* argv[])
166
288
{
167
289
  GOOGLE_PROTOBUF_VERIFY_VERSION;
168
290
 
171
293
    return -1;
172
294
  }
173
295
 
174
 
  drizzle::Table table;
 
296
  message::Table table;
175
297
 
176
298
  {
177
 
    // Read the existing address book.
178
 
    fstream input(argv[1], ios::in | ios::binary);
179
 
    if (!table.ParseFromIstream(&input)) 
 
299
    int fd= open(argv[1], O_RDONLY);
 
300
 
 
301
    if(fd==-1)
 
302
    {
 
303
      perror("Failed to open table definition file");
 
304
      return -1;
 
305
    }
 
306
 
 
307
    protobuf::io::ZeroCopyInputStream* input=
 
308
      new protobuf::io::FileInputStream(fd);
 
309
 
 
310
    if (!table.ParseFromZeroCopyStream(input))
180
311
    {
181
312
      cerr << "Failed to parse table." << endl;
 
313
      close(fd);
182
314
      return -1;
183
315
    }
 
316
 
 
317
    close(fd);
184
318
  }
185
319
 
186
 
  printTable(&table);
 
320
  print_table(table);
187
321
 
188
322
  return 0;
189
323
}