Commit c33a0fc5 authored by unknown's avatar unknown

updated all examples to use mysql c-api create table

parent d10e6cf0
-include .defs.mk
#NDB_OS = OS_YOU_ARE_RUNNING_ON
#NDB_OS = LINUX
#You need to set the NDB_OS variable here (LINUX, SOLARIS, MACOSX)
TARGET = ndbapi_async TARGET = ndbapi_async
SRCS = ndbapi_async.cpp SRCS = $(TARGET).cpp
OBJS = ndbapi_async.o OBJS = $(TARGET).o
CC = g++ CXX = g++
CFLAGS = -c -Wall -fno-rtti -D$(NDB_OS) CFLAGS = -g -c -Wall -fno-rtti -fno-exceptions
CXXFLAGS = -g
DEBUG = DEBUG =
LFLAGS = -Wall LFLAGS = -Wall
INCLUDE_DIR = ../../include TOP_SRCDIR = ../../..
LIB_DIR = ../../lib INCLUDE_DIR = $(TOP_SRCDIR)
ifeq ($(NDB_OS), SOLARIS) LIB_DIR = -L$(TOP_SRCDIR)/ndb/src/.libs \
# Here is the definition of system libraries necessary for Solaris 7 -L$(TOP_SRCDIR)/libmysql_r/.libs \
SYS_LIB = -lpthread -lsocket -lnsl -lrt -L$(TOP_SRCDIR)/mysys
endif
ifeq ($(NDB_OS), LINUX)
# Here is the definition of system libraries necessary for Linux 2.4
SYS_LIB = -lpthread
endif
ifeq ($(NDB_OS), MACOSX)
# Here is the definition of system libraries necessary for Mac OS X
SYS_LIB = SYS_LIB =
endif
$(TARGET): $(OBJS) $(TARGET): $(OBJS)
$(CC) $(LFLAGS) -L$(LIB_DIR) -lNDB_API $(OBJS) $(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)
$(CC) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS) $(CXX) $(CFLAGS) -I$(INCLUDE_DIR)/include -I$(INCLUDE_DIR)/extra -I$(INCLUDE_DIR)/ndb/include -I$(INCLUDE_DIR)/ndb/include/ndbapi $(SRCS)
clean: clean:
rm -f *.o $(TARGET) rm -f *.o $(TARGET)
...@@ -24,9 +24,12 @@ ...@@ -24,9 +24,12 @@
* *
* Classes and methods in NDBAPI used in this example: * Classes and methods in NDBAPI used in this example:
* *
* Ndb_cluster_connection
* connect()
* wait_until_ready()
*
* Ndb * Ndb
* init() * init()
* waitUntilRead()
* getDictionary() * getDictionary()
* startTransaction() * startTransaction()
* closeTransaction() * closeTransaction()
...@@ -38,23 +41,6 @@ ...@@ -38,23 +41,6 @@
* executeAsynchPrepare() * executeAsynchPrepare()
* getNdbError() * getNdbError()
* *
* NdbDictionary::Dictionary
* getTable()
* dropTable()
* createTable()
* getNdbError()
*
* NdbDictionary::Column
* setName()
* setType()
* setLength()
* setPrimaryKey()
* setNullable()
*
* NdbDictionary::Table
* setName()
* addColumn()
*
* NdbOperation * NdbOperation
* insertTuple() * insertTuple()
* equal() * equal()
...@@ -63,10 +49,10 @@ ...@@ -63,10 +49,10 @@
*/ */
#include <ndb_global.h> #include <mysql.h>
#include <mysqld_error.h>
#include <NdbApi.hpp> #include <NdbApi.hpp>
#include <NdbScanFilter.hpp>
#include <iostream> // Used for cout #include <iostream> // Used for cout
/** /**
...@@ -85,11 +71,16 @@ milliSleep(int milliseconds){ ...@@ -85,11 +71,16 @@ milliSleep(int milliseconds){
/** /**
* error printout macro * error printout macro
*/ */
#define APIERROR(error) \ #define PRINT_ERROR(code,msg) \
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \ std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
<< error.code << ", msg: " << error.message << "." << std::endl; \ << ", 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); } exit(-1); }
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL #ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
/** /**
...@@ -207,61 +198,26 @@ callback(int result, NdbConnection* trans, void* aObject) ...@@ -207,61 +198,26 @@ callback(int result, NdbConnection* trans, void* aObject)
/** /**
* Create table "GARAGE" * Create table "GARAGE"
*/ */
int create_table(Ndb * myNdb) int create_table(MYSQL &mysql)
{ {
NdbDictionary::Table myTable; while (mysql_query(&mysql,
NdbDictionary::Column myColumn; "CREATE TABLE"
" GARAGE"
NdbDictionary::Dictionary* myDict = myNdb->getDictionary(); " (REG_NO INT UNSIGNED NOT NULL,"
" BRAND CHAR(20) NOT NULL,"
/********************************************************* " COLOR CHAR(20) NOT NULL,"
* Create a table named GARAGE if it does not exist * " PRIMARY KEY USING HASH (REG_NO))"
*********************************************************/ " ENGINE=NDB"))
if (myDict->getTable("GARAGE") != NULL)
{ {
std::cout << "NDB already has example table: GARAGE. " if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
MYSQLERROR(mysql);
std::cout << "MySQL Cluster already has example table: GARAGE. "
<< "Dropping it..." << std::endl; << "Dropping it..." << std::endl;
if(myDict->dropTable("GARAGE") == -1) /**************
{ * Drop table *
std::cout << "Failed to drop: GARAGE." << std::endl; **************/
exit(1); if (mysql_query(&mysql, "DROP TABLE GARAGE"))
} MYSQLERROR(mysql);
}
myTable.setName("GARAGE");
/**
* Column REG_NO
*/
myColumn.setName("REG_NO");
myColumn.setType(NdbDictionary::Column::Unsigned);
myColumn.setLength(1);
myColumn.setPrimaryKey(true);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
/**
* Column BRAND
*/
myColumn.setName("BRAND");
myColumn.setType(NdbDictionary::Column::Char);
myColumn.setLength(20);
myColumn.setPrimaryKey(false);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
/**
* Column COLOR
*/
myColumn.setName("COLOR");
myColumn.setType(NdbDictionary::Column::Char);
myColumn.setLength(20);
myColumn.setPrimaryKey(false);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
if (myDict->createTable(myTable) == -1) {
APIERROR(myDict->getNdbError());
} }
return 1; return 1;
} }
...@@ -455,21 +411,47 @@ int populate(Ndb * myNdb, int data, async_callback_t * cbData) ...@@ -455,21 +411,47 @@ int populate(Ndb * myNdb, int data, async_callback_t * cbData)
int main() int main()
{ {
ndb_init(); ndb_init();
Ndb* myNdb = new Ndb( "TEST_DB" ); // Object representing the database MYSQL mysql;
/******************************************* /**************************************************************
* Initialize NDB and wait until its ready * * Connect to mysql server and create table *
*******************************************/ **************************************************************/
if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions {
APIERROR(myNdb->getNdbError()); 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);
mysql_query(&mysql, "CREATE DATABASE TEST_DB");
if (mysql_query(&mysql, "USE TEST_DB") != 0) MYSQLERROR(mysql);
if (myNdb->waitUntilReady(30) != 0) { create_table(mysql);
std::cout << "NDB was not ready within 30 secs." << std::endl; }
/**************************************************************
* Connect to ndb cluster *
**************************************************************/
Ndb_cluster_connection cluster_connection;
if (cluster_connection.connect(4, 5, 1))
{
std::cout << "Unable to connect to cluster within 30 secs." << std::endl;
exit(-1);
}
// Optionally connect and wait for the storage nodes (ndbd's)
if (cluster_connection.wait_until_ready(30,0) < 0)
{
std::cout << "Cluster was not ready within 30 secs.\n";
exit(-1); exit(-1);
} }
create_table(myNdb);
Ndb* myNdb = new Ndb( &cluster_connection,
"TEST_DB" ); // Object representing the database
if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions
APIERROR(myNdb->getNdbError());
}
/** /**
* Initialise transaction array * Initialise transaction array
......
...@@ -141,8 +141,8 @@ static void create_table(MYSQL &mysql) ...@@ -141,8 +141,8 @@ static void create_table(MYSQL &mysql)
if (mysql_query(&mysql, if (mysql_query(&mysql,
"CREATE TABLE" "CREATE TABLE"
" MYTABLENAME" " MYTABLENAME"
" (ATTR1 INT UNSIGNED PRIMARY KEY," " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
" ATTR2 INT UNSIGNED)" " ATTR2 INT UNSIGNED NOT NULL)"
" ENGINE=NDB")) " ENGINE=NDB"))
MYSQLERROR(mysql); MYSQLERROR(mysql);
} }
...@@ -234,7 +234,7 @@ static void do_read(Ndb &myNdb) ...@@ -234,7 +234,7 @@ static void do_read(Ndb &myNdb)
NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME"); NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
myOperation->readTuple(); myOperation->readTuple(NdbOperation::LM_Read);
myOperation->equal("ATTR1", i); myOperation->equal("ATTR1", i);
NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL); NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
......
TARGET = ndbapi_example4 TARGET = ndbapi_example4
SRCS = ndbapi_example4.cpp SRCS = $(TARGET).cpp
OBJS = ndbapi_example4.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)
...@@ -31,117 +31,89 @@ ...@@ -31,117 +31,89 @@
// 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>
#define APIERROR(error) \ #define PRINT_ERROR(code,msg) \
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \ std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
<< error.code << ", msg: " << error.message << "." << std::endl; \ << ", 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); } exit(-1); }
int main() int main()
{ {
ndb_init(); ndb_init();
MYSQL mysql;
Ndb_cluster_connection *cluster_connection= /**************************************************************
new Ndb_cluster_connection(); // Object representing the cluster * Connect to mysql server and create table *
**************************************************************/
int r= cluster_connection->connect(5 /* retries */,
3 /* delay between retries */,
1 /* verbose */);
if (r > 0)
{ {
std::cout if ( !mysql_init(&mysql) ) {
<< "Cluster connect failed, possibly resolved with more retries.\n"; std::cout << "mysql_init failed\n";
exit(-1); exit(-1);
} }
else if (r < 0) if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
3306, "/tmp/mysql.sock", 0) )
MYSQLERROR(mysql);
mysql_query(&mysql, "CREATE DATABASE TEST_DB_1");
if (mysql_query(&mysql, "USE TEST_DB_1") != 0) MYSQLERROR(mysql);
if (mysql_query(&mysql,
"CREATE TABLE"
" MYTABLENAME"
" (ATTR1 INT UNSIGNED,"
" ATTR2 INT UNSIGNED NOT NULL,"
" PRIMARY KEY USING HASH (ATTR1),"
" UNIQUE MYINDEXNAME USING HASH (ATTR2))"
" ENGINE=NDB"))
MYSQLERROR(mysql);
}
/**************************************************************
* Connect to ndb cluster *
**************************************************************/
Ndb_cluster_connection *cluster_connection=
new Ndb_cluster_connection(); // Object representing the cluster
if (cluster_connection->connect(5,3,1))
{ {
std::cout std::cout << "Connect to cluster management server failed.\n";
<< "Cluster connect failed.\n";
exit(-1); exit(-1);
} }
if (cluster_connection->wait_until_ready(30,30)) if (cluster_connection->wait_until_ready(30,30))
{ {
std::cout << "Cluster was not ready within 30 secs." << std::endl; std::cout << "Cluster was not ready within 30 secs.\n";
exit(-1); exit(-1);
} }
Ndb* myNdb = new Ndb( cluster_connection, Ndb* myNdb = new Ndb( cluster_connection,
"TEST_DB_1" ); // Object representing the database "TEST_DB_1" ); // Object representing the database
NdbDictionary::Table myTable;
NdbDictionary::Column myColumn;
NdbDictionary::Index myIndex;
NdbTransaction *myTransaction; // For transactions
NdbOperation *myOperation; // For primary key operations
NdbIndexOperation *myIndexOperation; // For index operations
NdbRecAttr *myRecAttr; // Result of reading attribute value
if (myNdb->init() == -1) { if (myNdb->init() == -1) {
APIERROR(myNdb->getNdbError()); APIERROR(myNdb->getNdbError());
exit(-1); exit(-1);
} }
/*********************************************************
* Create a table named MYTABLENAME if it does not exist *
*********************************************************/
NdbDictionary::Dictionary* myDict = myNdb->getDictionary();
if (myDict->getTable("MYTABLENAME") != NULL) {
std::cout << "NDB already has example table: MYTABLENAME." << std::endl;
exit(-1);
}
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());
/**********************************************************
* Create an index named MYINDEXNAME if it does not exist *
**********************************************************/
if (myDict->getIndex("MYINDEXNAME", "MYTABLENAME") != NULL) {
std::cout << "NDB already has example index: MYINDEXNAME." << std::endl;
exit(-1);
}
myIndex.setName("MYINDEXNAME");
myIndex.setTable("MYTABLENAME");
myIndex.setType(NdbDictionary::Index::UniqueHashIndex);
const char* attr_arr[] = {"ATTR2"};
myIndex.addIndexColumns(1, attr_arr);
if (myDict->createIndex(myIndex) == -1)
APIERROR(myDict->getNdbError());
/************************************************************************** /**************************************************************************
* Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) * * Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
**************************************************************************/ **************************************************************************/
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
myTransaction = myNdb->startTransaction(); NdbTransaction *myTransaction= myNdb->startTransaction();
if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
myOperation = myTransaction->getNdbOperation("MYTABLENAME"); NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
myOperation->insertTuple(); myOperation->insertTuple();
...@@ -167,33 +139,34 @@ int main() ...@@ -167,33 +139,34 @@ int main()
std::cout << "ATTR1 ATTR2" << std::endl; std::cout << "ATTR1 ATTR2" << std::endl;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
myTransaction = myNdb->startTransaction(); NdbTransaction *myTransaction= myNdb->startTransaction();
if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME", NdbIndexOperation *myIndexOperation=
"MYTABLENAME"); myTransaction->getNdbIndexOperation("MYINDEXNAME$unique","MYTABLENAME");
if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
myIndexOperation->readTuple(); myIndexOperation->readTuple(NdbOperation::LM_Read);
myIndexOperation->equal("ATTR2", i); myIndexOperation->equal("ATTR2", i);
myRecAttr = myIndexOperation->getValue("ATTR1", NULL); NdbRecAttr *myRecAttr= myIndexOperation->getValue("ATTR1", NULL);
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
if(myTransaction->execute( Commit ) != -1) if(myTransaction->execute( Commit ) != -1)
printf(" %2d %2d\n", myRecAttr->u_32_value(), i); printf(" %2d %2d\n", myRecAttr->u_32_value(), i);
}
myNdb->closeTransaction(myTransaction); myNdb->closeTransaction(myTransaction);
}
/***************************************************************** /*****************************************************************
* Update the second attribute in half of the tuples (adding 10) * * Update the second attribute in half of the tuples (adding 10) *
*****************************************************************/ *****************************************************************/
for (int i = 0; i < 10; i+=2) { for (int i = 0; i < 10; i+=2) {
myTransaction = myNdb->startTransaction(); NdbTransaction *myTransaction= myNdb->startTransaction();
if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME", NdbIndexOperation *myIndexOperation=
"MYTABLENAME"); myTransaction->getNdbIndexOperation("MYINDEXNAME$unique", "MYTABLENAME");
if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError()); if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
myIndexOperation->updateTuple(); myIndexOperation->updateTuple();
...@@ -209,13 +182,13 @@ int main() ...@@ -209,13 +182,13 @@ int main()
/************************************************* /*************************************************
* Delete one tuple (the one with primary key 3) * * Delete one tuple (the one with primary key 3) *
*************************************************/ *************************************************/
myTransaction = myNdb->startTransaction(); {
NdbTransaction *myTransaction= myNdb->startTransaction();
if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME", NdbIndexOperation *myIndexOperation=
"MYTABLENAME"); myTransaction->getNdbIndexOperation("MYINDEXNAME$unique", "MYTABLENAME");
if (myIndexOperation == NULL) if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
APIERROR(myTransaction->getNdbError());
myIndexOperation->deleteTuple(); myIndexOperation->deleteTuple();
myIndexOperation->equal( "ATTR2", 3 ); myIndexOperation->equal( "ATTR2", 3 );
...@@ -224,28 +197,30 @@ int main() ...@@ -224,28 +197,30 @@ int main()
APIERROR(myTransaction->getNdbError()); APIERROR(myTransaction->getNdbError());
myNdb->closeTransaction(myTransaction); myNdb->closeTransaction(myTransaction);
}
/***************************** /*****************************
* Read and print all tuples * * Read and print all tuples *
*****************************/ *****************************/
{
std::cout << "ATTR1 ATTR2" << std::endl; std::cout << "ATTR1 ATTR2" << std::endl;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
myTransaction = myNdb->startTransaction(); NdbTransaction *myTransaction= myNdb->startTransaction();
if (myTransaction == NULL) APIERROR(myNdb->getNdbError()); if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
myOperation = myTransaction->getNdbOperation("MYTABLENAME"); NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
if (myOperation == NULL) APIERROR(myTransaction->getNdbError()); if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
myOperation->readTuple(); myOperation->readTuple(NdbOperation::LM_Read);
myOperation->equal("ATTR1", i); myOperation->equal("ATTR1", i);
myRecAttr = myOperation->getValue("ATTR2", NULL); NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError()); if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
if(myTransaction->execute( Commit ) == -1) if(myTransaction->execute( Commit ) == -1)
if (i == 3) { if (i == 3) {
std::cout << "Detected that deleted tuple doesn't exist!" << std::endl; std::cout << "Detected that deleted tuple doesn't exist!\n";
} else { } else {
APIERROR(myTransaction->getNdbError()); APIERROR(myTransaction->getNdbError());
} }
...@@ -255,18 +230,13 @@ int main() ...@@ -255,18 +230,13 @@ int main()
} }
myNdb->closeTransaction(myTransaction); myNdb->closeTransaction(myTransaction);
} }
}
/**************
* Drop index *
**************/
if (myDict->dropIndex("MYINDEXNAME", "MYTABLENAME") == -1)
APIERROR(myDict->getNdbError());
/************** /**************
* Drop table * * Drop table *
**************/ **************/
if (myDict->dropTable("MYTABLENAME") == -1) if (mysql_query(&mysql, "DROP TABLE MYTABLENAME"))
APIERROR(myDict->getNdbError()); MYSQLERROR(mysql);
delete myNdb; delete myNdb;
delete cluster_connection; delete cluster_connection;
......
TARGET = ndbapi_scan TARGET = ndbapi_scan
SRCS = ndbapi_scan.cpp SRCS = $(TARGET).cpp
OBJS = ndbapi_scan.o OBJS = $(TARGET).o
CXX = g++ CXX = g++
CFLAGS = -c -Wall -fno-rtti -fno-exceptions CFLAGS = -g -c -Wall -fno-rtti -fno-exceptions
CXXFLAGS = CXXFLAGS = -g
DEBUG = DEBUG =
LFLAGS = -Wall LFLAGS = -Wall
TOP_SRCDIR = ../../.. TOP_SRCDIR = ../../..
INCLUDE_DIR = $(TOP_SRCDIR)/ndb/include INCLUDE_DIR = $(TOP_SRCDIR)
LIB_DIR = -L$(TOP_SRCDIR)/ndb/src/.libs \ LIB_DIR = -L$(TOP_SRCDIR)/ndb/src/.libs \
-L$(TOP_SRCDIR)/libmysql_r/.libs \ -L$(TOP_SRCDIR)/libmysql_r/.libs \
-L$(TOP_SRCDIR)/mysys -L$(TOP_SRCDIR)/mysys
...@@ -17,7 +17,7 @@ $(TARGET): $(OBJS) ...@@ -17,7 +17,7 @@ $(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) $(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)/extra -I$(INCLUDE_DIR)/ndb/include -I$(INCLUDE_DIR)/ndb/include/ndbapi $(SRCS)
clean: clean:
rm -f *.o $(TARGET) rm -f *.o $(TARGET)
...@@ -38,8 +38,6 @@ ...@@ -38,8 +38,6 @@
* getNdbScanOperation() * getNdbScanOperation()
* execute() * execute()
* *
* NdbResultSet
*
* NdbScanOperation * NdbScanOperation
* getValue() * getValue()
* readTuples() * readTuples()
...@@ -47,21 +45,14 @@ ...@@ -47,21 +45,14 @@
* deleteCurrentTuple() * deleteCurrentTuple()
* updateCurrentTuple() * updateCurrentTuple()
* *
* NdbDictionary::Dictionary * const NdbDictionary::Dictionary
* getTable() * getTable()
* dropTable()
* createTable()
* *
* NdbDictionary::Column * const NdbDictionary::Table
* setName() * getColumn()
* setType()
* setLength()
* setPrimaryKey()
* setNullable()
* *
* NdbDictionary::Table * const NdbDictionary::Column
* setName() * getLength()
* addColumn()
* *
* NdbOperation * NdbOperation
* insertTuple() * insertTuple()
...@@ -76,6 +67,8 @@ ...@@ -76,6 +67,8 @@
*/ */
#include <mysql.h>
#include <mysqld_error.h>
#include <NdbApi.hpp> #include <NdbApi.hpp>
// Used for cout // Used for cout
#include <iostream> #include <iostream>
...@@ -97,9 +90,15 @@ milliSleep(int milliseconds){ ...@@ -97,9 +90,15 @@ milliSleep(int milliseconds){
/** /**
* Helper sleep function * Helper sleep function
*/ */
#define APIERROR(error) \ #define PRINT_ERROR(code,msg) \
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \ std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
<< error.code << ", msg: " << error.message << "." << std::endl; \ << ", 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); } exit(-1); }
struct Car struct Car
...@@ -112,55 +111,26 @@ struct Car ...@@ -112,55 +111,26 @@ struct Car
/** /**
* Function to create table * Function to create table
*/ */
int create_table(Ndb * myNdb) int create_table(MYSQL &mysql)
{ {
NdbDictionary::Table myTable; while (mysql_query(&mysql,
NdbDictionary::Column myColumn; "CREATE TABLE"
" GARAGE"
NdbDictionary::Dictionary* myDict = myNdb->getDictionary(); " (REG_NO INT UNSIGNED NOT NULL,"
" BRAND CHAR(20) NOT NULL,"
/********************************************************* " COLOR CHAR(20) NOT NULL,"
* Create a table named GARAGE if it does not exist * " PRIMARY KEY USING HASH (REG_NO))"
*********************************************************/ " ENGINE=NDB"))
if (myDict->getTable("GARAGE") != NULL) { {
std::cout << "NDB already has example table: GARAGE. " if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
MYSQLERROR(mysql);
std::cout << "MySQL Cluster already has example table: GARAGE. "
<< "Dropping it..." << std::endl; << "Dropping it..." << std::endl;
if(myDict->dropTable("GARAGE") == -1) /**************
{ * Drop table *
std::cout << "Failed to drop: GARAGE." << std::endl; **************/
exit(1); if (mysql_query(&mysql, "DROP TABLE GARAGE"))
} MYSQLERROR(mysql);
}
Car car;
myTable.setName("GARAGE");
myColumn.setName("REG_NO");
myColumn.setType(NdbDictionary::Column::Unsigned);
myColumn.setLength(1);
myColumn.setPrimaryKey(true);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
myColumn.setName("BRAND");
myColumn.setType(NdbDictionary::Column::Char);
myColumn.setLength(sizeof(car.brand));
myColumn.setPrimaryKey(false);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
myColumn.setName("COLOR");
myColumn.setType(NdbDictionary::Column::Char);
myColumn.setLength(sizeof(car.color));
myColumn.setPrimaryKey(false);
myColumn.setNullable(false);
myTable.addColumn(myColumn);
if (myDict->createTable(myTable) == -1) {
APIERROR(myDict->getNdbError());
return -1;
} }
return 1; return 1;
} }
...@@ -460,7 +430,7 @@ int scan_update(Ndb* myNdb, ...@@ -460,7 +430,7 @@ int scan_update(Ndb* myNdb,
/** /**
* Define a result set for the scan. * Define a result set for the scan.
*/ */
if( myScanOp->readTuplesExclusive(NdbOperation::LM_Exclusive) ) if( myScanOp->readTuples(NdbOperation::LM_Exclusive) )
{ {
std::cout << myTrans->getNdbError().message << std::endl; std::cout << myTrans->getNdbError().message << std::endl;
myNdb->closeTransaction(myTrans); myNdb->closeTransaction(myTrans);
...@@ -726,35 +696,66 @@ int scan_print(Ndb * myNdb) ...@@ -726,35 +696,66 @@ int scan_print(Ndb * myNdb)
int main() int main()
{ {
ndb_init(); ndb_init();
MYSQL mysql;
Ndb_cluster_connection cluster_connection; /**************************************************************
* Connect to mysql server and create table *
**************************************************************/
{
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);
if (cluster_connection.connect(12, 5, 1)) mysql_query(&mysql, "CREATE DATABASE TEST_DB");
if (mysql_query(&mysql, "USE TEST_DB") != 0) MYSQLERROR(mysql);
create_table(mysql);
}
/**************************************************************
* Connect to ndb cluster *
**************************************************************/
Ndb_cluster_connection cluster_connection;
if (cluster_connection.connect(4, 5, 1))
{ {
std::cout << "Unable to connect to cluster within 30 secs." << std::endl; std::cout << "Unable to connect to cluster within 30 secs." << std::endl;
exit(-1); exit(-1);
} }
// Optionally connect and wait for the storage nodes (ndbd's)
if (cluster_connection.wait_until_ready(30,30)) if (cluster_connection.wait_until_ready(30,0) < 0)
{ {
std::cout << "Cluster was not ready within 30 secs." << std::endl; std::cout << "Cluster was not ready within 30 secs.\n";
exit(-1); exit(-1);
} }
Ndb myNdb(&cluster_connection,"TEST_DB" ); Ndb myNdb(&cluster_connection,"TEST_DB");
/*******************************************
* Initialize NDB and wait until its ready *
*******************************************/
if (myNdb.init(1024) == -1) { // Set max 1024 parallel transactions if (myNdb.init(1024) == -1) { // Set max 1024 parallel transactions
APIERROR(myNdb.getNdbError()); APIERROR(myNdb.getNdbError());
exit(-1); exit(-1);
} }
create_table(&myNdb); /*******************************************
* Check table definition *
*******************************************/
int column_color;
{
const NdbDictionary::Dictionary* myDict= myNdb.getDictionary();
const NdbDictionary::Table *t= myDict->getTable("GARAGE");
NdbDictionary::Dictionary* myDict = myNdb.getDictionary(); Car car;
int column_color = myDict->getTable("GARAGE")->getColumn("COLOR")->getColumnNo(); if (t->getColumn("COLOR")->getLength() != sizeof(car.color) ||
t->getColumn("BRAND")->getLength() != sizeof(car.brand))
{
std::cout << "Wrong table definition" << std::endl;
exit(-1);
}
column_color= t->getColumn("COLOR")->getColumnNo();
}
if(populate(&myNdb) > 0) if(populate(&myNdb) > 0)
std::cout << "populate: Success!" << std::endl; std::cout << "populate: Success!" << std::endl;
...@@ -789,4 +790,6 @@ int main() ...@@ -789,4 +790,6 @@ int main()
} }
if(scan_print(&myNdb) > 0) if(scan_print(&myNdb) > 0)
std::cout << "scan_print: Success!" << std::endl << std::endl; std::cout << "scan_print: Success!" << std::endl << std::endl;
return 0;
} }
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