1
/* Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
* ndbapi_simple.cpp: Using synchronous transactions in NDB API
19
* Correct output from this program is:
25
* Detected that deleted tuple doesn't exist!
41
static void run_application(MYSQL &, Ndb_cluster_connection &);
43
#define PRINT_ERROR(code,msg) \
44
std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
45
<< ", code: " << code \
46
<< ", msg: " << msg << "." << std::endl
47
#define MYSQLERROR(mysql) { \
48
PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
50
#define APIERROR(error) { \
51
PRINT_ERROR(error.code,error.message); \
54
int main(int argc, char** argv)
58
std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
61
// ndb_init must be called first
64
// connect to mysql server and cluster and run application
66
char * mysqld_sock = argv[1];
67
const char *connectstring = argv[2];
68
// Object representing the cluster
69
Ndb_cluster_connection cluster_connection(connectstring);
71
// Connect to cluster management server (ndb_mgmd)
72
if (cluster_connection.connect(4 /* retries */,
73
5 /* delay between retries */,
76
std::cout << "Cluster management server was not ready within 30 secs.\n";
80
// Optionally connect and wait for the storage nodes (ndbd's)
81
if (cluster_connection.wait_until_ready(30,0) < 0)
83
std::cout << "Cluster was not ready within 30 secs.\n";
87
// connect to mysql server
89
if ( !mysql_init(&mysql) ) {
90
std::cout << "mysql_init failed\n";
93
if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
97
// run the application code
98
run_application(mysql, cluster_connection);
106
static void create_table(MYSQL &);
107
static void drop_table(MYSQL &);
108
static void do_insert(Ndb &);
109
static void do_update(Ndb &);
110
static void do_delete(Ndb &);
111
static void do_read(Ndb &);
113
static void run_application(MYSQL &mysql,
114
Ndb_cluster_connection &cluster_connection)
116
/********************************************
117
* Connect to database via mysql-c *
118
********************************************/
119
mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
120
if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
123
/********************************************
124
* Connect to database via NdbApi *
125
********************************************/
126
// Object representing the database
127
Ndb myNdb( &cluster_connection, "TEST_DB_1" );
128
if (myNdb.init()) APIERROR(myNdb.getNdbError());
131
* Do different operations on database
138
mysql_query(&mysql, "DROP DATABASE TEST_DB_1");
141
/*********************************************************
142
* Create a table named MYTABLENAME if it does not exist *
143
*********************************************************/
144
static void create_table(MYSQL &mysql)
146
if (mysql_query(&mysql,
149
" (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
150
" ATTR2 INT UNSIGNED NOT NULL)"
155
/***********************************
156
* Drop a table named MYTABLENAME
157
***********************************/
158
static void drop_table(MYSQL &mysql)
160
if (mysql_query(&mysql,
166
/**************************************************************************
167
* Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
168
**************************************************************************/
169
static void do_insert(Ndb &myNdb)
171
const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
172
const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
175
APIERROR(myDict->getNdbError());
177
for (int i = 0; i < 5; i++) {
178
NdbTransaction *myTransaction= myNdb.startTransaction();
179
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
181
NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
182
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
184
myOperation->insertTuple();
185
myOperation->equal("ATTR1", i);
186
myOperation->setValue("ATTR2", i);
188
myOperation= myTransaction->getNdbOperation(myTable);
189
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
191
myOperation->insertTuple();
192
myOperation->equal("ATTR1", i+5);
193
myOperation->setValue("ATTR2", i+5);
195
if (myTransaction->execute( NdbTransaction::Commit ) == -1)
196
APIERROR(myTransaction->getNdbError());
198
myNdb.closeTransaction(myTransaction);
202
/*****************************************************************
203
* Update the second attribute in half of the tuples (adding 10) *
204
*****************************************************************/
205
static void do_update(Ndb &myNdb)
207
const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
208
const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
211
APIERROR(myDict->getNdbError());
213
for (int i = 0; i < 10; i+=2) {
214
NdbTransaction *myTransaction= myNdb.startTransaction();
215
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
217
NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
218
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
220
myOperation->updateTuple();
221
myOperation->equal( "ATTR1", i );
222
myOperation->setValue( "ATTR2", i+10);
224
if( myTransaction->execute( NdbTransaction::Commit ) == -1 )
225
APIERROR(myTransaction->getNdbError());
227
myNdb.closeTransaction(myTransaction);
231
/*************************************************
232
* Delete one tuple (the one with primary key 3) *
233
*************************************************/
234
static void do_delete(Ndb &myNdb)
236
const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
237
const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
240
APIERROR(myDict->getNdbError());
242
NdbTransaction *myTransaction= myNdb.startTransaction();
243
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
245
NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
246
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
248
myOperation->deleteTuple();
249
myOperation->equal( "ATTR1", 3 );
251
if (myTransaction->execute(NdbTransaction::Commit) == -1)
252
APIERROR(myTransaction->getNdbError());
254
myNdb.closeTransaction(myTransaction);
257
/*****************************
258
* Read and print all tuples *
259
*****************************/
260
static void do_read(Ndb &myNdb)
262
const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
263
const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME");
266
APIERROR(myDict->getNdbError());
268
std::cout << "ATTR1 ATTR2" << std::endl;
270
for (int i = 0; i < 10; i++) {
271
NdbTransaction *myTransaction= myNdb.startTransaction();
272
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
274
NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
275
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
277
myOperation->readTuple(NdbOperation::LM_Read);
278
myOperation->equal("ATTR1", i);
280
NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
281
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
283
if(myTransaction->execute( NdbTransaction::Commit ) == -1)
284
APIERROR(myTransaction->getNdbError());
286
if (myTransaction->getNdbError().classification == NdbError::NoDataFound)
288
std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
290
APIERROR(myTransaction->getNdbError());
293
printf(" %2d %2d\n", i, myRecAttr->u_32_value());
295
myNdb.closeTransaction(myTransaction);