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
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
26
4
#include <drizzled/message/table.pb.h>
28
6
using namespace std;
29
using namespace drizzled;
7
using namespace drizzled::message;
32
10
Written from Google proto example
35
static void fill_engine(message::Table::StorageEngine *engine)
13
static void fill_engine(::drizzled::message::Table::StorageEngine *engine)
39
17
engine->set_name("InnoDB");
40
message::Table::StorageEngine::EngineOption *option;
18
Table::StorageEngine::EngineOption *option;
42
20
string option_names[2]= {
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);
62
static void new_index_to_table(message::Table *table,
40
static void new_index_to_table(::drizzled::message::Table *table,
64
42
uint16_t num_index_parts,
65
43
uint32_t field_indexes[],
72
message::Table::Index *index;
73
message::Table::Index::IndexPart *index_part;
51
Table::Index::IndexPart *index_part;
75
53
index= table->add_indexes();
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);
84
for(int i=0; i< num_index_parts; i++)
85
key_length+= compare_lengths[i];
87
index->set_key_length(key_length);
89
60
while (x < num_index_parts)
91
62
index_part= index->add_index_part();
102
static void fill_table(message::Table *table, const char *name)
73
static void fill_table(::drizzled::message::Table *table, const char *name)
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;
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;
112
83
table->set_name(name);
113
table->set_type(message::Table::STANDARD);
84
table->set_type(Table::STANDARD);
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();
123
snprintf(buffer, sizeof(buffer), "sample%u", x);
94
sprintf(buffer, "sample%u", x);
125
96
field->set_name(buffer);
126
field->set_type(message::Table::Field::VARCHAR);
97
field->set_type(Table::Field::VARCHAR);
128
99
field_constraints->set_is_nullable((x % 2));
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);
144
115
/* Write out a ENUM */
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");
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());
155
127
/* Write out a BLOB */
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);
162
134
/* Write out a DECIMAL */
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);
168
140
field_constraints= field->mutable_constraints();
169
141
field_constraints->set_is_nullable(true);
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);
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_type(message::Table::INTERNAL);
208
tableopts= table->mutable_options();
209
tableopts->set_comment("Table without a StorageEngine message");
212
field= table->add_field();
213
field->set_name("number");
214
field->set_type(message::Table::Field::INTEGER);
219
static void usage(char *argv0)
221
cerr << "Usage: " << argv0 << " [-t N] TABLE_NAME.dfe" << endl;
223
cerr << "-t N\tTable Number" << endl;
224
cerr << "\t0 - default" << endl;
228
172
int main(int argc, char* argv[])
233
174
GOOGLE_PROTOBUF_VERIFY_VERSION;
235
while ((opt= getopt(argc, argv, "t:")) != -1)
240
table_number= atoi(optarg);
248
if (optind >= argc) {
249
fprintf(stderr, "Expected Table name argument\n\n");
254
message::Table table;
256
switch (table_number)
259
fill_table(&table, "example_table");
265
fprintf(stderr, "Invalid table number.\n\n");
270
fstream output(argv[optind], ios::out | ios::trunc | ios::binary);
178
cerr << "Usage: " << argv[0] << " SCHEMA" << endl;
184
fill_table(&table, "example_table");
186
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
271
187
if (!table.SerializeToOstream(&output))
273
189
cerr << "Failed to write schema." << endl;