2. Embedded SQL for C : Advanced Processing : Sample Programs : Handler
 
Share this page                  
Handler
This example assumes that the book table was created with the statement:
exec sql create table book (chapter_num integer,
    chapter_name char(50), chapter_text long
         varchar);
This program inserts a row into the book table using the data handler Put_Handler to transmit the value of column chapter_text from a text file to the database in segments. Then it selects the column chapter_text from the table book using the data handler Get_Handler to retrieve the chapter_text column a segment at a time.
/*
** For this example the argument to the datahandlers
** will be a pointer to a HDLR_PARAM structure.
*/
 typedef struct hdlr_arg_struct
{
     char *arg_str;
     int arg_int;
 } HDLR_PARAM;
main()
{
/* Do not declare the datahandlers or the datahandler argument to the ESQL
** preprocessor. The argument passed to a datahandler must be a pointer.
*/
         int Put_Handler();
         int Get_Handler();
    HDLR_PARAM hdlr_arg;
    /*
    ** The indicator variable must be declared to ESQL.
    */
         exec sql begin declare section;
                 short indvar;
                 int chapter_num;
         exec sql end declare section;
    /*
    ** Insert a long varchar value chapter_text into the table book
    ** using the datahandler Put_Handler. The argument passed to the
    ** datahandler is the address of structure hdlr_arg.
    */
    . . .
    . . .
    exec sql insert into book (chapter_num,
        chapter_name,
             chapter_text)
             values (5,'One dark and stormy night',
       datahandler(Put_Handler(&hdlr_arg)));
  . .
    /*
    ** Select the column chapter_num and the long
    ** varchar column chapter_text from the table
    ** book. The Datahandler (Get_Handler) will be
    ** invoked for each non null value of the column
    ** chapter_text retrieved. For null values the
    ** indicator variable will be set to "-1" and the
    ** datahandler will not be called
    */
  . .
  . .
exec sql select chapter_num, chapter_text into
   :chapter_num,
      datahandler(Get_Handler(&hdlr_arg)):indvar
           from book;
 exec sql begin;
      process row...
 exec sql end;
    . . .
}