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