1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
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.
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.
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
21
#include <drizzled/global.h>
22
#include <sys/types.h>
32
#include <drizzled/message/table.pb.h>
33
#include <google/protobuf/io/zero_copy_stream.h>
34
#include <google/protobuf/io/zero_copy_stream_impl.h>
5
36
using namespace std;
37
using namespace drizzled;
38
using namespace google;
8
41
Written from Google proto example
11
void printType(const drizzle::Table::Field *field)
44
static void print_field(const message::Table::Field &field)
13
switch (field->type())
46
cout << "\t`" << field.name() << "`";
48
message::Table::Field::FieldType field_type= field.type();
15
case drizzle::Table::DOUBLE:
52
case message::Table::Field::DOUBLE:
16
53
cout << " DOUBLE ";
18
case drizzle::Table::VARCHAR:
19
cout << " VARCHAR(" << field->length() << ")";
21
case drizzle::Table::TEXT:
24
case drizzle::Table::BLOB:
27
case drizzle::Table::ENUM:
55
case message::Table::Field::VARCHAR:
56
cout << " VARCHAR(" << field.string_options().length() << ")";
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() << " ";
63
case message::Table::Field::ENUM:
32
for (x= 0; x < field->values_size() ; x++)
68
for (x= 0; x < field.set_options().field_value_size() ; x++)
34
const string type= field->values(x);
70
const string type= field.set_options().field_value(x);
43
case drizzle::Table::SET:
46
case drizzle::Table::TINYINT:
49
case drizzle::Table::SMALLINT:
52
case drizzle::Table::INTEGER:
55
case drizzle::Table::BIGINT:
79
case message::Table::Field::INTEGER:
82
case message::Table::Field::BIGINT:
56
83
cout << " BIGINT ";
58
case drizzle::Table::DECIMAL:
59
cout << " DECIMAL(" << field->length() << "," << field->scale() << ") ";
61
case drizzle::Table::VARBINARY:
62
cout << " VARBINARY(" << field->length() << ") ";
64
case drizzle::Table::DATE:
85
case message::Table::Field::DECIMAL:
86
cout << " DECIMAL(" << field.numeric_options().precision() << "," << field.numeric_options().scale() << ") ";
88
case message::Table::Field::DATE:
67
case drizzle::Table::TIME:
91
case message::Table::Field::TIME:
70
case drizzle::Table::TIMESTAMP:
94
case message::Table::Field::TIMESTAMP:
71
95
cout << " TIMESTAMP ";
73
case drizzle::Table::DATETIME:
97
case message::Table::Field::DATETIME:
74
98
cout << " DATETIME ";
78
if (field->has_characterset())
79
cout << " CHARACTER SET " << field->characterset();
81
if (field->has_collation())
82
cout << " COLLATE " << field->collation();
84
if (field->is_notnull())
102
if (field.type() == message::Table::Field::INTEGER
103
|| field.type() == message::Table::Field::BIGINT)
105
if (field.has_constraints()
106
&& field.constraints().has_is_unsigned())
107
if (field.constraints().is_unsigned())
110
if (field.has_numeric_options() &&
111
field.numeric_options().is_autoincrement())
112
cout << " AUTOINCREMENT ";
115
if (!( field.has_constraints()
116
&& field.constraints().is_nullable()))
85
117
cout << " NOT NULL ";
87
if (field->has_default_value())
88
cout << " DEFAULT `" << field->default_value() << "` " ;
90
if (field->on_update())
91
cout << " ON UPDATE CURRENT_TIMESTAMP";
93
if (field->autoincrement())
94
cout << " AUTOINCREMENT ";
96
if (field->has_comment())
97
cout << " COMMENT `" << field->comment() << "` ";
100
void printTable(const drizzle::Table *table)
104
cout << "CREATE TABLE";
107
cout << " TEMPORARY";
109
cout << " `" << table->name() << "` (" << endl;
119
if (field.type() == message::Table::Field::BLOB
120
|| field.type() == message::Table::Field::VARCHAR)
122
if (field.string_options().has_collation())
123
cout << " COLLATE " << field.string_options().collation();
126
if (field.options().has_default_value())
127
cout << " DEFAULT `" << field.options().default_value() << "` " ;
129
if (field.options().has_default_bin_value())
131
string v= field.options().default_bin_value();
132
cout << " DEFAULT 0x";
133
for(unsigned int i=0; i< v.length(); i++)
135
printf("%.2x", *(v.c_str()+i));
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";
144
if (field.has_comment())
145
cout << " COMMENT `" << field.comment() << "` ";
148
static void print_engine(const message::Table::StorageEngine &engine)
152
cout << " ENGINE = " << engine.name() << endl;
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;
161
static void print_index(const message::Table::Index &index)
164
if (index.is_primary())
166
else if (index.is_unique())
168
cout << " KEY `" << index.name() << "` (";
172
for (x= 0; x < index.index_part_size() ; x++)
174
const message::Table::Index::IndexPart part= index.index_part(x);
178
cout << "`" << part.fieldnr() << "`"; /* FIXME */
179
if (part.has_compare_length())
180
cout << "(" << part.compare_length() << ")";
187
static void print_table_options(const message::Table::TableOptions &options)
189
if (options.has_comment())
190
cout << " COMMENT = '" << options.comment() << "' " << endl;
192
if (options.has_collation())
193
cout << " COLLATE = '" << options.collation() << "' " << endl;
195
if (options.has_auto_increment())
196
cout << " AUTOINCREMENT_OFFSET = " << options.auto_increment() << endl;
198
if (options.has_collation_id())
199
cout << "-- collation_id = " << options.collation_id() << endl;
111
for (x= 0; x < table->field_size() ; x++)
113
const drizzle::Table::Field field = table->field(x);
201
if (options.has_row_type())
202
cout << " ROW_TYPE = " << options.row_type() << endl;
204
if (options.has_data_file_name())
205
cout << " DATA_FILE_NAME = '" << options.data_file_name() << "'" << endl;
207
if (options.has_index_file_name())
208
cout << " INDEX_FILE_NAME = '" << options.index_file_name() << "'" << endl;
210
if (options.has_max_rows())
211
cout << " MAX_ROWS = " << options.max_rows() << endl;
213
if (options.has_min_rows())
214
cout << " MIN_ROWS = " << options.min_rows() << endl;
216
if (options.has_auto_increment_value())
217
cout << " AUTO_INCREMENT = " << options.auto_increment_value() << endl;
219
if (options.has_avg_row_length())
220
cout << " AVG_ROW_LENGTH = " << options.avg_row_length() << endl;
222
if (options.has_key_block_size())
223
cout << " KEY_BLOCK_SIZE = " << options.key_block_size() << endl;
225
if (options.has_block_size())
226
cout << " BLOCK_SIZE = " << options.block_size() << endl;
228
if (options.has_comment())
229
cout << " COMMENT = '" << options.comment() << "'" << endl;
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;
242
static void print_table(const message::Table &table)
248
if (table.type() == message::Table::TEMPORARY)
249
cout << "TEMPORARY ";
251
cout << "TABLE `" << table.name() << "` (" << endl;
253
for (x= 0; x < table.field_size() ; x++)
255
const message::Table::Field field = table.field(x);
263
for (x= 0; x < table.indexes_size() ; x++)
265
const message::Table::Index index= table.indexes(x);
116
268
cout << "," << endl;;
118
cout << "\t`" << field.name() << "`";
122
for (x= 0; x < table->index_size() ; x++)
124
const drizzle::Table::Index index = table->index(x);
126
cout << "," << endl;;
131
cout << " PRIMARY KEY (`" << index.name() << "`)";
136
cout << " UNIQUE KEY `" << index.name() << "` (";
137
for (x= 0; x < index.values_size() ; x++)
139
const drizzle::Table::KeyPart key= index.values(x);
143
cout << "`" << key.name() << "`";
150
275
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;
277
print_engine(table.engine());
279
if (table.has_options())
280
print_table_options(table.options());
282
if (table->has_stats())
283
print_table_stats(&table->stats());
165
int main(int argc, char* argv[])
287
int main(int argc, char* argv[])
167
289
GOOGLE_PROTOBUF_VERIFY_VERSION;