Products Support Documentation Download
tutorials/HelloWorldCPP/hello_worldCPPTutorial_main.cpp
/*
** HELLO WORLD CPP
** -----------
** This document describes the process to create a simple database, insert a
** record containing a text field, read the text field from database and print
** it out.
*/
#include <iostream>
#include "cpp-tfs.h" // The RDM TFS class
#include "cpp-exception.h" // The RDM exception class
#include "cpp-transaction.h" // The RDM transaction class
#include "hello_world_api.h" // The hello_world database API.
using namespace RDM_CPP; // RDM Class
using namespace RDM_CPP::HELLO_WORLD; // RDM hello_world Db Class
static void hello_worldCPP(void)
{
TFS tfs;
Db_hello_world db;
RDM_TABLE_ID tables[] = { TABLE_INFO };
try
{
// Allocate RDM_TFS handle
tfs = TFS::Alloc("");
// Allocate RDM_DB handle
db = tfs.AllocDatabase();
// Open database
db.Open("hello_world");
}
catch (rdm_exception)
{
std::cerr << std::endl << "Sorry, I can't open the hello_world database." << std::endl;
throw;
}
/* Start an update transaction and lock the table */
db.StartUpdate(tables, RDM_LEN(tables));
try
{
/* Remove all of the rows from the database */
db.DeleteAllRows();
/* Commit the transaction */
db.End();
}
catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback();
throw;
}
/* Start an update transaction and lock the table */
db.StartUpdate(tables, RDM_LEN(tables));
try
{
INFO infoInserted;
/* Insert a row into the table */
strcpy(infoInserted.mychar, "Hello World CPP! - using the embedded TFS");
db.Insert_info_Row(infoInserted);
/* Commit a transaction */
db.End();
}
catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback();
throw;
}
/* Start a read transaction and lock the table */
db.StartRead(tables, RDM_LEN(tables));
try
{
Cursor_info cursor;
INFO infoRead;
/* Fill a cursor with all the rows in a table */
cursor = db.Get_info_Rows();
/* Move to the first row in the info table */
cursor.MoveToFirst();
/* Read the full content of the current record */
cursor.ReadRow(infoRead);
std::cout << infoRead.mychar << std::endl;
/* Free the read lock on the table */
db.End();
}
catch (rdm_exception)
{
/* Rollback the transaction */
db.EndRollback();
throw;
}
}
int32_t hello_worldCPPTutorial_main(
int32_t argc,
const char *const *argv)
{
RDM_UNREF(argc);
RDM_UNREF(argv);
try
{
hello_worldCPP();
}
catch (const rdm_exception& e)
{
std::cerr << "There was an error in this Tutorial (" << e.GetEnum() << ", " << e.GetDescription() << ")" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE_CR(hello_worldCPPTutorial)