30
29
/* Add some engine options */
31
for (x= 0; x < 2; x++)
30
for (x= 0; x < 2; ++x) {
33
31
option= engine->add_option();
34
option->set_option_name(option_names[x]);
35
option->set_option_value(option_values[x]);
36
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);
40
static void new_index_to_table(message::Table *table,
42
uint16_t num_index_parts,
43
uint32_t field_indexes[],
44
uint32_t compare_lengths[],
50
message::Table::Index *index;
51
message::Table::Index::IndexPart *index_part;
53
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();
55
56
index->set_name(name);
56
index->set_type(message::Table::Index::BTREE);
57
index->set_type(Table::Index::BTREE);
57
58
index->set_is_primary(is_primary);
58
59
index->set_is_unique(is_unique);
60
while (x < num_index_parts)
61
while (x < num_index_parts) {
62
63
index_part= index->add_index_part();
64
index_part->set_fieldnr(field_indexes[x]);
65
field= index_part->mutable_field();
66
*field= table->field(field_indexes[x]);
66
68
if (compare_lengths[x] > 0)
67
69
index_part->set_compare_length(compare_lengths[x]);
73
static void fill_table(message::Table *table, const char *name)
75
void fill_table(drizzle::Table *table, const char *name)
77
message::Table::Field *field;
78
message::Table::Field::FieldConstraints *field_constraints;
79
message::Table::Field::StringFieldOptions *string_field_options;
80
message::Table::Field::NumericFieldOptions *numeric_field_options;
81
message::Table::Field::SetFieldOptions *set_field_options;
79
using namespace drizzle;
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;
83
88
table->set_name(name);
84
table->set_type(message::Table::STANDARD);
89
table->set_type(Table::STANDARD);
86
91
/* Write out some random varchar */
87
92
for (x= 0; x < 3; x++)
111
117
field= table->add_field();
112
118
field->set_name("number");
113
field->set_type(message::Table::Field::INTEGER);
119
field->set_type(Table::Field::INTEGER);
115
121
/* Write out a ENUM */
117
123
field= table->add_field();
118
field->set_type(message::Table::Field::ENUM);
124
field->set_type(Table::Field::ENUM);
119
125
field->set_name("colors");
121
127
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());
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());
127
133
/* Write out a BLOB */
129
135
field= table->add_field();
130
136
field->set_name("some_btye_string");
131
field->set_type(message::Table::Field::BLOB);
137
field->set_type(Table::Field::BLOB);
134
139
/* Write out a DECIMAL */
136
141
field= table->add_field();
137
142
field->set_name("important_number");
138
field->set_type(message::Table::Field::DECIMAL);
143
field->set_type(Table::Field::DECIMAL);
140
145
field_constraints= field->mutable_constraints();
141
146
field_constraints->set_is_nullable(true);
143
148
numeric_field_options= field->mutable_numeric_options();
144
149
numeric_field_options->set_precision(8);
145
150
numeric_field_options->set_scale(3);
149
uint32_t fields_in_index[1]= {6};
150
uint32_t compare_lengths_in_index[1]= {0};
151
bool is_unique= true;
152
bool is_primary= false;
153
/* Add a single-column index on important_number field */
154
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
158
/* Add a double-column index on first two varchar fields */
159
uint32_t fields_in_index[2]= {0,1};
160
uint32_t compare_lengths_in_index[2]= {20,35};
161
bool is_unique= true;
162
bool is_primary= true;
163
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
166
187
/* Do engine-specific stuff */
167
message::Table::StorageEngine *engine= table->mutable_engine();
188
Table::StorageEngine *engine= table->mutable_engine();
168
189
fill_engine(engine);
172
int main(int argc, char* argv[])
193
int main(int argc, char* argv[])
174
195
GOOGLE_PROTOBUF_VERIFY_VERSION;
178
198
cerr << "Usage: " << argv[0] << " SCHEMA" << endl;
182
message::Table table;
202
drizzle::Table table;
184
204
fill_table(&table, "example_table");
186
206
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
187
if (!table.SerializeToOstream(&output))
207
if (!table.SerializeToOstream(&output))
189
209
cerr << "Failed to write schema." << endl;