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[] = {
static const MANY no_one[] = {
static const MANY duncans[] = {
RDM_EXPORT 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);
{
rc = rdm_tfsInitialize (tfs);
/* Allocate a database hande */
if (rc == sOKAY)
{
RDM_DB db;
rc = rdm_tfsAllocDatabase (tfs, &db);
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. */
if (rc != sOKAY)
{
printf ("Can't open the core31 database.");
}
}
if (rc == sOKAY)
{
/* Start an update transaction and lock ALL tables */
}
if (rc == sOKAY)
{
/* Remove all of the rows from the database */
rc = rdm_dbDeleteAllRowsFromDatabase (db);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
rdm_dbEndRollback (db);
}
}
/* Add all of the rows to the one table*/
if (rc == sOKAY)
{
/* Start an transaction and lock the "one" table for updates
*/
rc = rdm_dbStartUpdate (
db, table_one, RDM_LEN (table_one), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t iO;
{
/* Insert rows into the "one" table */
rc = rdm_dbInsertRow (
db, TABLE_ONE, &ones[iO], sizeof (ones[iO]), NULL);
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
rdm_dbEndRollback (db);
}
}
/* 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 */
rc = rdm_dbStartUpdate (
db, table_many, RDM_LEN (table_many), table_one,
RDM_LEN (table_one), NULL);
}
if (rc == sOKAY)
{
uint32_t iM;
{
/* Insert rows into the "many" table */
rc = rdm_dbInsertRow (
db, TABLE_MANY, &johns[iM], sizeof (johns[iM]),
NULL);
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
rdm_dbEndRollback (db);
}
}
/* Add "No One's" rows to the many table */
if (rc == sOKAY)
{
/* Start an transaction and lock the "many" table for
* updates */
rc = rdm_dbStartUpdate (
db, table_many, RDM_LEN (table_many), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t iM;
{
/* Insert rows into the "many" table */
rc = rdm_dbInsertRow (
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 */
rdm_dbEndRollback (db);
}
}
/* 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 */
rc = rdm_dbStartUpdate (
db, table_many, RDM_LEN (table_many), table_one,
RDM_LEN (table_one), NULL);
}
if (rc == sOKAY)
{
RDM_CURSOR curOne = NULL;
RDM_CURSOR curMany = NULL;
uint32_t iM;
/* Get a cursor that is position to the "Duncan" row of the
* one table */
rc = rdm_dbGetRowsByKeyAtKey (
db, KEY_ONE_MYCHAR, &ones[2], sizeof (ones[2]),
&curOne);
{
/* Insert rows into the "many" table */
rc = rdm_dbInsertRow (
db, TABLE_MANY, &duncans[iM], sizeof (duncans[iM]),
&curMany);
if (rc == sOKAY)
{
rc = rdm_cursorLinkRow (
curMany, REF_MANY_MYCHAR_ONE, curOne);
}
}
/* Free the cursors used for link the one and many rows */
rdm_cursorFree (curOne);
rdm_cursorFree (curMany);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
rdm_dbEndRollback (db);
}
/* Display all of the rows in many table */
if (rc == sOKAY)
{
/* Start a transaction and lock the tables for reads */
rc = rdm_dbStartRead (
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 */
rc = rdm_cursorReadRow (
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*/
{
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 */
rc = rdm_dbStartRead (
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 */
rc = rdm_cursorReadRow (
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 */
rc = rdm_cursorGetMemberRows (
cursorOne, REF_MANY_MYCHAR_ONE,
&cursorMany);
}
if (rc == sOKAY)
{
rc = rdm_cursorMoveToFirst (cursorMany);
}
while (rc == sOKAY)
{
MANY sMany;
rc = rdm_cursorReadColumn (
cursorMany, COL_MANY_MYCHAR, sMany.mychar,
sizeof (sMany.mychar), NULL);
if (rc == sOKAY)
{
printf (" %s\n", sMany.mychar);
}
{
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);
}
}
rdm_dbFree (db);
}
}
rdm_tfsFree (tfs);
}
if (rc != sOKAY)
{
printf (
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (core31ExampleTutorial)
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
Header for the native RaimaDB Runtime API.
Header for the RaimaDB Core API.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
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.
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 RaimaDB database using the specified database handle.
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_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RaimaDB db.
Header for the Transactional File Server (TFS) API.
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_cursorGetMemberRows(RDM_CURSOR ownerCursor, RDM_REF_ID refId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with members.
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.
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.