56 #define PRINT_ERROR(code,msg) \
57 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
58 << ", code: " << code \
59 << ", msg: " << msg << "." << std::endl
60 #define MYSQLERROR(mysql) { \
61 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
63 #define APIERROR(error) { \
64 PRINT_ERROR(error.code,error.message); \
67 int main(
int argc,
char** argv)
71 std::cout <<
"Arguments are <socket mysqld1> <connect_string cluster 1> <socket mysqld2> <connect_string cluster 2>.\n";
77 char * mysqld1_sock = argv[1];
78 const char *connectstring1 = argv[2];
79 char * mysqld2_sock = argv[3];
80 const char *connectstring2 = argv[4];
91 if (cluster1_connection.connect(4 ,
95 std::cout <<
"Cluster 1 management server was not ready within 30 secs.\n";
99 if (cluster1_connection.wait_until_ready(30,0) < 0)
101 std::cout <<
"Cluster 1 was not ready within 30 secs.\n";
105 if ( !mysql_init(&mysql1) ) {
106 std::cout <<
"mysql_init failed\n";
109 if ( !mysql_real_connect(&mysql1,
"localhost",
"root",
"",
"",
110 0, mysqld1_sock, 0) )
117 if (cluster2_connection.connect(4 ,
121 std::cout <<
"Cluster 2 management server was not ready within 30 secs.\n";
125 if (cluster2_connection.wait_until_ready(30,0) < 0)
127 std::cout <<
"Cluster 2 was not ready within 30 secs.\n";
131 if ( !mysql_init(&mysql2) ) {
132 std::cout <<
"mysql_init failed\n";
135 if ( !mysql_real_connect(&mysql2,
"localhost",
"root",
"",
"",
136 0, mysqld2_sock, 0) )
140 run_application(mysql1, cluster1_connection,
"api_simple_dual_1",
"ndb_examples");
141 run_application(mysql2, cluster2_connection,
"api_simple_dual_2",
"ndb_examples");
150 static void do_insert(
Ndb &,
const char*
table);
151 static void do_update(
Ndb &,
const char*
table);
152 static void do_delete(
Ndb &,
const char*
table);
153 static void do_read(
Ndb &,
const char*
table);
155 static void run_application(
MYSQL &mysql,
164 sprintf(db_stmt,
"CREATE DATABASE %s\n", db);
165 mysql_query(&mysql, db_stmt);
166 sprintf(db_stmt,
"USE %s", db);
167 if (mysql_query(&mysql, db_stmt) != 0) MYSQLERROR(mysql);
174 Ndb myNdb( &cluster_connection, db );
175 if (myNdb.init()) APIERROR(myNdb.getNdbError());
180 do_insert(myNdb, table);
181 do_update(myNdb, table);
182 do_delete(myNdb, table);
183 do_read(myNdb, table);
187 mysql_query(&mysql, db_stmt);
195 char create_stmt[256];
197 sprintf(create_stmt,
"CREATE TABLE %s \
198 (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,\
199 ATTR2 INT UNSIGNED NOT NULL)\
201 if (mysql_query(&mysql, create_stmt))
209 static void do_insert(
Ndb &myNdb,
const char* table)
217 for (
int i = 0;
i < 5;
i++) {
219 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
222 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
224 myOperation->insertTuple();
225 myOperation->equal(
"ATTR1",
i);
226 myOperation->setValue(
"ATTR2",
i);
229 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
231 myOperation->insertTuple();
232 myOperation->equal(
"ATTR1",
i+5);
233 myOperation->setValue(
"ATTR2",
i+5);
245 static void do_update(
Ndb &myNdb,
const char* table)
253 for (
int i = 0;
i < 10;
i+=2) {
255 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
258 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
260 myOperation->updateTuple();
261 myOperation->equal(
"ATTR1",
i );
262 myOperation->setValue(
"ATTR2",
i+10);
274 static void do_delete(
Ndb &myNdb,
const char* table)
283 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
285 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
286 if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
288 myOperation->deleteTuple();
289 myOperation->equal(
"ATTR1", 3 );
292 APIERROR(myTransaction->getNdbError());
300 static void do_read(
Ndb &myNdb,
const char* table)
308 std::cout <<
"ATTR1 ATTR2" << std::endl;
310 for (
int i = 0;
i < 10;
i++) {
312 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
315 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
318 myOperation->equal(
"ATTR1",
i);
320 NdbRecAttr *myRecAttr= myOperation->getValue(
"ATTR2", NULL);
321 if (myRecAttr == NULL) APIERROR(myTransaction->
getNdbError());
325 std::cout <<
"Detected that deleted tuple doesn't exist!" << std::endl;