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