~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_writer.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
#include <iostream>
23
23
#include <fstream>
24
24
#include <string>
25
 
#include <getopt.h>
26
25
#include <drizzled/message/table.pb.h>
27
26
 
 
27
#include <boost/program_options.hpp>
 
28
 
28
29
using namespace std;
29
30
using namespace drizzled;
30
31
 
 
32
namespace po=boost::program_options;
 
33
 
31
34
/*
32
35
  Written from Google proto example
33
36
*/
34
37
 
35
 
static void fill_engine(message::Table::StorageEngine *engine)
 
38
static void fill_engine(message::Engine *engine)
36
39
{
37
 
  int16_t x;
38
 
 
39
40
  engine->set_name("InnoDB");
40
 
  message::Table::StorageEngine::EngineOption *option;
 
41
  message::Engine::Option *option;
41
42
 
42
43
  string option_names[2]= {
43
44
    "INDEX_DIRECTORY"
50
51
  };
51
52
 
52
53
  /* Add some engine options */
53
 
  for (x= 0; x < 2; x++)
 
54
  for (int16_t x= 0; x < 2; x++)
54
55
  {
55
 
    option= engine->add_option();
56
 
    option->set_option_name(option_names[x]);
57
 
    option->set_option_value(option_values[x]);
58
 
    option->set_option_type(message::Table::StorageEngine::EngineOption::STRING);
 
56
    option= engine->add_options();
 
57
    option->set_name(option_names[x]);
 
58
    option->set_state(option_values[x]);
59
59
  }
60
60
}
61
61
 
107
107
  message::Table::Field::FieldConstraints *field_constraints;
108
108
  message::Table::Field::StringFieldOptions *string_field_options;
109
109
  message::Table::Field::NumericFieldOptions *numeric_field_options;
110
 
  message::Table::Field::SetFieldOptions *set_field_options;
 
110
  message::Table::Field::EnumerationValues *enumeration_options;
111
111
 
112
112
  table->set_name(name);
113
113
  table->set_type(message::Table::STANDARD);
120
120
    field_constraints= field->mutable_constraints();
121
121
    string_field_options= field->mutable_string_options();
122
122
 
123
 
    sprintf(buffer, "sample%u", x);
 
123
    snprintf(buffer, sizeof(buffer), "sample%u", x);
124
124
 
125
125
    field->set_name(buffer);
126
126
    field->set_type(message::Table::Field::VARCHAR);
127
127
 
128
 
    field_constraints->set_is_nullable((x % 2));
 
128
    field_constraints->set_is_notnull((x % 2));
129
129
 
130
130
    string_field_options->set_length(rand() % 100);
131
131
 
147
147
    field->set_type(message::Table::Field::ENUM);
148
148
    field->set_name("colors");
149
149
 
150
 
    set_field_options= field->mutable_set_options();
151
 
    set_field_options->add_field_value("red");
152
 
    set_field_options->add_field_value("blue");
153
 
    set_field_options->add_field_value("green");
154
 
    set_field_options->set_count_elements(set_field_options->field_value_size());
 
150
    enumeration_options= field->mutable_enumeration_values();
 
151
    enumeration_options->add_field_value("red");
 
152
    enumeration_options->add_field_value("blue");
 
153
    enumeration_options->add_field_value("green");
155
154
  }
156
155
  /* Write out a BLOB */
157
156
  {
167
166
    field->set_type(message::Table::Field::DECIMAL);
168
167
 
169
168
    field_constraints= field->mutable_constraints();
170
 
    field_constraints->set_is_nullable(true);
 
169
    field_constraints->set_is_notnull(false);
171
170
 
172
171
    numeric_field_options= field->mutable_numeric_options();
173
172
    numeric_field_options->set_precision(8);
193
192
  }
194
193
 
195
194
  /* Do engine-specific stuff */
196
 
  message::Table::StorageEngine *engine= table->mutable_engine();
 
195
  message::Engine *engine= table->mutable_engine();
197
196
  fill_engine(engine);
198
197
 
199
198
}
204
203
  message::Table::TableOptions *tableopts;
205
204
 
206
205
  table->set_name("t1");
 
206
  table->set_catalog("LOCAL");
207
207
  table->set_type(message::Table::INTERNAL);
208
208
 
209
209
  tableopts= table->mutable_options();
217
217
 
218
218
}
219
219
 
220
 
static void usage(char *argv0)
221
 
{
222
 
  cerr << "Usage:  " << argv0 << " [-t N] TABLE_NAME.dfe" << endl;
223
 
  cerr << endl;
224
 
  cerr << "-t N\tTable Number" << endl;
225
 
  cerr << "\t0 - default" << endl;
226
 
  cerr << endl;
227
 
}
228
220
 
229
221
int main(int argc, char* argv[])
230
222
{
231
 
  int opt;
232
223
  int table_number= 0;
233
224
 
234
225
  GOOGLE_PROTOBUF_VERIFY_VERSION;
235
226
 
236
 
  while ((opt= getopt(argc, argv, "t:")) != -1)
 
227
  po::options_description desc("Allowed options");
 
228
  desc.add_options()
 
229
    ("help", "produce help message")
 
230
    ("table-number,t", po::value<int>(&table_number)->default_value(0), "Table Number");
 
231
 
 
232
  po::variables_map vm;
 
233
  po::positional_options_description p;
 
234
  p.add("table-name", 1);
 
235
 
 
236
  // Disable allow_guessing
 
237
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
 
238
 
 
239
  po::store(po::command_line_parser(argc, argv).options(desc).style(style).
 
240
            positional(p).run(), vm);
 
241
 
 
242
  if (not vm.count("table-name"))
237
243
  {
238
 
    switch (opt)
239
 
    {
240
 
    case 't':
241
 
      table_number= atoi(optarg);
242
 
      break;
243
 
    default:
244
 
      usage(argv[0]);
245
 
      exit(EXIT_FAILURE);
246
 
    }
247
 
  }
248
 
 
249
 
  if (optind >= argc) {
250
244
    fprintf(stderr, "Expected Table name argument\n\n");
251
 
    usage(argv[0]);
 
245
    cerr << desc << endl;
252
246
    exit(EXIT_FAILURE);
253
247
  }
254
248
 
264
258
    break;
265
259
  default:
266
260
    fprintf(stderr, "Invalid table number.\n\n");
267
 
    usage(argv[0]);
 
261
    cerr << desc << endl;
268
262
    exit(EXIT_FAILURE);
269
263
  }
270
264
 
271
 
  fstream output(argv[optind], ios::out | ios::trunc | ios::binary);
272
 
  if (!table.SerializeToOstream(&output))
 
265
  fstream output(vm["table-name"].as<string>().c_str(),
 
266
                 ios::out | ios::trunc | ios::binary);
 
267
  if (not table.SerializeToOstream(&output))
273
268
  {
274
269
    cerr << "Failed to write schema." << endl;
275
270
    return -1;