~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.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
 
/* -*- 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; 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
 
#include <sys/types.h>
24
 
#include <sys/stat.h>
25
 
#include <fcntl.h>
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <errno.h>
29
 
#include <unistd.h>
30
 
 
31
1
#include <iostream>
 
2
#include <fstream>
32
3
#include <string>
33
 
#include <drizzled/message/statement_transform.h>
34
 
#include <google/protobuf/io/zero_copy_stream.h>
35
 
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
 
 
 
4
#include "table.pb.h"
37
5
using namespace std;
38
 
using namespace drizzled;
39
 
using namespace google;
40
6
 
41
 
/*
 
7
/* 
42
8
  Written from Google proto example
43
9
*/
44
10
 
45
 
int main(int argc, char* argv[])
 
11
void print_field(const drizzle::Table::Field *field) 
 
12
{
 
13
  using namespace drizzle;
 
14
  cout << "\t`" << field->name() << "`";
 
15
  switch (field->type())
 
16
  {
 
17
    case Table::Field::DOUBLE:
 
18
    cout << " DOUBLE ";
 
19
    break;
 
20
  case Table::Field::VARCHAR:
 
21
    cout << " VARCHAR(" << field->string_options().length() << ")";
 
22
    break;
 
23
  case Table::Field::TEXT:
 
24
    cout << " TEXT ";
 
25
    break;
 
26
  case Table::Field::BLOB:
 
27
    cout << " BLOB ";
 
28
    break;
 
29
  case Table::Field::ENUM:
 
30
    {
 
31
      int x;
 
32
 
 
33
      cout << " ENUM(";
 
34
      for (x= 0; x < field->set_options().value_size() ; x++)
 
35
      {
 
36
        const string type= field->set_options().value(x);
 
37
 
 
38
        if (x != 0)
 
39
          cout << ",";
 
40
        cout << "'" << type << "'";
 
41
      }
 
42
      cout << ") ";
 
43
      break;
 
44
    }
 
45
  case Table::Field::SET:
 
46
    cout << " SET ";
 
47
    break;
 
48
  case Table::Field::TINYINT:
 
49
    cout << " TINYINT ";
 
50
    break;
 
51
  case Table::Field::SMALLINT:
 
52
    cout << " SMALLINT ";
 
53
    break;
 
54
  case Table::Field::INTEGER:
 
55
    cout << " INTEGER ";
 
56
    break;
 
57
  case Table::Field::BIGINT:
 
58
    cout << " BIGINT ";
 
59
    break;
 
60
  case Table::Field::DECIMAL:
 
61
    cout << " DECIMAL(" << field->numeric_options().precision() << "," << field->numeric_options().scale() << ") ";
 
62
    break;
 
63
  case Table::Field::VARBINARY:
 
64
    cout << " VARBINARY(" << field->string_options().length() << ") ";
 
65
    break;
 
66
  case Table::Field::DATE:
 
67
    cout << " DATE ";
 
68
    break;
 
69
  case Table::Field::TIME:
 
70
    cout << " TIME ";
 
71
    break;
 
72
  case Table::Field::TIMESTAMP:
 
73
    cout << " TIMESTAMP ";
 
74
    break;
 
75
  case Table::Field::DATETIME:
 
76
    cout << " DATETIME ";
 
77
    break;
 
78
  }
 
79
 
 
80
  if (field->type() == Table::Field::INTEGER
 
81
      || field->type() == Table::Field::BIGINT
 
82
      || field->type() == Table::Field::SMALLINT
 
83
      || field->type() == Table::Field::TINYINT) {
 
84
    if (field->has_constraints()
 
85
        && field->constraints().has_is_unsigned())
 
86
      if (field->constraints().is_unsigned())
 
87
        cout << " UNSIGNED";
 
88
 
 
89
    if (field->has_numeric_options() &&
 
90
      field->numeric_options().is_autoincrement())
 
91
      cout << " AUTOINCREMENT ";
 
92
  }
 
93
 
 
94
  if (! field->has_constraints()
 
95
      && field->constraints().is_nullable())
 
96
    cout << " NOT NULL ";
 
97
 
 
98
  if (field->type() == Table::Field::TEXT
 
99
      || field->type() == Table::Field::VARCHAR) {
 
100
    if (field->string_options().has_charset())
 
101
      cout << " CHARACTER SET " << field->string_options().charset();
 
102
 
 
103
    if (field->string_options().has_collation())
 
104
      cout << " COLLATE " << field->string_options().collation();
 
105
  }
 
106
 
 
107
  if (field->options().has_default_value())
 
108
    cout << " DEFAULT `" << field->options().default_value() << "` " ;
 
109
 
 
110
  if (field->type() == Table::Field::TIMESTAMP)
 
111
    if (field->timestamp_options().has_auto_updates()
 
112
      && field->timestamp_options().auto_updates())
 
113
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
114
 
 
115
  if (field->has_comment())
 
116
    cout << " COMMENT `" << field->comment() << "` ";
 
117
}
 
118
 
 
119
void print_engine(const drizzle::Table::StorageEngine *engine) {
 
120
  using namespace drizzle;
 
121
  uint32_t x;
 
122
 
 
123
  cout << " ENGINE = " << engine->name()  << endl;
 
124
 
 
125
  for (x= 0; x < engine->option_size(); ++x) {
 
126
    const Table::StorageEngine::EngineOption option= engine->option(x);
 
127
    cout << "\t" << option.name() << " = " << option.value() << endl;
 
128
  }
 
129
}
 
130
 
 
131
void print_index(const drizzle::Table::Index *index) {
 
132
  using namespace drizzle;
 
133
  uint32_t x;
 
134
 
 
135
  if (index->is_primary())
 
136
    cout << " PRIMARY"; 
 
137
  else if (index->is_unique())
 
138
    cout << " UNIQUE"; 
 
139
  cout << " KEY `" << index->name() << "` (";
 
140
  {
 
141
    int x;
 
142
 
 
143
    for (x= 0; x < index->index_part_size() ; x++)
 
144
    {
 
145
      const Table::Index::IndexPart part= index->index_part(x);
 
146
 
 
147
      if (x != 0)
 
148
        cout << ",";
 
149
      cout << "`" << part.field().name() << "`";
 
150
      if (part.has_compare_length())
 
151
        cout << "(" << part.compare_length() << ")";
 
152
    }
 
153
    cout << ")";
 
154
  }
 
155
  cout << "\t";
 
156
}
 
157
 
 
158
void print_table(const drizzle::Table *table) 
 
159
{
 
160
  using namespace drizzle;
 
161
  uint32_t x;
 
162
 
 
163
  cout << "CREATE ";
 
164
 
 
165
  if (table->type() == Table::TEMPORARY)
 
166
    cout << "TEMPORARY ";
 
167
 
 
168
  cout << "TABLE `" << table->name() << "` (" << endl;
 
169
  
 
170
  for (x= 0; x < table->field_size() ; x++)
 
171
  {
 
172
    const Table::Field field = table->field(x);
 
173
 
 
174
    if (x != 0)
 
175
      cout << "," << endl;
 
176
 
 
177
    print_field(&field);
 
178
  }
 
179
 
 
180
  for (x= 0; x < table->index_size() ; x++)
 
181
  {
 
182
    const Table::Index index= table->index(x);
 
183
 
 
184
    if (x != 0)
 
185
      cout << "," << endl;;
 
186
 
 
187
    print_index(&index);
 
188
 
 
189
  }
 
190
  cout << endl;
 
191
 
 
192
  cout << ") " << endl;
 
193
  
 
194
  print_engine(&table->engine());
 
195
 
 
196
  /*
 
197
  if (table->has_options())
 
198
    print_table_options(&table->options());
 
199
  if (table->has_stats())
 
200
    print_table_stats(&table->stats());
 
201
  */
 
202
  if (table->has_comment())
 
203
    cout << " COMMENT = `" << table->comment() << "` " << endl;
 
204
}
 
205
 
 
206
void print_table_stats(const drizzle::Table::TableStats *stats) {
 
207
  
 
208
}
 
209
 
 
210
void print_table_options(const drizzle::Table::TableOptions *options) {
 
211
  if (options->has_collation())
 
212
    cout << " COLLATE = `" << options->collation() << "` " << endl;
 
213
  if (options->has_charset())
 
214
    cout << " CHARACTER SET = `" << options->charset() << "` " << endl;
 
215
}
 
216
 
 
217
int main(int argc, char* argv[]) 
46
218
{
47
219
  GOOGLE_PROTOBUF_VERIFY_VERSION;
48
220
 
51
223
    return -1;
52
224
  }
53
225
 
54
 
  message::Table table;
 
226
  drizzle::Table table;
55
227
 
56
228
  {
57
 
    int fd= open(argv[1], O_RDONLY);
58
 
 
59
 
    if(fd==-1)
60
 
    {
61
 
      perror("Failed to open table definition file");
62
 
      return -1;
63
 
    }
64
 
 
65
 
    protobuf::io::ZeroCopyInputStream* input=
66
 
      new protobuf::io::FileInputStream(fd);
67
 
 
68
 
    if (!table.ParseFromZeroCopyStream(input))
 
229
    // Read the existing address book.
 
230
    fstream input(argv[1], ios::in | ios::binary);
 
231
    if (!table.ParseFromIstream(&input)) 
69
232
    {
70
233
      cerr << "Failed to parse table." << endl;
71
 
      close(fd);
72
234
      return -1;
73
235
    }
74
 
 
75
 
    close(fd);
76
236
  }
77
237
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
238
  print_table(&table);
82
239
 
83
240
  return 0;
84
241
}