core33Example_main.c
                                            
                                            Basic tutorial in C where a database with a unique key is created, a row is inserted and then a row is retrieved and displayed. This example needs a compiled schema, core33Example.sdl.
/*
                                                    
                                                     * UNIQUE KEY
                                                    
                                                     * ----------
                                                    
                                                     *
                                                    
                                                     * This document describes the process to create a simple database
                                                    
                                                     * containing a unique 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 a single field
                                                    
                                                     * which is designated as a unique key. This means that every record
                                                    
                                                     * of type info that is successfully inserted into the database will
                                                    
                                                     * have a unique value. If an insertion is attempted with a key field
                                                    
                                                     * value that already exists in the database, the insertion is
                                                    
                                                     * rejected and an error code is returned from the d_fillnew function.
                                                    
                                                     */
                                                    
                                                    #include "rdm.h"
                                                    
                                                    #include "rdmapi.h"
                                                    
                                                    #include "rdmtfsapi.h"
                                                    
                                                    #include "core33Example_structs.h"
                                                    
                                                    #include "core33Example_cat.h"
                                                    
                                                    #include "rdmstartupapi.h"
                                                    
                                                    #include <stdio.h>
                                                    
                                                    #include <string.h>
                                                    
                                                    #include <stdlib.h>
                                                    
                                                    static const char szData[][34] = {
                                                    "John", "James", "Duncan",
                                                    "Bill", "John", /* Inserting this should fail, referential integrity rules
                                                                           violated. */
                                                    
                                                    "Paul", "David"};
                                                    int32_t main_core33ExampleTutorial (int32_t argc, const char *const *argv)
                                                    {
                                                    RDM_RETCODE rc;
                                                        uint32_t ii;
                                                        INFO sInfo;
                                                    RDM_TFS tfs;
                                                    RDM_TABLE_ID tables[] = {TABLE_INFO};
                                                    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)
                                                                {
                                                    /* Override default DURABLE setting to CONSISTENT */
                                                    
                                                    
                                                                }
                                                    if (rc == sOKAY)
                                                                {
                                                                    rc = rdm_dbSetCatalog (db, core33Example_cat);
                                                    if (rc == sOKAY)
                                                                    {
                                                    /* Open the database. */
                                                    
                                                    
                                                    if (rc != sOKAY)
                                                                        {
                                                                            printf ("Can't open the core33Example 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)
                                                                    {
                                                    /* Start an update transaction and lock the table */
                                                    
                                                                        rc = rdm_dbStartUpdate (
                                                                            db, tables, RDM_LEN (tables), NULL, 0, NULL);
                                                                    }
                                                    if (rc == sOKAY)
                                                                    {
                                                    /* Add rows to the info table */
                                                    
                                                    
                                                                        {
                                                                            strcpy (sInfo.mychar, szData[ii]);
                                                                            sInfo._mychar_has_value = RDM_COL_HAS_VALUE;
                                                                            rc = rdm_dbInsertRow (
                                                                                db, TABLE_INFO, &sInfo, sizeof (sInfo), NULL);
                                                                            {
                                                                                printf (
                                                    "%s is a duplicate record.\n", sInfo.mychar);
                                                                                rc = sOKAY; /* This was expected, re-set error code.
                                                                                             */
                                                    
                                                                            }
                                                                        }
                                                                    }
                                                    if (rc == sOKAY)
                                                                    {
                                                    /* Start a read transaction and lock the table */
                                                    
                                                    
                                                                    }
                                                    if (rc == sOKAY)
                                                                    {
                                                    RDM_CURSOR cursor = NULL;
                                                                        INFO_MYCHAR_KEY keyVal = {"", RDM_COL_HAS_VALUE};
                                                    /* Get a key cursor positioned to the first row with the
                                                    
                                                                         * request key value */
                                                    
                                                                        strcpy (keyVal.mychar, szData[3]);
                                                                        rc = rdm_dbGetRowsByKeyAtKey (
                                                                            db, KEY_INFO_MYCHAR, &keyVal, sizeof (keyVal), &cursor);
                                                    if (rc == sOKAY)
                                                                        {
                                                                            rc = rdm_cursorReadRow (
                                                                                cursor, &sInfo, sizeof (sInfo), NULL);
                                                    if (rc == sOKAY)
                                                                            {
                                                                                printf (
                                                    "Row found doing a key lookup: %s\n",
                                                                                    sInfo.mychar);
                                                                            }
                                                    /* Do a closest key match lookup, and return the rest in
                                                    
                                                                             * key order. */
                                                    
                                                    if (rc == sOKAY)
                                                                            {
                                                                                INFO_MYCHAR_KEY keyVal = {"Jim", RDM_COL_HAS_VALUE};
                                                                                rc = rdm_cursorMoveToKey (
                                                                                    cursor, KEY_INFO_MYCHAR, &keyVal,
                                                    sizeof (keyVal));
                                                    /* If we didn't get en exact match move to the next
                                                    
                                                                                 * close key value */
                                                    
                                                    
                                                                                {
                                                                                    rc = rdm_cursorMoveToNext (cursor);
                                                                                }
                                                    while (rc == sOKAY)
                                                                                {
                                                                                    rc = rdm_cursorReadRow (
                                                                                        cursor, &sInfo, sizeof (sInfo), NULL);
                                                    if (rc == sOKAY)
                                                                                    {
                                                                                        printf (
                                                    "Row found doing a key scan lookup: "
                                                    
                                                    "%s\n",
                                                                                            sInfo.mychar);
                                                                                        rc = rdm_cursorMoveToNext (cursor);
                                                                                    }
                                                                                }
                                                    /* We expect to break out of the loop with a
                                                    
                                                                                 * sENDOFCURSOR return code */
                                                    
                                                    
                                                                                {
                                                                                    rc = sOKAY;
                                                                                }
                                                                            }
                                                    /* Free the cursor allocated by rdm_dbGetRowsByKeyAtKey
                                                    
                                                                             */
                                                    
                                                    rdm_cursorFree (cursor);
                                                                        }
                                                    rdm_dbEnd (db);
                                                                    }
                                                    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 (core33ExampleTutorial)
                                                const char * rdm_retcodeGetName(RDM_RETCODE retcode)
                                            Get the mnemonic name for an error or status code.
                                        Header for the native RDM Runtime API.
                                        Header for the RDM 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 RDM database using the specified database handle.
                                        RDM_RETCODE rdm_dbGetRowsByKeyAtKey(RDM_DB db, RDM_KEY_ID keyId, const void *keyValue, size_t len, RDM_CURSOR *pCursor)
                                            Associate an RDM_CURSOR with a row set that is ordered by key value and is initially positioned at th...
                                        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.
                                        const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
                                            Invoke RDM 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 RDM Startup API used by startup macros.