~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

  • Committer: Brian Aker
  • Date: 2008-12-15 19:32:58 UTC
  • mfrom: (677.1.2 devel)
  • Revision ID: brian@tangent.org-20081215193258-fsvc1sh9h7a9sb1t
Merge from Monty

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
 
#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
 
 
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::TINYINT:
 
46
    cout << " TINYINT ";
 
47
    break;
 
48
  case Table::Field::INTEGER:
 
49
    cout << " INT" ;
 
50
    break;
 
51
  case Table::Field::BIGINT:
 
52
    cout << " BIGINT ";
 
53
    break;
 
54
  case Table::Field::DECIMAL:
 
55
    cout << " DECIMAL(" << field->numeric_options().precision() << "," << field->numeric_options().scale() << ") ";
 
56
    break;
 
57
  case Table::Field::DATE:
 
58
    cout << " DATE ";
 
59
    break;
 
60
  case Table::Field::TIME:
 
61
    cout << " TIME ";
 
62
    break;
 
63
  case Table::Field::TIMESTAMP:
 
64
    cout << " TIMESTAMP ";
 
65
    break;
 
66
  case Table::Field::DATETIME:
 
67
    cout << " DATETIME ";
 
68
    break;
 
69
  }
 
70
 
 
71
  if (field->type() == Table::Field::INTEGER
 
72
      || field->type() == Table::Field::BIGINT
 
73
      || field->type() == Table::Field::TINYINT)
 
74
  {
 
75
    if (field->has_constraints()
 
76
        && field->constraints().has_is_unsigned())
 
77
      if (field->constraints().is_unsigned())
 
78
        cout << " UNSIGNED";
 
79
 
 
80
    if (field->has_numeric_options() &&
 
81
      field->numeric_options().is_autoincrement())
 
82
      cout << " AUTOINCREMENT ";
 
83
  }
 
84
 
 
85
  if (! field->has_constraints()
 
86
      && field->constraints().is_nullable())
 
87
    cout << " NOT NULL ";
 
88
 
 
89
  if (field->type() == Table::Field::TEXT
 
90
      || field->type() == Table::Field::VARCHAR)
 
91
  {
 
92
    if (field->string_options().has_collation())
 
93
      cout << " COLLATE " << field->string_options().collation();
 
94
  }
 
95
 
 
96
  if (field->options().has_default_value())
 
97
    cout << " DEFAULT `" << field->options().default_value() << "` " ;
 
98
 
 
99
  if (field->type() == Table::Field::TIMESTAMP)
 
100
    if (field->timestamp_options().has_auto_updates()
 
101
      && field->timestamp_options().auto_updates())
 
102
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
103
 
 
104
  if (field->has_comment())
 
105
    cout << " COMMENT `" << field->comment() << "` ";
 
106
}
 
107
 
 
108
void print_engine(const drizzle::Table::StorageEngine *engine)
 
109
{
 
110
  using namespace drizzle;
 
111
  uint32_t x;
 
112
 
 
113
  cout << " ENGINE = " << engine->name()  << endl;
 
114
 
 
115
  for (x= 0; x < engine->option_size(); ++x) {
 
116
    const Table::StorageEngine::EngineOption option= engine->option(x);
 
117
    cout << "\t" << option.name() << " = " << option.value() << endl;
 
118
  }
 
119
}
 
120
 
 
121
void print_index(const drizzle::Table::Index *index)
 
122
{
 
123
  using namespace drizzle;
 
124
  uint32_t x;
 
125
 
 
126
  if (index->is_primary())
 
127
    cout << " PRIMARY";
 
128
  else if (index->is_unique())
 
129
    cout << " UNIQUE";
 
130
  cout << " KEY `" << index->name() << "` (";
 
131
  {
 
132
    int x;
 
133
 
 
134
    for (x= 0; x < index->index_part_size() ; x++)
 
135
    {
 
136
      const Table::Index::IndexPart part= index->index_part(x);
 
137
 
 
138
      if (x != 0)
 
139
        cout << ",";
 
140
      cout << "`" << part.field().name() << "`";
 
141
      if (part.has_compare_length())
 
142
        cout << "(" << part.compare_length() << ")";
 
143
    }
 
144
    cout << ")";
 
145
  }
 
146
  cout << "\t";
 
147
}
 
148
 
 
149
void print_table_stats(const drizzle::Table::TableStats *stats)
 
150
{
 
151
 
 
152
}
 
153
 
 
154
void print_table_options(const drizzle::Table::TableOptions *options)
 
155
{
 
156
  if (options->has_comment())
 
157
    cout << " COMMENT = '" << options->comment() << "' " << endl;
 
158
 
 
159
  if (options->has_collation())
 
160
    cout << " COLLATE = '" << options->collation() << "' " << endl;
 
161
}
 
162
 
 
163
 
 
164
void print_table(const drizzle::Table *table)
 
165
{
 
166
  using namespace drizzle;
 
167
  uint32_t x;
 
168
 
 
169
  cout << "CREATE ";
 
170
 
 
171
  if (table->type() == Table::TEMPORARY)
 
172
    cout << "TEMPORARY ";
 
173
 
 
174
  cout << "TABLE `" << table->name() << "` (" << endl;
 
175
 
 
176
  for (x= 0; x < table->field_size() ; x++)
 
177
  {
 
178
    const Table::Field field = table->field(x);
 
179
 
 
180
    if (x != 0)
 
181
      cout << "," << endl;
 
182
 
 
183
    print_field(&field);
 
184
  }
 
185
 
 
186
  for (x= 0; x < table->index_size() ; x++)
 
187
  {
 
188
    const Table::Index index= table->index(x);
 
189
 
 
190
    if (x != 0)
 
191
      cout << "," << endl;;
 
192
 
 
193
    print_index(&index);
 
194
 
 
195
  }
 
196
  cout << endl;
 
197
 
 
198
  cout << ") " << endl;
 
199
 
 
200
  print_engine(&table->engine());
 
201
 
 
202
  if (table->has_options())
 
203
    print_table_options(&table->options());
 
204
  /*
 
205
  if (table->has_stats())
 
206
    print_table_stats(&table->stats());
 
207
  */
 
208
}
 
209
 
45
210
int main(int argc, char* argv[])
46
211
{
47
212
  GOOGLE_PROTOBUF_VERIFY_VERSION;
51
216
    return -1;
52
217
  }
53
218
 
54
 
  message::Table table;
 
219
  drizzle::Table table;
55
220
 
56
221
  {
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))
 
222
    // Read the existing address book.
 
223
    fstream input(argv[1], ios::in | ios::binary);
 
224
    if (!table.ParseFromIstream(&input))
69
225
    {
70
226
      cerr << "Failed to parse table." << endl;
71
 
      close(fd);
72
227
      return -1;
73
228
    }
74
 
 
75
 
    close(fd);
76
229
  }
77
230
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
231
  print_table(&table);
82
232
 
83
233
  return 0;
84
234
}