~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/tools/listTables.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
/*
 
17
 * list_tables
 
18
 *
 
19
 * List objects(tables, triggers, etc.) in NDB Cluster
 
20
 *
 
21
 */
 
22
 
 
23
#include <ndb_global.h>
 
24
#include <ndb_opts.h>
 
25
 
 
26
#include <NdbApi.hpp>
 
27
#include <NDBT.hpp>
 
28
 
 
29
static Ndb_cluster_connection *ndb_cluster_connection= 0;
 
30
static Ndb* ndb = 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;
 
35
 
 
36
const char *load_default_groups[]= { "mysql_cluster",0 };
 
37
 
 
38
static void
 
39
fatal(char const* fmt, ...)
 
40
{
 
41
    va_list ap;
 
42
    char buf[500];
 
43
    va_start(ap, fmt);
 
44
    BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
 
45
    va_end(ap);
 
46
    ndbout << buf;
 
47
    if (ndb)
 
48
      ndbout << " - " << ndb->getNdbError();
 
49
    ndbout << endl;
 
50
    NDBT_ProgramExit(NDBT_FAILED);
 
51
    exit(1);
 
52
}
 
53
 
 
54
static void
 
55
fatal_dict(char const* fmt, ...)
 
56
{
 
57
    va_list ap;
 
58
    char buf[500];
 
59
    va_start(ap, fmt);
 
60
    BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
 
61
    va_end(ap);
 
62
    ndbout << buf;
 
63
    if (dic)
 
64
      ndbout << " - " << dic->getNdbError();
 
65
    ndbout << endl;
 
66
    NDBT_ProgramExit(NDBT_FAILED);
 
67
    exit(1);
 
68
}
 
69
 
 
70
static void
 
71
list(const char * tabname, 
 
72
     NdbDictionary::Object::Type type)
 
73
{
 
74
    NdbDictionary::Dictionary::List list;
 
75
    if (tabname == 0) {
 
76
        if (dic->listObjects(list, type) == -1)
 
77
            fatal_dict("listObjects");
 
78
    } else {
 
79
        if (dic->listIndexes(list, tabname) == -1)
 
80
            fatal_dict("listIndexes");
 
81
    }
 
82
    if (!_parsable)
 
83
    {
 
84
      if (ndb->usingFullyQualifiedNames())
 
85
      {
 
86
        if (show_temp_status)
 
87
          ndbout_c("%-5s %-20s %-8s %-7s %-4s %-12s %-8s %s", "id", "type", "state", "logging", "temp", "database", "schema", "name");
 
88
        else
 
89
          ndbout_c("%-5s %-20s %-8s %-7s %-12s %-8s %s", "id", "type", "state", "logging", "database", "schema", "name");
 
90
      }
 
91
      else
 
92
      {
 
93
        if (show_temp_status)
 
94
          ndbout_c("%-5s %-20s %-8s %-7s %-4s %s", "id", "type", "state", "logging", "temp", "name");
 
95
        else
 
96
          ndbout_c("%-5s %-20s %-8s %-7s %s", "id", "type", "state", "logging", "name");
 
97
      }
 
98
    }
 
99
    for (unsigned i = 0; i < list.count; i++) {
 
100
        NdbDictionary::Dictionary::List::Element& elt = list.elements[i];
 
101
        char type[100];
 
102
        bool isTable = false;
 
103
        switch (elt.type) {
 
104
        case NdbDictionary::Object::SystemTable:
 
105
            strcpy(type, "SystemTable");
 
106
            isTable = true;
 
107
            break;
 
108
        case NdbDictionary::Object::UserTable:
 
109
            strcpy(type, "UserTable");
 
110
            isTable = true;
 
111
            break;
 
112
        case NdbDictionary::Object::UniqueHashIndex:
 
113
            strcpy(type, "UniqueHashIndex");
 
114
            isTable = true;
 
115
            break;
 
116
        case NdbDictionary::Object::OrderedIndex:
 
117
            strcpy(type, "OrderedIndex");
 
118
            isTable = true;
 
119
            break;
 
120
        case NdbDictionary::Object::HashIndexTrigger:
 
121
            strcpy(type, "HashIndexTrigger");
 
122
            break;
 
123
        case NdbDictionary::Object::IndexTrigger:
 
124
            strcpy(type, "IndexTrigger");
 
125
            break;
 
126
        case NdbDictionary::Object::SubscriptionTrigger:
 
127
            strcpy(type, "SubscriptionTrigger");
 
128
            break;
 
129
        case NdbDictionary::Object::ReadOnlyConstraint:
 
130
            strcpy(type, "ReadOnlyConstraint");
 
131
            break;
 
132
        case NdbDictionary::Object::Tablespace:
 
133
          strcpy(type, "Tablespace");
 
134
          break;
 
135
        case NdbDictionary::Object::LogfileGroup:
 
136
          strcpy(type, "LogfileGroup");
 
137
          break;
 
138
        case NdbDictionary::Object::Datafile:
 
139
          strcpy(type, "Datafile");
 
140
          break;
 
141
        case NdbDictionary::Object::Undofile:
 
142
          strcpy(type, "Undofile");
 
143
          break;
 
144
        default:
 
145
          sprintf(type, "%d", (int)elt.type);
 
146
            break;
 
147
        }
 
148
        char state[100];
 
149
        switch (elt.state) {
 
150
        case NdbDictionary::Object::StateOffline:
 
151
            strcpy(state, "Offline");
 
152
            break;
 
153
        case NdbDictionary::Object::StateBuilding:
 
154
            strcpy(state, "Building");
 
155
            break;
 
156
        case NdbDictionary::Object::StateDropping:
 
157
            strcpy(state, "Dropping");
 
158
            break;
 
159
        case NdbDictionary::Object::StateOnline:
 
160
            strcpy(state, "Online");
 
161
            break;
 
162
        case NdbDictionary::Object::StateBackup:
 
163
            strcpy(state, "Backup");
 
164
            break;
 
165
        case NdbDictionary::Object::StateBroken:
 
166
            strcpy(state, "Broken");
 
167
            break;
 
168
        default:
 
169
            sprintf(state, "%d", (int)elt.state);
 
170
            break;
 
171
        }
 
172
        char store[100];
 
173
        if (! isTable)
 
174
            strcpy(store, "-");
 
175
        else {
 
176
            switch (elt.store) {
 
177
            case NdbDictionary::Object::StoreNotLogged:
 
178
                strcpy(store, "No");
 
179
                break;
 
180
            case NdbDictionary::Object::StorePermanent:
 
181
                strcpy(store, "Yes");
 
182
                break;
 
183
            default:
 
184
                sprintf(store, "%d", (int)elt.store);
 
185
                break;
 
186
            }
 
187
        }
 
188
        char temp[100];
 
189
        if (show_temp_status)
 
190
        {
 
191
          if (! isTable)
 
192
              strcpy(temp, "-");
 
193
          else {
 
194
              switch (elt.temp) {
 
195
              case NDB_TEMP_TAB_PERMANENT:
 
196
                  strcpy(temp, "No");
 
197
                  break;
 
198
              case NDB_TEMP_TAB_TEMPORARY:
 
199
                  strcpy(temp, "Yes");
 
200
                  break;
 
201
              default:
 
202
                  sprintf(temp, "%d", (int)elt.temp);
 
203
                  break;
 
204
              }
 
205
          }
 
206
        }
 
207
        if (ndb->usingFullyQualifiedNames())
 
208
        {
 
209
          if (_parsable)
 
210
          {
 
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);
 
213
            else
 
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);
 
215
          }
 
216
          else
 
217
          {
 
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);
 
220
            else
 
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);
 
222
          }
 
223
        }
 
224
        else
 
225
        {
 
226
          if (_parsable)
 
227
          {
 
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);
 
230
            else
 
231
              ndbout_c("%d\t'%s'\t'%s'\t'%s'\t'%s'", elt.id, type, state, store, elt.name);
 
232
          }
 
233
          else
 
234
          {
 
235
            if (show_temp_status)
 
236
              ndbout_c("%-5d %-20s %-8s %-7s %-4s %s", elt.id, type, state, store, temp, elt.name);
 
237
            else
 
238
              ndbout_c("%-5d %-20s %-8s %-7s %s", elt.id, type, state, store, elt.name);
 
239
          }
 
240
        }
 
241
    }
 
242
    if (_parsable)
 
243
      exit(0);
 
244
}
 
245
 
 
246
NDB_STD_OPTS_VARS;
 
247
 
 
248
static const char* _dbname = "TEST_DB";
 
249
static int _loops;
 
250
static int _type;
 
251
enum options_ndb_show_tables
 
252
{
 
253
  OPT_SHOW_TMP_STATUS=256
 
254
};
 
255
static struct my_option my_long_options[] =
 
256
{
 
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}
 
277
};
 
278
static void usage()
 
279
{
 
280
#ifdef NOT_USED
 
281
  char desc[] = 
 
282
    "tabname\n"\
 
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";
 
288
#endif
 
289
  ndb_std_print_version();
 
290
  print_defaults(MYSQL_CONFIG_NAME,load_default_groups);
 
291
  puts("");
 
292
  my_print_help(my_long_options);
 
293
  my_print_variables(my_long_options);
 
294
}
 
295
 
 
296
int main(int argc, char** argv){
 
297
  NDB_INIT(argv[0]);
 
298
  const char* _tabname;
 
299
  load_defaults("my",load_default_groups,&argc,&argv);
 
300
  int ho_error;
 
301
#ifndef DBUG_OFF
 
302
  opt_debug= "d:t:O,/tmp/ndb_show_tables.trace";
 
303
#endif
 
304
  if ((ho_error=handle_options(&argc, &argv, my_long_options,
 
305
                               ndb_std_get_one_option)))
 
306
    return NDBT_ProgramExit(NDBT_WRONGARGS);
 
307
  _tabname = argv[0];
 
308
 
 
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.");
 
315
 
 
316
  ndb = new Ndb(ndb_cluster_connection, _dbname);
 
317
  if (ndb->init() != 0)
 
318
    fatal("init");
 
319
  dic = ndb->getDictionary();
 
320
  for (int i = 0; _loops == 0 || i < _loops; i++) {
 
321
    list(_tabname, (NdbDictionary::Object::Type)_type);
 
322
  }
 
323
  delete ndb;
 
324
  delete ndb_cluster_connection;
 
325
  return NDBT_ProgramExit(NDBT_OK);
 
326
}
 
327
 
 
328
// vim: set sw=4: