~drizzle-trunk/drizzle/development

1119.2.10 by Monty Taylor
Merged Stewart from lp:~stewart-flamingspork/drizzle/table-proto-text-reader
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
1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
21
#include "drizzled/global.h"
22
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
23
#include <sys/types.h>
24
#include <sys/stat.h>
25
#include <fcntl.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <errno.h>
873.2.1 by Monty Taylor
Added missing header for solaris build.
29
#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
30
323 by Brian Aker
Updated proto file for table (not FRM work).
31
#include <iostream>
32
#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.
33
#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
34
#include <google/protobuf/io/zero_copy_stream.h>
35
#include <google/protobuf/io/zero_copy_stream_impl.h>
685.1.5 by Monty Taylor
Fixed a few things to make VPATH builds work.
36
323 by Brian Aker
Updated proto file for table (not FRM work).
37
using namespace std;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
38
using namespace drizzled;
39
using namespace google;
323 by Brian Aker
Updated proto file for table (not FRM work).
40
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
41
/*
323 by Brian Aker
Updated proto file for table (not FRM work).
42
  Written from Google proto example
43
*/
44
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
45
static void print_field(const message::Table::Field &field)
323 by Brian Aker
Updated proto file for table (not FRM work).
46
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
47
  cout << "\t`" << field.name() << "`";
896.3.7 by Stewart Smith
fix table_reader for virtual columns
48
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
49
  message::Table::Field::FieldType field_type= field.type();
896.3.7 by Stewart Smith
fix table_reader for virtual columns
50
51
  switch (field_type)
323 by Brian Aker
Updated proto file for table (not FRM work).
52
  {
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
53
    case message::Table::Field::DOUBLE:
323 by Brian Aker
Updated proto file for table (not FRM work).
54
    cout << " DOUBLE ";
55
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
56
  case message::Table::Field::VARCHAR:
779.3.10 by Monty Taylor
Turned on -Wshadow.
57
    cout << " VARCHAR(" << field.string_options().length() << ")";
323 by Brian Aker
Updated proto file for table (not FRM work).
58
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
59
  case message::Table::Field::BLOB:
896.3.6 by Stewart Smith
Read Fields out of proto instead of FRM.
60
    cout << " BLOB "; /* FIXME: or text, depends on collation */
61
    if(field.string_options().has_collation_id())
62
      cout << "COLLATION=" << field.string_options().collation_id() << " ";
323 by Brian Aker
Updated proto file for table (not FRM work).
63
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
64
  case message::Table::Field::ENUM:
323 by Brian Aker
Updated proto file for table (not FRM work).
65
    {
66
      int x;
67
68
      cout << " ENUM(";
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
69
      for (x= 0; x < field.set_options().field_value_size() ; x++)
323 by Brian Aker
Updated proto file for table (not FRM work).
70
      {
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
71
        const string type= field.set_options().field_value(x);
323 by Brian Aker
Updated proto file for table (not FRM work).
72
73
        if (x != 0)
74
          cout << ",";
75
        cout << "'" << type << "'";
76
      }
77
      cout << ") ";
78
      break;
79
    }
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
80
  case message::Table::Field::INTEGER:
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
81
    cout << " INT" ;
323 by Brian Aker
Updated proto file for table (not FRM work).
82
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
83
  case message::Table::Field::BIGINT:
323 by Brian Aker
Updated proto file for table (not FRM work).
84
    cout << " BIGINT ";
85
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
86
  case message::Table::Field::DECIMAL:
779.3.10 by Monty Taylor
Turned on -Wshadow.
87
    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
88
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
89
  case message::Table::Field::DATE:
323 by Brian Aker
Updated proto file for table (not FRM work).
90
    cout << " DATE ";
91
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
92
  case message::Table::Field::TIME:
323 by Brian Aker
Updated proto file for table (not FRM work).
93
    cout << " TIME ";
94
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
95
  case message::Table::Field::TIMESTAMP:
323 by Brian Aker
Updated proto file for table (not FRM work).
96
    cout << " TIMESTAMP ";
97
    break;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
98
  case message::Table::Field::DATETIME:
323 by Brian Aker
Updated proto file for table (not FRM work).
99
    cout << " DATETIME ";
100
    break;
101
  }
102
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
103
  if (field.type() == message::Table::Field::INTEGER
1144.3.3 by Stewart Smith
remove TINYINT from table proto (but not fully from the server)
104
      || field.type() == message::Table::Field::BIGINT)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
105
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
106
    if (field.has_constraints()
107
        && field.constraints().has_is_unsigned())
108
      if (field.constraints().is_unsigned())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
109
        cout << " UNSIGNED";
110
779.3.10 by Monty Taylor
Turned on -Wshadow.
111
    if (field.has_numeric_options() &&
112
      field.numeric_options().is_autoincrement())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
113
      cout << " AUTOINCREMENT ";
114
  }
115
869.1.21 by Stewart Smith
correctly store nullable attribute in table proto. fix table_reader.
116
  if (!( field.has_constraints()
117
	 && field.constraints().is_nullable()))
323 by Brian Aker
Updated proto file for table (not FRM work).
118
    cout << " NOT NULL ";
119
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
120
  if (field.type() == message::Table::Field::BLOB
121
      || field.type() == message::Table::Field::VARCHAR)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
122
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
123
    if (field.string_options().has_collation())
124
      cout << " COLLATE " << field.string_options().collation();
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
125
  }
126
779.3.10 by Monty Taylor
Turned on -Wshadow.
127
  if (field.options().has_default_value())
128
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
129
896.3.4 by Stewart Smith
Create default_values record from proto instead of reading from FRM. assert if different to FRM.
130
  if (field.options().has_default_bin_value())
131
  {
132
    string v= field.options().default_bin_value();
133
    cout << " DEFAULT 0x";
134
    for(unsigned int i=0; i< v.length(); i++)
135
    {
136
      printf("%.2x", *(v.c_str()+i));
137
    }
138
  }
139
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
140
  if (field.type() == message::Table::Field::TIMESTAMP)
779.3.10 by Monty Taylor
Turned on -Wshadow.
141
    if (field.timestamp_options().has_auto_updates()
142
      && field.timestamp_options().auto_updates())
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
143
      cout << " ON UPDATE CURRENT_TIMESTAMP";
323 by Brian Aker
Updated proto file for table (not FRM work).
144
779.3.10 by Monty Taylor
Turned on -Wshadow.
145
  if (field.has_comment())
146
    cout << " COMMENT `" << field.comment() << "` ";
323 by Brian Aker
Updated proto file for table (not FRM work).
147
}
148
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
149
static void print_engine(const message::Table::StorageEngine &engine)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
150
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
151
  int32_t x;
152
153
  cout << " ENGINE = " << engine.name()  << endl;
154
155
  for (x= 0; x < engine.option_size(); ++x) {
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
156
    const message::Table::StorageEngine::EngineOption option= engine.option(x);
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
157
    cout << "\t" << option.option_name() << " = "
158
	 << option.option_value() << endl;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
159
  }
160
}
161
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
162
static void print_index(const message::Table::Index &index)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
163
{
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
164
779.3.10 by Monty Taylor
Turned on -Wshadow.
165
  if (index.is_primary())
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
166
    cout << " PRIMARY";
779.3.10 by Monty Taylor
Turned on -Wshadow.
167
  else if (index.is_unique())
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
168
    cout << " UNIQUE";
779.3.10 by Monty Taylor
Turned on -Wshadow.
169
  cout << " KEY `" << index.name() << "` (";
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
170
  {
779.3.10 by Monty Taylor
Turned on -Wshadow.
171
    int32_t x;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
172
779.3.10 by Monty Taylor
Turned on -Wshadow.
173
    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
174
    {
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
175
      const message::Table::Index::IndexPart part= index.index_part(x);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
176
177
      if (x != 0)
178
        cout << ",";
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
179
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
180
      if (part.has_compare_length())
181
        cout << "(" << part.compare_length() << ")";
182
    }
183
    cout << ")";
184
  }
185
  cout << "\t";
186
}
187
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
188
static void print_table_options(const message::Table::TableOptions &options)
779.3.10 by Monty Taylor
Turned on -Wshadow.
189
{
190
  if (options.has_comment())
191
    cout << " COMMENT = '" << options.comment() << "' " << endl;
192
193
  if (options.has_collation())
194
    cout << " COLLATE = '" << options.collation() << "' " << endl;
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
195
196
  if (options.has_auto_increment())
197
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
198
199
  if (options.has_collation_id())
200
    cout << "-- collation_id = " << options.collation_id() << endl;
201
  
202
  if (options.has_row_type())
203
    cout << " ROW_TYPE = " << options.row_type() << endl;
204
896.4.3 by Stewart Smith
fix table_reader for new table_options
205
  if (options.has_data_file_name())
206
    cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
207
208
  if (options.has_index_file_name())
209
    cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
210
211
  if (options.has_max_rows())
212
    cout << " MAX_ROWS = " << options.max_rows() << endl;
213
214
  if (options.has_min_rows())
215
    cout << " MIN_ROWS = " << options.min_rows() << endl;
216
217
  if (options.has_auto_increment_value())
218
    cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
219
220
  if (options.has_avg_row_length())
221
    cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
222
223
  if (options.has_key_block_size())
224
    cout << " KEY_BLOCK_SIZE = "  << options.key_block_size() << endl;
225
226
  if (options.has_block_size())
227
    cout << " BLOCK_SIZE = " << options.block_size() << endl;
228
229
  if (options.has_comment())
230
    cout << " COMMENT = '" << options.comment() << "'" << endl;
231
232
  if (options.has_pack_keys())
233
    cout << " PACK_KEYS = " << options.pack_keys() << endl;
234
  if (options.has_pack_record())
235
    cout << " PACK_RECORD = " << options.pack_record() << endl;
236
  if (options.has_checksum())
237
    cout << " CHECKSUM = " << options.checksum() << endl;
238
  if (options.has_page_checksum())
239
    cout << " PAGE_CHECKSUM = " << options.page_checksum() << endl;
779.3.10 by Monty Taylor
Turned on -Wshadow.
240
}
241
242
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
243
static void print_table(const message::Table &table)
779.3.10 by Monty Taylor
Turned on -Wshadow.
244
{
245
  int32_t x;
323 by Brian Aker
Updated proto file for table (not FRM work).
246
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
247
  cout << "CREATE ";
248
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
249
  if (table.type() == message::Table::TEMPORARY)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
250
    cout << "TEMPORARY ";
251
779.3.10 by Monty Taylor
Turned on -Wshadow.
252
  cout << "TABLE `" << table.name() << "` (" << endl;
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
253
779.3.10 by Monty Taylor
Turned on -Wshadow.
254
  for (x= 0; x < table.field_size() ; x++)
323 by Brian Aker
Updated proto file for table (not FRM work).
255
  {
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
256
    const message::Table::Field field = table.field(x);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
257
258
    if (x != 0)
259
      cout << "," << endl;
260
779.3.10 by Monty Taylor
Turned on -Wshadow.
261
    print_field(field);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
262
  }
263
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
264
  for (x= 0; x < table.indexes_size() ; x++)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
265
  {
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
266
    const message::Table::Index index= table.indexes(x);
323 by Brian Aker
Updated proto file for table (not FRM work).
267
268
    if (x != 0)
269
      cout << "," << endl;;
270
779.3.10 by Monty Taylor
Turned on -Wshadow.
271
    print_index(index);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
272
323 by Brian Aker
Updated proto file for table (not FRM work).
273
  }
274
  cout << endl;
275
276
  cout << ") " << endl;
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
277
779.3.10 by Monty Taylor
Turned on -Wshadow.
278
  print_engine(table.engine());
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
279
779.3.10 by Monty Taylor
Turned on -Wshadow.
280
  if (table.has_options())
281
    print_table_options(table.options());
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
282
  /*
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
283
  if (table->has_stats())
284
    print_table_stats(&table->stats());
285
  */
323 by Brian Aker
Updated proto file for table (not FRM work).
286
}
287
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
288
int main(int argc, char* argv[])
323 by Brian Aker
Updated proto file for table (not FRM work).
289
{
290
  GOOGLE_PROTOBUF_VERIFY_VERSION;
291
292
  if (argc != 2) {
293
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
294
    return -1;
295
  }
296
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
297
  message::Table table;
323 by Brian Aker
Updated proto file for table (not FRM work).
298
299
  {
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
300
    int fd= open(argv[1], O_RDONLY);
301
302
    if(fd==-1)
303
    {
304
      perror("Failed to open table definition file");
305
      return -1;
306
    }
307
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
308
    protobuf::io::ZeroCopyInputStream* input=
309
      new protobuf::io::FileInputStream(fd);
820.1.9 by Stewart Smith
remove the ass that is fstream and use good old open(2) and the protobuf ZeroCopyInputStream
310
311
    if (!table.ParseFromZeroCopyStream(input))
323 by Brian Aker
Updated proto file for table (not FRM work).
312
    {
313
      cerr << "Failed to parse table." << endl;
869.1.1 by Stewart Smith
closing file descriptors is good.
314
      close(fd);
323 by Brian Aker
Updated proto file for table (not FRM work).
315
      return -1;
316
    }
869.1.1 by Stewart Smith
closing file descriptors is good.
317
318
    close(fd);
323 by Brian Aker
Updated proto file for table (not FRM work).
319
  }
320
779.3.10 by Monty Taylor
Turned on -Wshadow.
321
  print_table(table);
323 by Brian Aker
Updated proto file for table (not FRM work).
322
323
  return 0;
324
}