core15Example_main.c
Simple example on how to create and work with a circular limited table. This example needs a compiled schema, core15Example.sdl.
/*
* CIRCULAR TABLE
* --------------
* This document describes the process to create a circular record in a
* database, insert a circular record, and read data from this database.
*/
#include "rdm.h" /* The RDM API. */
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "core15Example_structs.h" /* The one_to_many database definitions. */
#include "core15Example_cat.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#include <stdlib.h>
{"Miles Davis", RDM_COL_HAS_VALUE},
{"Charles Mingus", RDM_COL_HAS_VALUE},
{"Chico Hamilton", RDM_COL_HAS_VALUE},
{"John Coltrane", RDM_COL_HAS_VALUE}};
RDM_EXPORT int32_t main_circular_tableTutorial (int32_t argc, const char *const *argv)
{
RDM_RETCODE rc;
RDM_TFS tfs;
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
RDM_DB db;
/* Allocate a database hande */
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
RDM_TABLE_ID tables[] = {TABLE_ALBUM};
uint32_t ii;
rc = rdm_dbSetCatalog (db, core15Example_cat);
if (rc == sOKAY)
{
/* Open the database. */
if (rc != sOKAY)
{
printf ("I can't open the core15 database.");
}
}
if (rc == sOKAY)
{
/* Start an update transaction and lock the table */
rc = rdm_dbStartUpdate (
db, tables, RDM_LEN (tables), NULL, 0, NULL);
}
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);
}
}
/* Insert rows into the circular table */
{
RDM_CURSOR cursor = NULL;
/* Start an update transaction and lock the table */
rc = rdm_dbStartUpdate (
db, tables, RDM_LEN (tables), NULL, 0, NULL);
if (rc == sOKAY)
{
rc = rdm_dbInsertRow (
db, TABLE_ALBUM, &albums[ii], sizeof (albums[ii]),
NULL);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
rdm_dbEndRollback (db);
}
}
/* Read and dump all album records. */
if (rc == sOKAY)
{
printf ("\nDUMPING ALL ROWS\n");
rc = rdm_dbGetRows (db, TABLE_ALBUM, &cursor);
}
if (rc == sOKAY)
{
/* Lock the table for reading */
rc = rdm_dbStartRead (
db, tables, RDM_LEN (tables), NULL);
if (rc == sOKAY)
{
rc == sOKAY;
rc = rdm_cursorMoveToNext (cursor))
{
ALBUM sAlbum;
rc = rdm_cursorReadRow (
cursor, &sAlbum, sizeof (sAlbum), NULL);
if (rc == sOKAY)
{
printf (" %s\n", sAlbum.artist);
}
}
/* Reset the rc value, the for loop will end with a
* sENDOFCURSOR when there are no more rows. */
{
rc = sOKAY;
}
/* Free the read locks */
rdm_dbEnd (db);
}
/* Free the cursor */
rdm_cursorFree (cursor);
}
}
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 (circular_tableTutorial)
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_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.
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_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.
Internal RaimaDB Startup API used by startup macros.