~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <iostream>
2
2
#include <fstream>
3
3
#include <string>
4
 
#include <drizzled/message/table.pb.h>
5
 
 
 
4
#include "table.pb.h"
6
5
using namespace std;
7
 
using namespace drizzled;
8
6
 
9
 
/*
 
7
/* 
10
8
  Written from Google proto example
11
9
*/
12
10
 
13
 
static void fill_engine(message::Table::StorageEngine *engine)
14
 
{
 
11
void fill_engine(drizzle::Table::StorageEngine *engine) {
 
12
  using namespace drizzle;
 
13
  using std::string;
15
14
  int16_t x;
16
15
 
17
16
  engine->set_name("InnoDB");
18
 
  message::Table::StorageEngine::EngineOption *option;
 
17
  Table::StorageEngine::EngineOption *option;
19
18
 
20
19
  string option_names[2]= {
21
20
    "INDEX_DIRECTORY"
28
27
  };
29
28
 
30
29
  /* Add some engine options */
31
 
  for (x= 0; x < 2; x++)
32
 
  {
 
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);
37
35
  }
38
36
}
39
37
 
40
 
static void new_index_to_table(message::Table *table,
41
 
                               const string name,
42
 
                               uint16_t num_index_parts,
43
 
                               uint32_t field_indexes[],
44
 
                               uint32_t compare_lengths[],
45
 
                               bool is_primary,
46
 
                               bool is_unique)
47
 
{
48
 
  uint16_t x= 0;
49
 
 
50
 
  message::Table::Index *index;
51
 
  message::Table::Index::IndexPart *index_part;
52
 
 
53
 
  index= table->add_indexes();
 
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();
54
55
 
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);
59
60
 
60
 
  while (x < num_index_parts)
61
 
  {
 
61
  while (x < num_index_parts) {
 
62
 
62
63
    index_part= index->add_index_part();
63
64
 
64
 
    index_part->set_fieldnr(field_indexes[x]);
 
65
    field= index_part->mutable_field();
 
66
    *field= table->field(field_indexes[x]);
65
67
 
66
68
    if (compare_lengths[x] > 0)
67
69
      index_part->set_compare_length(compare_lengths[x]);
70
72
  }
71
73
}
72
74
 
73
 
static void fill_table(message::Table *table, const char *name)
 
75
void fill_table(drizzle::Table *table, const char *name) 
74
76
{
75
77
  uint16_t x;
76
78
 
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;
 
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;
82
87
 
83
88
  table->set_name(name);
84
 
  table->set_type(message::Table::STANDARD);
 
89
  table->set_type(Table::STANDARD);
85
90
 
86
91
  /* Write out some random varchar */
87
92
  for (x= 0; x < 3; x++)
94
99
    sprintf(buffer, "sample%u", x);
95
100
 
96
101
    field->set_name(buffer);
97
 
    field->set_type(message::Table::Field::VARCHAR);
 
102
    field->set_type(Table::Field::VARCHAR);
98
103
 
99
104
    field_constraints->set_is_nullable((x % 2));
100
105
 
103
108
    if (x % 3)
104
109
    {
105
110
      string_field_options->set_collation("utf8_swedish_ci");
 
111
      string_field_options->set_charset("utf8");
106
112
    }
107
113
  }
108
114
 
110
116
  {
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);
114
120
  }
115
121
  /* Write out a ENUM */
116
122
  {
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");
120
126
 
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());
126
132
  }
127
133
  /* Write out a BLOB */
128
134
  {
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);
132
138
  }
133
 
 
134
139
  /* Write out a DECIMAL */
135
140
  {
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);
139
144
 
140
145
    field_constraints= field->mutable_constraints();
141
146
    field_constraints->set_is_nullable(true);
142
 
 
 
147
    
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);
146
151
  }
147
152
 
148
153
  {
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 */
 
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
    );
155
168
  }
156
169
 
157
170
  {
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;
 
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
    );
164
185
  }
165
186
 
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);
169
190
 
170
191
}
171
192
 
172
 
int main(int argc, char* argv[])
 
193
int main(int argc, char* argv[]) 
173
194
{
174
195
  GOOGLE_PROTOBUF_VERIFY_VERSION;
175
196
 
176
 
  if (argc != 2)
177
 
  {
 
197
  if (argc != 2) {
178
198
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
179
199
    return -1;
180
200
  }
181
201
 
182
 
  message::Table table;
 
202
  drizzle::Table table;
183
203
 
184
204
  fill_table(&table, "example_table");
185
205
 
186
206
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
187
 
  if (!table.SerializeToOstream(&output))
 
207
  if (!table.SerializeToOstream(&output)) 
188
208
  {
189
209
    cerr << "Failed to write schema." << endl;
190
210
    return -1;