~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

code clean move Item_func_abs, Item_func_int_exp, Item_func_ln, Item_func_log to functions directory

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
 
#include <drizzled/message/table.pb.h>
27
 
 
 
4
#include "table.pb.h"
28
5
using namespace std;
29
 
using namespace drizzled;
30
6
 
31
 
/*
 
7
/* 
32
8
  Written from Google proto example
33
9
*/
34
10
 
35
 
static void fill_engine(message::Table::StorageEngine *engine)
36
 
{
 
11
void fill_engine(drizzle::Table::StorageEngine *engine) {
 
12
  using namespace drizzle;
 
13
  using std::string;
37
14
  int16_t x;
38
15
 
39
16
  engine->set_name("InnoDB");
40
 
  message::Table::StorageEngine::EngineOption *option;
 
17
  Table::StorageEngine::EngineOption *option;
41
18
 
42
19
  string option_names[2]= {
43
20
    "INDEX_DIRECTORY"
50
27
  };
51
28
 
52
29
  /* Add some engine options */
53
 
  for (x= 0; x < 2; x++)
54
 
  {
 
30
  for (x= 0; x < 2; ++x) {
55
31
    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);
 
32
    option->set_name(option_names[x]);
 
33
    option->set_value(option_values[x]);
 
34
    option->set_type(Table::StorageEngine::EngineOption::STRING);
59
35
  }
60
36
}
61
37
 
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();
 
38
void new_index_to_table(
 
39
    drizzle::Table *table
 
40
    , const std::string name
 
41
    , uint16_t num_index_parts
 
42
    , uint32_t field_indexes[]
 
43
    , uint32_t compare_lengths[]
 
44
    , bool is_primary
 
45
    , bool is_unique
 
46
    ) {
 
47
  using namespace drizzle;
 
48
  uint16_t x;
 
49
 
 
50
  Table::Index *index;
 
51
  Table::Field *field;
 
52
  Table::Index::IndexPart *index_part;
 
53
 
 
54
  index= table->add_index();
76
55
 
77
56
  index->set_name(name);
78
 
  index->set_type(message::Table::Index::BTREE);
 
57
  index->set_type(Table::Index::BTREE);
79
58
  index->set_is_primary(is_primary);
80
59
  index->set_is_unique(is_unique);
81
60
 
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
 
  {
 
61
  while (x < num_index_parts) {
 
62
 
91
63
    index_part= index->add_index_part();
92
64
 
93
 
    index_part->set_fieldnr(field_indexes[x]);
 
65
    field= index_part->mutable_field();
 
66
    *field= table->field(field_indexes[x]);
94
67
 
95
68
    if (compare_lengths[x] > 0)
96
69
      index_part->set_compare_length(compare_lengths[x]);
99
72
  }
100
73
}
101
74
 
102
 
static void fill_table(message::Table *table, const char *name)
 
75
void fill_table(drizzle::Table *table, const char *name) 
103
76
{
104
77
  uint16_t x;
105
78
 
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;
 
79
  using namespace drizzle;
 
80
 
 
81
  Table::Field *field;
 
82
  Table::Field::FieldConstraints *field_constraints;
 
83
  Table::Field::FieldOptions *field_options;
 
84
  Table::Field::StringFieldOptions *string_field_options;
 
85
  Table::Field::NumericFieldOptions *numeric_field_options;
 
86
  Table::Field::SetFieldOptions *set_field_options;
111
87
 
112
88
  table->set_name(name);
113
 
  table->set_type(message::Table::STANDARD);
 
89
  table->set_type(Table::STANDARD);
114
90
 
115
91
  /* Write out some random varchar */
116
92
  for (x= 0; x < 3; x++)
120
96
    field_constraints= field->mutable_constraints();
121
97
    string_field_options= field->mutable_string_options();
122
98
 
123
 
    snprintf(buffer, sizeof(buffer), "sample%u", x);
 
99
    sprintf(buffer, "sample%u", x);
124
100
 
125
101
    field->set_name(buffer);
126
 
    field->set_type(message::Table::Field::VARCHAR);
 
102
    field->set_type(Table::Field::VARCHAR);
127
103
 
128
104
    field_constraints->set_is_nullable((x % 2));
129
105
 
132
108
    if (x % 3)
133
109
    {
134
110
      string_field_options->set_collation("utf8_swedish_ci");
 
111
      string_field_options->set_charset("utf8");
135
112
    }
136
113
  }
137
114
 
139
116
  {
140
117
    field= table->add_field();
141
118
    field->set_name("number");
142
 
    field->set_type(message::Table::Field::INTEGER);
 
119
    field->set_type(Table::Field::INTEGER);
143
120
  }
144
121
  /* Write out a ENUM */
145
122
  {
146
123
    field= table->add_field();
147
 
    field->set_type(message::Table::Field::ENUM);
 
124
    field->set_type(Table::Field::ENUM);
148
125
    field->set_name("colors");
149
126
 
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");
 
127
    set_field_options= field->mutable_set_options();
 
128
    set_field_options->add_value("red");
 
129
    set_field_options->add_value("blue");
 
130
    set_field_options->add_value("green");
 
131
    set_field_options->set_count_elements(set_field_options->value_size());
154
132
  }
155
133
  /* Write out a BLOB */
156
134
  {
157
135
    field= table->add_field();
158
136
    field->set_name("some_btye_string");
159
 
    field->set_type(message::Table::Field::BLOB);
 
137
    field->set_type(Table::Field::BLOB);
160
138
  }
161
 
 
162
139
  /* Write out a DECIMAL */
163
140
  {
164
141
    field= table->add_field();
165
142
    field->set_name("important_number");
166
 
    field->set_type(message::Table::Field::DECIMAL);
 
143
    field->set_type(Table::Field::DECIMAL);
167
144
 
168
145
    field_constraints= field->mutable_constraints();
169
146
    field_constraints->set_is_nullable(true);
170
 
 
 
147
    
171
148
    numeric_field_options= field->mutable_numeric_options();
172
149
    numeric_field_options->set_precision(8);
173
150
    numeric_field_options->set_scale(3);
174
151
  }
175
152
 
176
153
  {
177
 
    uint32_t fields_in_index[1]= {6};
178
 
    uint32_t compare_lengths_in_index[1]= {0};
179
 
    bool is_unique= true;
180
 
    bool is_primary= false;
181
 
    /* Add a single-column index on important_number field */
182
 
    new_index_to_table(table, "idx_important_decimal", 1, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
 
154
  uint32_t fields_in_index[1]= {6};
 
155
  uint32_t compare_lengths_in_index[1]= {0};
 
156
  bool is_unique= true;
 
157
  bool is_primary= false;
 
158
  /* Add a single-column index on important_number field */
 
159
  new_index_to_table(
 
160
      table
 
161
    , "idx_important_decimal"
 
162
    , 1
 
163
    , fields_in_index
 
164
    , compare_lengths_in_index
 
165
    , is_primary
 
166
    , is_unique
 
167
    );
183
168
  }
184
169
 
185
170
  {
186
 
    /* Add a double-column index on first two varchar fields */
187
 
    uint32_t fields_in_index[2]= {0,1};
188
 
    uint32_t compare_lengths_in_index[2]= {20,35};
189
 
    bool is_unique= true;
190
 
    bool is_primary= true;
191
 
    new_index_to_table(table, "idx_varchar1_2", 2, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
 
171
  /* Add a double-column index on first two varchar fields */
 
172
  uint32_t fields_in_index[2]= {0,1};
 
173
  uint32_t compare_lengths_in_index[2]= {20,35};
 
174
  bool is_unique= true;
 
175
  bool is_primary= true;
 
176
  new_index_to_table(
 
177
      table
 
178
    , "idx_varchar1_2"
 
179
    , 2
 
180
    , fields_in_index
 
181
    , compare_lengths_in_index
 
182
    , is_primary
 
183
    , is_unique
 
184
    );
192
185
  }
193
186
 
194
187
  /* Do engine-specific stuff */
195
 
  message::Table::StorageEngine *engine= table->mutable_engine();
 
188
  Table::StorageEngine *engine= table->mutable_engine();
196
189
  fill_engine(engine);
197
190
 
198
191
}
199
192
 
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
 
int main(int argc, char* argv[])
229
 
{
230
 
  int opt;
231
 
  int table_number= 0;
232
 
 
 
193
int main(int argc, char* argv[]) 
 
194
{
233
195
  GOOGLE_PROTOBUF_VERIFY_VERSION;
234
196
 
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);
271
 
  if (!table.SerializeToOstream(&output))
 
197
  if (argc != 2) {
 
198
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
 
199
    return -1;
 
200
  }
 
201
 
 
202
  drizzle::Table table;
 
203
 
 
204
  fill_table(&table, "example_table");
 
205
 
 
206
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
 
207
  if (!table.SerializeToOstream(&output)) 
272
208
  {
273
209
    cerr << "Failed to write schema." << endl;
274
210
    return -1;