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 <cstring>
#include <cstdlib>
#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"
using namespace RDM_CPP::CPP01EXAMPLE; // RDM cpp01Example Db Class
static void hello_worldCPP (void)
{
TFS tfs;
Db_cpp01Example db;
RDM_TABLE_ID tables[] = {TABLE_INFO};
try
{
// Allocate RDM_TFS handle
// Allocate RDM_DB handle
db = tfs.AllocDatabase ();
// Open database
{
std::cerr << "I can't open the cpp01Example database." << std::endl;
throw;
}
/* Start an update transaction and lock the table */
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;
}
}
RDM_C_EXPORT int32_t main_cpp01Example (
int32_t /*argc*/,
const char *const * /*argv*/)
{
try
{
hello_worldCPP ();
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (cpp01Example)
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.
Db Open(const char *name, RDM_OPEN_MODE mode, const char *const catalog) const
Open the database.
virtual const char * GetEnum() const
Header for the C++ transaction API.
the rdm_exception class This class implements the exception thrown by the RaimaDB 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
Internal RaimaDB Startup API used by startup macros.