47 #include <mysqld_error.h>
54 #define PRINT_ERROR(code,msg) \
55 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
56 << ", code: " << code \
57 << ", msg: " << msg << "." << std::endl
58 #define MYSQLERROR(mysql) { \
59 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
61 #define APIERROR(error) { \
62 PRINT_ERROR(error.code,error.message); \
65 int main(
int argc,
char** argv)
69 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
72 char * mysqld_sock = argv[1];
73 const char *connectstring = argv[2];
81 if ( !mysql_init(&mysql) ) {
82 std::cout <<
"mysql_init failed\n";
85 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
89 mysql_query(&mysql,
"CREATE DATABASE ndb_examples_1");
90 if (mysql_query(&mysql,
"USE ndb_examples") != 0) MYSQLERROR(mysql);
92 while (mysql_query(&mysql,
95 " (ATTR1 INT UNSIGNED,"
96 " ATTR2 INT UNSIGNED NOT NULL,"
97 " PRIMARY KEY USING HASH (ATTR1),"
98 " UNIQUE MYINDEXNAME USING HASH (ATTR2))"
101 if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR)
103 std::cout <<
"MySQL Cluster already has example table: api_scan. "
104 <<
"Dropping it..." << std::endl;
105 mysql_query(&mysql,
"DROP TABLE api_simple_index");
107 else MYSQLERROR(mysql);
118 if (cluster_connection->
connect(5,3,1))
120 std::cout <<
"Connect to cluster management server failed.\n";
126 std::cout <<
"Cluster was not ready within 30 secs.\n";
130 Ndb* myNdb =
new Ndb( cluster_connection,
132 if (myNdb->
init() == -1) {
148 for (
int i = 0;
i < 5;
i++) {
150 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
153 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
155 myOperation->insertTuple();
156 myOperation->equal(
"ATTR1",
i);
157 myOperation->setValue(
"ATTR2",
i);
160 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
162 myOperation->insertTuple();
163 myOperation->equal(
"ATTR1",
i+5);
164 myOperation->setValue(
"ATTR2",
i+5);
175 std::cout <<
"ATTR1 ATTR2" << std::endl;
177 for (
int i = 0;
i < 10;
i++) {
179 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
183 if (myIndexOperation == NULL) APIERROR(myTransaction->
getNdbError());
186 myIndexOperation->equal(
"ATTR2",
i);
188 NdbRecAttr *myRecAttr= myIndexOperation->getValue(
"ATTR1", NULL);
189 if (myRecAttr == NULL) APIERROR(myTransaction->
getNdbError());
201 for (
int i = 0;
i < 10;
i+=2) {
203 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
207 if (myIndexOperation == NULL) APIERROR(myTransaction->
getNdbError());
209 myIndexOperation->updateTuple();
210 myIndexOperation->equal(
"ATTR2",
i );
211 myIndexOperation->setValue(
"ATTR2",
i+10);
224 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
228 if (myIndexOperation == NULL) APIERROR(myTransaction->
getNdbError());
230 myIndexOperation->deleteTuple();
231 myIndexOperation->equal(
"ATTR2", 3 );
243 std::cout <<
"ATTR1 ATTR2" << std::endl;
245 for (
int i = 0;
i < 10;
i++) {
247 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
250 if (myOperation == NULL) APIERROR(myTransaction->
getNdbError());
253 myOperation->equal(
"ATTR1",
i);
255 NdbRecAttr *myRecAttr= myOperation->getValue(
"ATTR2", NULL);
256 if (myRecAttr == NULL) APIERROR(myTransaction->
getNdbError());
261 std::cout <<
"Detected that deleted tuple doesn't exist!\n";
274 delete cluster_connection;