1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
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.
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.
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
25
#include <drizzled/message/table.pb.h>
27
#include <boost/program_options.hpp>
29
5
using namespace std;
30
using namespace drizzled;
32
namespace po=boost::program_options;
35
8
Written from Google proto example
38
static void fill_engine(message::Engine *engine)
11
void fill_engine(drizzle::Table::StorageEngine *engine) {
12
using namespace drizzle;
40
16
engine->set_name("InnoDB");
41
message::Engine::Option *option;
17
Table::StorageEngine::EngineOption *option;
43
19
string option_names[2]= {
53
29
/* Add some engine options */
54
for (int16_t x= 0; x < 2; x++)
56
option= engine->add_options();
30
for (x= 0; x < 2; ++x) {
31
option= engine->add_option();
57
32
option->set_name(option_names[x]);
58
option->set_state(option_values[x]);
33
option->set_value(option_values[x]);
34
option->set_type(Table::StorageEngine::EngineOption::STRING);
62
static void new_index_to_table(message::Table *table,
64
uint16_t num_index_parts,
65
uint32_t field_indexes[],
66
uint32_t compare_lengths[],
72
message::Table::Index *index;
73
message::Table::Index::IndexPart *index_part;
75
index= table->add_indexes();
38
void new_index_to_table(
40
, const std::string name
41
, uint16_t num_index_parts
42
, uint32_t field_indexes[]
43
, uint32_t compare_lengths[]
47
using namespace drizzle;
52
Table::Index::IndexPart *index_part;
54
index= table->add_index();
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);
84
for(int i=0; i< num_index_parts; i++)
85
key_length+= compare_lengths[i];
87
index->set_key_length(key_length);
89
while (x < num_index_parts)
61
while (x < num_index_parts) {
91
63
index_part= index->add_index_part();
93
index_part->set_fieldnr(field_indexes[x]);
65
field= index_part->mutable_field();
66
*field= table->field(field_indexes[x]);
95
68
if (compare_lengths[x] > 0)
96
69
index_part->set_compare_length(compare_lengths[x]);
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);
144
121
/* Write out a ENUM */
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");
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());
155
133
/* Write out a BLOB */
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);
162
139
/* Write out a DECIMAL */
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);
168
145
field_constraints= field->mutable_constraints();
169
field_constraints->set_is_notnull(false);
146
field_constraints->set_is_nullable(true);
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);
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 */
161
, "idx_important_decimal"
164
, compare_lengths_in_index
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;
181
, compare_lengths_in_index
194
187
/* Do engine-specific stuff */
195
message::Engine *engine= table->mutable_engine();
188
Table::StorageEngine *engine= table->mutable_engine();
196
189
fill_engine(engine);
200
static void fill_table1(message::Table *table)
202
message::Table::Field *field;
203
message::Table::TableOptions *tableopts;
205
table->set_name("t1");
206
table->set_catalog("LOCAL");
207
table->set_type(message::Table::INTERNAL);
209
tableopts= table->mutable_options();
210
tableopts->set_comment("Table without a StorageEngine message");
213
field= table->add_field();
214
field->set_name("number");
215
field->set_type(message::Table::Field::INTEGER);
221
int main(int argc, char* argv[])
193
int main(int argc, char* argv[])
225
195
GOOGLE_PROTOBUF_VERIFY_VERSION;
227
po::options_description desc("Allowed options");
229
("help", "produce help message")
230
("table-number,t", po::value<int>(&table_number)->default_value(0), "Table Number");
232
po::variables_map vm;
233
po::positional_options_description p;
234
p.add("table-name", 1);
236
// Disable allow_guessing
237
int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
239
po::store(po::command_line_parser(argc, argv).options(desc).style(style).
240
positional(p).run(), vm);
242
if (not vm.count("table-name"))
244
fprintf(stderr, "Expected Table name argument\n\n");
245
cerr << desc << endl;
249
message::Table table;
251
switch (table_number)
254
fill_table(&table, "example_table");
260
fprintf(stderr, "Invalid table number.\n\n");
261
cerr << desc << endl;
265
fstream output(vm["table-name"].as<string>().c_str(),
266
ios::out | ios::trunc | ios::binary);
267
if (not table.SerializeToOstream(&output))
198
cerr << "Usage: " << argv[0] << " SCHEMA" << endl;
202
drizzle::Table table;
204
fill_table(&table, "example_table");
206
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
207
if (!table.SerializeToOstream(&output))
269
209
cerr << "Failed to write schema." << endl;