learn/bookStore_embed.c
#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 */
{
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 */
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 (stderr, "Error: %s (%d): %s\n", rdm_retcodeGetName (rc), rc, rdm_retcodeGetDescription (rc));
rdm_dbEndRollback (db);
}
}
return rc;
}
/* Read rows from author table */
{
RDM_RETCODE rc;
RDM_TABLE_ID tables[] = {TABLE_AUTHOR};
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 (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)
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
/* Allocate a database handle */
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
/* 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
{
fprintf (stderr, "\nSorry, can't open bookStore database.\n");
fprintf (stderr, "Error: %s (%d): %s\n", rdm_retcodeGetName (rc), rc, rdm_retcodeGetDescription (rc));
}
rdm_dbFree (db);
}
}
rdm_tfsFree (tfs);
}
return rc == sOKAY ? EXIT_SUCCESS : EXIT_FAILURE;
}
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_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_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.