core37Example_main.c

CLOB example in C language. This example needs a compiled schema, core37Example.sdl.

/*
* HELLO WORLD
* -----------
*
* This document describes the process to create a simple database,
* insert a record containing a text field, read the text field from
* database and print it out.
*/
#include "rdm.h"/* The RDM API. */
#include "core37Example_structs.h" /* The core01Example database definitions. */
#include "core37Example_cat.h"/* The core01Example database catalog */
#include "rdmstartupapi.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DBNAME "core37"
#define SAMPLETEXT "The quick brown fox jumps over the lazy dog. "
/* Remove the database to cleanup the example */
static void cleanup(RDM_TFS tfs)
{
rdm_tfsDropDatabase(tfs, DBNAME);
}
static RDM_RETCODE updateClob(RDM_CURSOR cursor, RDM_COLUMN_ID col)
{
char buffer[] = { SAMPLETEXT };
uint64_t offset;
size_t bufSize;
/* Setting bufSize to the length of the actual data
* rather than the length of the buffer. For this
* example we are excluding the NULL byte at the end
* of the input string so the strings will be appended
* together.
*/
bufSize = strlen(buffer);
/* Appending characters into CLOB until approximately 2000 characters reached */
for (offset = 0; offset < 2000 && rc == sOKAY; offset += bufSize)
{
rc = rdm_cursorUpdateBlob(cursor, col, offset, buffer, bufSize);
}
return rc;
}
static RDM_RETCODE readClob(RDM_CURSOR cursor, RDM_COLUMN_ID col)
{
char buffer[1000];
uint64_t offset;
size_t bytesOut;
size_t charactersOut;
uint64_t numCharacters;
/* Read entire contents of CLOB using 1000 byte buffer */
rc = rdm_cursorGetClobSize(cursor, col, &numCharacters);
if (rc == sOKAY && numCharacters > 0)
{
/* Only read blob if there are characters stored. A
* zero numCharacters indicates the CLOB is empty.
*/
for (offset = 0; rc == sOKAY; offset += charactersOut )
{
/* in the following, the bytesIn value is reduced by 1 character
* to allow a NULL byte to be appended to the buffer later
*/
rc = rdm_cursorReadClob(cursor, col, offset, buffer, sizeof(buffer)-1, &bytesOut, &charactersOut);
if (bytesOut == 0)
break; /* bytesOut == 0 indicates end of CLOB has been reached */
/* Since we put the string into the CLOB w/o a NULL terminator,
* we need to add it to the end of buffer we are printing.
*/
buffer[bytesOut] = '\0';
printf("%s", buffer);
}
}
return rc;
}
static RDM_RETCODE readARow (RDM_DB db, uint32_t id)
{
RDM_TABLE_ID tables[] = {TABLE_INFO};
/* Start an update transaction and lock the table */
rc = rdm_dbStartRead (db, tables, RDM_LEN (tables), NULL);
if (rc == sOKAY)
{
INFO_ID_KEY idLookup;
RDM_CURSOR cursor = NULL;
/* Find the requested row */
idLookup.id = id;
rc = rdm_dbGetRowsByKeyAtKey(db, KEY_INFO_ID, &idLookup, sizeof(idLookup), &cursor);
if (rc == sOKAY)
{
INFO infoRead; /* Row buffer */
/* Read the full content of the current record */
rc = rdm_cursorReadRow (cursor, &infoRead, sizeof (infoRead), NULL);
if (rc == sOKAY)
{
printf ("ID\t: %08u\n", infoRead.id);
printf ("Model\t: %s\n", infoRead.model);
printf ("Comment\t: \n");
if (infoRead._description_has_value == RDM_TRUE)
{
rc = readClob(cursor, COL_INFO_DESCRIPTION);
}
else
{
puts("**NULL DESCRIPTION**");
}
printf("\n");
}
}
if (cursor)
{
/* Free the cursor */
rdm_cursorFree (cursor);
}
/* Free the read lock on the table */
rdm_dbEnd (db);
}
return rc;
}
static RDM_RETCODE writeARow (RDM_DB db, uint32_t id)
{
RDM_TABLE_ID tables[] = {TABLE_INFO};
RDM_CURSOR cursor = NULL;
/* Start an update transaction and lock the table */
rc = rdm_dbStartUpdate (db, tables, RDM_LEN (tables), NULL, 0, NULL);
if (rc == sOKAY)
{
INFO infoInserted; /* Row buffer */
/* Populate the mychar column in the Row buffer */
infoInserted.id = id;
sprintf(infoInserted.model, "Model #%x", id);
infoInserted._description_has_value = RDM_FALSE;
/* Insert a row into the table */
db, TABLE_INFO, &infoInserted, sizeof (infoInserted), &cursor);
if (rc == sOKAY)
{
uint32_t task = (id % 2);
/* The example will add a Description CLOB to every other ID */
if (task == 0)
{
rc = updateClob(cursor, COL_INFO_DESCRIPTION);
}
}
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd (db);
}
else
{
/* Abort the transaction */
}
}
return rc;
}
int32_t main_core37 (int32_t argc, const char *const *argv)
{
RDM_RETCODE rc; /* Status/Error Return Code */
RDM_TFS tfs; /* TFS Handle */
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
RDM_DB db;
/* Allocate a database hande */
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
/* Associate the catalog with the database handle */
rc = rdm_dbSetCatalog(db, core37Example_cat);
if (rc == sOKAY)
{
/* Open the database */
rc = rdm_dbOpen(db, DBNAME, RDM_OPEN_SHARED);
if (rc != sOKAY)
{
printf("I can't open the " DBNAME " database.\n");
}
else
{
/* insert 10 rows into the database */
for (uint32_t ii = 0; ii < 10 && rc == sOKAY; ii++)
{
rc = writeARow(db, 1000+ii);
}
/* Read ids #4 and #5 */
readARow(db, 1004);
readARow(db, 1005);
}
}
}
cleanup (tfs);
}
}
if (rc != sOKAY)
{
printf (
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
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.
@ RDM_FALSE
Definition: psptypes.h:59
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
RDM_RETCODE rdm_cursorReadClob(RDM_CURSOR cursor, RDM_COLUMN_ID columnId, uint64_t offset, char *value, size_t bytesIn, size_t *bytesOut, size_t *charactersOut)
Read data from a clob column as UTF-8.
struct RDM_CURSOR_S * RDM_CURSOR
Definition: rdmtypes.h:306
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_cursorGetClobSize(RDM_CURSOR cursor, RDM_COLUMN_ID columnId, uint64_t *numCharacters)
Get the current size of a clob column.
RDM_RETCODE rdm_dbClose(RDM_DB db)
Close the database associated with a database handle.
RDM_RETCODE rdm_cursorUpdateBlob(RDM_CURSOR cursor, RDM_COLUMN_ID columnId, uint64_t offset, const void *value, size_t bytesIn)
Update a blob column.
uint32_t RDM_COLUMN_ID
Definition: rdmtypes.h:28
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:97
@ RDM_TRUE
Definition: psptypes.h:60
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_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
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_RETCODE rdm_tfsDropDatabase(RDM_TFS tfs, const char *dbNameSpec)
Drop the specified database.
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.
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.
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_LEN(x)
Definition: psptypes.h:78
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.