~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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, Inc.
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; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
 
23
1
#include <sys/types.h>
24
2
#include <sys/stat.h>
25
3
#include <fcntl.h>
30
8
 
31
9
#include <iostream>
32
10
#include <string>
33
 
#include <drizzled/message/statement_transform.h>
 
11
#include <drizzled/serialize/table.pb.h>
34
12
#include <google/protobuf/io/zero_copy_stream.h>
35
13
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
14
 
37
15
using namespace std;
38
 
using namespace drizzled;
39
 
using namespace google;
 
16
using namespace drizzle;
 
17
using namespace google::protobuf::io;
40
18
 
41
19
/*
42
20
  Written from Google proto example
43
21
*/
44
22
 
 
23
void print_field(const ::drizzle::Table::Field &field)
 
24
{
 
25
  cout << "\t`" << field.name() << "`";
 
26
  switch (field.type())
 
27
  {
 
28
    case Table::Field::DOUBLE:
 
29
    cout << " DOUBLE ";
 
30
    break;
 
31
  case Table::Field::VARCHAR:
 
32
    cout << " VARCHAR(" << field.string_options().length() << ")";
 
33
    break;
 
34
  case Table::Field::TEXT:
 
35
    cout << " TEXT ";
 
36
    break;
 
37
  case Table::Field::BLOB:
 
38
    cout << " BLOB ";
 
39
    break;
 
40
  case Table::Field::ENUM:
 
41
    {
 
42
      int x;
 
43
 
 
44
      cout << " ENUM(";
 
45
      for (x= 0; x < field.set_options().field_value_size() ; x++)
 
46
      {
 
47
        const string type= field.set_options().field_value(x);
 
48
 
 
49
        if (x != 0)
 
50
          cout << ",";
 
51
        cout << "'" << type << "'";
 
52
      }
 
53
      cout << ") ";
 
54
      break;
 
55
    }
 
56
  case Table::Field::TINYINT:
 
57
    cout << " TINYINT ";
 
58
    break;
 
59
  case Table::Field::INTEGER:
 
60
    cout << " INT" ;
 
61
    break;
 
62
  case Table::Field::BIGINT:
 
63
    cout << " BIGINT ";
 
64
    break;
 
65
  case Table::Field::DECIMAL:
 
66
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
 
67
    break;
 
68
  case Table::Field::DATE:
 
69
    cout << " DATE ";
 
70
    break;
 
71
  case Table::Field::TIME:
 
72
    cout << " TIME ";
 
73
    break;
 
74
  case Table::Field::TIMESTAMP:
 
75
    cout << " TIMESTAMP ";
 
76
    break;
 
77
  case Table::Field::DATETIME:
 
78
    cout << " DATETIME ";
 
79
    break;
 
80
  case Table::Field::VIRTUAL:
 
81
    cout << " VIRTUAL"; // FIXME
 
82
    break;
 
83
  }
 
84
 
 
85
  if (field.type() == Table::Field::INTEGER
 
86
      || field.type() == Table::Field::BIGINT
 
87
      || field.type() == Table::Field::TINYINT)
 
88
  {
 
89
    if (field.has_constraints()
 
90
        && field.constraints().has_is_unsigned())
 
91
      if (field.constraints().is_unsigned())
 
92
        cout << " UNSIGNED";
 
93
 
 
94
    if (field.has_numeric_options() &&
 
95
      field.numeric_options().is_autoincrement())
 
96
      cout << " AUTOINCREMENT ";
 
97
  }
 
98
 
 
99
  if (!( field.has_constraints()
 
100
         && field.constraints().is_nullable()))
 
101
    cout << " NOT NULL ";
 
102
 
 
103
  if (field.type() == Table::Field::TEXT
 
104
      || field.type() == Table::Field::VARCHAR)
 
105
  {
 
106
    if (field.string_options().has_collation())
 
107
      cout << " COLLATE " << field.string_options().collation();
 
108
  }
 
109
 
 
110
  if (field.options().has_default_value())
 
111
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
 
112
 
 
113
  if (field.type() == Table::Field::TIMESTAMP)
 
114
    if (field.timestamp_options().has_auto_updates()
 
115
      && field.timestamp_options().auto_updates())
 
116
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
117
 
 
118
  if (field.has_comment())
 
119
    cout << " COMMENT `" << field.comment() << "` ";
 
120
}
 
121
 
 
122
void print_engine(const ::drizzle::Table::StorageEngine &engine)
 
123
{
 
124
  int32_t x;
 
125
 
 
126
  cout << " ENGINE = " << engine.name()  << endl;
 
127
 
 
128
  for (x= 0; x < engine.option_size(); ++x) {
 
129
    const Table::StorageEngine::EngineOption option= engine.option(x);
 
130
    cout << "\t" << option.option_name() << " = "
 
131
         << option.option_value() << endl;
 
132
  }
 
133
}
 
134
 
 
135
void print_index(const ::drizzle::Table::Index &index)
 
136
{
 
137
 
 
138
  if (index.is_primary())
 
139
    cout << " PRIMARY";
 
140
  else if (index.is_unique())
 
141
    cout << " UNIQUE";
 
142
  cout << " KEY `" << index.name() << "` (";
 
143
  {
 
144
    int32_t x;
 
145
 
 
146
    for (x= 0; x < index.index_part_size() ; x++)
 
147
    {
 
148
      const Table::Index::IndexPart part= index.index_part(x);
 
149
 
 
150
      if (x != 0)
 
151
        cout << ",";
 
152
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
 
153
      if (part.has_compare_length())
 
154
        cout << "(" << part.compare_length() << ")";
 
155
    }
 
156
    cout << ")";
 
157
  }
 
158
  cout << "\t";
 
159
}
 
160
 
 
161
void print_table_stats(const ::drizzle::Table::TableStats&) 
 
162
{
 
163
 
 
164
}
 
165
 
 
166
void print_table_options(const ::drizzle::Table::TableOptions &options)
 
167
{
 
168
  if (options.has_comment())
 
169
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
170
 
 
171
  if (options.has_collation())
 
172
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
173
 
 
174
  if (options.has_auto_increment())
 
175
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
176
 
 
177
  if (options.has_collation_id())
 
178
    cout << "-- collation_id = " << options.collation_id() << endl;
 
179
  
 
180
  if (options.has_connect_string())
 
181
    cout << " CONNECT_STRING = '" << options.connect_string() << "'"<<endl;
 
182
 
 
183
  if (options.has_row_type())
 
184
    cout << " ROW_TYPE = " << options.row_type() << endl;
 
185
 
 
186
/*    optional string data_file_name = 5;
 
187
    optional string index_file_name = 6;
 
188
    optional uint64 max_rows = 7;
 
189
    optional uint64 min_rows = 8;
 
190
    optional uint64 auto_increment_value = 9;
 
191
    optional uint32 avg_row_length = 11;
 
192
    optional uint32 key_block_size = 12;
 
193
    optional uint32 block_size = 13;
 
194
    optional string comment = 14;
 
195
    optional bool pack_keys = 15;
 
196
    optional bool checksum = 16;
 
197
    optional bool page_checksum = 17;
 
198
    optional bool delay_key_write = 18;
 
199
*/
 
200
}
 
201
 
 
202
 
 
203
void print_table(const ::drizzle::Table &table)
 
204
{
 
205
  int32_t x;
 
206
 
 
207
  cout << "CREATE ";
 
208
 
 
209
  if (table.type() == Table::TEMPORARY)
 
210
    cout << "TEMPORARY ";
 
211
 
 
212
  cout << "TABLE `" << table.name() << "` (" << endl;
 
213
 
 
214
  for (x= 0; x < table.field_size() ; x++)
 
215
  {
 
216
    const Table::Field field = table.field(x);
 
217
 
 
218
    if (x != 0)
 
219
      cout << "," << endl;
 
220
 
 
221
    print_field(field);
 
222
  }
 
223
 
 
224
  for (x= 0; x < table.indexes_size() ; x++)
 
225
  {
 
226
    const Table::Index index= table.indexes(x);
 
227
 
 
228
    if (x != 0)
 
229
      cout << "," << endl;;
 
230
 
 
231
    print_index(index);
 
232
 
 
233
  }
 
234
  cout << endl;
 
235
 
 
236
  cout << ") " << endl;
 
237
 
 
238
  print_engine(table.engine());
 
239
 
 
240
  if (table.has_options())
 
241
    print_table_options(table.options());
 
242
  /*
 
243
  if (table->has_stats())
 
244
    print_table_stats(&table->stats());
 
245
  */
 
246
}
 
247
 
45
248
int main(int argc, char* argv[])
46
249
{
47
250
  GOOGLE_PROTOBUF_VERIFY_VERSION;
51
254
    return -1;
52
255
  }
53
256
 
54
 
  message::Table table;
 
257
  Table table;
55
258
 
56
259
  {
57
260
    int fd= open(argv[1], O_RDONLY);
62
265
      return -1;
63
266
    }
64
267
 
65
 
    protobuf::io::ZeroCopyInputStream* input=
66
 
      new protobuf::io::FileInputStream(fd);
 
268
    ZeroCopyInputStream* input = new FileInputStream(fd);
67
269
 
68
270
    if (!table.ParseFromZeroCopyStream(input))
69
271
    {
75
277
    close(fd);
76
278
  }
77
279
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
280
  print_table(table);
82
281
 
83
282
  return 0;
84
283
}