1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
* List objects(tables, triggers, etc.) in NDB Cluster
23
#include <ndb_global.h>
29
static Ndb_cluster_connection *ndb_cluster_connection= 0;
31
static const NdbDictionary::Dictionary * dic = 0;
32
static int _unqualified = 0;
33
static int _parsable = 0;
34
static int show_temp_status = 0;
36
const char *load_default_groups[]= { "mysql_cluster",0 };
39
fatal(char const* fmt, ...)
44
BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
48
ndbout << " - " << ndb->getNdbError();
50
NDBT_ProgramExit(NDBT_FAILED);
55
fatal_dict(char const* fmt, ...)
60
BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
64
ndbout << " - " << dic->getNdbError();
66
NDBT_ProgramExit(NDBT_FAILED);
71
list(const char * tabname,
72
NdbDictionary::Object::Type type)
74
NdbDictionary::Dictionary::List list;
76
if (dic->listObjects(list, type) == -1)
77
fatal_dict("listObjects");
79
if (dic->listIndexes(list, tabname) == -1)
80
fatal_dict("listIndexes");
84
if (ndb->usingFullyQualifiedNames())
87
ndbout_c("%-5s %-20s %-8s %-7s %-4s %-12s %-8s %s", "id", "type", "state", "logging", "temp", "database", "schema", "name");
89
ndbout_c("%-5s %-20s %-8s %-7s %-12s %-8s %s", "id", "type", "state", "logging", "database", "schema", "name");
94
ndbout_c("%-5s %-20s %-8s %-7s %-4s %s", "id", "type", "state", "logging", "temp", "name");
96
ndbout_c("%-5s %-20s %-8s %-7s %s", "id", "type", "state", "logging", "name");
99
for (unsigned i = 0; i < list.count; i++) {
100
NdbDictionary::Dictionary::List::Element& elt = list.elements[i];
102
bool isTable = false;
104
case NdbDictionary::Object::SystemTable:
105
strcpy(type, "SystemTable");
108
case NdbDictionary::Object::UserTable:
109
strcpy(type, "UserTable");
112
case NdbDictionary::Object::UniqueHashIndex:
113
strcpy(type, "UniqueHashIndex");
116
case NdbDictionary::Object::OrderedIndex:
117
strcpy(type, "OrderedIndex");
120
case NdbDictionary::Object::HashIndexTrigger:
121
strcpy(type, "HashIndexTrigger");
123
case NdbDictionary::Object::IndexTrigger:
124
strcpy(type, "IndexTrigger");
126
case NdbDictionary::Object::SubscriptionTrigger:
127
strcpy(type, "SubscriptionTrigger");
129
case NdbDictionary::Object::ReadOnlyConstraint:
130
strcpy(type, "ReadOnlyConstraint");
132
case NdbDictionary::Object::Tablespace:
133
strcpy(type, "Tablespace");
135
case NdbDictionary::Object::LogfileGroup:
136
strcpy(type, "LogfileGroup");
138
case NdbDictionary::Object::Datafile:
139
strcpy(type, "Datafile");
141
case NdbDictionary::Object::Undofile:
142
strcpy(type, "Undofile");
145
sprintf(type, "%d", (int)elt.type);
150
case NdbDictionary::Object::StateOffline:
151
strcpy(state, "Offline");
153
case NdbDictionary::Object::StateBuilding:
154
strcpy(state, "Building");
156
case NdbDictionary::Object::StateDropping:
157
strcpy(state, "Dropping");
159
case NdbDictionary::Object::StateOnline:
160
strcpy(state, "Online");
162
case NdbDictionary::Object::StateBackup:
163
strcpy(state, "Backup");
165
case NdbDictionary::Object::StateBroken:
166
strcpy(state, "Broken");
169
sprintf(state, "%d", (int)elt.state);
177
case NdbDictionary::Object::StoreNotLogged:
180
case NdbDictionary::Object::StorePermanent:
181
strcpy(store, "Yes");
184
sprintf(store, "%d", (int)elt.store);
189
if (show_temp_status)
195
case NDB_TEMP_TAB_PERMANENT:
198
case NDB_TEMP_TAB_TEMPORARY:
202
sprintf(temp, "%d", (int)elt.temp);
207
if (ndb->usingFullyQualifiedNames())
211
if (show_temp_status)
212
ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, temp, (elt.database)?elt.database:"", (elt.schema)?elt.schema:"", elt.name);
214
ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, (elt.database)?elt.database:"", (elt.schema)?elt.schema:"", elt.name);
218
if (show_temp_status)
219
ndbout_c("%-5d %-20s %-8s %-7s %-4s %-12s %-8s %s", elt.id, type, state, store, temp, (elt.database)?elt.database:"", (elt.schema)?elt.schema:"", elt.name);
221
ndbout_c("%-5d %-20s %-8s %-7s %-12s %-8s %s", elt.id, type, state, store, (elt.database)?elt.database:"", (elt.schema)?elt.schema:"", elt.name);
228
if (show_temp_status)
229
ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, temp, elt.name);
231
ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, elt.name);
235
if (show_temp_status)
236
ndbout_c("%-5d %-20s %-8s %-7s %-4s %s", elt.id, type, state, store, temp, elt.name);
238
ndbout_c("%-5d %-20s %-8s %-7s %s", elt.id, type, state, store, elt.name);
248
static const char* _dbname = "TEST_DB";
251
enum options_ndb_show_tables
253
OPT_SHOW_TMP_STATUS=256
255
static struct my_option my_long_options[] =
257
NDB_STD_OPTS("ndb_show_tables"),
258
{ "database", 'd', "Name of database table is in",
259
(uchar**) &_dbname, (uchar**) &_dbname, 0,
260
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
261
{ "loops", 'l', "loops",
262
(uchar**) &_loops, (uchar**) &_loops, 0,
263
GET_INT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0 },
264
{ "type", 't', "type",
265
(uchar**) &_type, (uchar**) &_type, 0,
266
GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
267
{ "unqualified", 'u', "Use unqualified table names",
268
(uchar**) &_unqualified, (uchar**) &_unqualified, 0,
269
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
270
{ "parsable", 'p', "Return output suitable for mysql LOAD DATA INFILE",
271
(uchar**) &_parsable, (uchar**) &_parsable, 0,
272
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
273
{ "show-temp-status", OPT_SHOW_TMP_STATUS, "Show table temporary flag",
274
(uchar**) &show_temp_status, (uchar**) &show_temp_status, 0,
275
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
276
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
283
"This program list all system objects in NDB Cluster.\n"\
284
"Type of objects to display can be limited with -t option\n"\
285
" ex: ndb_show_tables -t 2 would show all UserTables\n"\
286
"To show all indexes for a table write table name as final argument\n"\
287
" ex: ndb_show_tables T1\n";
289
ndb_std_print_version();
290
print_defaults(MYSQL_CONFIG_NAME,load_default_groups);
292
my_print_help(my_long_options);
293
my_print_variables(my_long_options);
296
int main(int argc, char** argv){
298
const char* _tabname;
299
load_defaults("my",load_default_groups,&argc,&argv);
302
opt_debug= "d:t:O,/tmp/ndb_show_tables.trace";
304
if ((ho_error=handle_options(&argc, &argv, my_long_options,
305
ndb_std_get_one_option)))
306
return NDBT_ProgramExit(NDBT_WRONGARGS);
309
ndb_cluster_connection = new Ndb_cluster_connection(opt_connect_str);
310
ndb_cluster_connection->set_name("ndb_show_tables");
311
if (ndb_cluster_connection->connect(12,5,1))
312
fatal("Unable to connect to management server.");
313
if (ndb_cluster_connection->wait_until_ready(30,0) < 0)
314
fatal("Cluster nodes not ready in 30 seconds.");
316
ndb = new Ndb(ndb_cluster_connection, _dbname);
317
if (ndb->init() != 0)
319
dic = ndb->getDictionary();
320
for (int i = 0; _loops == 0 || i < _loops; i++) {
321
list(_tabname, (NdbDictionary::Object::Type)_type);
324
delete ndb_cluster_connection;
325
return NDBT_ProgramExit(NDBT_OK);