38 #include <mysqld_error.h>
46 #define PRINT_ERROR(code,msg) \
47 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
48 << ", code: " << code \
49 << ", msg: " << msg << "." << std::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); \
57 int main(
int argc,
char** argv)
61 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
69 char * mysqld_sock = argv[1];
70 const char *connectstring = argv[2];
75 if (cluster_connection.connect(4 ,
79 std::cout <<
"Cluster management server was not ready within 30 secs.\n";
84 if (cluster_connection.wait_until_ready(30,0) < 0)
86 std::cout <<
"Cluster was not ready within 30 secs.\n";
92 if ( !mysql_init(&mysql) ) {
93 std::cout <<
"mysql_init failed\n";
96 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
101 run_application(mysql, cluster_connection);
110 static void do_insert(
Ndb &);
111 static void do_update(
Ndb &);
112 static void do_delete(
Ndb &);
113 static void do_read(
Ndb &);
115 static void run_application(
MYSQL &mysql,
121 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
122 if (mysql_query(&mysql,
"USE ndb_examples") != 0) MYSQLERROR(mysql);
129 Ndb myNdb( &cluster_connection,
"ndb_examples" );
130 if (myNdb.init()) APIERROR(myNdb.getNdbError());
146 while (mysql_query(&mysql,
149 " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
150 " ATTR2 INT UNSIGNED NOT NULL)"
153 if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR)
155 std::cout <<
"MySQL Cluster already has example table: api_simple. "
156 <<
"Dropping it..." << std::endl;
157 mysql_query(&mysql,
"DROP TABLE api_simple");
159 else MYSQLERROR(mysql);
166 static void do_insert(
Ndb &myNdb)
174 for (
int i = 0;
i < 5;
i++) {
176 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
179 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
181 myOperation->insertTuple();
182 myOperation->equal(
"ATTR1",
i);
183 myOperation->setValue(
"ATTR2",
i);
186 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
188 myOperation->insertTuple();
189 myOperation->equal(
"ATTR1",
i+5);
190 myOperation->setValue(
"ATTR2",
i+5);
202 static void do_update(
Ndb &myNdb)
210 for (
int i = 0;
i < 10;
i+=2) {
212 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
215 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
217 myOperation->updateTuple();
218 myOperation->equal(
"ATTR1",
i );
219 myOperation->setValue(
"ATTR2",
i+10);
231 static void do_delete(
Ndb &myNdb)
240 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
242 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable);
243 if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
245 myOperation->deleteTuple();
246 myOperation->equal(
"ATTR1", 3 );
249 APIERROR(myTransaction->getNdbError());
257 static void do_read(
Ndb &myNdb)
265 std::cout <<
"ATTR1 ATTR2" << std::endl;
267 for (
int i = 0;
i < 10;
i++) {
269 if (myTransaction == NULL) APIERROR(myNdb.
getNdbError());
272 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
275 myOperation->equal(
"ATTR1",
i);
277 NdbRecAttr *myRecAttr= myOperation->getValue(
"ATTR2", NULL);
278 if (myRecAttr == NULL) APIERROR(myTransaction->
getNdbError());
285 std::cout <<
"Detected that deleted tuple doesn't exist!" << std::endl;