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); \
81 int main(
int argc,
char** argv)
85 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
88 char * mysqld_sock = argv[1];
89 const char *connectstring = argv[2];
97 if ( !mysql_init(&mysql) ) {
98 std::cout <<
"mysql_init failed\n";
101 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
105 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
106 if (mysql_query(&mysql,
"USE ndb_examples") != 0)
109 mysql_query(&mysql,
"DROP TABLE api_s_i_ndbrecord");
110 if (mysql_query(&mysql,
113 " (ATTR1 INT UNSIGNED,"
114 " ATTR2 INT UNSIGNED NOT NULL,"
115 " PRIMARY KEY USING HASH (ATTR1),"
116 " UNIQUE MYINDEXNAME USING HASH (ATTR2))"
128 if (cluster_connection->
connect(5,3,1))
130 std::cout <<
"Connect to cluster management server failed.\n";
136 std::cout <<
"Cluster was not ready within 30 secs.\n";
140 Ndb* myNdb =
new Ndb( cluster_connection,
142 if (myNdb->
init() == -1) {
165 spec[0].column= col1;
168 spec[0].nullbit_byte_offset= 0;
169 spec[0].nullbit_bit_in_byte= 0;
171 myDict->createRecord(myTable, spec, 1,
sizeof(spec[0]));
172 if (pk_record == NULL)
176 spec[0].column= col1;
178 spec[0].nullbit_byte_offset= 0;
179 spec[0].nullbit_bit_in_byte= 0;
180 spec[1].column= col2;
182 spec[1].nullbit_byte_offset= 0;
183 spec[1].nullbit_bit_in_byte= 0;
185 myDict->createRecord(myTable, spec, 2,
sizeof(spec[0]));
186 if (attr_record == NULL)
190 spec[0].column= col2;
192 spec[0].nullbit_byte_offset= 0;
193 spec[0].nullbit_bit_in_byte= 0;
195 myDict->createRecord(myIndex, spec, 1,
sizeof(spec[0]));
196 if (key_record == NULL)
204 for (
int i = 0;
i < 5;
i++) {
206 if (myTransaction == NULL) APIERROR(myNdb->
getNdbError());
213 row.attr1= row.attr2=
i;
216 myTransaction->insertTuple(attr_record, (
const char*)&row);
217 if (myOperation == NULL)
220 row.attr1= row.attr2=
i+5;
222 myTransaction->insertTuple(attr_record, (
const char*)&row);
223 if (myOperation == NULL)
235 std::cout <<
"ATTR1 ATTR2" << std::endl;
237 for (
int i = 0;
i < 10;
i++) {
239 if (myTransaction == NULL)
251 getSpec[0].column=NdbDictionary::Column::FRAGMENT;
252 getSpec[0].appStorage=&frag;
255 options.optionsPresent = NdbOperation::OperationOptions::OO_GETVALUE;
256 options.extraGetValues = &getSpec[0];
257 options.numExtraGetValues = 1;
266 unsigned char mask[1]= { 0x01 };
268 myTransaction->readTuple(key_record, (
const char*) &row,
269 attr_record, (
char*) &resultRow,
273 if (myOperation == NULL)
279 printf(
" %2d %2d (frag=%u)\n", resultRow.attr1,
i, frag);
288 for (
int i = 0;
i < 10;
i+=2) {
290 if (myTransaction == NULL)
298 newRowData.attr2=
i+10;
299 unsigned char mask[1]= { 0x02 };
302 myTransaction->updateTuple(key_record, (
const char*)&row,
303 attr_record,(
char*) &newRowData, mask);
304 if (myOperation == NULL)
318 if (myTransaction == NULL)
323 myTransaction->deleteTuple(key_record, (
const char*) &row,
325 if (myOperation == NULL)
338 std::cout <<
"ATTR1 ATTR2" << std::endl;
340 for (
int i = 0;
i < 10;
i++) {
342 if (myTransaction == NULL)
351 myTransaction->readTuple(pk_record, (
const char*) &row,
352 attr_record, (
char*) &row);
353 if (myOperation == NULL)
359 std::cout <<
"Detected that deleted tuple doesn't exist!\n";
365 printf(
" %2d %2d\n", row.attr1, row.attr2);
372 delete cluster_connection;