core31Example_main.c

Basic tutorial in C where a simple one to many relationship between two tables is made and used. This example needs a compiled schema, core31Example.sdl.

/*
* ONE-TO-MANY
* -----------
*
* This document describes the process to create a simple one-to-many
* relationship between two tables in a RDM database. We'll insert
* rows and read them back from the database.
*/
#include "rdm.h" /* The RDM API. */
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "core31Example_structs.h" /* The core31Example database definitions. */
#include "core31Example_cat.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#include <stdlib.h>
static const ONE ones[] = {{"John"}, {"James"}, {"Duncan"}};
static const MANY johns[] = {
{"John's First", "John", RDM_COL_HAS_VALUE, RDM_COL_HAS_VALUE},
{"John's Second", "John", RDM_COL_HAS_VALUE, RDM_COL_HAS_VALUE},
{"John's Third", "John", RDM_COL_HAS_VALUE, RDM_COL_HAS_VALUE},
static const MANY no_one[] = {
{"No One's First", "", RDM_COL_HAS_VALUE, RDM_COL_IS_NULL},
{"No One's Second", "", RDM_COL_HAS_VALUE, RDM_COL_IS_NULL}};
static const MANY duncans[] = {
{"Duncan's First", "", RDM_COL_HAS_VALUE, RDM_COL_IS_NULL},
{"Duncan's Second", "", RDM_COL_HAS_VALUE, RDM_COL_IS_NULL}};
int32_t main_core31ExampleTutorial (int32_t argc, const char *const *argv)
{
RDM_RETCODE rc; /*lint -esym(850,iStatus) */
RDM_TFS tfs;
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
/* Allocate a database hande */
if (rc == sOKAY)
{
RDM_DB db;
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
/* Override default DURABLE setting to CONSISTENT */
rc = rdm_dbSetOptions (db, "durability=consistent");
}
if (rc == sOKAY)
{
RDM_TABLE_ID tables[] = {TABLE_ONE, TABLE_MANY};
RDM_TABLE_ID table_one[] = {TABLE_ONE};
RDM_TABLE_ID table_many[] = {TABLE_MANY};
rc = rdm_dbSetCatalog (db, core31Example_cat);
if (rc == sOKAY)
{
/* Open the database. */
rc = rdm_dbOpen (db, "core31Example", RDM_OPEN_SHARED);
if (rc != sOKAY)
{
printf ("Can't open the core31Example database.");
}
}
if (rc == sOKAY)
{
/* Start an update transaction and lock ALL tables */
rc = rdm_dbStartUpdate (db, RDM_LOCK_ALL, 0, NULL, 0, NULL);
}
if (rc == sOKAY)
{
/* Remove all of the rows from the database */
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
/* Add all of the rows to the one table*/
if (rc == sOKAY)
{
/* Start an transaction and lock the "one" table for updates
*/
db, table_one, RDM_LEN (table_one), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t iO;
for (iO = 0; iO < RDM_LEN (ones) && rc == sOKAY; iO++)
{
/* Insert rows into the "one" table */
db, TABLE_ONE, &ones[iO], sizeof (ones[iO]), NULL);
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
/* Add John's rows to the many table using referential integrity
*/
if (rc == sOKAY)
{
/* Start a transaction and lock the "many" table for
* updates, and the "one" table for reads */
db, table_many, RDM_LEN (table_many), table_one,
RDM_LEN (table_one), NULL);
}
if (rc == sOKAY)
{
uint32_t iM;
for (iM = 0; iM < RDM_LEN (johns) && rc == sOKAY; iM++)
{
/* Insert rows into the "many" table */
db, TABLE_MANY, &johns[iM], sizeof (johns[iM]),
NULL);
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
/* Add "No One's" rows to the many table */
if (rc == sOKAY)
{
/* Start an transaction and lock the "many" table for
* updates */
db, table_many, RDM_LEN (table_many), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t iM;
for (iM = 0; iM < RDM_LEN (no_one) && rc == sOKAY; iM++)
{
/* Insert rows into the "many" table */
db, TABLE_MANY, &no_one[iM], sizeof (no_one[iM]),
NULL);
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
/* Add "Duncan's" rows to the many table, assocate the with */
if (rc == sOKAY)
{
/* Start an transaction and lock the "many" table for
updates and the "one" table for reading */
db, table_many, RDM_LEN (table_many), table_one,
RDM_LEN (table_one), NULL);
}
if (rc == sOKAY)
{
RDM_CURSOR cursorOne = NULL;
RDM_CURSOR cursorMany = NULL;
uint32_t iM;
/* Get a cursor that is position to the "Duncan" row of the
* one table */
db, KEY_ONE_MYCHAR, &ones[2], sizeof (ones[2]),
&cursorOne);
for (iM = 0; iM < RDM_LEN (no_one) && rc == sOKAY; iM++)
{
/* Insert rows into the "many" table */
db, TABLE_MANY, &duncans[iM], sizeof (duncans[iM]),
&cursorMany);
if (rc == sOKAY)
{
cursorMany, REF_MANY_MYCHAR_ONE, cursorOne);
}
}
/* Free the cursors used for link the one and many rows */
rdm_cursorFree (cursorOne);
rdm_cursorFree (cursorMany);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
/* Display all of the rows in many table */
if (rc == sOKAY)
{
/* Start a transaction and lock the tables for reads */
db, tables, RDM_LEN (tables), NULL);
}
if (rc == sOKAY)
{
RDM_CURSOR cursor = NULL;
rc = rdm_dbGetRows (db, TABLE_MANY, &cursor);
if (rc == sOKAY)
{
/* Navigate to the first row in the cursor */
printf ("Displaying all of the rows in the many "
"table\n");
printf ("many.mychar_one many.mychar\n");
printf (
"____________________ ____________________\n");
rc = rdm_cursorMoveToFirst (cursor);
}
while (rc == sOKAY)
{
MANY sMany;
/* Read the row column values */
cursor, &sMany, sizeof (sMany), NULL);
if (rc == sOKAY)
{
printf (
"%-20s %-20s\n",
sMany._mychar_one_has_value ==
? "**NULL**"
: sMany.mychar_one,
sMany._mychar_has_value == RDM_COL_IS_NULL
? "**NULL**"
: sMany.mychar);
/* 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);
}
/* Display all of the rows in one table, with all of the
many rows associated with a one row */
if (rc == sOKAY)
{
/* Start a transaction and lock the tables for reads */
db, tables, RDM_LEN (tables), NULL);
}
if (rc == sOKAY)
{
RDM_CURSOR cursorOne = NULL;
RDM_CURSOR cursorMany = NULL;
rc = rdm_dbGetRows (db, TABLE_ONE, &cursorOne);
if (rc == sOKAY)
{
/* Navigate to the first row in the cursor */
printf ("\n\nDisplaying all of the rows in the one "
"table (and their associated many rows)\n");
rc = rdm_cursorMoveToFirst (cursorOne);
}
while (rc == sOKAY)
{
ONE sOne;
/* Read the row column values */
cursorOne, &sOne, sizeof (sOne), NULL);
if (rc == sOKAY)
{
printf ("%s\n", sOne.mychar);
/* Get a cursor containing the many rows
associated with the current one row */
cursorOne, REF_MANY_MYCHAR_ONE,
&cursorMany);
}
if (rc == sOKAY)
{
rc = rdm_cursorMoveToFirst (cursorMany);
}
while (rc == sOKAY)
{
MANY sMany;
cursorMany, COL_MANY_MYCHAR, sMany.mychar,
sizeof (sMany.mychar), NULL);
if (rc == sOKAY)
{
printf (" %s\n", sMany.mychar);
}
else if (rc == sNULLVAL)
{
printf (" **NULL**\n");
rc = sOKAY;
}
if (rc == sOKAY)
{
rc = rdm_cursorMoveToNext (cursorMany);
}
}
/* We will break out of the while loop with
* sENDOFCURSOR return code */
if (rc == sENDOFCURSOR)
{
rc = sOKAY;
}
/* Move to the next row in the cursor */
if (rc == sOKAY)
{
rc = rdm_cursorMoveToNext (cursorOne);
}
}
/* We expect to break out of the loop with a
* sENDOFCURSOR code*/
if (rc == sENDOFCURSOR)
{
rc = sOKAY;
}
/* Free the cursors allocated in rdm_dbGetRows and
* rdm_cursorGetMemberRows */
rdm_cursorFree (cursorOne);
rdm_cursorFree (cursorMany);
/* release the read locks */
rdm_dbEnd (db);
}
}
}
}
}
if (rc != sOKAY)
{
printf (
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (core31ExampleTutorial)
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_cursorMoveToNext(RDM_CURSOR cursor)
Position a cursor to the next row in the collection.
RDM_RETCODE rdm_dbSetOptions(RDM_DB db, const char *optString)
Set RDM options.
Header for the native RDM Runtime API.
RDM_RETCODE rdm_rdmAllocTFS(RDM_TFS *phTFS)
Allocate a TFS handle.
Header for the RDM Core API.
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
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.
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
#define RDM_COL_IS_NULL
Definition: rdmtypes.h:190
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
RDM_RETCODE rdm_cursorLinkRow(RDM_CURSOR cursor, RDM_REF_ID refId, RDM_CURSOR cursorOwner)
Link a row to an owner.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.
@ sOKAY
Definition: rdmretcodetypes.h:97
RDM_RETCODE rdm_dbGetRowsByKeyAtKey(RDM_DB db, RDM_KEY_ID keyId, const void *keyValue, size_t len, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with a row set that is ordered by key value and is initially positioned at th...
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.
Header for the Transactional File Server (TFS) API.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
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 RDM error handler.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
@ sNULLVAL
Definition: rdmretcodetypes.h:73
RDM_RETCODE rdm_cursorGetMemberRows(RDM_CURSOR ownerCursor, RDM_REF_ID refId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with members.
#define RDM_UNREF(a)
Definition: psptypes.h:45
@ RDM_OPEN_SHARED
Definition: rdmtypes.h:253
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_cursorReadColumn(RDM_CURSOR cursor, RDM_COLUMN_ID columnId, void *columnValue, size_t bytesIn, size_t *bytesOut)
Read a single column from a table row.
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
#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.
#define RDM_COL_HAS_VALUE
Definition: rdmtypes.h:189
#define RDM_LEN(x)
Definition: psptypes.h:78
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:58
Internal RDM Startup API used by startup macros.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:43
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.