MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
create_tab.cpp
1 /*
2  Copyright (C) 2003-2007 MySQL AB
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 static int g_diskbased = 0;
28 static const char* g_tsname = 0;
29 
30 static int
31 g_create_hook(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
32 {
33  if (when == 0) {
34  if (g_diskbased) {
35  for (int i = 0; i < tab.getNoOfColumns(); i++) {
36  NdbDictionary::Column* col = tab.getColumn(i);
37  if (! col->getPrimaryKey()) {
38  col->setStorageType(NdbDictionary::Column::StorageTypeDisk);
39  }
40  }
41  }
42  if (g_tsname != NULL) {
43  tab.setTablespaceName(g_tsname);
44  }
45  }
46  return 0;
47 }
48 
49 int main(int argc, const char** argv){
50  ndb_init();
51 
52  int _temp = false;
53  int _help = 0;
54  int _all = 0;
55  int _print = 0;
56  const char* _connectstr = NULL;
57  int _diskbased = 0;
58  const char* _tsname = NULL;
59  int _trans = false;
60 
61  struct getargs args[] = {
62  { "all", 'a', arg_flag, &_all, "Create/print all tables", 0 },
63  { "print", 'p', arg_flag, &_print, "Print table(s) instead of creating it", 0},
64  { "temp", 't', arg_flag, &_temp, "Temporary table", 0 },
65  { "trans", 'x', arg_flag, &_trans, "Use single schema trans", 0 },
66  { "connstr", 'c', arg_string, &_connectstr, "Connect string", "cs" },
67  { "diskbased", 0, arg_flag, &_diskbased, "Store attrs on disk if possible", 0 },
68  { "tsname", 0, arg_string, &_tsname, "Tablespace name", "ts" },
69  { "usage", '?', arg_flag, &_help, "Print help", "" }
70  };
71  int num_args = sizeof(args) / sizeof(args[0]);
72  int optind = 0;
73  char desc[] =
74  "tabname\n"\
75  "This program will create one table in Ndb.\n"\
76  "The tables may be selected from a fixed list of tables\n"\
77  "defined in NDBT_Tables class\n";
78 
79  if(getarg(args, num_args, argc, argv, &optind) || _help){
80  arg_printusage(args, num_args, argv[0], desc);
81  return NDBT_ProgramExit(NDBT_WRONGARGS);
82  }
83 
84  if(argv[optind] == NULL && !_all){
85  arg_printusage(args, num_args, argv[0], desc);
86  return NDBT_ProgramExit(NDBT_WRONGARGS);
87  }
88 
89  g_diskbased = _diskbased;
90  g_tsname = _tsname;
91 
92  int res = 0;
93  if(_print){
97  if(optind < argc){
98  for(int i = optind; i<argc; i++){
99  NDBT_Tables::print(argv[i]);
100  }
101  } else {
102  NDBT_Tables::printAll();
103  }
104  } else {
109  // Connect to Ndb
110  Ndb_cluster_connection con(_connectstr);
111  if(con.connect(12, 5, 1) != 0)
112  {
113  return NDBT_ProgramExit(NDBT_FAILED);
114  }
115  Ndb MyNdb(&con, "TEST_DB" );
116 
117  if(MyNdb.init() != 0){
118  ERR(MyNdb.getNdbError());
119  return NDBT_ProgramExit(NDBT_FAILED);
120  }
121 
122  while(MyNdb.waitUntilReady() != 0)
123  ndbout << "Waiting for ndb to become ready..." << endl;
124 
125  NdbDictionary::Dictionary* MyDic = MyNdb.getDictionary();
126 
127  if (_trans) {
128  if (MyDic->beginSchemaTrans() == -1) {
129  ERR(MyDic->getNdbError());
130  return NDBT_ProgramExit(NDBT_FAILED);
131  }
132  }
133 
134  if(_all){
135  res = NDBT_Tables::createAllTables(&MyNdb, _temp);
136  } else {
137  int tmp;
138  for(int i = optind; i<argc; i++){
139  ndbout << "Trying to create " << argv[i] << endl;
140  if((tmp = NDBT_Tables::createTable(&MyNdb, argv[i], _temp, false,
141  g_create_hook)) != 0)
142  res = tmp;
143  }
144  }
145 
146  if (_trans) {
147  if (MyDic->endSchemaTrans() == -1) {
148  ERR(MyDic->getNdbError());
149  return NDBT_ProgramExit(NDBT_FAILED);
150  }
151  }
152  }
153 
154  if(res != 0)
155  return NDBT_ProgramExit(NDBT_FAILED);
156  else
157  return NDBT_ProgramExit(NDBT_OK);
158 }