~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

  • Committer: Brian Aker
  • Date: 2008-07-15 06:45:16 UTC
  • Revision ID: brian@tangent.org-20080715064516-fnbq7kowh7w57bxj
Merge Monty's code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <iostream>
2
 
#include <fstream>
3
 
#include <string>
4
 
#include "table.pb.h"
5
 
using namespace std;
6
 
 
7
 
/* 
8
 
  Written from Google proto example
9
 
*/
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::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[]) 
218
 
{
219
 
  GOOGLE_PROTOBUF_VERIFY_VERSION;
220
 
 
221
 
  if (argc != 2) {
222
 
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
223
 
    return -1;
224
 
  }
225
 
 
226
 
  drizzle::Table table;
227
 
 
228
 
  {
229
 
    // Read the existing address book.
230
 
    fstream input(argv[1], ios::in | ios::binary);
231
 
    if (!table.ParseFromIstream(&input)) 
232
 
    {
233
 
      cerr << "Failed to parse table." << endl;
234
 
      return -1;
235
 
    }
236
 
  }
237
 
 
238
 
  print_table(&table);
239
 
 
240
 
  return 0;
241
 
}