46 #define PRINT_ERROR(code,msg) \
47 ndbout << "Error in " << __FILE__ << ", line: " << __LINE__ \
48 << ", code: " << code \
49 << ", msg: " << msg << "." << endl
50 #define MYSQLERROR(mysql) { \
51 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
53 #define APIERROR(error) { \
54 PRINT_ERROR(error.code,error.message); \
68 if (cluster_connection.
connect(4 ,
72 ndbout <<
"Cluster management server was not ready within 30 secs.\n";
79 ndbout <<
"Cluster was not ready within 30 secs.\n";
85 if ( !mysql_init(&mysql) ) {
86 ndbout <<
"mysql_init failed\n";
89 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
90 3306,
"/tmp/mysql.sock", 0) )
94 run_application(mysql, cluster_connection);
99 ndbout <<
"\nTo drop created table use:\n"
100 <<
"echo \"drop table MYTABLENAME\" | mysql TEST_DB_1 -u root\n";
106 static void do_insert(
Ndb &);
107 static void do_update(
Ndb &);
108 static void do_delete(
Ndb &);
109 static void do_read(
Ndb &);
111 static void run_application(
MYSQL &mysql,
117 mysql_query(&mysql,
"CREATE DATABASE TEST_DB_1");
118 if (mysql_query(&mysql,
"USE TEST_DB_1") != 0) MYSQLERROR(mysql);
125 Ndb myNdb( &cluster_connection,
"TEST_DB_1" );
126 if (myNdb.init()) APIERROR(myNdb.getNdbError());
142 if (mysql_query(&mysql,
145 " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
146 " ATTR2 INT UNSIGNED NOT NULL)"
154 static void do_insert(
Ndb &myNdb)
162 for (
int i = 0;
i < 5;
i++) {
164 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
167 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
169 myOperation->insertTuple();
170 myOperation->equal(
"ATTR1",
i);
171 myOperation->setValue(
"ATTR2",
i);
174 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
176 myOperation->insertTuple();
177 myOperation->equal(
"ATTR1",
i+5);
178 myOperation->setValue(
"ATTR2",
i+5);
190 static void do_update(
Ndb &myNdb)
198 for (
int i = 0;
i < 10;
i+=2) {
200 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
203 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
205 myOperation->updateTuple();
206 myOperation->equal(
"ATTR1",
i );
207 myOperation->setValue(
"ATTR2",
i+10);
219 static void do_delete(
Ndb &myNdb)
228 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
230 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
231 if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
233 myOperation->deleteTuple();
234 myOperation->equal(
"ATTR1", 3 );
237 APIERROR(myTransaction->getNdbError());
245 static void do_read(
Ndb &myNdb)
253 ndbout <<
"ATTR1 ATTR2" << endl;
255 for (
int i = 0;
i < 10;
i++) {
257 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
260 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
263 myOperation->equal(
"ATTR1",
i);
265 NdbRecAttr *myRecAttr= myOperation->getValue(
"ATTR2", NULL);
266 if (myRecAttr == NULL) APIERROR(myTransaction->
getNdbError());
271 ndbout <<
"Detected that deleted tuple doesn't exist!" << endl;