~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

  • Committer: Monty Taylor
  • Date: 2008-08-21 03:34:05 UTC
  • mfrom: (352.1.2 proto-definitions)
  • Revision ID: monty@inaugust.com-20080821033405-299aw5nxk7cpj6de
Merged from Jay.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
  Written from Google proto example
9
9
*/
10
10
 
11
 
void updateTable(drizzle::Table *table, const char *name) 
 
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) 
12
76
{
13
 
  int x;
 
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;
14
87
 
15
88
  table->set_name(name);
16
 
  table->set_engine("Innodb");
 
89
  table->set_type(Table::STANDARD);
17
90
 
18
91
  /* Write out some random varchar */
19
92
  for (x= 0; x < 3; x++)
20
93
  {
21
94
    char buffer[1024];
22
 
    drizzle::Table::Field *field = table->add_field();
 
95
    field= table->add_field();
 
96
    field_constraints= field->mutable_constraints();
 
97
    string_field_options= field->mutable_string_options();
23
98
 
24
99
    sprintf(buffer, "sample%u", x);
25
100
 
26
101
    field->set_name(buffer);
27
 
    field->set_type(drizzle::Table::VARCHAR);
28
 
    if (x % 2)
29
 
      field->set_is_notnull(true);
30
 
 
31
 
    field->set_length(rand() % 100);
 
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);
32
107
 
33
108
    if (x % 3)
34
109
    {
35
 
      field->set_collation("utf8_swedish_ci");
36
 
      field->set_characterset("utf8");
 
110
      string_field_options->set_collation("utf8_swedish_ci");
 
111
      string_field_options->set_charset("utf8");
37
112
    }
38
113
  }
39
114
 
40
115
  /* Write out an INTEGER */
41
116
  {
42
 
    drizzle::Table::Field *field = table->add_field();
 
117
    field= table->add_field();
43
118
    field->set_name("number");
44
 
    field->set_type(drizzle::Table::INTEGER);
 
119
    field->set_type(Table::Field::INTEGER);
45
120
  }
46
121
  /* Write out a ENUM */
47
122
  {
48
 
    drizzle::Table::Field *field = table->add_field();
49
 
    field->set_type(drizzle::Table::ENUM);
 
123
    field= table->add_field();
 
124
    field->set_type(Table::Field::ENUM);
50
125
    field->set_name("colors");
51
 
    field->add_values("red");
52
 
    field->add_values("blue");
53
 
    field->add_values("green");
 
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());
54
132
  }
55
133
  /* Write out a BLOB */
56
134
  {
57
 
    drizzle::Table::Field *field = table->add_field();
 
135
    field= table->add_field();
58
136
    field->set_name("some_btye_string");
59
 
    field->set_type(drizzle::Table::BLOB);
 
137
    field->set_type(Table::Field::BLOB);
60
138
  }
61
139
  /* Write out a DECIMAL */
62
140
  {
63
 
    drizzle::Table::Field *field = table->add_field();
 
141
    field= table->add_field();
64
142
    field->set_name("important_number");
65
 
    field->set_type(drizzle::Table::DECIMAL);
66
 
    field->set_length(8);
67
 
    field->set_scale(3);
68
 
  }
69
 
  /* Write out a VARCHAR with index */
70
 
  {
71
 
    drizzle::Table::Field *field = table->add_field();
72
 
    field->set_name("important_string");
73
 
    field->set_type(drizzle::Table::VARCHAR);
74
 
    field->set_length(20);
75
 
    field->set_unique(true);
76
 
  }
77
 
 
78
 
  {
79
 
    drizzle::Table::Index *index = table->add_index();
80
 
    index->set_name("number");
81
 
    index->set_primary(true);
82
 
  }
83
 
 
84
 
  for (x= 0; x < 2; x++)
85
 
  {
86
 
    char buffer[1024];
87
 
    drizzle::Table::Index *index = table->add_index();
88
 
    drizzle::Table::KeyPart *keypart = index->add_values();
89
 
 
90
 
    sprintf(buffer, "sample%u", x);
91
 
    index->set_name(buffer);
92
 
 
93
 
    keypart->set_name(buffer);
94
 
  }
 
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);
95
190
 
96
191
}
97
192
 
106
201
 
107
202
  drizzle::Table table;
108
203
 
109
 
  updateTable(&table, "example_table");
 
204
  fill_table(&table, "example_table");
110
205
 
111
206
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
112
207
  if (!table.SerializeToOstream(&output))