c-core/14_core/core14Example_main.c
#include "rdm.h"
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "bulkInsert_Tutorial_structs.h"
#include "bulkInsert_Tutorial_cat.h"
#include "rdmdatetimeapi.h"
#include "rdmstartupapi.h"
 
#include <stdio.h>
#include <stdlib.h>
 
int32_t main_bulkInsertTutorial (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_BULKINSERT};
 
                rc = rdm_dbSetCatalog (db, bulkInsert_Tutorial_cat);
 
                if (rc == sOKAY)
                {
                    /* Open the database. */
                    rc =
                        rdm_dbOpen (db, "bulkInsert_Tutorial"RDM_OPEN_SHARED);
                }
                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)
                {
                    /* Start an update transaction and lock the table */
                    rc = rdm_dbStartUpdate (
                        db, tables, RDM_LEN (tables), NULL, 0, NULL);
                }
                if (rc == sOKAY)
                {
                    BULKINSERT insertions[3];
                    RDM_PACKED_DATE_T rdmDate;
                    int16_t tzPST = -420;
                    uint32_t counter;
 
                    (void) rdm_dateFromString (
                        "11-11-2011"RDM_MMDDYYYY, &rdmDate);
                    insertions[0].id = rdmDate;
                    insertions[0]._id_has_value = RDM_COL_HAS_VALUE;
                    (void) rdm_dateToday (tzPST, &rdmDate);
                    insertions[1].id = rdmDate;
                    insertions[1]._id_has_value = RDM_COL_HAS_VALUE;
                    (void) rdm_dateFromString (
                        "09-17-2013"RDM_MMDDYYYY, &rdmDate);
                    insertions[2].id = rdmDate;
                    insertions[2]._id_has_value = RDM_COL_HAS_VALUE;
 
                    /* Insert multiple rows into the table */
                    rc = rdm_dbInsertRows (
                        db, TABLE_BULKINSERT, insertions, sizeof (insertions),
                        RDM_LEN (insertions), NULL, NULL);
 
                    for (counter = 0;
                         rc == sOKAY && counter < RDM_LEN (insertions);
                         counter++)
                    {
                        char outBuffer[100];
                        rdm_dateToString (
                            insertions[counter].idRDM_MMDDYYYY,
                            RDM_DEF_DATE_SEP, outBuffer, sizeof (outBuffer),
                            NULL);
                        printf ("Date Inserted: %s\n", outBuffer);
                    }
 
                    if (rc == sOKAY)
                    {
                        /* Commit a transaction */
                        rc = rdm_dbEnd (db);
                    }
                    else
                    {
                        /* Abort the transaction */
                        rdm_dbEndRollback (db);
                    }
                }
 
                if (rc == sOKAY)
                {
                    /* Start a read transaction and lock the table */
                    rc = rdm_dbStartRead (db, tables, RDM_LEN (tables), NULL);
                }
                if (rc == sOKAY)
                {
                    RDM_CURSOR cursor = NULL;
                    rc = rdm_dbGetRows (db, TABLE_BULKINSERT, &cursor);
                    if (rc == sOKAY)
                    {
                        rc = rdm_cursorMoveToFirst (cursor);
 
                        while (rc == sOKAY)
                        {
                            BULKINSERT recS;
 
                            rc = rdm_cursorReadRow (
                                cursor, &recS, sizeof (recS), NULL);
                            if (rc == sOKAY)
                            {
                                char outBuffer[100];
                                rdm_dateToString (
                                    recS.id, RDM_DEF_DATE_FORMAT,
                                    RDM_DEF_DATE_SEP, outBuffer,
                                    sizeof (outBuffer), NULL);
                                printf ("Date Retrieved: %s\n", outBuffer);
                                rc = rdm_cursorMoveToNext (cursor);
                            }
                        }
                        /* We will break out of the loop with a sENDOFCURSOR
                         * return code */
                        if (rc == sENDOFCURSOR)
                        {
                            rc = sOKAY;
                        }
 
                        /* Free the cursor allocated in the rdm_dbGetRows call
                         */
                        rdm_cursorFree (cursor);
                    }
 
                    /* Free the read locks */
                    rdm_dbEnd (db);
                }
                /* Free the databse handle */
                rdm_dbFree (db);
            }
        }
        /* Free the TFS handle */
        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 (bulkInsertTutorial)
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.
RDM_RETCODE rdm_dateToString(RDM_PACKED_DATE_T dateVal, RDM_DATE_FORMAT date_fmt, char date_sep, char *buf, size_t bufSize, size_t *puSize)
Convert a date to a string.
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.
RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
Read all columns from a row.
uint32_t RDM_PACKED_DATE_T
The RDM packed Date data structure.
Definition: rdmdatetimetypes.h:110
Header for the public Date/Time API.
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:95
#define RDM_DEF_DATE_SEP
The default date separator.
Definition: rdmdatetimetypes.h:33
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
RDM_RETCODE rdm_dbInsertRows(RDM_DB db, RDM_TABLE_ID tableId, const void *colValues, size_t bytesIn, uint32_t num, RDM_CURSOR *pCursorStatus, RDM_CURSOR *pCursor)
Insert rows into a table.
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_dateToday(int16_t time_zone, RDM_PACKED_DATE_T *pdt)
Get the current date.
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_MMDDYYYY
Definition: rdmdatetimetypes.h:147
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:303
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
#define RDM_DEF_DATE_FORMAT
The default date format.
Definition: rdmdatetimetypes.h:31
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:58
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.
RDM_RETCODE rdm_dateFromString(const char *str, RDM_DATE_FORMAT date_fmt, RDM_PACKED_DATE_T *pdv)
Convert from a string to a date.