1 /*
2    Copyright (C) 2003-2006 MySQL AB, 2009 Sun Microsystems, Inc.
3     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, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 #include <ndb_global.h>
27 
28 #include <NdbOut.hpp>
29 #include <NdbApi.hpp>
30 #include <NDBT.hpp>
31 #include "UtilTransactions.hpp"
32 
33 #include <getarg.h>
34 
main(int argc,const char ** argv)35 int main(int argc, const char** argv){
36   ndb_init();
37 
38   const char* _tabname = NULL;
39   const char* _dbname = "TEST_DB";
40   const char* _connectstr = NULL;
41   int _copy_data = true;
42   int _help = 0;
43 
44   struct getargs args[] = {
45     { "database", 'd', arg_string, &_dbname, "dbname",
46       "Name of database table is in"},
47     { "connstr", 'c', arg_string, &_connectstr, "connect string",
48       "How to connect to NDB"},
49     { "copy-data", '\0', arg_negative_flag, &_copy_data, "Don't copy data to new table",
50       "How to connect to NDB"},
51     { "usage", '?', arg_flag, &_help, "Print help", "" }
52   };
53   int num_args = sizeof(args) / sizeof(args[0]);
54   int optind = 0;
55   char desc[] =
56     "srctab desttab\n"\
57     "This program will copy one table in Ndb\n";
58 
59   if(getarg(args, num_args, argc, argv, &optind) ||
60      argv[optind] == NULL || argv[optind + 1] == NULL || _help){
61     arg_printusage(args, num_args, argv[0], desc);
62     return NDBT_ProgramExit(NDBT_WRONGARGS);
63   }
64   _tabname = argv[optind];
65 
66   Ndb_cluster_connection con(_connectstr);
67   if(con.connect(12, 5, 1) != 0)
68   {
69     return NDBT_ProgramExit(NDBT_FAILED);
70   }
71   Ndb MyNdb(&con,_dbname);
72   if(MyNdb.init() != 0){
73     NDB_ERR(MyNdb.getNdbError());
74     return NDBT_ProgramExit(NDBT_FAILED);
75   }
76 
77   while(MyNdb.waitUntilReady() != 0)
78     ndbout << "Waiting for ndb to become ready..." << endl;
79 
80   const NdbDictionary::Table* ptab = MyNdb.getDictionary()->getTable(_tabname);
81   if (ptab == 0)
82   {
83     ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
84     return NDBT_ProgramExit(NDBT_FAILED);
85   }
86 
87   Vector<NdbDictionary::Index*> indexes;
88   {
89     NdbDictionary::Dictionary::List list;
90     if (MyNdb.getDictionary()->listIndexes(list, *ptab) != 0)
91     {
92       ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
93       return NDBT_ProgramExit(NDBT_FAILED);
94     }
95     for (unsigned i = 0; i<list.count; i++)
96     {
97       const NdbDictionary::Index* idx =
98         MyNdb.getDictionary()->getIndex(list.elements[i].name,
99                                         _tabname);
100       if (idx)
101       {
102         ndbout << " found index " << list.elements[i].name << endl;
103         NdbDictionary::Index * copy = new NdbDictionary::Index();
104         copy->setName(idx->getName());
105         copy->setType(idx->getType());
106         copy->setLogging(idx->getLogging());
107         for (unsigned j = 0; j<idx->getNoOfColumns(); j++)
108         {
109           copy->addColumn(idx->getColumn(j)->getName());
110         }
111         indexes.push_back(copy);
112       }
113     }
114   }
115   for (int i = optind + 1; i<argc; i++)
116   {
117     const char *_to_tabname = argv[i];
118     ndbout << "Copying table " <<  _tabname << " to " << _to_tabname << "...";
119     NdbDictionary::Table tab2(*ptab);
120     tab2.setName(_to_tabname);
121     if (MyNdb.getDictionary()->beginSchemaTrans() != 0)
122     {
123       ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
124       return NDBT_ProgramExit(NDBT_FAILED);
125     }
126     if (MyNdb.getDictionary()->createTable(tab2) != 0){
127       ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
128       return NDBT_ProgramExit(NDBT_FAILED);
129     }
130 
131     for (unsigned j = 0; j<indexes.size(); j++)
132     {
133       NdbDictionary::Index * idx = indexes[j];
134       idx->setTable(_to_tabname);
135       int res = MyNdb.getDictionary()->createIndex(*idx);
136       if (res != 0)
137       {
138         ndbout << "Failed to create index: " << idx->getName() << " : "
139                << MyNdb.getDictionary()->getNdbError() << endl;
140         return NDBT_ProgramExit(NDBT_FAILED);
141       }
142     }
143 
144     if (MyNdb.getDictionary()->endSchemaTrans() != 0)
145     {
146       ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
147       return NDBT_ProgramExit(NDBT_FAILED);
148     }
149 
150     ndbout << "OK" << endl;
151     if (_copy_data){
152       ndbout << "Copying data..."<<endl;
153       const NdbDictionary::Table * tab3 =
154         NDBT_Table::discoverTableFromDb(&MyNdb,
155                                         _tabname);
156       UtilTransactions util(*tab3);
157 
158       if(util.copyTableData(&MyNdb,
159                             _to_tabname) != NDBT_OK){
160         return NDBT_ProgramExit(NDBT_FAILED);
161       }
162       ndbout << "OK" << endl;
163     }
164   }
165 
166   for (unsigned j = 0; j<indexes.size(); j++)
167   {
168     delete indexes[j];
169   }
170 
171   return NDBT_ProgramExit(NDBT_OK);
172 }
173 
174 template class Vector<NdbDictionary::Index*>;
175