HelloWorld.c Source File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rdm.h"                   /* The RDM API. */
#include "HelloWorld_structs.h"    /* The hello_world database definitions. */
#include "HelloWorld_cat.h"        /* The hello_world database catalog */
#include "rdmstartupapi.h"         /* Definition for RDM_STARTUP_EXAMPLE() */

/* Read "helloworld" row to database */
static RDM_RETCODE readARow(
    RDM_DB db)
{
    RDM_TABLE_ID tables[] = { TABLE_INFO };
    RDM_RETCODE  rc;

    /* Start an update transaction and lock the table */
    rc = rdm_dbStartRead(db, tables, RDM_LEN(tables), NULL);
    if (rc == sOKAY)
    {
        RDM_CURSOR cursor;

        rc = rdm_dbAllocCursor(db, &cursor);
        if (rc == sOKAY)
        {
            /* Fill a cursor with all the rows in a table */
            rc = rdm_dbGetRows(db, TABLE_INFO, &cursor);
            if (rc == sOKAY)
            {
                /* Move to the first row in the info table */
                rc = rdm_cursorMoveToFirst(cursor);
                if (rc == sOKAY)
                {
                    INFO infoRead;      /* Row buffer */

                    /* Read the full content of the current record */
                    rc = rdm_cursorReadRow(cursor, &infoRead, sizeof(infoRead), NULL);
                    if (rc == sOKAY)
                    {
                        printf("%s\n", infoRead.MYCHAR);
                    }
                }
            }
            /* Free the cursor */
            rdm_cursorFree(cursor);
        }
        /* Free the read lock on the table */
        rdm_dbEnd(db);
    }
    return rc;
}

/* Insert "helloworld" row to database */
static RDM_RETCODE writeARow(
    RDM_DB  db)
{
    RDM_TABLE_ID tables[] = { TABLE_INFO };
    RDM_RETCODE  rc;

    /* Start an update transaction and lock the table */
    rc = rdm_dbStartUpdate(db, tables, RDM_LEN(tables), NULL, 0, NULL);
    if (rc == sOKAY)
    {
        INFO infoInserted;      /* Row buffer */

        /* Populate the mychar column in the Row buffer */
        strcpy(infoInserted.MYCHAR, "Hello World! - using the embedded TFS");

        /* Insert a row into the table */
        rc = rdm_dbInsertRow(db, TABLE_INFO, &infoInserted, sizeof(infoInserted), NULL);
        if (rc == sOKAY)
        {
            /* Commit a transaction */
            rc = rdm_dbEnd(db);
        }
        else
        {
            /* Abort the transaction */
            rdm_dbEndRollback(db);
        }
    }
    return rc;
}

int32_t main_HelloWorld(
    int32_t            argc,
    const char* const* argv)
{
    RDM_RETCODE  rc;        /* Status/Error Return Code */
    RDM_TFS      tfs;       /* TFS Handle */
    RDM_DB       db;        /* Database Handle */

    RDM_UNREF(argc);
    RDM_UNREF(argv);

    /* Allocate a TFS Handle */
    rc = rdm_rdmAllocTFS(&tfs);
    if (rc == sOKAY)
    {
        /* Set the database location (docroot) to the /ram0 directory */
        rc = rdm_tfsSetOption(tfs, "docroot", "/ram0");
    }
    if (rc == sOKAY)
    {
        rc = rdm_tfsInitialize(tfs);
        if (rc == sOKAY)
        {
            /* Allocate a database hande */
            rc = rdm_tfsAllocDatabase(tfs, &db);
            if (rc == sOKAY)
            {
                /* Associate the catalog with the database handle */
                rc = rdm_dbSetCatalog(db, HelloWorld_cat);
                if (rc == sOKAY)
                {
                    /* Open the database */
                    rc = rdm_dbOpen(db, "HelloWorld", RDM_OPEN_SHARED);
                    if (rc == sOKAY)
                    {
                        /* Insert "helloworld" row to database */
                        rc = writeARow(db);
                        if (rc == sOKAY)
                        {
                            /* Read "helloworld" row to database */
                            rc = readARow(db);
                        }
                        rdm_dbClose(db);
                    }
                    else
                    {
                        fprintf(stderr, "\nSorry, I can't open the HelloWorld database.\n");
                    }
                }
                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(HelloWorld)