c-core/12_core/core12Example_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 "core12_structs.h"
/* Generated catalog definition to be used with the RDM rdm_dbSetCatalog() API
*/
#include "core12_cat.h"
const char *const description = "Demonstrates transaction commit/rollback";
const RDM_CMDLINE_OPT opts[] = {{NULL, NULL, NULL, NULL}};
RDM_RETCODE openEmptyDatabase (
RDM_TFS *pTFS,
RDM_DB *pDB)
{
rc = rdm_rdmAllocTFS (pTFS);
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (*pTFS);
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_tfsAllocDatabase (*pTFS, pDB);
print_error (rc);
if (rc == sOKAY)
{
/*
* Associate the compiled catalog with the DB handle.
* If the database does not exist on open, it will be created
* in the default DOCROOT directory.
*/
rc = rdm_dbSetCatalog (*pDB, core12_cat);
print_error (rc);
if (rc == sOKAY)
{
rc = rdm_dbOpen (*pDB, "core12", RDM_OPEN_EXCLUSIVE);
print_error (rc);
}
if (rc == sOKAY)
{
*pDB, RDM_LOCK_ALL, 0, NULL, 0, NULL);
print_error (rc);
}
if (rc == sOKAY)
{
print_error (rc);
rdm_dbEnd (*pDB);
}
/* If any of the above DB operations failed, free the DB handle
*/
if (rc != sOKAY)
{
rdm_dbFree (*pDB);
}
}
}
/* If any of the above DB operations failed, free the TFS handle */
if (rc != sOKAY)
{
rdm_tfsFree (*pTFS);
}
}
return rc;
}
void cleanup (
RDM_TFS hTFS,
RDM_DB hDB)
{
/* close the database */
rdm_dbClose (hDB);
/* free the database handle */
rdm_dbFree (hDB);
/* free the TFS handle */
rdm_tfsFree (hTFS);
}
RDM_RETCODE display_offices (RDM_DB hDB)
{
OFFICE office_rec;
RDM_CURSOR cursor = NULL;
/* The following cursor association call will allocate the cursor
* if the cursor is set to NULL. This short-cut can eliminate the
* requirement to call rdm_dbAllocCursor() before using the cursor
* in this function */
rc = rdm_dbGetRowsByKey (hDB, KEY_OFFICE_NAME, &cursor);
print_error (rc);
for (rc = rdm_cursorMoveToFirst (cursor); rc == sOKAY;
rc = rdm_cursorMoveToNext (cursor))
{
/* Read and display the current person record */
rc = rdm_cursorReadRow (cursor, &office_rec, sizeof (office_rec), NULL);
print_error (rc);
printf ("%s\n", office_rec.name);
}
/* free the cursor if it was allocated */
if (cursor)
rdm_cursorFree (cursor);
/* Expect rc to be sENDOFCURSOR when we exit the loop */
if (rc == sENDOFCURSOR)
{
rc = sOKAY;
}
return rc;
}
RDM_RETCODE insertOffices (RDM_DB hDB, const char **officeList, size_t listSize)
{
OFFICE office_rec;
for (int ii = 0; ii < (int) listSize; ii++)
{
strncpy (office_rec.name, officeList[ii], sizeof (office_rec.name));
hDB, TABLE_OFFICE, &office_rec, sizeof (office_rec), NULL);
print_error (rc);
}
return rc;
}
static const char *na_office_names[] = {"Seattle", "Boise", "San Francisco",
"Dallas"};
static const char *emea_office_names[] = {"Paris", "London", "Dublin", "Zurich",
"Madrid"};
#define RLEN(x) (sizeof (x) / sizeof (x[0]))
int main_core12 (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 = openEmptyDatabase (&hTFS, &hDB);
if (rc == sOKAY)
{
rc = rdm_dbStartUpdate (hDB, RDM_LOCK_ALL, 0, NULL, 0, NULL);
print_error (rc);
if (rc == sOKAY)
{
insertOffices (hDB, na_office_names, RLEN (na_office_names));
printf ("\nAll Offices before transaction commit.\n");
display_offices (hDB);
rdm_dbEnd (hDB);
print_error (rc);
if (rc == sOKAY)
{
/* Display all offices */
printf ("\nAll Offices after transaction commit.\n");
display_offices (hDB);
}
}
if (rc == sOKAY)
{
rc = rdm_dbStartUpdate (hDB, RDM_LOCK_ALL, 0, NULL, 0, NULL);
print_error (rc);
if (rc == sOKAY)
{
insertOffices (
hDB, emea_office_names, RLEN (emea_office_names));
printf ("\nAll Offices before transaction commit.\n");
display_offices (hDB);
rc = rdm_dbEndRollback (hDB);
print_error (rc);
/* Display all offices */
printf ("\nAll Offices after transaction rollback.\n");
display_offices (hDB);
}
}
}
cleanup (hTFS, hDB);
}
return (int) rc;
}