learn/bookStore_vxWorks7.c
#include <stdio.h>
#include "rdm.h"
#include "bookStore_structs.h"
#include "bookStore_cat.h"
#include "rdmstartupapi.h"
/* Write a row to from author and book tables */
{
RDM_RETCODE rc;
RDM_TABLE_ID tables[] = {TABLE_BOOK, TABLE_AUTHOR};
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};
size_t 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';
}
ii++;
}
/* Start an update transaction and lock the table */
{
AUTHOR authInsert; /* Row buffer */
BOOK bookInsert; /* Row buffer */
/* Populate the columns in the AUTHOR Row buffer */
strcpy_s (authInsert.LASTNAME, sizeof (authInsert.LASTNAME), last_name);
strcpy_s (authInsert.FULLNAME, sizeof (authInsert.FULLNAME), full_name);
authInsert._FULLNAME_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_s (bookInsert.TITLE, sizeof (bookInsert.TITLE), title);
bookInsert._TITLE_has_value = RDM_COL_HAS_VALUE;
strcpy_s (
bookInsert.BOOKISBN, sizeof (bookInsert.BOOKISBN), 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 */
fprintf (
stderr,
"A problem occurred when adding data to the database.\n");
fprintf (
rdm_retcodeGetDescription (rc));
rdm_dbEndRollback (db);
}
}
return rc;
}
/* Read rows from author table */
{
RDM_TABLE_ID tables[] = { TABLE_AUTHOR };
RDM_RETCODE rc;
AUTHOR authRead;
RDM_CURSOR cursor = 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 */
if (rc == sOKAY)
{
printf("%s", authRead.LASTNAME);
/* Move to the next row in the cursor */
rc = rdm_cursorMoveToNext(cursor);
}
}
/* We expect to break out of the loop with a sENDOFCURSOR code*/
{
rc = sOKAY;
}
/* Free the cursor allocated in rdm_dbGetRows */
rdm_cursorFree(cursor);
}
/* release the read locks */
rdm_dbEnd(db);
}
return rc;
}
int main_bookStore(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 */
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS(&tfs);
if (rc == sOKAY)
{
/* Set the database location (docroot) to the /ram0 directory */
if (rc == sOKAY)
{
rc = rdm_tfsInitialize(tfs);
if (rc == sOKAY)
{
/* Allocate a database handle */
rc = rdm_tfsAllocDatabase(tfs, &db);
if (rc == sOKAY)
{
rc = rdm_dbSetCatalog(db, bookStore_cat);
/* Open the database */
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");
}
rdm_dbFree(db);
}
}
}
rdm_tfsFree(tfs);
}
if (rc != sOKAY)
{
printf("There was an error in this Tutorial\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE(bookStore)
RDM_RETCODE rdm_bcdFromString(const char *operand, RDM_BCD_T *result)
Convert a string to a BCD number.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
Header for the native RaimaDB Runtime API.
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.
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RaimaDB database using the specified database handle.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RaimaDB 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 RaimaDB error handler.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
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.
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.
Internal RaimaDB Startup API used by startup macros.