~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Stewart Smith
  • Date: 2009-10-19 04:17:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1192.
  • Revision ID: stewart@flamingspork.com-20091019041738-bsjyzmittghcomqj
remove some unused PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP related things as errorcheck mutexes are never used (and haven't been since at least MySQL 4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
22
 
 
 
21
#include <drizzled/global.h>
23
22
#include <sys/types.h>
24
23
#include <sys/stat.h>
25
24
#include <fcntl.h>
30
29
 
31
30
#include <iostream>
32
31
#include <string>
33
 
#include <drizzled/message/statement_transform.h>
 
32
#include <drizzled/message/table.pb.h>
34
33
#include <google/protobuf/io/zero_copy_stream.h>
35
34
#include <google/protobuf/io/zero_copy_stream_impl.h>
36
35
 
42
41
  Written from Google proto example
43
42
*/
44
43
 
 
44
static void print_field(const message::Table::Field &field)
 
45
{
 
46
  cout << "\t`" << field.name() << "`";
 
47
 
 
48
  message::Table::Field::FieldType field_type= field.type();
 
49
 
 
50
  switch (field_type)
 
51
  {
 
52
    case message::Table::Field::DOUBLE:
 
53
    cout << " DOUBLE ";
 
54
    break;
 
55
  case message::Table::Field::VARCHAR:
 
56
    cout << " VARCHAR(" << field.string_options().length() << ")";
 
57
    break;
 
58
  case message::Table::Field::BLOB:
 
59
    cout << " BLOB "; /* FIXME: or text, depends on collation */
 
60
    if(field.string_options().has_collation_id())
 
61
      cout << "COLLATION=" << field.string_options().collation_id() << " ";
 
62
    break;
 
63
  case message::Table::Field::ENUM:
 
64
    {
 
65
      int x;
 
66
 
 
67
      cout << " ENUM(";
 
68
      for (x= 0; x < field.set_options().field_value_size() ; x++)
 
69
      {
 
70
        const string type= field.set_options().field_value(x);
 
71
 
 
72
        if (x != 0)
 
73
          cout << ",";
 
74
        cout << "'" << type << "'";
 
75
      }
 
76
      cout << ") ";
 
77
      break;
 
78
    }
 
79
  case message::Table::Field::INTEGER:
 
80
    cout << " INT" ;
 
81
    break;
 
82
  case message::Table::Field::BIGINT:
 
83
    cout << " BIGINT ";
 
84
    break;
 
85
  case message::Table::Field::DECIMAL:
 
86
    cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
 
87
    break;
 
88
  case message::Table::Field::DATE:
 
89
    cout << " DATE ";
 
90
    break;
 
91
  case message::Table::Field::TIME:
 
92
    cout << " TIME ";
 
93
    break;
 
94
  case message::Table::Field::TIMESTAMP:
 
95
    cout << " TIMESTAMP ";
 
96
    break;
 
97
  case message::Table::Field::DATETIME:
 
98
    cout << " DATETIME ";
 
99
    break;
 
100
  }
 
101
 
 
102
  if (field.type() == message::Table::Field::INTEGER
 
103
      || field.type() == message::Table::Field::BIGINT)
 
104
  {
 
105
    if (field.has_constraints()
 
106
        && field.constraints().has_is_unsigned())
 
107
      if (field.constraints().is_unsigned())
 
108
        cout << " UNSIGNED";
 
109
 
 
110
    if (field.has_numeric_options() &&
 
111
      field.numeric_options().is_autoincrement())
 
112
      cout << " AUTOINCREMENT ";
 
113
  }
 
114
 
 
115
  if (!( field.has_constraints()
 
116
         && field.constraints().is_nullable()))
 
117
    cout << " NOT NULL ";
 
118
 
 
119
  if (field.type() == message::Table::Field::BLOB
 
120
      || field.type() == message::Table::Field::VARCHAR)
 
121
  {
 
122
    if (field.string_options().has_collation())
 
123
      cout << " COLLATE " << field.string_options().collation();
 
124
  }
 
125
 
 
126
  if (field.options().has_default_value())
 
127
    cout << " DEFAULT `" << field.options().default_value() << "` " ;
 
128
 
 
129
  if (field.options().has_default_bin_value())
 
130
  {
 
131
    string v= field.options().default_bin_value();
 
132
    cout << " DEFAULT 0x";
 
133
    for(unsigned int i=0; i< v.length(); i++)
 
134
    {
 
135
      printf("%.2x", *(v.c_str()+i));
 
136
    }
 
137
  }
 
138
 
 
139
  if (field.type() == message::Table::Field::TIMESTAMP)
 
140
    if (field.timestamp_options().has_auto_updates()
 
141
      && field.timestamp_options().auto_updates())
 
142
      cout << " ON UPDATE CURRENT_TIMESTAMP";
 
143
 
 
144
  if (field.has_comment())
 
145
    cout << " COMMENT `" << field.comment() << "` ";
 
146
}
 
147
 
 
148
static void print_engine(const message::Table::StorageEngine &engine)
 
149
{
 
150
  int32_t x;
 
151
 
 
152
  cout << " ENGINE = " << engine.name()  << endl;
 
153
 
 
154
  for (x= 0; x < engine.option_size(); ++x) {
 
155
    const message::Table::StorageEngine::EngineOption option= engine.option(x);
 
156
    cout << "\t" << option.option_name() << " = "
 
157
         << option.option_value() << endl;
 
158
  }
 
159
}
 
160
 
 
161
static void print_index(const message::Table::Index &index)
 
162
{
 
163
 
 
164
  if (index.is_primary())
 
165
    cout << " PRIMARY";
 
166
  else if (index.is_unique())
 
167
    cout << " UNIQUE";
 
168
  cout << " KEY `" << index.name() << "` (";
 
169
  {
 
170
    int32_t x;
 
171
 
 
172
    for (x= 0; x < index.index_part_size() ; x++)
 
173
    {
 
174
      const message::Table::Index::IndexPart part= index.index_part(x);
 
175
 
 
176
      if (x != 0)
 
177
        cout << ",";
 
178
      cout << "`" << part.fieldnr() << "`"; /* FIXME */
 
179
      if (part.has_compare_length())
 
180
        cout << "(" << part.compare_length() << ")";
 
181
    }
 
182
    cout << ")";
 
183
  }
 
184
  cout << "\t";
 
185
}
 
186
 
 
187
static void print_table_options(const message::Table::TableOptions &options)
 
188
{
 
189
  if (options.has_comment())
 
190
    cout << " COMMENT = '" << options.comment() << "' " << endl;
 
191
 
 
192
  if (options.has_collation())
 
193
    cout << " COLLATE = '" << options.collation() << "' " << endl;
 
194
 
 
195
  if (options.has_auto_increment())
 
196
    cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
 
197
 
 
198
  if (options.has_collation_id())
 
199
    cout << "-- collation_id = " << options.collation_id() << endl;
 
200
  
 
201
  if (options.has_row_type())
 
202
    cout << " ROW_TYPE = " << options.row_type() << endl;
 
203
 
 
204
  if (options.has_data_file_name())
 
205
    cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
 
206
 
 
207
  if (options.has_index_file_name())
 
208
    cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
 
209
 
 
210
  if (options.has_max_rows())
 
211
    cout << " MAX_ROWS = " << options.max_rows() << endl;
 
212
 
 
213
  if (options.has_min_rows())
 
214
    cout << " MIN_ROWS = " << options.min_rows() << endl;
 
215
 
 
216
  if (options.has_auto_increment_value())
 
217
    cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
 
218
 
 
219
  if (options.has_avg_row_length())
 
220
    cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
 
221
 
 
222
  if (options.has_key_block_size())
 
223
    cout << " KEY_BLOCK_SIZE = "  << options.key_block_size() << endl;
 
224
 
 
225
  if (options.has_block_size())
 
226
    cout << " BLOCK_SIZE = " << options.block_size() << endl;
 
227
 
 
228
  if (options.has_comment())
 
229
    cout << " COMMENT = '" << options.comment() << "'" << endl;
 
230
 
 
231
  if (options.has_pack_keys())
 
232
    cout << " PACK_KEYS = " << options.pack_keys() << endl;
 
233
  if (options.has_pack_record())
 
234
    cout << " PACK_RECORD = " << options.pack_record() << endl;
 
235
  if (options.has_checksum())
 
236
    cout << " CHECKSUM = " << options.checksum() << endl;
 
237
  if (options.has_page_checksum())
 
238
    cout << " PAGE_CHECKSUM = " << options.page_checksum() << endl;
 
239
}
 
240
 
 
241
 
 
242
static void print_table(const message::Table &table)
 
243
{
 
244
  int32_t x;
 
245
 
 
246
  cout << "CREATE ";
 
247
 
 
248
  if (table.type() == message::Table::TEMPORARY)
 
249
    cout << "TEMPORARY ";
 
250
 
 
251
  cout << "TABLE `" << table.name() << "` (" << endl;
 
252
 
 
253
  for (x= 0; x < table.field_size() ; x++)
 
254
  {
 
255
    const message::Table::Field field = table.field(x);
 
256
 
 
257
    if (x != 0)
 
258
      cout << "," << endl;
 
259
 
 
260
    print_field(field);
 
261
  }
 
262
 
 
263
  for (x= 0; x < table.indexes_size() ; x++)
 
264
  {
 
265
    const message::Table::Index index= table.indexes(x);
 
266
 
 
267
    if (x != 0)
 
268
      cout << "," << endl;;
 
269
 
 
270
    print_index(index);
 
271
 
 
272
  }
 
273
  cout << endl;
 
274
 
 
275
  cout << ") " << endl;
 
276
 
 
277
  print_engine(table.engine());
 
278
 
 
279
  if (table.has_options())
 
280
    print_table_options(table.options());
 
281
  /*
 
282
  if (table->has_stats())
 
283
    print_table_stats(&table->stats());
 
284
  */
 
285
}
 
286
 
45
287
int main(int argc, char* argv[])
46
288
{
47
289
  GOOGLE_PROTOBUF_VERIFY_VERSION;
75
317
    close(fd);
76
318
  }
77
319
 
78
 
  string output;
79
 
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
80
 
 
81
 
  cout << output << endl;
 
320
  print_table(table);
82
321
 
83
322
  return 0;
84
323
}