~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/message/table_writer.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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
#include <iostream>
 
23
#include <fstream>
 
24
#include <string>
 
25
#include <getopt.h>
 
26
#include <drizzled/message/table.pb.h>
 
27
 
 
28
using namespace std;
 
29
using namespace drizzled;
 
30
 
 
31
/*
 
32
  Written from Google proto example
 
33
*/
 
34
 
 
35
static void fill_engine(message::Table::StorageEngine *engine)
 
36
{
 
37
  int16_t x;
 
38
 
 
39
  engine->set_name("InnoDB");
 
40
  message::Table::StorageEngine::EngineOption *option;
 
41
 
 
42
  string option_names[2]= {
 
43
    "INDEX_DIRECTORY"
 
44
    , "DATA_DIRECTORY"
 
45
  };
 
46
 
 
47
  string option_values[2]= {
 
48
    "/var/drizzle/indexdir"
 
49
    , "/var/drizzle/datadir"
 
50
  };
 
51
 
 
52
  /* Add some engine options */
 
53
  for (x= 0; x < 2; x++)
 
54
  {
 
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);
 
59
  }
 
60
}
 
61
 
 
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)
 
69
{
 
70
  uint16_t x= 0;
 
71
 
 
72
  message::Table::Index *index;
 
73
  message::Table::Index::IndexPart *index_part;
 
74
 
 
75
  index= table->add_indexes();
 
76
 
 
77
  index->set_name(name);
 
78
  index->set_type(message::Table::Index::BTREE);
 
79
  index->set_is_primary(is_primary);
 
80
  index->set_is_unique(is_unique);
 
81
 
 
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
  while (x < num_index_parts)
 
90
  {
 
91
    index_part= index->add_index_part();
 
92
 
 
93
    index_part->set_fieldnr(field_indexes[x]);
 
94
 
 
95
    if (compare_lengths[x] > 0)
 
96
      index_part->set_compare_length(compare_lengths[x]);
 
97
 
 
98
    x++;
 
99
  }
 
100
}
 
101
 
 
102
static void fill_table(message::Table *table, const char *name)
 
103
{
 
104
  uint16_t x;
 
105
 
 
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::SetFieldOptions *set_field_options;
 
111
 
 
112
  table->set_name(name);
 
113
  table->set_type(message::Table::STANDARD);
 
114
 
 
115
  /* Write out some random varchar */
 
116
  for (x= 0; x < 3; x++)
 
117
  {
 
118
    char buffer[1024];
 
119
    field= table->add_field();
 
120
    field_constraints= field->mutable_constraints();
 
121
    string_field_options= field->mutable_string_options();
 
122
 
 
123
    sprintf(buffer, "sample%u", x);
 
124
 
 
125
    field->set_name(buffer);
 
126
    field->set_type(message::Table::Field::VARCHAR);
 
127
 
 
128
    field_constraints->set_is_nullable((x % 2));
 
129
 
 
130
    string_field_options->set_length(rand() % 100);
 
131
 
 
132
    if (x % 3)
 
133
    {
 
134
      string_field_options->set_collation("utf8_swedish_ci");
 
135
    }
 
136
  }
 
137
 
 
138
  /* Write out an INTEGER */
 
139
  {
 
140
    field= table->add_field();
 
141
    field->set_name("number");
 
142
    field->set_type(message::Table::Field::INTEGER);
 
143
  }
 
144
  /* Write out a ENUM */
 
145
  {
 
146
    field= table->add_field();
 
147
    field->set_type(message::Table::Field::ENUM);
 
148
    field->set_name("colors");
 
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());
 
155
  }
 
156
  /* Write out a BLOB */
 
157
  {
 
158
    field= table->add_field();
 
159
    field->set_name("some_btye_string");
 
160
    field->set_type(message::Table::Field::BLOB);
 
161
  }
 
162
 
 
163
  /* Write out a DECIMAL */
 
164
  {
 
165
    field= table->add_field();
 
166
    field->set_name("important_number");
 
167
    field->set_type(message::Table::Field::DECIMAL);
 
168
 
 
169
    field_constraints= field->mutable_constraints();
 
170
    field_constraints->set_is_nullable(true);
 
171
 
 
172
    numeric_field_options= field->mutable_numeric_options();
 
173
    numeric_field_options->set_precision(8);
 
174
    numeric_field_options->set_scale(3);
 
175
  }
 
176
 
 
177
  {
 
178
    uint32_t fields_in_index[1]= {6};
 
179
    uint32_t compare_lengths_in_index[1]= {0};
 
180
    bool is_unique= true;
 
181
    bool is_primary= false;
 
182
    /* Add a single-column index on important_number field */
 
183
    new_index_to_table(table, "idx_important_decimal", 1, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
 
184
  }
 
185
 
 
186
  {
 
187
    /* Add a double-column index on first two varchar fields */
 
188
    uint32_t fields_in_index[2]= {0,1};
 
189
    uint32_t compare_lengths_in_index[2]= {20,35};
 
190
    bool is_unique= true;
 
191
    bool is_primary= true;
 
192
    new_index_to_table(table, "idx_varchar1_2", 2, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
 
193
  }
 
194
 
 
195
  /* Do engine-specific stuff */
 
196
  message::Table::StorageEngine *engine= table->mutable_engine();
 
197
  fill_engine(engine);
 
198
 
 
199
}
 
200
 
 
201
static void fill_table1(message::Table *table)
 
202
{
 
203
  message::Table::Field *field;
 
204
  message::Table::TableOptions *tableopts;
 
205
 
 
206
  table->set_name("t1");
 
207
  table->set_type(message::Table::INTERNAL);
 
208
 
 
209
  tableopts= table->mutable_options();
 
210
  tableopts->set_comment("Table without a StorageEngine message");
 
211
 
 
212
  {
 
213
    field= table->add_field();
 
214
    field->set_name("number");
 
215
    field->set_type(message::Table::Field::INTEGER);
 
216
  }
 
217
 
 
218
}
 
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
 
 
229
int main(int argc, char* argv[])
 
230
{
 
231
  int opt;
 
232
  int table_number= 0;
 
233
 
 
234
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
235
 
 
236
  while ((opt= getopt(argc, argv, "t:")) != -1)
 
237
  {
 
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
    fprintf(stderr, "Expected Table name argument\n\n");
 
251
    usage(argv[0]);
 
252
    exit(EXIT_FAILURE);
 
253
  }
 
254
 
 
255
  message::Table table;
 
256
 
 
257
  switch (table_number)
 
258
  {
 
259
  case 0:
 
260
    fill_table(&table, "example_table");
 
261
    break;
 
262
  case 1:
 
263
    fill_table1(&table);
 
264
    break;
 
265
  default:
 
266
    fprintf(stderr, "Invalid table number.\n\n");
 
267
    usage(argv[0]);
 
268
    exit(EXIT_FAILURE);
 
269
  }
 
270
 
 
271
  fstream output(argv[optind], ios::out | ios::trunc | ios::binary);
 
272
  if (!table.SerializeToOstream(&output))
 
273
  {
 
274
    cerr << "Failed to write schema." << endl;
 
275
    return -1;
 
276
  }
 
277
 
 
278
  return 0;
 
279
}