#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rdm.h"
#include "bookStore_structs.h"
/* Write a row to from author and book tables */
static RDM_RETCODE writeARow (RDM_DB db)
char last_name[13];
char full_name[35];
char bookid[14];
char title[105];
char price[10];
RDM_BCD_T dPrice;
char *inputs[5] = {last_name, full_name, bookid, title, price};
unsigned int len;
int ii = 0;
printf ("ADDING AUTHOR TO DATABASE\nAuthor Last Name:\n");
fgets (last_name, 13, stdin);
printf ("Author Full Name:\n");
fgets (full_name, 35, stdin);
printf ("ADDING BOOK TO DATABASE\nTitle of Book:\n");
fgets (title, 105, stdin);
printf ("Enter id located on book:\n");
fgets (bookid, 14, stdin);
printf ("Price:\n");
fgets (price, 10, stdin);
/* Remove newline character from fgets */
while (ii < (sizeof (inputs) / sizeof (inputs[0])))
len = strlen (inputs[ii]);
if (inputs[ii][len - 1] == '\n')
inputs[ii][len - 1] = '\0';
/* Start an update transaction and lock the table */
rc = rdm_dbStartUpdate (db, tables, RDM_LEN(tables), NULL, 0, NULL);
if (rc == sOKAY)
AUTHOR authInsert; /* Row buffer */
BOOK bookInsert; /* Row buffer */
/* Populate the columns in the AUTHOR Row buffer */
strcpy (authInsert.LAST_NAME, last_name);
strcpy (authInsert.FULL_NAME, full_name);
authInsert._FULL_NAME_has_value = RDM_COL_HAS_VALUE;
/* Insert a row into the table */
rc = rdm_dbInsertRow (db, TABLE_AUTHOR, &authInsert, sizeof (authInsert), NULL);
if (rc == sOKAY)
/* Convert the string to a binary-coded decimal */
rdm_bcdFromString (price, &dPrice);
/* Populate the columns in the BOOK Row buffer */
strcpy (bookInsert.TITLE, title);
bookInsert._TITLE_has_value = RDM_COL_HAS_VALUE;
strcpy (bookInsert.BOOKID, bookid);
bookInsert.PRICE = dPrice;
bookInsert._PRICE_has_value = RDM_COL_HAS_VALUE;
/* Insert a row into the table */
rc = rdm_dbInsertRow (db, TABLE_BOOK, &bookInsert, sizeof (bookInsert), NULL);
if (rc == sOKAY)
/* Commit a transaction */
printf ("The book %s by %s at a price of $%s was added to the database successfully.\n",
title, full_name, price);
rc = rdm_dbEnd (db);
/* Abort the transaction */
fprintf (stderr, "A problem occurred when adding data to the database.\n");
fprintf (stderr, "Error: %s (%d): %s\n", rdm_retcodeGetName (rc), rc, rdm_retcodeGetDescription (rc));
return rc;
/* Read rows from author table */
static RDM_RETCODE readRows (RDM_DB db)
AUTHOR authRead;
rc = rdm_dbStartRead (db, tables, RDM_LEN(tables), NULL);
if (rc == sOKAY)
rc = rdm_dbGetRows (db, TABLE_AUTHOR, &cursor);
if (rc == sOKAY)
/* Navigate to the first row in the cursor */
printf ("Displaying all of the authors in the author table\n");
rc = rdm_cursorMoveToFirst (cursor);
while (rc == sOKAY)
/* Read the row column values */
rc = rdm_cursorReadRow (cursor, &authRead, sizeof (authRead), NULL);
if (rc == sOKAY)
printf ("%s", authRead.LAST_NAME);
/* Move to the next row in the cursor */
rc = rdm_cursorMoveToNext (cursor);
/* We expect to break out of the loop with a sENDOFCURSOR code*/
if (rc == sENDOFCURSOR)
rc = sOKAY;
/* Free the cursor allocated in rdm_dbGetRows */
rdm_cursorFree (cursor);
/* release the read locks */
rdm_dbEnd (db);
return rc;
int main (int argc, const char* const* argv)
RDM_RETCODE rc; /* Status/Error Return Code */
RDM_TFStfs; /* TFS Handle */
RDM_DBdb; /* Database Handle */
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
if (rc == sOKAY)
rc = rdm_tfsSetOption (tfs, "tfstype", "remote");
if (rc == sOKAY)
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
/* Allocate a database handle */
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
/* Open the database */
rc = rdm_dbOpen (db, "tfs://localhost:21553/bookStore", RDM_OPEN_SHARED);
if (rc == sOKAY)
/* Insert a row to database */
rc = writeARow (db);
if (rc == sOKAY)
/* Read the rows */
rc = readRows (db);
fprintf (stderr, "\nSorry, can't open bookStore database.\n");
fprintf (stderr, "Error: %s (%d): %s\n", rdm_retcodeGetName (rc), rc, rdm_retcodeGetDescription (rc));
RDM_RETCODE rdm_bcdFromString(const char *operand, RDM_BCD_T *result)
Convert a string to a BCD number.
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_cursorMoveToNext(RDM_CURSOR cursor)
Position a cursor to the next row in the collection.
Header for the native RDM Runtime API.
Allocate a TFS handle.
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
Definition: rdmtypes.h:306
RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
Get read locks.
RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
Read all columns from a row.
RDM_RETCODE rdm_dbClose(RDM_DB db)
Close the database associated with a database handle.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.
Definition: rdmretcodetypes.h:97
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.
RDM_RETCODE rdm_tfsSetOption(RDM_TFS tfs, const char *keyword, const char *strValue)
Set a single TFS option from a string.
RDM_RETCODE rdm_dbGetRows(RDM_DB db, RDM_TABLE_ID tableId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with rows based on a table id.
const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
Invoke RDM error handler.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
The RDM Binary Coded Decimal (BCD) data structure.
Definition: rdmbcdtypes.h:65
Definition: rdmtypes.h:253
RDM_RETCODE rdm_dbInsertRow(RDM_DB db, RDM_TABLE_ID tableId, const void *colValues, size_t bytesIn, RDM_CURSOR *pCursor)
Insert a new row into a table at the specified rowId.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
Terminate a TFS service.
RDM_RETCODE rdm_dbStartUpdate(RDM_DB db, const RDM_TABLE_ID *writeTableIds, uint32_t numWriteTableIds, const RDM_TABLE_ID *readTableIds, uint32_t numReadTableIds, RDM_TRANS *pTrans)
Get write locks.
Definition: rdmtypes.h:189
#define RDM_LEN(x)
Definition: psptypes.h:78
Definition: rdmretcodetypes.h:58
RDM status and error return codes.
Definition: rdmretcodetypes.h:43
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.