core19Example_main.c

Basic tutorial in C where a pure in-memory database is created, a row is inserted and then a row is retrieved and displayed. This example needs a compiled schema, core19Example.sdl.

/*
** IN-MEMORY
** ---------
** This document describes the process to create a simple in-memory database,
** insert a record, and read data from the database. This application
*requires no
** persistent storage to run and can act as a simple example for disk less
** applications.
*/
#include "rdm.h" /* The RDM API. */
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "core19Example_structs.h" /* The core19Example database definitions.*/
#include "core19Example_cat.h" /* The core19Example database dictionary. */
#include "rdmstartupapi.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int32_t main_core19ExampleTutorial (int32_t argc, const char *const *argv)
{
RDM_TFS tfs;
RDM_DB db = NULL;
RDM_TABLE_ID tables[] = {TABLE_INFO};
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)
{
/* Associate the catalog with the database handle */
rc = rdm_dbSetCatalog (db, core19Example_cat);
if (rc == sOKAY)
{
/* Set the in-memory volatile database option */
rc = rdm_dbSetOptions (db, "storage=inmemory_volatile");
}
if (rc == sOKAY)
{
/* Open the database. */
rc = rdm_dbOpen (db, "core19Example", RDM_OPEN_SHARED);
if (rc == sOKAY)
{
/* Start an update transaction and lock the table */
db, tables, RDM_LEN (tables), NULL, 0, NULL);
if (rc == sOKAY)
{
INFO infoInsert;
/* Create a row in the in-memory database */
strcpy (
infoInsert.mychar, "I'm running in memory!");
infoInsert._mychar_has_value = RDM_COL_HAS_VALUE;
db, TABLE_INFO, &infoInsert,
sizeof (infoInsert), NULL);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
db, tables, RDM_LEN (tables), NULL);
if (rc == sOKAY)
{
RDM_CURSOR cursor = NULL;
rc = rdm_dbGetRows (db, TABLE_INFO, &cursor);
if (rc == sOKAY)
{
/* Move to the first row in the cursor */
rc = rdm_cursorMoveToFirst (cursor);
if (rc == sOKAY)
{
INFO infoRead;
/* read the row values */
cursor, &infoRead, sizeof (infoRead),
NULL);
if (rc == sOKAY)
{
printf ("%s\n\n", infoRead.mychar);
}
else
{
fprintf (
stderr,
"Sorry, I can't read the first "
"info row.");
}
}
/* Free the cursor allocated in the call to
* rdm_dbGetRows */
rdm_cursorFree (cursor);
}
rdm_dbEnd (db);
}
}
else
{
fprintf (
stderr,
"Sorry, I can't open the core19Example database.");
}
}
}
}
}
if (rc != sOKAY)
{
printf (
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (core19ExampleTutorial)
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_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.
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
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
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:96
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.
#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.
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.
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
Internal RDM Startup API used by startup macros.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:44
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.