core05Example_main.c

Indexing. Add 5 new records with random index values. Scan through index & print records in order. Each run adds 5 more records in the database. This example needs a compiled schema, core05.sdl.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "example_fcns.h"
#include "rdm.h"
#include "rdmstartupapi.h"
/* Generated \c struct and \c typedef definitions to be used with the RDM APIs
*/
#include "core05_structs.h"
/* Generated catalog definition to be used with the RDM rdm_dbSetCatalog() API
*/
#include "core05_cat.h"
const char *const description = "Demonstrates using an index for ordering";
const RDM_CMDLINE_OPT opts[] = {{NULL, NULL, NULL, NULL}};
/*
* \mainpage Core05 Popcorn Example
*
* Each time you run this example:
* \li The database is initialized (if necessary)
* \li 5 new person records are added to the database
* \li A list of each person is displayed in index order.
*
* \par Table of Contents
*
* - \subpage hDB
* - \subpage hPGMfunc
*
* For additional information, please refer to the product documentation at
* http://docs.raima.com/.
*
* \page hDB Database Schema
*
* \par Database Schema Definition
*
* The DDL (Database Definition Language) specification for the database used
* in this example is located in the file \c core05.sdl.
*
* \include core05.sdl
*
* The schema was compiled using the RDM rdm-compile utility with the -s option
* to generate C-structures for interfacing with the database.
*
* \code rdm-compile -s core05.sdl \endcode
*
* Each of these functions returns an integer status code, where the
* value sOKAY indicates a successful call.
*
* The actual database will be stored in a directory named 'core05.rdm' in the
* project directory.
*
* \page hPGMfunc Program Functions
* \li main() - \copybrief main
*/
/* \brief Insert one row into the core02 database
*
* This function adds one more row to the core02 database. The row contains only
* a non-indexed character string.
*
* @return Returns an \c RDM_RETCODE code (\b sOKAY if successful)
*/
RDM_RETCODE insertFiveRows (
RDM_DB hDB) /*< [in] RDM db handle with the database open */
{
PERSON person_rec;
rc = rdm_dbStartUpdate (hDB, RDM_LOCK_ALL, 0, NULL, 0, NULL);
if (rc == sOKAY)
{
int ii;
printf ("Adding 5 new person records\n");
for (ii = 0; ii < 5; ii++)
{
unsigned int value;
value = (unsigned int) rand () % 10000;
sprintf (person_rec.last_four_ssn, "%04u", value);
hDB, TABLE_PERSON, &person_rec, sizeof (person_rec), NULL);
printf ("\t%s\n", person_rec.last_four_ssn);
}
if (rc == sOKAY)
rc = rdm_dbEnd (hDB);
else
rc = rdm_dbEndRollback (hDB);
}
return rc;
}
/* \brief Reads and displays all rows in the core02 database
*
* This function reads and displays all rows in the core02 database. Each time
* the core02 program is executed, an additional row will be added to the
* database.
*
* @return Returns an \c RDM_RETCODE code (\b sOKAY if successful)
*/
RDM_RETCODE readAllRows (
RDM_DB hDB) /*< [in] RDM db handle with the database open */
{
PERSON person_rec;
RDM_CURSOR cursor = NULL;
rc = rdm_dbStartRead (hDB, RDM_LOCK_ALL, 0, NULL);
if (rc == sOKAY)
{
printf ("\nList all records in index order\n");
rc = rdm_dbGetRowsByKey (hDB, KEY_PERSON_LAST_FOUR_SSN, &cursor);
if (rc == sOKAY)
{
for (rc = rdm_cursorMoveToFirst (cursor); rc == sOKAY;
rc = rdm_cursorMoveToNext (cursor))
{
cursor, &person_rec, sizeof (person_rec), NULL);
printf ("%s\n", person_rec.last_four_ssn);
}
rdm_cursorFree (cursor);
}
rdm_dbEnd (hDB);
/* We expect rc to be sENDOFCURSOR when we break out of the loop */
if (rc == sENDOFCURSOR)
{
rc = sOKAY; /* change status to sOKAY because sENDOFCURSOR was
expected. */
}
else
{
}
}
return rc;
}
/* \brief Main function for core05 example
*
* The function initializes the RDM environment and runs the create, read
* operations.
*
* @return Returns the \c RDM_RETCODE on exit.
*/
int main_core05 (int argc, const char *const *argv)
{
RDM_TFS hTFS;
RDM_DB hDB;
/* Seed the random number generator */
srand ((unsigned int) time (NULL));
rc = rdm_cmdlineInit (&cmd, argc, argv, description, opts);
if (rc != sCMD_USAGE)
if (rc == sOKAY)
{
rc = exampleOpenDatabase (&hTFS, &hDB, "core05", core05_cat);
if (rc == sOKAY)
{
rc = insertFiveRows (hDB);
if (rc == sOKAY)
{
rc = readAllRows (hDB);
}
exampleCleanup (hTFS, hDB);
}
}
return (int) rc;
}
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.
RDM_RETCODE rdm_cursorMoveToNext(RDM_CURSOR cursor)
Position a cursor to the next row in the collection.
Header for the native RDM Runtime API.
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
@ sCMD_USAGE
Definition: rdmretcodetypes.h:72
struct RDM_CURSOR_S * RDM_CURSOR
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.
#define exampleOpenDatabase(tfs, db, name, catalog)
Definition: example_fcns.h:18
The buffer used by the command line parser to hold state information.
Definition: rdmcmdlinetypes.h:85
RDM_RETCODE rdm_dbGetRowsByKey(RDM_DB db, RDM_KEY_ID keyId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with a row set based on a key.
#define exampleCleanup(tfs, db)
Definition: example_fcns.h:30
#define print_error(rc)
Definition: example_fcns.h:17
@ sOKAY
Definition: rdmretcodetypes.h:98
Generic usage function option record.
Definition: rdmcmdlinetypes.h:32
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
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.
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
RDM_RETCODE rdm_cmdlineInit(RDM_CMDLINE *cmd, int32_t argc, const char *const argv[], const char *description, const RDM_CMDLINE_OPT *opts)
Initialize an RDM_CMDLINE buffer and validate the command line.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
#define RDM_LOCK_ALL
Definition: rdmtypes.h:170
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.
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:59
Internal RDM Startup API used by startup macros.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:44