Products Support Documentation Download
Example C-API Core Cursor Application

Schema Compilation

The schema compile process creates generated source files necessary for your application program to access the database defined by the schema.

Generated source files from RDM utilities should never be edited.

For this hello_world example, we will use the schema compiler tool to generate the C-struct definitions that define the rows in each table. We will also use the optional --catalog feature of rdm-compile to create a version of the database catalog we can embed in our application. This embedded catalog will be used to create the database if it does not already exist.

Generating C program interface files:
rdm-compile --c-structs --catalog hello_world.sdl

The --c-structs option creates the examples/hello_world_structs.h file in the current directory. The --catalog option creates the examples/hello_world_cat.h and examples/hello_world_cat.c files in the current directory.

The schema file must ALWAYS have an .sdl extension. The output files from rdm-compile will begin with the same base name as the .sdl file.

Writing the Application

The complete source for this example HelloWorld application can found in tutorials/HelloWorld/hello_worldTutorial_main.c.

Include Files Needed

Application developed in C should always include the rdm.h header file which contains the required prototypes and types for building an RDM database application. The application must also include the generated C-structs header file which contains the table and column definitions. Optionally, the catalog header file will need to be included.

11 #include "rdm.h" /* The RDM API. */
12 #include "hello_world_structs.h" /* The hello_world database definitions. */
13 #include "hello_world_cat.h" /* The hello_world database catalog */
Header for the native RDM Runtime API.
Allocating Handles

The Database handle should NOT be shared across multiple threads within an application process. If multiple threads need access to the same database, it is advised that a separate RDM_DB handle for each thread be allocated.

108  /* Allocate a TFS Handle */
109  rc = rdm_rdmAllocTFS ("", &tfs);
110  if (rc == sOKAY)
111  {
112  RDM_DB db; /* Database Handle */
113  rc = rdm_tfsDropDatabase (tfs, "hello_world");
114  if (rc == sNODB)
115  {
116  /* It is okay if there wasn't a database to drop */
117  rc = sOKAY;
118  }
119  if (rc == sOKAY)
120  {
121  /* Allocate a database hande */
122  rc = rdm_tfsAllocDatabase (tfs, &db);
RDM_RETCODE rdm_tfsDropDatabase(RDM_TFS tfs, const RDM_TCHAR_T *dbNameSpec)
Drop the specified database.
RDM_RETCODE rdm_rdmAllocTFS(const RDM_TCHAR_T *optString, RDM_TFS *phTFS)
Allocate the TFS handle.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:265
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.

The empty string in the above snippet call to rdm_rdmAllocTFS() is where additional configuration options for the TFS can be set. See TFS Configuration Options. For example, setting the docroot to a specific directory on the disk will be done here. For example:

rc = rdm_rdmAllocTFS("docroot=C:\\DataDir", &tfs);
The above example places the docroot in the C:\DataDir directory. This directory MUST exist prior before the TFS engine starts.

The database handle is associated with the TFS handle. Once the handle is allocated, additional database attributes can be set prior to opening the database. Below, after the rdm_tfsAllocDatabase() function successfully returns, the HelloWorld program associates the catalog with the database handle. This step is optional if the database already exists.

Some additional options that can be set using rdm_dbSetOptions() after the database handle is allocated can be found in the Database Configuration Options section.

Opening the Database for Access

With the handles allocated and options set, we are ready to open the database. For this example, we will be opening the database in the "shared" mode which would allow others to open the same database simultaneously (assuming they are all using the same TFS instance).

133  /* Open the database */
134  rc = rdm_dbOpen (db, "hello_world", RDM_OPEN_SHARED);
135  if (rc != sOKAY)
136  {
137  printf ("I can't open the hello_world database.\n");
138  }
RDM_RETCODE rdm_dbOpen(RDM_DB db, const RDM_TCHAR_T *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.

The rdm_dbOpen() function will establish a connection with the TFS and open the hello_world database.

Writing to the Database

Writing to the database is always performed within a transaction. In the example code snippet below, notice the call to start a write transaction, rdm_dbStartUpdate(), which requests a write lock on the INFO table.

At line 76, the C-struct, which defines the row layout, is populated with the data we want to add to the database.

After the successful insert of the INFO row buffer into the database, the call to rdm_dbEnd() commits the changes to the database and releases the locks.

66 /* Insert "helloworld" row to database */
67 static RDM_RETCODE writeARow (RDM_DB db)
68 {
69  RDM_TABLE_ID tables[] = {TABLE_INFO};
72  /* Start an update transaction and lock the table */
73  rc = rdm_dbStartUpdate (db, tables, RDM_LEN (tables), NULL, 0, NULL);
74  if (rc == sOKAY)
75  {
76  INFO infoInserted; /* Row buffer */
78  /* Populate the mychar column in the Row buffer */
79  strcpy (infoInserted.mychar, "Hello World! - using the embedded TFS");
81  /* Insert a row into the table */
82  rc = rdm_dbInsertRow (
83  db, TABLE_INFO, &infoInserted, sizeof (infoInserted), NULL);
84  if (rc == sOKAY)
85  {
86  /* Commit a transaction */
87  rc = rdm_dbEnd (db);
88  }
89  else
90  {
91  /* Abort the transaction */
92  rdm_dbEndRollback (db);
93  }
94  }
95  return rc;
96 }
#define RDM_LEN(x)
Definition: psptypes.h:198
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.
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
RDM status and error return codes.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:265
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.
Reading from the Database

Reading from the database is, also, performed within a transaction. In the example code snippet below, notice the call to start a read transaction, rdm_dbStartRead(), which requests a read lock on the INFO table.

Since the schema for this example database did not define any alternate access methods, we will use the rdm_dbGetRows() function to create a row scan cursor to access the rows directly (rather than through an index or set). The call to rdm_dbGetRows() on line 34 associates the cursor with the INFO table and positions the cursor to the BeforeFirst position.

See Locking and Transaction Start Functions

21 /* Read "helloworld" row to database */
22 static RDM_RETCODE readARow (RDM_DB db)
23 {
24  RDM_TABLE_ID tables[] = {TABLE_INFO};
27  /* Start an update transaction and lock the table */
28  rc = rdm_dbStartRead (db, tables, RDM_LEN (tables), NULL);
29  if (rc == sOKAY)
30  {
31  RDM_CURSOR cursor;
33  rc = rdm_dbAllocCursor (db, &cursor);
34  if (rc == sOKAY)
35  {
36  /* Fill a cursor with all the rows in a table */
37  rc = rdm_dbGetRows (db, TABLE_INFO, &cursor);
38  if (rc == sOKAY)
39  {
40  /* Move to the first row in the info table */
41  rc = rdm_cursorMoveToFirst (cursor);
42  }
43  if (rc == sOKAY)
44  {
45  INFO infoRead; /* Row buffer */
47  /* Read the full content of the current record */
48  rc = rdm_cursorReadRow (
49  cursor, &infoRead, sizeof (infoRead), NULL);
50  if (rc == sOKAY)
51  {
52  printf ("%s\n", infoRead.mychar);
53  }
54  }
55  /* Free the cursor */
56  rdm_cursorFree (cursor);
57  }
58  /* Free the read lock on the table */
59  rdm_dbEnd (db);
60  }
61  return rc;
62 }
#define RDM_LEN(x)
Definition: psptypes.h:198
Definition: rdmtypes.h:266
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
RDM_RETCODE rdm_dbGetRows(RDM_DB db, RDM_TABLE_ID tableId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with rows based on a table id.
RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
Read all columns from a row.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
RDM_RETCODE rdm_dbAllocCursor(RDM_DB db, RDM_CURSOR *pCursor)
Allocate a cursor.
RDM status and error return codes.
RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
Get read locks.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:265
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.