c-core/02_core/core02Example_main.c
/*
* Raima Database Manager
*
* Copyright (c) 2019 Raima Inc., All rights reserved.
*
* Use of this software, whether in source code format, or in executable,
* binary object code form, is governed by the Raima LICENSE which
* is fully described in the LICENSE.TXT file, included within this
* distribution of files.
*/
#include <stdio.h>
#include <string.h>
#include "example_fcns.h"
#include "rdm.h"
#include "rdmstartupapi.h"
/* Generated \c struct and \c typedef definitions to be used with the RDM APIs
*/
#include "core02_structs.h"
/* Generated catalog definition to be used with the RDM rdm_dbSetCatalog() API
*/
#include "core02_cat.h"
const char *const description =
"Demonstrates inserting a row and reading rows from database";
const RDM_CMDLINE_OPT opts[] = {{NULL, NULL, NULL, NULL}};
RDM_RETCODE insertOneRow (
RDM_DB hDB)
{
CORE02_RECORD core02_input;
rc = rdm_dbStartUpdate (hDB, RDM_LOCK_ALL, 0, NULL, 0, NULL);
print_error (rc);
if (rc == sOKAY)
{
/* create a record in the new database */
strncpy (core02_input.message, "Core02", sizeof (core02_input.message));
hDB, TABLE_CORE02_RECORD, &core02_input, sizeof (core02_input),
NULL);
print_error (rc);
if (rc == sOKAY)
rc = rdm_dbEnd (hDB);
else
rc = rdm_dbEndRollback (hDB);
print_error (rc);
}
return rc;
}
RDM_RETCODE readAllRows (
RDM_DB hDB)
{
CORE02_RECORD core02_output;
RDM_CURSOR cursor = NULL;
rc = rdm_dbStartRead (hDB, RDM_LOCK_ALL, 0, NULL);
print_error (rc);
if (rc == sOKAY)
{
/* allocate a cursor resource */
rc = rdm_dbAllocCursor (hDB, &cursor);
print_error (rc);
if (rc == sOKAY)
{
/* setup the type of cursor navigation to be used */
rc = rdm_dbGetRows (hDB, TABLE_CORE02_RECORD, &cursor);
print_error (rc);
}
if (rc == sOKAY)
{
/* scan and read all created records */
/* one additional record will be created each time you run this
* example */
for (rc = rdm_cursorMoveToFirst (cursor); rc == sOKAY;
rc = rdm_cursorMoveToNext (cursor))
{
/* read the row at the current cursor position */
cursor, &core02_output, sizeof (core02_output), NULL);
print_error (rc);
if (rc == sOKAY)
{
printf ("%s\n", core02_output.message);
}
}
/* We expect rc to be sENDOFCURSOR when we break out of the loop */
if (rc == sENDOFCURSOR)
{
rc = sOKAY; /* change status to sOKAY because sENDOFCURSOR was
expected. */
}
else
{
print_error (rc);
}
}
if (cursor)
{
/* free the cursor resource when we're done */
rdm_cursorFree (cursor);
}
rdm_dbEnd (hDB);
}
return rc;
}
int main_core02 (int argc, const char *const *argv)
{
RDM_TFS hTFS;
RDM_DB hDB;
rc = rdm_cmdlineInit (&cmd, argc, argv, description, opts);
if (rc != sCMD_USAGE)
print_error (rc);
if (rc == sOKAY)
{
rc = exampleOpenDatabase (&hTFS, &hDB, "core02", core02_cat);
if (rc == sOKAY)
{
rc = insertOneRow (hDB);
if (rc == sOKAY)
{
rc = readAllRows (hDB);
}
exampleCleanup (hTFS, hDB);
}
}
return (int) rc;
}