bookStore_main.c Example TFS Remote Program

This program is based on the bookStore.sdl Example Schema.

(Click here to download bookStore_client.c source file.)

#include <stdio.h>#include <string.h>#include "rdm.h"#include "bookStore_structs.h"/* Display message and populate buffer with input from   stdin. The input buffer will have the LF character   stripped off.*/static void getInput(    const char* msg,    char* input,    int len){    char* p;    puts(msg);    fgets(input, len, stdin);    p = strrchr(input, '\n');    if (p)        *p = '\0'; /* remove line feed char */}/* 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];    getInput("ADDING AUTHOR TO DATABASE\nAuthor Last Name:", last_name, sizeof(last_name));    getInput("Author Full Name:", full_name, sizeof(full_name));    getInput("ADDING BOOK TO DATABASE\nTitle of Book:", title, sizeof(title));    getInput("Enter id located on book:", bookid, sizeof(bookid));    getInput("Price:", price, sizeof(price));    RDM_TABLE_ID tables[] = { TABLE_BOOK, TABLE_AUTHOR };    RDM_RETCODE  rc;    /* 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)        {            /* Populate the columns in the BOOK Row buffer */            strcpy(bookInsert.TITLE, title);            bookInsert._TITLE_has_value = RDM_COL_HAS_VALUE;            strcpy(bookInsert.BOOKID, bookid);            /* Convert the string to a binary-coded decimal */            rdm_bcdFromString(price, &bookInsert.PRICE);            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);        }        else        {            /* Abort the transaction */            printf("A problem occurred when adding data to the database.\n");            printf("Error: %s (%d): %s\n", rdm_retcodeGetName(rc), rc, rdm_retcodeGetDescription(rc));            rdm_dbEndRollback(db);        }    }    return rc;}/* Read rows from author table */static RDM_RETCODE readRows(RDM_DB db){    RDM_TABLE_ID tables[] = { TABLE_AUTHOR };    RDM_RETCODE  rc;    AUTHOR authRead;    RDM_CURSOR cursor = NULL;    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_TFS      tfs;       /* TFS Handle */    RDM_DB       db;        /* Database Handle */    RDM_UNREF(argc);    RDM_UNREF(argv);    /* Allocate a TFS Handle */    rc = rdm_rdmAllocTFS("tfstype=remote", &tfs);     if (rc == sOKAY)    {        /* Allocate a database handle */        rc = rdm_tfsAllocDatabase(tfs, &db);        if (rc == sOKAY)        {            /* Open the database */            rc = rdm_dbOpen(db, "tfs-tcp://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);                }                rdm_dbClose(db);            }            else            {                printf("\nSorry, can't open bookStore database.\n");                printf("Error: %s (%d): %s\n", rdm_retcodeGetName(rc), rc, rdm_retcodeGetDescription(rc));            }            rdm_dbFree(db);        }    }    rdm_tfsFree(tfs);    if (rc != sOKAY)    {        printf("There was an error in this Tutorial\n");        return EXIT_FAILURE;    }    return EXIT_SUCCESS;}