mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / test / tools / hugoPkReadRecord.cpp
blobef38cec3f13e49134825e67873ccfddb0bb5f026
1 /* Copyright (c) 2003-2005 MySQL AB
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include <ndb_global.h>
18 #include <NdbOut.hpp>
19 #include <NdbSleep.h>
20 #include <NDBT_Tables.hpp>
21 #include <getarg.h>
22 #include <NDBT.hpp>
23 #include <Ndb.hpp>
24 #include <NdbDictionary.hpp>
26 //extern NdbOut g_info;
28 int main(int argc, const char** argv)
30 ndb_init();
31 int _row = 0;
32 int _hex = 0;
33 int _primaryKey = 0;
34 const char* _tableName = NULL;
36 struct getargs args[] = {
37 { "row", 'r',
38 arg_integer, &_row, "The row number", "row" },
39 { "primarykey", 'p',
40 arg_integer, &_primaryKey, "The primary key", "primarykey" },
41 { "hex", 'h',
42 arg_flag, &_hex, "Print hex", "hex" }
45 int num_args = sizeof(args) / sizeof(args[0]);
46 int optind = 0, i;
48 if(getarg(args, num_args, argc, argv, &optind) || argv[optind] == NULL) {
49 arg_printusage(args, num_args, argv[0], "table name\n");
50 return NDBT_WRONGARGS;
52 // Check if table name is supplied
53 if (argv[optind] != NULL)
54 _tableName = argv[optind];
57 const NdbDictionary::Table* table = NDBT_Tables::getTable(_tableName);
58 // const NDBT_Attribute* attribute = table->getAttribute(_column);
60 g_info << "Table " << _tableName << endl
61 << "Row: " << _row << ", PrimaryKey: " << _primaryKey
62 << endl;
64 Ndb_cluster_connection con;
65 if(con.connect(12, 5, 1) != 0)
67 return NDBT_ProgramExit(NDBT_FAILED);
69 Ndb* ndb = new Ndb(&con, "TEST_DB");
70 if (ndb->init() == 0 && ndb->waitUntilReady(30) == 0)
72 NdbConnection* conn = ndb->startTransaction();
73 if (conn == NULL)
75 g_info << "ERROR: " << ndb->getNdbError() << endl;
76 delete ndb;
77 return -1;
79 NdbOperation* op = conn->getNdbOperation(_tableName);
80 if (op == NULL)
82 g_info << "ERROR: " << conn->getNdbError() << endl;
83 delete ndb;
84 return -1;
86 op->readTuple();
87 NdbRecAttr** data = new NdbRecAttr*[table->getNoOfColumns()];
88 for (i = 0; i < table->getNoOfColumns(); i++)
90 const NdbDictionary::Column* c = table->getColumn(i);
91 if (c->getPrimaryKey())
93 op->equal(c->getName(), _primaryKey);
94 data[i] = op->getValue(c->getName(), NULL);
96 else
98 data[i] = op->getValue(c->getName(), NULL);
101 if (conn->execute(Commit) == 0)
103 // Print column names
104 for (i = 0; i < table->getNoOfColumns(); i++)
106 const NdbDictionary::Column* c = table->getColumn(i);
108 g_info
109 << c->getName()
110 << "[" << c->getType() << "] ";
112 g_info << endl;
114 if (_hex)
116 g_info << hex;
118 for (i = 0; i < table->getNoOfColumns(); i++)
120 NdbRecAttr* a = data[i];
121 ndbout << (* a) << " ";
122 } // for
123 g_info << endl;
124 } // if (conn
125 else
127 g_info << "Failed to commit read transaction... "
128 << conn->getNdbError()
129 << ", commitStatus = " << conn->commitStatus()
130 << endl;
133 delete[] data;
135 ndb->closeTransaction(conn);
136 } // if (ndb.init
137 else
139 g_info << "ERROR: Unable to connect to NDB, "
140 << ndb->getNdbError() << endl;
142 delete ndb;
144 return 0;