MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hugoPkReadRecord.cpp
1 /*
2  Copyright (C) 2003-2006 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 <NdbSleep.h>
23 #include <NDBT_Tables.hpp>
24 #include <getarg.h>
25 #include <NDBT.hpp>
26 #include <Ndb.hpp>
27 #include <NdbDictionary.hpp>
28 
29 //extern NdbOut g_info;
30 
31 int main(int argc, const char** argv)
32 {
33  ndb_init();
34  int _row = 0;
35  int _hex = 0;
36  int _primaryKey = 0;
37  const char* _tableName = NULL;
38 
39  struct getargs args[] = {
40  { "row", 'r',
41  arg_integer, &_row, "The row number", "row" },
42  { "primarykey", 'p',
43  arg_integer, &_primaryKey, "The primary key", "primarykey" },
44  { "hex", 'h',
45  arg_flag, &_hex, "Print hex", "hex" }
46  };
47 
48  int num_args = sizeof(args) / sizeof(args[0]);
49  int optind = 0, i;
50 
51  if(getarg(args, num_args, argc, argv, &optind) || argv[optind] == NULL) {
52  arg_printusage(args, num_args, argv[0], "table name\n");
53  return NDBT_WRONGARGS;
54  }
55  // Check if table name is supplied
56  if (argv[optind] != NULL)
57  _tableName = argv[optind];
58 
59 
60  const NdbDictionary::Table* table = NDBT_Tables::getTable(_tableName);
61  // const NDBT_Attribute* attribute = table->getAttribute(_column);
62 
63  g_info << "Table " << _tableName << endl
64  << "Row: " << _row << ", PrimaryKey: " << _primaryKey
65  << endl;
66 
68  if(con.connect(12, 5, 1) != 0)
69  {
70  return NDBT_ProgramExit(NDBT_FAILED);
71  }
72  Ndb* ndb = new Ndb(&con, "TEST_DB");
73  if (ndb->init() == 0 && ndb->waitUntilReady(30) == 0)
74  {
76  if (conn == NULL)
77  {
78  g_info << "ERROR: " << ndb->getNdbError() << endl;
79  delete ndb;
80  return -1;
81  }
82  NdbOperation* op = conn->getNdbOperation(_tableName);
83  if (op == NULL)
84  {
85  g_info << "ERROR: " << conn->getNdbError() << endl;
86  delete ndb;
87  return -1;
88  }
89  op->readTuple();
90  NdbRecAttr** data = new NdbRecAttr*[table->getNoOfColumns()];
91  for (i = 0; i < table->getNoOfColumns(); i++)
92  {
93  const NdbDictionary::Column* c = table->getColumn(i);
94  if (c->getPrimaryKey())
95  {
96  op->equal(c->getName(), _primaryKey);
97  data[i] = op->getValue(c->getName(), NULL);
98  }
99  else
100  {
101  data[i] = op->getValue(c->getName(), NULL);
102  }
103  }
104  if (conn->execute(Commit) == 0)
105  {
106  // Print column names
107  for (i = 0; i < table->getNoOfColumns(); i++)
108  {
109  const NdbDictionary::Column* c = table->getColumn(i);
110 
111  g_info
112  << c->getName()
113  << "[" << c->getType() << "] ";
114  }
115  g_info << endl;
116 
117  if (_hex)
118  {
119  g_info << hex;
120  }
121  for (i = 0; i < table->getNoOfColumns(); i++)
122  {
123  NdbRecAttr* a = data[i];
124  ndbout << (* a) << " ";
125  } // for
126  g_info << endl;
127  } // if (conn
128  else
129  {
130  g_info << "Failed to commit read transaction... "
131  << conn->getNdbError()
132  << ", commitStatus = " << conn->commitStatus()
133  << endl;
134  }
135 
136  delete[] data;
137 
138  ndb->closeTransaction(conn);
139  } // if (ndb.init
140  else
141  {
142  g_info << "ERROR: Unable to connect to NDB, "
143  << ndb->getNdbError() << endl;
144  }
145  delete ndb;
146 
147  return 0;
148 }