MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
copy_tab.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 #include "UtilTransactions.hpp"
25 
26 #include <getarg.h>
27 
28 int main(int argc, const char** argv){
29  ndb_init();
30 
31  const char* _tabname = NULL;
32  const char* _dbname = "TEST_DB";
33  const char* _connectstr = NULL;
34  int _copy_data = true;
35  int _help = 0;
36 
37  struct getargs args[] = {
38  { "database", 'd', arg_string, &_dbname, "dbname",
39  "Name of database table is in"},
40  { "connstr", 'c', arg_string, &_connectstr, "connect string",
41  "How to connect to NDB"},
42  { "copy-data", '\0', arg_negative_flag, &_copy_data, "Don't copy data to new table",
43  "How to connect to NDB"},
44  { "usage", '?', arg_flag, &_help, "Print help", "" }
45  };
46  int num_args = sizeof(args) / sizeof(args[0]);
47  int optind = 0;
48  char desc[] =
49  "srctab desttab\n"\
50  "This program will copy one table in Ndb\n";
51 
52  if(getarg(args, num_args, argc, argv, &optind) ||
53  argv[optind] == NULL || argv[optind + 1] == NULL || _help){
54  arg_printusage(args, num_args, argv[0], desc);
55  return NDBT_ProgramExit(NDBT_WRONGARGS);
56  }
57  _tabname = argv[optind];
58 
59  Ndb_cluster_connection con(_connectstr);
60  if(con.connect(12, 5, 1) != 0)
61  {
62  return NDBT_ProgramExit(NDBT_FAILED);
63  }
64  Ndb MyNdb(&con,_dbname);
65  if(MyNdb.init() != 0){
66  ERR(MyNdb.getNdbError());
67  return NDBT_ProgramExit(NDBT_FAILED);
68  }
69 
70  while(MyNdb.waitUntilReady() != 0)
71  ndbout << "Waiting for ndb to become ready..." << endl;
72 
73  const NdbDictionary::Table* ptab = MyNdb.getDictionary()->getTable(_tabname);
74  if (ptab == 0)
75  {
76  ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
77  return NDBT_ProgramExit(NDBT_FAILED);
78  }
79 
81  {
83  if (MyNdb.getDictionary()->listIndexes(list, *ptab) != 0)
84  {
85  ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
86  return NDBT_ProgramExit(NDBT_FAILED);
87  }
88  for (unsigned i = 0; i<list.count; i++)
89  {
90  const NdbDictionary::Index* idx =
91  MyNdb.getDictionary()->getIndex(list.elements[i].name,
92  _tabname);
93  if (idx)
94  {
95  ndbout << " found index " << list.elements[i].name << endl;
97  copy->setName(idx->getName());
98  copy->setType(idx->getType());
99  copy->setLogging(idx->getLogging());
100  for (unsigned j = 0; j<idx->getNoOfColumns(); j++)
101  {
102  copy->addColumn(idx->getColumn(j)->getName());
103  }
104  indexes.push_back(copy);
105  }
106  }
107  }
108  for (int i = optind + 1; i<argc; i++)
109  {
110  const char *_to_tabname = argv[i];
111  ndbout << "Copying table " << _tabname << " to " << _to_tabname << "...";
112  NdbDictionary::Table tab2(*ptab);
113  tab2.setName(_to_tabname);
114  if (MyNdb.getDictionary()->beginSchemaTrans() != 0)
115  {
116  ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
117  return NDBT_ProgramExit(NDBT_FAILED);
118  }
119  if (MyNdb.getDictionary()->createTable(tab2) != 0){
120  ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
121  return NDBT_ProgramExit(NDBT_FAILED);
122  }
123 
124  for (unsigned j = 0; j<indexes.size(); j++)
125  {
126  NdbDictionary::Index * idx = indexes[j];
127  idx->setTable(_to_tabname);
128  int res = MyNdb.getDictionary()->createIndex(*idx);
129  if (res != 0)
130  {
131  ndbout << "Failed to create index: " << idx->getName() << " : "
132  << MyNdb.getDictionary()->getNdbError() << endl;
133  return NDBT_ProgramExit(NDBT_FAILED);
134  }
135  }
136 
137  if (MyNdb.getDictionary()->endSchemaTrans() != 0)
138  {
139  ndbout << endl << MyNdb.getDictionary()->getNdbError() << endl;
140  return NDBT_ProgramExit(NDBT_FAILED);
141  }
142 
143  ndbout << "OK" << endl;
144  if (_copy_data){
145  ndbout << "Copying data..."<<endl;
146  const NdbDictionary::Table * tab3 =
147  NDBT_Table::discoverTableFromDb(&MyNdb,
148  _tabname);
149  UtilTransactions util(*tab3);
150 
151  if(util.copyTableData(&MyNdb,
152  _to_tabname) != NDBT_OK){
153  return NDBT_ProgramExit(NDBT_FAILED);
154  }
155  ndbout << "OK" << endl;
156  }
157  }
158 
159  for (unsigned j = 0; j<indexes.size(); j++)
160  {
161  delete indexes[j];
162  }
163 
164  return NDBT_ProgramExit(NDBT_OK);
165 }
166 
167 template class Vector<NdbDictionary::Index*>;