bookStore_main.c Example TFS Embed Program

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

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

#include <stdio.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 };    int len;    int i = 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 (i < (sizeof(inputs) / sizeof(inputs[0])))    {        len = strlen(inputs[i]);        if (inputs[i][len - 1] == '\n')        {            inputs[i][len - 1] = '\0';        }        i++;    }    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)        {            /* 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);        }        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("", &tfs);    if (rc == sOKAY)    {        /* Allocate a database handle */        rc = rdm_tfsAllocDatabase(tfs, &db);        if (rc == sOKAY)        {            /* Open the database */            rc = rdm_dbOpen(db, "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;}