cpp01Example_main.cpp

Basic tutorial in C++ where a database is created, a row is inserted and then a row is retrieved and displayed. This example needs a compiled schema, cpp01Example.sdl.

/*
* 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 <string.h>
#include <stdlib.h>
/* [includes_CPP] */
#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 "cpp01Example_gen_api.h" // The cpp01Example database API.
#include "rdmstartupapi.h"
/* [includes_CPP] */
/* [namespace_CPP] */
using namespace RDM_CPP; // RDM Class
using namespace RDM_CPP::CPP01EXAMPLE; // RDM cpp01Example Db Class
/* [namespace_CPP] */
static void hello_worldCPP (void)
{
TFS tfs;
Db_cpp01Example db;
RDM_TABLE_ID tables[] = {TABLE_INFO};
/* [allocHandles_CPP] */
try
{
// Allocate RDM_TFS handle
tfs = TFS::Alloc ("");
// Allocate RDM_DB handle
db = tfs.AllocDatabase ();
// Open database
db.Open ("cpp01Example");
} catch (rdm_exception)
{
std::cerr << "I can't open the cpp01Example database." << std::endl;
throw;
}
/* [allocHandles_CPP] */
/* 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 main_hello_worldCPPTutorial (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 (hello_worldCPPTutorial)
Header for C++ exceptions.
Header for the C++ Transaction File Server (TFS) API.
Transaction StartUpdate(const RDM_TABLE_ID writeTableIds[], uint32_t numWriteTableIds, const RDM_TABLE_ID readTableIds[]=NULL, uint32_t numReadTableIds=0) const
Begin updating a database.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
The TFS class This class provides the TFS implementation.
Definition: cpp-tfs.h:64
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
Db Open(const char *name, RDM_OPEN_MODE mode, const char *const catalog) const
Open the database.
#define RDM_UNREF(a)
Definition: psptypes.h:45
virtual const char * GetEnum() const
Header for the C++ transaction API.
Db AllocDatabase(const char *opts=NULL)
Instantiate a database.
void End(void)
End a transaction by committing it.
the rdm_exception class This class implements the exception thrown by the RDM CPP API
Definition: cpp-exception.h:46
virtual const char * GetDescription() const
method to get a text description of the error causing the exception to be thrown
#define RDM_LEN(x)
Definition: psptypes.h:78
The RDM C++ Namespace.
Definition: cpp-cursor.h:27
static TFS Alloc(RDM_TFS tfs)
Return TFS object using the specified RDM_TFS.
Internal RDM Startup API used by startup macros.