mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / test / tools / create_index.cpp
blob6098a681cb2eae9a2ef917ffa61cccb87734985d
1 /* Copyright (c) 2003-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include <ndb_global.h>
18 #include <NdbOut.hpp>
19 #include <NdbApi.hpp>
20 #include <NDBT.hpp>
22 #include <getarg.h>
26 int
27 main(int argc, const char** argv){
28 ndb_init();
30 const char* _dbname = "TEST_DB";
31 int _help = 0;
32 int _ordered = 0, _pk = 1;
34 struct getargs args[] = {
35 { "database", 'd', arg_string, &_dbname, "dbname",
36 "Name of database table is in"},
37 { "ordered", 'o', arg_flag, &_ordered, "Create ordered index", "" },
38 { "pk", 'p', arg_flag, &_pk, "Create index on primary key", "" },
39 { "usage", '?', arg_flag, &_help, "Print help", "" }
42 int num_args = sizeof(args) / sizeof(args[0]);
43 int optind = 0;
44 char desc[] =
45 "<tabname>+\n"\
46 "This program will create one unique hash index named ind_<tabname> "
47 " for each table. The index will contain all columns in the table";
49 if(getarg(args, num_args, argc, argv, &optind) || _help ||
50 argv[optind] == NULL){
51 arg_printusage(args, num_args, argv[0], desc);
52 return NDBT_ProgramExit(NDBT_WRONGARGS);
55 Ndb_cluster_connection con;
56 if(con.connect(12, 5, 1) != 0)
58 return NDBT_ProgramExit(NDBT_FAILED);
61 Ndb MyNdb(&con, _dbname);
62 if(MyNdb.init() != 0){
63 ERR(MyNdb.getNdbError());
64 return NDBT_ProgramExit(NDBT_FAILED);
67 while(MyNdb.waitUntilReady() != 0)
68 ndbout << "Waiting for ndb to become ready..." << endl;
70 NdbDictionary::Dictionary * dict = MyNdb.getDictionary();
72 for(int i = optind; i<argc; i++){
73 const NdbDictionary::Table * tab = dict->getTable(argv[i]);
74 if(tab == 0){
75 g_err << "Unknown table: " << argv[i] << endl;
76 continue;
79 if(tab->getNoOfColumns() > 16){
80 g_err << "Table " << argv[i] << " has more than 16 columns" << endl;
83 NdbDictionary::Index ind;
84 if(_ordered){
85 ind.setType(NdbDictionary::Index::OrderedIndex);
86 ind.setLogging(false);
87 } else {
88 ind.setType(NdbDictionary::Index::UniqueHashIndex);
90 char buf[512];
91 sprintf(buf, "IND_%s_%s_%c",
92 argv[i], (_pk ? "PK" : "FULL"), (_ordered ? 'O' : 'U'));
93 ind.setName(buf);
94 ind.setTable(argv[i]);
95 for(int c = 0; c<tab->getNoOfColumns(); c++){
96 if(!_pk || tab->getColumn(c)->getPrimaryKey())
97 ind.addIndexColumn(tab->getColumn(c)->getName());
99 ndbout << "creating index " << buf << " on table " << argv[i] << "...";
100 const int res = dict->createIndex(ind);
101 if(res != 0)
102 ndbout << endl << dict->getNdbError() << endl;
103 else
104 ndbout << "OK" << endl;
107 return NDBT_ProgramExit(NDBT_OK);