#include <mysql.h>#include <NdbApi.hpp>#include <stdio.h>#include <iostream>Include dependency graph for ndbapi_simple.cpp:

Go to the source code of this file.
Defines | |
| #define | PRINT_ERROR(code, msg) |
| #define | MYSQLERROR(mysql) |
| #define | APIERROR(error) |
Functions | |
| static void | run_application (MYSQL &, Ndb_cluster_connection &) |
| int | main () |
| static void | create_table (MYSQL &) |
| static void | do_insert (Ndb &) |
| static void | do_update (Ndb &) |
| static void | do_delete (Ndb &) |
| static void | do_read (Ndb &) |
| #define APIERROR | ( | error | ) |
Value:
{ \
PRINT_ERROR(error.code,error.message); \
exit(-1); }
Definition at line 51 of file ndbapi_simple.cpp.
| #define MYSQLERROR | ( | mysql | ) |
Value:
{ \
PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
exit(-1); }
Definition at line 48 of file ndbapi_simple.cpp.
| #define PRINT_ERROR | ( | code, | |||
| msg | ) |
Value:
std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ << ", code: " << code \ << ", msg: " << msg << "." << std::endl
Definition at line 44 of file ndbapi_simple.cpp.
| static void create_table | ( | MYSQL & | mysql | ) | [static] |
Function to create table
Definition at line 196 of file ndbapi_async.cpp.
References ER_TABLE_EXISTS_ERROR, mysql, mysql_errno(), mysql_query(), and MYSQLERROR.
00197 { 00198 while (mysql_query(&mysql, 00199 "CREATE TABLE" 00200 " GARAGE" 00201 " (REG_NO INT UNSIGNED NOT NULL," 00202 " BRAND CHAR(20) NOT NULL," 00203 " COLOR CHAR(20) NOT NULL," 00204 " PRIMARY KEY USING HASH (REG_NO))" 00205 " ENGINE=NDB")) 00206 { 00207 if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR) 00208 MYSQLERROR(mysql); 00209 std::cout << "MySQL Cluster already has example table: GARAGE. " 00210 << "Dropping it..." << std::endl; 00211 /************** 00212 * Drop table * 00213 **************/ 00214 if (mysql_query(&mysql, "DROP TABLE GARAGE")) 00215 MYSQLERROR(mysql); 00216 } 00217 return 1; 00218 }
Here is the call graph for this function:

| static void do_delete | ( | Ndb & | ) | [static] |
Definition at line 217 of file ndbapi_simple.cpp.
References APIERROR, Ndb::closeTransaction(), NdbTransaction::Commit, Ndb::getDictionary(), Ndb::getNdbError(), NdbDictionary::Dictionary::getNdbError(), NdbDictionary::Dictionary::getTable(), NULL, and Ndb::startTransaction().
Referenced by run_application().
00218 { 00219 const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); 00220 const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); 00221 00222 if (myTable == NULL) 00223 APIERROR(myDict->getNdbError()); 00224 00225 NdbTransaction *myTransaction= myNdb.startTransaction(); 00226 if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); 00227 00228 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00229 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00230 00231 myOperation->deleteTuple(); 00232 myOperation->equal( "ATTR1", 3 ); 00233 00234 if (myTransaction->execute(NdbTransaction::Commit) == -1) 00235 APIERROR(myTransaction->getNdbError()); 00236 00237 myNdb.closeTransaction(myTransaction); 00238 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void do_insert | ( | Ndb & | ) | [static] |
Definition at line 152 of file ndbapi_simple.cpp.
References APIERROR, Ndb::closeTransaction(), NdbTransaction::Commit, Ndb::getDictionary(), Ndb::getNdbError(), NdbDictionary::Dictionary::getNdbError(), NdbDictionary::Dictionary::getTable(), NULL, and Ndb::startTransaction().
Referenced by Dbtup::execTUPKEYREQ(), and run_application().
00153 { 00154 const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); 00155 const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); 00156 00157 if (myTable == NULL) 00158 APIERROR(myDict->getNdbError()); 00159 00160 for (int i = 0; i < 5; i++) { 00161 NdbTransaction *myTransaction= myNdb.startTransaction(); 00162 if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); 00163 00164 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00165 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00166 00167 myOperation->insertTuple(); 00168 myOperation->equal("ATTR1", i); 00169 myOperation->setValue("ATTR2", i); 00170 00171 myOperation= myTransaction->getNdbOperation(myTable); 00172 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00173 00174 myOperation->insertTuple(); 00175 myOperation->equal("ATTR1", i+5); 00176 myOperation->setValue("ATTR2", i+5); 00177 00178 if (myTransaction->execute( NdbTransaction::Commit ) == -1) 00179 APIERROR(myTransaction->getNdbError()); 00180 00181 myNdb.closeTransaction(myTransaction); 00182 } 00183 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void do_read | ( | Ndb & | ) | [static] |
Definition at line 243 of file ndbapi_simple.cpp.
References APIERROR, Ndb::closeTransaction(), NdbTransaction::Commit, Ndb::getDictionary(), Ndb::getNdbError(), NdbDictionary::Dictionary::getNdbError(), NdbDictionary::Dictionary::getTable(), NdbOperation::LM_Read, NULL, Ndb::startTransaction(), and NdbRecAttr::u_32_value().
Referenced by run_application().
00244 { 00245 const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); 00246 const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); 00247 00248 if (myTable == NULL) 00249 APIERROR(myDict->getNdbError()); 00250 00251 std::cout << "ATTR1 ATTR2" << std::endl; 00252 00253 for (int i = 0; i < 10; i++) { 00254 NdbTransaction *myTransaction= myNdb.startTransaction(); 00255 if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); 00256 00257 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00258 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00259 00260 myOperation->readTuple(NdbOperation::LM_Read); 00261 myOperation->equal("ATTR1", i); 00262 00263 NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL); 00264 if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); 00265 00266 if(myTransaction->execute( NdbTransaction::Commit ) == -1) 00267 if (i == 3) { 00268 std::cout << "Detected that deleted tuple doesn't exist!" << std::endl; 00269 } else { 00270 APIERROR(myTransaction->getNdbError()); 00271 } 00272 00273 if (i != 3) { 00274 printf(" %2d %2d\n", i, myRecAttr->u_32_value()); 00275 } 00276 myNdb.closeTransaction(myTransaction); 00277 } 00278 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void do_update | ( | Ndb & | ) | [static] |
Definition at line 188 of file ndbapi_simple.cpp.
References APIERROR, Ndb::closeTransaction(), NdbTransaction::Commit, Ndb::getDictionary(), Ndb::getNdbError(), NdbDictionary::Dictionary::getNdbError(), NdbDictionary::Dictionary::getTable(), NULL, and Ndb::startTransaction().
Referenced by run_application().
00189 { 00190 const NdbDictionary::Dictionary* myDict= myNdb.getDictionary(); 00191 const NdbDictionary::Table *myTable= myDict->getTable("MYTABLENAME"); 00192 00193 if (myTable == NULL) 00194 APIERROR(myDict->getNdbError()); 00195 00196 for (int i = 0; i < 10; i+=2) { 00197 NdbTransaction *myTransaction= myNdb.startTransaction(); 00198 if (myTransaction == NULL) APIERROR(myNdb.getNdbError()); 00199 00200 NdbOperation *myOperation= myTransaction->getNdbOperation(myTable); 00201 if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); 00202 00203 myOperation->updateTuple(); 00204 myOperation->equal( "ATTR1", i ); 00205 myOperation->setValue( "ATTR2", i+10); 00206 00207 if( myTransaction->execute( NdbTransaction::Commit ) == -1 ) 00208 APIERROR(myTransaction->getNdbError()); 00209 00210 myNdb.closeTransaction(myTransaction); 00211 } 00212 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int main | ( | void | ) |
Definition at line 55 of file ndbapi_simple.cpp.
References Ndb_cluster_connection::connect(), exit, mysql, mysql_init(), mysql_real_connect(), MYSQLERROR, ndb_end(), ndb_init(), run_application(), and Ndb_cluster_connection::wait_until_ready().
00056 { 00057 // ndb_init must be called first 00058 ndb_init(); 00059 00060 // connect to mysql server and cluster and run application 00061 { 00062 // Object representing the cluster 00063 Ndb_cluster_connection cluster_connection; 00064 00065 // Connect to cluster management server (ndb_mgmd) 00066 if (cluster_connection.connect(4 /* retries */, 00067 5 /* delay between retries */, 00068 1 /* verbose */)) 00069 { 00070 std::cout << "Cluster management server was not ready within 30 secs.\n"; 00071 exit(-1); 00072 } 00073 00074 // Optionally connect and wait for the storage nodes (ndbd's) 00075 if (cluster_connection.wait_until_ready(30,0) < 0) 00076 { 00077 std::cout << "Cluster was not ready within 30 secs.\n"; 00078 exit(-1); 00079 } 00080 00081 // connect to mysql server 00082 MYSQL mysql; 00083 if ( !mysql_init(&mysql) ) { 00084 std::cout << "mysql_init failed\n"; 00085 exit(-1); 00086 } 00087 if ( !mysql_real_connect(&mysql, "localhost", "root", "", "", 00088 3306, "/tmp/mysql.sock", 0) ) 00089 MYSQLERROR(mysql); 00090 00091 // run the application code 00092 run_application(mysql, cluster_connection); 00093 } 00094 00095 ndb_end(0); 00096 00097 std::cout << "\nTo drop created table use:\n" 00098 << "echo \"drop table MYTABLENAME\" | mysql TEST_DB_1 -u root\n"; 00099 00100 return 0; 00101 }
Here is the call graph for this function:

| static void run_application | ( | MYSQL & | , | |
| Ndb_cluster_connection & | ||||
| ) | [static] |
Definition at line 109 of file ndbapi_simple.cpp.
References APIERROR, create_table(), do_delete(), do_insert(), do_read(), do_update(), Ndb::getNdbError(), Ndb::init(), mysql, mysql_query(), and MYSQLERROR.
Referenced by main().
00111 { 00112 /******************************************** 00113 * Connect to database via mysql-c * 00114 ********************************************/ 00115 mysql_query(&mysql, "CREATE DATABASE TEST_DB_1"); 00116 if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql); 00117 create_table(mysql); 00118 00119 /******************************************** 00120 * Connect to database via NdbApi * 00121 ********************************************/ 00122 // Object representing the database 00123 Ndb myNdb( &cluster_connection, "TEST_DB_1" ); 00124 if (myNdb.init()) APIERROR(myNdb.getNdbError()); 00125 00126 /* 00127 * Do different operations on database 00128 */ 00129 do_insert(myNdb); 00130 do_update(myNdb); 00131 do_delete(myNdb); 00132 do_read(myNdb); 00133 }
Here is the call graph for this function:

Here is the caller graph for this function:

1.4.7

