~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <iostream>
2
 
#include <fstream>
3
 
#include <string>
4
 
#include "table.pb.h"
5
 
using namespace std;
6
 
 
7
 
/* 
8
 
  Written from Google proto example
9
 
*/
10
 
 
11
 
void fill_engine(drizzle::Table::StorageEngine *engine) {
12
 
  using namespace drizzle;
13
 
  using std::string;
14
 
  int16_t x;
15
 
 
16
 
  engine->set_name("InnoDB");
17
 
  Table::StorageEngine::EngineOption *option;
18
 
 
19
 
  string option_names[2]= {
20
 
    "INDEX_DIRECTORY"
21
 
    , "DATA_DIRECTORY"
22
 
  };
23
 
 
24
 
  string option_values[2]= {
25
 
    "/var/drizzle/indexdir"
26
 
    , "/var/drizzle/datadir"
27
 
  };
28
 
 
29
 
  /* Add some engine options */
30
 
  for (x= 0; x < 2; ++x) {
31
 
    option= engine->add_option();
32
 
    option->set_name(option_names[x]);
33
 
    option->set_value(option_values[x]);
34
 
    option->set_type(Table::StorageEngine::EngineOption::STRING);
35
 
  }
36
 
}
37
 
 
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();
55
 
 
56
 
  index->set_name(name);
57
 
  index->set_type(Table::Index::BTREE);
58
 
  index->set_is_primary(is_primary);
59
 
  index->set_is_unique(is_unique);
60
 
 
61
 
  while (x < num_index_parts) {
62
 
 
63
 
    index_part= index->add_index_part();
64
 
 
65
 
    field= index_part->mutable_field();
66
 
    *field= table->field(field_indexes[x]);
67
 
 
68
 
    if (compare_lengths[x] > 0)
69
 
      index_part->set_compare_length(compare_lengths[x]);
70
 
 
71
 
    x++;
72
 
  }
73
 
}
74
 
 
75
 
void fill_table(drizzle::Table *table, const char *name) 
76
 
{
77
 
  uint16_t x;
78
 
 
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;
87
 
 
88
 
  table->set_name(name);
89
 
  table->set_type(Table::STANDARD);
90
 
 
91
 
  /* Write out some random varchar */
92
 
  for (x= 0; x < 3; x++)
93
 
  {
94
 
    char buffer[1024];
95
 
    field= table->add_field();
96
 
    field_constraints= field->mutable_constraints();
97
 
    string_field_options= field->mutable_string_options();
98
 
 
99
 
    sprintf(buffer, "sample%u", x);
100
 
 
101
 
    field->set_name(buffer);
102
 
    field->set_type(Table::Field::VARCHAR);
103
 
 
104
 
    field_constraints->set_is_nullable((x % 2));
105
 
 
106
 
    string_field_options->set_length(rand() % 100);
107
 
 
108
 
    if (x % 3)
109
 
    {
110
 
      string_field_options->set_collation("utf8_swedish_ci");
111
 
      string_field_options->set_charset("utf8");
112
 
    }
113
 
  }
114
 
 
115
 
  /* Write out an INTEGER */
116
 
  {
117
 
    field= table->add_field();
118
 
    field->set_name("number");
119
 
    field->set_type(Table::Field::INTEGER);
120
 
  }
121
 
  /* Write out a ENUM */
122
 
  {
123
 
    field= table->add_field();
124
 
    field->set_type(Table::Field::ENUM);
125
 
    field->set_name("colors");
126
 
 
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());
132
 
  }
133
 
  /* Write out a BLOB */
134
 
  {
135
 
    field= table->add_field();
136
 
    field->set_name("some_btye_string");
137
 
    field->set_type(Table::Field::BLOB);
138
 
  }
139
 
  /* Write out a DECIMAL */
140
 
  {
141
 
    field= table->add_field();
142
 
    field->set_name("important_number");
143
 
    field->set_type(Table::Field::DECIMAL);
144
 
 
145
 
    field_constraints= field->mutable_constraints();
146
 
    field_constraints->set_is_nullable(true);
147
 
    
148
 
    numeric_field_options= field->mutable_numeric_options();
149
 
    numeric_field_options->set_precision(8);
150
 
    numeric_field_options->set_scale(3);
151
 
  }
152
 
 
153
 
  {
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
 
    );
168
 
  }
169
 
 
170
 
  {
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
 
    );
185
 
  }
186
 
 
187
 
  /* Do engine-specific stuff */
188
 
  Table::StorageEngine *engine= table->mutable_engine();
189
 
  fill_engine(engine);
190
 
 
191
 
}
192
 
 
193
 
int main(int argc, char* argv[]) 
194
 
{
195
 
  GOOGLE_PROTOBUF_VERIFY_VERSION;
196
 
 
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)) 
208
 
  {
209
 
    cerr << "Failed to write schema." << endl;
210
 
    return -1;
211
 
  }
212
 
 
213
 
  return 0;
214
 
}