~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_writer.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-07-30 02:39:13 UTC
  • mto: (1115.3.11 captain)
  • mto: This revision was merged to the branch mainline in revision 1121.
  • Revision ID: osullivan.padraig@gmail.com-20090730023913-o2zuocp32l6btnc2
Removing references to MY_BITMAP throughout the code base and updating calls
to MyBitmap in various places to use the new interface.

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 <getopt.h>
26
4
#include <drizzled/message/table.pb.h>
27
5
 
28
6
using namespace std;
29
 
using namespace drizzled;
 
7
using namespace drizzled::message;
30
8
 
31
9
/*
32
10
  Written from Google proto example
33
11
*/
34
12
 
35
 
static void fill_engine(message::Table::StorageEngine *engine)
 
13
static void fill_engine(::drizzled::message::Table::StorageEngine *engine)
36
14
{
37
15
  int16_t x;
38
16
 
39
17
  engine->set_name("InnoDB");
40
 
  message::Table::StorageEngine::EngineOption *option;
 
18
  Table::StorageEngine::EngineOption *option;
41
19
 
42
20
  string option_names[2]= {
43
21
    "INDEX_DIRECTORY"
55
33
    option= engine->add_option();
56
34
    option->set_option_name(option_names[x]);
57
35
    option->set_option_value(option_values[x]);
58
 
    option->set_option_type(message::Table::StorageEngine::EngineOption::STRING);
 
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,
 
40
static void new_index_to_table(::drizzled::message::Table *table,
63
41
                               const string name,
64
42
                               uint16_t num_index_parts,
65
43
                               uint32_t field_indexes[],
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
static void fill_table(::drizzled::message::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::Table::StorageEngine *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
 
static void usage(char *argv0)
220
 
{
221
 
  cerr << "Usage:  " << argv0 << " [-t N] TABLE_NAME.dfe" << endl;
222
 
  cerr << endl;
223
 
  cerr << "-t N\tTable Number" << endl;
224
 
  cerr << "\t0 - default" << endl;
225
 
  cerr << endl;
226
 
}
227
 
 
228
172
int main(int argc, char* argv[])
229
173
{
230
 
  int opt;
231
 
  int table_number= 0;
232
 
 
233
174
  GOOGLE_PROTOBUF_VERIFY_VERSION;
234
175
 
235
 
  while ((opt= getopt(argc, argv, "t:")) != -1)
236
 
  {
237
 
    switch (opt)
238
 
    {
239
 
    case 't':
240
 
      table_number= atoi(optarg);
241
 
      break;
242
 
    default:
243
 
      usage(argv[0]);
244
 
      exit(EXIT_FAILURE);
245
 
    }
246
 
  }
247
 
 
248
 
  if (optind >= argc) {
249
 
    fprintf(stderr, "Expected Table name argument\n\n");
250
 
    usage(argv[0]);
251
 
    exit(EXIT_FAILURE);
252
 
  }
253
 
 
254
 
  message::Table table;
255
 
 
256
 
  switch (table_number)
257
 
  {
258
 
  case 0:
259
 
    fill_table(&table, "example_table");
260
 
    break;
261
 
  case 1:
262
 
    fill_table1(&table);
263
 
    break;
264
 
  default:
265
 
    fprintf(stderr, "Invalid table number.\n\n");
266
 
    usage(argv[0]);
267
 
    exit(EXIT_FAILURE);
268
 
  }
269
 
 
270
 
  fstream output(argv[optind], 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);
271
187
  if (!table.SerializeToOstream(&output))
272
188
  {
273
189
    cerr << "Failed to write schema." << endl;