summaryrefslogtreecommitdiff
path: root/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp')
-rw-r--r--storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp81
1 files changed, 68 insertions, 13 deletions
diff --git a/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp b/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp
index e8bc19e267b..beb38ce771d 100644
--- a/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp
+++ b/storage/ndb/ndbapi-examples/ndbapi_async1/ndbapi_async1.cpp
@@ -17,38 +17,48 @@
//
// ndbapi_async1.cpp: Using asynchronous transactions in NDB API
//
-// Execute ndbapi_example1 to create the table "MYTABLENAME"
-// before executing this program.
//
// Correct output from this program is:
//
// Successful insert.
// Successful insert.
+#include <mysql.h>
#include <NdbApi.hpp>
// Used for cout
#include <iostream>
+
+#define PRINT_ERROR(code,msg) \
+ std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
+ << ", code: " << code \
+ << ", msg: " << msg << "." << std::endl
+#define MYSQLERROR(mysql) { \
+ PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
+ exit(-1); }
#define APIERROR(error) \
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \
<< error.code << ", msg: " << error.message << "." << std::endl; \
exit(-1); }
+static void create_table(MYSQL &);
+static void drop_table(MYSQL &);
static void callback(int result, NdbTransaction* NdbObject, void* aObject);
-int main()
+int main(int argc, char** argv)
{
- ndb_init();
-
- Ndb_cluster_connection *cluster_connection=
- new Ndb_cluster_connection(); // Object representing the cluster
-
- if (cluster_connection->wait_until_ready(30,30))
+ if (argc != 3)
{
- std::cout << "Cluster was not ready within 30 secs." << std::endl;
+ std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
exit(-1);
}
+ char * mysqld_sock = argv[1];
+ const char *connectstring = argv[2];
+ ndb_init();
+
+ Ndb_cluster_connection *cluster_connection=
+ new Ndb_cluster_connection(connectstring); // Object representing the cluster
int r= cluster_connection->connect(5 /* retries */,
3 /* delay between retries */,
@@ -65,15 +75,32 @@ int main()
<< "Cluster connect failed.\n";
exit(-1);
}
-
- if (cluster_connection->wait_until_ready(30,30))
+
+ if (cluster_connection->wait_until_ready(30,0))
{
std::cout << "Cluster was not ready within 30 secs." << std::endl;
exit(-1);
}
+
+ // connect to mysql server
+ MYSQL mysql;
+ if ( !mysql_init(&mysql) ) {
+ std::cout << "mysql_init failed\n";
+ exit(-1);
+ }
+ if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
+ 0, mysqld_sock, 0) )
+ MYSQLERROR(mysql);
+
+ /********************************************
+ * Connect to database via mysql-c *
+ ********************************************/
+ mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
+ if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
+ create_table(mysql);
Ndb* myNdb = new Ndb( cluster_connection,
- "TEST_DB_2" ); // Object representing the database
+ "TEST_DB_1" ); // Object representing the database
NdbTransaction* myNdbTransaction[2]; // For transactions
NdbOperation* myNdbOperation; // For operations
@@ -119,10 +146,38 @@ int main()
delete myNdb;
delete cluster_connection;
+ drop_table(mysql);
+
ndb_end(0);
return 0;
}
+/*********************************************************
+ * Create a table named MYTABLENAME if it does not exist *
+ *********************************************************/
+static void create_table(MYSQL &mysql)
+{
+ if (mysql_query(&mysql,
+ "CREATE TABLE"
+ " MYTABLENAME"
+ " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
+ " ATTR2 INT UNSIGNED NOT NULL)"
+ " ENGINE=NDB"))
+ MYSQLERROR(mysql);
+}
+
+/***********************************
+ * Drop a table named MYTABLENAME
+ ***********************************/
+static void drop_table(MYSQL &mysql)
+{
+ if (mysql_query(&mysql,
+ "DROP TABLE"
+ " MYTABLENAME"))
+ MYSQLERROR(mysql);
+}
+
+
/*
* callback : This is called when the transaction is polled
*