~drizzle-trunk/drizzle/development

1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 Sun Microsystems
5
 *
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.
9
 *
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.
14
 *
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
18
 */
19
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
20
#include "config.h"
1122.2.2 by Monty Taylor
Added missing copyright headers. Added drizzled/global.h to a few things that
21
323 by Brian Aker
Updated proto file for table (not FRM work).
22
#include <iostream>
23
#include <fstream>
24
#include <string>
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
25
#include <drizzled/message/table.pb.h>
779.3.10 by Monty Taylor
Turned on -Wshadow.
26
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
27
#include <boost/program_options.hpp>
28
323 by Brian Aker
Updated proto file for table (not FRM work).
29
using namespace std;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
30
using namespace drizzled;
323 by Brian Aker
Updated proto file for table (not FRM work).
31
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
32
namespace po=boost::program_options;
33
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
34
/*
323 by Brian Aker
Updated proto file for table (not FRM work).
35
  Written from Google proto example
36
*/
37
1502.1.31 by Brian Aker
Merge engine options for schema/table.
38
static void fill_engine(message::Engine *engine)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
39
{
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
40
  engine->set_name("InnoDB");
1502.1.31 by Brian Aker
Merge engine options for schema/table.
41
  message::Engine::Option *option;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
42
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
43
  string option_names[2]= {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
44
    "INDEX_DIRECTORY"
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
45
    , "DATA_DIRECTORY"
46
  };
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
47
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
48
  string option_values[2]= {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
49
    "/var/drizzle/indexdir"
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
50
    , "/var/drizzle/datadir"
51
  };
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
52
53
  /* Add some engine options */
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
54
  for (int16_t x= 0; x < 2; x++)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
55
  {
1502.1.30 by Brian Aker
First pass on cleanup of Stewart's patch, plus re-engineer to make it work a
56
    option= engine->add_options();
57
    option->set_name(option_names[x]);
58
    option->set_state(option_values[x]);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
59
  }
60
}
61
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
62
static void new_index_to_table(message::Table *table,
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
63
                               const string name,
64
                               uint16_t num_index_parts,
65
                               uint32_t field_indexes[],
66
                               uint32_t compare_lengths[],
67
                               bool is_primary,
68
                               bool is_unique)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
69
{
779.3.10 by Monty Taylor
Turned on -Wshadow.
70
  uint16_t x= 0;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
71
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
72
  message::Table::Index *index;
73
  message::Table::Index::IndexPart *index_part;
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
74
820.1.8 by Stewart Smith
start reading table definition from proto instead of FRM.
75
  index= table->add_indexes();
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
76
77
  index->set_name(name);
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
78
  index->set_type(message::Table::Index::BTREE);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
79
  index->set_is_primary(is_primary);
80
  index->set_is_unique(is_unique);
81
1273.2.19 by Stewart Smith
Fix table_write to write a valid Table protobuf message.
82
  int key_length= 0;
83
84
  for(int i=0; i< num_index_parts; i++)
85
    key_length+= compare_lengths[i];
86
87
  index->set_key_length(key_length);
88
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
89
  while (x < num_index_parts)
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
90
  {
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
91
    index_part= index->add_index_part();
92
820.1.15 by Stewart Smith
Read (nearly the whole) index information (key and key parts) out of the proto, not FRM.
93
    index_part->set_fieldnr(field_indexes[x]);
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
94
95
    if (compare_lengths[x] > 0)
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
96
      index_part->set_compare_length(compare_lengths[x]);
97
98
    x++;
99
  }
100
}
101
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
102
static void fill_table(message::Table *table, const char *name)
323 by Brian Aker
Updated proto file for table (not FRM work).
103
{
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
104
  uint16_t x;
105
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
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;
1396 by Brian Aker
Simple rename.
110
  message::Table::Field::EnumerationValues *enumeration_options;
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
111
323 by Brian Aker
Updated proto file for table (not FRM work).
112
  table->set_name(name);
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
113
  table->set_type(message::Table::STANDARD);
323 by Brian Aker
Updated proto file for table (not FRM work).
114
115
  /* Write out some random varchar */
116
  for (x= 0; x < 3; x++)
117
  {
118
    char buffer[1024];
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
119
    field= table->add_field();
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
120
    field_constraints= field->mutable_constraints();
121
    string_field_options= field->mutable_string_options();
323 by Brian Aker
Updated proto file for table (not FRM work).
122
1366.1.11 by Siddharth Prakash Singh
sprintf->snprintf continued
123
    snprintf(buffer, sizeof(buffer), "sample%u", x);
323 by Brian Aker
Updated proto file for table (not FRM work).
124
125
    field->set_name(buffer);
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
126
    field->set_type(message::Table::Field::VARCHAR);
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
127
128
    field_constraints->set_is_nullable((x % 2));
129
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
130
    string_field_options->set_length(rand() % 100);
323 by Brian Aker
Updated proto file for table (not FRM work).
131
132
    if (x % 3)
133
    {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
134
      string_field_options->set_collation("utf8_swedish_ci");
323 by Brian Aker
Updated proto file for table (not FRM work).
135
    }
136
  }
137
138
  /* Write out an INTEGER */
139
  {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
140
    field= table->add_field();
323 by Brian Aker
Updated proto file for table (not FRM work).
141
    field->set_name("number");
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
142
    field->set_type(message::Table::Field::INTEGER);
323 by Brian Aker
Updated proto file for table (not FRM work).
143
  }
144
  /* Write out a ENUM */
145
  {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
146
    field= table->add_field();
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
147
    field->set_type(message::Table::Field::ENUM);
323 by Brian Aker
Updated proto file for table (not FRM work).
148
    field->set_name("colors");
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
149
1396 by Brian Aker
Simple rename.
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");
323 by Brian Aker
Updated proto file for table (not FRM work).
154
  }
155
  /* Write out a BLOB */
156
  {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
157
    field= table->add_field();
323 by Brian Aker
Updated proto file for table (not FRM work).
158
    field->set_name("some_btye_string");
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
159
    field->set_type(message::Table::Field::BLOB);
323 by Brian Aker
Updated proto file for table (not FRM work).
160
  }
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
161
323 by Brian Aker
Updated proto file for table (not FRM work).
162
  /* Write out a DECIMAL */
163
  {
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
164
    field= table->add_field();
323 by Brian Aker
Updated proto file for table (not FRM work).
165
    field->set_name("important_number");
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
166
    field->set_type(message::Table::Field::DECIMAL);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
167
168
    field_constraints= field->mutable_constraints();
169
    field_constraints->set_is_nullable(true);
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
170
352.1.2 by Jay Pipes
Working reader and writer for table.proto definitions now
171
    numeric_field_options= field->mutable_numeric_options();
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
172
    numeric_field_options->set_precision(8);
173
    numeric_field_options->set_scale(3);
174
  }
175
176
  {
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
177
    uint32_t fields_in_index[1]= {6};
178
    uint32_t compare_lengths_in_index[1]= {0};
179
    bool is_unique= true;
180
    bool is_primary= false;
181
    /* Add a single-column index on important_number field */
182
    new_index_to_table(table, "idx_important_decimal", 1, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
183
  }
184
185
  {
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
186
    /* Add a double-column index on first two varchar fields */
187
    uint32_t fields_in_index[2]= {0,1};
188
    uint32_t compare_lengths_in_index[2]= {20,35};
189
    bool is_unique= true;
190
    bool is_primary= true;
191
    new_index_to_table(table, "idx_varchar1_2", 2, fields_in_index, compare_lengths_in_index, is_primary, is_unique);
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
192
  }
193
194
  /* Do engine-specific stuff */
1502.1.31 by Brian Aker
Merge engine options for schema/table.
195
  message::Engine *engine= table->mutable_engine();
352.1.1 by Jay Pipes
New table definition and updated reader and writer test programs
196
  fill_engine(engine);
323 by Brian Aker
Updated proto file for table (not FRM work).
197
198
}
199
1273.2.22 by Stewart Smith
add a table to table_write missing a StorageEngine message (a required field). Should generate error if ever accessed/loaded.
200
static void fill_table1(message::Table *table)
201
{
202
  message::Table::Field *field;
203
  message::Table::TableOptions *tableopts;
204
205
  table->set_name("t1");
206
  table->set_type(message::Table::INTERNAL);
207
208
  tableopts= table->mutable_options();
209
  tableopts->set_comment("Table without a StorageEngine message");
210
211
  {
212
    field= table->add_field();
213
    field->set_name("number");
214
    field->set_type(message::Table::Field::INTEGER);
215
  }
216
217
}
218
1273.2.20 by Stewart Smith
add some basic command line parsing to table_writer. In prep for writing several table messages of varying correctness
219
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
220
int main(int argc, char* argv[])
323 by Brian Aker
Updated proto file for table (not FRM work).
221
{
1273.2.20 by Stewart Smith
add some basic command line parsing to table_writer. In prep for writing several table messages of varying correctness
222
  int table_number= 0;
223
323 by Brian Aker
Updated proto file for table (not FRM work).
224
  GOOGLE_PROTOBUF_VERIFY_VERSION;
225
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
226
  po::options_description desc("Allowed options");
227
  desc.add_options()
228
    ("help", "produce help message")
229
    ("table-number,t", po::value<int>(&table_number)->default_value(0), "Table Number");
230
231
  po::variables_map vm;
232
  po::positional_options_description p;
233
  p.add("table-name", 1);
234
235
  po::store(po::command_line_parser(argc, argv).options(desc).
236
            positional(p).run(), vm);
237
238
  if (not vm.count("table-name"))
584.2.2 by Stewart Smith
Brian's table(_reader|_writer|.proto) fixes
239
  {
1273.2.20 by Stewart Smith
add some basic command line parsing to table_writer. In prep for writing several table messages of varying correctness
240
    fprintf(stderr, "Expected Table name argument\n\n");
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
241
    cerr << desc << endl;
1273.2.20 by Stewart Smith
add some basic command line parsing to table_writer. In prep for writing several table messages of varying correctness
242
    exit(EXIT_FAILURE);
323 by Brian Aker
Updated proto file for table (not FRM work).
243
  }
244
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
245
  message::Table table;
323 by Brian Aker
Updated proto file for table (not FRM work).
246
1273.2.21 by Stewart Smith
add checking of valid table number to table_writer
247
  switch (table_number)
248
  {
249
  case 0:
250
    fill_table(&table, "example_table");
251
    break;
1273.2.22 by Stewart Smith
add a table to table_write missing a StorageEngine message (a required field). Should generate error if ever accessed/loaded.
252
  case 1:
253
    fill_table1(&table);
254
    break;
1273.2.21 by Stewart Smith
add checking of valid table number to table_writer
255
  default:
256
    fprintf(stderr, "Invalid table number.\n\n");
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
257
    cerr << desc << endl;
1273.2.21 by Stewart Smith
add checking of valid table number to table_writer
258
    exit(EXIT_FAILURE);
259
  }
323 by Brian Aker
Updated proto file for table (not FRM work).
260
1702.2.1 by Monty Taylor
Removed gnulib and the last usage of normal getopt.
261
  fstream output(vm["table-name"].as<string>().c_str(),
262
                 ios::out | ios::trunc | ios::binary);
584.2.3 by Stewart Smith
remove trailing whitespace in serialize/table
263
  if (!table.SerializeToOstream(&output))
323 by Brian Aker
Updated proto file for table (not FRM work).
264
  {
265
    cerr << "Failed to write schema." << endl;
266
    return -1;
267
  }
268
269
  return 0;
270
}