~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/test/ndbapi/create_tab.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <ndb_global.h>
 
17
 
 
18
#include <NdbOut.hpp>
 
19
#include <NdbApi.hpp>
 
20
#include <NDBT.hpp>
 
21
 
 
22
#include <getarg.h>
 
23
 
 
24
static int g_diskbased = 0;
 
25
static const char* g_tsname = 0;
 
26
 
 
27
static int
 
28
g_create_hook(Ndb* ndb, NdbDictionary::Table& tab, int when, void* arg)
 
29
{
 
30
  if (when == 0) {
 
31
    if (g_diskbased) {
 
32
      for (int i = 0; i < tab.getNoOfColumns(); i++) {
 
33
        NdbDictionary::Column* col = tab.getColumn(i);
 
34
        if (! col->getPrimaryKey()) {
 
35
          col->setStorageType(NdbDictionary::Column::StorageTypeDisk);
 
36
        }
 
37
      }
 
38
    }
 
39
    if (g_tsname != NULL) {
 
40
      tab.setTablespaceName(g_tsname);
 
41
    }
 
42
  }
 
43
  return 0;
 
44
}
 
45
 
 
46
int main(int argc, const char** argv){
 
47
  ndb_init();
 
48
 
 
49
  int _temp = false;
 
50
  int _help = 0;
 
51
  int _all = 0;
 
52
  int _print = 0;
 
53
  const char* _connectstr = NULL;
 
54
  int _diskbased = 0;
 
55
  const char* _tsname = NULL;
 
56
 
 
57
  struct getargs args[] = {
 
58
    { "all", 'a', arg_flag, &_all, "Create/print all tables", 0 },
 
59
    { "print", 'p', arg_flag, &_print, "Print table(s) instead of creating it", 0},
 
60
    { "temp", 't', arg_flag, &_temp, "Temporary table", 0 },
 
61
    { "connstr", 'c', arg_string, &_connectstr, "Connect string", "cs" }, 
 
62
    { "diskbased", 0, arg_flag, &_diskbased, "Store attrs on disk if possible", 0 },
 
63
    { "tsname", 0, arg_string, &_tsname, "Tablespace name", "ts" },
 
64
    { "usage", '?', arg_flag, &_help, "Print help", "" }
 
65
  };
 
66
  int num_args = sizeof(args) / sizeof(args[0]);
 
67
  int optind = 0;
 
68
  char desc[] = 
 
69
    "tabname\n"\
 
70
    "This program will create one table in Ndb.\n"\
 
71
    "The tables may be selected from a fixed list of tables\n"\
 
72
    "defined in NDBT_Tables class\n";
 
73
 
 
74
  if(getarg(args, num_args, argc, argv, &optind) || _help){
 
75
    arg_printusage(args, num_args, argv[0], desc);
 
76
    return NDBT_ProgramExit(NDBT_WRONGARGS);
 
77
  }
 
78
 
 
79
  if(argv[optind] == NULL && !_all){
 
80
    arg_printusage(args, num_args, argv[0], desc);
 
81
    return NDBT_ProgramExit(NDBT_WRONGARGS);
 
82
  }
 
83
 
 
84
  g_diskbased = _diskbased;
 
85
  g_tsname = _tsname;
 
86
 
 
87
  int res = 0;
 
88
  if(_print){
 
89
    /**
 
90
     * Print instead of creating
 
91
     */
 
92
    if(optind < argc){
 
93
      for(int i = optind; i<argc; i++){
 
94
        NDBT_Tables::print(argv[i]);
 
95
      }
 
96
    } else {
 
97
      NDBT_Tables::printAll();
 
98
    }
 
99
  } else {
 
100
    /**
 
101
     * Creating
 
102
     */
 
103
    
 
104
    // Connect to Ndb
 
105
    Ndb_cluster_connection con(_connectstr);
 
106
    if(con.connect(12, 5, 1) != 0)
 
107
    {
 
108
      return NDBT_ProgramExit(NDBT_FAILED);
 
109
    }
 
110
    Ndb MyNdb(&con, "TEST_DB" );
 
111
    
 
112
    if(MyNdb.init() != 0){
 
113
      ERR(MyNdb.getNdbError());
 
114
      return NDBT_ProgramExit(NDBT_FAILED);
 
115
    }
 
116
    
 
117
    while(MyNdb.waitUntilReady() != 0)
 
118
      ndbout << "Waiting for ndb to become ready..." << endl;
 
119
    
 
120
    if(_all){
 
121
      res = NDBT_Tables::createAllTables(&MyNdb, _temp);
 
122
    } else {
 
123
      int tmp;
 
124
      for(int i = optind; i<argc; i++){
 
125
        ndbout << "Trying to create " <<  argv[i] << endl;
 
126
        if((tmp = NDBT_Tables::createTable(&MyNdb, argv[i], _temp, false,
 
127
                                           g_create_hook)) != 0)
 
128
          res = tmp;
 
129
      }
 
130
    } 
 
131
  }
 
132
  
 
133
  if(res != 0)
 
134
    return NDBT_ProgramExit(NDBT_FAILED);
 
135
  else
 
136
    return NDBT_ProgramExit(NDBT_OK);
 
137
}