× News Cego SysMT Croom Web Statistics Impressum
SysMT Logo
14   Using the C Wrapper API
Back to TOC

Cego also provides a plain C API in addition to the native C++ access interface. The required library libcgwrap is part ot the cego distribution.

To use the C API, you should have compiled and installed the cego C library, the base library, the xml library and the cego client library. All packages are available as opensource at www.lemke-it.com. Please refer to the README information of each package, how to compile and install it.

14.1   Connecting to the database

To establish a connection to a running database, the cego_connect is used. The corresponding database server name, the database port, the network protocol type, tableset and user information have to be provided with the function call. For the network protocol choose either serial or xml, depending on which mode your cego daemon is running on.

#include <stdio.h>
#include "cgwrap.h"

main()
{

    CGDB *cgdb;

    char prot[] = "serial";

    if ( (cgdb = cego_connect("game", 2200, prot, "TS1", "lemke", "lemke", "cgwrap.log")) == NULL )
    {
	printf("Error : %s\n", cgerrmsg);
	exit(1);
    }

    ...

    cego_disconnect(cgdb);

}

The logfile parameter can also be NULL. In this case, no logging is provided from the wrapper.
As a result of the connect call, a database handle is returned in case of a successful connection establishment. Otherwise, a zero pointer is returned and the global variable cgerrmsg is setup with an error message.

14.2   Executing a query

After a database connection has been established succesful, any queries can be executed.

...
if ( cego_query(cgdb, "insert into t1 values ( 34, 'HHH');", 0) != 0)
{
   printf("Error : %s\n", errmsg); 
}
...

If no query results are expected, the third parameter of the cego_query function call can be zero. In general, any insert, update or delete queries are used in this sense.

14.3   Tuple fetching

In case of select queries, a tuple result set must be retrieved. For this, a fetch handle has to be allocated using the cego_allocate_fetch function. The fetch handle is then provided to the cego_query function call. Using the fetch handle and an appropriate value array, the result tuples can be retrieved.

...      
printf("Creating fetch handle ...\n");
CGFetch* cgfetch = cego_allocate_fetch();
      
if ( cego_query(cgdb, "select a, b from t1;", cgfetch) != 0 )
{
   printf("Error : %s\n", errmsg);
   exit(1);
}
	    
printf("Size=%d\n", cego_num_col(cgfetch));

CGVal cgval[2];

int colnum;
while ( ( colnum = cego_fetch (cgfetch, cgval, 2)) > 0 )
{
   int i=0;
   while ( i < colnum )
   {
      if ( cgval[i].type == CG_VARCHAR )
         printf("%s ", cgval[i].val);
      else if ( cgval[i].type == CG_INT )
         printf("%d ", *(int*)cgval[i].val);
       i++;
   }
   printf("\n");
}
printf("Freeing fetch handle ...\n");
cego_free_fetch(cgfetch);
...

14.4   Prepared Statements

For dynamic parameter binding, prepared statements can be used. A placeholder sign ? is used to indicate the paremter slots in the query. After the prepared statement has been created, the paramters can be binded using the cego_bind function.
To the bind call, a variable value of type CGVal with appropriate type and value information has to be provided.

 ...
CGStmt *pStmt;
pStmt = cego_prepare("insert into t1 values ( ? , ? );");

int i=42;
CGVal v1;
v1.type = CG_INT;
v1.len = sizeof(int);
v1.val = &i;
    
char s[20] = "This is a X"; 
CGVal v2;
    
v2.type = CG_VARCHAR;
v2.len = strlen(s);
v2.val = s;
    
cego_bind_in(pStmt, &v1, 1);
cego_bind_in(pStmt, &v2, 2);

if ( cego_execute(cgdb, pStmt, 0) != 0)
{
   printf("Error : %s\n", cgerrmsg); 
}
 ...

14.5   Function reference

A listing and description of all available API functions is given in the following.

Variable Description
extern char cgerrmsg[];
      
Character array, which contains exception messages in case of failed API function calls
Type Description
typedef struct CegoDBHandle CGDB;
      
Database connection handle. The handle is reuturned if a connection coulbe be established and is used to execute subsequent database queries.
typedef struct CegoFetchHandle CGFetch;
      
The fetch handle is used as a row cursor to retrieve result tuples.
typedef struct CegoValue
{
   int type;
   int len;
   void *val;
} CGVal;
      
Query results are stored in array of CegoValue variables.
typedef struct CegoBlobValue
{
   int fileId;
   int pageId;
   int len;
   char *buf;
} CGBlob;
      
Blob values are stored and retrieved using the CegoBlobValue struct.
typedef struct CegoStatement CGStmt;
      
The statement handle is used for prepared statement with parameter binding.
Function Description
 void cego_modlog(char* module, int level);
Set up the log level for a dedicated module, or for all module ( ALL ). Level ist one of CG_LOG_ERROR, CG_LOG_NOTICE, CG_LOG_DEBUG
 CGDB* cego_connect(char *servername,
                   int portno, 
                   char *tsname, 
                   char *user, 
                   char *pass,
                   char *logfile); 
Connect to the given database host, port and tableset with the corresponding user information.
 CGFetch* cego_allocate_fetch();
Allocates a fetch handle, which can be used for select queries.
 void cego_free_fetch(CGFetch *cgfetch);
Frees an allocated fetch handle.
int cego_num_col(CGFetch* cgfetch);
Retrieves the numer of cols for the fetch handle.
CGStmt* cego_prepare (char *stmt);
Prepare a statement for parameter binding. A statement handle is returned.
int cego_bind_in (CGStmt* cgstmt,
        CGVal* cgval,
        int pos);
Bind in parameter for a statement handle to the given position.
int cego_bind_out (CGStmt* cgstmt,
        CGVal* cgval,
        int pos);
Bind out parameter for a statement handle to the given position.
int cego_bind_inout (CGStmt* cgstmt,
        CGVal* cgval,
        int pos);
Bind inout parameter for a statement handle to the given position.
int cego_execute (CGDB* cgdb,
        CGStmt* cgstmt,
        CGFetch* cgfetch);
Execute a prepared statement.
int cego_query (CGDB *cgdb, 
        char *stmt, 
        CGFetch* cgfetch);
Executes a database query using the given database connection. In case of select queries, a valid fetch handle has to be given to the function call.
int cego_fetch (CGFetch *cgfetch,
        CGVal cgval[],
        int numCol);
Fetches the next available result from the fetch handle. Am appropriate CegoValue array has to given with the call. The numCol parameters indicates the size of the value array.
int cego_abort (CGFetch *cgfetch);
Abortes the current fetch.
int cego_reset (CGFetch *cgfetch);
Resets the current fetch to the first row.
int cego_putblob (CGDB *cgdb, CGBlob* b);
Puts a given blob to the database. If successful, value of zero is returned and the corresponding blob reference ( fileId, pageId) are stored to the blob variable. Otherwise, a value of -1 is returned.
int cego_getblob (CGDB *cgdb, CGBlob* b);
Retrieves a given blob from the database. The fileId and pageId fields of the blob variable must be setup to appropriate values. If successful, the data is stored in the blob variable and zero is returned. Otherwise, a value of -1 is returned.
int cego_delblob (CGDB *cgdb, CGBlob* b);
Deletes a given blob from the database. The fileId and pageId fields of the blob variable must be setup to appropriate values. If successful, the blob is deleted and zero is returned. Otherwise, a value of -1 is returned.
char* cego_error ();
Returns the last error stored in the glbal variable cgerrmsg