~drizzle-trunk/drizzle/development

820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
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>
873.2.1 by Monty Taylor
Added missing header for solaris build.
7
#include <unistd.h>
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
8
323 by Brian Aker
Updated proto file for table (not FRM work).
9
#include <iostream>
10
#include <string>
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
11
#include <drizzled/message/table.pb.h>
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
12
#include <google/protobuf/io/zero_copy_stream.h>
13
#include <google/protobuf/io/zero_copy_stream_impl.h>
685.1.5 by Monty Taylor
Fixed a few things to make VPATH builds work.
14
323 by Brian Aker
Updated proto file for table (not FRM work).
15
using namespace std;
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
16
using namespace drizzled::message;
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
17
using namespace google::protobuf::io;
323 by Brian Aker
Updated proto file for table (not FRM work).
18
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
19
/*
323 by Brian Aker
Updated proto file for table (not FRM work).
20
  Written from Google proto example
21
*/
22
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
23
void print_field(const ::drizzled::message::Table::Field &field)
323 by Brian Aker
Updated proto file for table (not FRM work).
24
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
25
  cout << "\t`" << field.name() << "`";
896.3.7 by Stewart Smith
fix table_reader for virtual columns
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)
323 by Brian Aker
Updated proto file for table (not FRM work).
36
  {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
37
    case Table::Field::DOUBLE:
323 by Brian Aker
Updated proto file for table (not FRM work).
38
    cout << " DOUBLE ";
39
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
40
  case Table::Field::VARCHAR:
779.3.10 by Monty Taylor
Turned on -Wshadow.
41
    cout << " VARCHAR(" << field.string_options().length() << ")";
323 by Brian Aker
Updated proto file for table (not FRM work).
42
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
43
  case Table::Field::BLOB:
896.3.6 by Stewart Smith
Read Fields out of proto instead of FRM.
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() << " ";
323 by Brian Aker
Updated proto file for table (not FRM work).
47
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
48
  case Table::Field::ENUM:
323 by Brian Aker
Updated proto file for table (not FRM work).
49
    {
50
      int x;
51
52
      cout << " ENUM(";
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
53
      for (x= 0; x < field.set_options().field_value_size() ; x++)
323 by Brian Aker
Updated proto file for table (not FRM work).
54
      {
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
55
        const string type= field.set_options().field_value(x);
323 by Brian Aker
Updated proto file for table (not FRM work).
56
57
        if (x != 0)
58
          cout << ",";
59
        cout << "'" << type << "'";
60
      }
61
      cout << ") ";
62
      break;
63
    }
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
64
  case Table::Field::TINYINT:
65
    cout << " TINYINT ";
323 by Brian Aker
Updated proto file for table (not FRM work).
66
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
67
  case Table::Field::INTEGER:
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
68
    cout << " INT" ;
323 by Brian Aker
Updated proto file for table (not FRM work).
69
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
70
  case Table::Field::BIGINT:
323 by Brian Aker
Updated proto file for table (not FRM work).
71
    cout << " BIGINT ";
72
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
73
  case Table::Field::DECIMAL:
779.3.10 by Monty Taylor
Turned on -Wshadow.
74
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
75
    break;
76
  case Table::Field::DATE:
323 by Brian Aker
Updated proto file for table (not FRM work).
77
    cout << " DATE ";
78
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
79
  case Table::Field::TIME:
323 by Brian Aker
Updated proto file for table (not FRM work).
80
    cout << " TIME ";
81
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
82
  case Table::Field::TIMESTAMP:
323 by Brian Aker
Updated proto file for table (not FRM work).
83
    cout << " TIMESTAMP ";
84
    break;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
85
  case Table::Field::DATETIME:
323 by Brian Aker
Updated proto file for table (not FRM work).
86
    cout << " DATETIME ";
87
    break;
869.1.10 by Stewart Smith
Add some missing things to field storage in proto
88
  case Table::Field::VIRTUAL:
896.3.7 by Stewart Smith
fix table_reader for virtual columns
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 ";
323 by Brian Aker
Updated proto file for table (not FRM work).
97
  }
98
779.3.10 by Monty Taylor
Turned on -Wshadow.
99
  if (field.type() == Table::Field::INTEGER
100
      || field.type() == Table::Field::BIGINT
101
      || field.type() == Table::Field::TINYINT)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
102
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
103
    if (field.has_constraints()
104
        && field.constraints().has_is_unsigned())
105
      if (field.constraints().is_unsigned())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
106
        cout << " UNSIGNED";
107
779.3.10 by Monty Taylor
Turned on -Wshadow.
108
    if (field.has_numeric_options() &&
109
      field.numeric_options().is_autoincrement())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
110
      cout << " AUTOINCREMENT ";
111
  }
112
869.1.21 by Stewart Smith
correctly store nullable attribute in table proto. fix table_reader.
113
  if (!( field.has_constraints()
114
	 && field.constraints().is_nullable()))
323 by Brian Aker
Updated proto file for table (not FRM work).
115
    cout << " NOT NULL ";
116
896.3.6 by Stewart Smith
Read Fields out of proto instead of FRM.
117
  if (field.type() == Table::Field::BLOB
779.3.10 by Monty Taylor
Turned on -Wshadow.
118
      || field.type() == Table::Field::VARCHAR)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
119
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
120
    if (field.string_options().has_collation())
121
      cout << " COLLATE " << field.string_options().collation();
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
122
  }
123
779.3.10 by Monty Taylor
Turned on -Wshadow.
124
  if (field.options().has_default_value())
125
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
126
896.3.4 by Stewart Smith
Create default_values record from proto instead of reading from FRM. assert if different to FRM.
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
779.3.10 by Monty Taylor
Turned on -Wshadow.
137
  if (field.type() == Table::Field::TIMESTAMP)
138
    if (field.timestamp_options().has_auto_updates()
139
      && field.timestamp_options().auto_updates())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
140
      cout << " ON UPDATE CURRENT_TIMESTAMP";
323 by Brian Aker
Updated proto file for table (not FRM work).
141
779.3.10 by Monty Taylor
Turned on -Wshadow.
142
  if (field.has_comment())
143
    cout << " COMMENT `" << field.comment() << "` ";
323 by Brian Aker
Updated proto file for table (not FRM work).
144
}
145
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
146
void print_engine(const ::drizzled::message::Table::StorageEngine &engine)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
147
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
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);
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
154
    cout << "\t" << option.option_name() << " = "
155
	 << option.option_value() << endl;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
156
  }
157
}
158
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
159
void print_index(const ::drizzled::message::Table::Index &index)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
160
{
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
161
779.3.10 by Monty Taylor
Turned on -Wshadow.
162
  if (index.is_primary())
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
163
    cout << " PRIMARY";
779.3.10 by Monty Taylor
Turned on -Wshadow.
164
  else if (index.is_unique())
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
165
    cout << " UNIQUE";
779.3.10 by Monty Taylor
Turned on -Wshadow.
166
  cout << " KEY `" << index.name() << "` (";
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
167
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
168
    int32_t x;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
169
779.3.10 by Monty Taylor
Turned on -Wshadow.
170
    for (x= 0; x < index.index_part_size() ; x++)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
171
    {
779.3.10 by Monty Taylor
Turned on -Wshadow.
172
      const Table::Index::IndexPart part= index.index_part(x);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
173
174
      if (x != 0)
175
        cout << ",";
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
176
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
177
      if (part.has_compare_length())
178
        cout << "(" << part.compare_length() << ")";
179
    }
180
    cout << ")";
181
  }
182
  cout << "\t";
183
}
184
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
185
void print_table_stats(const ::drizzled::message::Table::TableStats&) 
779.3.10 by Monty Taylor
Turned on -Wshadow.
186
{
187
188
}
189
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
190
void print_table_options(const ::drizzled::message::Table::TableOptions &options)
779.3.10 by Monty Taylor
Turned on -Wshadow.
191
{
192
  if (options.has_comment())
193
    cout << " COMMENT = '" << options.comment() << "' " << endl;
194
195
  if (options.has_collation())
196
    cout << " COLLATE = '" << options.collation() << "' " << endl;
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
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
896.4.3 by Stewart Smith
fix table_reader for new table_options
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;
779.3.10 by Monty Taylor
Turned on -Wshadow.
247
}
248
249
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
250
void print_table(const ::drizzled::message::Table &table)
779.3.10 by Monty Taylor
Turned on -Wshadow.
251
{
252
  int32_t x;
323 by Brian Aker
Updated proto file for table (not FRM work).
253
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
254
  cout << "CREATE ";
255
779.3.10 by Monty Taylor
Turned on -Wshadow.
256
  if (table.type() == Table::TEMPORARY)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
257
    cout << "TEMPORARY ";
258
779.3.10 by Monty Taylor
Turned on -Wshadow.
259
  cout << "TABLE `" << table.name() << "` (" << endl;
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
260
779.3.10 by Monty Taylor
Turned on -Wshadow.
261
  for (x= 0; x < table.field_size() ; x++)
323 by Brian Aker
Updated proto file for table (not FRM work).
262
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
263
    const Table::Field field = table.field(x);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
264
265
    if (x != 0)
266
      cout << "," << endl;
267
779.3.10 by Monty Taylor
Turned on -Wshadow.
268
    print_field(field);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
269
  }
270
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
271
  for (x= 0; x < table.indexes_size() ; x++)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
272
  {
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
273
    const Table::Index index= table.indexes(x);
323 by Brian Aker
Updated proto file for table (not FRM work).
274
275
    if (x != 0)
276
      cout << "," << endl;;
277
779.3.10 by Monty Taylor
Turned on -Wshadow.
278
    print_index(index);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
279
323 by Brian Aker
Updated proto file for table (not FRM work).
280
  }
281
  cout << endl;
282
283
  cout << ") " << endl;
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
284
779.3.10 by Monty Taylor
Turned on -Wshadow.
285
  print_engine(table.engine());
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
286
779.3.10 by Monty Taylor
Turned on -Wshadow.
287
  if (table.has_options())
288
    print_table_options(table.options());
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
289
  /*
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
290
  if (table->has_stats())
291
    print_table_stats(&table->stats());
292
  */
323 by Brian Aker
Updated proto file for table (not FRM work).
293
}
294
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
295
int main(int argc, char* argv[])
323 by Brian Aker
Updated proto file for table (not FRM work).
296
{
297
  GOOGLE_PROTOBUF_VERIFY_VERSION;
298
299
  if (argc != 2) {
300
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
301
    return -1;
302
  }
303
779.3.10 by Monty Taylor
Turned on -Wshadow.
304
  Table table;
323 by Brian Aker
Updated proto file for table (not FRM work).
305
306
  {
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
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))
323 by Brian Aker
Updated proto file for table (not FRM work).
318
    {
319
      cerr << "Failed to parse table." << endl;
869.1.1 by Stewart Smith
closing file descriptors is good.
320
      close(fd);
323 by Brian Aker
Updated proto file for table (not FRM work).
321
      return -1;
322
    }
869.1.1 by Stewart Smith
closing file descriptors is good.
323
324
    close(fd);
323 by Brian Aker
Updated proto file for table (not FRM work).
325
  }
326
779.3.10 by Monty Taylor
Turned on -Wshadow.
327
  print_table(table);
323 by Brian Aker
Updated proto file for table (not FRM work).
328
329
  return 0;
330
}