core34Example_main.c

Basic tutorial in C where a connection to a database using different Uniform Resource Identifiers (URI) are used. This example needs a compiled schema, core34Example.sdl.

#include "rdm.h"
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "core34Example_cat.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#include <stdlib.h>
int32_t main_uriTutorial(int32_t argc, const char* const* argv)
{
RDM_TFS tfs = NULL;
RDM_DB db = NULL;
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)
{
rc = rdm_tfsAllocDatabase(tfs, &db);
if (rc == sOKAY)
{
rc = rdm_dbSetCatalog(db, core34Example_cat);
}
}
/* Test opening with no URI */
if (rc == sOKAY)
{
/* Open the database using no URI (all default values) */
rc = rdm_dbOpen(db, "core34Example", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
printf("Connected to the remote TFS using all defaults "
"(core34Example)\n");
/* Remove all of the rows from the database, in exclusive
mode we still need to use a transaction */
rc = rdm_dbStartUpdate(db, RDM_LOCK_ALL, 0, NULL, 0, NULL);
rc = rdm_dbEnd(db);
/* Close the database */
}
else if (rc == eTX_CONNECT)
{
printf(
"Could not connect to the remote TFS, make sure one has "
"been started.\n");
}
}
/* Test opening with a URI that specifies the IPv4 TCP/IP transport*/
if (rc == sOKAY)
{
/* Open the database using a TCP/IP URI specification */
rc = rdm_dbOpen(
db, "tfs-tcp://localhost:21553/core34Example",
if (rc == sOKAY)
{
printf("Connected to the remote TFS using tcp/ip "
"(tfs-tcp://localhost:21553/core34Example)\n");
/* Remove all of the rows from the database, in exclusive
mode we still need to use a transaction */
rc = rdm_dbStartUpdate(db, RDM_LOCK_ALL, 0, NULL, 0, NULL);
rc = rdm_dbEnd(db);
/* Close the database */
}
else if (rc == eTX_CONNECT)
{
printf(
"Could not connect to the remote TFS, make sure one has "
"been started.\n");
}
}
/* Test opening with a URI that specifies the Shared Memory transport*/
if (rc == sOKAY)
{
/* Open the database using a Shared Memory specification */
rc = rdm_dbOpen(
db, "tfs-shm://21553/core34Example", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
printf("Connected to the remote TFS using shared memory "
"(tfs-shm://21553/core34Example)\n");
/* Remove all of the rows from the database, in exclusive
mode we still need to use a transaction */
rc = rdm_dbStartUpdate(db, RDM_LOCK_ALL, 0, NULL, 0, NULL);
rc = rdm_dbEnd(db);
/* Close the database */
}
else if (rc == eTX_CONNECT)
{
printf(
"Could not connect to the remote TFS, make sure one has "
"been started.\n");
}
}
/* Test opening with a URI that specifies the default transport*/
if (rc == sOKAY)
{
/* Open the database using the default specification */
rc = rdm_dbOpen(db, "tfs:///core34Example", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
printf("Connected to the remote TFS using default transport "
"(tfs:///core34Example)\n");
/* Remove all of the rows from the database, in exclusive
mode we still need to use a transaction */
rc = rdm_dbStartUpdate(db, RDM_LOCK_ALL, 0, NULL, 0, NULL);
rc = rdm_dbEnd(db);
/* Close the database */
}
else if (rc == eTX_CONNECT)
{
printf(
"Could not connect to the remote TFS, make sure one has "
"been started.\n");
}
}
}
if (rc != sOKAY)
{
printf(
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE(uriTutorial)
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
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_dbEnd(RDM_DB db)
End a transactional operation.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
@ RDM_OPEN_EXCLUSIVE
Definition: rdmtypes.h:254
RDM_RETCODE rdm_dbClose(RDM_DB db)
Close the database associated with a database handle.
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
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:97
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
const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
Invoke RDM error handler.
#define RDM_UNREF(a)
Definition: psptypes.h:45
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
#define RDM_LOCK_ALL
Definition: rdmtypes.h:170
@ eTX_CONNECT
Definition: rdmretcodetypes.h:651
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.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:43
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.