MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
listTables.cpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 /*
19  * list_tables
20  *
21  * List objects(tables, triggers, etc.) in NDB Cluster
22  *
23  */
24 
25 #include <ndb_global.h>
26 #include <ndb_opts.h>
27 
28 #include <NdbApi.hpp>
29 #include <NDBT.hpp>
30 
31 static Ndb_cluster_connection *ndb_cluster_connection= 0;
32 static Ndb* ndb = 0;
33 static const NdbDictionary::Dictionary * dic = 0;
34 static int _unqualified = 0;
35 static int _parsable = 0;
36 static int show_temp_status = 0;
37 
38 const char *load_default_groups[]= { "mysql_cluster",0 };
39 
40 static void
41 fatal(char const* fmt, ...)
42 {
43  va_list ap;
44  char buf[500];
45  va_start(ap, fmt);
46  BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
47  va_end(ap);
48  ndbout << buf;
49  if (ndb)
50  ndbout << " - " << ndb->getNdbError();
51  ndbout << endl;
52  NDBT_ProgramExit(NDBT_FAILED);
53  exit(1);
54 }
55 
56 static void
57 fatal_dict(char const* fmt, ...)
58 {
59  va_list ap;
60  char buf[500];
61  va_start(ap, fmt);
62  BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
63  va_end(ap);
64  ndbout << buf;
65  if (dic)
66  ndbout << " - " << dic->getNdbError();
67  ndbout << endl;
68  NDBT_ProgramExit(NDBT_FAILED);
69  exit(1);
70 }
71 
72 static void
73 list(const char * tabname,
75 {
77  if (tabname == 0) {
78  if (dic->listObjects(list, type) == -1)
79  fatal_dict("listObjects");
80  } else {
81  if (dic->listIndexes(list, tabname) == -1)
82  fatal_dict("listIndexes");
83  }
84  if (!_parsable)
85  {
86  if (ndb->usingFullyQualifiedNames())
87  {
88  if (show_temp_status)
89  ndbout_c("%-5s %-20s %-8s %-7s %-4s %-12s %-8s %s", "id", "type", "state", "logging", "temp", "database", "schema", "name");
90  else
91  ndbout_c("%-5s %-20s %-8s %-7s %-12s %-8s %s", "id", "type", "state", "logging", "database", "schema", "name");
92  }
93  else
94  {
95  if (show_temp_status)
96  ndbout_c("%-5s %-20s %-8s %-7s %-4s %s", "id", "type", "state", "logging", "temp", "name");
97  else
98  ndbout_c("%-5s %-20s %-8s %-7s %s", "id", "type", "state", "logging", "name");
99  }
100  }
101  for (unsigned i = 0; i < list.count; i++) {
103  char type[100];
104  bool isTable = false;
105  switch (elt.type) {
107  strcpy(type, "SystemTable");
108  isTable = true;
109  break;
111  strcpy(type, "UserTable");
112  isTable = true;
113  break;
115  strcpy(type, "UniqueHashIndex");
116  isTable = true;
117  break;
119  strcpy(type, "OrderedIndex");
120  isTable = true;
121  break;
123  strcpy(type, "HashIndexTrigger");
124  break;
126  strcpy(type, "IndexTrigger");
127  break;
129  strcpy(type, "SubscriptionTrigger");
130  break;
132  strcpy(type, "ReadOnlyConstraint");
133  break;
134  case NdbDictionary::Object::ReorgTrigger:
135  strcpy(type, "ReorgTrigger");
136  break;
138  strcpy(type, "Tablespace");
139  break;
141  strcpy(type, "LogfileGroup");
142  break;
144  strcpy(type, "Datafile");
145  break;
147  strcpy(type, "Undofile");
148  break;
150  strcpy(type, "TableEvent");
151  break;
152  default:
153  sprintf(type, "%d", (int)elt.type);
154  break;
155  }
156  char state[100];
157  switch (elt.state) {
159  strcpy(state, "Offline");
160  break;
162  strcpy(state, "Building");
163  break;
165  strcpy(state, "Dropping");
166  break;
168  strcpy(state, "Online");
169  break;
171  strcpy(state, "Backup");
172  break;
174  strcpy(state, "Broken");
175  break;
176  default:
177  sprintf(state, "%d", (int)elt.state);
178  break;
179  }
180  char store[100];
181  if (! isTable)
182  strcpy(store, "-");
183  else {
184  switch (elt.store) {
186  strcpy(store, "No");
187  break;
189  strcpy(store, "Yes");
190  break;
191  default:
192  sprintf(store, "%d", (int)elt.store);
193  break;
194  }
195  }
196  char temp[100];
197  if (show_temp_status)
198  {
199  if (! isTable)
200  strcpy(temp, "-");
201  else {
202  switch (elt.temp) {
203  case NDB_TEMP_TAB_PERMANENT:
204  strcpy(temp, "No");
205  break;
206  case NDB_TEMP_TAB_TEMPORARY:
207  strcpy(temp, "Yes");
208  break;
209  default:
210  sprintf(temp, "%d", (int)elt.temp);
211  break;
212  }
213  }
214  }
215  if (ndb->usingFullyQualifiedNames())
216  {
217  if (_parsable)
218  {
219  if (show_temp_status)
220  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);
221  else
222  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);
223  }
224  else
225  {
226  if (show_temp_status)
227  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);
228  else
229  ndbout_c("%-5d %-20s %-8s %-7s %-12s %-8s %s", elt.id, type, state, store, (elt.database)?elt.database:"", (elt.schema)?elt.schema:"", elt.name);
230  }
231  }
232  else
233  {
234  if (_parsable)
235  {
236  if (show_temp_status)
237  ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, temp, elt.name);
238  else
239  ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, elt.name);
240  }
241  else
242  {
243  if (show_temp_status)
244  ndbout_c("%-5d %-20s %-8s %-7s %-4s %s", elt.id, type, state, store, temp, elt.name);
245  else
246  ndbout_c("%-5d %-20s %-8s %-7s %s", elt.id, type, state, store, elt.name);
247  }
248  }
249  }
250  if (_parsable)
251  exit(0);
252 }
253 
254 static const char* _dbname = "TEST_DB";
255 static int _loops;
256 static int _type;
257 
258 static struct my_option my_long_options[] =
259 {
260  NDB_STD_OPTS("ndb_show_tables"),
261  { "database", 'd', "Name of database table is in",
262  (uchar**) &_dbname, (uchar**) &_dbname, 0,
263  GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
264  { "loops", 'l', "loops",
265  (uchar**) &_loops, (uchar**) &_loops, 0,
266  GET_INT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0 },
267  { "type", 't', "type",
268  (uchar**) &_type, (uchar**) &_type, 0,
269  GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
270  { "unqualified", 'u', "Use unqualified table names",
271  (uchar**) &_unqualified, (uchar**) &_unqualified, 0,
272  GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
273  { "parsable", 'p', "Return output suitable for mysql LOAD DATA INFILE",
274  (uchar**) &_parsable, (uchar**) &_parsable, 0,
275  GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
276  { "show-temp-status", NDB_OPT_NOSHORT, "Show table temporary flag",
277  (uchar**) &show_temp_status, (uchar**) &show_temp_status, 0,
278  GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
279  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
280 };
281 
282 static void short_usage_sub(void)
283 {
284  ndb_short_usage_sub(NULL);
285 }
286 
287 static void usage()
288 {
289  ndb_usage(short_usage_sub, load_default_groups, my_long_options);
290 }
291 
292 int main(int argc, char** argv){
293  NDB_INIT(argv[0]);
294  const char* _tabname;
295  ndb_opt_set_usage_funcs(short_usage_sub, usage);
296  load_defaults("my",load_default_groups,&argc,&argv);
297  int ho_error;
298 #ifndef DBUG_OFF
299  opt_debug= "d:t:O,/tmp/ndb_show_tables.trace";
300 #endif
301  if ((ho_error=handle_options(&argc, &argv, my_long_options,
302  ndb_std_get_one_option)))
303  return NDBT_ProgramExit(NDBT_WRONGARGS);
304  _tabname = argv[0];
305 
306  ndb_cluster_connection = new Ndb_cluster_connection(opt_ndb_connectstring,
307  opt_ndb_nodeid);
308  if (ndb_cluster_connection == NULL)
309  fatal("Unable to create cluster connection");
310 
311  ndb_cluster_connection->set_name("ndb_show_tables");
312  if (ndb_cluster_connection->connect(12,5,1))
313  fatal("Unable to connect to management server.");
314  if (ndb_cluster_connection->wait_until_ready(30,0) < 0)
315  fatal("Cluster nodes not ready in 30 seconds.");
316 
317  ndb = new Ndb(ndb_cluster_connection, _dbname);
318  if (ndb->init() != 0)
319  fatal("init");
320  dic = ndb->getDictionary();
321  for (int i = 0; _loops == 0 || i < _loops; i++) {
322  list(_tabname, (NdbDictionary::Object::Type)_type);
323  }
324  delete ndb;
325  delete ndb_cluster_connection;
326  return NDBT_ProgramExit(NDBT_OK);
327 }
328 
329 // vim: set sw=4: