2. Embedded SQL for C : C++ Programming : How to Create ESQL/C++ Programs : How to Create User-Defined Handlers
 
Share this page                  
How to Create User-Defined Handlers
To declare user‑defined handlers in ESQL/C++ programs, you must declare them to the C++ compiler as C functions. For example:
// Function prototype for event handler
extern "C" int event_func(void);
To direct the DBMS to call the handler routine when a database event is raised, your application must issue the following SQL statement:
exec sql set_sql(dbeventhandler=event_func);
You cannot overload a function that you intend to use as a handler routine.
User‑defined handlers (data handlers) for long varchar and long byte I/O require an argument to be passed to the data handler. The argument must be defined as a generic pointer (void *) in the function prototype, and must be cast to the correct data type in the data handler routine.
The following example illustrates the construction of a data handler in C++:
// Handler prototype
// ESQL/C++ requires extern "C"
extern "C" int Put_Handler(void *hdlr_arg);
 typedef struct hdlr_param_
{
       char * arg_str;
       int arg_int;
 } HDLR_PARAM;
 void
main()
{
       HDLR_PARAM hdlr_arg;
       // Connect to the database
       exec sql connect testdatabase;
       exec sql insert into book(idno, long_text) values (1,
          datahandler(Put_Handler(&hdlr_arg)));
       exec sql disconnect;
 }
 
// Argument is declared as a generic pointer
int Put_Handler(void *hdlr_arg)
 {
       exec sql begin declare section;
          char seg_buf[50];
          int seg_length;
          int data_end;
       exec sql end declare section;
 // Cast argument for ESQL/C++
 ((HDLR_PARAM *)hdlr_arg)->arg_int = 0;
 rloop:
   read data from a file
    fill seg_buf and set seg_length
   at end of loop sent data_end to 1
   exec sql put data (segment = :seg_buf,
             segmentlength = :seg_length,
             dataend = :data_end);
 end rloop
       return 0;
 }