c-core/16_core/core16Example_main.c
/*
 * COMPOUND KEY
 * ------------
 *
 * This document describes the process to create a simple database
 * containing a key, insert records, and read data through the key.
 *
 * For a full description of RDM keys, see Database Design in the
 * Users Guide.
 *
 * This example contains a single record type with two fields which
 * are referenced in a non-unique compound key.
 */
 
#include "rdm.h"
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "compound_key_structs.h"
#include "compound_key_cat.h"
#include "rdmstartupapi.h"
 
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
static const INFO infos[] = {
    {"John", 30, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"James", 33, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"Duncan", 37, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"Bill", 44, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"John", 27, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"Paul", 21, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"David", 27, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE},
    {"David", 27, RDM_COL_HAS_VALUERDM_COL_HAS_VALUE}};
 
int32_t main_compound_keyTutorial (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);
    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_INFO};
                RDM_CURSOR cursor = NULL;
                RDM_SEARCH_KEY key = {0};
                INFO_MYKEY_KEY mykey = {"", 0, RDM_COL_HAS_VALUE,
                                        RDM_COL_HAS_VALUE};
 
                rc = rdm_dbSetCatalog (db, compound_key_cat);
                if (rc == sOKAY)
                {
                    /* Open the database. */
                    rc = rdm_dbOpen (db, "compound_key"RDM_OPEN_SHARED);
                    if (rc != sOKAY)
                    {
                        printf ("I can't open the compound_key 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);
                    }
                }
 
                if (rc == sOKAY)
                {
                    rc = rdm_dbStartUpdate (
                        db, tables, RDM_LEN (tables), NULL, 0, NULL);
                }
                if (rc == sOKAY)
                {
                    uint32_t ii;
                    /* Insert the rows in the table */
                    for (ii = 0; ii < RDM_LEN (infos) && rc == sOKAY; ii++)
                    {
                        rc = rdm_dbInsertRow (
                            db, TABLE_INFO, &infos[ii], sizeof (infos[ii]),
                            NULL);
                    }
 
                    if (rc == sOKAY)
                    {
                        /* Commit a transaction */
                        rc = rdm_dbEnd (db);
                    }
                    else
                    {
                        /* Abort the transaction */
                        rdm_dbEndRollback (db);
                    }
                }
 
                if (rc == sOKAY)
                {
                    /* Do a key search on all 'John' records.  We will set
                       the numKeyCols to 1 so that we will only use the name
                       portion of the key.  We will retrieve all rows that
                       have a mychar value of 'John' and a myage value of
                       anything */
                    strcpy (mykey.mychar, "John");
                    mykey._mychar_has_value = RDM_COL_HAS_VALUE;
                    key.value = &mykey;
                    key.bytesIn = sizeof (mykey);
                    key.numKeyCols = 1;
                    key.stringLen = 0;
 
                    /* get a lock on the table */
                    rc = rdm_dbStartRead (db, tables, RDM_LEN (tables), NULL);
                }
                if (rc == sOKAY)
                {
                    rc = rdm_dbGetRowsByKeyInSearchKeyRange (
                        db, KEY_INFO_MYKEY, &key, &key, &cursor);
                    if (rc == sOKAY)
                    {
                        rc = rdm_cursorMoveToFirst (cursor);
 
                        while (rc == sOKAY)
                        {
                            INFO sInfo = {"", 0, RDM_COL_IS_NULL,
                                          RDM_COL_IS_NULL};
                            /* Read the full content of the current row */
                            rc = rdm_cursorReadRow (
                                cursor, &sInfo, sizeof (sInfo), NULL);
                            if (rc == sOKAY)
                            {
                                printf (
                                    "Row found doing a search key range "
                                    "lookup: "
                                    "%s, %d\n",
                                    sInfo.mychar, sInfo.myage);
                            }
 
                            /* Go to the next row in key order. */
                            if (rc == sOKAY)
                            {
                                rc = rdm_cursorMoveToNext (cursor);
                            }
                        }
 
                        /* We expect to break out of the loop when rc =
                         * sENDOFCURSOR
                         */
                        if (rc == sENDOFCURSOR)
                        {
                            rc = sOKAY;
                        }
 
                        /* Free the cursor allocated in the rdm_dbGet call */
                        rdm_cursorFree (cursor);
                    }
 
                    /* Free the lock on the table */
                    rdm_dbEnd (db);
                }
                rdm_dbFree (db);
            }
        }
        rdm_tfsFree (tfs);
    }
 
    if (rc != sOKAY)
    {
        printf (
            "There was an error in this Tutorial (%s - %s)\n",
            rdm_retcodeGetName (rc), rdm_retcodeGetDescription (rc));
        return EXIT_FAILURE;
    }
 
    return EXIT_SUCCESS;
}
 
RDM_STARTUP_EXAMPLE (compound_keyTutorial)
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.
uint16_t stringLen
Definition: rdmtypes.h:69
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:304
RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
Get read locks.
size_t bytesIn
Definition: rdmtypes.h:61
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_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:95
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
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
The RDM Partial Key data structure.
Definition: rdmtypes.h:59
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:303
const void * value
Definition: rdmtypes.h:60
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
uint16_t numKeyCols
Definition: rdmtypes.h:62
#define RDM_LEN(x)
Definition: psptypes.h:78
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:58
RDM_RETCODE rdm_dbGetRowsByKeyInSearchKeyRange(RDM_DB db, RDM_KEY_ID keyId, const RDM_SEARCH_KEY *startValue, const RDM_SEARCH_KEY *endValue, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with rows in a specified key range.
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.