72 #include <mysqld_error.h>
84 milliSleep(
int milliseconds){
86 sleeptime.tv_sec = milliseconds / 1000;
87 sleeptime.tv_usec = (milliseconds - (sleeptime.tv_sec * 1000)) * 1000000;
88 select(0, 0, 0, 0, &sleeptime);
95 #define PRINT_ERROR(code,msg) \
96 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
97 << ", code: " << code \
98 << ", msg: " << msg << "." << std::endl
99 #define MYSQLERROR(mysql) { \
100 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
102 #define APIERROR(error) { \
103 PRINT_ERROR(error.code,error.message); \
112 Car() { memset(
this, 0,
sizeof(*
this)); }
122 void drop_table(
MYSQL &mysql)
124 if (mysql_query(&mysql,
"DROP TABLE IF EXISTS api_scan"))
134 while (mysql_query(&mysql,
137 " (REG_NO INT UNSIGNED NOT NULL,"
138 " BRAND CHAR(20) NOT NULL,"
139 " COLOR CHAR(20) NOT NULL,"
140 " PRIMARY KEY USING HASH (REG_NO))"
143 if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
145 std::cout <<
"MySQL Cluster already has example table: api_scan. "
146 <<
"Dropping it..." << std::endl;
151 int populate(
Ndb * myNdb)
165 for (i = 0; i < 5; i++)
168 sprintf(cars[i].brand,
"Mercedes");
169 sprintf(cars[i].color,
"Blue");
175 for (i = 5; i < 10; i++)
178 sprintf(cars[i].brand,
"BMW");
179 sprintf(cars[i].color,
"Black");
185 for (i = 10; i < 15; i++)
188 sprintf(cars[i].brand,
"Toyota");
189 sprintf(cars[i].color,
"Pink");
196 for (i = 0; i < 15; i++)
199 if (myNdbOperation == NULL)
202 myNdbOperation->
equal(
"REG_NO", cars[i].reg_no);
203 myNdbOperation->
setValue(
"BRAND", cars[i].brand);
204 myNdbOperation->
setValue(
"COLOR", cars[i].color);
214 int scan_delete(
Ndb* myNdb,
222 int retryAttempt = 0;
223 const int retryMax = 10;
247 if (retryAttempt >= retryMax)
249 std::cout <<
"ERROR: has retried this operation " << retryAttempt
250 <<
" times, failing!" << std::endl;
265 std::cout << err.
message << std::endl;
273 if (myScanOp == NULL)
314 std::cout << err.
code << std::endl;
325 while((check = myScanOp->
nextResult(
true)) == 0){
341 }
while((check = myScanOp->
nextResult(
false)) == 0);
396 int scan_update(
Ndb* myNdb,
398 const char * before_color,
399 const char * after_color)
405 int retryAttempt = 0;
406 const int retryMax = 10;
431 if (retryAttempt >= retryMax)
433 std::cout <<
"ERROR: has retried this operation " << retryAttempt
434 <<
" times, failing!" << std::endl;
449 std::cout << err.
message << std::endl;
457 if (myScanOp == NULL)
508 while((check = myScanOp->
nextResult(
true)) == 0){
525 myUpdateOp->
setValue(update_column, after_color);
531 }
while((check = myScanOp->
nextResult(
false)) == 0);
588 int scan_print(
Ndb * myNdb)
592 int retryAttempt = 0;
593 const int retryMax = 10;
622 if (retryAttempt >= retryMax)
624 std::cout <<
"ERROR: has retried this operation " << retryAttempt
625 <<
" times, failing!" << std::endl;
640 std::cout << err.
message << std::endl;
648 if (myScanOp == NULL)
671 myRecAttr[0] = myScanOp->
getValue(
"REG_NO");
672 myRecAttr[1] = myScanOp->
getValue(
"BRAND");
673 myRecAttr[2] = myScanOp->
getValue(
"COLOR");
674 if(myRecAttr[0] ==NULL || myRecAttr[1] == NULL || myRecAttr[2]==NULL)
691 std::cout << err.
code << std::endl;
701 while((check = myScanOp->
nextResult(
true)) == 0){
708 std::cout << myRecAttr[0]->
u_32_value() <<
"\t";
713 std::cout << myRecAttr[1]->
aRef() <<
"\t";
718 std::cout << myRecAttr[2]->
aRef() << std::endl;
725 }
while((check = myScanOp->
nextResult(
false)) == 0);
736 int main(
int argc,
char** argv)
740 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
743 char * mysqld_sock = argv[1];
744 const char *connectstring = argv[2];
752 if ( !mysql_init(&mysql) ) {
753 std::cout <<
"mysql_init failed\n";
756 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
760 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
761 if (mysql_query(&mysql,
"USE ndb_examples") != 0) MYSQLERROR(mysql);
771 if (cluster_connection.connect(4, 5, 1))
773 std::cout <<
"Unable to connect to cluster within 30 secs." << std::endl;
777 if (cluster_connection.wait_until_ready(30,0) < 0)
779 std::cout <<
"Cluster was not ready within 30 secs.\n";
783 Ndb myNdb(&cluster_connection,
"ndb_examples");
784 if (myNdb.
init(1024) == -1) {
798 std::cout <<
"Dictionary::getTable() failed.";
805 std::cout <<
"Wrong table definition" << std::endl;
811 if(populate(&myNdb) > 0)
812 std::cout <<
"populate: Success!" << std::endl;
814 if(scan_print(&myNdb) > 0)
815 std::cout <<
"scan_print: Success!" << std::endl << std::endl;
817 std::cout <<
"Going to delete all pink cars!" << std::endl;
824 sprintf(tmp.color,
"Pink");
825 if(scan_delete(&myNdb, column_color, tmp.color) > 0)
826 std::cout <<
"scan_delete: Success!" << std::endl << std::endl;
829 if(scan_print(&myNdb) > 0)
830 std::cout <<
"scan_print: Success!" << std::endl << std::endl;
837 sprintf(tmp1.color,
"Blue");
838 sprintf(tmp2.color,
"Black");
839 std::cout <<
"Going to update all " << tmp1.color
840 <<
" cars to " << tmp2.color <<
" cars!" << std::endl;
841 if(scan_update(&myNdb, column_color, tmp1.color, tmp2.color) > 0)
842 std::cout <<
"scan_update: Success!" << std::endl << std::endl;
844 if(scan_print(&myNdb) > 0)
845 std::cout <<
"scan_print: Success!" << std::endl << std::endl;