Commit 0587b7e6 authored by unknown's avatar unknown

updated example1 to use mysql c-api for table creation

parent 5bb7ff14
TARGET = ndbapi_example1 TARGET = ndbapi_example1
SRCS = ndbapi_example1.cpp SRCS = $(TARGET).cpp
OBJS = ndbapi_example1.o OBJS = $(TARGET).o
CXX = g++ CXX = g++
CFLAGS = -c -Wall -fno-rtti -fno-exceptions CFLAGS = -c -Wall -fno-rtti -fno-exceptions
CXXFLAGS =
DEBUG = DEBUG =
LFLAGS = -Wall LFLAGS = -Wall
INCLUDE_DIR = ../../include TOP_SRCDIR = ../../..
LIB_DIR = -L../../src/.libs \ INCLUDE_DIR = $(TOP_SRCDIR)
-L../../../libmysql_r/.libs \ LIB_DIR = -L$(TOP_SRCDIR)/ndb/src/.libs \
-L../../../mysys -L$(TOP_SRCDIR)/libmysql_r/.libs \
-L$(TOP_SRCDIR)/mysys
SYS_LIB = SYS_LIB =
$(TARGET): $(OBJS) $(TARGET): $(OBJS)
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET) $(CXX) $(CXXFLAGS) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET)
$(TARGET).o: $(SRCS) $(TARGET).o: $(SRCS)
$(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) $(CXX) $(CFLAGS) -I$(INCLUDE_DIR)/include -I$(INCLUDE_DIR)/ndb/include -I$(INCLUDE_DIR)/ndb/include/ndbapi $(SRCS)
clean: clean:
rm -f *.o $(TARGET) rm -f *.o $(TARGET)
...@@ -14,36 +14,50 @@ ...@@ -14,36 +14,50 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
// /*
// ndbapi_example1.cpp: Using synchronous transactions in NDB API * ndbapi_example1.cpp: Using synchronous transactions in NDB API
// *
// Correct output from this program is: * Correct output from this program is:
// *
// ATTR1 ATTR2 * ATTR1 ATTR2
// 0 10 * 0 10
// 1 1 * 1 1
// 2 12 * 2 12
// Detected that deleted tuple doesn't exist! * Detected that deleted tuple doesn't exist!
// 4 14 * 4 14
// 5 5 * 5 5
// 6 16 * 6 16
// 7 7 * 7 7
// 8 18 * 8 18
// 9 9 * 9 9
*
*/
#include <mysql.h>
#include <NdbApi.hpp> #include <NdbApi.hpp>
// Used for cout // Used for cout
#include <stdio.h> #include <stdio.h>
#include <iostream> #include <iostream>
static void run_application(Ndb_cluster_connection &); static void run_application(MYSQL &, Ndb_cluster_connection &);
#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) { \
PRINT_ERROR(error.code,error.message); \
exit(-1); }
int main() int main()
{ {
// ndb_init must be called first // ndb_init must be called first
ndb_init(); ndb_init();
// connect to cluster and run application // connect to mysql server and cluster and run application
{ {
// Object representing the cluster // Object representing the cluster
Ndb_cluster_connection cluster_connection; Ndb_cluster_connection cluster_connection;
...@@ -64,31 +78,47 @@ int main() ...@@ -64,31 +78,47 @@ int main()
exit(-1); 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", "", "",
3306, "/tmp/mysql.sock", 0) )
MYSQLERROR(mysql);
// run the application code // run the application code
run_application(cluster_connection); run_application(mysql, cluster_connection);
} }
// ndb_end should not be called until all "Ndb" objects are deleted // ndb_end should not be called until all "Ndb" objects are deleted
ndb_end(0); ndb_end(0);
std::cout << "\nTo drop created table use:\n"
<< "echo \"drop table MYTABLENAME\" | mysql TEST_DB_1 -u root\n";
return 0; return 0;
} }
#define APIERROR(error) \ static void create_table(MYSQL &);
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \ static void do_insert(Ndb &);
<< error.code << ", msg: " << error.message << "." << std::endl; \ static void do_update(Ndb &);
exit(-1); } static void do_delete(Ndb &);
static void do_read(Ndb &);
static void create_table(Ndb &myNdb); static void run_application(MYSQL &mysql,
static void do_insert(Ndb &myNdb); Ndb_cluster_connection &cluster_connection)
static void do_update(Ndb &myNdb);
static void do_delete(Ndb &myNdb);
static void do_read(Ndb &myNdb);
static void run_application(Ndb_cluster_connection &cluster_connection)
{ {
/******************************************** /********************************************
* Connect to database * * 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);
/********************************************
* Connect to database via NdbApi *
********************************************/ ********************************************/
// Object representing the database // Object representing the database
Ndb myNdb( &cluster_connection, "TEST_DB_1" ); Ndb myNdb( &cluster_connection, "TEST_DB_1" );
...@@ -97,7 +127,6 @@ static void run_application(Ndb_cluster_connection &cluster_connection) ...@@ -97,7 +127,6 @@ static void run_application(Ndb_cluster_connection &cluster_connection)
/* /*
* Do different operations on database * Do different operations on database
*/ */
create_table(myNdb);
do_insert(myNdb); do_insert(myNdb);
do_update(myNdb); do_update(myNdb);
do_delete(myNdb); do_delete(myNdb);
...@@ -107,37 +136,15 @@ static void run_application(Ndb_cluster_connection &cluster_connection) ...@@ -107,37 +136,15 @@ static void run_application(Ndb_cluster_connection &cluster_connection)
/********************************************************* /*********************************************************
* Create a table named MYTABLENAME if it does not exist * * Create a table named MYTABLENAME if it does not exist *
*********************************************************/ *********************************************************/
static void create_table(Ndb &myNdb) static void create_table(MYSQL &mysql)
{ {
NdbDictionary::Dictionary* myDict = myNdb.getDictionary(); if (mysql_query(&mysql,
"CREATE TABLE"
if (myDict->getTable("MYTABLENAME") != NULL) { " MYTABLENAME"
std::cout " (ATTR1 INT UNSIGNED PRIMARY KEY,"
<< "NDB already has example table: MYTABLENAME.\n" " ATTR2 INT UNSIGNED)"
<< "Use ndb_drop_table -d TEST_DB_1 MYTABLENAME\n"; " ENGINE=NDB"))
exit(-1); MYSQLERROR(mysql);
}
NdbDictionary::Table myTable;
NdbDictionary::Column myColumn;
myTable.setName("MYTABLENAME");
myColumn.setName("ATTR1");
myColumn.setType(NdbDictionary::Column::Unsigned);
myColumn.setLength(1);
myColumn.setPrimaryKey(true);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
myColumn.setName("ATTR2");
myColumn.setType(NdbDictionary::Column::Unsigned);
myColumn.setLength(1);
myColumn.setPrimaryKey(false);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
if (myDict->createTable(myTable) == -1) APIERROR(myDict->getNdbError());
} }
/************************************************************************** /**************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment