~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_reader.cc

  • Committer: Brian Aker
  • Date: 2008-08-13 23:12:06 UTC
  • Revision ID: brian@tangent.org-20080813231206-4yx6enf40usz7bhc
Updated proto file for table (not FRM work).

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 printType(const drizzle::Table::Field *field) 
 
12
{
 
13
  switch (field->type())
 
14
  {
 
15
  case drizzle::Table::DOUBLE:
 
16
    cout << " DOUBLE ";
 
17
    break;
 
18
  case drizzle::Table::VARCHAR:
 
19
    cout << " VARCHAR(" << field->length() << ")";
 
20
    break;
 
21
  case drizzle::Table::TEXT:
 
22
    cout << " TEXT ";
 
23
    break;
 
24
  case drizzle::Table::BLOB:
 
25
    cout << " BLOB ";
 
26
    break;
 
27
  case drizzle::Table::ENUM:
 
28
    {
 
29
      int x;
 
30
 
 
31
      cout << " ENUM(";
 
32
      for (x= 0; x < field->values_size() ; x++)
 
33
      {
 
34
        const string type= field->values(x);
 
35
 
 
36
        if (x != 0)
 
37
          cout << ",";
 
38
        cout << "'" << type << "'";
 
39
      }
 
40
      cout << ") ";
 
41
      break;
 
42
    }
 
43
  case drizzle::Table::SET:
 
44
    cout << " SET ";
 
45
    break;
 
46
  case drizzle::Table::TINYINT:
 
47
    cout << " TINYINNT ";
 
48
    break;
 
49
  case drizzle::Table::SMALLINT:
 
50
    cout << " SMALLINT ";
 
51
    break;
 
52
  case drizzle::Table::INTEGER:
 
53
    cout << " INTEGER ";
 
54
    break;
 
55
  case drizzle::Table::BIGINT:
 
56
    cout << " BIGINT ";
 
57
    break;
 
58
  case drizzle::Table::DECIMAL:
 
59
    cout << " DECIMAL(" << field->length() << "," << field->scale() << ") ";
 
60
    break;
 
61
  case drizzle::Table::VARBINARY:
 
62
    cout << " VARBINARY(" << field->length() << ") ";
 
63
    break;
 
64
  case drizzle::Table::DATE:
 
65
    cout << " DATE ";
 
66
    break;
 
67
  case drizzle::Table::TIME:
 
68
    cout << " TIME ";
 
69
    break;
 
70
  case drizzle::Table::TIMESTAMP:
 
71
    cout << " TIMESTAMP ";
 
72
    break;
 
73
  case drizzle::Table::DATETIME:
 
74
    cout << " DATETIME ";
 
75
    break;
 
76
  }
 
77
 
 
78
  if (field->has_characterset())
 
79
    cout << " CHARACTER SET " << field->characterset();
 
80
 
 
81
  if (field->has_collation())
 
82
    cout << " COLLATE " << field->collation();
 
83
 
 
84
  if (field->is_notnull())
 
85
    cout << " NOT NULL ";
 
86
 
 
87
  if (field->has_default_value())
 
88
    cout << " DEFAULT `" << field->default_value() << "` " ;
 
89
 
 
90
  if (field->on_update())
 
91
    cout << " ON UPDATE CURRENT_TIMESTAMP";
 
92
 
 
93
  if (field->autoincrement())
 
94
    cout << " AUTOINCREMENT ";
 
95
 
 
96
  if (field->has_comment())
 
97
    cout << " COMMENT `" << field->comment() << "` ";
 
98
}
 
99
 
 
100
void printTable(const drizzle::Table *table) 
 
101
{
 
102
  uint32_t x;
 
103
 
 
104
  cout << "CREATE TABLE";
 
105
 
 
106
  if (table->temp())
 
107
    cout << " TEMPORARY";
 
108
 
 
109
  cout << " `" << table->name() << "` (" << endl;
 
110
  
 
111
  for (x= 0; x < table->field_size() ; x++)
 
112
  {
 
113
    const drizzle::Table::Field field = table->field(x);
 
114
 
 
115
    if (x != 0)
 
116
      cout << "," << endl;;
 
117
 
 
118
    cout << "\t`" << field.name() << "`";
 
119
    printType(&field);
 
120
  }
 
121
 
 
122
  for (x= 0; x < table->index_size() ; x++)
 
123
  {
 
124
    const drizzle::Table::Index index = table->index(x);
 
125
 
 
126
    cout << "," << endl;;
 
127
 
 
128
    cout << "\t";
 
129
 
 
130
    if (index.primary())
 
131
      cout << " PRIMARY KEY (`" << index.name() << "`)";
 
132
    else
 
133
    {
 
134
      int x;
 
135
 
 
136
      cout << " UNIQUE KEY `" << index.name() << "` (";
 
137
      for (x= 0; x < index.values_size() ; x++)
 
138
      {
 
139
        const drizzle::Table::KeyPart key= index.values(x);
 
140
 
 
141
        if (x != 0)
 
142
          cout << ",";
 
143
        cout << "`" << key.name() << "`";
 
144
      }
 
145
      cout << ")";
 
146
    }
 
147
  }
 
148
  cout << endl;
 
149
 
 
150
  cout << ") " << endl;
 
151
  if (table->has_collation())
 
152
    cout << " COLLATE = `" << table->collation() << "` " << endl;;
 
153
  if (table->has_characterset())
 
154
    cout << " CHARACTER SET = `" << table->characterset() << "` " << endl;;
 
155
  if (table->has_comment())
 
156
    cout << " COMMENT = `" << table->comment() << "` " << endl;;
 
157
  if (table->has_engine())
 
158
  if (table->has_data_directory())
 
159
    cout << " DATA DIRECTORY = `" << table->data_directory() << "` " << endl;;
 
160
  if (table->has_index_directory())
 
161
    cout << " INDEX DIRECTORY = `" << table->index_directory() << "`" << endl;
 
162
  cout << " ENGINE = " << table->engine() << ";"  << endl;
 
163
}
 
164
 
 
165
int main(int argc, char* argv[]) 
 
166
{
 
167
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
168
 
 
169
  if (argc != 2) {
 
170
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
 
171
    return -1;
 
172
  }
 
173
 
 
174
  drizzle::Table table;
 
175
 
 
176
  {
 
177
    // Read the existing address book.
 
178
    fstream input(argv[1], ios::in | ios::binary);
 
179
    if (!table.ParseFromIstream(&input)) 
 
180
    {
 
181
      cerr << "Failed to parse table." << endl;
 
182
      return -1;
 
183
    }
 
184
  }
 
185
 
 
186
  printTable(&table);
 
187
 
 
188
  return 0;
 
189
}