~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

  • Committer: Monty Taylor
  • Date: 2009-03-04 02:48:12 UTC
  • mto: (917.1.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 918.
  • Revision ID: mordred@inaugust.com-20090304024812-5wb6wpye5c1iitbq
Applied atomic patch to current tree.

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; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
1
#include <iostream>
23
2
#include <fstream>
24
3
#include <string>
25
 
#include <drizzled/message/table.pb.h>
26
 
 
27
 
#include <boost/program_options.hpp>
 
4
#include <drizzled/serialize/table.pb.h>
28
5
 
29
6
using namespace std;
30
 
using namespace drizzled;
31
 
 
32
 
namespace po=boost::program_options;
 
7
using namespace drizzle;
33
8
 
34
9
/*
35
10
  Written from Google proto example
36
11
*/
37
12
 
38
 
static void fill_engine(message::Engine *engine)
 
13
void fill_engine(::drizzle::Table::StorageEngine *engine)
39
14
{
 
15
  int16_t x;
 
16
 
40
17
  engine->set_name("InnoDB");
41
 
  message::Engine::Option *option;
 
18
  Table::StorageEngine::EngineOption *option;
42
19
 
43
20
  string option_names[2]= {
44
21
    "INDEX_DIRECTORY"
51
28
  };
52
29
 
53
30
  /* Add some engine options */
54
 
  for (int16_t x= 0; x < 2; x++)
 
31
  for (x= 0; x < 2; x++)
55
32
  {
56
 
    option= engine->add_options();
57
 
    option->set_name(option_names[x]);
58
 
    option->set_state(option_values[x]);
 
33
    option= engine->add_option();
 
34
    option->set_option_name(option_names[x]);
 
35
    option->set_option_value(option_values[x]);
 
36
    option->set_option_type(Table::StorageEngine::EngineOption::STRING);
59
37
  }
60
38
}
61
39
 
62
 
static void new_index_to_table(message::Table *table,
63
 
                               const string name,
64
 
                               uint16_t num_index_parts,
65
 
                               uint32_t field_indexes[],
66
 
                               uint32_t compare_lengths[],
67
 
                               bool is_primary,
68
 
                               bool is_unique)
 
40
void new_index_to_table(::drizzle::Table *table,
 
41
                        const string name,
 
42
                        uint16_t num_index_parts,
 
43
                        uint32_t field_indexes[],
 
44
                        uint32_t compare_lengths[],
 
45
                        bool is_primary,
 
46
                        bool is_unique)
69
47
{
70
48
  uint16_t x= 0;
71
49
 
72
 
  message::Table::Index *index;
73
 
  message::Table::Index::IndexPart *index_part;
 
50
  Table::Index *index;
 
51
  Table::Index::IndexPart *index_part;
74
52
 
75
53
  index= table->add_indexes();
76
54
 
77
55
  index->set_name(name);
78
 
  index->set_type(message::Table::Index::BTREE);
 
56
  index->set_type(Table::Index::BTREE);
79
57
  index->set_is_primary(is_primary);
80
58
  index->set_is_unique(is_unique);
81
59
 
82
 
  int key_length= 0;
83
 
 
84
 
  for(int i=0; i< num_index_parts; i++)
85
 
    key_length+= compare_lengths[i];
86
 
 
87
 
  index->set_key_length(key_length);
88
 
 
89
60
  while (x < num_index_parts)
90
61
  {
91
62
    index_part= index->add_index_part();
99
70
  }
100
71
}
101
72
 
102
 
static void fill_table(message::Table *table, const char *name)
 
73
void fill_table(::drizzle::Table *table, const char *name)
103
74
{
104
75
  uint16_t x;
105
76
 
106
 
  message::Table::Field *field;
107
 
  message::Table::Field::FieldConstraints *field_constraints;
108
 
  message::Table::Field::StringFieldOptions *string_field_options;
109
 
  message::Table::Field::NumericFieldOptions *numeric_field_options;
110
 
  message::Table::Field::EnumerationValues *enumeration_options;
 
77
  Table::Field *field;
 
78
  Table::Field::FieldConstraints *field_constraints;
 
79
  Table::Field::StringFieldOptions *string_field_options;
 
80
  Table::Field::NumericFieldOptions *numeric_field_options;
 
81
  Table::Field::SetFieldOptions *set_field_options;
111
82
 
112
83
  table->set_name(name);
113
 
  table->set_type(message::Table::STANDARD);
 
84
  table->set_type(Table::STANDARD);
114
85
 
115
86
  /* Write out some random varchar */
116
87
  for (x= 0; x < 3; x++)
120
91
    field_constraints= field->mutable_constraints();
121
92
    string_field_options= field->mutable_string_options();
122
93
 
123
 
    snprintf(buffer, sizeof(buffer), "sample%u", x);
 
94
    sprintf(buffer, "sample%u", x);
124
95
 
125
96
    field->set_name(buffer);
126
 
    field->set_type(message::Table::Field::VARCHAR);
 
97
    field->set_type(Table::Field::VARCHAR);
127
98
 
128
99
    field_constraints->set_is_nullable((x % 2));
129
100
 
139
110
  {
140
111
    field= table->add_field();
141
112
    field->set_name("number");
142
 
    field->set_type(message::Table::Field::INTEGER);
 
113
    field->set_type(Table::Field::INTEGER);
143
114
  }
144
115
  /* Write out a ENUM */
145
116
  {
146
117
    field= table->add_field();
147
 
    field->set_type(message::Table::Field::ENUM);
 
118
    field->set_type(Table::Field::ENUM);
148
119
    field->set_name("colors");
149
120
 
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");
 
121
    set_field_options= field->mutable_set_options();
 
122
    set_field_options->add_field_value("red");
 
123
    set_field_options->add_field_value("blue");
 
124
    set_field_options->add_field_value("green");
 
125
    set_field_options->set_count_elements(set_field_options->field_value_size());
154
126
  }
155
127
  /* Write out a BLOB */
156
128
  {
157
129
    field= table->add_field();
158
130
    field->set_name("some_btye_string");
159
 
    field->set_type(message::Table::Field::BLOB);
 
131
    field->set_type(Table::Field::BLOB);
160
132
  }
161
133
 
162
134
  /* Write out a DECIMAL */
163
135
  {
164
136
    field= table->add_field();
165
137
    field->set_name("important_number");
166
 
    field->set_type(message::Table::Field::DECIMAL);
 
138
    field->set_type(Table::Field::DECIMAL);
167
139
 
168
140
    field_constraints= field->mutable_constraints();
169
141
    field_constraints->set_is_nullable(true);
192
164
  }
193
165
 
194
166
  /* Do engine-specific stuff */
195
 
  message::Engine *engine= table->mutable_engine();
 
167
  Table::StorageEngine *engine= table->mutable_engine();
196
168
  fill_engine(engine);
197
169
 
198
170
}
199
171
 
200
 
static void fill_table1(message::Table *table)
201
 
{
202
 
  message::Table::Field *field;
203
 
  message::Table::TableOptions *tableopts;
204
 
 
205
 
  table->set_name("t1");
206
 
  table->set_type(message::Table::INTERNAL);
207
 
 
208
 
  tableopts= table->mutable_options();
209
 
  tableopts->set_comment("Table without a StorageEngine message");
210
 
 
211
 
  {
212
 
    field= table->add_field();
213
 
    field->set_name("number");
214
 
    field->set_type(message::Table::Field::INTEGER);
215
 
  }
216
 
 
217
 
}
218
 
 
219
 
 
220
172
int main(int argc, char* argv[])
221
173
{
222
 
  int table_number= 0;
223
 
 
224
174
  GOOGLE_PROTOBUF_VERIFY_VERSION;
225
175
 
226
 
  po::options_description desc("Allowed options");
227
 
  desc.add_options()
228
 
    ("help", "produce help message")
229
 
    ("table-number,t", po::value<int>(&table_number)->default_value(0), "Table Number");
230
 
 
231
 
  po::variables_map vm;
232
 
  po::positional_options_description p;
233
 
  p.add("table-name", 1);
234
 
 
235
 
  // Disable allow_guessing
236
 
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
237
 
 
238
 
  po::store(po::command_line_parser(argc, argv).options(desc).style(style).
239
 
            positional(p).run(), vm);
240
 
 
241
 
  if (not vm.count("table-name"))
242
 
  {
243
 
    fprintf(stderr, "Expected Table name argument\n\n");
244
 
    cerr << desc << endl;
245
 
    exit(EXIT_FAILURE);
246
 
  }
247
 
 
248
 
  message::Table table;
249
 
 
250
 
  switch (table_number)
251
 
  {
252
 
  case 0:
253
 
    fill_table(&table, "example_table");
254
 
    break;
255
 
  case 1:
256
 
    fill_table1(&table);
257
 
    break;
258
 
  default:
259
 
    fprintf(stderr, "Invalid table number.\n\n");
260
 
    cerr << desc << endl;
261
 
    exit(EXIT_FAILURE);
262
 
  }
263
 
 
264
 
  fstream output(vm["table-name"].as<string>().c_str(),
265
 
                 ios::out | ios::trunc | ios::binary);
 
176
  if (argc != 2)
 
177
  {
 
178
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
 
179
    return -1;
 
180
  }
 
181
 
 
182
  Table table;
 
183
 
 
184
  fill_table(&table, "example_table");
 
185
 
 
186
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
266
187
  if (!table.SerializeToOstream(&output))
267
188
  {
268
189
    cerr << "Failed to write schema." << endl;