MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
create_index.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB, 2009 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 
21 #include <NdbOut.hpp>
22 #include <NdbApi.hpp>
23 #include <NDBT.hpp>
24 
25 #include <getarg.h>
26 
27 
28 
29 int
30 main(int argc, const char** argv){
31  ndb_init();
32 
33  const char* _dbname = "TEST_DB";
34  int _help = 0;
35  int _ordered = 0, _pk = 1;
36  char* _iname= NULL;
37  char* _tname= NULL;
38 
39  struct getargs args[] = {
40  { "database", 'd', arg_string, &_dbname, "dbname",
41  "Name of database table is in"},
42  { "ordered", 'o', arg_flag, &_ordered, "Create ordered index", "" },
43  { "pk", 'p', arg_flag, &_pk, "Create index on primary key", "" },
44  { "idxname", 'i', arg_string, &_iname, "idxname", "Override default name for index" },
45  { "tabname", 't', arg_string, &_tname, "tabname", "Specify single tabname and list of col names as args" },
46  { "usage", '?', arg_flag, &_help, "Print help", "" }
47  };
48 
49  int num_args = sizeof(args) / sizeof(args[0]);
50  int optind = 0;
51  char desc[] =
52  "<tabname>+\n"\
53  "This program will create one unique hash index named ind_<tabname> "
54  " for each table. The index will contain all columns in the table";
55 
56  if(getarg(args, num_args, argc, argv, &optind) || _help ||
57  argv[optind] == NULL){
58  arg_printusage(args, num_args, argv[0], desc);
59  return NDBT_ProgramExit(NDBT_WRONGARGS);
60  }
61 
63  if(con.connect(12, 5, 1) != 0)
64  {
65  return NDBT_ProgramExit(NDBT_FAILED);
66  }
67 
68  Ndb MyNdb(&con, _dbname);
69  if(MyNdb.init() != 0){
70  ERR(MyNdb.getNdbError());
71  return NDBT_ProgramExit(NDBT_FAILED);
72  }
73 
74  while(MyNdb.waitUntilReady() != 0)
75  ndbout << "Waiting for ndb to become ready..." << endl;
76 
77  NdbDictionary::Dictionary * dict = MyNdb.getDictionary();
78 
79  for(int i = optind; i<argc; i++){
80  const char* tabName= (_tname)? _tname : argv[i];
81  const NdbDictionary::Table * tab = dict->getTable(tabName);
82  if(tab == 0){
83  g_err << "Unknown table: " << tabName << endl;
84  if (_tname)
85  return NDBT_ProgramExit(NDBT_FAILED);
86  continue;
87  }
88 
90  if(_ordered){
92  ind.setLogging(false);
93  } else {
95  }
96  char buf[512];
97  if (!_iname)
98  {
99  sprintf(buf, "IND_%s_%s_%c",
100  argv[i], (_pk ? "PK" : "FULL"), (_ordered ? 'O' : 'U'));
101  ind.setName(buf);
102  }
103  else
104  {
105  ind.setName(_iname);
106  }
107 
108  ind.setTable(tabName);
109 
110  if (!_tname)
111  {
112  ndbout << "creating index " << ind.getName() << " on table " << tabName
113  << "(";
114  for(int c = 0; c<tab->getNoOfColumns(); c++){
115  if(!_pk || tab->getColumn(c)->getPrimaryKey())
116  {
117  ndbout << tab->getColumn(c)->getName() << ", ";
118  ind.addIndexColumn(tab->getColumn(c)->getName());
119  }
120  }
121  ndbout << ")" << endl;
122  }
123  else
124  {
125  /* Treat args as column names */
126  ndbout << "creating index " << ind.getName() << " on table " << tabName
127  << "(";
128  for(int argNum=i; argNum < argc; argNum++)
129  {
130  const char* colName= argv[argNum];
131  if (tab->getColumn(colName) == NULL)
132  {
133  g_err << "Column " << colName << " does not exist in table " << tabName
134  << endl;
135  return NDBT_ProgramExit(NDBT_FAILED);
136  }
137  ndbout << colName << ", ";
138  ind.addIndexColumn(colName);
139  }
140  ndbout << ")" << endl;
141  }
142  const int res = dict->createIndex(ind);
143  if(res != 0)
144  ndbout << endl << dict->getNdbError() << endl;
145  else
146  ndbout << "OK" << endl;
147 
148  if (_tname) // Just create a single index
149  return NDBT_ProgramExit(NDBT_OK);
150  }
151 
152  return NDBT_ProgramExit(NDBT_OK);
153 }
154 
155